dracut: use /bin/sh instead of bash as the intepreter
Despite that dracut has a hard dependency on bash,
its modules doesn't, dracut only has a hard dependency on bash for
module-setup (on a fully usable machine). Inside initramfs, dracut
allows users choose from a list of handful other shells, e.g. bash,
busybox, dash, mkfsh.
In fact, my local machine's initramfs is being built with dash,
and it's functional for a very long time.
Before 64025fa3a (Silence 'make checkbashisms', 2020-08-20), we also
allows our users to have that right, too.
Let's fix the problem 'make checkbashisms' reported and allows our users
to have that right, again.
For 'plymouth' case, let's simply run the command inside the if instead
of checking for the existence of command before running it, because the
status is also failture if plymouth is unavailable.
While we're at it, let's remove an unnecessary fork for grep in
zfs-generator.sh.in and its following complicated 'if elif fi' with
a simple 'case ... esac'.
To support this change, also exclude 90zfs from "make checkbashisms"
because the current CI infrastructure ships an old version of
"checkbashisms", which complains about "command -v", while the current
latest "checkbashisms" thinks it's fine. In the near future, we can
revert that change to "Makefile.am" when CI infrastructure is updated.
Reviewed-by: Gabriel A. Devenyi <gdevenyi@gmail.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
Closes #11244
2020-11-28 22:02:08 +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
|
2021-01-21 23:59:24 +03:00
|
|
|
[ -e /bin/systemctl ] || [ -e /usr/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
|
2020-11-13 01:06:24 +03:00
|
|
|
while [ "$(zpool list -H)" = "" ]; do
|
|
|
|
systemctl is-failed --quiet zfs-import-cache.service zfs-import-scan.service && 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'
|
2020-11-13 01:06:24 +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
|
2020-11-13 01:06:24 +03:00
|
|
|
ENCRYPTIONROOT="$(zfs get -H -o value encryptionroot "${BOOTFS}")"
|
2019-12-26 21:55:20 +03:00
|
|
|
# where the key is stored (in a file or loaded via prompt)
|
2020-11-13 01:06:24 +03:00
|
|
|
KEYLOCATION="$(zfs get -H -o value keylocation "${ENCRYPTIONROOT}")"
|
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
|
2019-12-26 21:55:20 +03: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 21:20:34 +03:00
|
|
|
fi
|
|
|
|
fi
|