]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
fix(dracut): use grep -q/-s to silence output/error
authorCoiby Xu <coxu@redhat.com>
Thu, 16 Oct 2025 07:43:05 +0000 (15:43 +0800)
committerNeal Gompa (ニール・ゴンパ) <ngompa13@gmail.com>
Wed, 29 Oct 2025 11:41:06 +0000 (07:41 -0400)
There is no need to redirect output/error to /dev/null. Simply use -q/-s
option provided by grep.

Note we should use avoid using
"set -o pipefail lsinitrd PAHT_PATH_INITRAMFS | grep -q PATTERN".
Because with set -o pipefail, a pipeline’s exit status becomes non-zero
if any element of the pipeline fails and grep -q exits as soon as it
sees a match.

Signed-off-by: Coiby Xu <coxu@redhat.com>
14 files changed:
dracut-init.sh
dracut-initramfs-restore.sh
modules.d/10systemd/module-setup.sh
modules.d/11systemd-ask-password/module-setup.sh
modules.d/35network-legacy/dhcp-multi.sh
modules.d/35network-legacy/ifup.sh
modules.d/35network-manager/nm-run.sh
modules.d/70bluetooth/module-setup.sh
modules.d/74debug/module-setup.sh
modules.d/74fcoe/module-setup.sh
modules.d/74resume/module-setup.sh
modules.d/74udev-rules/module-setup.sh
modules.d/77dracut-systemd/parse-root.sh
modules.d/80base/module-setup.sh

index 74ec1cdfe688fdeece6297bb242dd3f0ca7412c1..0235cd2445e127d50c8073267e0fde37df6bacd2 100755 (executable)
@@ -475,15 +475,15 @@ inst_rule_group_owner() {
 
     # shellcheck disable=SC2013
     for i in $(sed -nr 's/.*OWNER=?"([^ "]+).*/\1/p' "$1"); do
-        if ! grep -Eq "^$i:" "$initdir/etc/passwd" 2> /dev/null; then
-            grep -E "^$i:" "${dracutsysrootdir-}/etc/passwd" 2> /dev/null >> "$initdir/etc/passwd"
+        if ! grep -Eqs "^$i:" "$initdir/etc/passwd"; then
+            grep -Es "^$i:" "${dracutsysrootdir-}/etc/passwd" >> "$initdir/etc/passwd"
         fi
     done
 
     # shellcheck disable=SC2013
     for i in $(sed -nr 's/.*GROUP=?"([^ "]+).*/\1/p' "$1"); do
-        if ! grep -Eq "^$i:" "$initdir/etc/group" 2> /dev/null; then
-            grep -E "^$i:" "${dracutsysrootdir-}/etc/group" 2> /dev/null >> "$initdir/etc/group"
+        if ! grep -Eqs "^$i:" "$initdir/etc/group"; then
+            grep -Es "^$i:" "${dracutsysrootdir-}/etc/group" >> "$initdir/etc/group"
         fi
     done
 }
index 45aeead8bef6b04b13fba1ae652893aaf75a9bb4..828edca59c50c32a7f950de8c0caf9769502ce70 100755 (executable)
@@ -99,7 +99,7 @@ elif [[ -f erofs-root.img ]]; then
     fi
 fi
 
-if grep -q -w selinux /sys/kernel/security/lsm 2> /dev/null \
+if grep -qs -w selinux /sys/kernel/security/lsm \
     && [ -e /etc/selinux/config ] && [ -x /usr/sbin/setfiles ]; then
     . /etc/selinux/config
     if [[ $SELINUX != "disabled" && -n $SELINUXTYPE ]]; then
