How to get KVM working in LXD Container

Hi

I needed to test whether KVM will run in a LXD container to run some old non-UEFI images need for production, but not having much luck, so could do with some help please. Trying on my Ubuntu 20.04 AMD64 notebook which currently is LXD v4.22, happily running GUI containers for old apps, a Windows VM. Also able to run VirtualBox VMs fine on the host OS, but never installed KVM on this host.

Lunched Ubuntu 20.04 AMD64 container last night (01 Feb '22), with config:

image.architecture: amd64
image.description: ubuntu 20.04 LTS amd64 (release) (20220131.1)
image.label: release
image.os: ubuntu
image.release: focal
image.serial: “20220131.1”
image.type: squashfs
image.version: “20.04”
security.nesting: “true”
volatile.base_image: 57263910d51e637a64d2d94f6a94832acbd886b2eda532ab0b522b4f9b85bd86
volatile.eth0.host_name: vetha2fc9a63
volatile.eth0.hwaddr: 00:16:3e:a2:07:14
volatile.idmap.base: “0”
volatile.idmap.current: ‘[]’
volatile.idmap.next: ‘[]’
volatile.last_state.idmap: ‘[]’
volatile.last_state.power: RUNNING
volatile.uuid: 751e1470-646e-4b2b-b223-6aa4868d5e49
devices:
kvm:
gid: “108” # gid in container
path: /dev/kvm
type: unix-char

Updated restart container and installed KVM:

apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils

Sadly it can’t find module kvm_intel to start qemu-kvm, and nothing exists below /lib/modules which if memory serves me correctly, could be normal:

root@kvm:~# systemctl status qemu-kvm
● qemu-kvm.service - QEMU KVM preparation - module, ksm, hugepages
Loaded: loaded (/lib/systemd/system/qemu-kvm.service; enabled; vendor preset: enabled)
Active: active (exited) since Wed 2022-02-02 22:22:49 UTC; 1min 2s ago
Process: 127 ExecStart=/usr/share/qemu/init/qemu-kvm-init start (code=exited, status=0/SUCCESS)
Main PID: 127 (code=exited, status=0/SUCCESS)

Feb 02 22:22:49 kvm systemd[1]: Starting QEMU KVM preparation - module, ksm, hugepages…
Feb 02 22:22:49 kvm qemu-kvm-init[145]: modprobe: FATAL: Module kvm_intel not found in directory /lib/modules/5.13.0-28-generic
Feb 02 22:22:49 kvm qemu-kvm-init[152]: mknod: /dev/kvm: File exists
Feb 02 22:22:49 kvm systemd[1]: Finished QEMU KVM preparation - module, ksm, hugepages.

root@kvm:~#
root@kvm:~#
root@kvm:~# modprobe kvm_intel
modprobe: FATAL: Module kvm_intel not found in directory /lib/modules/5.13.0-28-generic

This is an Intel Core i7 machine, and lsmod on this host gives me this:

$ lsmod | grep -i kvm
kvm_intel 303104 0
kvm 864256 1 kvm_intel

Thanks

So I’m not sure that there’s an actual problem here (other than noise).

That systemd unit shows that it completed successfully, you have the kernel module loaded already (through the host, you could set linux.kernel_modules to kvm_intel to have LXD ensure it’s loaded before startup).

Does qemu or libvirt actually fail and if so, why?

Good morning

Thanks for your time… yes, you’re correct… inexperience and other things going on that appeared that this was the issue.

So I see what would be a simple guide to get KVM VMs running in a LXD Ubuntu 20.04 AMD64 unprivileged container:

  • create a standard ubuntu 20.04 AMD64 container (lxc launch ubuntu:20.04/amd64 {ContainerName})
  • allow virtualisation nesting ( lxc config set {ContainerName} security.nesting true)
  • open shell in the container (lxc exec {ContainerName} -- bash)
  • in the container shell, install qemu-kvm (apt update && apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils)
  • in the container shell, edit “/etc/libvirt/qemu.conf” and set “remember_owner = 0# to get around the issue in this article for unprivileged containers
  • in the container shell, get the gid of the kvm group (getent group kvm) # not sure this is still needed
  • on the host shell, to the container’s config, add the kvm device with the gid of the kvm group discovered above (lxc config device add {ContainerName} kvm unix-char path=/dev/kvm gid=???)
  • restart the container (lxc restart {ContainerName})
  • start creating VMs in KVM.