26

I have a KVM virtual machine that is managed via libvirsh. Now I want to use a different ISO image inside the VM.

How do I change the DVD in the virtual drive using virsh?

Nifle
  • 34,203
  • 26
  • 108
  • 137
DerMike
  • 869
  • 3
  • 11
  • 20

4 Answers4

23

In libvirt 0.9.12 and maybe earlier, a command change-media exists:

change-media <domain> <path> [<source>] [--eject] [--insert] [--update] [--current] [--live] [--config] [--force]

Change CD:

change-media guest01 hdb /pool/disc.iso

Eject CD:

change-media guest01 hdb --eject
mss
  • 331
  • 2
  • 4
  • This is the right command when using SATA controller for CD-ROM. – Xdg Oct 09 '16 at 12:41
  • This worked for SATA CDROM: `virsh change-media guest01 sdb /var/lib/libvirt/images/disc.iso` – Alek Apr 25 '21 at 05:00
  • +1. This works on not started vms, and very convenient in case vm cannot start because iso file is missing: virsh start testingvm error: Failed to start domain testingvm error: Cannot access storage file '/ISO/virtio-win-0.1.229.iso': No such file or directory – Arunas Bartisius May 16 '23 at 22:31
19

Add CDROM:

attach-disk guest01 /root/disc1.iso hdc --driver file --type cdrom
--mode readonly

Change CDROM:

attach-disk guest01 /root/disc2.iso hdc --driver file --type cdrom
--mode readonly

Remove CDROM:

 attach-disk guest01 " " hdc --driver file --type cdrom
 --mode readonly
rkthkr
  • 421
  • 5
  • 5
  • Thanks, rkthr. I test this next week and come back here. +1 anyway :-) – DerMike Jun 10 '11 at 08:24
  • 7
    Thanks for tip. For me it works but only if I remove '--driver file' from the command (version of `virsh`: 0.9.2). – Lukasz Stelmach Dec 23 '11 at 20:50
  • 2
    I had to add `--config` to the command. Probably due to the fact that the guest wasn't originally configured with a cdrom. This addressed the following: `error: internal error: No device with bus 'ide' and target 'hdc'. cdrom and floppy device hotplug isn't supported by libvirt`. – user59156 Oct 19 '15 at 15:59
  • 1
    virsh attach-disk virtual.host.name /tank/kvm/iso/ubuntu-16.04.1-server-amd64.iso hda --type cdrom --mode readonly – Xdg Aug 31 '16 at 08:20
  • It seems that the domain must be started to attach a new source for the cdrom. – imz -- Ivan Zakharyaschev Jan 31 '17 at 12:00
  • All I get for this is "Operation not supported: cdrom/floppy device hotplug isn't supported". (virsh version 5.0.0, libvirtd 5.0.0-4). Any idea why that is? – foo Aug 22 '19 at 14:00
  • FYI: I got `error: unsupported configuration: unsupported driver name 'file' for disk 'sdb'` and had to use `--driver qemu` instead – Giel Feb 10 '21 at 12:12
1

I tried the attach-disk command and it didn't work for me. However, I found this doc on fedora which asks you to use the "update-device" command. This worked for me, and you can find it at Attaching and updating a device with virsh. Here are the steps:

  • Create an XML file:

    <backingStore/>
    <target dev='hdc' bus='ide'/>
    <readonly/>
    <alias name='ide0-1-0'/>
    <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    

Make sure you don't have the <source> tag in your definition

  • Update the device:

    virsh update-device <guest name> <XML file name>
    
jww
  • 11,918
  • 44
  • 119
  • 208
anaken78
  • 111
  • 1
  • That would remove the device, not change the CD as the OP asks. It's also badly copied from your link - it's malformed XML as it stands. –  Nov 22 '14 at 12:07
0

First you have to export existing configuration:

virsh dumpxml guest_name > config.xml

Then you have to open file and copy cdrom section and add the line with iso image path like

<source file='some.iso'/>

So the result is something like

<disk type='file' device='cdrom'>
   <source file='some.iso'/>
   <driver name='qemu' type='raw'/>
   <backingStore/>
   <target dev='hdb' bus='ide'/>
   <readonly/>
   <alias name='ide0-0-1'/>
   <address type='drive' controller='0' bus='0' target='0' unit='1'/>
 </disk>

and save it as cdrom.xml.

After that:

virsh update-device guest_name cdrom.xml

#Device updated successfully