Booting from a cdrom in a kvm guest using libvirt

There are a few different reasons you might want to boot from a cdrom, however in my case I wanted to run a livecd so I could resize some partitions in the guest. For reference, the one I used is from http://www.sysresccd.org/

First put the iso for the cdrom somewhere on the host, in my example the file is called systemresucecd-x86-2.2.0.iso and is in the /isos/ directory.

Next edit the guest definition to tell it about this image being mounted as the cdrom. To do this, on the host run

# virsh

Now edit the domain, my example here will be called squeeze (as in debian squeeze), adjust the name to whatever the name of your guest is.

# edit squeeze

This will bring up the xml definition of the host. We need to add a section to tell the guest about the cdrom that looks like this

<disk type='file' device='cdrom'>
  <driver name='qemu' type='raw'/>
  <source file='/isos/systemresucecd-x86-2.2.0.iso'/>
  <target dev='hdc' bus='ide'/>
  <readonly/>
  <address type='drive' controller='0' bus='1' unit='0'/>
</disk>

The main things to note here are that we are adding a 'disk' of type 'file' which will be available as a 'cdrom'. it will be 'readonly' and use the file '/isos/systemresucecd-x86-2.2.0.iso'

Now we just need to tell the guest to boot off the cdrom, to do so, edit the section of the xml and add another boot device before any hard drives you have defined. In my case before, it was

<os>
  <type arch='x86_64' machine='pc-0.12'>hvm</type>
  <boot dev='hd'/>
</os>

which then became

<os>
  <type arch='x86_64' machine='pc-0.12'>hvm</type>
  <boot dev='cdrom'/>
  <boot dev='hd'/>
</os>

Save and exit the file, then shutdown the guest and start it again to pick up the new configuration.

Bring up a vnc console and you should see the boot loader for your iso. If there are any problems, run virsh edit again on the guest and verify that your configuration was saved. If there are any syntax errors virsh will discard them so that is the first thing to check.

If that is ok, double check the iso with

# file /isos/systemresucecd-x86-2.2.0.iso
/isos/systemresucecd-x86-2.2.0.iso: ISO 9660 CD-ROM filesystem data 'sysrcd-2.2.0                   ' (bootable)

If the configuration is ok, and the iso file is ok, then try destroying and starting the guest again.

To stop the machine from booting from the cdrom, edit the virsh configuration and remove the boot device for the cdrom from the os section.