Skip to content

Commit 385b003

Browse files
develop
1 parent d069dad commit 385b003

12 files changed

+79
-44
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
# This file is part of https://github.com/random-archer/mkinitcpio-systemd-tool
22

3-
#
43
# eclipse meta data
5-
#
64
.project
75

8-
#
96
# generated resources
10-
#
117
/*/
128
/*.xz
139
/*.log

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ Never write another mkinitcpio hook again, use systemd-tool.
88

99
Provisioning tool for systemd in initramfs (systemd-tool)
1010

11-
Features:
12-
* initrd debugging
13-
* early network setup
14-
* remote ssh access in initrd
15-
* cryptsetup password answer over ssh
11+
mkinitcpio hook name: `systemd-tool`
12+
13+
Core features provided by the hook:
1614
* unified systemd + mkinitcpio configuration
1715
* automatic provisioning of binary and config resources
1816
* on-demand invocation of mkinitcpio scripts and in-line functions
1917

20-
mkinitcpio hook name: `systemd-tool`
18+
Features provided by the included service units:
19+
* initrd debugging
20+
* early network setup
21+
* interactive user shell
22+
* remote ssh access in initrd
23+
* cryptsetup + custom password agent
2124

2225
### Example
2326

@@ -66,6 +69,10 @@ how can I enable my custom service unit in initrd?
6669
how can I disable my custom service unit in initrd?
6770
* alter the tag marker string, i.e.: `ConditionPathExists=/etc/xxx/initrd-release`
6871

72+
how systemd unit transitive dependency provisioning works?
73+
* see `mkinitcpio-install.sh/add_systemd_unit_X()`
74+
* services and targets found in `[Unit]/Requires|OnFailure` are recursively installed
75+
6976
what is the purpose of `[X-SystemdTool]` section in service unit files?
7077
* see https://github.com/systemd/systemd/issues/3340
7178
* this section provides configuration interface for `mkinitcpio` provisioning actions
@@ -137,6 +144,7 @@ is there a silent or no-echo mode during password entry in `initrd-shell.sh`?
137144
### Package Build Questions and Answers
138145

139146
how can I install a development version of this?
147+
* create a marker file `.PKGDEV` to build from latest master, for example
140148
```
141149
mkdir -p /tmp/aur
142150
cd /tmp/aur
@@ -145,3 +153,4 @@ cd mkinitcpio-systemd-tool
145153
touch .PKGDEV
146154
makepkg --syncdeps --install --noconfirm --needed
147155
```
156+
* release versions look like `3-1`, development like `3.25.d069dad-1`

crypttab

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# to setup crypto disks in initramfs over ssh
1212
# manually provide here partition UUID with help of `lsblk`
13-
# then active initrd-cryptsetup.service as well as "ask password prompt" in shell.sh
13+
# then active initrd-cryptsetup.service
1414

1515
# <name> <device> <password> <options>
1616
swap UUID=${UUID_SWAP} none luks

initrd-build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ do_ssh_host_keys() {
3838
source=$(keypath_openssh "$keytype")
3939
target=$(keypath_dropbear $(keytype_dropbear "$keytype"))
4040
if [ -f "$target" ]; then
41-
quiet "provision existing dropbear host key: $target"
41+
quiet "use existing dropbear host key: $target"
4242
else
4343
if [ -f "$source" ] ; then
4444
plain "convert openssh to dropbear host key: $target"

initrd-cryptsetup.service

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ Before=cryptsetup.target
1717
Requires=initrd-shell.service
1818

1919
[Service]
20-
ExecStart=/etc/systemd/system/initrd-shell.sh script_entry=service
20+
ExecStart=/etc/systemd/system/initrd-shell.sh script_entry=service service_name=cryptsetup
2121
Restart=on-failure
2222
RestartSec=1s
23+
RestartPreventExitStatus=100
2324
StandardInput=tty
2425
StandardOutput=inherit
2526
StandardError=inherit

initrd-debug-progs.service

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ WantedBy=initrd-debug-shell.service
2020

2121
[X-SystemdTool]
2222

23+
# debug tools
24+
InitrdBinary=/usr/bin/strace optional=yes
25+
2326
# systemd tools
2427
InitrdBinary=/usr/bin/systemd-analyze
2528
InitrdBinary=/usr/bin/journalctl

initrd-network.service

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ RemainAfterExit=true
1717
ExecStart=/bin/true
1818
#### required to recover from emergency service
1919
ExecStart=/bin/sh -c "echo '%N: enalbe network devices'"
20-
ExecStart=/bin/sh -c "for dev in $(ls /sys/class/net) ; do iplink set "$dev" up ; done"
21-
#### required for interface renaming after root switch
20+
ExecStart=/bin/sh -c "for dev in $(ls /sys/class/net) ; do ip link set $dev up ; done"
21+
#### required for interface renaming after switch-root
2222
ExecStop=/bin/sh -c "echo '%N: disable network devices'"
23-
ExecStop=/bin/sh -c "for dev in $(ls /sys/class/net) ; do iplink set "$dev" down ; done"
23+
ExecStop=/bin/sh -c "for dev in $(ls /sys/class/net) ; do ip link set $dev down ; done"
2424

2525
[Install]
2626
WantedBy=sysinit.target
@@ -38,3 +38,6 @@ InitrdCall=add_checked_modules /drivers/net/
3838

3939
# runtime location of resolv.conf provided by systemd-resolved.service
4040
InitrdLink=/etc/resolv.conf /run/systemd/resolve/resolv.conf
41+
42+
# dns resolution support
43+
InitrdBinary=/usr/lib/libnss_dns.so.2 optional=yes

initrd-shell.service

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ WantedBy=sysinit.target
2121

2222
# provision initrd shell
2323
InitrdPath=/etc/systemd/system/initrd-shell.sh mode=700
24-
#InitrdBinary=/usr/bin/sulogin
24+
InitrdBinary=/usr/bin/sulogin
2525
InitrdBinary=/usr/bin/systemctl
2626
InitrdBinary=/usr/bin/systemd-cat
2727
InitrdBinary=/usr/bin/journalctl
@@ -34,7 +34,7 @@ InitrdPath=/etc/group
3434
InitrdPath=/etc/passwd
3535
InitrdPath=/etc/shadow
3636
InitrdBuild=/etc/systemd/system/initrd-build.sh command=do_root_shell
37-
#InitrdBuild=/etc/systemd/system/initrd-build.sh command=do_secret_clean
37+
InitrdBuild=/etc/systemd/system/initrd-build.sh command=do_secret_clean
3838

3939
# include ssh credentials
4040
InitrdPath=/root/.ssh/authorized_keys source=/root/.ssh/authorized_keys mode=600

initrd-shell.sh

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ convert_ask_file() {
100100
# read named field from string of [name=value] entries
101101
extract_property() {
102102
local text="$1" name="$2"
103-
local $text # inject
104-
eval echo \${$name} # extract
103+
local $text && eval echo \${$name}
105104
}
106105

107106
# remove any pending content from the console input
@@ -245,13 +244,29 @@ entry_console() {
245244
fi
246245
}
247246

248-
# process invocation from systemd unit initrd-cryptsetup.service
247+
# process invocation from a systemd service unit
249248
entry_service() {
249+
case "$service_name" in
250+
default) service_default ;;
251+
cryptsetup) service_cryptsetup ;;
252+
*) log_error "invalid service_name=$service_name" ;;
253+
esac
254+
}
255+
256+
# default service implementation
257+
service_default() {
258+
log_info "default service"
259+
do_exit $service_restart_prevent_code
260+
}
261+
262+
# cryptsetup service implementation
263+
service_cryptsetup() {
264+
log_info "cryptsetup service"
250265
if has_crypt_jobs ; then
251266
run_crypt_jobs
252267
else
253268
log_info "nothing to do"
254-
do_exit 0
269+
do_exit $service_restart_prevent_code
255270
fi
256271
}
257272

@@ -267,11 +282,11 @@ do_prompt() {
267282
read -n 1 -p "?> " choice
268283
print_eol
269284
case "$choice" in
270-
a) do_agent_custom ;;
271-
s) do_shell ;;
272-
r) do_reboot ;;
273-
q) do_exit 0 ;;
274-
*) echo "$choice ?" ;;
285+
a) do_agent_custom ;;
286+
s) do_shell ;;
287+
r) do_reboot ;;
288+
q) do_exit 0 ;;
289+
*) echo "$choice ?" ;;
275290
esac
276291
done
277292
}
@@ -329,7 +344,10 @@ setup_defaults() {
329344
[ -z "$script_verbose" ] && readonly script_verbose="error" # can be {info,warn,error}
330345
[ -z "$script_tool_vars" ] && readonly script_tool_vars="$MC_SID" # tool shell detection
331346
[ -z "$script_identifier" ] && readonly script_identifier="shell" # systemd journal log tag
332-
# reboot options
347+
# service settings
348+
[ -z "$service_name" ] && readonly service_name="default"
349+
[ -z "$service_restart_prevent_code" ] && readonly service_restart_prevent_code=100 # see [Unit]/RestartPreventExitStatus
350+
# reboot options
333351
[ -z "$reboot_options" ] && readonly reboot_options="--force --force --no-ask-password"
334352
# password query settings
335353
[ -z "$query_prompt" ] && readonly query_prompt=" secret>"
@@ -353,8 +371,8 @@ setup_defaults() {
353371
setup_interrupts() {
354372
trap trap_HUP HUP
355373
trap trap_INT INT
356-
#trap trap_QUIT QUIT
357-
#trap trap_TSTP TSTP
374+
trap trap_QUIT QUIT
375+
trap trap_TSTP TSTP
358376
trap trap_TERM TERM
359377
}
360378

mkinitcpio-install.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,24 @@ add_systemd_unit_X() {
9696
InitrdBinary)
9797
# provision binaries
9898
# format:
99-
# InitrdBinary=/path/exec [replace=yes]
100-
local target= args= replace=
99+
# InitrdBinary=/path/exec [replace=yes] [optional=yes]
100+
local target= args= replace= optional=
101101
target=${values[0]} ; args=${values[@]:1:9}
102102
[[ $args ]] && local ${args[*]}
103103
if [[ -f $BUILDROOT$target ]] ; then
104104
if [[ $replace == "yes" ]] ; then
105-
quiet "replace present binary $target"
106-
add_binary "$target"
105+
quiet "replace present binary $target"
106+
add_binary "$target"
107107
else
108-
quiet "reuse present binary $target"
108+
quiet "reuse present binary $target"
109109
fi
110+
elif [[ -f $target ]] ; then
111+
quiet "provision new binary $target"
112+
add_binary "$target"
113+
elif [[ $optional = "yes" ]] ; then
114+
quiet "skip optional binary $target"
110115
else
111-
quiet "provision new binary $target"
112-
add_binary "$target"
116+
error "missing host binary $target"
113117
fi
114118
;;
115119
InitrdPath)

0 commit comments

Comments
 (0)