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