Creating a Virtual Machine on Linux with KVM, QEMU and Virt

Now a days there are quite a few different options available for virtualization on linux, the most famous ones being:

 

This blog post will be taking about KVM.

A quick summary of which software will be covered here:

KVM – Kernel Virtual Machine
QEMU – Quick Emulator
Virt – The virtualization API

When KVM and QEMU are used in conjunction, the KVM takes care of virtualizing the CPU and memory management while QEMU emulates all the other hardware resources, such as hard-drives, video, cd-rom, peripherals, etc.

Virt is built on top of libvirt, it provides a set of features to manage virtual machines.

1. Checking for support

Before installing any of the software listed above, you first need to check if your hardware supports virtualization.

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

That should output a list of flags if virtualization is enabled on your hardware:

flags        : fpu vme de pse tsc msr pae mce cx8 apic 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 aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 lahf_lm dts tpr_shadow vnmi flexpriority
flags        : fpu vme de pse tsc msr pae mce cx8 apic 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 aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 lahf_lm dts tpr_shadow vnmi flexpriority

If the VMX flag is enabled it means your CPU is Intel, SVM means AMD

2. Installing KVM

After checking if the processor supports virtualization, you can start by installing KVM

yum install kvm kmod-kvm

There are several version of KVM, here is a list explaining which version is suitable for which need

3. Installing QEMU

QEMU is not available on the default repositories enabled on CentOS, you need to enable the rpmforge-extras repository to have access to the QEMU package with yum.

To enable the repository:

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm
rpm -Uhv rpmforge*

Then modify the file:

sudo vim /etc/yum.repos.d/rpmforge.repo

Set the enabled key for the [rpmforge-extrs] to 1

### Name: RPMforge RPM Repository for RHEL 6 - dag
### URL: http://rpmforge.net/
[rpmforge]
name = RHEL $releasever - RPMforge.net - dag
baseurl = http://apt.sw.be/redhat/el6/en/$basearch/rpmforge
mirrorlist = http://apt.sw.be/redhat/el6/en/mirrors-rpmforge
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1

[rpmforge-extras]
name = RHEL $releasever - RPMforge.net - extras
baseurl = http://apt.sw.be/redhat/el6/en/$basearch/extras
mirrorlist = http://apt.sw.be/redhat/el6/en/mirrors-rpmforge-extras
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge-extras
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1

Now you should be able to run:

sudo yum install qemu qemu-kvm

And QEMU should be installed

4. Loading the module

With KVM and QEMU installed, it is time to load the kvm module to start playing with the virtualization tools:

sudo modprobe kvm-intel

You might get the error:

ERRROR:

FATAL: Error inserting kvm_intel (/lib/modules/2.6.32-279.11.1.el6.x86_64/kernel/arch/x86/kvm/kvm-intel.ko): Operation not supported

Well, but we checked before and the CPU supports virtualization, right?
Actually most times the BIOS disable virtualization by default, so you need to modify the BIOS settings yourself.
To enable virtualization is very simple, here is a good tutorial explaining the steps.

**Just restarting the computer didn’t work for me.
I had to shutdown my computer and wait a few minutes for the new BIOS settings to take effect.

After enabaling VT for your CPU, you can go ahead and load the modules again:

sudo modprobe kvm-intel

To check if they were successfully loaded:

lsmod  | grep kvm

You should see something like:

kvm_intel              52890  0
kvm                   314739  1 kvm_intel

5. Adding your user to the KVM group

There are two ways to add your user to the KVM group.
The simples and fastest:

sudo usermod -G kvm -a diogogmt

Or if you prefer:

Side Note:
To check the script that will automatically load the KVM module every time the computer is booted:

cat /etc/sysconfig/modules/kvm.modules

The contents of the file:

#!/bin/sh

if [ $(grep -c vmx /proc/cpuinfo) -ne 0 ]; then
modprobe -b kvm-intel >/dev/null 2>&1
fi

if [ $(grep -c svm /proc/cpuinfo) -ne 0 ]; then
modprobe -b kvm-amd >/dev/null 2>&1
fi

modprobe -b vhost_net >/dev/null 2>&1

exit 0

As you can see it checks if the CPU is Intel or AMD and then loads the appropriate module.

6. Installing Virt

The last step is to install Virt, the software that will allow us to manipulate and configure the virtual machine from a nice rich feature API.

sudo yum install lib-virt python-virtinst virt-manager virt-viewer

After installing the packages above you could restart the computer so the changes take effect or start the libvirt service yourself:

sudo service libvirtd start

7. Running a VM

Now that everything is installed you can test it out by creating a Virtual Machine.

References:
https://fedoraproject.org/wiki/Getting_started_with_virtualization?rd=Virtualization_Quick_Start
http://linux.die.net/man/8/modprobe
http://www.sysprobs.com/disable-enable-virtualization-technology-bios
https://wiki.ubuntu.com/kvm
http://www.campworld.net/thewiki/pmwiki.php/LinuxServersCentOS/Cent6BaseServer
http://en.wikipedia.org/wiki/QEMU
http://www.linux-kvm.org/page/Guest_Support_Status

About these ads

2 thoughts on “Creating a Virtual Machine on Linux with KVM, QEMU and Virt

  1. Oh, also you might want to try “yum groupinstall Virtualization” next time you set it up. That works in Fedora, might work in CentOS too and installs all the dependencies you need so you don’t have to do one package at a time.

Comments are closed.