Prefetch on deadlists merge
[zfs.git] / contrib / dracut / 90zfs / zfs-generator.sh.in
blob56f7ca9785baa90a9ad10135801816fd4d8f31eb
1 #!/bin/sh
2 # shellcheck disable=SC2016,SC1004,SC2154
4 grep -wq debug /proc/cmdline && debug=1
5 [ -n "$debug" ] && echo "zfs-generator: starting" >> /dev/kmsg
7 GENERATOR_DIR="$1"
8 [ -n "$GENERATOR_DIR" ] || {
9 echo "zfs-generator: no generator directory specified, exiting" >> /dev/kmsg
10 exit 1
13 # shellcheck source=zfs-lib.sh.in
14 . /lib/dracut-zfs-lib.sh
15 decode_root_args || exit 0
17 [ -z "${rootflags}" ] && rootflags=$(getarg rootflags=)
18 case ",${rootflags}," in
19 *,zfsutil,*) ;;
20 ,,) rootflags=zfsutil ;;
21 *) rootflags="zfsutil,${rootflags}" ;;
22 esac
24 [ -n "$debug" ] && echo "zfs-generator: writing extension for sysroot.mount to $GENERATOR_DIR/sysroot.mount.d/zfs-enhancement.conf" >> /dev/kmsg
27 mkdir -p "$GENERATOR_DIR"/sysroot.mount.d "$GENERATOR_DIR"/initrd-root-fs.target.requires "$GENERATOR_DIR"/dracut-pre-mount.service.d
29 echo "[Unit]"
30 echo "Before=initrd-root-fs.target"
31 echo "After=zfs-import.target"
32 echo
33 echo "[Mount]"
34 if [ "${root}" = "zfs:AUTO" ]; then
35 echo "PassEnvironment=BOOTFS"
36 echo 'What=${BOOTFS}'
37 else
38 echo "What=${root}"
40 echo "Type=zfs"
41 echo "Options=${rootflags}"
42 } > "$GENERATOR_DIR"/sysroot.mount.d/zfs-enhancement.conf
43 ln -fs ../sysroot.mount "$GENERATOR_DIR"/initrd-root-fs.target.requires/sysroot.mount
46 if [ "${root}" = "zfs:AUTO" ]; then
48 echo "[Unit]"
49 echo "Before=initrd-root-fs.target"
50 echo "After=sysroot.mount"
51 echo "DefaultDependencies=no"
52 echo
53 echo "[Service]"
54 echo "Type=oneshot"
55 echo "PassEnvironment=BOOTFS"
56 echo "ExecStart=/bin/sh -c '" ' \
57 . /lib/dracut-zfs-lib.sh; \
58 _zfs_nonroot_necessities_cb() { \
59 zfs mount | grep -m1 -q "^$1 " && return 0; \
60 echo "Mounting $1 on /sysroot$2"; \
61 mount -o zfsutil -t zfs "$1" "/sysroot$2"; \
62 }; \
63 for_relevant_root_children "${BOOTFS}" _zfs_nonroot_necessities_cb;' \
64 "'"
65 } > "$GENERATOR_DIR"/zfs-nonroot-necessities.service
66 ln -fs ../zfs-nonroot-necessities.service "$GENERATOR_DIR"/initrd-root-fs.target.requires/zfs-nonroot-necessities.service
67 else
68 # We can solve this statically at generation time, so do!
69 _zfs_generator_cb() {
70 dset="${1}"
71 mpnt="${2}"
72 unit="$(systemd-escape --suffix=mount -p "/sysroot${mpnt}")"
75 echo "[Unit]"
76 echo "Before=initrd-root-fs.target"
77 echo "After=sysroot.mount"
78 echo
79 echo "[Mount]"
80 echo "Where=/sysroot${mpnt}"
81 echo "What=${dset}"
82 echo "Type=zfs"
83 echo "Options=zfsutil"
84 } > "$GENERATOR_DIR/${unit}"
85 ln -fs ../"${unit}" "$GENERATOR_DIR"/initrd-root-fs.target.requires/"${unit}"
88 for_relevant_root_children "${root}" _zfs_generator_cb
93 echo "[Unit]"
94 echo "After=zfs-import.target"
95 } > "$GENERATOR_DIR"/dracut-pre-mount.service.d/zfs-enhancement.conf
97 [ -n "$debug" ] && echo "zfs-generator: finished" >> /dev/kmsg
99 exit 0