int 2Eh
int 2Eh

Reputation: 545

How to launch qemu-kvm from inside a Docker container?

Assuming the host system already supports KVM, is it possible to create a docker image which contains some scripts to launch a VM (inside the container) with virsh and QEMU-KVM?

We are looking into dockerize a script which launches a VM through QEMU-KVM and extracts some results from the VM.

Upvotes: 30

Views: 61497

Answers (5)

Achyuth Nandikotkur
Achyuth Nandikotkur

Reputation: 11

If you prefer not to use the --privileged option, Smarter-device-manager allows containers to access host devices in a secure way.

Upvotes: 1

hldev
hldev

Reputation: 1414

--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.

Upvotes: 12

Matt
Matt

Reputation: 416

--device=/dev/kvm

Adding to the previous answer: Using --privileged may open up too many permissions for your use case. I have been able to run qemu with kvm and without privileges using the device parameter instead.

Try the following commands:

docker run --device=/dev/kvm -it ubuntu bash

Inside docker:

apt-get update -y
apt-get install -y qemu-system-x86
qemu-system-x86_64 \
  -append 'root=/dev/vda console=ttyS0' \
  -drive file='rootfs.ext2.qcow2,if=virtio,format=qcow2'  \
  -enable-kvm \
  -kernel 'bzImage' \
  -nographic \
;

Upvotes: 19

docker --privileged

Some working commands from Ubuntu 17.10 host, Docker 1.13.1:

sudo docker run --name ub16 -i --privileged -t ubuntu:16.04 bash

Then inside Docker:

apt-get update -y
apt-get install qemu -y
qemu-system-x86_64
qemu-system-x86_64 \
  -append 'root=/dev/vda console=ttyS0' \
  -drive file='rootfs.ext2.qcow2,if=virtio,format=qcow2'  \
  -enable-kvm \
  -kernel 'bzImage' \
  -nographic \
;

Root file system and bzImage generated with this setup.

Upvotes: 24

crashtua
crashtua

Reputation: 491

Easy. You need run privileged container, ensure that you have /dev/kvm node in container, install all packages to serve kvm(libvirt, quemu, whatever else) - that is all you need. See https://github.com/sivaramsk/docker-kvm for reference.

Upvotes: 0

Related Questions