guest: run Debian's init scripts under sysv-rc from kw-init, so that a package's service starts on install and at boot

This commit is contained in:
Pooya Parsa
2026-11-19 21:24:23 +00:00
parent 1c1725a182
commit 85f53a1835
4 changed files with 68 additions and 10 deletions

View File

@@ -245,12 +245,21 @@ layer: boot to a prompt 0.25 s (Alpine) and 0.32 s (Debian) from
serve` (respawned); `poweroff`/`halt`/`reboot` in `/usr/local/sbin`
signal it (through `sudo -n` for the user), and it stops
everything, runs the layer's shutdown and calls reboot(2) through the
base's busybox. apt: `90kw` (no recommends, pdiffs or pty), debconf
base's busybox. Services are Debian's init scripts under sysv-rc (with
insserv and startpar, in `DEBIAN_PACKAGES`): kw-init runs runlevel 2
after net and 0 or 6 before it stops everything, only when that rc
directory has links to run; runlevel S (rcS.d) never runs, the layer's
rc does early boot. `/usr/sbin/runlevel` prints what kw-init writes to
`/run/kw-runlevel` (`N 2`), so that a package's invoke-rc.d starts its
service on install (`apt-get install cron`: running, and started at
every boot with a data disk). sudo's script is disabled (it only resets
`/run/sudo`). apt: `90kw` (no recommends, pdiffs or pty), debconf
Noninteractive, the package lists of the image's date baked in
(`DEBIAN_LISTS`, compressed by `tools/apt-lz4` as apt does) and apt's
source cache made by the image's own apt at build time
(`guest/tools/vm-run.ts` on the base bundle), so `apt-get install`
works with no `apt-get update` first. The image's `policy-rc.d` stays.
works with no `apt-get update` first. The image's `policy-rc.d`
(docker's `exit 101`) is removed after the build's own install.
`DEBIAN_PACKAGES` in build.sh (curl, ca-certificates, unzip) are
installed at build time by the image's apt in the same build VM, from
.debs the host fetches from snapshot.debian.org (checked against the

View File

@@ -16,7 +16,9 @@
# rc, welcome and net, a login shell on hvc0 and
# kwtty serve (respawned); poweroff, halt and
# reboot in /usr/local/sbin signal it, and it
# runs the layer's shutdown, then reboot(2)
# runs the layer's shutdown, then reboot(2);
# services: sysv-rc's runlevels 2, then 0 or 6
# (/usr/sbin/runlevel; no policy-rc.d)
# /usr/lib/kw-distro "debian"
# /usr/lib/kw-image debian-<version>-<hash of the tree>
# /var/lib/apt/lists the package lists of the image's date, as
@@ -25,8 +27,8 @@
# by the image's apt at build time (needs the
# base bundle and node; else left out)
# curl, ca-certificates, unzip, procps, less, vim-tiny, xz-utils, zstd,
# iproute2, htop, tmux, sudo, libatomic1 installed (DEBIAN_PACKAGES,
# below; needs the same)
# iproute2, htop, tmux, sudo, libatomic1, sysv-rc installed
# (DEBIAN_PACKAGES, below; needs the same)
# /usr/lib/kw-commands "NAME PACKAGE" per command of main, for
# kw-apt-install (command not found:
# etc/profile.d/kw-apt.sh; tools/commands.py)
@@ -190,7 +192,9 @@ install_common "$stage"
# The packages the image comes with, on top of the tarball's (with their
# dependencies, no recommends). Adding one is adding its name here.
# libatomic1: the official Node.js build links it (the kw layer's node shim).
DEBIAN_PACKAGES="curl ca-certificates unzip procps less vim-tiny xz-utils zstd iproute2 htop tmux sudo libatomic1"
# sysv-rc (with insserv and startpar): the service runner kw-init drives
# (below, and rootfs/usr/local/sbin/kw-init).
DEBIAN_PACKAGES="curl ca-certificates unzip procps less vim-tiny xz-utils zstd iproute2 htop tmux sudo libatomic1 sysv-rc"
# Both are made by the image's own programs: the tree as it is at that
# point becomes a first image (erofs, uncompressed), which boots on the
# distroless bundle (guest/tools/vm-run.ts); files come out over exec's
@@ -277,6 +281,7 @@ else
$DEBIAN_PACKAGES > /tmp/kw-log 2>&1 ||
{ cat /tmp/kw-log; exit 1; }
[ \"\$(dpkg-query -W -f '\${db:Status-Abbrev}\n' $DEBIAN_PACKAGES | sort -u)\" = 'ii ' ]
update-rc.d sudo disable >> /tmp/kw-log 2>&1 || { cat /tmp/kw-log; exit 1; }
cd $u
find . -mindepth 1 -cnewer /tmp/kw-stamp -type c | sort > /tmp/kw-gone
find . -mindepth 1 -cnewer /tmp/kw-stamp ! -type c ! -path './var/log/*' \\
@@ -289,6 +294,17 @@ else
rm -f "$distro_out/$gen".{erofs,tar,id,uris,sums,pkgs.tar,gone}
fi
# --- services ------------------------------------------------------------------
# Debian's packages enable their init scripts (update-rc.d: insserv's links
# in /etc/rcN.d) and start them (invoke-rc.d), and kw-init runs runlevel 2
# at boot. The image's policy-rc.d (docker's: exit 101, start nothing) goes,
# so that an apt-get install starts its service as on a Debian system; it
# stays for the build's own install above. sudo's script (enabled by the
# build, disabled above) only clears /run/sudo, which is new at every boot.
policy="$stage/usr/sbin/policy-rc.d"
[ ! -e "$policy" ] || { grep -qx 'exit 101' "$policy" && rm "$policy"; } ||
{ echo "debian: $policy is not docker's exit 101 (upstream changed it?)" >&2; exit 1; }
# --- the user -------------------------------------------------------------------
# After the packages, whose scripts may add system users and groups. The
# group input, which udev's package would add (the kw layer's rc gives it

View File

@@ -2,10 +2,14 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
# PID 1 of the Debian image (/sbin/init -> /usr/local/sbin/kw-init; Debian's
# /sbin is a link to /usr/sbin; the image has no init system). The kw
# layer's runtime (/usr/lib/kw/, from the base) does the rest: rc (mounts,
# hostname, binfmt), welcome, net (DHCP), and at the end, after this has
# stopped every process, shutdown (leave /data clean). In between it
# /sbin is a link to /usr/sbin). The kw layer's runtime (/usr/lib/kw/, from
# the base) does the rest: rc (mounts, hostname, binfmt), welcome, net
# (DHCP), and at the end, after this has stopped every process, shutdown
# (leave /data clean). Services are Debian's init scripts as sysv-rc runs
# them: runlevel 2 after net (/etc/rc2.d, in insserv's order), 0 or 6 at
# the end; runlevel S (rcS.d, initscripts' early boot) is the kw layer's rc
# and never runs. /usr/sbin/runlevel reports the level from /run/kw-runlevel,
# so that a package's invoke-rc.d starts what it installs. In between it
# respawns the console's login shell on hvc0 (kwtty login: the user "user",
# or kw.user= on the kernel command line) and kwtty serve, until poweroff,
# halt or reboot (/usr/local/sbin) signal it (SIGUSR1, SIGUSR2); then
@@ -21,6 +25,19 @@ fi
/usr/lib/kw/welcome
/usr/lib/kw/net
# Runs sysv-rc's rc for a runlevel change from $1 to $2, only when that
# level's directory has a script to run (a boot without services starts no
# shell for it): at boot a start link (a disabled service has K links in
# rc2.d), at the end any.
rc() {
echo "$1 $2" > /run/kw-runlevel
local links="/etc/rc$2.d/[SK][0-9][0-9]*"
[ "$1" != N ] || links="/etc/rc$2.d/S[0-9][0-9]*"
compgen -G "$links" >/dev/null || return 0
RUNLEVEL=$2 PREVLEVEL=$1 /etc/init.d/rc "$2"
}
rc N 2
action=
trap 'action=poweroff' USR1
trap 'action=reboot' USR2
@@ -52,6 +69,7 @@ while [ -z "$action" ]; do
done
trap '' USR1 USR2
if [ "$action" = reboot ]; then rc 2 6; else rc 2 0; fi
exec 3>&2 2>/dev/null # no "Killed" notices for the children
kill -TERM -1
sleep 0.2

View File

@@ -0,0 +1,15 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
# runlevel under kw-init: the previous and current runlevel, which kw-init
# writes to /run/kw-runlevel (sysvinit's runlevel reads them from utmp).
# invoke-rc.d asks it, and starts a package's service only in a runlevel
# whose rc directory has the service's start link.
# unkernel: runlevel under kw-init.
if [ -s /run/kw-runlevel ]; then
cat /run/kw-runlevel
else
echo unknown
exit 1
fi