Creating a KVM Guest Using libvirt

Creating a KVM Guest Using libvirt :

 
There are a couple of different ways to create a KVM guest:
  • Manually create the XML definition of the guest, then use virsh define <Name of XML file> to import the definition. You could, naturally, create a new XML definition based on an existing definition and just change a few parameters.
  • Use a libvirt-compatible tool, like virt-install, to create the guest definition.
Here’s a quick example of creating a KVM guest using virt-install
virt-install –name masterimage –ram 1024 –cdrom=/var/lib/libvirt/images/Windows7.iso –disk path=/var/lib/libvirt/images/masterimage.img,size=20 –network bridge=br0 –graphics vnc,listen=0.0.0.0 –noautoconsole
This command creates a KVM guest named “vmname”, with 1024 MB of RAM,  a 20 GB virtual disk (in the default QEMU raw format, thin provisioned so that not all 20 GB are allocated up front), connected to an OS installation ISO at the path specified and using hardware virtualization. The network connection is to a bridge called br0. Access to the console is handled via VNC.

Starting and Stopping KVM Guests

This one is easy:
  • To start a VM, just run virsh start <Name of guest VM> and you’re off to the races.
virsh start masterimage
  • To stop a VM, run virsh shutdown <Name of guest VM> and the guest will start an orderly shutdown.
virsh shutdown masterimage

Changing Guest Configuration

If the VM is shut down, you can use the virsh edit <Name of guest VM>command to edit the XML definition directly.
virsh edit masterimage
If the VM is running, then only certain things can be done. You are supposed to be able to swap floppy or CD/DVD images using the virsh qemu-monitor-command command.

Cold Migrations of KVM Guests

You can probably figure out how this one works by now:
  1. Shut down the guest using virsh shutdown <Name of guest VM>.
virsh shutdown masterimage
  1. Export the XML configuration using virsh dumpxml <Name of guest VM> > <Name of XML file>.
virsh dumpxml masterimage > master.xml
  1. Copy the XML definition you just created and the guest’s disk image (specified in the XML configuration) to the target node.
    i.e., cp master.xml clone.xml
Then now create a new copy-on-write image called clone.qcow2 which uses your original masterimage as your template base image. You don’t need to specify a filesize with this command as it will automatically set your new image to the same size as your base image.
kvm-img create -b masterimage.img -f qcow2   clone.img
  1. Edit the XML configuration (as needed) to reflect any changes in paths.
i.e., change the file path to the newly created clone.img & also the UUID of image
  1. Define the guest on the new node using virsh define <Name of XML file>.
virsh define clone.xml
Obviously, this assumes an image-based disk, not a LVM-backed disk.
While hardly a comprehensive list of all the various operations that might need to be done with a KVM guest.

 

Now using VIRSH .you can control VMs using Virsh list.
To attach CD ROM to VMs:-
virsh attach-disk winxp /var/lib/libvirt/images/samp.iso hdc –type cdrom –mode readonly
To attach Network interface card:-
virsh attach-interface winserv7 –type network –source default –persistent

 

Note:-A copy of the XML file is copied to the /etc/libvirt/qemu directory

Leave a Comment