KVM Setup

KVM setup on Ubuntu 20.04 LTS

Posted on July 22, 2020

Kernel as Virtual Machine or Kernel based Virtual Machine (KVM) is a Linux Kernel module, which allows later to function as a hypervisor. KVM is full virtualization solution, hence based on Virtualization Extensions provided by CPU.

Here, I am using Intel Xeon machine with Ubuntu 20.04 LTS, Linux Kernel v5.4.y.

Host Setup

KVM is enabled in Ubuntu by default, however you can verfiy this by using following commands.

$ sudo apt install cpu-checker
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

Tweak the Host

$ sudo apt install qemu-system-x86 bridge-utils virt-manager qemu-kvm libvirt-daemon
$ sudo systemctl status libvirtd.service
$ sudo systemctl enable libvirtd --now

Tweak the network

For obvious reasons, one needs to have network access to guest domains. For this, you can use a software bridge. We already installed bridge-utils during the installation step. In Ubuntu 20.04 LTS, the network is managed by Netplan.

$ sudo cp /etc/netplan/*.yaml /etc/netplan/*.yaml
network:
  ethernets:
    eno2:
      dhcp4: false

  bridges:
    virbr0:
      interfaces: [eno2]
      dhcp4: true

  version: 2
$ sudo netplan generate
$ sudo netplan apply
$ sudo systemctl restart network-manager

Reboot Host

After these changes, you may want to restart the system for changes to take effect.

$ sudo reboot

Guest Setup

As a guest we can install Debian buster, with the following command.

$ sudo virt-install --name gdeb --virt-type kvm --ram 4096 --vcpus 2 --disk path=/path/to/gdeb2/disk.qcow2,size=20 --os-type linux --os-variant debian10 --network bridge=virbr0 --graphics none --console pty,target_type=serial --location 'http://deb.debian.org/debian/dists/buster/main/installer-amd64/' --extra-args 'console=ttyS0,115200n8 serial'

Follow the installation instructions.

List the gust domains started

$ sudo virsh list

Start the domain

$ sudo virsh start --domain gdeb

Shutdown the domain

$ sudo virsh shutdown --domain gdeb

Connect to the guest domain

$ sudo virsh console --domain gdeb

Also, you can SSH to the domain if you enabled network-bridge.

To pin the CPU

$ sudo virsh vcpupin --domain gdeb
$ sudo virsh vcpupin --domain gdeb <vCPU> <CPU>