Saturday, July 11, 2015

KVM – a free and yet powerful hypervisor



Setting up a home lab had been a struggle for me because of budget.  A few years back after I passed the VCP 4 and 5 (I passed both within a week because I have to take advantage of the free VCP upgrade exam) I was so happy to have my copy of VMware Workstation.  With the VMware Workstation I can run virtual machine on my Windows 7 server (Dell T110 with 16G memory).  I think a few years back VMware Workstation is the best to offer as a hypervisor for the home lab.

VMware is doing a great job not only on it server virtualization product but also has pushed other hypervisor maker to produce better product.  As a software developer I used Unix as a development platform for a long time.  Lately with my involvement with OpenStack I get to play with Ubuntu a lot more.  The biggest step that I have taken is bought for myself a laptop where I can dual boot Windows 8.1 and Ubuntu desktop.  As I have used Ubuntu more and more I have discovered that KVM is a very powerful hypervisor and yet it is free.

 image source: http://www.linux-kvm.org/kvmless/kvmbanner-logo3.png

How does KVM work?
KVM stands for Kernel-based Virtual Machine. KVM is part of the Linux Kernel since version 2.6.20.  

KVM turns the Linux Kernel into a Type 1 hypervisor.  This is an essential characteristics of KVM.  Difference between Type 1 and Type 2 hypervisor can be found here

In Linux term the hypervisor is known as Virtual Machine Monitor (VMM).

Wikipedia has a good picture on how KVM works:
image source: https://en.wikipedia.org/wiki/Kernel-based_Virtual_Machine#/media/File:Kernel-based_Virtual_Machine.svg

To understand how KVM works there are 3 key concepts that we need to understand:
  1. kvm.ko - a module in the Linux kernel
  2. QEMU - short for Quick Emulator 
  3. Guest Mode
kvm.ko
kvm.ko is the driver in the Linux kernel that interacts with the guest operating system of the virtual machine that is running on the user space of the host operating system.  This driver consists of 3 files:
  • kvm.ko - provides the core virtualization infrastructure
  • kvm_intel.ko - specific to the Intel processor
  • kvm_amd.ko - specific to the AMD processor
It used to be a loadable module and starting from Linux version 2.6.20, this module is part of the mainline module.

On your Linux host machine, with the modprobe command we can see:

atc@atc-OptiPlex-740:~$ modprobe -l | grep kvm
kernel/arch/x86/kvm/kvm.ko
kernel/arch/x86/kvm/kvm-intel.ko
kernel/arch/x86/kvm/kvm-amd.ko


kvm.ko is responsible for switching the host processor into the "Guest" mode.

QEMU



image source: https://dw1.s81c.com/developerworks/mydeveloperworks/blogs/a2674a1d-a968-4f17-998f-b8b38497c9f7/resource/BLOGS_UPLOADED_IMAGES/Screenshot-2012-07-0615%3A53%3A40.png

The correct term should be qemu-kvm.  There is the regular QEMU (Quick Emulation) that does machine emulation to run guest operating system on a Linux machine.  The regular QEMU is making the host Linux machine a Type-2 hypervisor.  KVM make use of the regular QEMU framework to host the guest operating system and make changes to interact with the kvm.ko module in the kernel of the host Linux machine. To install KVM on a Ubuntu system we have to install the qemu-kvm package.  Since qemu-kvm and QEMU are so close together that a lots of people use QEMU as a generic name for the module that runs the guest operating system. 

Guest Mode
Linux has the concept of user mode and kernel mode.  This blog has good information on these 2 modes.  KVM introduce a third mode - Guest Mode:
This is where the guest operating system can have its own user and kernel mode while running at the user mode of the host Linux system under qemu-kvm.

Guest mode is implemented as a hardeare file - /dev/kvm which acts as an interrupter between the actual hardware of the host system and the VMM. When KVM is install you will see:

atc@atc-OptiPlex-740:/dev$ l -lt /dev/kvm
crw-rw----+ 1 root kvm 10, 232 Jul 10 10:33 /dev/kvm



 

                               image source: http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/102/10251/10251f1.jpg

For a more detail description of the guest-mode, The Linux Journal has a good article here.

KVM features
It is true that KVM is free and is efficient but what feature does it offer?  Is KVM enterprise ready?

This KVM page has a list of KVM feature but with a commend saying it is not the complete list of features for KVM.  Live migration is one of the list feature and I believe this is a very important, attractive, fundamental  and necessary feature for KVM to be enterprise ready.

When it comes to Linux and enterprise, Red Hat has its RHEV (Red Hat Enterprise Virtualization) and is being used in various data centers.  Canonical Ltd is the company behind Ubuntu is catching up on its "enterprise ready" distribution.

SUSE which is popular in Europe also has it version of "enterprise ready" distribution.

I will in another blog post compare these 3 versions of Linux distribution that has value-add feature for KVM for commercial use.


Management for KVM
This page has a list of ways to manage KVM.  This page is pretty up-to-date because I see Platform9 being listed.  Platform9 is a new startup that ship product to manage OpenStack.

Most management tool be it web based (e.g. oVirt by Red Hat), GUI based (e.g. virt-manager) or command line (e.g. virsh) used libvirt as the interface to manage KVM.

libvirt
I found an excellent article that explain libvirt in detail.  It explains what libvirt is and lists out some major function of libvirt. In the article it also explains how to install and configure libvirt.

IBM blog has a good article on libvirt. Again a picture is worth a thousand words, this diagram explain how libvirt fit into KVM management:
image source: http://www.ibm.com/developerworks/library/l-libvirt/figure3.gif

How to install KVM
There are lots of blog post covering how to install KVM.  I have followed this post to install KVM on my Ubuntu laptop.  One basic prerequisite to run KVM is that the hardware that your Linux is running supports hardware virtualization.  This can easily be find out by typing this on the terminal of a Linux machine.

 egrep -c ‘(svm|vmx)’ /proc/cpuinfo

If it returns a non zero value, the hardware supports hardware virtualization and we can run KVM on this machine.

For working with KVM guest, this post by Scott Lowe is a good start.

Give KVM a try
Personally, KVM is a very good tool for my home lab.  I believe it is also a good option for commercial deployment of  virtualization or cloud infrastructure.  KVM is the most used hypervisor for OpenStack as of today.  (Note: it seems container is becoming more and more popular and lots of development effort is being put in by individual contributors and vendors to make container "enterprise ready" for the virtualization or cloud infrastructure so let see if container will replace KVM).

3 comments:

  1. Great to know about the virtual machine, i am looking for vmware jobs in hyderabad, So keep on posting like this. Where as it helps me alot.

    ReplyDelete
  2. I got this features comparison document between different hypervisor based replication solutions. Although i am not the publisher of this document so all credit goes to the Zerto team who created it.

    ReplyDelete