Monday, 28 November 2011

Setting up KVM on Ubuntu 10.04 (Lucid Lynx)

After doing a KVM install on Debian Squeeze and trying to get a VM up and running, the hassle convinced me to go back to Ubuntu and their vm-builder package, which allow ones to create VMs relatively easy once the setup is complete. There is a vm-builder port for Debian, though that only works for building older versions of Ubuntu and I want to run the latest, Ubuntu 11.10 (Oneiric Ocelot).

Starting with a bare-metal Ubuntu 10.04 LTS (Lucid Lynx) 64-bit, below is the list of commands and instructions to install and set up the KVM. Details on these instructions can be read in the Ubuntu community documentation, KVM Installation and KVM Networking:

  • sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils

  • virsh -c qemu:///system list (To verify installation, should have no errors)

  • sudo apt-get install libcap2-bin

  • sudo setcap cap_net_admin=ei /usr/bin/qemu-system-x86_64

  • sudo vi /etc/network/interfaces
    • Original file:
      # The loopback network interface
      auto lo
      iface lo inet loopback

      # The primary network interface
      auto eth0
      iface eth0 inet static
      address 10.10.3.140
      broadcast 10.10.3.143
      netmask 255.255.255.248
      gateway 10.10.3.137

      # default route to access subnet
      up route add -net 10.10.3.136 netmask 255.255.255.248 gw 10.10.3.137 eth0

    • Modified file:
      # The loopback network interface
      auto lo
      iface lo inet loopback

      # device: eth0
      auto eth0
      iface eth0 inet manual

      # The primary network interface
      auto br0
      iface br0 inet static
      address 10.10.3.140
      broadcast 10.10.3.143
      netmask 255.255.255.248
      gateway 10.10.3.137
      bridge_ports eth0
      bridge_stp off
      bridge_fd 9
      bridge_hello 2
      bridge_maxage 12


      # default route to access subnet
      up route add -net 10.10.3.136 netmask 255.255.255.248 gw 10.10.3.137 eth0
      up route add -net 10.10.3.136 netmask 255.255.255.248 gw 10.10.3.137 br0

  • sudo /etc/init.d/networking restart

  • Running ifconfig lists the following interfaces br0, eth0, lo, virbr0

This completes the KVM installation and creation of a bridge for the VMs. Up next is replacement of the vm-builder. The one in the Ubuntu packages is faulty and also will not allow you to install Ubuntu 11.10 (Oneiric Ocelot). So I updated to the latest, downloading the source, building and installing it. The steps below can be found in this accepted answer:

  • sudo apt-get install bzr

  • sudo apt-get install epydoc (big install here, ~400mb)

  • bzr branch lp:ubuntu/vm-builder ubzr-vm-builder

  • cd ubzr-vm-builder

  • fakeroot debian/rules binary

  • sudo dpkg -i ../*vm-builder*.deb


With that, everything is installed and vm-builder is ready to run. The easiest way is to use a script so that vm creation can be set once and repeated as desired. The only changes required being hostname, ip and maybe memory. Obtain the Ubuntu 11.10 64-bit server iso and put it in the same place as the script. The directory I used is ~/vm/basekvm:

  • cd ~/vm/basekvm

  • sudo vi create_vm.sh
    • File:
      #!/bin/bash

      # Configure this before running the command
      HOSTNAME=myhostname
      MEMORY=2048
      IP=192.168.122.10
      # -- End of configuration

      vmbuilder kvm ubuntu \
      --destdir=/var/lib/libvirt/images/$HOSTNAME \
      --ip=$IP \
      --hostname=$HOSTNAME \
      --mem=$MEMORY \
      --suite=oneiric \
      --flavour=virtual \
      --arch=amd64 \
      --iso=/root/vm/basekvm/ubuntu-11.10-server-amd64.iso \
      --mirror=http://de.archive.ubuntu.com/ubuntu \
      --libvirt=qemu:///system \
      --domain=localdomain \
      --part=/root/vm/basekvm/vmbuilder.partition \
      --bridge=virbr0 \
      --gw=192.168.122.1 \
      --mask=255.255.255.0 \
      --user=myusername \
      --name=myname \
      --pass=mypassword \
      --tmpfs=- \
      --addpkg=vim-nox \
      --addpkg=acpid \
      --addpkg=unattended-upgrades \
      --addpkg=openssh-server \
      --firstboot=/root/vm/basekvm/fboot.sh \
      -o

  • sudo chmod 700 create_vm.sh

  • sudo vi fboot.sh (Optional)
    • File:
      # This script will run the first time the virtual machine boots
      # It is ran as root.

      # Expire the user account
      passwd -e administrator

      # Install openssh-server
      apt-get update
      apt-get install -qqy --force-yes openssh-server

  • sudo chmod 777 fboot.sh

  • sudo vi vmbuilder.partition
    • File:
      root 8000
      swap 4000
      ---
      /var 8000

  • cd ~/vm

  • ln -s /var/lib/libvirt/images/ images


The create_vm.sh is basically a template script. You can modify it to accept console input so that you don't need to go and edit the file values, that is left for another time. The symbolic link shows the directory where the VM disk images are located once created. Below is how you would use it to create a VM:

  • sudo cp basekvm/create_vm.sh create_vm_myvmname.sh

  • sudo vi create_vm_myvmname.sh. Edit the HOSTNAME, IP and MEMORY as desired

  • sudo ./create_vm_myvmname.sh

  • virsh start myvmname


And that's it! A VM has been successfully created and started up. Give it a few minutes and then you can log in through ssh using the information in the script. If the ssh is slow to connect, try this.

No comments: