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?
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
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
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>
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