KVM installation on CentOS 7 and guest OS provisioning

This is a simple guide for KVM installation on CentOS 7 and provisioning guest operating system via virt-install.

Setup

First, install required packages:

yum install bridge-utils tunctl bridge-utils bind-utils tuned-utils-systemtap lvm virt-manager libvirt virt-install qemu-kvm xauth dejavu-lgc-sans-fonts

Enable IPv4 forwarding:

echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.d/99-sysctl.conf

reboot your system and check if you can run virsh command:

virsh -c qemu:///system list

Edit /etc/sysconfig/network-scripts/ifcfg-eno1 file so it would reflect the following:

TYPE="Ethernet"
NAME="eno1"
UUID="60093307-ad32-425c-b859-724fd9941ba7"
DEVICE="eno1"
ONBOOT="yes"
BRIDGE="br0"

Create br0 interface named ifcfg-br0 and paste the following:

DEVICE=br0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
NAME=br0

Enable and start libvirtd daemon:

systemctl start libvirtd && systemctl enable libvirtd

Stop/disable NetworkManager and restart network daemon:

systemctl stop NetworkManager; systemctl restart network


Provisioning

Run osinfo-query os | grep centos to get required guest OS short ID like centos7.0

Download OS base image to /tmp:

wget http://mirror.duomenucentras.lt/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1503-01.iso

Install guest OS via virt-install:

virt-install --connect qemu:///system -n [name] -r [ram_size_in_MiBs] --vcpus=[virtual_cpus] --disk path=/var/lib/libvirt/images/[name].img,size=[size_in_GiBs] --graphics inc,listen=0.0.0.0 --noautoconsole --os-type linux --os-variant [short_id] --accelerate --network=bridge:br0 --hvm --cdrom [path_to_iso.iso]

i.e.:

virt-install --connect qemu:///system -n anaconda -r 2048 --vcpus=2 --disk path=/var/lib/libvirt/images/anaconda.img,size=2 --graphics inc,listen=0.0.0.0 --noautoconsole --os-type linux --os-variant centos7.0 --accelerate --network=bridge:virbr0 --hvm --cdrom /tmp/CentOS-7-x86_64-Minimal-1503-01.iso

After successful provision, we need to find VNC port to connect to:

virsh vncdisplay [name]

:0 means 5900, :1 - 5901 and so on.

Accordingly open firewall port:

firewall-cmd --zone=public --add-port=5900/tcp --permanent

And reload configuration to apply changes:

firewall-cmd --reload

Connect to your guest via VNC client to finish installation.

Chicken example:


Deleting guest OS and image

Destroy VM:

virsh destroy [name]

Make sure VM is not running with virsh list and then undefine domain (name):

virsh undefine [name]

Finally, remove VM image:

rm -rfv /var/lib/libvirt/images/[name].img


Useful commands

virsh list - show all running VMs

virsh --connect qemu:///system autostart [name] - enable guest OS autostart

virsh start [name] - start VM.

virsh shutdown [name] - stop VM.

virsh destroy [name] - restart VM.