KVM · Virtualization

KVM – Kernel Virtual Machine

According to Wikipedia, KVM(Kernel Based Virtual Machine) is a  virtualization infrastructure for the Linux kernel that turns it into a hypervisor. In simple words KVM is a kernel module that provides the Linux Kernel, the capability to act as a hypervisor.

KVM support was added to Linux kernel in the version 2.6.20 in February 2007.

Requirements for KVM:

  1. Hardware virtualization extension
  2. Linux Kernel 2.6.20 and above( If you are using Linux as the host OS)
  3. Disk space according to drive size of guest OS
  4. Free Memory according to memory size of guest OS

KVM can be installed on the following Operating Systems as of Nov 2015

  • Debian
  • Gentoo
  • CentOS
  • OpenSUSE
  • Ubuntu
  • Arch Linux
  • RHEL
  • SLES
  • FreeBSD
  • Illumos based Operaing Systems
  • SmartOS

KVM installation


# apt-get install kvm


# apt-get install qemu-kvm

Redhat, CentOS and Fedora

# yum install qemu-kvm


# zypper install kvm


Processor check

Initially before starting we would have to check if our processor is capable of running KVM. We can do that by running the following command.

cat /proc/cpuinfo |grep flags

The output will be something like this for an Intel processor. The output shows various extensions of the processor. If the processor supports VT extensions, you should be able to see a flag name “vmx” which has been marked in red below.

flags      : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid


If yours is an AMD processor you should be able to see a output like this.you should be able to see a flag name “svm” which has been marked in red below.

flags      : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf eagerfpu pni pclmulqdq monitor ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt topoext perfctr_nb perfctr_l2 arat cpb hw_pstate npt lbrv svm_lock nrip_save tsc_scale flushbyasid decodeassists pausefilter pfthreshold vmmcall bmi1 xsaveopt

Running a simple virtual machine using KVM

Note: KVM can be run only as a super user i.e. with root privileges.
Create a new image file that emulates the hard drive for the virtual machine.

qemu-img create -f qcow2 test.img 5G

Now let us start a vritual machine now.

kvm -drive file=test.img

The above command start a basic virtual machine with the following resources

  • A single virtual CPU
  • 128 MB of RAM
  • 5 GB Hard drive

If we want to start a VM with our requirements, for example, A single virtual CPU, 512 MB of RAM and 10 GB hard disk, we would have the run the following commands

qemu-img create -f qcow2 test.img 10G
kvm -m 512 -drive file=test.img

Let us see some of the usually used options that can be added to ‘kvm’ command.

-m memory size
-drive Emulated hard disk file
-net nic Add an ethernet controller
-net user Add the ethernet controller in NAT mode
-net tap Add the ethernet controller in Bridged mode
-cdrom Image of the CD that needs to be used in the VM
-boot The 1st device in boot sequence

Let us now try to install a Linux distribution. Here we can try Ubuntu Mate

Use the following command to download the bootable ISO file.

wget http://cdimage.ubuntu.com/ubuntu-mate/releases/15.04/release/ubuntu-mate-15.04-desktop-amd64.iso

The following are the specifications of the VM.

  • 512 MB RAM
  • 10 GB Hard drive
  • 1 VCPU
  • Ethernet card Enabled
  • Ethernet card attached in NAT mode
  • Ubuntu Mate ISO

Start the VM using the following commands.

qemu-img create -f qcow2 Ubuntu-Mate.img 10G
kvm -m 512 -drive file=Ubuntu-Mate.img -cdrom ubuntu-mate-15.04-desktop-amd64.iso -boot d -net nic -net user

Install the operating system using the interactive installer. Once the installation is complete, shutdown the instance and boot it using the following command

kvm -m 512 -drive file=Ubuntu-Mate.img -net nic -net user

Now this is an example of installing the Operation System on KVM virtual machine.

The above command works in a system with GUI installed. If you want to run KVM virtual machines in a system with CLI only, then we would have to modify the command a bit.

kvm -m 512 -drive file=Ubuntu-Mate.img -net nic -net user --no-graphic -vnc :25

Where 25 is arbitrary port number. If this port number is already being used by another application, you can use any other number.
You can now connect to this VM from another machine with VNC client using the following command

vncviewer :25

If you want the internal processor, to which the guest OS is exposed, to be Virtualization enabled, the following command can be used.

kvm -m 2048 -drive file=/home/johnson/Ubuntu-Mate.img -net nic -net user  -cpu phenom,+vmx  -smp 2 --nographic -vnc :25

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s