From d2709ce157d1d32d4f93ff10d62a52a35f70e828 Mon Sep 17 00:00:00 2001 From: Lichen Liu Date: Wed, 21 Jan 2026 11:53:46 +0800 Subject: [PATCH] kdump-lib-initramfs: rewrite kdump_get_conf_val Resolves: RHEL-115464 Upstream: kdump-utils Conflict: None commit d81109cf9291250b42434fc51798c93b613c4ee2 Author: Philipp Rudo Date: Thu Nov 6 15:07:42 2025 +0100 kdump-lib-initramfs: rewrite kdump_get_conf_val Previously, kdump_get_conf_val() relied on a "magic" regular expression to parse kdump.conf, making maintenance and extension difficult. This patch rewrites the parsing logic to be more human-readable. The '\|' needs to be changed to a single '|' because after the rewrite we will use another tool to parse the options to find. Furthermore, kdump_read_conf() has been refactored to invoke kdump_get_conf_val(), as their logic is functionally identical. Signed-off-by: Philipp Rudo Signed-off-by: Lichen Liu Signed-off-by: Lichen Liu --- kdump-lib-initramfs.sh | 49 +++++++++++++++++++++++++++++++++++------- kdump-lib.sh | 6 +++--- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/kdump-lib-initramfs.sh b/kdump-lib-initramfs.sh index 27d6cce..1fc0aa2 100755 --- a/kdump-lib-initramfs.sh +++ b/kdump-lib-initramfs.sh @@ -16,19 +16,52 @@ LUKS_KEY_PRFIX="kdump-cryptsetup:vk-" # Read kdump config in well formated style kdump_read_conf() { - # Following steps are applied in order: strip trailing comment, strip trailing space, - # strip heading space, match non-empty line, remove duplicated spaces between conf name and value - [ -f "$KDUMP_CONFIG_FILE" ] && sed -n -e "s/#.*//;s/\s*$//;s/^\s*//;s/\(\S\+\)\s*\(.*\)/\1 \2/p" $KDUMP_CONFIG_FILE + kdump_get_conf_val "" } # Retrieves config value defined in kdump.conf -# $1: config name, sed regexp compatible +# $1: config name, if empty print full config kdump_get_conf_val() { - # For lines matching "^\s*$1\s+", remove matched part (config name including space), - # remove tailing comment, space, then store in hold space. Print out the hold buffer on last line. - [ -f "$KDUMP_CONFIG_FILE" ] && - sed -n -e "/^\s*\($1\)\s\+/{s/^\s*\($1\)\s\+//;s/#.*//;s/\s*$//;h};\${x;p}" $KDUMP_CONFIG_FILE + _to_find="$1" + _found="" + + [ -f "$KDUMP_CONFIG_FILE" ] || return + while read -r _line; do + _line="$(echo "$_line" | tr -s "[:blank:]" " ")" + case "$_line" in + "" | \#*) + continue + ;; + *\#*) + _line="${_line%%\#*}" + _line="${_line% }" + ;; + esac + + _opt=${_line%% *} + _val=${_line#* } + + case "$_val" in + \"*\") + # Remove quotes + _val="${_val#\"}" + _val="${_val%\"}" + ;; + esac + + if [ -z "$_to_find" ]; then + echo "$_opt $_val" + elif echo "$_opt" | grep -q -E "^($_to_find)$"; then + # make sure to only return the last match to mirror the + # old behavior + _found="$_val" + fi + done < "$KDUMP_CONFIG_FILE" + [ -n "$_found" ] && echo "$_found" + + # make sure we return 0 even when a option isn't set + return 0 } is_mounted() diff --git a/kdump-lib.sh b/kdump-lib.sh index a8c5ca5..fabdbe7 100755 --- a/kdump-lib.sh +++ b/kdump-lib.sh @@ -101,7 +101,7 @@ to_dev_name() is_user_configured_dump_target() { - [[ $(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw\|nfs\|ssh\|virtiofs") ]] || is_mount_in_dracut_args + [[ $(kdump_get_conf_val "ext[234]|xfs|btrfs|minix|raw|nfs|ssh|virtiofs") ]] || is_mount_in_dracut_args } get_block_dump_target() @@ -112,7 +112,7 @@ get_block_dump_target() return fi - _target=$(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw\|virtiofs") + _target=$(kdump_get_conf_val "ext[234]|xfs|btrfs|minix|raw|virtiofs") [[ -n $_target ]] && to_dev_name "$_target" && return _target=$(get_dracut_args_target "$(kdump_get_conf_val "dracut_args")") @@ -130,7 +130,7 @@ get_block_dump_target() is_dump_to_rootfs() { - [[ $(kdump_get_conf_val 'failure_action\|default') == dump_to_rootfs ]] + [[ $(kdump_get_conf_val 'failure_action|default') == dump_to_rootfs ]] } is_lvm2_thinp_dump_target()