--device=/dev/kvm
works only if the container user has access to /dev/kvm
on host system already.
The correct way is to add the container user to the kvm
group, but the group ID (GID) under the container must be the same GID on the host system. You can find the group IDs on host with grep kvm /etc/groups
.
The problem now is that GIDs depends on host system, different hosts will generally have different GIDs. To fix this you can set a known GID for kvm
group on both the image and host system with groupmod
:
groupmod -g 1100 kvm
Make sure /dev/kvm
on host system has kvm
as group.
Another easier way is set the group at container startup:
docker run --device=/dev/kvm --group-add GID
where GID
is the ID of kvm
group on host system.
This all is required because permissions are tracked by UID and GID, docker uses the host system's kernel, so UID and GIDs on docker containers maps directly to IDs on the host system. Container users and groups with same names as the ones on host system doesn't mean they have same IDs.