Table of Contents

Kernel-based Virtual Machine (KVM)

General Information

KVM is “a full virtualization solution for Linux on x86 hardware containing virtualization extensions.” (KVM Main Page) It can be run on the desktop with GUI managers or on a headless server.

Checklist


Installation

Install: Required Virtualization Packages (CLI)

yum install qemu-kvm python-virtinst virt-top libvirt libvirt-client

CLI Package Explanations

Install: Optional Virtualization Packages (GUI)

yum install virt-manager virt-viewer

GUI Package Explanations

Install: Optional Useful Packages

yum install bridge-utils dejavu-lgc-sans-fonts tigervnc xorg-x11-xauth

Packages Descriptions


Verify Installation and Start Services

[root@bash ~]# lsmod | grep kvm
kvm_intel              55496  0 
kvm                   337900  1 kvm_intel
modprobe -v kvm
service libvirtd start
chkconfig libvirtd on
service libvirtd status
virsh -c qemu:///system list

Setup Networking

By default, VMs will be on a private network with no access to the outside world unless they use a physical host network interface as a bridge.

NetworkManager vs network

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

Create a Bridge

virsh iface-bridge eth0 br0

This will create the new file “ifcfg-br0” and modify “ifcfg-eth0” to look as follows:

/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE="eth0"
HWADDR="00:04:4B:17:3C:FA"
ONBOOT="yes"
BRIDGE="br0"

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

DEVICE="br0"
ONBOOT="yes"
TYPE="Bridge"
BOOTPROTO="dhcp"
STP="on"
DELAY="0"

br0 will be the interface with an IP address. To make it static, simply edit ifcfg-br0 and change it:

DEVICE="br0"
ONBOOT="yes"
TYPE="Bridge"
BOOTPROTO="none"
STP="on"
DELAY="0"
IPADDR="192.168.0.100"
NETMASK="255.255.255.0"
GATEWAY="192.168.0.1"
DNS1="208.67.222.222"
DNS2="208.67.220.220"

Note: Those DNS entries are for OpenDNS.

IP Forwarding

Edit /etc/sysctl.conf

inet.ipv4.ip_forward = 1
sysctl -p /etc/sysctl.conf
service network restart

SELinux

If SELinux is on and in Enforcing mode, there is additional configuration if you change the default directory for VM images.

Check if SELinux is on/enabled

[bash]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing

Default VM Image directory: /var/lib/libvirt/images/

Changing the Default VM Image Path

mkdir /vm-images
chcon -R --reference /var/lib/libvirt/images /vm-images
ls -Z /vm-images
rmdir /var/lib/libvirt/images
ln -s /vm-images /var/lib/libvirt/images

Creating VMs

VMs can be installed via a command line tool (virt-install) or a GUI (virt-manager).

CLI: virt-install

virt-install has many options, see them with “virt-install –help”.

Common options:

General Options
--name=NAME  (Name of the guest)
--ram=MEMORY  (Memory in megabytes)
--vcpus=VCPUS  (Number of vcpus)
 
Installation Method
--cdrom=CDROM  (CD-ROM installation media, can be ISO or physical cd-rom drive)
--location=LOCATION  (Installation source via http, ftp, or nfs.)
--pxe  (Boot from network using PXE)
--extra-args "ks=http://myserver/mykickstartfile.ks"  (Path to kickstart file for automated installs)
 
Storage Configuration
--disk=DISKOPTS  (Storage with different options)
  --disk path=/my/existing/disk 
  --disk path=/my/new/disk,size=10 (in GB)
 
Network
--network bridge=br0  (Guest network interface)
 
Graphics
--graphics=GRAPHICS  (Guest display settings)
  --graphics vnc  (Default if DISPLAY environment variable is set)
  --graphics none  (Default if DISPLAY environment variable is not set)

Example: Interactive Install with VNC

Install a VM called “centos1”, create a 20GB hard drive, 1 CPU, 1GB ram, point it to the iso, have it use the network interface “br0”

virt-install \
--name=centos1 \
--disk path=/vm-images/centos1.img,size=20 \
--vcpus=1 --ram=1024 \
--cdrom=/vm-images/CentOS-6.6-x86_64-netinstall.iso \
--network bridge=br0

Connect to Guest for Installation

After the VM is created, if you are using the “–graphics vnc” option (either by default or explicitly), a console is opened up for interactive install.

If you close it, this is how you can connect back to the guest:

Example connect to the guest named “centos1” (Yes, that is 3 slashes)

virt-viewer --connect qemu:///system centos1

Example open virt-manager and then the VM “centos1”

virt-manager

Proceed with installation as normal.


GUI: virt-manager

Installing locally via a GUI.

virt-manager

Disabling virbr0 NAT Interface

On some desktop installations, a NAT interface “virbr0” is auto created.

To disable this interface: