2018-02-22 04:54:54 +03:00
|
|
|
#!/bin/sh
|
2018-01-18 21:20:34 +03:00
|
|
|
|
2018-02-08 05:31:54 +03:00
|
|
|
# only run this on systemd systems, we handle the decrypt in mount-zfs.sh in the mount hook otherwise
|
2018-02-22 04:54:54 +03:00
|
|
|
[ -e /bin/systemctl ] || return 0
|
2018-02-08 05:31:54 +03:00
|
|
|
|
2018-01-18 21:20:34 +03:00
|
|
|
# This script only gets executed on systemd systems, see mount-zfs.sh for non-systemd systems
|
|
|
|
|
|
|
|
# import the libs now that we know the pool imported
|
|
|
|
[ -f /lib/dracut-lib.sh ] && dracutlib=/lib/dracut-lib.sh
|
|
|
|
[ -f /usr/lib/dracut/modules.d/99base/dracut-lib.sh ] && dracutlib=/usr/lib/dracut/modules.d/99base/dracut-lib.sh
|
2018-02-22 04:54:54 +03:00
|
|
|
# shellcheck source=./lib-zfs.sh.in
|
2018-01-18 21:20:34 +03:00
|
|
|
. "$dracutlib"
|
|
|
|
|
|
|
|
# load the kernel command line vars
|
2018-02-22 04:54:54 +03:00
|
|
|
[ -z "$root" ] && root="$(getarg root=)"
|
2018-01-18 21:20:34 +03:00
|
|
|
# If root is not ZFS= or zfs: or rootfstype is not zfs then we are not supposed to handle it.
|
2018-02-22 04:54:54 +03:00
|
|
|
[ "${root##zfs:}" = "${root}" ] && [ "${root##ZFS=}" = "${root}" ] && [ "$rootfstype" != "zfs" ] && exit 0
|
2018-01-18 21:20:34 +03:00
|
|
|
|
|
|
|
# There is a race between the zpool import and the pre-mount hooks, so we wait for a pool to be imported
|
|
|
|
while true; do
|
|
|
|
zpool list -H | grep -q -v '^$' && break
|
2018-02-22 04:54:54 +03:00
|
|
|
[ "$(systemctl is-failed zfs-import-cache.service)" = 'failed' ] && exit 1
|
|
|
|
[ "$(systemctl is-failed zfs-import-scan.service)" = 'failed' ] && exit 1
|
2018-01-18 21:20:34 +03:00
|
|
|
sleep 0.1s
|
|
|
|
done
|
|
|
|
|
|
|
|
# run this after import as zfs-import-cache/scan service is confirmed good
|
2019-10-01 22:54:27 +03:00
|
|
|
# we do not overwrite the ${root} variable, but create a new one, BOOTFS, to hold the dataset
|
2018-02-22 04:54:54 +03:00
|
|
|
if [ "${root}" = "zfs:AUTO" ] ; then
|
2019-10-01 22:54:27 +03:00
|
|
|
BOOTFS="$(zpool list -H -o bootfs | awk '$1 != "-" {print; exit}')"
|
2018-01-18 21:20:34 +03:00
|
|
|
else
|
2019-10-01 22:54:27 +03:00
|
|
|
BOOTFS="${root##zfs:}"
|
2019-10-31 00:38:41 +03:00
|
|
|
BOOTFS="${BOOTFS##ZFS=}"
|
2018-01-18 21:20:34 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# if pool encryption is active and the zfs command understands '-o encryption'
|
2019-10-01 22:54:27 +03:00
|
|
|
if [ "$(zpool list -H -o feature@encryption $(echo "${BOOTFS}" | awk -F\/ '{print $1}'))" = 'active' ]; then
|
2018-02-20 21:13:20 +03:00
|
|
|
# if the root dataset has encryption enabled
|
2019-10-01 22:54:27 +03:00
|
|
|
ENCRYPTIONROOT=$(zfs get -H -o value encryptionroot "${BOOTFS}")
|
2018-02-20 21:13:20 +03:00
|
|
|
if ! [ "${ENCRYPTIONROOT}" = "-" ]; then
|
2019-11-09 01:34:07 +03:00
|
|
|
KEYSTATUS="$(zfs get -H -o value keystatus "${ENCRYPTIONROOT}")"
|
|
|
|
# continue only if the key needs to be loaded
|
|
|
|
[ "$KEYSTATUS" = "unavailable" ] || exit 0
|
2018-01-18 21:20:34 +03:00
|
|
|
# decrypt them
|
|
|
|
TRY_COUNT=5
|
2018-02-22 04:54:54 +03:00
|
|
|
while [ $TRY_COUNT -gt 0 ]; do
|
2019-10-01 22:54:27 +03:00
|
|
|
systemd-ask-password "Encrypted ZFS password for ${BOOTFS}" --no-tty | zfs load-key "${ENCRYPTIONROOT}" && break
|
2018-02-22 04:54:54 +03:00
|
|
|
TRY_COUNT=$((TRY_COUNT - 1))
|
2018-01-18 21:20:34 +03:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|