KVM on CentOS 7

kvm icon

kvm on centos 7

So, I hope everyone of you are doing great. This post has taken more time than I thought and I apologize for that. I was provided a server in office and was told to get some kind of virtualization setup on it for my colleagues so that they can have their own Linux based virtual machines for testing purposes. I am more of a OpenVZ guy and working with KVM was one hell of a ride. Why KVM? First of all, I was told that installation of CentOS 6 over i7 processor did not went so well, hence they had CentOS 7 up and running on this beast. To add more to it, there is no OpenVZ kernel for EL 7 variant yet, so I knew from day one that I need to get my hands dirty. Well, why not, it’s an opportunity to learn something new, so I start my search with KVM. Few words about KVM:
1. It is a Kernel based virtualization, integrated with EL7 by default (lucky me).
2. Unlike OpenVZ, it creates a total isolated environment for guest operating systems, so the wall are strong enough in this one.
3. It does takes toll on the hardrive I/O speeds sometimes but with less number of KVM’s (less than 15) on this i7 hardly made any difference.
Enough said, let’s get to the configuration.
1. Make sure your server supports hardware virtualization by running the following as root, if nothing is displayed, then you are either out of luck or you need to enable virtualization from within the BIOS:

egrep '(vmx|svm)' --color=always /proc/cpuinfo

2. Install the necessary packages (most of them will/should be present, in case they are not, install them):

yum install kvm libvirt python-virtinst qemu-kvm

3. Start ‘libvirt’ daemon and enable it at boot time as:

systemctl start libvirtd && systemctl enable libvirtd

To check if KVM is installed successfully, run the following:

virsh -c qemu:///system list

and you should see something like

[root@techlinux ~]# virsh -c qemu:///system list
Id Name                 State
----------------------------------
[root@techlinux ~]#

If you see any errors, then OH BOY!, you did something wrong and go back from here and check the setup again.

4. Install ‘bridge-utils’ package and setup a network bridge so that our guests machines could use it and can be accessed from each other:

yum install bridge-utils

Next, we actually need to create a network bridge. Create a file at: “/etc/sysconfig/network-scripts/ifcfg-br0” and edit it with your favorite editor (vi, vim, nano etc.; I least care unless and until you make the entries right):

vim /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0
TYPE=Bridge
BOOTPROTO=none
ONBOOT=yes
IPADDR=172.16.128.251
NETMASK=255.255.254.0
GATEWAY=172.16.128.2
DNS1=8.8.8.8
DNS2=8.8.4.4

Next, modify the main network file for your interface, for example, if your interface is named as ‘eth0’ or ‘eno1’ or ‘enp0s3’ etc, then edit the file at: /etc/sysconfig/network-scripts/ifcfg-eno1′ and make it like following:

vim /etc/sysconfig/network-scripts/ifcfg-eno1

HWADDR=54:BE:F7:37:CE:BA
TYPE=Ethernet
NM_CONTROLLLED=yes
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=eno1
UUID=da169b0d-7cf0-4b5f-984f-5dc8daefd12e
ONBOOT=yes
BRIDGE=br0

Next, restart the network as:

systemctl restart network

Make sure that the newly created network bridge is listed by doing:

