How to setup a KVM on Ubuntu

KVM (Kernel Virtual Machine) is a brilliant visualization software.  Over the last few years, I have installed quite a few hosts and have been keeping notes – so I thought that I would put them up on this blog.

Install Packages
Firstly, you need to install the basic Ubuntu packages.  I use ubuntu-vm-builder which automates most of the tasks.  The command to install everything you need is:

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

Out of interest, KVM doesn’t have it’s own configuration directory. You’ll find the configuration files in:

/etc/libvirt/qemu/

Add Networking Bridge Adaptor
To give your virtual machines access to the network and you need to create a bridge on the host server.  You have already installed bridge-utils – so you just need to edit the network configuration in /etc/network/interfaces file.  If you make a mistake here and restart the networking the server will loose it’s network connection and you will have to use servers virtual console or actually terminal on the server – if it worth double checking everything.  Here is the configuration that I use:

auto br0
iface br0 inet static
address *.*.*.*
netmask *.*.*.*
network *.*.*.*
broadcast *.*.*.*
gateway *.*.*.*
dns-nameservers *.*.*.* *.*.*.*
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_maxwait 0

If you have installed a firewall on the host server (I always use UFW because it’s so easy to use) then you will need to relax the firewall to allow connections for the bridge adaptor.  To do this add the following line to /etc/ufw/before.rules, before COMMIT:

# allow bridge networking
-I FORWARD -m physdev –physdev-is-bridged -j ACCEPT

Installing a New Virtual Machine
To install a new VM you need to use vmbuilder. There are many, many, many options to vmbuilder. To view them all type vmbuilder –help.  The command I use to create the vm’s is shown below.  Obviously adjust everything to suit.

vmbuilder kvm ubuntu -v
–suite=precise
–libvirt=qemu:///system
–arch=amd64 –cpus=1 –mem=1024 –swapsize=4096 –rootsize=204800
–flavour=virtual
–hostname=hostname.co.uk
–ip=*.*.*.*
–mask=*.*.*.*
–net=*.*.*.*
–bcast=*.*.*.*
–gw=*.*.*.*
–dns=’8.8.8.8 8.8.4.4′
–bridge=br0
–mirror=http://archive.ubuntu.com/ubuntu –components=’main,universe’
–addpkg=openssh-server
–addpkg=acpid
–addpkg=acpi-support
–addpkg=linux-image-generic
–user=user
–pass=password
–dest=/somefolder/

Your guest will be built.  This will take some time as the host server has to connect to the Internet and download Ubuntu and install from there.  Once it is complete, if you selected to install the openssh-server package you can SSH into the guest and continue your set up from there.

Starting and Stopping the Guest
After installation you will need to start the VM.  To start and stop the guest you need to use Virsh (which you have already installed).  You can start the Virsh console by typing virsh. Alternatively you can type commands after virsh at the bash shell and it will execute the commands and return to the shell.

To see the list of currently running guests:

virsh list

To start your guest type:

virsh start hostname

To stop a guest it is recommended to connect (via SSH or other) to the guest and use the guest OS’ shutdown command. If that is not possible you can try send the machine an ACPI shutdown command using:

virsh shutdown hostname

In order for Ubuntu VM’s to respond to ACPI shutdown request they need to have acpid and acpi-support packages installed.  If you followed the instructions above when creating the VM, this will have already been installed.   However, you can install them on the VM by running:

sudo apt-get install acpid acpi-support

If the guest doesn’t have ACPI enabled or doesn’t want to respond you can forcefully shutdown the machine by using the destroy command.  This has to be the most worryingly named command – but don’t worry it only forcefully stops the VM.  You run it by issuing:

virsh destroy hostname

If the guest hostname is not recognised, stop and restart the libvirt-bin service using:

sudo service libvirt-bin stop
sudo service libvirt-bin start

For reasons unknown it is not sufficient to simply restart the service, it has to be stopped and started. Fortunately the running VMs do not shutdown when stopping the libvirt-bin service.

 

2 thoughts to “How to setup a KVM on Ubuntu”

  1. Note to cut and paste ninjas: double hyphens are mangled in the entry above (and possibly will be in this comment) to a single character. Options like –user, –addpkg and so on should be typed with two hyphens at the start.

  2. I have added –addpkg=linux-image-generic because from otherwise from Ubuntu 13.10 onwards you get this error: This kernel does not support a non-PAE CPU.

Leave a Reply