]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
feat: make variable set check work with "set -u"
authorBenjamin Drung <benjamin.drung@canonical.com>
Fri, 20 Jun 2025 20:55:18 +0000 (22:55 +0200)
committerNeal Gompa (ニール・ゴンパ) <ngompa13@gmail.com>
Thu, 3 Jul 2025 13:34:19 +0000 (09:34 -0400)
Make checking if a variable is defined/set work with `set -u`.

dracut-init.sh
dracut-logger.sh
dracut.sh
modules.d/80base/dracut-lib.sh
test/TEST-20-STORAGE/test.sh
test/TEST-72-NBD/test.sh
test/test-functions

index 4ba4897233f09c8a38defb9ee2fd256b45b57080..89d80fd8d7e043ab1c98ab002b0e0c516f0c1b38 100755 (executable)
@@ -31,7 +31,7 @@ is_func() {
     [[ "$(type -t "$1")" == "function" ]]
 }
 
-if ! [[ $dracutbasedir ]]; then
+if ! [[ ${dracutbasedir-} ]]; then
     dracutbasedir=${BASH_SOURCE[0]%/*}
     [[ $dracutbasedir == dracut-functions* ]] && dracutbasedir="."
     [[ $dracutbasedir ]] || dracutbasedir="."
@@ -44,7 +44,7 @@ if ! is_func dinfo > /dev/null 2>&1; then
     dlog_init
 fi
 
-if ! [[ $initdir ]]; then
+if ! [[ ${initdir-} ]]; then
     dfatal "initdir not set"
     exit 1
 fi
@@ -53,14 +53,14 @@ if ! [[ -d $initdir ]]; then
     mkdir -p "$initdir"
 fi
 
-if ! [[ $kernel ]]; then
+if ! [[ ${kernel-} ]]; then
     kernel=$(uname -r)
     export kernel
 fi
 
 srcmods="$(realpath -e "${dracutsysrootdir-}/lib/modules/$kernel")"
 
-[[ $drivers_dir ]] && {
+[[ ${drivers_dir-} ]] && {
     if ! command -v kmod &> /dev/null && vercmp "$(modprobe --version | cut -d' ' -f3)" lt 3.7; then
         dfatal 'To use --kmoddir option module-init-tools >= 3.7 is required.'
         exit 1
@@ -70,7 +70,7 @@ srcmods="$(realpath -e "${dracutsysrootdir-}/lib/modules/$kernel")"
 export srcmods
 
 # export standard hookdirs
-[[ $hookdirs ]] || {
+[[ ${hookdirs-} ]] || {
     hookdirs="cmdline pre-udev pre-trigger netroot "
     hookdirs+="initqueue initqueue/settled initqueue/online initqueue/finished initqueue/timeout "
     hookdirs+="pre-mount pre-pivot cleanup mount "
@@ -85,7 +85,7 @@ PKG_CONFIG=${PKG_CONFIG:-pkg-config}
 # shellcheck source=./dracut-functions.sh
 . "$dracutbasedir"/dracut-functions.sh
 
-if ! [[ $DRACUT_INSTALL ]]; then
+if ! [[ "${DRACUT_INSTALL-}" ]]; then
     DRACUT_INSTALL=$(find_binary dracut-install)
 fi
 
@@ -112,7 +112,7 @@ if [[ $DRINSTALLPARTS == 1 ]] && ! command -v "$DRACUT_INSTALL" > /dev/null 2>&1
 fi
 
 # Detect lib paths
-if ! [[ $libdirs ]]; then
+if ! [[ ${libdirs-} ]]; then
     if [[ $($DRACUT_INSTALL ${dracutsysrootdir:+-r "$dracutsysrootdir"} --dry-run -R "$DRACUT_TESTBIN") == */lib64/* ]] &> /dev/null \
         && [[ -d "${dracutsysrootdir-}/lib64" ]]; then
         libdirs+=" /lib64"
index c46a0e6c8c42a05f0227609fd87b3b79b08d7521..7c075f973c35f157a32252cebaa22c016958e815 100755 (executable)
@@ -104,16 +104,16 @@ dlog_init() {
     local __oldumask
     local ret=0
     local errmsg=
-    [ -z "$stdloglvl" ] && stdloglvl=4
-    [ -z "$sysloglvl" ] && sysloglvl=0
-    [ -z "$kmsgloglvl" ] && kmsgloglvl=0
+    [ -z "${stdloglvl-}" ] && stdloglvl=4
+    [ -z "${sysloglvl-}" ] && sysloglvl=0
+    [ -z "${kmsgloglvl-}" ] && kmsgloglvl=0
     # Skip initialization if it's already done.
-    [ -n "$maxloglvl" ] && return 0
+    [ -n "${maxloglvl-}" ] && return 0
 
-    if [ -z "$fileloglvl" ]; then
-        [ -w "$logfile" ] && fileloglvl=4 || fileloglvl=0
+    if [ -z "${fileloglvl-}" ]; then
+        [ -w "${logfile-}" ] && fileloglvl=4 || fileloglvl=0
     elif ((fileloglvl > 0)); then
-        if [[ $logfile ]]; then
+        if [[ ${logfile-} ]]; then
             __oldumask=$(umask)
             umask 0377
             ! [ -e "$logfile" ] && : > "$logfile"
index 480c35c1d384fdf3cf65c4e84386621765ecdfa2..39d6a91010d0a0b4e5d420b5be8525f311104a3b 100755 (executable)
--- a/dracut.sh
+++ b/dracut.sh
@@ -955,7 +955,7 @@ export DRACUT_LOG_LEVEL=warning
     debug=yes
 }
 
-[[ $dracutbasedir ]] || dracutbasedir="${dracutsysrootdir-}"/usr/lib/dracut
+[[ ${dracutbasedir-} ]] || dracutbasedir="${dracutsysrootdir-}"/usr/lib/dracut
 
 # These config variables needs to be exported for dracut-install.
 export add_dlopen_features="" omit_dlopen_features=""
@@ -1102,7 +1102,7 @@ export SYSTEMCTL=${SYSTEMCTL:-systemctl}
 ((${#libdirs_l[@]})) && libdirs="${libdirs_l[*]}"
 
 [[ $stdloglvl_l ]] && stdloglvl=$stdloglvl_l
-[[ ! $stdloglvl ]] && stdloglvl=4
+[[ ! ${stdloglvl-} ]] && stdloglvl=4
 stdloglvl=$((stdloglvl + verbosity_mod_l))
 ((stdloglvl > 6)) && stdloglvl=6
 ((stdloglvl < 0)) && stdloglvl=0
index 7a7496884ed18ca1e65147b7515c87185a776d4d..6892d1c41de9b5d808223b3edb65750df94c562e 100755 (executable)
@@ -970,7 +970,7 @@ are_lists_eq() {
 }
 
 setmemdebug() {
-    if [ -z "$DEBUG_MEM_LEVEL" ]; then
+    if [ -z "${DEBUG_MEM_LEVEL-}" ]; then
         DEBUG_MEM_LEVEL=$(getargnum 0 0 5 rd.memdebug)
         export DEBUG_MEM_LEVEL
     fi
index c788cfa407248451c6ff0426e354640e6084ea4d..8ed05104d73de3b009ca63a6230896024a86b27c 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env bash
 set -e
 
-[ -z "$TEST_FSTYPE" ] && TEST_FSTYPE="ext4"
+[ -z "${TEST_FSTYPE-}" ] && TEST_FSTYPE="ext4"
 
 # shellcheck disable=SC2034
 TEST_DESCRIPTION="root filesystem on multiple device $TEST_FSTYPE (on top of RAID and LUKS)"
index e87d0d3911b1b95fca62c817a13c19fae6190919..9476c66c097e9b484c0e624fd124491468d2bd98 100755 (executable)
@@ -63,13 +63,10 @@ client_test() {
     local test_name="$1"
     local mac=$2
     local cmdline="$3"
-    local fstype=$4
-    local fsopt=$5
+    local fstype=${4-ext4}
+    local fsopt=${5-ro}
     local found opts nbdinfo
 
-    [[ $fstype ]] || fstype=ext4
-    [[ $fsopt ]] || fsopt="ro"
-
     echo "CLIENT TEST START: $test_name"
 
     declare -a disk_args=()
index 2a4c1eca2dbbc7cda01691b58480ae22fd82f0b4..7125e4239bef25d8f29bf165a0e84789d4189708 100644 (file)
@@ -4,14 +4,14 @@ export PATH
 
 # shellcheck disable=SC1090
 [[ -e .testdir${TEST_RUN_ID:+-$TEST_RUN_ID} ]] && . .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
-if [[ -z $TESTDIR ]] || [[ ! -d $TESTDIR ]]; then
+if [[ -z ${TESTDIR-} ]] || [[ ! -d $TESTDIR ]]; then
     TESTDIR=$(mktemp -d -p "/var/tmp" -t dracut-test.XXXXXX)
 fi
 echo "TESTDIR=\"$TESTDIR\"" > .testdir${TEST_RUN_ID:+-$TEST_RUN_ID}
 export TESTDIR
 export BOOT_ROOT="$TESTDIR"
 
-if [[ -z $basedir ]]; then basedir="$(realpath ../..)"; fi
+if [[ -z ${basedir-} ]]; then basedir="$(realpath ../..)"; fi
 
 DRACUT=${DRACUT-dracut}
 PKGLIBDIR=${PKGLIBDIR-$basedir}
@@ -19,7 +19,7 @@ PKGLIBDIR=${PKGLIBDIR-$basedir}
 if [[ -f /etc/machine-id ]]; then
     read -r TOKEN < /etc/machine-id || true
 fi
-[ -z "$TOKEN" ] && . /etc/os-release && TOKEN="$ID"
+[ -z "${TOKEN-}" ] && . /etc/os-release && TOKEN="$ID"
 
 TEST_KERNEL_CMDLINE+=" root=LABEL=dracut panic=1 oops=panic softlockup_panic=1 systemd.crash_reboot ${DEBUGFAIL-} "
 
@@ -106,10 +106,10 @@ determine_kernel_version() {
     # we are running inside a container already
 
     # shellcheck disable=SC2012
-    [[ $KVERSION ]] || KVERSION="$(cd /lib/modules && ls -1 | tail -1)"
+    [[ ${KVERSION-} ]] || KVERSION="$(cd /lib/modules && ls -1 | tail -1)"
     # shellcheck disable=SC2012
-    [[ $KVERSION ]] || KVERSION="$(cd /usr/lib/modules && ls -1 | tail -1)"
-    [[ $KVERSION ]] || KVERSION="$(uname -r)"
+    [[ ${KVERSION-} ]] || KVERSION="$(cd /usr/lib/modules && ls -1 | tail -1)"
+    [[ ${KVERSION-} ]] || KVERSION="$(uname -r)"
 }
 
 set_vmlinux_env() {
@@ -121,7 +121,7 @@ set_vmlinux_env() {
     if ! [ -f "$VMLINUZ" ]; then
         [[ -f /etc/machine-id ]] && read -r MACHINE_ID < /etc/machine-id
 
-        if [[ $MACHINE_ID ]] && { [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]]; }; then
+        if [[ ${MACHINE_ID-} ]] && { [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]]; }; then
             VMLINUZ="/boot/${MACHINE_ID}/$KVERSION/linux"
         elif [ -f "/boot/vmlinuz-${KVERSION}" ]; then
             VMLINUZ="/boot/vmlinuz-${KVERSION}"