mirror_zfs/contrib/dracut/90zfs/zfs-generator.sh.in

100 lines
3.2 KiB
Bash
Raw Normal View History

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
# shellcheck disable=SC2016,SC1004,SC2154
grep -wq debug /proc/cmdline && debug=1
[ -n "$debug" ] && echo "zfs-generator: starting" >> /dev/kmsg
Use a different technique to detect whether to mount-zfs The behavior of the Dracut module was very wrong before. The correct behavior: initramfs should not run `zfs-mount` to completion if the two generator files exist. If, however, one of them is missing, it indicates one of three cases: * The kernel command line did not specify a root ZFS file system, and another Dracut module is already handling root mount (via systemd). `mount-zfs` can run, but it will do nothing. * There is no systemd to run `sysroot.mount` to begin with. `mount-zfs` must run. * The root parameter is zfs:AUTO, which cannot be run in sysroot.mount. `mount-zfs` must run. In any of these three cases, it is safe to run `zfs-mount` to completion. `zfs-mount` must also delete itself if it determines it should not run, or else Dracut will do the insane thing of running it over and over again. Literally, the definition of insanity, doing the same thing that did not work before, expecting different results. Doing that may have had a great result before, when we had a race between devices appearing and pools being mounted, and `mount-zfs` was tasked with the full responsibility of importing the needed pool, but nowadays it is wrong behavior and should be suppressed. I deduced that self-deletion was the correct thing to do by looking at other Dracut code, because (as we all are very fully aware of) Dracut is entirely, ahem, "implementation-defined". Tested-by: @wphilips Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Manuel Amador (Rudd-O) <rudd-o@rudd-o.com> Closes #5157 Closes #5204
2016-10-06 20:26:47 +03:00
GENERATOR_DIR="$1"
Use a different technique to detect whether to mount-zfs The behavior of the Dracut module was very wrong before. The correct behavior: initramfs should not run `zfs-mount` to completion if the two generator files exist. If, however, one of them is missing, it indicates one of three cases: * The kernel command line did not specify a root ZFS file system, and another Dracut module is already handling root mount (via systemd). `mount-zfs` can run, but it will do nothing. * There is no systemd to run `sysroot.mount` to begin with. `mount-zfs` must run. * The root parameter is zfs:AUTO, which cannot be run in sysroot.mount. `mount-zfs` must run. In any of these three cases, it is safe to run `zfs-mount` to completion. `zfs-mount` must also delete itself if it determines it should not run, or else Dracut will do the insane thing of running it over and over again. Literally, the definition of insanity, doing the same thing that did not work before, expecting different results. Doing that may have had a great result before, when we had a race between devices appearing and pools being mounted, and `mount-zfs` was tasked with the full responsibility of importing the needed pool, but nowadays it is wrong behavior and should be suppressed. I deduced that self-deletion was the correct thing to do by looking at other Dracut code, because (as we all are very fully aware of) Dracut is entirely, ahem, "implementation-defined". Tested-by: @wphilips Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Manuel Amador (Rudd-O) <rudd-o@rudd-o.com> Closes #5157 Closes #5204
2016-10-06 20:26:47 +03:00
[ -n "$GENERATOR_DIR" ] || {
echo "zfs-generator: no generator directory specified, exiting" >> /dev/kmsg
exit 1
}
# shellcheck source=zfs-lib.sh.in
. /lib/dracut-zfs-lib.sh
decode_root_args || exit 0
[ -z "${rootflags}" ] && rootflags=$(getarg rootflags=)
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
case ",${rootflags}," in
*,zfsutil,*) ;;
,,) rootflags=zfsutil ;;
*) rootflags="zfsutil,${rootflags}" ;;
esac
[ -n "$debug" ] && echo "zfs-generator: writing extension for sysroot.mount to $GENERATOR_DIR/sysroot.mount.d/zfs-enhancement.conf" >> /dev/kmsg
Use a different technique to detect whether to mount-zfs The behavior of the Dracut module was very wrong before. The correct behavior: initramfs should not run `zfs-mount` to completion if the two generator files exist. If, however, one of them is missing, it indicates one of three cases: * The kernel command line did not specify a root ZFS file system, and another Dracut module is already handling root mount (via systemd). `mount-zfs` can run, but it will do nothing. * There is no systemd to run `sysroot.mount` to begin with. `mount-zfs` must run. * The root parameter is zfs:AUTO, which cannot be run in sysroot.mount. `mount-zfs` must run. In any of these three cases, it is safe to run `zfs-mount` to completion. `zfs-mount` must also delete itself if it determines it should not run, or else Dracut will do the insane thing of running it over and over again. Literally, the definition of insanity, doing the same thing that did not work before, expecting different results. Doing that may have had a great result before, when we had a race between devices appearing and pools being mounted, and `mount-zfs` was tasked with the full responsibility of importing the needed pool, but nowadays it is wrong behavior and should be suppressed. I deduced that self-deletion was the correct thing to do by looking at other Dracut code, because (as we all are very fully aware of) Dracut is entirely, ahem, "implementation-defined". Tested-by: @wphilips Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Manuel Amador (Rudd-O) <rudd-o@rudd-o.com> Closes #5157 Closes #5204
2016-10-06 20:26:47 +03:00
mkdir -p "$GENERATOR_DIR"/sysroot.mount.d "$GENERATOR_DIR"/initrd-root-fs.target.requires "$GENERATOR_DIR"/dracut-pre-mount.service.d
{
echo "[Unit]"
Use a different technique to detect whether to mount-zfs The behavior of the Dracut module was very wrong before. The correct behavior: initramfs should not run `zfs-mount` to completion if the two generator files exist. If, however, one of them is missing, it indicates one of three cases: * The kernel command line did not specify a root ZFS file system, and another Dracut module is already handling root mount (via systemd). `mount-zfs` can run, but it will do nothing. * There is no systemd to run `sysroot.mount` to begin with. `mount-zfs` must run. * The root parameter is zfs:AUTO, which cannot be run in sysroot.mount. `mount-zfs` must run. In any of these three cases, it is safe to run `zfs-mount` to completion. `zfs-mount` must also delete itself if it determines it should not run, or else Dracut will do the insane thing of running it over and over again. Literally, the definition of insanity, doing the same thing that did not work before, expecting different results. Doing that may have had a great result before, when we had a race between devices appearing and pools being mounted, and `mount-zfs` was tasked with the full responsibility of importing the needed pool, but nowadays it is wrong behavior and should be suppressed. I deduced that self-deletion was the correct thing to do by looking at other Dracut code, because (as we all are very fully aware of) Dracut is entirely, ahem, "implementation-defined". Tested-by: @wphilips Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Manuel Amador (Rudd-O) <rudd-o@rudd-o.com> Closes #5157 Closes #5204
2016-10-06 20:26:47 +03:00
echo "Before=initrd-root-fs.target"
echo "After=zfs-import.target"
echo
echo "[Mount]"
if [ "${root}" = "zfs:AUTO" ]; then
echo "PassEnvironment=BOOTFS"
echo 'What=${BOOTFS}'
else
echo "What=${root}"
fi
echo "Type=zfs"
echo "Options=${rootflags}"
Use a different technique to detect whether to mount-zfs The behavior of the Dracut module was very wrong before. The correct behavior: initramfs should not run `zfs-mount` to completion if the two generator files exist. If, however, one of them is missing, it indicates one of three cases: * The kernel command line did not specify a root ZFS file system, and another Dracut module is already handling root mount (via systemd). `mount-zfs` can run, but it will do nothing. * There is no systemd to run `sysroot.mount` to begin with. `mount-zfs` must run. * The root parameter is zfs:AUTO, which cannot be run in sysroot.mount. `mount-zfs` must run. In any of these three cases, it is safe to run `zfs-mount` to completion. `zfs-mount` must also delete itself if it determines it should not run, or else Dracut will do the insane thing of running it over and over again. Literally, the definition of insanity, doing the same thing that did not work before, expecting different results. Doing that may have had a great result before, when we had a race between devices appearing and pools being mounted, and `mount-zfs` was tasked with the full responsibility of importing the needed pool, but nowadays it is wrong behavior and should be suppressed. I deduced that self-deletion was the correct thing to do by looking at other Dracut code, because (as we all are very fully aware of) Dracut is entirely, ahem, "implementation-defined". Tested-by: @wphilips Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Manuel Amador (Rudd-O) <rudd-o@rudd-o.com> Closes #5157 Closes #5204
2016-10-06 20:26:47 +03:00
} > "$GENERATOR_DIR"/sysroot.mount.d/zfs-enhancement.conf
ln -fs ../sysroot.mount "$GENERATOR_DIR"/initrd-root-fs.target.requires/sysroot.mount
if [ "${root}" = "zfs:AUTO" ]; then
{
echo "[Unit]"
echo "Before=initrd-root-fs.target"
echo "After=sysroot.mount"
echo "DefaultDependencies=no"
echo
echo "[Service]"
echo "Type=oneshot"
echo "PassEnvironment=BOOTFS"
echo "ExecStart=/bin/sh -c '" ' \
. /lib/dracut-zfs-lib.sh; \
_zfs_nonroot_necessities_cb() { \
zfs mount | grep -m1 -q "^$1 " && return 0; \
echo "Mounting $1 on /sysroot$2"; \
mount -o zfsutil -t zfs "$1" "/sysroot$2"; \
}; \
for_relevant_root_children "${BOOTFS}" _zfs_nonroot_necessities_cb;' \
"'"
} > "$GENERATOR_DIR"/zfs-nonroot-necessities.service
ln -fs ../zfs-nonroot-necessities.service "$GENERATOR_DIR"/initrd-root-fs.target.requires/zfs-nonroot-necessities.service
else
# We can solve this statically at generation time, so do!
_zfs_generator_cb() {
dset="${1}"
mpnt="${2}"
unit="$(systemd-escape --suffix=mount -p "/sysroot${mpnt}")"
{
echo "[Unit]"
echo "Before=initrd-root-fs.target"
echo "After=sysroot.mount"
echo
echo "[Mount]"
echo "Where=/sysroot${mpnt}"
echo "What=${dset}"
echo "Type=zfs"
echo "Options=zfsutil"
} > "$GENERATOR_DIR/${unit}"
ln -fs ../"${unit}" "$GENERATOR_DIR"/initrd-root-fs.target.requires/"${unit}"
}
for_relevant_root_children "${root}" _zfs_generator_cb
fi
Use a different technique to detect whether to mount-zfs The behavior of the Dracut module was very wrong before. The correct behavior: initramfs should not run `zfs-mount` to completion if the two generator files exist. If, however, one of them is missing, it indicates one of three cases: * The kernel command line did not specify a root ZFS file system, and another Dracut module is already handling root mount (via systemd). `mount-zfs` can run, but it will do nothing. * There is no systemd to run `sysroot.mount` to begin with. `mount-zfs` must run. * The root parameter is zfs:AUTO, which cannot be run in sysroot.mount. `mount-zfs` must run. In any of these three cases, it is safe to run `zfs-mount` to completion. `zfs-mount` must also delete itself if it determines it should not run, or else Dracut will do the insane thing of running it over and over again. Literally, the definition of insanity, doing the same thing that did not work before, expecting different results. Doing that may have had a great result before, when we had a race between devices appearing and pools being mounted, and `mount-zfs` was tasked with the full responsibility of importing the needed pool, but nowadays it is wrong behavior and should be suppressed. I deduced that self-deletion was the correct thing to do by looking at other Dracut code, because (as we all are very fully aware of) Dracut is entirely, ahem, "implementation-defined". Tested-by: @wphilips Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Manuel Amador (Rudd-O) <rudd-o@rudd-o.com> Closes #5157 Closes #5204
2016-10-06 20:26:47 +03:00
{
echo "[Unit]"
echo "After=zfs-import.target"
} > "$GENERATOR_DIR"/dracut-pre-mount.service.d/zfs-enhancement.conf
[ -n "$debug" ] && echo "zfs-generator: finished" >> /dev/kmsg
exit 0