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.
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:
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:
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 update-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:
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:
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) ... CONFIG_KVM=m
If the result is m or y, you’re good to go. For VIRTIO modules, execute the following:
grep -E "CONFIG_VIRTIO_BLK|CONFIG_VIRTIO_NET|CONFIG_VIRTIO_BALLOON|CONFIG_VIRTIO=|CONFIG_VIRTIO_BLK_SCSI" /boot/config-$(uname -r) ... CONFIG_VIRTIO_BLK=m CONFIG_VIRTIO_BLK_SCSI=y CONFIG_VIRTIO_NET=m CONFIG_VIRTIO=y CONFIG_VIRTIO_BALLOON=y
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 address XXX.XXX.XXX.XXX netmask XXX.XXX.XXX.XXX network XXX.XXX.XXX.XXX broadcast XXX.XXX.XXX.XXX gateway XXX.XXX.XXX.XXX dns-nameservers XXX.XXX.XXX.XXX dns-search somedomain.com 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.
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!