This drove me crazy googling for an answer.
Most results suggested using virsh’s attach-disk command. Trouble is, attach-disk adds a whole new cdrom drive to the guest OS. After several uses our guest OS would have several cdrom drives, each with a once-needed iso mounted. Not exactly what we want.
Want to shut down your guest os, edit its xml file, then restart it every time you need to change disks? Neither do I.
libvirt is an abstraction on top of your virtual machine hypervisor. So libvirt doesn’t actually touch your guests. It just shuffles commands down to the real hypervisor. In my case it’s linux kvm/qemu. I wondered if kvm/qemu had a way to change cdrom disks.
Google says that changing the cd image in qemu requires issuing the eject and change commands. Awesome.
Now we need to pass a command down through libvirt into the nougaty kvm/qemu center. More reading provided me with virsh’s qemu-monitor-command which does just that. Use it like this:
virsh # qemu-monitor-command <domain> "<command>"
By the way—and this will jump up and bite you the first time you try it—but you will need to enclose the command in quotes so virsh passes it to qemu without complaint.
One final thing. We need a device name qemu understands. It’s not the same as you’d find in your libvirt xml files or /dev directory. You could probably figure it out, but it’s much easier to just ask qemu for a list of block devices (e.g. disk drives) like so:
virsh # qemu-monitor-command mirage "info block"
drive-virtio-disk0: type=hd removable=0 file=/home/daoist/mirage/mirage.qcow2 ro=0 drv=raw encrypted=0
drive-ide0-0-0: type=cdrom removable=1 locked=0 file=/home/daoist/iso/en_windows_7_ultimate_with_sp1_x64_dvd_u_677332.iso ro=1 drv=raw encrypted=0
drive-ide0-1-0: type=cdrom removable=1 locked=0 file=/home/daoist/iso/virtio-win-0.1-22.iso ro=1 drv=raw encrypted=0
See how there’s two cdrom drives in there? It’s because I tried the attach-disk technique. From this we can see that the device name we care about is drive-ide0-0-0. Tell qemu to eject the disk and then change to the new media:
virsh # qemu-monitor-command mirage "eject drive-ide0-0-0"
virsh # qemu-monitor-command mirage "change drive-ide0-0-0 /home/daoist/iso/en_office_professional_plus_2010_x86_x64_dvd_515529.iso"
virsh #
Your guest OS now has a new disk in its cdrom drive.