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) diff --git a/docker/README.md b/docker/README.md index 84349200..1b0bf333 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 @@ -61,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 +``` + diff --git a/docker/common/run.sh b/docker/common/run.sh index 231fa8c3..b3ab673f 100755 --- a/docker/common/run.sh +++ b/docker/common/run.sh @@ -4,11 +4,19 @@ : "${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" +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 +24,8 @@ ARGS=( --group-add "$(grep "^kvm:" /etc/group | cut -d: -f3)" --rm -i + --init + --log-driver none --privileged ) test -t 1 && ARGS+=("-t") @@ -26,7 +36,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() { @@ -38,8 +48,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") @@ -47,4 +59,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