KVM - Virtualization Solution

Introduction

KVM (Kernel-based Virtual Machine) is a full virtualization solution for x86 platforms that have Intel VT or AMD-V support. It includes a loadable core kernel module, kvm.ko (kvm-intel.ko or kvm-AMD.ko), that provides low-level virtualization for the processor. The KVM module alone is far from enough — the KVM kernel module by itself can only virtualize CPU and memory, and users can’t directly control the kernel module to do things. KVM also needs a modified copy of Qemu as the upper-level control software for the virtual machine, in order to form a complete virtualization solution. Qemu is itself also a virtualization program. Its distinguishing feature is that it can virtualize different CPUs — for example, on an x86 CPU it can virtualize a Power CPU, and can be used to compile programs that run on Power.

Installation

apt-get install qemu-kvm libvirt-bin [1]

KVM uses, with slight modification, the x86-based portion of QEMU, forming the userspace tool QEMU-KVM that controls the KVM kernel module. [2] QEMU-KVM calls the /dev/kvm interface via ioctl, handing the CPU-instruction-related parts off to the kernel module. KVM implements CPU and memory virtualization, while qemu emulates I/O devices.

The QEMU-KVM tool isn’t very efficient and isn’t easy to use, so RedHat developed more auxiliary tools for KVM, such as libvirt

libvirt is a set of APIs offering interfaces in multiple languages, providing a convenient and reliable programming interface for various virtualization tools. It supports not only KVM but also other virtual machines such as Xen, and several commonly-used VM management tools (such as virsh, Virt-install, virt-manager, etc.) and cloud computing framework platforms (such as OpenStack, OpenNebula, Eucalyptus, etc.) all use libvirt’s application programming interface under the hood.

To make managing virtual machines easier, you should add the current operating user to the kvm and libvirt groups. This lets you use the virsh command.

Usage

Setting up bridged networking

By default, Qemu provides the guest with a NAT network, in which case the host and guest cannot reach each other. To give the guest full network access, you should create a bridge. [3]

apt-get install bridge-utils

Modify the /etc/network/interfaces file so that bridge br0 proxies eth0. Once configured, use the bridge interface within the guest.

# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto br0
iface br0 inet static
  address 10.10.10.70
  netmask 255.255.255.0
  gateway 10.10.10.10
  bridge_ports eth0
  bridge_stp off
  bridge_fd 0
  bridge_maxwait 0

Creating a new guest

virtinst provides the Virt-install command line for creating virtual machines.

apt-get install virtinst

virt-install
  --connect qemu:///system \
  --virt-type kvm \
  --name demo \
  --memory 500 \
  --disk size=20 \
  --cdrom /var/lib/libvirt/boot/debian-8.5.0-amd64-netinst.iso \
  --os-variant debianwheezy \
  --network bridge=br0 \
  --noautoconsole \
  --graphics vnc,password=XYZ12345,listen=0.0.0.0

After successful creation, use virsh -c qemu:///system list –all to check the VM status; a status of running means creation and startup succeeded, and you can complete the installation by connecting to the server with a VNC client.

If you need unattended preseeded installation, refer to preseed [4] .

Starting and stopping

When logged in as a regular user, libvirt uses the qemu:///session domain by default; add the -c qemu:///system option to use the qemu:///system domain instead.

List guests

virsh -c qemu:///system list --all

Stop a guest

virsh -c qemu:///system shutdown demo

If stopping fails, you can force-stop it

virsh -c qemu:///system destroy demo

Start a guest

virsh -c qemu:///system start demo

Set a VM to autostart on boot

virsh autostart alice

References