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 "@sbindir@/zgenhostid" "@sbindir@/zpool" "@sbindir@/zfs" "@mounthelperdir@/mount.zfs" ; do
10		test -x "$tool" || return 1
11	done
12
13	return 0
14}
15
16depends() {
17	echo udev-rules
18	return 0
19}
20
21installkernel() {
22	instmods zfs
23	instmods zcommon
24	instmods znvpair
25	instmods zavl
26	instmods zunicode
27	instmods zlua
28	instmods icp
29	instmods spl
30	instmods zlib_deflate
31	instmods zlib_inflate
32}
33
34install() {
35	inst_rules @udevruledir@/90-zfs.rules
36	inst_rules @udevruledir@/69-vdev.rules
37	inst_rules @udevruledir@/60-zvol.rules
38	dracut_install hostid
39	dracut_install grep
40	dracut_install @sbindir@/zgenhostid
41	dracut_install @sbindir@/zfs
42	dracut_install @sbindir@/zpool
43	# Workaround for https://github.com/openzfs/zfs/issues/4749 by
44	# ensuring libgcc_s.so(.1) is included
45	if ldd @sbindir@/zpool | grep -qF 'libgcc_s.so'; then
46		# Dracut will have already tracked and included it
47		:;
48	elif command -v gcc-config >/dev/null 2>&1; then
49		# On systems with gcc-config (Gentoo, Funtoo, etc.):
50		# Use the current profile to resolve the appropriate path
51		s="$(gcc-config -c)"
52		dracut_install "/usr/lib/gcc/${s%-*}/${s##*-}/libgcc_s.so"*
53	elif [ "$(echo /usr/lib/libgcc_s.so*)" != "/usr/lib/libgcc_s.so*" ]; then
54		# Try a simple path first
55		dracut_install /usr/lib/libgcc_s.so*
56	elif [ "$(echo /lib*/libgcc_s.so*)" != "/lib*/libgcc_s.so*" ]; then
57		# SUSE
58		dracut_install /lib*/libgcc_s.so*
59	else
60		# Fallback: Guess the path and include all matches
61		dracut_install /usr/lib*/gcc/**/libgcc_s.so*
62	fi
63	# shellcheck disable=SC2050
64	if [ @LIBFETCH_DYNAMIC@ != 0 ]; then
65		for d in $libdirs; do
66			[ -e "$d/"@LIBFETCH_SONAME@ ] && dracut_install "$d/"@LIBFETCH_SONAME@
67		done
68	fi
69	dracut_install @mounthelperdir@/mount.zfs
70	dracut_install @udevdir@/vdev_id
71	dracut_install awk
72	dracut_install cut
73	dracut_install tr
74	dracut_install head
75	dracut_install @udevdir@/zvol_id
76	inst_hook cmdline 95 "${moddir}/parse-zfs.sh"
77	if [ -n "$systemdutildir" ] ; then
78		inst_script "${moddir}/zfs-generator.sh" "$systemdutildir"/system-generators/dracut-zfs-generator
79	fi
80	inst_hook pre-mount 90 "${moddir}/zfs-load-key.sh"
81	inst_hook mount 98 "${moddir}/mount-zfs.sh"
82	inst_hook cleanup 99 "${moddir}/zfs-needshutdown.sh"
83	inst_hook shutdown 20 "${moddir}/export-zfs.sh"
84
85	inst_simple "${moddir}/zfs-lib.sh" "/lib/dracut-zfs-lib.sh"
86	if [ -e @sysconfdir@/zfs/zpool.cache ]; then
87		inst @sysconfdir@/zfs/zpool.cache
88		type mark_hostonly >/dev/null 2>&1 && mark_hostonly @sysconfdir@/zfs/zpool.cache
89	fi
90
91	if [ -e @sysconfdir@/zfs/vdev_id.conf ]; then
92		inst @sysconfdir@/zfs/vdev_id.conf
93		type mark_hostonly >/dev/null 2>&1 && mark_hostonly @sysconfdir@/zfs/vdev_id.conf
94	fi
95
96	# Synchronize initramfs and system hostid
97	if [ -f @sysconfdir@/hostid ]; then
98		inst @sysconfdir@/hostid
99		type mark_hostonly >/dev/null 2>&1 && mark_hostonly @sysconfdir@/hostid
100	elif HOSTID="$(hostid 2>/dev/null)" && [ "${HOSTID}" != "00000000" ]; then
101		zgenhostid -o "${initdir}@sysconfdir@/hostid" "${HOSTID}"
102		type mark_hostonly >/dev/null 2>&1 && mark_hostonly @sysconfdir@/hostid
103	fi
104
105	if dracut_module_included "systemd"; then
106		mkdir -p "${initdir}/$systemdsystemunitdir/zfs-import.target.wants"
107		for _service in "zfs-import-scan.service" "zfs-import-cache.service" ; do
108			dracut_install "@systemdunitdir@/$_service"
109			if ! [ -L "${initdir}/$systemdsystemunitdir/zfs-import.target.wants/$_service" ]; then
110				ln -sf ../$_service "${initdir}/$systemdsystemunitdir/zfs-import.target.wants/$_service"
111				type mark_hostonly >/dev/null 2>&1 && mark_hostonly "@systemdunitdir@/$_service"
112			fi
113		done
114
115		inst "${moddir}"/zfs-env-bootfs.service "${systemdsystemunitdir}"/zfs-env-bootfs.service
116		ln -s ../zfs-env-bootfs.service "${initdir}/${systemdsystemunitdir}/zfs-import.target.wants"/zfs-env-bootfs.service
117		type mark_hostonly >/dev/null 2>&1 && mark_hostonly @systemdunitdir@/zfs-env-bootfs.service
118
119		dracut_install systemd-ask-password
120		dracut_install systemd-tty-ask-password-agent
121
122		mkdir -p "${initdir}/$systemdsystemunitdir/initrd.target.wants"
123		dracut_install @systemdunitdir@/zfs-import.target
124		if ! [ -L "${initdir}/$systemdsystemunitdir/initrd.target.wants"/zfs-import.target ]; then
125			ln -s ../zfs-import.target "${initdir}/$systemdsystemunitdir/initrd.target.wants"/zfs-import.target
126			type mark_hostonly >/dev/null 2>&1 && mark_hostonly @systemdunitdir@/zfs-import.target
127		fi
128
129		for _service in zfs-snapshot-bootfs.service zfs-rollback-bootfs.service ; do
130			inst "${moddir}/$_service" "${systemdsystemunitdir}/$_service"
131			if ! [ -L "${initdir}/$systemdsystemunitdir/initrd.target.wants/$_service" ]; then
132				ln -s "../$_service" "${initdir}/$systemdsystemunitdir/initrd.target.wants/$_service"
133			fi
134		done
135
136		# There isn't a pkg-config variable for this,
137		# and dracut doesn't automatically resolve anything this'd be next to
138		local systemdsystemenvironmentgeneratordir
139		systemdsystemenvironmentgeneratordir="$(pkg-config --variable=prefix systemd || echo "/usr")/lib/systemd/system-environment-generators"
140		mkdir -p "${initdir}/${systemdsystemenvironmentgeneratordir}"
141		inst "${moddir}"/import-opts-generator.sh "${systemdsystemenvironmentgeneratordir}"/zfs-import-opts.sh
142	fi
143}
144