[Unit]
Description=Set BOOTFS and BOOTFSFLAGS environment variables for dracut
DefaultDependencies=no
After=zfs-import-cache.service
After=zfs-import-scan.service
Before=zfs-import.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c '                                                                         \
    . /lib/dracut-zfs-lib.sh;                                                                  \
    decode_root_args || exit 0;                                                                \
    [ "$root" = "zfs:AUTO" ] && root="$(@sbindir@/zpool list -H -o bootfs | grep -m1 -vFx -)"; \
    rootflags="$(getarg rootflags=)";                                                          \
    [ "$(@sbindir@/zfs get -H -o value mountpoint "$root")" = legacy ] ||                      \
        case ",$rootflags," in                                                                 \
            *,zfsutil,*) ;;                                                                    \
            ,,) rootflags=zfsutil ;;                                                           \
            *)  rootflags="zfsutil,$rootflags" ;;                                              \
        esac;                                                                                  \
    exec systemctl set-environment BOOTFS="$root" BOOTFSFLAGS="$rootflags"'

[Install]
WantedBy=zfs-import.target
