Table of Contents

Workshop libVirt & KVM

Ressources

Software

Required software packages:

apt-get install libvirt-bin qemu-kvm qemu-utils

Graphical user interface:

apt-get install virt-manager

Kernel 2.6.32 and according libvirt highly recommended, e.g. Debian/squeeze and Debian/lenny with backports works fine.

ifaces example

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# bridge for kvm
auto eth0
iface eth0 inet manual

auto vnet
iface vnet inet dhcp
    bridge_ports eth0

Simple demo setup with Grml ISO

cd /dev/shm
wget http://download.grml.org/grml-small_2010.04.iso
ISO=/dev/shm/grml-small_2010.04.iso
cat > grml.xml << EOF
<domain type='kvm'>
  <name>grml-kvm</name>
  <memory>262144</memory>
  <currentMemory>262144</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc'>hvm</type>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='cdrom'>                                                                                  
      <driver name='qemu'/>
      <source file='/grml/isos/grml-small_2010.04.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <alias name='ide0-1-0'/>
      <address type='drive' controller='0' bus='1' unit='0'/>
    </disk>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
    </controller>
    <interface type='bridge'>
      <source bridge='vnet'/>
      <model type='virtio'/>
      <alias name='net0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/9'/>
      <target port='0'/>
      <alias name='serial0'/>`
    </serial>
    <console type='pty'>
    </console>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' autoport='yes'/>
  </devices>
</domain>
EOF
sudo virsh [--connect qemu:///system]

Inside virsh then define and start grml instance:

virsh # define grml.xml
virsh # start grml-kvm

List VNC port:

virsh # vncdisplay grml-kvm
:0

List all defined instances:

virsh # list --all
 Id Name                 State
----------------------------------
 14 grml-kvm             running

List assigned serial console:

virsh # ttyconsole grml-kvm
/dev/pts/8

Connect via serial console (choose the “Serial” boot menu entry under additional entries for “grml-small” in the bootsplash to automatically enable serial console):

sudo -u libvirt-qemu cu -l /dev/pts/8

Connect via VNC:

vncviewer localhost:5900

Possible falltraps while setup:

Verify defined configuration:

define $FOO
dumpxml $FOO

Autostart setup:

autostart $HOST
net-auostart default
net-autostart --disable default
autostart --disable $HOST

Ballooning:

setmem $HOST 256000
<currentMemory> <= <memory> -> but can be overriden via setmaxmem

Overbooking of CPU:

<vcpu>16</vcpu>

Tip: Disable caching in the virtualised instance so just the internal system (the system running inside the virtual machine) uses the caches (add this line inside every <disk> … </disk> environment):

"<driver cache='none'/>"

Serial console access (guest)

Adjust Grub2 configuration /etc/default/grub:

GRUB_CMDLINE_LINUX="console=ttyS0,115200n8"
GRUB_SERIAL_COMMAND="serial --speed=1152200 --unit=0 --word=8 --parity=no --stop=1"

Adjust /etc/inittab for serial console access:

T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100

Tips

Convert file config in the native guest configuration format named by format to a domain XML format:

virsh [--connect qemu:///system domxml-to-native] domxml-to-native qemu-argv /dev/shm/grml.xml

virsh like console access to kvm/qemu:

kvm [...] -monitor stdio

Block device:

qemu-img create -f qcow2 block.img 1G

    <disk type='block' device='disk'>
      <driver cache='none'/>
      <source dev='/dev/shm/block.img'/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
    </disk>