Skip to content

Install and Configure KVM

Note

This section assumes that you have already installed Ubuntu 20.04 LTS server on your physical host

KVM requires that your host supports hardware virtualization. This means that your CPU needs to have virtualization extensions and that these extensions are enabled in system BIOS.

Check virtualization support

Depending on whether your system is equipped with Intel or AMD CPU that supports hardware virtualization, it will come with either VMX or SVM extensions available respectively. You can check whether one of these extension types is available by running:

grep -E 'vmx|svm' /proc/cpuinfo | wc -l

You should see a positive integer returned by the command above. 0 means your CPU doesn't have support for hardware virtualization.

Next, check that KVM's prerequisites are satisfied:

sudo apt update
sudo apt install cpu-checker
kvm-ok

You should see kvm-ok return an output similar to:

INFO: /dev/kvm exists
KVM acceleration can be used
If, instead, kvm-ok returned errors, you will have fix the issues and rerun the command.

Note

The vast majority of modern (manufactured in the last 10 years) systems with an x86 Intel or AMD CPUs have hardware virtualization capability. However, it is common for VMX or SVM extensions to be disabled in the BIOS out of the box. Consult your motherboard manual on how to enable them.

Disable swap

Deactivate running swap

sudo swapoff -a

Then, in /etc/fstab/, remove or comment out any swap mountpoints, such as /swapfile or /swap.img

Optionally, you can save a bit of disk space by removing the allocated swap file, i.e

sudo rm /swap.img

Set up virtual bridge and static IP

Follow instructions to set up br0 virtual bridge network interface and configure static IP of your KVM host.

Install KVM packages

sudo apt install -y qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager

Enable libvirtd service

sudo systemctl start libvirtd
sudo systemctl enable --now libvirtd

Validate installation

sudo systemctl status libvirtd
sudo virsh list --all

If everything is ok, libvirtd will be in active (running) state without any errors and virsh will return an empty list of VMs (since we haven't created any yet).

Fix bridge interface configuration

By default KVM will create a virtual bridge interface virbr0. We don't want to use this interface, since we already have a br0, mapped to our host's physical NIC. In order to make sure that our VMs bind to the br0 bridge, we will have to delete the virbr0 interface and associated KVM network and then re-create the network bound to the correct br0 interface.

Destroy the default KVM network

sudo virsh net-destroy default
sudo virsh net-undefine default

Set up new virtual network bound to br0

Create file default.xml.

default.xml
<network>
  <name>default</name>
  <forward mode="bridge"/>
  <bridge name="br0"/>
</network>

Recreate the bridge network:

sudo virsh net-define default.xml
sudo virsh net-start default
sudo virsh net-autostart default

Check virtual networking configuration

sudo virsh net-list --all

You should see a single virtual network, called default, set in active, persistent state:

 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes

Next, check your bridge interfaces:

brctl show

You should see a single bridge interface br0 bound to the physical NIC like so:

bridge name   bridge id           STP enabled   interfaces
br0           8000.2cf05d6019a1   yes           enp37s0

Set correct time zone

Check that you have correct time zone, and set it up if necessary, i.e.:

sudo timedatectl set-timezone America/Toronto