index 21a8f5b6f00d9a0d71a33bf99cc88e438271dc81..c5be7a846b279ab4f85bcc7398ded1c918cfc540 100755 (executable)
@@ -118,18 +118,18 @@ install() {
 
     inst_multiple -o nologin
     {
-        grep '^adm:' "${dracutsysrootdir-}"/etc/passwd 2> /dev/null
+        grep -s '^adm:' "${dracutsysrootdir-}"/etc/passwd
         # we don't use systemd-networkd, but the user is in systemd.conf tmpfiles snippet
-        grep '^systemd-network:' "${dracutsysrootdir-}"/etc/passwd 2> /dev/null
+        grep -s '^systemd-network:' "${dracutsysrootdir-}"/etc/passwd
     } >> "$initdir/etc/passwd"
 
     {
-        grep '^wheel:' "${dracutsysrootdir-}"/etc/group 2> /dev/null
-        grep '^adm:' "${dracutsysrootdir-}"/etc/group 2> /dev/null
-        grep '^utmp:' "${dracutsysrootdir-}"/etc/group 2> /dev/null
-        grep '^root:' "${dracutsysrootdir-}"/etc/group 2> /dev/null
+        grep -s '^wheel:' "${dracutsysrootdir-}"/etc/group
+        grep -s '^adm:' "${dracutsysrootdir-}"/etc/group
+        grep -s '^utmp:' "${dracutsysrootdir-}"/etc/group
+        grep -s '^root:' "${dracutsysrootdir-}"/etc/group
         # we don't use systemd-networkd, but the user is in systemd.conf tmpfiles snippet
-        grep '^systemd-network:' "${dracutsysrootdir-}"/etc/group 2> /dev/null
+        grep -s '^systemd-network:' "${dracutsysrootdir-}"/etc/group
     } >> "$initdir/etc/group"
 
     local _systemdbinary="$systemdutildir"/systemd
index 8b09b69f2d862f675c91afc2d102f9b5b42af452..e1a3a9c789af3b54d17d3c88d9512bbf941f97e8 100755 (executable)
@@ -27,7 +27,7 @@ depends() {
         # - ramfb is not enough
         # Therefore, depend on the drm module if virtio_gpu is loaded on the system.
         if [[ ${DRACUT_ARCH:-$(uname -m)} == arm* || ${DRACUT_ARCH:-$(uname -m)} == aarch64 ]] \
-            && grep -r -q "virtio:d00000010v" /sys/bus/virtio/devices/*/modalias 2> /dev/null; then
+            && grep -r -qs "virtio:d00000010v" /sys/bus/virtio/devices/*/modalias; then
             echo drm
         fi
     fi
index f8104c33e0326ee5f28178284c741637298d15e2..1c5ee733469674663e16d22dd66aef27a693630c 100755 (executable)
@@ -22,7 +22,7 @@ do_dhclient() {
     _DHCPRETRY=$(getargnum 1 1 1000000000 rd.net.dhcp.retry=)
 
     if [ -n "$_timeout" ]; then
-        if ! (dhclient --help 2>&1 | grep -q -F -- '--timeout' 2> /dev/null); then
+        if ! (dhclient --help 2>&1 | grep -qs -F -- '--timeout'); then
             warn "rd.net.timeout.dhcp has no effect because dhclient does not implement the --timeout option"
             unset _timeout
         fi
index 59629f11135e5629375ef55b93916f9c0e03b9eb..b2ed4607064f55c8a0d4ee674b766e6b3b2c7afb 100755 (executable)
@@ -73,7 +73,7 @@ do_dhcp() {
     fi
 
     if [ -n "$_timeout" ]; then
-        if ! (dhclient --help 2>&1 | grep -q -F -- '--timeout' 2> /dev/null); then
+        if ! (dhclient --help 2>&1 | grep -qs -F -- '--timeout'); then
             warn "rd.net.timeout.dhcp has no effect because dhclient does not implement the --timeout option"
             unset _timeout
         fi
index 5d447fc39903c50bdf3809919863252f7d8a10c8..c7e8b4461825b7c47656a613c9a9f86f3e21e722 100755 (executable)
@@ -57,7 +57,7 @@ dhcpopts_create() {
 for _i in /sys/class/net/*; do
     [ -d "$_i" ] || continue
     state="/run/NetworkManager/devices/$(cat "$_i"/ifindex)"
-    grep -q '^connection-uuid=' "$state" 2> /dev/null || continue
+    grep -qs '^connection-uuid=' "$state" || continue
     ifname="${_i##*/}"
     dhcpopts_create "$state" > /tmp/dhclient."$ifname".dhcpopts
     source_hook initqueue/online "$ifname"
index e9d550dffa9e26e54f178f91a42de0c247a58bc8..3b5049950b26697c52a069500f849142223f9220 100755 (executable)
@@ -13,7 +13,7 @@ check() {
         #  * Keyboard (0x40)
         #  * Keyboard/pointing (0xC0)
         # and if Appearance is set to the value defined for keyboard (0x03C1)
-        [ -d "/sys/class/bluetooth" ] && grep -qiE -e 'Class=0x[0-9a-f]{3}5[4c]0' -e 'Appearance=0x03c1' /var/lib/bluetooth/*/*/info 2> /dev/null \
+        [ -d "/sys/class/bluetooth" ] && grep -qsiE -e 'Class=0x[0-9a-f]{3}5[4c]0' -e 'Appearance=0x03c1' /var/lib/bluetooth/*/*/info \
             && dwarn "If you need to use bluetooth, please include it explicitly."
     fi
 
index 38566c6bc5953899ad7013f08cd5019a34637f61..a0e5ffb0f4c2723adf9014a7c46f4b40801e982c 100755 (executable)
@@ -47,5 +47,5 @@ install() {
         tcpdump \
         vi
 
-    grep '^tcpdump:' "${dracutsysrootdir-}"/etc/passwd 2> /dev/null >> "$initdir/etc/passwd"
+    grep -s '^tcpdump:' "${dracutsysrootdir-}"/etc/passwd >> "$initdir/etc/passwd"
 }
index cb4659079e1d0f5aee01dea1f8ca36d794b4fcae..dfbf27bde03190382f0bf2e2df6e082faeccaaf1 100755 (executable)
@@ -79,12 +79,12 @@ cmdline() {
             # DCB_REQUIRED in "/etc/fcoe/cfg-xxx" is expected to set to "no".
             #
             # Force "nodcb" if there's any DCB_REQUIRED="no"(child or vlan parent).
-            if grep -q '^[[:blank:]]*DCB_REQUIRED="no"' /etc/fcoe/cfg-"${i##*/}" &> /dev/null; then
+            if grep -qs '^[[:blank:]]*DCB_REQUIRED="no"' /etc/fcoe/cfg-"${i##*/}"; then
                 dcb="nodcb"
             fi
 
             if [ "$p" ]; then
-                if grep -q '^[[:blank:]]*DCB_REQUIRED="no"' /etc/fcoe/cfg-"${p}" &> /dev/null; then
+                if grep -qs '^[[:blank:]]*DCB_REQUIRED="no"' /etc/fcoe/cfg-"${p}"; then
                     dcb="nodcb"
                 fi
             fi
index d8e914c6a61843034616fcadae29b68db9f8db1f..b0bbc8711bddd351ed01821ea7d3b43824c292b9 100755 (executable)
@@ -14,7 +14,7 @@ check() {
     if [[ $hostonly ]] || [[ $mount_needs ]]; then
         # Resuming won't work if swap is on a netdevice
         swap_on_netdevice && return 255
-        if grep -rqE '(^| )resume=' /proc/cmdline /etc/cmdline /etc/cmdline.d /etc/kernel/cmdline /usr/lib/kernel/cmdline 2> /dev/null; then
+        if grep -rqsE '(^| )resume=' /proc/cmdline /etc/cmdline /etc/cmdline.d /etc/kernel/cmdline /usr/lib/kernel/cmdline; then
             # hibernation support requested on kernel command line
             return 0
         else
index f98ed33a529535de41b3eaca9e87bac76a0acb59..b30eafda6c3d03463acb600e299a499b6312e36a 100755 (executable)
@@ -62,8 +62,8 @@ install() {
 
     {
         for i in cdrom tape dialout floppy; do
-            if ! grep -q "^$i:" "$initdir"/etc/group 2> /dev/null; then
-                if ! grep "^$i:" "${dracutsysrootdir-}"/etc/group 2> /dev/null; then
+            if ! grep -qs "^$i:" "$initdir"/etc/group; then
+                if ! grep -s "^$i:" "${dracutsysrootdir-}"/etc/group; then
                     case $i in
                         cdrom) echo "$i:x:11:" ;;
                         dialout) echo "$i:x:18:" ;;
index 350951b53dbcabf895fd3ff38cd8700871252ac2..7f96d660c92145fced9efd0bfa3cdfb0300e9836 100755 (executable)
@@ -26,7 +26,7 @@ if [ "$rootok" = "1" ]; then
         # after remote-fs-pre.target since the initqueue is ordered before it so
         # it will never actually show up (think Tang-pinned rootfs).
         cat > "$hookdir/initqueue/finished/devexists-${root_name}.sh" << EOF
-if ! grep -q After=remote-fs-pre.target /run/systemd/generator/systemd-cryptsetup@*.service 2>/dev/null; then
+if ! grep -qs After=remote-fs-pre.target /run/systemd/generator/systemd-cryptsetup@*.service; then
     [ -e "$root_dev" ]
 fi
 EOF
index f400f49b00dbd8744618b895d1c1e5d2a5a641fa..6e12de1c4f4445b0158b65168d4557dd0f7e3660 100755 (executable)
@@ -66,11 +66,11 @@ install() {
     # add common users in /etc/passwd, it will be used by nfs/ssh currently
     # use password for hostonly images to facilitate secure sulogin in emergency console
     [[ $hostonly ]] && pwshadow='x'
-    grep '^root:' "$initdir/etc/passwd" > /dev/null 2>&1 || echo "root:$pwshadow:0:0::/root:/bin/sh" >> "$initdir/etc/passwd"
+    grep -qs '^root:' "$initdir/etc/passwd" || echo "root:$pwshadow:0:0::/root:/bin/sh" >> "$initdir/etc/passwd"
 
     if [[ $hostonly ]]; then
         # check if other dracut modules already created an entry for root in /etc/shadow
-        if grep -q '^root:' "$initdir/etc/shadow" > /dev/null 2>&1; then
+        if grep -qs '^root:' "$initdir/etc/shadow"; then
             grep -v '^root:' "$initdir/etc/shadow" > "$initdir/etc/shadow-"
             mv "$initdir/etc/shadow-" "$initdir/etc/shadow"
         fi