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