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 19:02:08 +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
|
2020-11-12 22:06:24 +00:00
|
|
|
while [ "$(zpool list -H)" = "" ]; do
|
|
|
|
systemctl is-failed --quiet zfs-import-cache.service zfs-import-scan.service && 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'
|
2020-11-12 22:06:24 +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
|
2020-11-12 22:06:24 +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)
|
2020-11-12 22:06:24 +00:00
|
|
|
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
|