Menu

How to install and configure KVM on Ubuntu Server

NOTE: This guide has been tested on Lucid Lynx 10.04 & Karmic Koala 9.10 although it may work equally well on earlier or later version of Ubuntu.

Overview

KVM (Kernel-based Virtual Machine) is a virtualisation technology that allows you to run multiple operating systems, including multiple instances of the same operating system, concurrently on the same physical computer. It is not the same as a "dual boot" setup where you choose which operating system you want to run. With KVM the operating systems can all be running at the same time and you can access them all and use them all just like they were physically separate computers.

When I first started reading up about virtualisation (or virtualization as some people prefer to spell it) I quickly concluded that there were two types of users of virtualisation technology: 1. Large corporations running huge data centres and 2. Geeks at home sitting in their bedrooms pretending they were running large data centres!
As a result I dismissed the technology as being of little use to me. However, I kept coming across it more and more.
Now, here I am sitting here with my own server which is running more than half a dozen virtual machines on it simultaneously and I honestly can't believe I managed without it!

And I guess that just about sums up virtualisation. It is either a must have or is something you can't for the life of you think you'd ever want to implement in your own environment.

So, let me give you a quick overview of my setup so you can decide whether you want to do the same or not.

My server is running Ubuntu Server. So, it's a headless system with very little in the way of extra packages installed on it. I now rarely touch this core installation except for applying the occasional patch.

On top of this core installation I'm running a virtual instance of Ubuntu Server. I'm using this as my main media server now. Previously the media server was running on the core machine.
I'm also running a clone of this media server. I use this clone when I want to install new packages. I can test them on here without doing damage to the "live" instance. When I'm happy that everything is working properly I simply replace the "live" instance with my new instance.

I'm also running a few virtual installations of Windows XP & Vista. These were previously installed on full-blown desktops which I had dotted around the house. I now use "thin clients" to connect to these virtual machines so everything is now centralised. There are a couple of reasons I did this:
1. I have some applications which only work on XP and some applications which only work in Vista. Instead of having separate computers running each version of Windows I now just have the one.
2. Anyone can connect to "their" copy of windows from any client in the house.

I'm also currently running a virtual instance of Ubuntu Desktop 10.4, just to test it.

Plus I have an instance of Ubuntu Server just so I can play with it to learn more about it.

Previously I was running my media server on the core OS. So it had all the packages needed for a media server installed on it: samba, squeezebox server, MusicIP, MediaTomb, MythTV, ps3 media server, deluge, FlexGet. You name it I had it. If I did anything that "broke" the server then I'd have to wipe the OS drive completely and start over. Given my whole family now rely on this server for their home entertainment I was becoming very unpopular every time I took the server down for a few days to rebuild it. That never happens now because I'm only "dabbling" with a backup copy of the media server. I used to actually run a copy of Ubuntu on a separate machine but given the hardware was not identical to my main machine I could never really be sure that I was testing like with like.

Finally, if you've already tried building your own media server then you'll no doubt have had quite a few "false starts" along the way requiring you to wipe the drive and start over. You'll know how time-consuming it can be to get everything back exactly the way it was, even if you're simply doing a restore from a backup. Now with my virtual machines it only takes a matter of seconds to drop a broken image and build a new one.

Just a quick point that is worth mentioning before we get stuck in. None of the "user data" (eg. music files, video files, photos) is stored inside these virtual machines. All user data is stored on the Host itself and is made available to the virtual machines. This is done via NFS for my Ubuntu machines and via samba for the Windows machines. It is definitely not recommended to store this sort of data inside a virtual machine for several reasons: If you replicate the virtual machine then you're also going to be replicating that user data and if you've a lot of data then that's a lot of pointless replication. Secondly, storing user data inside a virtual machine defeats one of the main advantages of having them in the first place and that is the ability to replace them at your leisure. You don't want to have to worry about whether you've backed everything up or not. By all means back up the entire virtual machine, but store all your user data centrally so other machines can access it and you can back it up just the once.

So, assuming you've got this far and you now actually want to go ahead and install some virtual machines then here's how to do it:

Does your computer actually support virtualisation?

Before you start you must check that your computer actually supports virtualisation. If it doesn't then all is not list. You can still use something like VirtualBox to create your virtual machines. It doesn't offer the same level of performance, functionality or flexibility as KVM but is still a very useful application.

So, to check your computer, or more accurately your CPU, supports virtualisation then issue the following command either from a Putty Session or from a Terminal (Applications -> Accessories -> Terminal). You could of course do this directly from the command line on your server:

sudo egrep '(vmx|svm)' --color=always /proc/cpuinfo

If no output is produced then you will be unable to use KVM although, as mentioned above, you can still use something like VirtualBox. For information, my server produces the following output from the above command:

~$ sudo egrep '(vmx|svm)' --color=always /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm tpr_shadow vnmi flexpriority
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm tpr_shadow vnmi flexpriority

If the output from your machine is similar to the above output then we're good to go. So we now need to install some packages by typing the following command:

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

Now is a good time to reboot your machine. So:

sudo reboot -h now

