From 24a6f8384706e94bb3780e8d6f040872ecd2a71e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C4=90o=C3=A0n=20Tr=E1=BA=A7n=20C=C3=B4ng=20Danh?=
 <congdanhqx@gmail.com>
Date: Sun, 29 Nov 2020 02:02:08 +0700
Subject: [PATCH] dracut: use /bin/sh instead of bash as the intepreter
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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
---
 Makefile.am                                 |  1 +
 contrib/dracut/90zfs/export-zfs.sh.in       |  2 +-
 contrib/dracut/90zfs/mount-zfs.sh.in        |  2 +-
 contrib/dracut/90zfs/parse-zfs.sh.in        |  2 +-
 contrib/dracut/90zfs/zfs-generator.sh.in    | 23 +++++++++++----------
 contrib/dracut/90zfs/zfs-lib.sh.in          |  4 ++--
 contrib/dracut/90zfs/zfs-load-key.sh.in     |  2 +-
 contrib/dracut/90zfs/zfs-needshutdown.sh.in |  4 ++--
 8 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index eac1a3e97e..436b78d762 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -159,6 +159,7 @@ checkbashisms:
 				-o -name 'smart' -prune \
 				-o -name 'paxcheck.sh' -prune \
 				-o -name 'make_gitrev.sh' -prune \
+				-o -name '90zfs' -prune \
 				-o -type f ! -name 'config*' \
 				! -name 'libtool' \
 			-exec sh -c 'awk "NR==1 && /\#\!.*bin\/sh.*/ {print FILENAME;}" "{}"' \;); \
diff --git a/contrib/dracut/90zfs/export-zfs.sh.in b/contrib/dracut/90zfs/export-zfs.sh.in
index 09e4a3cc0e..8926503834 100755
--- a/contrib/dracut/90zfs/export-zfs.sh.in
+++ b/contrib/dracut/90zfs/export-zfs.sh.in
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 . /lib/dracut-zfs-lib.sh
 
diff --git a/contrib/dracut/90zfs/mount-zfs.sh.in b/contrib/dracut/90zfs/mount-zfs.sh.in
index f5b3d9056c..73300a9b66 100755
--- a/contrib/dracut/90zfs/mount-zfs.sh.in
+++ b/contrib/dracut/90zfs/mount-zfs.sh.in
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 . /lib/dracut-zfs-lib.sh
 
diff --git a/contrib/dracut/90zfs/parse-zfs.sh.in b/contrib/dracut/90zfs/parse-zfs.sh.in
index 2ff76d8fa0..768de9dd25 100755
--- a/contrib/dracut/90zfs/parse-zfs.sh.in
+++ b/contrib/dracut/90zfs/parse-zfs.sh.in
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 . /lib/dracut-lib.sh
 
diff --git a/contrib/dracut/90zfs/zfs-generator.sh.in b/contrib/dracut/90zfs/zfs-generator.sh.in
index 120b9ecf95..59cdadcbea 100755
--- a/contrib/dracut/90zfs/zfs-generator.sh.in
+++ b/contrib/dracut/90zfs/zfs-generator.sh.in
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+#!/bin/sh
 
 echo "zfs-generator: starting" >> /dev/kmsg
 
@@ -11,7 +11,7 @@ GENERATOR_DIR="$1"
 [ -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
 
-type getarg >/dev/null 2>&1 || {
+command -v getarg >/dev/null 2>&1 || {
     echo "zfs-generator: loading Dracut library from $dracutlib" >> /dev/kmsg
     . "$dracutlib"
 }
@@ -22,16 +22,17 @@ type getarg >/dev/null 2>&1 || {
 
 # If root is not ZFS= or zfs: or rootfstype is not zfs
 # then we are not supposed to handle it.
-[ "${root##zfs:}" = "${root}" -a "${root##ZFS=}" = "${root}" -a "$rootfstype" != "zfs" ] && exit 0
+[ "${root##zfs:}" = "${root}" ] &&
+	[ "${root##ZFS=}" = "${root}" ] &&
+	[ "$rootfstype" != "zfs" ] &&
+	exit 0
 
 rootfstype=zfs
-if echo "${rootflags}" | grep -Eq '^zfsutil$|^zfsutil,|,zfsutil$|,zfsutil,' ; then
-    true
-elif test -n "${rootflags}" ; then
-    rootflags="zfsutil,${rootflags}"
-else
-    rootflags=zfsutil
-fi
+case ",${rootflags}," in
+	*,zfsutil,*) ;;
+	,,)	rootflags=zfsutil ;;
+	*)	rootflags="zfsutil,${rootflags}" ;;
+esac
 
 echo "zfs-generator: writing extension for sysroot.mount to $GENERATOR_DIR"/sysroot.mount.d/zfs-enhancement.conf >> /dev/kmsg
 
@@ -58,4 +59,4 @@ echo "zfs-generator: writing extension for sysroot.mount to $GENERATOR_DIR"/sysr
 [ -d "$GENERATOR_DIR"/initrd-root-fs.target.requires ] || mkdir -p "$GENERATOR_DIR"/initrd-root-fs.target.requires
 ln -s ../sysroot.mount "$GENERATOR_DIR"/initrd-root-fs.target.requires/sysroot.mount
 
-echo "zfs-generator: finished" >> /dev/kmsg
\ No newline at end of file
+echo "zfs-generator: finished" >> /dev/kmsg
diff --git a/contrib/dracut/90zfs/zfs-lib.sh.in b/contrib/dracut/90zfs/zfs-lib.sh.in
index f470bfcc54..c39cc5cfff 100755
--- a/contrib/dracut/90zfs/zfs-lib.sh.in
+++ b/contrib/dracut/90zfs/zfs-lib.sh.in
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 command -v getarg >/dev/null || . /lib/dracut-lib.sh
 command -v getargbool >/dev/null || {
@@ -144,7 +144,7 @@ ask_for_password() {
 
     { flock -s 9;
         # Prompt for password with plymouth, if installed and running.
-        if type plymouth >/dev/null 2>&1 && plymouth --ping 2>/dev/null; then
+        if plymouth --ping 2>/dev/null; then
             plymouth ask-for-password \
                 --prompt "$ply_prompt" --number-of-tries="$ply_tries" \
                 --command="$ply_cmd"
diff --git a/contrib/dracut/90zfs/zfs-load-key.sh.in b/contrib/dracut/90zfs/zfs-load-key.sh.in
index ff586ef654..85e55c51bf 100755
--- a/contrib/dracut/90zfs/zfs-load-key.sh.in
+++ b/contrib/dracut/90zfs/zfs-load-key.sh.in
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 # only run this on systemd systems, we handle the decrypt in mount-zfs.sh in the mount hook otherwise
 [ -e /bin/systemctl ] || return 0
diff --git a/contrib/dracut/90zfs/zfs-needshutdown.sh.in b/contrib/dracut/90zfs/zfs-needshutdown.sh.in
index ddd3edae00..dd6de30c27 100755
--- a/contrib/dracut/90zfs/zfs-needshutdown.sh.in
+++ b/contrib/dracut/90zfs/zfs-needshutdown.sh.in
@@ -1,6 +1,6 @@
-#!/bin/bash
+#!/bin/sh
 
-type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
+command -v getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
 
 if zpool list 2>&1 | grep -q 'no pools available' ; then
     info "ZFS: No active pools, no need to export anything."