Virtualization using KVM on Ubuntu

There are a large number of webpages dedicated to getting KVM working. This is just another one outlining experiences in setting this up as a rival to VirtualBox. As KVM seems to be continually evolving it has become a challenge to get things going with the latest versions of software.

This note covers the migration of an existing 32 bit Windows 7 VM on VirtualBox to run under KVM. Note that the changes in the virtual hardware will be seen as sufficient to invoke a demand for activation in the Windows VM, so this is not suitable for OEM versions of Windows (you should be using Linux anyway).

Conversion of a VirtualBox VM to KVM

Before doing anything, if the VirtualBox VM is to be run under KVM, it is best to uninstall the VirtualBox guest extensions. I didn't do that and it still worked but the effects are not really apparent yet.

This is the first task and turns out to be quite straightforward. Firstly convert your vdi file to a raw format. One thing not mentioned on any webistes I've seen so far is to ensure you have heaps of space on the disk. The 22GB vdi file grew to 100GB.

$ VBoxManage clonehd --format RAW win7.vdi win7.raw

Since you are running VirtualBox you will have this command. Install KVM from the repository. Once you do this, VirtualBox will no longer run but it is possible to recover this as shown for example here. Also install virt-manager. There is a Ubuntu-virt which could be installed also. Now convert to the compressed qcow2 format:

$ qemu-img convert -f raw win7.raw -O qcow2 win7.qcow2

This drops the file size back to the size of the vdi file. The raw file can be deleted now (but hang onto the vdi file). Note that KVM does support other file formats, including VMware vmdk formats.

Now note down the VirtualBox settings for the original VM. The architecture (adm64 or i686), the number of CPUs, the memory size and importantly the MAC address on the network adapter.

Use virt-manager now to create a virtual machine using these settings. At this point in time it will start but not boot. A bug in Ubuntu's virt-manager gives the wrong file format (see this posting). Find the name of the VM (win7 in my case) and dump the xml definition.

$ virsh -c qemu:///system list --all

$ virsh -c qemu:///system dumpxml  win7 > win7.xml

Edit and change the mention of raw format to qcow2 in the disk definition part. Delete the VM from virt-manager and redefine it:

$ virsh -c qemu:///system define test.xml

The VM should now boot up. However the drivers need to be fixed, and herein lies another problem.

Video Drivers

Open VM Tools

The video can be improved by using one of the video drivers provided in virt-manager. VMware's open-vm-tools provides a better video driver. This package can be installed on the Ubuntu host from the repositories. In virt-manager select the Video model vmvga. Start the Windows guest with a virtual CD drive pointing the appropriate iso file in this posting (you may need to delete any existing CD drive and install a new one). Then in the Device Manager of Control Panel select the display adapter entry and choose to update the driver using the CD drive. The new display adapter will show as VMware SVGA II.

SPICE QXL

The QXL driver is related to the SPICE project which is an outstanding improvement over other solutions. Setting this up is not really difficult at first sight but at the moment it has rather irksome bugs. In the Windows guest download the drivers mentioned in this post or this. Shutdown the VM and back up the qcow2 file. Change the Video model to qxl and the Display to Spice. The guest probably will not boot at this point; see this bug report (refer to the postings towards the end). This bug is reported for the Ubuntu distribution of QEMU binaries and may not be present in other distros. There are workarounds for this.

In the Ubuntu host install the packages qemu-kvm-spice, python-spice-client-gtk and spice-client-gtk. Then restart virt-manager. The Video model may now need to be changed to Cirrus otherwise the VM will start with a black screen. Note that the video model selected affects the KVM display as well as the guest VM. Now delete the VM, keeping the virtual disk, and recreate it as done above. This new VM should now use kvm-spice as the hypervisor.

In the Windows guest, install the drivers that have been downloaded. Open an elevated command prompt (in the start menu run box type "cmd" and press ctl-alt-enter). Navigate to the location where the drivers have been unzipped, and find the appropriate Windows 7 i686 subdirectory. Note that in the case of a 64-bit Windows 7 installation the following procedure will need modification as the drivers must be signed. Now type the following to install the driver:

