Setup KVM virtualization in Linux

Klaus
5 min readAug 31, 2017

--

Virtualization has been around for many years, among current virtualization technologies, KVM, which stands for Kernel-based Virtual Machine is one of the most effective, cost-saving solutions with many features. The first feature is that KVM Virtualization was merged into the Linux kernel mainline in kernel version 2.6.20. It supports both Intel and AMD processors, which makes its implementation can be easily executed in a wide range of systems. Next, KVM uses QEMU for I/O hardware emulation and is managed via the libvirt API and tools. Finally, KVM is open-source and published through GNU licenses.

This article describes a quick and simple implementation of the KVM virtualization in Red Had Enterprise Linux 6.

  1. Prerequisites

1.1. CPU & virtualization support

KVM does not require a super powerful system to run smoothly, however, the more powerful it is, the more virtual machines can be created. Therefore, minimum requirements should be an average Intel or AMD processor, 30GB of space, 4GB of RAM. Another essential is that “Virtualization Technology” must be enabled in the BIOS setup, so please have it checked before proceed. Next, you should check whether your processors support VT, this is not actually a requirement but performance would be much better if the CPU does support VT,

Run the command:

# grep -E ‘svm|vmx’ /proc/cpuinfo

the output below means your CPUs do support VT

vmx is for Intel processors
svm is for AMD processors

1.2 Installation source

Installation OS source for Virtual Machines (VMs) could be an iso file (can be downloaded on distro’s websites) or a remote installation source where we have access such as http, ftp, or nfs. For example, you can use the following URL as a source:

http://centos.mirroring.pulsant.co.uk/7/os/x86_64/

1.3 Disk space

An enough disk space should be reserved for VMs, there are many types of virtual disk, in this article, “raw” disk type would be employed. KVM will create a virtual disk image file on your host (the physical server on which VMs will be installed). For this type of image, disk space will be consumed immediately, in other words, if you set 5GB for your virtual disk, a 5GB file would be created, even if your VM have not used up to that space.

The default path for storing disk image is /var/lib/libvirt/images, however, you can specify it to anywhere you want.

1.4 Networking

This is one of the most crucial part of setting up a virtual machine. By default, your VM only have access to other VMs on the same host, in order for them to connect to your LAN, a bridge networking setup is needed. In addition, you’ll need the following package before configuring networking

#yum install bridge-utils

Then you can follows these steps:

1.4.1 Turn off the Network Manager
Set NM_CONTROLLED=NO in your network configuration file, /etc/sysconfig/network-scripts/ifcfg-em1

# chkconfig NetworkManager off
# chkconfig network on
# service NetworkManager stop
# service network start

1.4.2 Add the following line to your network configuration file

BRIDGE=br0

1.4.3 Create an interface configuration file for your bridge interface, /etc/sysconfig/network-scripts/ifcfg-br0, with the following content:

DEVICE="br0"
# BOOTPROTO is your preference. It can be “dhcp” or “static”.
# If “static”, be sure to specify the IP address, netmask and gateway.
BOOTPROTO="dhcp"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Bridge"
DELAY="0"

1.4.4 For your VMs network traffic could be forwarded, a FORWARD RULE should be added to firewall configurations

# iptables -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
# service iptables save

1.4.5 To enable to forwarding, edit the file /etc/sysctl.conf as follows:

inet.ipv4.ip_forward = 1

then run the command:

# sysctl -p /etc/sysctl.conf

restart the network service

# service network restart

That’s all for the bridge network configuration.

1.5 SELinux

Another thing that requires attention is SELinux (Security Enhanced Linux). For system which have SELinux enabled (enforcing mode), and a non-default directory (other than /var/lib/libvirt/images) selected to save image files, it is necessary to change security context of the new image directory. Suppose that you would store image files in /vm-images. You need to do the following steps:

1.5.1 Install the policycoreutils-python package

# yum -y install policycoreutils-python

1.5.2 Set proper security context for the image directory and its sub directories

# semanage fcontext --add -t virt_image_t '/vm-images(/.*)?'

verity it

# semanage fcontext -l | grep virt_image_t

1.5.3 Restore the security context

# restorecon -R -v /vm-images

verity it:

# ls –aZ /vm-images

to see the out results

drwxr-xr-x. root root system_u:object_r:virt_image_t:s0 .
dr-xr-xr-x. root root system_u:object_r:root_t:s0

2. Installing

2.1 Install KVM packages

# yum install kvm python-virtinst libvirt libvirt-python virt-manager \
virt-viewer libguestfs-tools

After the installation, the daemon libvirtd should be running and the main configuration file is /etc/libvirt/qemu. Then, edit the config file and add the following lines:

user = "root"
group = "root"

2.2 Install a virtual machine (guest OS)

We are going to install a VM which has a name “vm1”, 1GB of RAM, 1 virtual cpu, 10GB HDD space, from the installation source http://centos.webxcreen.org/6/os/x86_64/, and no GUI, just run the command below:

# virt-install \
--network bridge:br0 \
--name vm1 \
--ram=1024 \
--vcpus=1 \
--disk path=/vm-images/vm1.img,size=10 \
--graphics none \
--location=http://centos.webxcreen.org/6/os/x86_64<strong>/</strong> \
--extra-args="console=tty0 console=ttyS0,115200"

An example of installation using iso file:

# virt-install \
--name vm1 \
--ram=2048 \
--vcpus=2 \
--disk path=/vm-images/vm1.img,size=10 \
--cdrom /data/CentOS-7-x86_64-Minimal.iso

The last parameter allow connections to guest OS using serial ports

--extra-args="console=tty0 console=ttyS0,115200"

3. Management

The management of VMs is straightforward and taken care of by virt-manager.

Run the virsh command to access the management console

#virshvirsh # help (view helps)
virsh # list --all (list all virtual machines)

#starting VMs Guest on booting
virsh # autostart [guest-name]
virsh # autostart [guest-name] --disable

#shutdown a guest OS
virsh# shutdown [guest-name]

#force shutdown a guest OS ( turn off power )
virsh# destroy [guest-name]

#start a guest OS
virsh# start [guest-name]

You can also connect to a guest OS using the command

virsh console [guest-name]

or using GUI (only on a host with GUI )

virt-viewer [guest-name]

--

--