summaryrefslogtreecommitdiff
path: root/osi-mk.d/systemd-osi-shell.sh
blob: dc161dcdb8bfa245e0ff811809c8032724f4c0c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/hint/bash -euE
# Copyright (C) 2018  Luke Shumaker
# SPDX-License-Identifier: AGPL-3.0-or-later

post_install+=(10:systemd-osi-shell:post_install)
systemd-osi-shell:post_install() {
	local arg_mountpoint=$1
	
	cat <<-'EOT' > "${arg_mountpoint}/etc/systemd/system/osi-shell.target"
		[Unit]
		Description=osi-shell target
		Requires=multi-user.target
		After=multi-user.target
		Conflicts=rescue.target
		AllowIsolate=yes
		EOT
	ln -sfT -- osi-shell.target "${arg_mountpoint}/etc/systemd/system/default.target"

	cat <<-'EOT' > "${arg_mountpoint}/etc/systemd/system/osi-shell.service"
		[Unit]
		Description=osi-shell service
		Wants=network-online.target
		After=network-online.target

		[Service]
		KillSignal=SIGHUP
		KillMode=process
		IgnoreSIGPIPE=no

		# We can't use login(1) because it masks the exit status of the shell,
		# but we want this to be a real local login with PAM, so use su(1),
		# but trick in in to using login(1)'s PAM config.  We undo this trick by
		# using nsenter(1) to reset the mount namespace after we've done the PAM stuff.
		ExecStart=/bin/unshare --mount -- sh -c 'mount --bind /etc/pam.d/login /etc/pam.d/su && exec -- su -c "exec nsenter --mount --target=1 -- bash -l"'
		StandardInput=tty
		TTYPath=/dev/ttyS0
		TTYReset=yes
		TTYVHangup=yes

		# Write the exit status to ttyS1 and poweroff
		# Bash sets exit status to 128+SIGNAL if we get killed by a signal,
		# but there's not a good one-liner way to get from signal name to number,
		# so just use 128.
		ExecStopPost=/bin/sh -c 'if [ "$EXIT_CODE" = exited ]; then echo $EXIT_STATUS; else echo 128; fi > /dev/ttyS1; systemctl poweroff --no-block'

		[Install]
		WantedBy=osi-shell.target
		EOT
	systemctl --root="$arg_mountpoint" enable osi-shell.service
}