diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-09-15 14:05:52 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-09-15 14:05:52 +0200 |
| commit | 717968007c67fca3067777cffa3dd40582968f4a (patch) | |
| tree | f032870cf59097cef1e5fd52e695abf8ebce9744 | |
| parent | d893a0e08fbf0ae7fc85b62a0814ab31d758169a (diff) | |
Support Custom Teardown
Similar to how we added an "RC" system to the init script, we
now have a custom tear-down script (which can do things like
unmount loop devices).
Furthermore, we make sure that an `$XDG_RUNTIME_DIR/dbx` exists
for things like SSH control sockets (so they can go somewhere
where an application has R/W access to, while only keeping RO
access to the configurations).
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | container/Containerfile | 9 | ||||
| -rwxr-xr-x | container/init | 22 | ||||
| -rwxr-xr-x | container/stop | 20 | ||||
| -rwxr-xr-x | dbx | 26 |
5 files changed, 57 insertions, 26 deletions
@@ -25,6 +25,6 @@ container: container/Containerfile container/init $(CONTAINER) build --tag $(IMAGE) container .PHONY: check -check: dbx - shellcheck dbx - shfmt -d dbx +check: dbx container/init container/stop + shellcheck $^ + shfmt -d $^ diff --git a/container/Containerfile b/container/Containerfile index f7fc00f..8aa03ec 100644 --- a/container/Containerfile +++ b/container/Containerfile @@ -1,8 +1,5 @@ FROM docker.io/library/debian:13 -# Flag that we are a devbox -RUN touch /run/.dbxenv - # Remove container-specific APT configurations, and install required packages RUN rm /etc/apt/apt.conf.d/docker-* && \ apt-get update && \ @@ -40,7 +37,7 @@ RUN echo "StreamLocalBindUnlink yes" >> /etc/ssh/sshd_config && \ echo "AuthorizedKeysFile /etc/ssh/authorized_keys" >> /etc/ssh/sshd_config && \ touch /etc/ssh/authorized_keys -# Set up the entrypoint -RUN mkdir -p /etc/dbx/rc.d -COPY init /sbin/init +# Set up the init and stop +RUN mkdir -p /etc/dbx +COPY init stop /sbin ENTRYPOINT ["/sbin/init"] diff --git a/container/init b/container/init index 49ee840..3f17b48 100755 --- a/container/init +++ b/container/init @@ -2,16 +2,22 @@ # SPDX-License-Identifier: GPL-3.0-only # SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> -if [ ! -f /run/.dbxenv ]; then - echo "ERROR: not in a devbox" >&2 +if [ "$(id -u)" -ne 0 ]; then + printf '%s: operation not permitted\n' "$0" >&2 exit 1 fi -for rc in /etc/dbx/rc.d/*; do - if [ ! -f "$rc" ] || [ ! -x "$rc" ]; then - continue - fi - "$rc" || printf '%s: %s failed (%d)\n' "$0" "$rc" "$?" >&2 -done +touch /run/.dbxenv +printf "%d\n" "$$" >/run/dbx.pid + +if [ -f /etc/dbx/init.local ] && [ -x /etc/dbx/init.local ]; then + /etc/dbx/init.local || + printf '%s: init.local failed (%d)\n' "$0" "$?" >&2 +fi + +# The init process is not guaranteed to be on PID 1 for all runtimes (notably, +# `krun` will wrap the init process). Tell tini to act like an init process +# regardless of its PID. +export TINI_SUBREAPER=yes exec /usr/bin/tini /usr/sbin/sshd -- -D -e diff --git a/container/stop b/container/stop new file mode 100755 index 0000000..9affb9c --- /dev/null +++ b/container/stop @@ -0,0 +1,20 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> + +if [ "$(id -u)" -ne 0 ]; then + printf '%s: operation not permitted\n' "$0" >&2 + exit 1 +fi + +if [ -x /etc/dbx/stop.local ]; then + /etc/dbx/stop.local || + printf '%s: stop.local failed (%d)\n' "$0" "$?" >&2 +fi + +if ! pid="$(cat /run/dbx.pid)"; then + printf '%s: could not read init PID file, assuming 1\n' "$0" >&2 + pid=1 +fi +kill -TERM "$pid" || + printf '%s: could not stop init[%s] (%d)\n' "$0" "$pid" "$?" >&2 @@ -20,6 +20,7 @@ args=() OS="$(uname -s)" DATADIR="${XDG_DATA_HOME:-$HOME/.local/share}/dbx" CONFIGDIR="${XDG_CONFIG_HOME:-$HOME/.config}/dbx" +RUNTIMEDIR="${XDG_RUNTIME_DIR:-/run/user/$UID}/dbx" usage() { cat <<USAGE @@ -255,6 +256,10 @@ parse_opts() { fi } +dbx_mkdirs() { + mkdir -p "$DATADIR" "$CONFIGDIR" "$RUNTIMEDIR" +} + ssh_id() { printf "%s\n" "$DATADIR/$name.id_ed25519" } @@ -279,7 +284,6 @@ ssh_knownhosts() { } ssh_init() { - mkdir -p "$DATADIR" "$CONFIGDIR" rm -f "$(ssh_id)" "$(ssh_id).pub" "$(ssh_knownhosts)" ssh-keygen -C dbx -f "$(ssh_id)" -N "" -t ed25519 \ </dev/null >/dev/null 2>/dev/null @@ -328,6 +332,7 @@ cmd_create() { general_error "devbox name in use" fi + dbx_mkdirs ssh_init engine_create @@ -395,12 +400,23 @@ cmd_start() { return 0 fi + dbx_mkdirs engine_start ssh_configure engine_waitforsshd } cmd_stop() { + # Stop the devbox, ensuring that any configured local stop script is + # run. Furthermore, the `krun` runtime does not support sending the + # `SIGTERM` signal to container processes for `podman stop` to work, + # so this also ensures that the init process is correctly terminated. + ssh -q -F "$(ssh_config)" "root@$name" -- \ + "/sbin/stop" \ + &>/dev/null || true + + # Additionally, defensively ask the engine to stop the devbox in case + # something went wrong. engine_stop } @@ -564,14 +580,6 @@ CONFIG } engine_stop() { - # `krun` runtime does not support sending `SIGTERM` signals to - # container processes for `podman stop` to work. Work around this - # by using SSH to terminate our init process. This is expected to - # produce an error, which we ignore. Additionally, `podman stop` - # defensively in case something went wrong with the kill. - ssh -q -F "$(ssh_config)" "root@$name" -- \ - "kill -TERM 1" \ - &>/dev/null || true podman stop "$name" >/dev/null } |
