2020-11-29 02:02:08 +07:00
|
|
|
#!/bin/sh
|
2021-05-14 14:02:11 +02:00
|
|
|
# shellcheck disable=SC2154
|
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
|
2021-01-21 21:59:24 +01:00
|
|
|
[ -e /bin/systemctl ] || [ -e /usr/bin/systemctl ] || return 0
|
2018-02-08 02:31:54 +00:00
|
|
|
|
2022-04-04 22:45:58 +02:00
|
|
|
# shellcheck source=zfs-lib.sh.in
|
|
|
|
|
. /lib/dracut-zfs-lib.sh
|
2018-01-18 18:20:34 +00:00
|
|
|
|
2022-04-04 22:45:58 +02:00
|
|
|
decode_root_args || return 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
|
2022-04-04 22:45:58 +02:00
|
|
|
while ! systemctl is-active --quiet zfs-import.target; do
|
|
|
|
|
systemctl is-failed --quiet zfs-import-cache.service zfs-import-scan.service && return 1
|
2018-01-18 18:20:34 +00:00
|
|
|
sleep 0.1s
|
|
|
|
|
done
|
|
|
|
|
|
2022-04-04 22:45:58 +02:00
|
|
|
BOOTFS="$root"
|
|
|
|
|
if [ "$BOOTFS" = "zfs:AUTO" ]; then
|
|
|
|
|
BOOTFS="$(zpool get -Ho value bootfs | grep -m1 -vFx -)"
|
2018-01-18 18:20:34 +00:00
|
|
|
fi
|
|
|
|
|
|
2022-04-04 22:52:43 +02:00
|
|
|
[ "$(zpool get -Ho value feature@encryption "${BOOTFS%%/*}")" = 'active' ] || return 0
|
|
|
|
|
|
2022-04-04 23:39:18 +02:00
|
|
|
_load_key_cb() {
|
|
|
|
|
dataset="$1"
|
|
|
|
|
|
|
|
|
|
ENCRYPTIONROOT="$(zfs get -Ho value encryptionroot "${dataset}")"
|
|
|
|
|
[ "${ENCRYPTIONROOT}" = "-" ] && return 0
|
|
|
|
|
|
|
|
|
|
[ "$(zfs get -Ho value keystatus "${ENCRYPTIONROOT}")" = "unavailable" ] || return 0
|
|
|
|
|
|
|
|
|
|
KEYLOCATION="$(zfs get -Ho value keylocation "${ENCRYPTIONROOT}")"
|
|
|
|
|
case "${KEYLOCATION%%://*}" in
|
|
|
|
|
prompt)
|
|
|
|
|
for _ in 1 2 3; do
|
2023-01-05 20:07:43 +00:00
|
|
|
systemd-ask-password --timeout=0 --no-tty "Encrypted ZFS password for ${dataset}" | zfs load-key "${ENCRYPTIONROOT}" && break
|
2022-04-04 22:52:43 +02:00
|
|
|
done
|
2022-04-04 23:39:18 +02:00
|
|
|
;;
|
|
|
|
|
http*)
|
|
|
|
|
systemctl start network-online.target
|
|
|
|
|
zfs load-key "${ENCRYPTIONROOT}"
|
|
|
|
|
;;
|
|
|
|
|
file)
|
|
|
|
|
KEYFILE="${KEYLOCATION#file://}"
|
|
|
|
|
[ -r "${KEYFILE}" ] || udevadm settle
|
|
|
|
|
[ -r "${KEYFILE}" ] || {
|
|
|
|
|
info "ZFS: Waiting for key ${KEYFILE} for ${ENCRYPTIONROOT}..."
|
|
|
|
|
for _ in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
|
|
|
|
|
sleep 0.5s
|
|
|
|
|
[ -r "${KEYFILE}" ] && break
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
[ -r "${KEYFILE}" ] || warn "ZFS: Key ${KEYFILE} for ${ENCRYPTIONROOT} hasn't appeared. Trying anyway."
|
|
|
|
|
zfs load-key "${ENCRYPTIONROOT}"
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
zfs load-key "${ENCRYPTIONROOT}"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_load_key_cb "$BOOTFS"
|
|
|
|
|
for_relevant_root_children "$BOOTFS" _load_key_cb
|