From Libvirt Wiki
Revision as of 07:24, 11 March 2009 by Bryan (talk | contribs) (Create Domain Manually)
Jump to: navigation, search

Switching to libvirt managed QEMU instances

This page gives tips for migrating from standalone QEMU instances, over to managed libvirt instances.

Walk-through on Ubuntu

This walk-through assumes you start qemu/kvm either manually or from a script. You already have a filesystem (eg: disk image, LVM partition). You are familiar with kvm/qemu networking.

Download libvirt

First, you'll need the libvirt tools:

 apt-get install libvirt-bin libvirt-doc


UML-Switch or VDE2


Bridged Networking

Follow the instructions in the Networking hints and tips.

Create The Domain

If you don't understand what a 'domain' is in libvirt-lingo, see the libvirt Architecture page.

Create Domain Using python-virtinst

Virtinst is a set of commandline tools to create virtual machines using libvirt.

 apt-get install python-virtinst

Now use virt-install to create your virtual machine, as described here. For example, to create a guest domain called 'vader' using a pre-existing LVM partition and KVM:

 virt-install -n vader -r 384 -f /dev/mapper/vg-vaderlv --accelerate --vnc --noautoconsole -v --network bridge:br0

The above command will create the domain definition file /etc/libvirt/qemu/vader.xml, and will attempt to start the domain.

FIXME: The above command will ask for a CDROM and attempt to boot from it. This is unnecessary if a filesystem already exists on /dev/mapper/vg-vaderlv. I bypassed this by setting the CDROM to /dev/null and then restarting the domain after the initial boot failure.

Create Domain Manually

To create a domain called 'vader' using your favorite text editor, create a file called /etc/libvirt/qemu/vader.xml which looks something like the following.

Note the uuid must be unique to this domain; use the 'uuid' command to create a unique (random) uuid.

<domain type='kvm'>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='block' device='disk'>
      <source dev='/dev/mapper/vg-vaderlv'/>
      <target dev='hda' bus='ide'/>
    <interface type='bridge'>
      <mac address='52:54:00:00:01:89'/>
      <source bridge='br0'/>
    <input type='tablet' bus='usb'/> 
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' listen=''/>

For more information, see the domain format documentation.

Domain Control: Start, Stop, Etc.

To list all currently-running domains:

 virsh list

To display info on a specific domain:

 virsh dominfo vader

To start/stop/reboot a domain:

 virsh start vader
 virsh shutdown vader
 virsh reboot vader

To hard-stop a domain (no elegant shutdown):

 virsh destroy vader

Connect to a VNC Console

The above examples connect a VNC terminal to the loopback device ( Pay attention to the port number if you have multiple domains running.

You can connect from the host machine:

 vncviewer localhost

or over the network using ssh port-forwarding. Login to the host:

 ssh deathstar -L 5900:

On the local computer, now run:

 vncviewer localhost

Start The Domain At Boot

Set the 'autostart' flag so the domain is started upon boot:

virsh autostart vader

Elegant Guest Shutdown

To enable elegant shutdown of domains, ensure they respond to ACPI power button presses. On Linux, install acpid in the guest OS.

Command line argument equivalence

Monitor command equivalence

change hdc /some/path/cdimage.iso

From the command line

 virsh block-attach --type cdrom --mode readonly myguest /some/path/cdimage.iso hdc

Or from an API call, pass the following XML

 <disk type='file' device='cdrom'>
    <source file='/some/path/cdimage.iso'/>
    <target dev='hcd'/>

...to virDomainAttachDevice