After being a happy Xen user for several years now, I’ve recently had to switch to an alternative virtualization solution. My colleague Arun (@iamclovin) actually struggled for a week with Xen VMs that locked up on Hardy; we’ve had much success with Hardy and Xen before, so we attributed it to a hardware problem since these were our first blade servers.
Out of ideas, we tried Karmic (Ubuntu 9.10) only to discover that Xen support via the apt package system is gone. I went down the path of compiling a paravirt_ops Dom0 kernel (this article was very useful) but ended up deciding the process took far too long despite being successful.
With KVM gaining official support from Ubuntu as the virtualization solution, I ended up ditching Xen and switching to KVM for these new servers on Karmic. The rest of the entry is a step-by-step guide on setting up KVM VMs on a Ubuntu server; I’m putting this down because like all wikis, the Ubuntu KVM wiki has grown a little too organically to be useful.
Preparing a host server for KVM
Update and upgrade apt packages (use your own discretion on whether this is necessary):
aptitude update && aptitude dist-upgrade
Check whether CPU supports hardware virtualization:
egrep '(vmx|svm)' --color=always /proc/cpuinfo
You should see lines with either “vmx” or “svm” highlighted.
If you see a FATAL: Error inserting kvm_intel message during installation, it means that virtualization is not enabled in your machine’s BIOS. You’ll need to reboot your machine, enter the BIOS setup and enable virtualization (you’ll have to hunt for the option).
After enabling virtualization in the BIOS and rebooting, run:
There should be no error shown (in fact, no console response).
Optionally, install virt-top, a top-like tool for your VMs:
aptitude install virt-top
Verify that you can connect to the hypervisor:
virsh -c qemu:///system list
You should see something like this:
Connecting to uri: qemu:///system
Id Name State
Setup a network bridge on the server for VMs. Edit /etc/network/interfaces so it looks like this (use your own IPs):
iface lo inet loopback
iface eth0 inet manual
iface br0 inet static
Make sure that you have a direct console to the server because you’re going to restart networking:
Verify that your changes took place with ifconfig. You should see 2 entries like these:
suite: Version of Ubuntu to install (e.g. karmic, hardy).
cpus: Number of CPUs to assign to VM.
mem: Amount of RAM in MB to assign to VM.
swapsize: Size of swap in MB of VM.
rootsize: Size of root filesystem in MB of VM.
flavour: The “flavour” of kernel to use in the VM. Either “virtual” or “server”.
hostname: Hostname of VM.
ip: IP address of VM.
mask: Netmask of VM.
net: Network of VM.
bcast: Broadcast address of VM.
gw: Gateway of VM.
dns: DNS server(s) for VM.
addpkg: APT packages to install in the VM. openssh-server is needed so that we can login to the VM to setup the virsh console.
user and pass: User account that’s setup for you to access the VM.
dest: Destination directory on server where VM disk image will reside.
If your VM is created successfully, there’ll be a config file for the VM in /etc/libvirt/qemu/ (e.g. /etc/libvirt/qemu/billiejean.xml), and a disk image in the directory specified in the --dest option (e.g. /root/vm-billiejean/disk0.qcow2).
You can verify that it works by starting the VM and SSHing into it (virsh console will not work yet).
virsh start billiejean
Converting Disk Images to LVM Logical Volumes
Now, we have the VM setup but it’s running off a disk image. For better performance, running the VM off a LVM logical volume will optimize disk IO.