Sunday, January 30, 2011

Creating KVM Guests using virsh tool

An example XML file to install an operating system from an ISO image The following example XML file can be modified to create a KVM and boot to any operating system from an ISO image or a CD-ROM drive.

<domain type='kvm'>
     <type arch='x86_64' machine='pc'>hvm</type>
     <boot dev='cdrom'/>
 <clock offset='localtime'/>
     <disk type='file' device='disk'>
       <source file='/var/lib/libvirt/images/kvm1.img'/>
       <target dev='hda' bus='ide'/>
    <disk type='file' device='cdrom'>
     <source file='/home/askarali/.bb/Fedora-14-x86_64-Live/Fedora-14-x86_64-Live-Desktop.iso'/>
     <target dev='hdc' bus='ide'/>
     <interface type='network'>
      <mac address='54:52:00:2a:58:0d'/>
      <source network='default'/>
     <input type='mouse' bus='ps2'/>
     <graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>

Edit the following before proceeding
a) Each guest needs a universal unique identifier (uuid), You must generate one for your guest by running the following command, and then copy and paste the identifier into the XML file <uuid>HERE</uuid>

# uuidgen

b) Replace <memory>524288</memory> and <currentMemory>524288</currentMemory> tag to memory you want to allocate to your guest.

c) Replace <mac address='54:52:00:2a:58:0d'/> with the mac address, To generate unique mac address, get the python script from

Creating Storage Image file
# qemu-img create -f qcow2 /var/lib/libvirt/images/kvm1.img 10G

Using virsh to create a KVM
1. Define your KVM by running:
# virsh define kvm1.xml

2. Start the KVM so that the installation of its operating system can begin:
# virsh start kvm1

If your KVM does not start, complete one of the following to redefine the KVM:

# virsh edit <Name of KVM>

After editing the definition file, try to start guest vm again

# virsh start vmName

Tips for installing your guest operating system (This should be run on mother host

To Connect from your PC/laptop

1. install virt-viewer
2. Upload your ssh public key to KVM host /root/.ssh/authorized_keys (For this you should allow root login in /etc/ssh/sshd_config)

From your laptop, run

$ virt-viewer -c qemu+ssh://root@KVMHostIP/system vmName

To connect KVMs from the Host

To connect using the virt-viewer tool, run the following command:
# virt-viewer vmName

To connect using the virt-manager tool, run the following command:
# virt-manager vmName

Editing the KVM definition file after operating system installation

To boot to the installed guest after the guest operating system installation is complete, revise the guest
definition so that it will boot from its hard disk drive.

# virsh edit vmName

1.  Replace <boot dev='cdrom'/> line with <boot dev='hd'/> (To boot from hard disk)
2.  and remove the ISO disk definitions are removed because they are no longer needed.

Propagating your KVMs
For example, if your task is to create 10 identical KVMs that have simple configurations, you can propagate existing KVMs rather than manually installing 10 identical KVMs.

1. Stop the template KVM with the virsh tool:

# virsh shutdown templateKVM

2. Make copies of the KVM disk image by using the qemu-image command with the convert option:

# qemu-img convert templateKVM.img -O qcow2 NewKVM1.img

3. Make copies of the templateKVM XML definition file. You can find it in the /etc/libvirt/qemu/directory or you can run the following command to see the definition file.

# virsh dumpxml templateKVM

4. Edit the definition files for name, memory, mac address, source file etc to define the new KVMs. It is important that a unique MAC address is used in the definition file to make sure that the network functions without a problem.

Networking in Bridge Mode

1. Configure the bridge interface on host operating system
2. replace <interface type='network'> section in kvm.xml with the following

<interface type='bridge'>
      <mac address='00:16:3e:56:40:ad'/>
      <source bridge='br0'/>

3. Install the guest OS, after installation configure the IP on guest.

Auto start KVM VMs on Host system boot

The following command will mark the VM for auto start on system reboot

# virsh autostart vmName

1 comment:

  1. This was helpful to me, thanks for posting it. I had to take two additional steps, though that could be down to updated libvirt binaries.
    1. Add to the tag. virtsh create autogenerated a tag with type='raw' set, which makes the disk appear to be sized at the current on-disk size, not enough space to install an OS

    2. Remove the tag from the tag, but add a tag to the cdrom device tag, and a similar one to the disk device tag. Otherwise you can not boot from the disk when you're done installing the OS.

    FWIW, I also realized that the disk image has to go where you put it, or (at least without configuration of additional storage pools), libvirt can not access the disk image.

    I also for some reason had to start the VM as sudo or else the default network could not be accessed. I get a failure of:
    "error: Requested operation is not valid: network 'default' is not active"