Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contest/remote/lib/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <user>
```

```ini
[Unit]
Wants=user@1000.service
After=user@1000.service

[Service]
User=<user>
Environment=XDG_RUNTIME_DIR=/run/user/1000
```

43 changes: 40 additions & 3 deletions docker/common/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@
: "${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}"
-u "${RUID:-$(id -u)}:${RGID:-$(id -g)}"
--group-add "$(grep "^kvm:" /etc/group | cut -d: -f3)"
--rm
-i
--init
--log-driver none
--privileged
)
test -t 1 && ARGS+=("-t")
Expand All @@ -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() {
Expand All @@ -38,13 +48,40 @@ 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=<dir>' or '--opt=<dir>'
for arg in "${@}"; do
[ -d "${arg}" ] || arg="${arg##*=}"
if [ -d "${arg}" ]; then
d="$(realpath "${arg}")"
ARGS+=(-v "${d}:${d}:rw")
add_git_worktree "${arg}"
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 </dev/null
fi

# exec: signals sent to this PID are forwarded by the client to the container
exec docker run "${ARGS[@]}" "${DOCKER_IMG}" "${@:-bash}"
8 changes: 8 additions & 0 deletions docker/selftests/wrapper.sh
Original file line number Diff line number Diff line change
@@ -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}")" "${@}"
Loading