Sorry for the ugly format - I try to improve this now…
Hi,
running on 64bit Debian11 host a lxc container called android-dev. Android Studio requires /dev/kvm for the emulator, therefor this is nested kvm in lxc: Debian11>lxc>qemu/kvm
LXC runs on the debian11 host using a bridge and the config of the container
cat /var/lib/lxc/android-dev/config
# Uncomment the following line to support nesting containers:
# I tried with or without the next line
lxc.include = /usr/share/lxc/config/nesting.conf# lxc.apparmor.profile = generated
lxc.apparmor.profile = unconfined
lxc.apparmor.allow_nesting = 1
lxc.net.0.type = veth
lxc.net.0.link = br0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:14:ba:34
lxc.rootfs.path = dir:/var/lib/lxc/android-dev/rootfs# Common configuration
lxc.include = /usr/share/lxc/config/debian.common.conf# Container specific configuration
lxc.tty.max = 4
lxc.uts.name = android-dev
lxc.arch = amd64
lxc.pty.max = 1024# Permit access to /dev/loop*
LXC.cgroup.devices.allow: b 7:* rwm
LXC.cgroup.devices.allow: c 10:237 rwm# Setup access to /dev/net/tun and /dev/kvm
lxc.mount.entry = /dev/net/tun dev/net/tun none bind,create=file 0 0
lxc.mount.entry = /dev/kvm dev/kvm none bind,create=file 0 0
cat /usr/share/lxc/config/nesting.conf
# Use a profile which allows nesting
lxc.apparmor.profile = lxc-container-default-with-nesting
# Add uncovered mounts of proc and sys, else unprivileged users
# cannot remount those
lxc.mount.entry = proc dev/.lxc/proc proc create=dir,optional 0 0
lxc.mount.entry = sys dev/.lxc/sys sysfs create=dir,optional 0 0
cat /usr/share/lxc/config/debian.common.conf
# This derives from the global common config
lxc.include = /usr/share/lxc/config/common.conf
# Doesn’t support consoles in /dev/lxc/
lxc.tty.dir =
# When using LXC with apparmor, the container will be confined by default.
# If you wish for it to instead run unconfined, copy the following line
# (uncommented) to the container’s configuration file.
LXC.apparmor.profile = unconfined
# If you wish to allow mounting block filesystems, then use the following
# line instead, and make sure to grant access to the block device and/or loop
# devices below in lxc.cgroup.devices.allow.
LXC.apparmor.profile = lxc-container-default-with-mounting
# Extra cgroup device access
## rtc
lxc.cgroup.devices.allow = c 254:0 rm
## tun
lxc.cgroup.devices.allow = c 10:200 rwm
## hpet
lxc.cgroup.devices.allow = c 10:228 rwm
## kvm
lxc.cgroup.devices.allow = c 10:232 rwm
## To use loop devices, copy the following line to the container’s
## configuration file (uncommented).
LXC.cgroup.devices.allow = b 7:* rwm
cat /etc/lxc/default.conf
# lxc.apparmor.profile = generated
lxc.apparmor.profile = unconfined
lxc.apparmor.allow_nesting = 1
lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
on the host
virt-host-validate
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking if device /dev/kvm exists : PASS
QEMU: Checking if device /dev/kvm is accessible : PASS
QEMU: Checking if device /dev/vhost-net exists : PASS
QEMU: Checking if device /dev/net/tun exists : PASS
QEMU: Checking for cgroup ‘cpu’ controller support : PASS
QEMU: Checking for cgroup ‘cpuacct’ controller support : PASS
QEMU: Checking for cgroup ‘cpuset’ controller support : PASS
QEMU: Checking for cgroup ‘memory’ controller support : PASS
QEMU: Checking for cgroup ‘devices’ controller support : PASS
QEMU: Checking for cgroup ‘blkio’ controller support : PASS
QEMU: Checking for device assignment IOMMU support : PASS
QEMU: Checking if IOMMU is enabled by kernel : PASS
QEMU: Checking for secure guest support : WARN (Unknown if this platform has Secure Guest support)
LXC: Checking for Linux >= 2.6.26 : PASS
LXC: Checking for namespace ipc : PASS
LXC: Checking for namespace mnt : PASS
LXC: Checking for namespace pid : PASS
LXC: Checking for namespace uts : PASS
LXC: Checking for namespace net : PASS
LXC: Checking for namespace user : PASS
LXC: Checking for cgroup ‘cpu’ controller support : PASS
LXC: Checking for cgroup ‘cpuacct’ controller support : PASS
LXC: Checking for cgroup ‘cpuset’ controller support : PASS
LXC: Checking for cgroup ‘memory’ controller support : PASS
LXC: Checking for cgroup ‘devices’ controller support : PASS
LXC: Checking for cgroup ‘freezer’ controller support : FAIL (Enable ‘freezer’ in kernel Kconfig file or mount/enable cgroup controller in your system)
LXC: Checking for cgroup ‘blkio’ controller support : PASS
LXC: Checking if device /sys/fs/fuse/connections exists : PASS
inside android-dev lxc
virt-host-validate
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking if device /dev/kvm exists : PASS
QEMU: Checking if device /dev/kvm is accessible : FAIL (Check /dev/kvm is world writable or you are in a group that is allowed to access it)
QEMU: Checking if device /dev/vhost-net exists : WARN (Load the ‘vhost_net’ module to improve performance of virtio networking)
QEMU: Checking if device /dev/net/tun exists : PASS
QEMU: Checking for cgroup ‘cpu’ controller support : PASS
QEMU: Checking for cgroup ‘cpuacct’ controller support : PASS
QEMU: Checking for cgroup ‘cpuset’ controller support : PASS
QEMU: Checking for cgroup ‘memory’ controller support : PASS
QEMU: Checking for cgroup ‘devices’ controller support : PASS
QEMU: Checking for cgroup ‘blkio’ controller support : PASS
QEMU: Checking for device assignment IOMMU support : PASS
QEMU: Checking if IOMMU is enabled by kernel : PASS
QEMU: Checking for secure guest support : WARN (Unknown if this platform has Secure Guest support)
LXC: Checking for Linux >= 2.6.26 : PASS
LXC: Checking for namespace ipc : PASS
LXC: Checking for namespace mnt : PASS
LXC: Checking for namespace pid : PASS
LXC: Checking for namespace uts : PASS
LXC: Checking for namespace net : PASS
LXC: Checking for namespace user : PASS
LXC: Checking for cgroup ‘cpu’ controller support : PASS
LXC: Checking for cgroup ‘cpuacct’ controller support : PASS
LXC: Checking for cgroup ‘cpuset’ controller support : PASS
LXC: Checking for cgroup ‘memory’ controller support : PASS
LXC: Checking for cgroup ‘devices’ controller support : PASS
LXC: Checking for cgroup ‘freezer’ controller support : FAIL (Enable ‘freezer’ in kernel Kconfig file or mount/enable cgroup controller in your system)
LXC: Checking for cgroup ‘blkio’ controller support : PASS
LXC: Checking if device /sys/fs/fuse/connections exists : PASS
root@android-dev:/#
Somethings really interesting is that /dev/kvm gets updated when the lxc container gets started:
root@host:/var/lib/lxc/android-dev# ls -la /dev/kvm
crw-rw----+ 1 root kvm 10, 232 Jan 22 20:05 /dev/kvm
root@host:/var/lib/lxc/android-dev# lxc-start android-dev
root@host:/var/lib/lxc/android-dev# ls -la /dev/kvm
crw-rw----+ 1 root Debian-exim 10, 232 Jan 22 20:05 /dev/kvm
root@host:/var/lib/lxc/android-dev# lxc-stop android-dev
root@host:/var/lib/lxc/android-dev# ls -la /dev/kvm
crw-rw----+ 1 root Debian-exim 10, 232 Jan 22 20:05 /dev/kvm
inside lxc
ls -la /dev/kvm
crw-rw----+ 1 root kvm 10, 232 Jan 22 09:05 /dev/kvm
I saw that the group id in lxc and the host don’t match, because I have most likely installed packages in a different order.
host:
root@host:/var/lib/lxc/android-dev# lsmod | grep kvm
kvm_intel 327680 0
kvm 921600 1 kvm_intel
irqbypass 16384 1 kvm
lxc:
root@android-dev:/# lsmod | grep kvm
kvm_intel 327680 0
kvm 921600 1 kvm_intel
irqbypass 16384 1 kvm
root@host:/var/lib/lxc/android-dev# cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
coretemp
ipmi_devintf
ipmi_msghandler
ipmi_si
fuse
vhost_net
kvm_intel
root@android-dev:/# cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
kvm_intel
vhost_net
kvm-intel
How can I get /dev/kvm to be useable within the lxc container?
Security is not an issue, I use the lxc container for pure software package separation purposes.
Help is appreciated.