From 28344bf89ba75759ad0f8204959e9d03f3d30783 Mon Sep 17 00:00:00 2001
From: Helmut Grohne <helmut@subdivi.de>
Date: Mon, 20 Feb 2023 13:04:02 +0100
Subject: debvm-create: extend autologin hook to work with sysvinit

---
 share/customize-autologin.sh | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

(limited to 'share/customize-autologin.sh')

diff --git a/share/customize-autologin.sh b/share/customize-autologin.sh
index 4340650..58bf149 100755
--- a/share/customize-autologin.sh
+++ b/share/customize-autologin.sh
@@ -1,5 +1,5 @@
 #!/bin/sh
-# Copyright 2022 Helmut Grohne <helmut@subdivi.de>
+# Copyright 2022-2023 Helmut Grohne <helmut@subdivi.de>
 # SPDX-License-Identifier: MIT
 #
 # This is a mmdebstrap customize hook that configures automatic root login on a
@@ -10,14 +10,28 @@ set -eu
 
 TARGET=$1
 
-UNIT=serial-getty@.service
+if test "$(dpkg-query --root "$TARGET" -f '${db:Status-Status}' -W systemd-sysv 2>/dev/null)" = installed; then
+	UNIT=serial-getty@.service
 
-mkdir "$TARGET/etc/systemd/system/$UNIT.d"
+	mkdir "$TARGET/etc/systemd/system/$UNIT.d"
 
-(
-	echo '[Service]'
-	printf '%s\n' 'ExecStartPre=/bin/sed -n -e "s/^\\(.* \\)\\?\\(TERM=[^ ]*\\).*/\\2/w/run/debvmterm" /proc/cmdline'
-	echo 'EnvironmentFile=-/run/debvmterm'
-	echo 'ExecStart='
-	sed -n 's,^ExecStart=-/sbin/agetty ,&-a root ,p' "$TARGET/lib/systemd/system/$UNIT"
-) > "$TARGET/etc/systemd/system/$UNIT.d/autologin.conf"
+	(
+		echo '[Service]'
+		printf '%s\n' 'ExecStartPre=/bin/sed -n -e "s/^\\(.* \\)\\?\\(TERM=[^ ]*\\).*/\\2/w/run/debvmterm" /proc/cmdline'
+		echo 'EnvironmentFile=-/run/debvmterm'
+		echo 'ExecStart='
+		sed -n 's,^ExecStart=-/sbin/agetty ,&-a root ,p' "$TARGET/lib/systemd/system/$UNIT"
+	) > "$TARGET/etc/systemd/system/$UNIT.d/autologin.conf"
+
+	exit 0
+fi
+
+if test "$(dpkg-query --root "$TARGET" -f '${db:Status-Status}' -W sysvinit-core 2>/dev/null)" = installed; then
+	# shellcheck disable=SC2016  # intentional non-expansion
+	echo 'C0:2345:respawn:/sbin/getty -8 --noclear --keep-baud -a root console 115200,38400,9600 $TERM' >> "$TARGET/etc/inittab"
+
+	exit 0
+fi
+
+echo "failed: init system not recognized by autologin customization" 1>&2
+exit 1
-- 
cgit v1.2.3