container: refactor setup stage to use Finit pre: script

When updating a container configuration we need to rerun the setup stage
of the container.  However, a run/task in Finit only runs once for each
runlevel, meaning changes at runtime of a container was broken.

This is a refactor of the container script to allow it to also run as a
pre:script in the Finit setup stage of a sysv/service.  Also included is
a fix to the timeout/retry handling in case the container setup fails.
We must call `read -t 60` *before* calling `ip monitor`, otherwise it
will not be called until `ip monitor` returns.

This patch requires Finit v4.9, or later.

Fixes #930

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2025-02-24 22:22:14 +01:00
parent 53d0995d0c
commit 3811df9ab2
2 changed files with 21 additions and 12 deletions
@@ -1,4 +1,7 @@
task name:container-%i :setup \
[2345] container -n %i setup -- Setup container %i
sysv <!usr/container:%i> :%i pid:!/run/container:%i.pid log:prio:local1,tag:%i kill:10 \
[2345] container -n %i -- container %i
# Start a container instance (%i) and redirect logs to /log/container
# Give podman enough time to properly shut down the container. Every
# time we start a container we run the setup stage, disable the Finit
# timeout to allow the setup stage to run to completion.
sysv log:prio:local1,tag:%i kill:10 pid:!/run/container:%i.pid \
[2345] <!> :%i pre:0,/usr/sbin/container container -n %i \
-- container %i
+14 -8
View File
@@ -9,6 +9,7 @@
DOWNLOADS=/var/lib/containers/oci
BUILTIN=/lib/oci
TMPDIR=/var/tmp
container=$0
checksum=""
extracted=
timeout=30
@@ -202,8 +203,8 @@ unpack_archive()
running()
{
run=$(podman inspect "$1" 2>/dev/null |jq .[].State.Running)
[ "$run" = "true" ] && return 0
status=$(podman inspect -f '{{.State.Status}}' "$1" 2>/dev/null)
[ "$status" = "running" ] && return 0
return 1
}
@@ -316,7 +317,7 @@ start()
return
fi
initctl -bq cond set "container:$name"
initctl start container:$name
# Real work is done by wrap() courtesy of finit sysv emulation
}
@@ -329,7 +330,7 @@ stop()
return
fi
initctl -bq cond clr "container:$name"
initctl stop container:$name
# Real work is done by wrap() courtesy of finit sysv emulation
}
@@ -338,7 +339,8 @@ wrap()
name=$1
cmd=$2
podman "$cmd" "$name"
# Skip "echo $name" from podman start in log
podman "$cmd" "$name" >/dev/null
}
# Removes network $1 from all containers
@@ -701,12 +703,12 @@ case $cmd in
script=/run/containers/${name}.sh
[ -x "$script" ] || err 1 "setup: $script does not exist or is not executable."
while ! "$script"; do
# Wait for address/route changes, or retry every 60 secods
# shellcheck disable=2162,3045
ip monitor address route | while read -t 60 _; do break; done
log "${name}: setup failed, waiting for network changes ..."
read -t 60 _ < <(ip monitor address route)
# On IP address/route changes, wait a few seconds more to ensure
# the system has ample time to react and set things up for us.
log "${name}: retrying ..."
sleep 2
done
;;
@@ -832,6 +834,10 @@ case $cmd in
esac
;;
*)
if [ -n "$SERVICE_SCRIPT_TYPE" ] && [ -n "$SERVICE_ID" ]; then
# Called as pre-script from Finit service
exec $container -n "$SERVICE_ID" setup
fi
usage
exit 1
;;