From deb6f69095495efc775569c0c8f0846af3512508 Mon Sep 17 00:00:00 2001
From: Helmut Grohne <helmut@subdivi.de>
Date: Mon, 20 Feb 2023 14:28:45 +0100
Subject: debvm-create: add --initsystem option

This option allows changing the init system. The notable addition is
sysv.
---
 bin/debvm-create            | 66 ++++++++++++++++++++++++++++++++++-----------
 share/customize-ifupdown.sh | 18 +++++++++++++
 2 files changed, 68 insertions(+), 16 deletions(-)
 create mode 100755 share/customize-ifupdown.sh

diff --git a/bin/debvm-create b/bin/debvm-create
index d9ba6fa..10794c7 100755
--- a/bin/debvm-create
+++ b/bin/debvm-create
@@ -37,6 +37,13 @@ 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>
+
+Select an init system to be used.
+The default is B<systemd> independently of the Debian release.
+Note that when selecting B<none>, the resulting image will not be bootable unless something else takes care of creating F</sbin/init>.
+Automatic customizations that are specific to a particular init system will be skipped when a different init system is selected.
+
 =item B<-k> F<sshkey>, B<--sshkey>=F<sshkey>
 
 Install the given ssh public key file into the virtual machine image for the root user.
@@ -75,13 +82,15 @@ Internally, B<mmdebstrap> creates a tar archive first and converts that to ext2,
 This option causes the conversion to ext2 and further steps to be skipped and the output image will be a tar archive instead.
 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.
+
 =item B<initsystem>
 
 skips installing an init system.
-When enabling this option, the resulting image will lack F</sbin/init> and the image will not be bootable by B<debvm-run> unless something else provides that path.
-One would specify this option when providing the init system via other means, such as passing C<--include=sysvinit-core> to B<mmdebstrap>.
-This option also implies disabling other integrations that depend on it.
-At present, this will also skip B<autologin> and B<systemdnetwork>.
+This is equivalent to specifying B<--initsystem=none>.
 
 =item B<kernel>
 
@@ -99,6 +108,7 @@ This generally produces a smaller image, but you need to run B<apt update> befor
 =item B<systemdnetwork>
 
 skips installing B<libnss-resolve> as well as automatic network configuration via B<systemd-networkd>.
+This is specific to using B<systemd> as init system.
 
 =item B<usrmerge>
 
@@ -140,6 +150,7 @@ set -u
 
 ARCHITECTURE=$(dpkg --print-architecture)
 IMAGE=rootfs.ext4
+INITSYSTEM=systemd
 SIZE=1G
 SKIP=,
 SSHKEY=
@@ -171,8 +182,22 @@ opt_architecture() {
 opt_hostname() {
 	VMNAME=$1
 }
+opt_initsystem() {
+	case "$1" in
+		systemd|sysv|none)
+		;;
+		*)
+			die "value for --initsystem must be one of systemd, sysv or none"
+		;;
+	esac
+	INITSYSTEM=$1
+}
 opt_skip() {
-	SKIP="$SKIP$1,"
+	if test "$1" = initsystem; then
+		opt_initsystem none
+	else
+		SKIP="$SKIP$1,"
+	fi
 }
 opt_sshkey() {
 	SSHKEY=$1
@@ -202,12 +227,12 @@ while getopts :a:h:k:o:r:s:z:-: OPTCHAR; do
 				help)
 					usage
 				;;
-				architecture|hostname|output|release|size|skip|sshkey)
+				architecture|hostname|initsystem|output|release|size|skip|sshkey)
 					test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG"
 					"opt_$OPTARG" "$(nth_arg "$OPTIND" "$@")"
 					OPTIND=$((OPTIND+1))
 				;;
-				architecture=*|hostname=*|output=*|release=*|size=*|skip=*|sshkey=*)
+				architecture=*|hostname=*|initsystem=*|output=*|release=*|size=*|skip=*|sshkey=*)
 					"opt_${OPTARG%%=*}" "${OPTARG#*=}"
 				;;
 				*)
@@ -239,10 +264,6 @@ check_skip() {
 	esac
 }
 
-if check_skip initsystem; then
-	SKIP="${SKIP}autologin,systemdnetwork,"
-fi
-
 if ! check_skip kernel; then
 	set -- "--customize-hook=$SHARE_DIR/customize-kernel.sh" "$@"
 fi
@@ -262,9 +283,17 @@ set -- \
 	'--customize-hook=echo "LABEL=debvm / ext4 defaults 0 1" >"$1/etc/fstab"' \
 	"$@"
 
-if ! check_skip initsystem; then
-	set -- --include=systemd-sysv "$@"
-fi
+case "$INITSYSTEM" in
+	systemd)
+		set -- --include=systemd-sysv "$@"
+	;;
+	sysv)
+		set -- \
+			--include=sysvinit-core,e2fsprogs \
+			'--include=?not(?virtual)?exact-name(orphan-sysvinit-scripts)' \
+			"$@"
+	;;
+esac
 
 # set up a hostname
 set -- \
@@ -275,13 +304,18 @@ set -- \
 # allow password-less root login
 set -- '--customize-hook=passwd --root "$1" --delete root' "$@"
 
-if ! check_skip systemdnetwork; then
+if test "$INITSYSTEM" = systemd && ! check_skip systemdnetwork; then
 	# dhcp on all network interfaces, and add a dns resolver
 	set -- \
 		"--customize-hook=$SHARE_DIR/customize-networkd.sh" \
 		'--include=?not(?virtual)?exact-name(libnss-resolve)' \
 		"--customize-hook=$SHARE_DIR/customize-resolved.sh" \
 		"$@"
+elif test "$INITSYSTEM" = sysv && ! check_skip ifupdown; then
+	set -- \
+		'--include=ifupdown,isc-dhcp-client' \
+		"--customize-hook=$SHARE_DIR/customize-ifupdown.sh" \
+		"$@"
 fi
 
 # add ssh key for root
@@ -310,7 +344,7 @@ if ! check_skip usrmerge; then
 	set -- --hook-dir=/usr/share/mmdebstrap/hooks/maybe-merged-usr "$@"
 fi
 
-if ! check_skip autologin; then
+if test "$INITSYSTEM" != none && ! check_skip autologin; then
 	set -- "--customize-hook=$SHARE_DIR/customize-autologin.sh" "$@"
 fi
 
diff --git a/share/customize-ifupdown.sh b/share/customize-ifupdown.sh
new file mode 100755
index 0000000..c556d43
--- /dev/null
+++ b/share/customize-ifupdown.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+# Copyright 2023 Helmut Grohne <helmut@subdivi.de>
+# SPDX-License-Identifier: MIT
+#
+# This is a mmdebstrap customize hook that configures ifupdown for dhcp.
+# It expects ifupdown and isc-dhcp-client to be installed.
+
+set -eu
+
+TARGET=$1
+
+IFFILE=interfaces
+test -d "$TARGET/etc/network/interfaces.d" && IFFILE="interfaces.d/eth"
+
+cat >"$TARGET/etc/network/$IFFILE" <<EOF
+auto /enp*=eth
+iface eth inet dhcp
+EOF
-- 
cgit v1.2.3