Workshop libVirt & KVM

  • by Christian Pointner
  • workshop notes by Michael Prokop

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.

# 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
cd /dev/shm
cat > grml.xml << EOF
<domain type='kvm'>
    <type arch='x86_64' machine='pc'>hvm</type>
    <boot dev='cdrom'/>
  <clock offset='utc'/>
    <disk type='file' device='cdrom'>                                                                                  
      <driver name='qemu'/>
      <source file='/grml/isos/grml-small_2010.04.iso'/>
      <target dev='hdc' bus='ide'/>
      <alias name='ide0-1-0'/>
      <address type='drive' controller='0' bus='1' unit='0'/>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
    <interface type='bridge'>
      <source bridge='vnet'/>
      <model type='virtio'/>
      <alias name='net0'/>
    <serial type='pty'>
      <source path='/dev/pts/9'/>
      <target port='0'/>
      <alias name='serial0'/>`
    <console type='pty'>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' autoport='yes'/>
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

List all defined instances:

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

List assigned serial console:

virsh # ttyconsole grml-kvm

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:

  • i686 vs. x86_64 as arch type
  • missing any defined block devices
  • missing bridge configuration as defined in config, example for /etc/network/interfaces see above

Verify defined configuration:

define $FOO
dumpxml $FOO

Autostart setup:

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


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

Overbooking of CPU:


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'/>"

Adjust Grub2 configuration /etc/default/grub:

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


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'/>

realraum Graz, Brockmanngasse 15, 8010 Graz, realraum - Verein für Technik in Kultur und Gesellschaft