Monday, August 20, 2012

Virtual Machine Management on Ubuntu

Installing virtual machines on a ubuntu server, managing them from a ubuntu desktop, and some other tips.

Table of Contents

  1. Prepare a Ubuntu Server as a Virtual Machine Host
  2. Configure the Bridge Interface on the Host Machine
  3. Create a Virtual Machine
  4. Define a Virtual Machine
  5. Manage Virtual Machines from Ubuntu Desktop
  6. Convert virtual disks from raw format to qcow2
  7. Migrating from VMWare to KVM
  8. How to mount a qcow2 image

1. Prepare a Ubuntu Server as a Virtual Machine Host

host is a system on top of which virtual machines run. Let's see how to prepare a ubuntu server as a virtual machine host.
  • Check whether the CPU supports the hardware virtualization:
    egrep '(vmx|svm)' --color=always /proc/cpuinfo
  • Enable virtualization on the BIOS setup.
  • Install KVM and vmbuilder:
    apt-get install ubuntu-virt-server python-vm-builder kvm-pxe
  • Add the user as which we're currently logged in (root) to the group libvirtd:
    adduser `id -un` libvirtd
    adduser `id -un` kvm
  • Check whether they have been successfully installed:
    virsh -c qemu:///system list
    It should display something like this:
    root@server1:~# virsh -c qemu:///system list
    Id Name                 State

2. Configure the Bridge Interface on the Host Machine

The bridge interface on the host system allows the virtual machines to access the local network (LAN) directly and independently from the host machine, as if they were real machines.
  • First install the bridge utils:
    apt-get install bridge-utils
  • Then add a bridge interface br0 at /etc/network/interfaces
    auto lo
    iface lo inet loopback
    auto br0
    iface br0 inet static
    bridge_ports eth0
    bridge_fd 9
    bridge_hello 2
    bridge_maxage 12
    bridge_stp off
  • Finally restart the networking:
    sudo /etc/init.d/networking restart

3. Create a Virtual Machine

  • Create a directory for the virtual machine on /var/lib/libvirt/images/:
    cd /var/lib/libvirt/images/
    mkdir vm1
    cd vm1/
  • We will use the vmbuilder tool to create VMs, which uses a template to create virtual machines. This template is located in the/etc/vmbuilder/libvirt/ directory and we make a copy of it:
    mkdir -p mytemplates/libvirt
    cp /etc/vmbuilder/libvirt/* mytemplates/libvirt/
  • Define the partitions in the file vmbuilder.partition with a content like this:
    root 1000
    swap 500
  • Create a script called that will be executed when the VM is booted for the first time:
    # This script will run the first time the virtual machine boots
    # It is ran as root.
    # Expire the user account
    passwd -e admin
    # Install openssh-server
    apt-get update
    apt-get install -qqy --force-yes openssh-server
  • Create the script with a content like this:
        # --mirror= \
        # --iso=/data/iso/ubuntu-11.10-server-amd64.iso \
    vmbuilder kvm ubuntu --suite=oneiric --flavour=virtual --arch=amd64 \
        --iso=/data/iso/ubuntu-11.10-server-amd64.iso \
        -o --libvirt=qemu:///system \
        --ip= --gw= \
        --part=vmbuilder.partition --templates=mytemplates \
        --user=admin --name=Admin --pass=Admin \
        --addpkg=vim-nox --addpkg=acpid \
        --firstboot=/var/lib/libvirt/images/vm1/ \
        --mem=256 --hostname=vm1 --bridge=br0
  • Run and do the installation. The disk images will be located in the ubuntu-kvm/ subdirectory of our VM directory
    ls -l /var/lib/libvirt/images/vm1/ubuntu-kvm/
  • Move the created disk image to the directory /images/:
    mkdir -p /images/
    mv ubuntu-kvm/tmpRwPa27.qcow2 /images/vm1.qcow2
  • Edit the file /etc/libvirt/qemu/vm1.xml and modify the path of the image, so that it looks like this:
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/images/vm1.qcow2'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' unit='0'/>
  • Redefine the virtual machine:
    virsh define /etc/libvirt/qemu/vm1.xml

4. Define a Virtual Machine

Whenever the configuration of a virtual machine changes, it should be redefined (which means updating the registry of virtual machines with the latest configurations):
virsh define /etc/libvirt/qemu/vm1.xml
virsh list --all
virsh start vm1

5. Manage Virtual Machines from Ubuntu Desktop

The program virt-manager is a desktop (GUI) application, based on libvirt, which can be used to manage virtual machines even on remote hosts (throughssh). Let's see how to install and use it.
  • Install the virtualization packages:
    sudo apt-get install qemu-kvm libvirt-bin virt-manager
  • Check that it is OK:
    sudo kvm-ok
  • Manage local and remote virtual hosts:
    virsh -c qemu:///system list
    virsh -c qemu+ssh://admin@ list
    virt-manager -c qemu:///system
    virt-manager -c qemu+ssh://admin@

6. Convert virtual disks from raw format to qcow2

The format qcow2 has some advantages with respect to the raw format. For example it can be compressed, it can have snapshots, etc.
  • Use qemu-img convert like this:
    cd /var/lib/libvirt/images/
    qemu-img convert -c -O qcow2 vm1.img vm1.qcow2
  • Modify /etc/libvirt/qemu/vm1.xml like this:
    <disk type='file' device='disk'>
    <driver name='qemu' type='qcow2'/>
       <source file='/var/lib/libvirt/images/vm1.qcow2'/>
       <target dev='vda' bus='virtio'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>

7. Migrating from VMWare to KVM

A virtual machine built with VMWare can be converted to KVM.
  • Install the neccessary packages:
    aptitude install virt-goodies qemu-kvm kvm \
             libvirt-bin ubuntu-vm-builder bridge-utils virt-top
  • Convert the disk image from format vmdk to qcow2:
    qemu-img convert User-PC.vmdk -O qcow2 User-PC.qcow2
  • Convert the vmx file to format xml:
    vmware2libvirt -f User-PC.vmx > User-PC.xml
  • Change also the disk type and source file on User-PC.xml like this:
    <disk type='file' device='disk'>
    <driver name='qemu' type='qcow2'/>
       <source file='/images/User-PC.qcow2'/>
       <target dev='vda' bus='virtio'/>
       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
  • Add this xml file to the VM manager:
    virsh -c qemu:///system define User-PC.xml 
  • Start it:
    virsh start User-PC

8. How to mount a qcow2 image

Sometimes we need to access directly the disk of a virtual machine, without booting it. It can be done by mounting it on the host system, as in the following example.
modprobe nbd max_part=16
qemu-nbd -c /dev/nbd0 image.qcow2
partprobe /dev/nbd0
mount /dev/nbd0p1 /mnt/image

fdisk /dev/nbd0

vgchange -ay vm1

mount /dev/vm1/root /mnt/image
umount /mnt/image
vgchange -an vm1
killall qemu-nbd


  1. Did you try Vagrant ? And also Puppet (Puppet labs)?

  2. Not yet. Can you point out some tutorials about how to use them?