Hardware virtualization for QEMU on a Linux machine: BIOS, kvm_intel and /dev/kvm

OpenBSD has its own kernel, so it cannot run as a Docker container on Linux: a container shares the kernel of the host. The way to run it next to other containers is a virtual machine started by qemu, and the machine is fast only when QEMU can hand the work to the processor through KVM. This page checks whether the computer can do it, switches the feature on in the BIOS and makes /dev/kvm available to Docker. It is the first of five pages that build an OpenBSD virtual machine in a container.

Check the machine

A physical machine and a virtual machine need different steps, so find out which one this is. systemd-detect-virt prints none on a physical machine and the name of the hypervisor otherwise.

systemd-detect-virt

none

The processor shows the virtualization feature as the vmx flag (Intel VT-x) or the svm flag (AMD-V). Count the lines of /proc/cpuinfo that carry it: a number above zero means the feature is on, zero means it is off or missing.

grep -c -w -E 'vmx|svm' /proc/cpuinfo

0

The KVM device is the file QEMU opens. Without the feature it does not exist:

ls -l /dev/kvm

ls: cannot access '/dev/kvm': No such file or directory

Zero on a physical machine whose processor supports VT-x means that the feature is switched off in the BIOS. Without KVM QEMU still works, but it emulates every instruction in software, which is many times slower. The pages that follow note where that shows.

A machine that is itself a virtual machine

When systemd-detect-virt names a hypervisor (kvm, vmware, oracle), the feature must be passed on to this machine, which is called nested virtualization. Switch it on in the settings of the hypervisor, not in the BIOS: in VirtualBox the option Enable Nested VT-x/AMD-V of the virtual machine, in VMware Virtualize Intel VT-x/EPT or AMD-V/RVI, in a KVM host options kvm_intel nested=1. Then run the checks above again inside the guest; the rest of the page is the same.

Switch it on in the BIOS

Restart and open the BIOS setup with the key the manufacturer shows on the first screen (F2, Del, F1; on a Lenovo laptop F2, or the small Novo button). The option sits under Security, Advanced or Configuration, and its name is one of these:

Set it to Enabled, save with F10 and boot Linux. A second option, VT-d or IOMMU, is for passing PCI devices through and is not needed here. Run the check again:

grep -c -w -E 'vmx|svm' /proc/cpuinfo

The number is the count of processor threads, for example 4.

Load the KVM module

The kernel ships the module, but it is loaded only when something asks for it. Load the one that matches the processor (kvm_intel or kvm_amd):

sudo modprobe kvm_intel

lsmod | grep kvm

Two lines mean that the module and the common kvm module it needs are loaded. The device file appears at the same moment:

ls -l /dev/kvm

The owner is root and the group is kvm, with the mode crw-rw----. Ubuntu usually loads the module by itself at the next boot once the BIOS option is on, so this command is needed only once. If modprobe answers Operation not supported or Permission denied for the file, the BIOS option is still off or the machine is virtual: go back to the first two sections. dmesg | grep -i kvm prints the reason, for example disabled by bios.

Allow your user to open the device

A container started by docker run runs QEMU as root and does not need this step. A QEMU started by hand from the shell does, because only the group kvm can open the file:

sudo usermod -aG kvm $USER

Log out and in again, then check that the group is there:

id -nG

Pass the device into a container

A container sees no devices of the host unless the command names them. The option --device hands the file over:

docker run --rm --device /dev/kvm alpine ls -l /dev/kvm

The file is listed inside the container. The next pages add this option to the command that starts QEMU; without the file the same command keeps working in software, so a machine that cannot use KVM is not blocked, only slower.

Related

Article author: Klavdiya Kondakova