Saturday, January 13, 2018

KVM on CentOS 7

I recently ditched ESXi in my home lab virtualization setup and went with KVM on CentOS. There wasn't any significant reason for doing so, other than I wanted to get more into KVM, and open source. The setup is one host, running CentOS 7, KVM, and the web based management tool Kimchi.

After running the install, which I did from a USB stick and the minimal ISO, I created a new SSH key to be used for KVM host access. Note that some of these commands are not suggested for production.

ssh-keygen -t rsa -b 4096 -C "kvm" -f ~/.ssh/id_rsa -N ""

Add the public key as an authorized key:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys

Disable host key checking and use of a known_hosts file:

echo "StrictHostKeyChecking no" >> ~/.ssh/config
echo "UserKnownHostsFile /dev/null" >> ~/.ssh/config

The next step is to update the installed packages:

yum -y update

Then stop and disable the default firewall, disable selinux, and reboot.

sed -i --follow-symlinks 's/^SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
systemctl stop firewalld
systemctl disable firewalld
reboot

At this point you have the installation of the CentOS host is ready and needs KVM. To be able to run the VMs on the system you will need:

QEMU: Machine emulator and virtualizer
KVM: Enables use of virtualization extensions provided by Intel and AMD processors
libvirt: A Toolkit to manage virtualization hosts
kimchi: A lightweight HTML5 web GUI for your host and VMs

Start with installing the required packages:

yum -y install qemu-kvm libvirt virt-install bridge-utils bind-utils \
virt-manager wget net-tools virt-viewer genisoimage epel-release

Start libvirt and enable it for autostart:

systemctl start libvirtd
systemctl enable libvirtd

Verify your installation, if the virtualization extensions are enabled in the BIOS, the output should look similar to the following:

[root@srv-kvm ~]# virsh nodeinfo
CPU model:           x86_64
CPU(s):              4
CPU frequency:       2659 MHz
CPU socket(s):       1
Core(s) per socket:  4
Thread(s) per core:  1
NUMA cell(s):        1
Memory size:         15691360 KiB

Otherwise, it may look like this, in which case you need to reboot and enable virtualization in the  BIOS:

[root@srv-kvm ~]# dmesg | grep kvm
[    0.745756] systemd[1]: Set hostname to <3-kvm3>.
[   11.851827] kvm: disabled by bios

Enable bridge networking so that your container host VMs can have routable IPs on the same network as the KVM host.

Identify the linux interface name for your active network connection using ip a. Change directory into the networking-scripts directory and then copy the config file for your active network connection to a new bridge config file (ifcfg-bridge0)

Edit the ifcfg-bridge0 as follows:

TYPE="Bridge"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp0s25"
UUID="6bd6c3456-bad9-4280-a7f3-c24aa93e6c0e"
DEVICE="bridge0"
ONBOOT="yes"
IPADDR="192.168.1.11"
PREFIX="24"
GATEWAY="192.168.1.1"
DNS1="192.168.1.1"
DNS2="8.8.8.8"
DNS3="8.8.4.4"
DOMAIN="domain.lan"
IPV6_PRIVACY="no"

Edit the ifcfg-DEVICE with the following:
TYPE="Ethernet"
BOOTPROTO="none"
DEVICE="enp0s25"
ONBOOT="yes"
BRIDGE="bridge0"

Restart networking and verify your fixed IP address is now assigned to the bridge interface, and that the physical interface is providing access to the bridge.

The final step is to install kimchi and the plugins needed to to manage the KVM host. Change directories to the tmp folder and download the kimchi installers

Wok: framework for multi-purpose plug-ins
Kimchi: The HTML5 management interface for KVM guests
Gingerbase: Host management plugin

cd /tmp
wget https://github.com/kimchi-project/kimchi/releases/download/2.5.0/wok-2.5.0-0.el7.centos.noarch.rpm
wget http://kimchi-project.github.io/gingerbase/downloads/latest/ginger-base.el7.centos.noarch.rpm
wget https://github.com/kimchi-project/kimchi/releases/download/2.5.0/kimchi-2.5.0-0.el7.centos.noarch.rpm
yum -y install wok.el7.centos.noarch.rpm
yum -y install ginger-base.el7.centos.noarch.rpm
yum -y install kimchi.el7.centos.noarch.rpm
sed -i  's/^#session_timeout = .*/session_timeout = 1440/g' /etc/wok/wok.conf
systemctl enable wokd
systemctl start wokd

Go in your web browser to https://KVM-IP:8001 and you will see a login page. Use your Linux root credentials to login, and you can now manage the host from here and create new virtual machines.

Here are some helpful libvirt CLI commands that you can run on your KVM host:

virsh list --all (show all defined VMs)
virsh console srv-vm1 (connect to console)
virsh start srv-vm1 (startup)
virsh shutdown srv-vm1 (nice shutdown)
virsh destroy srv-vm1 (hard power off)
virsh undefine srv-vm1 (remove config)