Tutorial

Virtualisation using KVM and Libvirt

- Aniketh Girish

2018-02-04





Hey fellas,

It feels like almost a decade that I have written some good technical blog posts. I was so caught up with work and life a bit (alot!! :P). So, this post is all about creating a virtual machine using KVM, libvirt. So, here is a step by step process from installing necessary stuff to running your favorite OS on the virtual machine. This post is just targetting Ubuntu/ Debian distro users. I haven't basically tested out others. Surely will come up with others if asked/needed :D.

Let's start with giving out a heads-up by answering some questions:

What is KVM?

Kernel-based Virtual Machine, KVM, is a Kernel-based hypervisor. This is a part of Linux kernel mainline. KVM is a virtualization infrastructure that turns Linux kernel into a hypervisor.

P.S: A hypervisor is a program that allows multiple operating systems to share a single hardware host.

What is libvirt?

So basically, libvirt is an open-source API/ library, which has access or the option to handle/ workaround with system kernel daemon and management tool for managing platform virtualization. KVM is being managed by this library. But this is certainly not limited to KVM alone. VMware, QEMU, and other virtualization technologies are being managed by this library.

Why prefer virtualization with KVM while it's easier to create VM using the Virtual box and other easier options available in town?

For me, it all started when I decided to start contributing code to OpenStack. So, I could say it is something developers prefer and recommend.

Giving an overview altogether, KVM stands apart in multiple aspects. Some noticeable highlights would be, better performance, the higher level of integration it allows, and the cost, obviously, since it is open-sourced,  the maturity and the stability KVM offers is commendable, scalability it brings in is outstanding, and unmatched functionality support.

KVM is typically the most popular choice for users who are concerned about the cost of operating each VM and less interested in enterprise-level features. This rule primarily applies to providers of cloud and host services, who are particularly sensitive to the cost and density of their servers. These users are highly likely to choose open-source hypervisors, especially KVM.

The tight integration with the host OS is one of the most common reasons for developers to choose KVM, especially those who use Linux. The inclusion of KVM in many Linux distributions also makes it a convenient choice for developers. KVM is also more popular among users who are unconcerned about brand names.

okay, now let's move on to the installation of KVM with libvirt(virt-install & virt-manager)

Step 1: Checking hardware compatibility for virtualization

Let's get into Terminal/command line for a while now.

We need to check if your processor supports virtualization or not:

~$ egrep -c '(vmx|svm)' /proc/cpuinfo

if the output of above command is 0 then your CPU does not support hardware virtualization and if the output is 1 or more then it does. Further, if you still need to check your BIOS id virtualization [VT-x in Intel and AMD-v in AMD processor] is enabled or not.

You can also execute the following command to check for KVM compatibility:

~$ kvm-ok

Check the output of the command:

INFO: /dev/kvm exists 
KVM acceleration can be used

If you are with an output something like the above, you are good to go. If you end up with something like the one below,  you can still run virtual machines, but it’ll be much slower without KVM extensions.

INFO: Your CPU does not support KVM extensions
KVM acceleration can NOT be used

But if you see, something like the following then checks your kernel version. KVM works only with Ubuntu-kernel. You might need to manually set your kernel too here.

INFO: /dev/kvm does not exist
HINT: sudo modprobe kvm\_intel modprobe
FATAL: Module msr not found.

Step 2: Installation of KVM

You need to install the following packages.

~$ sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils

Add your username to the group libvirtd

~$ sudo adduser \`username\` libvirtd

Note: Sometimes here you could end up with an error stating, 'Group 'libvirtd' does not exist', So what you need to do is that,

Create a group manually and add the user.

~$ sudo addgroup libvirtd sudo adduser \`username\` libvirtd

After adding the user, it is preferred to reboot the system once, so to have better and effective usage after adding the user to the libvirtd group.

Step 3: Verification of correct installation

Just follow the command:

~$ virsh -c qemu:///system list

This should give output like this:

 Id Name State ———————————-

If this command fails to connect with the hypervisor, then you can check the errors and the solutions here.

Step 4: Restart kernel modules

modprobe -a kvm

Step 5: Install virt-viewer

For GUI instance of the virtualization manager:

~$ sudo apt-get install virt-viewer

Step 6: Install virt-manager

Virt-manager is a GUI application for creating and managing virtual machines.

~$ sudo apt-get install virt-manager

Then you can create the virtual machine from the GUI interface easily using this manager. Or also you could follow the command-line interface for this as well :)

Hope this helps some people as it did for me and made my life lot easier. Will be back with lot more.

cya, peeps :D

Cheers.