From e2f4e63c43b5424ba8524c538b00f4e0f723282c Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Tue, 17 Nov 2026 11:12:14 +0100 Subject: [PATCH 1/8] docker: run: only print the image name with a TTY This message is useful when the script is used interactively, but it is just noise when the output is parsed, e.g. by contest/remote/lib/vm.py when 'vng' is executed from the container. Signed-off-by: Matthieu Baerts --- docker/common/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/common/run.sh b/docker/common/run.sh index 231fa8c3..9300a9fb 100755 --- a/docker/common/run.sh +++ b/docker/common/run.sh @@ -26,7 +26,7 @@ if [ "${1}" = "--pull" ]; then fi DOCKER_IMG="${INPUT_PREFIX}/nipa-${DIR_NAME}:${INPUT_TAG}" -echo "Using ${DOCKER_IMG} image" >&2 +test -t 2 && echo "Using ${DOCKER_IMG} image" >&2 # $1: basedir add_git_worktree() { From ab29ea6b8138ff7c12e3903b880e9867a335958f Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Tue, 17 Nov 2026 11:12:24 +0100 Subject: [PATCH 2/8] docker: run: mount dirs passed as values Directories passed as arguments are mounted, but not when they are passed as values, e.g. 'make O=', 'INSTALL_PATH=', or '--opt='. That's needed to wrap 'make' with this script when executing contest/remote/vmksft-p.py: get_prog_list() installs the selftests in a temp dir created on the host with 'INSTALL_PATH=', then reads the generated kselftest-list.txt file from there. Without this mount, the file would only be created inside the container. Signed-off-by: Matthieu Baerts --- docker/common/run.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docker/common/run.sh b/docker/common/run.sh index 9300a9fb..e41e1405 100755 --- a/docker/common/run.sh +++ b/docker/common/run.sh @@ -38,8 +38,10 @@ add_git_worktree() { } add_git_worktree . -# trying to be smart: mounting directories passed in argument +# trying to be smart: mounting directories passed in argument, or as values, +# e.g. 'INSTALL_PATH=' or '--opt=' for arg in "${@}"; do + [ -d "${arg}" ] || arg="${arg##*=}" if [ -d "${arg}" ]; then d="$(realpath "${arg}")" ARGS+=(-v "${d}:${d}:rw") From 98f6a0f24bf8d8d605b6a4eb9a3b78dad9ff334a Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Tue, 17 Nov 2026 11:14:10 +0100 Subject: [PATCH 3/8] docker: selftests: add a generic wrapper To easily execute some commands from the selftests container instead of the host, e.g. 'make' and 'vng' when executing contest/remote/vmksft-p.py to use the toolchain, QEMU and virtme-ng from the container: symlinks named after these commands and pointing to this wrapper can be placed in a directory at the beginning of $PATH. This wrapper is not a symlink to a common script, because it would then be resolved by 'realpath', and the wrong image would be used. Signed-off-by: Matthieu Baerts --- docker/README.md | 11 +++++++++++ docker/selftests/wrapper.sh | 8 ++++++++ 2 files changed, 19 insertions(+) create mode 100755 docker/selftests/wrapper.sh diff --git a/docker/README.md b/docker/README.md index 84349200..9af7e3ac 100644 --- a/docker/README.md +++ b/docker/README.md @@ -25,6 +25,17 @@ To use them, that's easy, either: - Use the same scripts followed by the same commands as before to execute them directly from the containers. +- Wrap commands, e.g. to always execute `make` and `vng` from the selftests + container, create symlinks named after them, pointing to + `docker/selftests/wrapper.sh`, in a directory placed first in `$PATH`: + + ```console + $ mkdir -p ~/nipa-bin + $ ln -s "${nipa}/docker/selftests/wrapper.sh" ~/nipa-bin/make + $ ln -s "${nipa}/docker/selftests/wrapper.sh" ~/nipa-bin/vng + $ export PATH="${HOME}/nipa-bin:${PATH}" + ``` + - Use Docker or Podman directly, e.g. ```console diff --git a/docker/selftests/wrapper.sh b/docker/selftests/wrapper.sh new file mode 100755 index 00000000..8519bb0d --- /dev/null +++ b/docker/selftests/wrapper.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Generic wrapper to execute commands from the container: create symlinks named +# after the commands to wrap -- e.g. 'make' and 'vng' -- pointing to this script +# in a directory placed at the beginning of $PATH. + +exec "$(dirname "$(realpath "${0}")")/run.sh" "$(basename "${0}")" "${@}" From 03ed78b2e9eed9ca049e177544f615f709494b29 Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Tue, 17 Nov 2026 11:14:02 +0100 Subject: [PATCH 4/8] docker: run: remove the container when the client is killed When the Docker client is killed with SIGKILL, the container is not stopped. This is what contest/remote/lib/vm.py does when a VM doesn't stop: all its processes are killed with SIGKILL. When 'vng' is executed from a container, QEMU then continues to run in the background, using CPU and memory resources. Such signal cannot be trapped, nor forwarded by the client. Instead, a detached process -- so not part of the killed process tree -- waits for the client to be gone, then removes the container, identified by a new unique name. This process also removes the container if it is asked to stop, e.g. when all processes of a systemd service are terminated. This watchdog is not started if one of the commands it uses is wrapped with wrapper.sh: it would call itself recursively. A warning is printed instead. While at it, the client is now started with 'exec': the signals sent to the script's PID are then received by the client, which forwards them to the container. '--init' is now used to avoid having these signals ignored by the command, executed as PID 1 in the container otherwise. Signed-off-by: Matthieu Baerts --- docker/common/run.sh | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/docker/common/run.sh b/docker/common/run.sh index e41e1405..977414ef 100755 --- a/docker/common/run.sh +++ b/docker/common/run.sh @@ -7,8 +7,10 @@ SCRIPT_DIR=$(cd "$(dirname "${0}")" && pwd) DIR_NAME="$(basename "${SCRIPT_DIR}")" CCACHE_DIR="${SCRIPT_DIR}/.ccache" +CONTAINER_NAME="nipa-${DIR_NAME}-${$}-${RANDOM}" mkdir -p "${CCACHE_DIR}" ARGS=( + --name "${CONTAINER_NAME}" -v "${CCACHE_DIR}:/home/nipa/.ccache:rw" -v "${PWD}:${PWD}:rw" -w "${PWD}" @@ -16,6 +18,7 @@ ARGS=( --group-add "$(grep "^kvm:" /etc/group | cut -d: -f3)" --rm -i + --init --privileged ) test -t 1 && ARGS+=("-t") @@ -49,4 +52,29 @@ for arg in "${@}"; do fi done -docker run "${ARGS[@]}" "${DOCKER_IMG}" "${@:-bash}" +# $@: commands, returns 0 if one of them would execute a wrapper.sh script +is_wrapped() { + local cmd path + for cmd in "${@}"; do + path="$(command -v "${cmd}")" || continue + [ "$(basename "$(realpath "${path}")")" = "wrapper.sh" ] && return 0 + done + return 1 +} + +# When the client is killed with SIGKILL -- e.g. by contest/remote/lib/vm.py +# when a VM doesn't stop -- the container is not stopped. A detached process, +# not part of the client's process tree, removes it when the client is gone. +# It is not started if it would call itself, via wrapped commands. +if is_wrapped setsid bash sleep docker; then + echo "Warning: wrapped commands, the container will not be removed if the client is killed" >&2 +else + # shellcheck disable=SC2016 # expanded by the detached process + setsid -f bash -c ' + trap "docker rm -f ${2}; exit" HUP INT TERM + while kill -0 "${1}"; do sleep 1; done + docker rm -f "${2}"' cleanup "${$}" "${CONTAINER_NAME}" &>/dev/null Date: Tue, 17 Nov 2026 11:46:10 +0100 Subject: [PATCH 5/8] contest: vm: give priority to extra paths The extra paths from '[env] paths' were appended to $PATH: they could then not be used to override commands from the host, e.g. to execute 'make' and 'vng' from a container via docker/selftests/wrapper.sh. Prepend them instead, like it is already done in contest/remote/exec.py. Signed-off-by: Matthieu Baerts --- contest/remote/lib/vm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contest/remote/lib/vm.py b/contest/remote/lib/vm.py index d754f32b..82fe4105 100644 --- a/contest/remote/lib/vm.py +++ b/contest/remote/lib/vm.py @@ -81,7 +81,7 @@ def __init__(self, config, vm_name=""): def tree_popen(self, cmd): env = os.environ.copy() if self.config.get('env', 'paths'): - env['PATH'] += ':' + self.config.get('env', 'paths') + env['PATH'] = self.config.get('env', 'paths') + ':' + env['PATH'] return subprocess.Popen(cmd, env=env, cwd=self.tree_path, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) From c5af28adb27bf353156d46bf3661b920c63f529b Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Tue, 17 Nov 2026 12:13:22 +0100 Subject: [PATCH 6/8] docker: run: disable container logs The output of the container is sent to the attached client, but it is also sent to a log driver by default. Depending on the config, it is then written a second time to the disk -- 'json-file', without size limit by default -- or to the journal -- 'journald', the default with Podman -- where it is kept after the removal of the container. That's not needed here: the output is only used via the client, and the container is removed at the end. That's also not wanted when executing 'vng' from the container: the whole serial console of each VM would be duplicated. Signed-off-by: Matthieu Baerts --- docker/common/run.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/docker/common/run.sh b/docker/common/run.sh index 977414ef..6f3922f3 100755 --- a/docker/common/run.sh +++ b/docker/common/run.sh @@ -19,6 +19,7 @@ ARGS=( --rm -i --init + --log-driver none --privileged ) test -t 1 && ARGS+=("-t") From 5c20a1dbd191ded98f15d410fbc464705fc6f3e9 Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Tue, 17 Nov 2026 12:15:47 +0100 Subject: [PATCH 7/8] docker: run: support rootless Podman With rootless Podman, the user's UID is mapped to root in the container by default, and '-u $(id -u)' is then mapped to another UID on the host, which cannot write to the mounted directories, e.g. the kernel tree. '--userns=keep-id' fixes that by keeping the same UID/GID in the container. Podman uses the PODMAN_USERNS environment variable as the default value for this option, while Docker ignores it: the same script can then be used with both, e.g. with Podman via the podman-docker package providing the 'docker' command. This is only done for non-root users, not to change the behaviour with rootful Podman. Signed-off-by: Matthieu Baerts --- docker/common/run.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docker/common/run.sh b/docker/common/run.sh index 6f3922f3..b3ab673f 100755 --- a/docker/common/run.sh +++ b/docker/common/run.sh @@ -4,6 +4,12 @@ : "${INPUT_PREFIX:="ghcr.io/linux-netdev"}" : "${INPUT_TAG:="latest"}" +# Rootless Podman: keep the same UID/GID in the container, to be able to write +# to the mounted directories. This variable is ignored by Docker. +if [ "$(id -u)" != 0 ]; then + export PODMAN_USERNS="${PODMAN_USERNS:-keep-id}" +fi + SCRIPT_DIR=$(cd "$(dirname "${0}")" && pwd) DIR_NAME="$(basename "${SCRIPT_DIR}")" CCACHE_DIR="${SCRIPT_DIR}/.ccache" From 28b814df8847ec05aa96a16c7b6d67b410b60f19 Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Tue, 17 Nov 2026 12:17:06 +0100 Subject: [PATCH 8/8] docker: readme: document Podman support The scripts can be used with Podman, via the 'docker' command provided by the podman-docker package. With rootless Podman, '--userns=keep-id' is set by the scripts, but it has to be added when using Podman directly, to be able to write to the mounted directories. When used from a systemd service with 'User=', lingering has to be enabled for this user, and the unit has to set its runtime directory: Podman can then use the user's systemd instance, e.g. to manage the containers' cgroups, without printing warnings at each call. Signed-off-by: Matthieu Baerts --- docker/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docker/README.md b/docker/README.md index 9af7e3ac..1b0bf333 100644 --- a/docker/README.md +++ b/docker/README.md @@ -72,3 +72,30 @@ using a Fedora base, so it is recommended to do the same. If you prefer to build container images locally, you can use the `docker/build.sh` script. + +## Podman + +Podman can be used instead of Docker: the scripts call `docker`, a command +provided by the `podman-docker` package. With rootless Podman, +`--userns=keep-id` is needed to keep the same UID/GID in the container, and be +able to write to the mounted directories: the scripts set it by default, but it +has to be added when using Podman directly. + +To use rootless Podman from a systemd service with `User=`, enable lingering for +this user, and set its runtime directory in the unit, so Podman can use the +user's systemd instance, e.g. for the UID 1000: + +```console +$ sudo loginctl enable-linger +``` + +```ini +[Unit] +Wants=user@1000.service +After=user@1000.service + +[Service] +User= +Environment=XDG_RUNTIME_DIR=/run/user/1000 +``` +