KVM: Optimizing performance on virtual machines (VM`s)

After having set up quite a few VM`s in my career, i have picked up a couple of tips on how to get the most power out of your VM`s:

Get new/correct drivers for your VM`s
Remember to make sure that you have all the correct drivers, this is at least important for IO(Disk) and Network devices. Windows has many devices that will work with basic Microsoft drivers, but that does not mean that the performance magically gets awesome. After making sure that the correct drivers were in place, i managed to go from ~900Mbit to 9.9Gbit on a 10gbit-network between a Linux (Red Hat) and Windows 2k8 server. (tested with iperf which exists for both Windows and Linux)

Turn off Power saving options in BIOS / Hardware
More or less all servers, either they being brick servers, home servers or blade servers, have BIOS settings that enables or disables power saving mode. I know from experience that at least all HP blades comes with power saving enabled per default. Turn off this to make sure that your VM`s gets the performance they expect to get. (I have had VM`s simply be sluggish with this feature turned on, turning it off made CPU-performance get normal)

Turn off CPU throttling on the VM host machine
I have also had issues with slow VM`s even when the power options was fixed in BIOS. I then realized that some Linux distributions (Ubuntu) have a default CPU scheduler that throttles down the CPU when it is not needed. After making sure that the host did NOT do this, the VM`s finally started acting as they should. Check out your Linux distributions guides on how to change this.


Any other tips i should add to the list? Feel free to add a comment below! šŸ™‚

Expanding a kVM disk image

Had to expand a KVM virtual machine today. Luckily, that`s pretty straight forward. You simply create a new disk image with the extra size needed, merge it into the original disk and voila. Then you just need to partition in the extra space and you are good to go.


1: Halt your virtual machine.

You need to stop your virtual machine before going wild with the drive. Virsh stop <vm name>, or virsh destroy <vm name> if it somehow wont stop.

2: Create a disk with the extra space needed:

qemu-img create -f raw 5gig.img 5G

3: Merge it into the disk you are working with

cat 5gig.img >> yourdisk.img

4: Boot up and and partition your drive.

Then start up your virtual machine again with virsh start <vm name>. If you use Windows server, all you need to do is to visit disk managent, right click your drive with little free space and choose “extend partition”. The job takes seconds and does not require any reboot.

Mount KVM/Xen disk image / loop device

Working with virtual disks is pretty straight forward. If you have a single partition virtual disk you can mount or fsck the image file directly. It is a bit more work if you have multiple partitions on the disk, but not to worry šŸ™‚

First, map op the partitions on the device:

[root@hostname images]# kpartx -v -a imagename.img
add map loop0p1 : 0 8369802 linear /dev/loop0 63
add map loop0p2 : 0 224910 linear /dev/loop0 8369865

This creates mappings for the device that can be accessed easily with for example fsck:

[root@hostnameĀ /]# fsck.ext3 /dev/mapper/loop0p1
e2fsck 1.39 (29-May-2006)
/: clean, 87953/1046528 files, 588235/1046225 blocks

You can even easily mount the partitions:

[root@hostnameĀ /]#Ā mount -o ro /dev/mapper/loop0p1 /mnt

To clean up after your self (after unmounting any mounted partitions):

[root@hostnameĀ /]#Ā kpartx -d /dev/loop0

SysRq-reboot macro for Blackbox KVM software

A nice way of rebooting a frozen Linux-based server when you do not have a power switch available isĀ to use the SysRq-abilities.Ā (Ubuntu Example.) Blackbox does not have a default macro installed for just that, but you can create a new macro and insert the following:

“<Alt-PRESS><Print Screen><Alt-RELEASE>RSEIUB”

This will create a working macro for writing any data in cache to disk, kill all processes (Sigkill), remount mounted disks and perform a reboot.