Virtualization with KVM – Installation and configuration

KVM ultimate setup - Featured Image

Virtualization is a technology that let’s you run multiple simulated virtual machines (guests) on top of a single physical hardware machine (host). Physical machine (host) is equipped with the hypervisor which handles physical resources like CPU, memory and storage and then distributes them to the virtual machines (guests). Guests are receiving resources they need when they need them, and everything is handled by hypervisor operators. This article will guide you trough setup of a Kernel-based Virtual Machine hypervisor, better known as KVM.


Before you start, I strongly recommend reading the following “Best Practices” document by IBM. It will give you a nice insight in KVM virtualization features.

Virtualization is a pretty old concept in computing world dating back to 1960’s, while it’s breakthrough to mainstream waters came in late 1990’s with the release of first “software emulator” programs like SoftPC and Virtual PC. At the same time a company named VMWare was established, which further more improved a mainstream popularity of virtualization and currently holds the position of a global market leader in virtualization domain. KVM on the other hand came a bit later. It is specifically developed for Linux on x86 hardware and it  consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko. This article will describe how to set it up on your computer, so let’s get started.

KVM Virtualization – Pre-install system checks

An installation of KVM hipervisor is pretty much simple and trivial on today’s modern Linux systems. To gain it’s full potential we first need to check if our computer and operating system are compatible with all the features offered by KVM. Once again I strongly recommend to read IBM document on KVM Best Practices where all the terms used below are explained in details.

KVM can both be installed on server and desktop Linux systems, so if you are lacking either, the following two articles can help you with that. One is for Server install and other for Desktop.

Next thing you need to check (although not mandatory) is if your CPU supports hardware virtualization. To see if this is true, execute the following from the console:

grep -E -c '(vmx|svm)' /proc/cpuinfo

If the result is 0 your CPU most probably does not support hardware virtualization, though the option for it may simply be disabled in your BIOS. To make sure, reboot your computer, enter BIOS and search for hardware virtualization options. Options for it are usually found in Processor/Chipset/Advanced CPU Configuration/Northbridge or similar submenus. If the check result is larger than 0 you’re all set, though a BIOS check is still required, just to be sure.

You may have some old hardware you would like to use for KVM installation running a 32bit CPU. This will limit your RAM allocation to max 2GB per virtual machine. Although situation like this is highly unlikely, execute the following to see if this is the case:

grep -E -c ' lm ' /proc/cpuinfo

If the result is 0, you have a 32bit CPU. Anything greater than 0 and you’re fine with 64bit CPU.

Same RAM allocation limitation goes if your kernel is 32bit. This may be a bit more common now days than 32bit CPU and to be sure, execute the following:

uname -m

If the result is x86_64 you’re fine to go. Now if you have a 32bit kernel and previous test indicates you have a 64bit CPU, just replace 32bit kernel with 64bit one.

Next option to check is nesting. Although not mandatory, it is required if you plan to run another hypervizor inside a virtual machine. To check if it’s enabled, execute the following:

cat /sys/module/kvm_intel/parameters/nested

This option is most probably disabled. To enable it, just pass kvm-intel.nested=1 option to GRUB boot loader. On Debian based systems, open the /etc/default/grub file in text editor and paste that option to line containing GRUB_CMDLINE_LINUX=” “. Paste it in between the double quotes and don’t forget to update grub. You can also use sed to do it:

sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="kvm-intel.nested=1"/g' /etc/default/grub

After a reboot, nesting will be enabled.

Now check if zone reclaim mode is disabled. In most cases that should be so and to make sure, execute the following:

cat /proc/sys/vm/zone_reclaim_mode

If the result is anything other than 0, zone reclaim mode is enabled, so execute the following to disable it:

echo 0 > /proc/sys/vm/zone_reclaim_mode
sed -i "\$a# Disable zone reclaim\nvm.zone_reclaim_mode=0" /etc/sysctl.conf

When talking virtualization, swappiness can cause serious performance issues, if it occurs. To cope with it, we need to tune a value for it so that it is set to minimum. Turning it off completely is not a good idea as it can cause running processes going out of memory (OOM), which is very nicely described in this article. To check the actual value, execute the following:

cat /proc/sys/vm/swappiness

In most cases value will most probably be 60. We need to set this value to lowest possible minimum of 1. To do it, execute the following:

echo 1 > /proc/sys/vm/swappiness
sed -i "\$a# Disable swappiness\nvm.swappiness=1" /etc/sysctl.conf
KVM Virtualization – Kernel modules

With most today modern Linux distributions, both KVM and VIRTIO kernel modules should be enabled and included in kernel. There may be cases where a custom built kernel is used for specific purposes and those modules may not me included and/or enabled. In cases like that a kernel will have to be re-compiled to enable those modules.

To check if those modules are enabled we can execute the following in our terminal. For KVM module, execute the following:

grep "CONFIG_KVM=" /boot/config-$(uname -r)

If the result is m or y, you’re good to go. For VIRTIO modules, execute the following:


Also, if the results are m or y, you’re good to go.

KVM Virtualization – KVM Installation

If all the checks from above passed, we can finally install a KVM hypervisor. On Ubuntu, execute the following in your terminal:

aptitude update
aptitude install -y -R ubuntu-virt-server libvirt-bin qemu-kvm qemu-system qemu-utils virtinst

With KVM installed, you can go ahead and create your first virtual machines. Please bare in mind, you will only be able to use a NAT network, unless you define a bridge to your physical network adapter. For instance, if you have a router or a dedicated DHCP server to which your physical machine is connected, you will not be able to use IP addresses from those sources without a bridge. To create a bridge, please modify your /etc/network/interfaces file so it looks something like this:

# The primary network interface
auto eth0
iface eth0 inet manual

# Bridge interface
auto br0
iface br0 inet static
broadcast XXX.XXX.XXX.XXX
dns-nameservers XXX.XXX.XXX.XXX
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0

In most cases a default NAT network will suffice. If you installed KVM on a desktop system and you don’t like managing KVM with default virsh CLI, you can install a management UI called Virtual Machine Manager:

aptitude -y -R install virt-manager

You can simply run it from your Start Menu and manage all your virtual machines from there.

Virtualization KVM - Virtual Machine Manager
Virtualization KVM – Virtual Machine Manager

To make all this easier for you, there is a script you can download here; it will run all the checks mentioned above and install KVM hypervisor all in one shot. Thank you for reading and happy virtualising!

Spread the knowledge...

Author: Zack

Zack can cook and administer systems. He likes to discuss both and share knowledge and wisdom to everyone interested. So go ahead and give him hello.

Leave a Reply

Your email address will not be published. Required fields are marked *