1#!/usr/bin/env bash
2
3echo "zfs-generator: starting" >> /dev/kmsg
4
5GENERATOR_DIR="$1"
6[ -n "$GENERATOR_DIR" ] || {
7    echo "zfs-generator: no generator directory specified, exiting" >> /dev/kmsg
8    exit 1
9}
10
11[ -f /lib/dracut-lib.sh ] && dracutlib=/lib/dracut-lib.sh
12[ -f /usr/lib/dracut/modules.d/99base/dracut-lib.sh ] && dracutlib=/usr/lib/dracut/modules.d/99base/dracut-lib.sh
13
14type getarg >/dev/null 2>&1 || {
15    echo "zfs-generator: loading Dracut library from $dracutlib" >> /dev/kmsg
16    . "$dracutlib"
17}
18
19[ -z "$root" ]       && root=$(getarg root=)
20[ -z "$rootfstype" ] && rootfstype=$(getarg rootfstype=)
21[ -z "$rootflags" ]  && rootflags=$(getarg rootflags=)
22
23# If root is not ZFS= or zfs: or rootfstype is not zfs
24# then we are not supposed to handle it.
25[ "${root##zfs:}" = "${root}" -a "${root##ZFS=}" = "${root}" -a "$rootfstype" != "zfs" ] && exit 0
26
27rootfstype=zfs
28if echo "${rootflags}" | grep -Eq '^zfsutil$|^zfsutil,|,zfsutil$|,zfsutil,' ; then
29    true
30elif test -n "${rootflags}" ; then
31    rootflags="zfsutil,${rootflags}"
32else
33    rootflags=zfsutil
34fi
35
36echo "zfs-generator: writing extension for sysroot.mount to $GENERATOR_DIR"/sysroot.mount.d/zfs-enhancement.conf >> /dev/kmsg
37
38[ -d "$GENERATOR_DIR" ] || mkdir "$GENERATOR_DIR"
39[ -d "$GENERATOR_DIR"/sysroot.mount.d ] || mkdir "$GENERATOR_DIR"/sysroot.mount.d
40
41{
42    echo "[Unit]"
43    echo "Before=initrd-root-fs.target"
44    echo "After=zfs-import.target"
45    echo "[Mount]"
46    if [ "${root}" = "zfs:AUTO" ] ; then
47      echo "PassEnvironment=BOOTFS"
48      echo 'What=${BOOTFS}'
49    else
50      root="${root##zfs:}"
51      root="${root##ZFS=}"
52      echo "What=${root}"
53    fi
54    echo "Type=${rootfstype}"
55    echo "Options=${rootflags}"
56} > "$GENERATOR_DIR"/sysroot.mount.d/zfs-enhancement.conf
57
58[ -d "$GENERATOR_DIR"/initrd-root-fs.target.requires ] || mkdir -p "$GENERATOR_DIR"/initrd-root-fs.target.requires
59ln -s ../sysroot.mount "$GENERATOR_DIR"/initrd-root-fs.target.requires/sysroot.mount
60
61echo "zfs-generator: finished" >> /dev/kmsg