Child pages
  • Packaged KVM on Debian
Skip to end of metadata
Go to start of metadata


apt-get install qemu-kvm libvirt-bin

This will set the current user as a member of the libvirt group, this allows the current user to manage virtual machines. To add additional users to this group, simply:

useradd [username] libvirt

Note: You will need to log out and back in before changes are effective

Network Configuration

If you have NetworkManager installed, you might first want to remove this:

apt-get remove network-manager

Setting up a bridged network

Now, in order for the VM guests to be externally addressable we will need to bridge the interfaces to eth0 (replace as appropriate). To do so, open up /etc/network/interfaces and edit as follows:

iface eth0 inet manual

auto br0
iface br0 inet static
  bridge_ports eth0
  bridge_stp off
  bridge_maxwait 0
  bridge_fd 0

Now, restart networking:

/etc/init.d/networking restart

ifconfig should now show br0 having the IP address previously assigned to eth0

VLANs for Guest VMs

Allowing different vlans per guest VM is a fairly straightforward task, as we simply require a different bridge per vlan, which we then assign the Guest to depending upon the required Vlan.

Installing required modules

First, ensure we have what we need for vlans:

apt-get install vlan

Edit /etc/modules and add the line:


Now, load the kernel module required:

modprobe 8021q

Configuring VLANs on network interface

Next, we require an eth0.vlan_id and a br0.vlan_id per VLAN. Example for VLAN 50:

iface eth0.50 inet manual
  vlan_raw_device eth0

auto br0.50
iface br0.50 inet static
  bridge_ports eth0.50
  bridge_stp off
  bridge_maxwait 0
  bridge_fd 0

Now, ensure that we have the relevant vlans tagged on the switch feed to the server, and reload the box.

Creating VM Guests

The most simple way of managing the VM guests on a box is using virt-manager. If you do not have a graphical install on the server running KVM, then you can use virt-manager from a remote (desktop) machine - this can be installed easily with:

apt-get install virt-manager

and run using:

virt-manager -c qemu+ssh://[hostname]

Managing Guests

Managing guests is best done via shell, using virsh. A good reference point for virsh would be Redhat's guide

Automating guest creation

Automating a guest creation requires the following:

  • A disk image with a fully installed/configured guest on (template)
  • kpartx to allow mounting of the image file
  • Script to take in parameters and tie everything together


First we must ensure we have kpartx available

apt-get install kpartx

Now, you need to create a VM called debian-template-x (where x is a unique identifier for this template). Install all relevant tools, and configure up as required (use a safe default IP address, this will be replace later on).

We would then need a script to:

  • Take in a name, hostname, ip details (could lookup from hostname) for the VM
  • Copy the guest disk image to a new location, with unique name (from above): /var/lib/libvirt/images/guest-name.img
  • Create the relevant VM, using virt-install with some default options
  • Mount the new VM disk image, with kpartx
  • Modify the relevant configuration files within the new image (/etc/network/interfaces, /etc/hostname, /etc/hosts etc.)
  • Update MAC address in /etc/udev/rules.d/70-persistent-net.rules - or clear/delete file before first boot fixes things well
  • Unmount the VM disk images
  • Start the new VM
  • Cause new Guest to autostart: virsh autostart guest-name
    Just a symlink in /etc/libvirt/qemu/autostart to ../image-name.xml
cp /var/lib/libvirt/images/debian-test.img /var/lib/libvirt/images/[server_name].img

kpartx -a /var/lib/libvirt/images/[server_name]
mount /dev/mapper/loop0p1 /mnt/vm_setup

// Now edit all relevant files in /mnt/vm_setup/etc/.

umount /mnt/vm_setup
kpartx -d /var/lib/libvirt/images/[server_name]

  --name [server_name]
  --ram 1024
  --network bridge=br0.50
  --disk path=/var/lib/libvirt/images/[server_name]
  --os-variant debianlenny

We may also need to take into account VLAN Tagging - which should be fairly straightforward using a bridge interface per vlan, and assigning the guest to the relevant bridge.

Deleting a Guest

To delete a guest requires multiple actions, this could also be scripted:

  • virsh undefine server_name
  • Remove the image file from /var/lib/libvirt/images/.

Considerations to discuss

There are some key factors that will need to be decided upon before deploying the guest template / scripts. Namely:

  • The size of hard drive image assigned to each guest
  • The amount of RAM assigned to each guest by default
  • Generate SSH Keys for each Guest
  • IPv6 - HV shouldn't be involved, as we are bridged networking
  • Kerberos keytab file in the Guest??
  • No labels

1 Comment

  1. You might want to look at I've ran 38 virtual hosts on one physical host, all production systems.