ifconfig
[root@techlinux ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.128.251 netmask 255.255.254.0 broadcast 172.16.129.255
inet6 fe80::56be:f7ff:fe37:ceba prefixlen 64 scopeid 0x20<link>
ether 54:be:f7:37:ce:ba txqueuelen 0 (Ethernet)
RX packets 1749877 bytes 142150172 (135.5 MiB)
RX errors 0 dropped 40 overruns 0 frame 0
TX packets 129457 bytes 25039760 (23.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 ether 54:be:f7:37:ce:ba txqueuelen 1000 (Ethernet)
 RX packets 1751503 bytes 173788501 (165.7 MiB)
 RX errors 0 dropped 801 overruns 0 frame 0
 TX packets 129465 bytes 25566348 (24.3 MiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
 device interrupt 20 memory 0xf7c00000-f7c20000 

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
 inet 127.0.0.1 netmask 255.0.0.0
 inet6 ::1 prefixlen 128 scopeid 0x10<host>
 loop txqueuelen 0 (Local Loopback)
 RX packets 28 bytes 3520 (3.4 KiB)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 28 bytes 3520 (3.4 KiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
 ether 52:54:00:08:e2:b8 txqueuelen 0 (Ethernet)
 RX packets 0 bytes 0 (0.0 B)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 3 bytes 222 (222.0 B)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@techlinux ~]#

5. Installing virt-manager on Archlinux desktop: The following command will install it on your archlinux desktop:

[aman@arch ~]$ sudo pacman -S virt-manager

6. Create CentOS 6 guest from command on the server and make sure you have the CentOS 6 ISO already placed in the following directory on server:

/var/lib/libvirt/images

Create the image based CentOS 6 guest as:

virt-install --connect qemu:///system -n cent6tmp -r 1024 --vcpus=2 --disk path=/var/lib/libvirt/images/cent6tmp.img,size=50 -c /var/lib/libvirt/images/CentOS-6.4-x86_64-bin-DVD1.iso --vnc --noautoconsole --os-type linux --os-variant rhel6 --accelerate --network=bridge:br0 --hvm

Output for the above command should be as:

Starting install... Allocating 'cent6tmp.img'              |  50 GB     00:0
Creating domain...                 |    0 B     00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
[root@techlinux ~]#

If you have reached this far, pat yourself on the back because most of the work is done.I have named this guest as ‘cent6tmp’ because this will act as a template for other CentOS 6 guests and instead of installing same variant guest (which is time consuming), we will simply clone this one which is pretty fast on an i7 server (not sure on what processor you are, so speed may vary).Your template guest machine is now ready to be installed. We will not head over the the Archlinux desktop system where we have ‘virt-manager’ waiting to be fired up and do the other tasks. Let’s get that done.

On the ArchLinux box:
1. Start the ‘virt-manager’ application and click on: File > Add Connection, this will show the screen similar to following:

virt-manager1
virt-manager1

2. Go ahead and edit and fields as:
a). Check the box “Connect to remote host”
b). Method: SSH
c). Username: root
d). Hostname: 172.16.128.251 (IP address of the server)

  1. Once connected, you will see the list of guest OS’es installed as shown in the screen shot below, in my case I have already added (cloned) the guest VM’s, what you will see is only ‘cent6tmp’ with a ‘Running’ status:
virt-manager2
virt-manager2

4. Double click on ‘cent6tmp’ and it will ask you for the root password of host machine, once you fill in that, you will see the virtual guest OS ready to install, go ahead and install it as you will install CentOS 6 on a physical machine.

  1. Once done, configure that machine (upgrade and stuff) and shut it off from ‘virt-manager’ itself. Now you have a template machine ready to reproduce similar ones and as many as you need, server is the limitation though 😉

Cloning the template guest OS:
1. Right click on ‘cent6tmp’ (keep it shut down) and click on ‘Clone’, you will see a similar window as below:

virt-manager3
virt-manager3

Some changes: I have my template saved as vm01 instead of cent6tmp, hence think ‘vm01’ as ‘cent6tmp’ whereever necessary.

  1. Change the name of the guest which will be created after clone, you can leave it as it is if you do not care about the names.
  2. Well, hit ‘Clone’. Depending upon the endurance power of your server, this might take some time to finish, I tried it on Core 2 Duo as well, which took around 30 minutes. If you are also on such processor, go grab a cup of coffee and let it finish.
  3. Once done, you will have a clone of ‘cent6os’ which you can boot into using the same username password that you have set for ‘cent6os’ and configure accordingly.

In case you have any queries after that, feel free to ask your questions in the comment section or drop me an email at:

aman.hanjrah@techlinux.net

Hope this was informative for you, have a nice day!

Leave a Reply

Your email address will not be published. Required fields are marked *