diff --git a/board/common/rootfs/etc/finit.d/available/container@.conf b/board/common/rootfs/etc/finit.d/available/container@.conf index 19facfb0..1942816d 100644 --- a/board/common/rootfs/etc/finit.d/available/container@.conf +++ b/board/common/rootfs/etc/finit.d/available/container@.conf @@ -1,4 +1,7 @@ -task name:container-%i :setup \ - [2345] container -n %i setup -- Setup container %i -sysv :%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 diff --git a/board/common/rootfs/usr/sbin/container b/board/common/rootfs/usr/sbin/container index 537a7347..b6ead39d 100755 --- a/board/common/rootfs/usr/sbin/container +++ b/board/common/rootfs/usr/sbin/container @@ -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 ;;