From 398fce482e8d8809bcec769155d7adbe913bf1af Mon Sep 17 00:00:00 2001
From: Helmut Grohne <helmut@subdivi.de>
Date: Thu, 23 Feb 2023 20:09:58 +0100
Subject: debvm-create: also support --initsystem=runit

---
 bin/debvm-create             | 11 +++++++----
 share/customize-autologin.sh | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/bin/debvm-create b/bin/debvm-create
index 75f153b..fffa494 100755
--- a/bin/debvm-create
+++ b/bin/debvm-create
@@ -37,7 +37,7 @@ A suitable kernel image is automatically selected and installed into the image.
 Set the hostname of the virtual machine.
 By default, the hostname is B<testvm>.
 
-=item B<--initsystem>=B<systemd> | B<sysv> | B<none>
+=item B<--initsystem>=B<systemd> | B<runit> | B<sysv> | B<none>
 
 Select an init system to be used.
 The default is B<systemd> independently of the Debian release.
@@ -85,7 +85,7 @@ Such a tar archive is not suitable for being booted by B<debvm-run>.
 =item B<ifupdown>
 
 skips installing B<ifupdown> configuration to automatically configure wired interfaces.
-This is specific to using B<sysv> as init system.
+This is specific to using B<runit> or B<sysv> as init system.
 
 =item B<initsystem>
 
@@ -184,7 +184,7 @@ opt_hostname() {
 }
 opt_initsystem() {
 	case "$1" in
-		systemd|sysv|none)
+		systemd|sysv|runit|none)
 		;;
 		*)
 			die "value for --initsystem must be one of systemd, sysv or none"
@@ -284,6 +284,9 @@ set -- \
 	"$@"
 
 case "$INITSYSTEM" in
+	runit)
+		set -- --include=runit-init,passwd "$@"
+	;;
 	systemd)
 		set -- --include=systemd-sysv "$@"
 	;;
@@ -311,7 +314,7 @@ if test "$INITSYSTEM" = systemd && ! check_skip systemdnetwork; then
 		'--include=?not(?virtual)?exact-name(libnss-resolve)' \
 		"--customize-hook=$SHARE_DIR/customize-resolved.sh" \
 		"$@"
-elif test "$INITSYSTEM" = sysv && ! check_skip ifupdown; then
+elif test "$INITSYSTEM" = sysv -o "$INITSYSTEM" = runit && ! check_skip ifupdown; then
 	set -- \
 		'--include=ifupdown,isc-dhcp-client' \
 		"--customize-hook=$SHARE_DIR/customize-ifupdown.sh" \
diff --git a/share/customize-autologin.sh b/share/customize-autologin.sh
index c0b4d28..9effa29 100755
--- a/share/customize-autologin.sh
+++ b/share/customize-autologin.sh
@@ -35,5 +35,20 @@ if test "$(dpkg-query --root "$TARGET" -f '${db:Status-Status}' -W sysvinit-core
 	exit 0
 fi
 
+if test "$(dpkg-query --root "$TARGET" -f '${db:Status-Status}' -W runit-init 2>/dev/null)" = installed; then
+	if ! test -f "$TARGET/etc/sv/getty-ttyS0/run"; then
+		echo "failed to guess serial console name for runit" 1>&2
+		exit 1
+	fi
+
+	# shellcheck disable=SC2016  # intentional non-expansion
+	sed -i \
+		-e 's,exec.*/sbin/getty ,&-a root ,' \
+		-e '/exec 1>&2/i: "${TERM:=$(sed -n -e '"'"'s/^\\(.* \\)\\?TERM=\\([^ ]*\\).*/\\2/p'"'"' /proc/cmdline)}"' \
+		-e '/exec.*getty /s, \(vt100\)$, "${TERM:-\1}",' \
+		"$TARGET/etc/sv/getty-ttyS0/run"
+	exit 0
+fi
+
 echo "failed: init system not recognized by autologin customization" 1>&2
 exit 1
-- 
cgit v1.2.3