Build the QEMU container image for the OpenBSD virtual machine
The container of this page holds only QEMU. It does not contain OpenBSD: the system lives in a disk file that the container opens, so the same image runs any disk and the disk can be replaced without a build. The image is small, one start script decides how the machine runs, and the SSH port of the guest is forwarded to the container.
Where the files go
Both files sit with the other container files of the repository, next to the Dockerfiles of the OpenCode images:
container/containerfiles/Dockerfile.openbsd-vm container/containerfiles/openbsd-vm-start.sh
The Dockerfile
Alpine carries QEMU in small packages. qemu-system-x86_64 is the emulator, qemu-img makes and inspects disk files, and openssh-client lets the container probe the guest.
vi container/containerfiles/Dockerfile.openbsd-vm
FROM alpine:3.22 RUN apk add --no-cache qemu-system-x86_64 qemu-img openssh-client COPY openbsd-vm-start.sh /usr/local/bin/openbsd-vm-start ENTRYPOINT ["/usr/local/bin/openbsd-vm-start"]
The start script
The script is the only logic of the image. It takes the disk, the memory and the number of processors from environment variables, uses KVM only when /dev/kvm is inside the container and writable, and otherwise falls back to software emulation (tcg). The guest sees a virtio disk and a virtio network card, which OpenBSD supports without extra drivers. hostfwd=tcp::22-:22 makes port 22 of the container the port 22 of the guest, and -serial stdio sends the console of the guest to the log of the container, which docker logs shows.
vi container/containerfiles/openbsd-vm-start.sh
#!/bin/sh set -eu disk="${VM_DISK:?VM_DISK is the disk file, for example /vm/overlay.qcow2}" mem="${VM_MEM:-1024}" if [ -w /dev/kvm ]; then accel=kvm cpu=host cpus="${VM_CPUS:-2}" else accel=tcg cpu=max cpus="${VM_CPUS:-1}" fi echo "openbsd-vm: accelerator $accel, $mem MB, $cpus CPUs" >&2 exec qemu-system-x86_64 -machine q35 -accel "$accel" -cpu "$cpu" \ -m "$mem" -smp "$cpus" -display none -serial stdio -monitor none \ -drive "file=$disk,if=none,id=hd0,format=qcow2" \ -device virtio-blk-pci,drive=hd0 \ -netdev user,id=n0,hostfwd=tcp::22-:22 \ -device virtio-net-pci,netdev=n0
chmod +x container/containerfiles/openbsd-vm-start.sh
Build and check the image
docker build -t openbsd-vm -f container/containerfiles/Dockerfile.openbsd-vm container/containerfiles
The tools that the next pages call are in the image, and the versions are what Alpine 3.22 ships:
docker run --rm --entrypoint sh openbsd-vm -c 'qemu-system-x86_64 --version | head -1; qemu-img --version | head -1; ssh -V'
QEMU emulator version 10.0.0
qemu-img version 10.0.0
OpenSSH_10.0p2, OpenSSL 3.5.9 29 Sep 2026
Which accelerator the machine gets
Without the option --device /dev/kvm the script reports software emulation. Run it with a disk name that does not exist, to see the message and the refusal of QEMU, which is all that is checked here:
docker run --rm -e VM_DISK=/none.qcow2 openbsd-vm 2>&1 | head -3
openbsd-vm: accelerator tcg, 1024 MB, 2 CPUs
qemu-system-x86_64: -drive file=/none.qcow2,if=none,id=hd0,format=qcow2: Could not open '/none.qcow2': No such file or directory
With the device the first line says accelerator kvm. Page 1 makes the device exist.
Related
- Install OpenBSD into a base disk: the next step, which runs this image with the installer.
- Download OpenBSD and verify the signature: the previous step.
- Docker: images, containers and Dockerfiles.
Article author: Klavdiya Kondakova