pnputil -i -a qxl.inf

Then navigate to the vdagent driver directory and find the appropriate subdirectory for Windows 7 i686. Type the following:

vdservice install

This results in a display driver that will not load. This is because of the enforced use of the Cirrus video model. Now change the Video model to qxl. When the VM is started the display will be a black screen. However when the Windows VM has started the new drivers will take effect and the display will appear. It may be necessary to reboot a couple of times for this to happen. The best way to reboot when faced with a black screen is to use virt-manager's controls. Note that this situation is untenable as it will not be possible to react to pre-startup messages from Windows such as when it is forced to shutdown or it crashes. In that case it is necessary to revert to the Cirrus driver, shutdown normally and go through the startup process again.

There are other packages compiled in an untrusted repository (now somewhat old) which could be used to replace the Ubuntu QEMU, or QEMU could even be compiled with the configure option --enable-spice. My attempts to use the repository only resulted in the VM crashing. This is possibly due to a mismatch with the Ubuntu release. So far I haven't pursued this further.

Other Drivers

Grab the virt-win.iso package (the latest is virt-win-0.15.iso) and mount this as a virtual CDROM. This driver package contains a balloon driver for memory management, a serial driver and a network and storage enhancement drivers.

In the guest navigate to the Windows 7 i686 subdirectories of the CDROM. Then in an elevated command prompt as described above, issue the commands:

pnputil -i -a balloon.inf

pnputil -i -a netkvm.inf

pnputil -i -a vioscsi.inf

pnputil -i -a vioser.inf

Note that there have been some issues with the storage at least which could result in an unbootable Windows. For the storage drivers to be used the disk must have been specified as a virtio disk when creating the VM. It may be possible to modify the VM's xml file, with some experimentation and care, to change the disk model.

Also the vioser driver failed to install but I had already installed one earlier, which may have been a reason.

Sound

Again this is something of a hassle. This post indicates that apparmor is a main culprit by preventing libvirt from working. The brutal suggestion (probably emanating from a frustrated user) is to remove apparmor as root:

# /etc/init.d/apparmor stop

# update-rc.d -f apparmor remove

# apt-get remove apparmor apparmor-utils

Also in the file /etc/libvirt/qemu.conf, enable the user and group entries and add your userid. Also enable the vnc_allow_host_audio and change its value to 1. On reboot of the Ubuntu host the sound appeared but was quite poor in quality. The sound device by default was ich6. When switched to AC97 (which required action in the Windows guest to update the sound driver by searching the Internet), the sound was perfect.

VirtualBox and KVM Together

VirtualBox will not run while KVM is present. To allow VirtualBox to run again remove the kernel modules for KVM:

$ sudo /sbin/rmmod kvm_amd

$ sudo /sbin/rmmod kvm

To replace them without rebooting:

$ sudo /sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm.ko

$ sudo /sbin/insmod /lib/modules/`uname -r`/kernel/arch/x86/kvm/kvm-amd.ko

Note: for an Intel CPU replace "amd" with "intel".

Shared Folders

This does not appear to be possible with KVM as it is with VirtualBox; certainly not as simple. There are a number of posts relating to this question. For Windows guests Samba needs to be installed.

Conclusion

The VM performance under KVM was poor with graphical activity. The Spice project promises better things but isn't quite there yet, with outstanding bugs despite workarounds being available and 3d graphics not yet available (although that is really only needed for games which are inappropriate on a VM anyway). When Spice is eventually installed, the graphics performance certainly matches that of VirtualBox on a modern machine. Clearly VirtualBox was a winner in the area of graphics drivers, as well as being almost transparently simple to use.



Contact: My email address can be constructed from the username "ksarkies" and the domain internode.on.net in the usual way.


First created 20 March 2013
Last modified
20 March 2013
Ken Sarkies 2013