Once your machine has rebooted you can run a quick test to check the installation of KVM has been successful:

virsh --connect qemu:///system list --all

You should see something similar to the following.

         Connecting to uri: qemu:///system
         Id Name State
         ----------------------------------

If you receive an error along the following lines:

virsh -c qemu:///system list
libvir: Remote error : Permission denied
error: failed to connect to the hypervisor

then you need to add your username to a couple of groups by issuing the following two commands:

sudo adduser YourUserNameHere libvirtd

followed by

sudo adduser YourUserNameHere kvm

where YourUserNameHere is your username, obviously. Then reboot for the changes to take effect:

sudo reboot -h now

How to create a Network Bridge

If you would like your virtual machines to be visible on your network and have them accept connections from other computers and devices on your network, so they appear as tho they were distinct physical machines, then you need to create what is called a Network Bridge. This process will "share" the physical network adapter on your host computer with each of the virtual machines. See here for a guide: Create a network bridge. NOTE: If you do not care for this functionality then you can skip this step.

How to create a virtual machine

Let's quickly clear up a bit of terminology before we get stuck in. Virtual machines are called "guests" and the machine the guests are running on is called the "host". Well, you can actually run virtual machines within virtual machines and so a guest can also be a host, but as long as you keep in mind that a guest runs inside a host then you'll be fine.

So, with that out of the way there are two ways to create a guest machine: One way is to create the machine using a small script and the other way is to create it using a graphical application. Let's tackle the former approach first, the script.

Create a virtual guest using a script

Create the script via Putty as follows:

  • I've written a starter script already so highlight the whole script here, right click and select Copy.

  • Using Putty navigate into the folder where you're going to store the script.
    For example type cd /home/xxx/MyScripts where xxx is your username.

  • Next type vim MyVMBuilder.sh (or your preferred script name) and press Enter. This will open a new file for editing.

  • Then press the [Insert] key once and add a couple of blank lines by pressing the [Enter] key. Next right click and the whole script will be pasted into the file.

  • Then press the [Esc] key once and type :wq to save and quit out of the script. If you make a mistake then issue :q! instead of :wq to abort your changes.

  • Don't forget to make the script executable: chmod a+x MyVMBuilder.sh

As explained in the script itself you should change a few of the arguments to suit your environment. Once you're happy you execute the script as follows:

sudo ./MyVMBuilder.sh

Create a virtual machine (vm) using a graphical user interface (gui)

Whilst it is fine to create VMs using a script I did hit a problem which made me rethink my strategy: There is currently a bug when cloning a VM which means the cloned machine cannot be seen on your network. So given this, and the fact that it is much easier to use a gui, I now use the gui to create and manage my virtual machines.

The first thing you need to do is install a graphical user interface on your server. I have detailed how to do this here: Install VNC. Once you've installed a gui on your server then you can then go ahead and install the package to manage your VMs graphically. It's called Virtual Machine Manager:

sudo apt-get install virt-manager

Virtual Machine Manager has a wizard for creating new VMs which makes creating them a breeze. You can also modify, start and stop VMs using this tool although personally I prefer to use the command line to start and stop them. Using the command line allows you to automate the starting and stopping of VMs.

How to start a VM

Although there is a setting within Virtual Machine Manager to automatically start a VM it doesn't work properly, at least not at the time of writing. Fortunately there is a simple command you can issue to start a VM manually. So, you can ether run this command manually each time you wish to start a particular VM or alternatively you can use a little script I've created. I run this script via a cron job to start a VM each time the host machine boots. See the bottom of this page for details of how to create a cron job to run this script.

So, to manually start a VM issue the following command:

virsh --connect qemu:///system start TheVMYouWishToStart

where TheVMYouWishToStart is the name of the VM you wish to start. To get a list of the VMs you've already created issue the following command:

virsh --connect qemu:///system list --all

This will return output similar to this:

     $ virsh --connect qemu:///system list --all
    Connecting to uri: qemu:///system
    Id Name State
    ----------------------------------
    2 MediaServer    running
    3 WindowsXP1    running
    4 UbuntuServer   shut off
    .....
    .....

So, to start the machine you've called UbuntuServer issue the following command:

virsh --connect qemu:///system start UbuntuServer

to stop it then use shutdown instead of start in the above command. However, by default you cannot shut down an Ubuntu guest gracefully.

How to shut down an Ubuntu guest gracefully

By default, when you create an Ubuntu guest, either using vmbuilder or by using the Virtual Machine Manager wizard, you cannot shut down the machine gracefully using either the virsh command (detailed above) or by using the Virtual Machine Manager. You can of course "kill" the machine if you want to stop it but this approach is not recommended since it forcibly halts the machine and thus can cause data loss and corruption. So, to allow the machine to be shut down gracefully we simply need to install a little package called acpid:

sudo apt-get install acpid

Note: One of the arguments in the VM creation script I've provided above installs this package for you so you do not need to install it again.

How to clone a VM

Cloning a VM is explained here: How to Clone a VM and things to watch out for

Still stuck? Not what you were looking for? Then head over to the Discussion Forum