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.

[sourcecode language=”bash”]
egrep ‘(vmx|svm)’ –color=always /proc/cpuinfo
[/sourcecode]

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

[sourcecode]
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 constanttsc archperfmon pebs bts repgood aperfmperf pni dtes64 monitor dscpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse41 lahflm dts tprshadow 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 archperfmon pebs bts repgood aperfmperf pni dtes64 monitor dscpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse41 lahflm dts tprshadow vnmi flexpriority
[/sourcecode]

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

[sourcecode language=”bash”]
yum install kvm kmod-kvm
[/sourcecode]

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:

[sourcecode language=”bash”]
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm
rpm -Uhv rpmforge*
[/sourcecode]

Then modify the file:

[sourcecode language=”bash”]
sudo vim /etc/yum.repos.d/rpmforge.repo
[/sourcecode]

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

[sourcecode language=”bash”]
### 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
[/sourcecode]

Now you should be able to run:

[sourcecode language=”bash”]
sudo yum install qemu qemu-kvm
[/sourcecode]

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:

[sourcecode language=”bash”]
sudo modprobe kvm-intel
[/sourcecode]

You might get the error:

ERRROR:

FATAL: Error inserting kvmintel (/lib/modules/2.6.32-279.11.1.el6.x8664/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:

[sourcecode language=”bash”]
sudo modprobe kvm-intel
[/sourcecode]

To check if they were successfully loaded:

[sourcecode language=”bash”]
lsmod | grep kvm
[/sourcecode]

You should see something like:

[sourcecode]
kvmintel 52890 0
kvm 314739 1 kvm
intel
[/sourcecode]

5. Adding your user to the KVM group

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

[sourcecode language=”bash”]
sudo usermod -G kvm -a diogogmt
[/sourcecode]

Or if you prefer:

Side Note:
To check the script that will automatically load the KVM module every time the computer is booted:
[sourcecode language=”bash”]
cat /etc/sysconfig/modules/kvm.modules
[/sourcecode]

The contents of the file:
[sourcecode language=”bash”]
#!/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
[/sourcecode]

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.

[sourcecode language=”bash”]
sudo yum install lib-virt python-virtinst virt-manager virt-viewer
[/sourcecode]

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

[sourcecode language=”bash”]
sudo service libvirtd start
[/sourcecode]

7. Running a VM

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

References:
https://fedoraproject.org/wiki/Gettingstartedwithvirtualization?rd=VirtualizationQuickStart
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