How to Install KVM on CentOS 7 / RHEL 7

In this tutorial we are going to learn How to Install KVM on CentOS 7 / RHEL 7. Here are some definitions of some important components.

  • Virtualization is the ability to create a virtual copy of the physical hardware to simultaneously run a software (usually an operating system) different from the one from the physical machine. The virtual copy is called virtual machine and it looks just like a physical machine, with the difference it is, in fact, virtual.
  • KVM is a kernel module that exposes the /dev/kvm interface that can be used to perform hardware-assisted virtualization. This enables virtual machines to leverage fast CPU instructions to perform virtualization.
  • QEMU is a hosted hypervisor that emulates physical hardware such as CPU and network interfaces.
  • QEMU+KVM (sometimes written as QEMU/KVM) is the use of KVM associated with QEMU. QEMU still emulates the hardware but also leverages KVM to execute the guest operating system.
  • Libvirt is a virtualization API capable of managing QEMU+KVM to provide higher level functions such as storage and network management.
egrep '(vmx|svm)' /proc/cpuinfo
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 smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid

If the above command returns with output showing VMX or SVM, then your hardware supports VT else it does not.

How to Install KVM on CentOS 7 / RHEL 7

yum install -y qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils dejavu-lgc-sans-fonts
  • qemu-kvm –  QEMU emulator
  • qemu-img – QEMU disk image manager
  • virt-install –  Command line tool to create virtual machines.
  • libvirt – Provides libvirtd daemon that manages virtual machines and controls hypervisor.
  • libvirt-client – provides client-side API for accessing servers and also provides the virsh utility which provides command line tool to manage virtual machines.
  • virt-viewer – Graphical console
  • bridge-utils – Utilities for configuring the linux ethernet bridge
  • dejavu-lgc-sans-fonts – Variable-width sans-serif font faces, Latin-Greek-Cyrillic subset

Starting libvirtd

In order to start using Libvirt utilities such as Virt Manager you will first need to start and enable Libvirt:

/bin/systemctl enable libvirtd
/bin/systemctl start libvirtd
/bin/systemctl status libvirtd

These commands will start the libvirtd service and enable it at boot.

Verify KVM Installation

lsmod | grep -i kvm
kvm_intel             139116  4
kvm                   388635  1 kvm_intel

Configure Bridge Interface

Before Start creating VMs, let’s first create the bridge interface. Bridge interface is required if you want to access virtual machines from outside of your hypervisor network.

cd /etc/sysconfig/network-scripts/
cp ifcfg-eno16777736 ifcfg-br0

vim ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=static
DEVICE=ifcfg-eno16777736
ONBOOT=yes
BRIDGE=br0

Edit the Bridge file (ifcfg-br0) and set the followings:

vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.100.21
NETMASK=255.255.255.0
GATEWAY=192.168.100.1
DNS1=192.168.100.11

Replace the IP address and DNS server details as per your setup.

Restart the network Service to enable the bridge interface.

/bin/systemctl restart network

Check the Bridge interface using below command :

ip addr show br0

Create Virtual Machine    

Once you have installed KVM and other tools, it is all set to start creating virtual machines. We will look be looking at creating virtual machines both in command-line and graphical mode.

Command Line Mode

virt-install \
--name node \
--ram 2048 \
--disk path=/var/lib/libvirt/images/node.img,size=30 \
--vcpus 4 \
--os-type linux \
--os-variant debianwheezy \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://ftp.jaist.ac.jp/pub/Linux/debian/dists/jessie/main/installer-amd64' \
--extra-args 'console=ttyS0,115200n8 serial'

virt-install \
--name haproxy \
--ram 4096 \
--disk path=/var/lib/libvirt/images/haproxy.img,size=30 \
--vcpus 4 \
--os-type linux \
--os-variant generic \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://jp.archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'

 

Avatar photo

Asif Khan

Responsible and proactive professional with more than 13 years of experience in IT systems, open source software applications, DevOps, Linux systems, and cloud operations. My main goals are to automate things, keep them safe, and make sure they are strong. I am very good at planning and building the infrastructure for services that people really want. I was drawn to the fast-paced world of cloud computing because it has resources that can be scaled up or down as needed. One of my best skills is being able to use a lot of different DevOps tools to set up, release management, and microservices ecosystems, as well as for provisioning, orchestration, and configuration management.