2018-02-22 01:54:54 +00:00
|
|
|
#!/bin/sh
|
2018-01-18 18:20:34 +00:00
|
|
|
|
2018-02-08 02:31:54 +00:00
|
|
|
# only run this on systemd systems, we handle the decrypt in mount-zfs.sh in the mount hook otherwise
|
2018-02-22 01:54:54 +00:00
|
|
|
[ -e /bin/systemctl ] || return 0
|
2018-02-08 02:31:54 +00:00
|
|
|
|
2018-01-18 18:20:34 +00: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 01:54:54 +00:00
|
|
|
# shellcheck source=./lib-zfs.sh.in
|
2018-01-18 18:20:34 +00:00
|
|
|
. "$dracutlib"
|
|
|
|
|
|
|
|
# load the kernel command line vars
|
2018-02-22 01:54:54 +00:00
|
|
|
[ -z "$root" ] && root="$(getarg root=)"
|
2018-01-18 18:20:34 +00:00
|
|
|
# If root is not ZFS= or zfs: or rootfstype is not zfs then we are not supposed to handle it.
|
2018-02-22 01:54:54 +00:00
|
|
|
[ "${root##zfs:}" = "${root}" ] && [ "${root##ZFS=}" = "${root}" ] && [ "$rootfstype" != "zfs" ] && exit 0
|
2018-01-18 18:20:34 +00: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 01:54:54 +00:00
|
|
|
[ "$(systemctl is-failed zfs-import-cache.service)" = 'failed' ] && exit 1
|
|
|
|
[ "$(systemctl is-failed zfs-import-scan.service)" = 'failed' ] && exit 1
|
2018-01-18 18:20:34 +00:00
|
|
|
sleep 0.1s
|
|
|
|
done
|
|
|
|
|
|
|
|
# run this after import as zfs-import-cache/scan service is confirmed good
|
2019-10-01 19:54:27 +00:00
|
|
|
# we do not overwrite the ${root} variable, but create a new one, BOOTFS, to hold the dataset
|
2018-02-22 01:54:54 +00:00
|
|
|
if [ "${root}" = "zfs:AUTO" ] ; then
|
2019-10-01 19:54:27 +00:00
|
|
|
BOOTFS="$(zpool list -H -o bootfs | awk '$1 != "-" {print; exit}')"
|
2018-01-18 18:20:34 +00:00
|
|
|
else
|
2019-10-01 19:54:27 +00:00
|
|
|
BOOTFS="${root##zfs:}"
|
2019-10-30 21:38:41 +00:00
|
|
|
BOOTFS="${BOOTFS##ZFS=}"
|
2018-01-18 18:20:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# if pool encryption is active and the zfs command understands '-o encryption'
|
2019-10-01 19:54:27 +00:00
|
|
|
if [ "$(zpool list -H -o feature@encryption $(echo "${BOOTFS}" | awk -F\/ '{print $1}'))" = 'active' ]; then
|
2018-02-20 18:13:20 +00:00
|
|
|
# if the root dataset has encryption enabled
|
2019-10-01 19:54:27 +00:00
|
|
|
ENCRYPTIONROOT=$(zfs get -H -o value encryptionroot "${BOOTFS}")
|
2019-12-26 18:55:20 +00:00
|
|
|
# where the key is stored (in a file or loaded via prompt)
|
|
|
|
KEYLOCATION=$(${ZFS} get -H -o value keylocation "${ENCRYPTIONROOT}")
|
2018-02-20 18:13:20 +00:00
|
|
|
if ! [ "${ENCRYPTIONROOT}" = "-" ]; then
|
2019-11-08 22:34:07 +00:00
|
|
|
KEYSTATUS="$(zfs get -H -o value keystatus "${ENCRYPTIONROOT}")"
|
|
|
|
# continue only if the key needs to be loaded
|
|
|
|
[ "$KEYSTATUS" = "unavailable" ] || exit 0
|
2019-12-26 18:55:20 +00:00
|
|
|
# if key is stored in a file, do not prompt
|
|
|
|
if ! [ "${KEYLOCATION}" = "prompt" ]; then
|
|
|
|
zfs load-key "${ENCRYPTIONROOT}"
|
|
|
|
else
|
|
|
|
# decrypt them
|
|
|
|
TRY_COUNT=5
|
|
|
|
while [ $TRY_COUNT -gt 0 ]; do
|
|
|
|
systemd-ask-password "Encrypted ZFS password for ${BOOTFS}" --no-tty | zfs load-key "${ENCRYPTIONROOT}" && break
|
|
|
|
TRY_COUNT=$((TRY_COUNT - 1))
|
|
|
|
done
|
|
|
|
fi
|
2018-01-18 18:20:34 +00:00
|
|
|
fi
|
|
|
|
fi
|