Use "grep -q <<< $(cmd)" instead of "cmd | grep -q"

Resolves: RHEL-40948
Conflict: Yes, commented the diff code within the patch's commit log,
          otherwise the patch command will regard the diff code as regular
          patch hunk and fail.
Upstream: fedora

commit 98087d78eda2ca58a3b55ee5dd9e0e7bad8467ef
Author: Tao Liu <ltao@redhat.com>
Date:   Tue Jun 25 16:35:16 2024 +1200

    Use "grep -q <<< $(cmd)" instead of "cmd | grep -q"

    The following line of code's exit value should be the exit value of
    grep by default:

        cmd | grep -q

    However it will not always work as expected, a 141 exit code may be
    returned, see the following debug info:

    --- a/usr/lib/dracut/modules.d/99kdumpbase/module-setup.sh
    +++ b/usr/lib/dracut/modules.d/99kdumpbase/module-setup.sh
    @@ -55,6 +55,11 @@ depends() {
             _dep="$_dep ssh-client"
         fi

    +    dracut --list-modules 1>&2
    +    echo $? 1>&2
    +    dracut --list-modules | grep -q lvmthinpool-monitor
    +    echo $? ${PIPESTATUS[0]} ${PIPESTATUS[1]} 1>&2
    +
         if is_lvm2_thinp_dump_target; then
             if dracut --list-modules | grep -q lvmthinpool-monitor; then
                 add_opt_module lvmthinpool-monitor

    $ kdumpctl rebuild
    kdump: Rebuilding /boot/initramfs-6.10.0-0.rc4.11.el10.x86_64kdump.img
    ...snip...
    lvmmerge
    lvmthinpool-monitor
    ..snip...
    uefi-lib
    0
    141 141 0

    The reason is, grep exits immediately when there is a match but the
    first cmd still keep writing to the pipe. Since there is no reader,
    a SIGPIPE signal is generated. As a result, a 141 exit code will be
    returned.

    Let's use the following line of code instead, which have the same effect
    but works as expected:

        grep -q <<< $(cmd)

    Signed-off-by: Tao Liu <ltao@redhat.com>

Signed-off-by: Tao Liu <ltao@redhat.com>
This commit is contained in:
Tao Liu 2024-06-28 15:15:14 +12:00
parent 54cb91011c
commit 1bcff505ed
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,69 @@
From 98087d78eda2ca58a3b55ee5dd9e0e7bad8467ef Mon Sep 17 00:00:00 2001
From: Tao Liu <ltao@redhat.com>
Date: Tue, 25 Jun 2024 16:35:16 +1200
Subject: [PATCH] Use "grep -q <<< $(cmd)" instead of "cmd | grep -q"
The following line of code's exit value should be the exit value of
grep by default:
cmd | grep -q
However it will not always work as expected, a 141 exit code may be
returned, see the following debug info:
//--- a/usr/lib/dracut/modules.d/99kdumpbase/module-setup.sh
//+++ b/usr/lib/dracut/modules.d/99kdumpbase/module-setup.sh
//@@ -55,6 +55,11 @@ depends() {
// _dep="$_dep ssh-client"
// fi
//
//+ dracut --list-modules 1>&2
//+ echo $? 1>&2
//+ dracut --list-modules | grep -q lvmthinpool-monitor
//+ echo $? ${PIPESTATUS[0]} ${PIPESTATUS[1]} 1>&2
//+
// if is_lvm2_thinp_dump_target; then
// if dracut --list-modules | grep -q lvmthinpool-monitor; then
// add_opt_module lvmthinpool-monitor
$ kdumpctl rebuild
kdump: Rebuilding /boot/initramfs-6.10.0-0.rc4.11.el10.x86_64kdump.img
...snip...
lvmmerge
lvmthinpool-monitor
..snip...
uefi-lib
0
141 141 0
The reason is, grep exits immediately when there is a match but the
first cmd still keep writing to the pipe. Since there is no reader,
a SIGPIPE signal is generated. As a result, a 141 exit code will be
returned.
Let's use the following line of code instead, which have the same effect
but works as expected:
grep -q <<< $(cmd)
Signed-off-by: Tao Liu <ltao@redhat.com>
---
dracut-module-setup.sh | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dracut-module-setup.sh b/dracut-module-setup.sh
index 89d9959..76fb5b2 100755
--- a/dracut-module-setup.sh
+++ b/dracut-module-setup.sh
@@ -56,7 +56,7 @@ depends() {
fi
if is_lvm2_thinp_dump_target; then
- if dracut --list-modules | grep -q lvmthinpool-monitor; then
+ if grep -q lvmthinpool-monitor <<< $(dracut --list-modules); then
add_opt_module lvmthinpool-monitor
else
dwarning "Required lvmthinpool-monitor modules is missing! Please upgrade dracut >= 057."
--
2.40.1

View File

@ -64,6 +64,7 @@ Patch608: 0006-sysconfig-add-pcie_ports-compat-to-KDUMP_COMMANDLINE.patch
Patch609: 0007-fadump-udev-do-not-re-register-fadump-if-kernel-hotp.patch
Patch610: kexec-tools-2.0.28-makedumpfile-0002-PATCH-Fix-failure-of-hugetlb-pages-exclusion-on-Linu.patch
Patch611: kexec-tools-2.0.28-makedumpfile-0003-PATCH-Fix-wrong-exclusion-of-Slab-pages-on-Linux-6.1.patch
Patch612: 0001-Use-grep-q-cmd-instead-of-cmd-grep-q.patch
%description
kexec-tools provides /sbin/kexec binary that facilitates a new
@ -158,6 +159,7 @@ tar -z -x -v -f %{SOURCE19}
%patch 609 -p1 -d kdump-utils-%{kdump_utils_ver}
%patch 610 -p1
%patch 611 -p1
%patch 612 -p1 -d kdump-utils-%{kdump_utils_ver}
%ifarch ppc
%define archdef ARCH=ppc