Overlay disks, Compose and SSH from a worker container into OpenBSD

The base disk stays as it was installed. Every task gets a thin overlay file on top of it: the virtual machine writes into the overlay, and deleting the overlay gives a clean machine in a second. This page makes the overlay, starts the machine and a second container with Docker Compose, and runs the first real checks in OpenBSD from that container, the way a worker would.

Make the overlay

An overlay is a qcow2 file that names the base disk as its backing file. Reads that the overlay does not hold come from the base, and every write lands in the overlay. The path of the backing file is the path inside the container, /vm/base.qcow2, so make the overlay from a container that has the folder mounted at the same place:

cd ~/.local/share/aredel/openbsd

docker run --rm --user "$(id -u):$(id -g)" -v "$PWD/vm:/vm" --entrypoint qemu-img openbsd-vm create -f qcow2 -b /vm/base.qcow2 -F qcow2 /vm/overlay.qcow2

Formatting '/vm/overlay.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=10737418240 backing_file=/vm/base.qcow2 backing_fmt=qcow2 lazy_refcounts=off refcount_bits=16

ls -l vm

-rw-r--r-- 1 user user 3283484672 Nov 25 12:14 base.qcow2
-rw-r--r-- 1 user user 196768 Nov 25 12:14 overlay.qcow2

The overlay is 192 KiB while the base is 3.1 GiB, and it grows only with what the task writes.

The Compose files

Three small files go next to the Dockerfile of page 3. The first one describes the two services. openbsd is the machine; its healthcheck logs in with the lab key and runs true, so the container is called healthy only when the guest has booted and its SSH server answers. client stands for the worker: it starts only when the machine is healthy and sees the same key. The two variables name the folder of the disks and the private key, and Compose stops with a message if they are missing.

vi container/containerfiles/compose.openbsd.yml

services: openbsd: build: context: . dockerfile: Dockerfile.openbsd-vm image: openbsd-vm environment: VM_DISK: /vm/overlay.qcow2 volumes: - ${OBSD_DIR:?set OBSD_DIR to the folder with base.qcow2}:/vm - ${OBSD_KEY:?set OBSD_KEY to the private key file}:/keys/lab_key:ro healthcheck: test: ["CMD", "ssh", "-i", "/keys/lab_key", "-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-o", "ConnectTimeout=5", "worker@127.0.0.1", "true"] interval: 15s timeout: 10s retries: 80 start_period: 120s client: image: openbsd-vm entrypoint: ["sleep", "infinity"] volumes: - ${OBSD_KEY:?set OBSD_KEY to the private key file}:/keys/lab_key:ro - ./obsd:/usr/local/bin/obsd:ro depends_on: openbsd: condition: service_healthy

The second file adds the KVM device and is used only on a machine that has the file /dev/kvm; Compose refuses a device that does not exist, so it is not in the first file.

vi container/containerfiles/compose.openbsd.kvm.yml

services: openbsd: devices: - /dev/kvm

The third file is the command the worker types. obsd is ssh with the key, the user and the host name already filled in, and with the host key check switched off: the guest is created from the same disk every time, and nothing outside the Compose network can reach it.

vi container/containerfiles/obsd

#!/bin/sh exec ssh -i "${OBSD_KEY:-/keys/lab_key}" -o BatchMode=yes \ -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \ "worker@${OBSD_HOST:-openbsd}" "$@"

chmod +x container/containerfiles/obsd

Start the machine

export OBSD_DIR=$HOME/.local/share/aredel/openbsd/vm OBSD_KEY=$HOME/.config/aredel/openbsd/lab_key

docker compose -p obsd -f container/containerfiles/compose.openbsd.yml config -q && echo config-ok

config-ok

Without KVM:

docker compose -p obsd -f container/containerfiles/compose.openbsd.yml up -d

With KVM, put the second file after the first one, because Compose merges the files in order:

docker compose -p obsd -f container/containerfiles/compose.openbsd.yml -f container/containerfiles/compose.openbsd.kvm.yml up -d

The command starts the machine and then waits, because client starts only when the machine is healthy. The healthcheck turns it healthy after the guest has booted, which took about 5 minutes here without KVM. From a second terminal the state is:

docker inspect -f '{{.State.Health.Status}}' obsd-openbsd-1

healthy

The guest console is in the log, with the line the start script printed first telling which accelerator the machine got:

docker logs obsd-openbsd-1 2>&1 | grep -a 'openbsd-vm:'

openbsd-vm: accelerator tcg, 1024 MB, 1 CPUs

Run commands in OpenBSD

From now on every command goes through obsd in the client. A command is one round trip of SSH, which took 1.7 seconds here with software emulation:

docker compose -p obsd -f container/containerfiles/compose.openbsd.yml exec client obsd uname -srm

OpenBSD 8.0 amd64

A file the client has goes in on the standard input. The option -T makes exec pass the input through. This checks a pf.conf with a typo with pfctl -nf, which parses the rules and loads nothing:

printf 'block in all\npass out quik\n' | docker compose -p obsd -f container/containerfiles/compose.openbsd.yml exec -T client obsd 'cat > /tmp/pf.test; doas pfctl -nf /tmp/pf.test; echo exit=$?'

/tmp/pf.test:2: syntax error
exit=1

The same rules with the typo fixed give an empty output and the exit status 0. The next three checks have the same shape. A shell script with an unmatched if goes to ksh -n:

printf 'if true; then\n echo x\n' | docker compose -p obsd -f container/containerfiles/compose.openbsd.yml exec -T client obsd 'cat > /tmp/bad.sh; ksh -n /tmp/bad.sh; echo exit=$?'

/tmp/bad.sh[1]: syntax error: `if' unmatched
exit=1

An sshd_config with a wrong value goes to sshd -t -f, which needs doas to read the host keys:

printf 'port 22\nPermitRootLogin maybe\n' | docker compose -p obsd -f container/containerfiles/compose.openbsd.yml exec -T client obsd 'cat > /tmp/t.conf; doas sshd -t -f /tmp/t.conf; echo exit=$?'

/tmp/t.conf line 2: unsupported option "maybe".
exit=255

A script with two common mistakes goes to shellcheck:

printf '#!/bin/sh\necho $x | cat\n' | docker compose -p obsd -f container/containerfiles/compose.openbsd.yml exec -T client obsd 'shellcheck -s sh -'

In - line 2:
echo $x | cat
^-- SC2154 (warning): x is referenced but not assigned.
^-- SC2086 (info): Double quote to prevent globbing and word splitting.

mandoc -Tlint does the same for a manual page, and pkg_info and rcctl answer for the installed packages and the services. A command that changes the machine, such as pkg_add, changes the overlay only.

Throw the machine away

The task is over, or a command broke the system: stop the containers, delete the overlay and make a new one. The base disk is not touched, and a new machine takes the time of one boot.

docker compose -p obsd -f container/containerfiles/compose.openbsd.yml down

rm $OBSD_DIR/overlay.qcow2

Then repeat the overlay command and up -d. The files of the two disks together took 3.1 GiB here; a running machine used about 900 MiB of memory.

What the key does and does not allow

Password login is refused for every user (Permission denied (publickey) was the answer of a try with the key switched off) and the machine publishes no port on the host, so it is reachable only from containers of the same Compose project. The client has the private key; the base disk has the public half and the host name obsd-lab in its configuration.

Related

Article author: Klavdiya Kondakova