Grant McWilliams

KVM on CentOS

So here's a quick howto on getting KVM to work on CentOS5.x. I'll add more tutorials when I start using KVM more. For now I spend all my time on VirtualBox at work and Xen everywhere else. KVM is maturing fast but still not where Xen is for the most part. It also needs VT support in the CPUs which means not everyone can use it. My 4 core, 8 vcpu Xeon machine in the garage won't run KVM so I'm not spending a lot of time with it right now. One of the reasons I don't use KVM in a production environment is it moves too fast and the distros are just too far behind as you'll see in the howto


1. First you need to see if you're CPU has the needed hardware VT support


egrep 'vmx|svm' /proc/cpuinfo

If you have an Intel CPU with VT support you should get back vmx and svm if it's AMD with VT support. If you get nothing you need to use Xen or VirtualBox as both support Virtualization without the hardware VT support.


2. Install KVM and QEMU


yum install kvm kmod-kvm qemu

The current version of KVM is 36 which is ancient (August 2007) but stable as far as KVM goes. Even with this version I've gotten lockups and strange behavior. The most recent version of KVM is 74 so you can imagine what's been added since version 36. If you want to check it out yourself here's the changelog.

If you're feeling adventurous you can install KVM-66 (April 2008) from the testing repo. I have to warn you though that it's in testing for a reason. Also even though this is drastically newer than 36 it's still 6 months old.

yum install --disablerepo=\* --enablerepo=c5-testing kvm kmod-kvm
yum install qemu

As a side note there is no PAE version of the KVM-66 in the testing repo which definately limits usefullness.


3. We need to load the KVM kernel module now. Which module you load depends on who made your CPU.


If you have an Intel CPU do this

modprobe kvm-intel

But if you have an AMD cpu do this

modprobe kvm-amd

Verify that the kernel module loaded properly by using lsmod.

/sbin/lsmod | grep kvm

Add the user that will be managing kvm to the kvm group

usermod -G kvm -a grant


4. Change permissions on the kvm devices. You won't need to do this if you reboot before using it.


chown root:kvm /dev/kvm
chmod 0660 /dev/kvm


5. Creating your virtual hard drive


Since we installed qemu we can use the qemu-img command to create a virtual disk of 5 GB. This is in the Copy on Write format which is very useful

qemu-img create -f qcow2 Centdisk.img 10G

You can use dd to create a sparse file as well

dd if=/dev/zero of=Centdisk.img count=1 seek=10G

Either of these will create a disk file that will go up to 10GB but not consume the space on the drive until it's filled. Using qemu-img is probably the better way though.


6. Install an OS


qemu-kvm -hda Centdisk.img -cdrom CentOS5.2.iso -m 512 -boot d

This tells kvm to use the 10GB disk we just created and use a .iso file as if it were inserted into the cdrom as well as boot from it. Memory is set to 512 MB.


7. Boot our OS


qemu-kvm -hda Centdisk.img

If all goes well you should see your newly installed OS boot up.