QEMUSwitchToLibvirt

From Libvirt Wiki
Revision as of 07:20, 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

Networking

UML-Switch or VDE2

Todo

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'>
  <name>vader</name>
  <uuid>f5b8c05b-9c7a-3211-49b9-2bd635f7e2aa</uuid>
  <memory>393216</memory>
  <currentMemory>393216</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='block' device='disk'>
      <source dev='/dev/mapper/vg-vaderlv'/>
      <target dev='hda' bus='ide'/>
    </disk>
    <interface type='bridge'>
      <mac address='52:54:00:00:01:89'/>
      <source bridge='br0'/>
    </interface>
    <input type='tablet' bus='usb'/> 
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' listen='127.0.0.1'/>
  </devices>
</domain>

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 (127.0.0.1). 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:127.0.0.1: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'/>
    <readonly/>
 </disk>

...to virDomainAttachDevice