Change CD-ROM via virsh

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?


Solution 1:

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

Solution 2:

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

Solution 3:

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>