1#!/bin/sh
2# shellcheck disable=SC2034,SC2154
3
4. /lib/dracut-lib.sh
5
6# Let the command line override our host id.
7spl_hostid=$(getarg spl_hostid=)
8if [ -n "${spl_hostid}" ] ; then
9	info "ZFS: Using hostid from command line: ${spl_hostid}"
10	zgenhostid -f "${spl_hostid}"
11elif [ -f "/etc/hostid" ] ; then
12	info "ZFS: Using hostid from /etc/hostid: $(hostid)"
13else
14	warn "ZFS: No hostid found on kernel command line or /etc/hostid."
15	warn "ZFS: Pools may not import correctly."
16fi
17
18wait_for_zfs=0
19case "${root}" in
20	""|zfs|zfs:)
21		# We'll take root unset, root=zfs, or root=zfs:
22		# No root set, so we want to read the bootfs attribute.  We
23		# can't do that until udev settles so we'll set dummy values
24		# and hope for the best later on.
25		root="zfs:AUTO"
26		rootok=1
27		wait_for_zfs=1
28
29		info "ZFS: Enabling autodetection of bootfs after udev settles."
30		;;
31
32	ZFS=*|zfs:*|FILESYSTEM=*)
33		# root is explicit ZFS root.  Parse it now.  We can handle
34		# a root=... param in any of the following formats:
35		# root=ZFS=rpool/ROOT
36		# root=zfs:rpool/ROOT
37		# root=zfs:FILESYSTEM=rpool/ROOT
38		# root=FILESYSTEM=rpool/ROOT
39		# root=ZFS=pool+with+space/ROOT+WITH+SPACE (translates to root=ZFS=pool with space/ROOT WITH SPACE)
40
41		# Strip down to just the pool/fs
42		root="${root#zfs:}"
43		root="${root#FILESYSTEM=}"
44		root="zfs:${root#ZFS=}"
45		# switch + with spaces because kernel cmdline does not allow us to quote parameters
46		root=$(printf '%s\n' "$root" | sed "s/+/ /g")
47		rootok=1
48		wait_for_zfs=1
49
50		info "ZFS: Set ${root} as bootfs."
51		;;
52esac
53
54# Make sure Dracut is happy that we have a root and will wait for ZFS
55# modules to settle before mounting.
56if [ ${wait_for_zfs} -eq 1 ]; then
57	ln -s /dev/null /dev/root 2>/dev/null
58	initqueuedir="${hookdir}/initqueue/finished"
59	test -d "${initqueuedir}" || {
60		initqueuedir="${hookdir}/initqueue-finished"
61	}
62	echo '[ -e /dev/zfs ]' > "${initqueuedir}/zfs.sh"
63fi
64