1#!/usr/bin/env bash
2# shellcheck disable=SC2154
3
4check() {
5	# We depend on udev-rules being loaded
6	[[ "${1}" = "-d" ]] && return 0
7
8	# Verify the zfs tool chain
9	for tool in "zgenhostid" "zpool" "zfs" "mount.zfs"; do
10		command -v "${tool}" >/dev/null || return 1
11	done
12}
13
14depends() {
15	echo udev-rules
16}
17
18installkernel() {
19	instmods -c zfs
20}
21
22install() {
23	inst_rules 90-zfs.rules 69-vdev.rules 60-zvol.rules
24
25	inst_multiple \
26		zgenhostid \
27		zfs \
28		zpool \
29		mount.zfs \
30		hostid \
31		grep \
32		awk \
33		tr \
34		cut \
35		head ||
36		{ dfatal "Failed to install essential binaries"; exit 1; }
37
38	# Adapted from https://github.com/zbm-dev/zfsbootmenu
39	if ! ldd "$(command -v zpool)" | grep -qF 'libgcc_s.so' && ldconfig -p 2> /dev/null | grep -qF 'libc.so.6' ; then
40		# On systems with gcc-config (Gentoo, Funtoo, etc.), use it to find libgcc_s
41		if command -v gcc-config >/dev/null; then
42			inst_simple "/usr/lib/gcc/$(s=$(gcc-config -c); echo "${s%-*}/${s##*-}")/libgcc_s.so.1" ||
43				{ dfatal "Unable to install libgcc_s.so"; exit 1; }
44			# Otherwise, use dracut's library installation function to find the right one
45		elif ! inst_libdir_file "libgcc_s.so*"; then
46			# If all else fails, just try looking for some gcc arch directory
47			inst_simple /usr/lib/gcc/*/*/libgcc_s.so* ||
48				{ dfatal "Unable to install libgcc_s.so"; exit 1; }
49		fi
50	fi
51
52	inst_hook cmdline 95 "${moddir}/parse-zfs.sh"
53	if [[ -n "${systemdutildir}" ]]; then
54		inst_script "${moddir}/zfs-generator.sh" "${systemdutildir}/system-generators/dracut-zfs-generator"
55	fi
56	inst_hook pre-mount 90 "${moddir}/zfs-load-key.sh"
57	inst_hook mount 98 "${moddir}/mount-zfs.sh"
58	inst_hook cleanup 99 "${moddir}/zfs-needshutdown.sh"
59	inst_hook shutdown 20 "${moddir}/export-zfs.sh"
60
61	inst_script "${moddir}/zfs-lib.sh" "/lib/dracut-zfs-lib.sh"
62
63	# -H ensures they are marked host-only
64	# -o ensures there is no error upon absence of these files
65	inst_multiple -o -H \
66		"@sysconfdir@/zfs/zpool.cache" \
67		"@sysconfdir@/zfs/vdev_id.conf"
68
69	# Synchronize initramfs and system hostid
70	if ! inst_simple -H @sysconfdir@/hostid; then
71		if HOSTID="$(hostid 2>/dev/null)" && [[ "${HOSTID}" != "00000000" ]]; then
72			zgenhostid -o "${initdir}@sysconfdir@/hostid" "${HOSTID}"
73			mark_hostonly @sysconfdir@/hostid
74		fi
75	fi
76
77	if dracut_module_included "systemd"; then
78		inst_simple "${systemdsystemunitdir}/zfs-import.target"
79		systemctl -q --root "${initdir}" add-wants initrd.target zfs-import.target
80
81		inst_simple "${moddir}/zfs-env-bootfs.service" "${systemdsystemunitdir}/zfs-env-bootfs.service"
82		systemctl -q --root "${initdir}" add-wants zfs-import.target zfs-env-bootfs.service
83
84		inst_simple "${moddir}/zfs-nonroot-necessities.service" "${systemdsystemunitdir}/zfs-nonroot-necessities.service"
85		systemctl -q --root "${initdir}" add-requires initrd-root-fs.target zfs-nonroot-necessities.service
86
87		# Add user-provided unit overrides:
88		# - /etc/systemd/system/${_service}
89		# - /etc/systemd/system/${_service}.d/overrides.conf
90		# -H ensures they are marked host-only
91		# -o ensures there is no error upon absence of these files
92		inst_multiple -o -H \
93			"${systemdsystemconfdir}/zfs-import.target" \
94			"${systemdsystemconfdir}/zfs-import.target.d/"*.conf
95
96		for _service in \
97			"zfs-import-scan.service" \
98			"zfs-import-cache.service"; do
99			inst_simple "${systemdsystemunitdir}/${_service}"
100			systemctl -q --root "${initdir}" add-wants zfs-import.target "${_service}"
101
102			# Add user-provided unit overrides:
103			# - /etc/systemd/system/${_service}
104			# - /etc/systemd/system/${_service}.d/overrides.conf
105			# -H ensures they are marked host-only
106			# -o ensures there is no error upon absence of these files
107			inst_multiple -o -H \
108				"${systemdsystemconfdir}/${_service}" \
109				"${systemdsystemconfdir}/${_service}.d/"*.conf
110
111		done
112
113		for _service in \
114			"zfs-snapshot-bootfs.service" \
115			"zfs-rollback-bootfs.service"; do
116			inst_simple "${moddir}/${_service}" "${systemdsystemunitdir}/${_service}"
117			systemctl -q --root "${initdir}" add-wants initrd.target "${_service}"
118
119			# Add user-provided unit overrides:
120			# - /etc/systemd/system/${_service}
121			# - /etc/systemd/system/${_service}.d/overrides.conf
122			# -H ensures they are marked host-only
123			# -o ensures there is no error upon absence of these files
124			inst_multiple -o -H \
125				"${systemdsystemconfdir}/${_service}" \
126				"${systemdsystemconfdir}/${_service}.d/"*.conf
127		done
128
129		inst_simple "${moddir}/import-opts-generator.sh" "${systemdutildir}/system-environment-generators/zfs-import-opts.sh"
130	fi
131}
132