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