From 14205c1d6f3fa74972be44e4437642289e46bf76 Mon Sep 17 00:00:00 2001 From: Tao Liu Date: Wed, 3 Nov 2021 15:19:50 +0800 Subject: [PATCH] kdump-lib.sh: add a config value retrive helper upstream: fedora resolves: bz2003832 conflict: none commit 09ccf88405793220af640c5317cbadb71cf03d36 Author: Kairui Song Date: Mon Aug 16 23:25:14 2021 +0800 kdump-lib.sh: add a config value retrive helper Add a helper kdump_get_conf_val to replace get_option_value. It can help cover more corner cases in the code, like when there are multiple spaces in config file, config value separated by a tab, heading spaces, or trailing comments. And this uses "sed group command" and "sed hold buffer", make it much faster than previous `grep | tail -1`. This helper is supposed to provide a universal way for kexec-tools scripts to read in config value. Currently, different scripts are reading the config in many different fragile ways. For example, following codes are found in kexec-tools script code base: 1. grep ^force_rebuild $KDUMP_CONFIG_FILE echo $_force_rebuild | cut -d' ' -f2 2. grep ^kdump_post $KDUMP_CONFIG_FILE | cut -d\ -f2 3. awk '/^sshkey/ {print $2}' $conf_file 4. grep ^path $KDUMP_CONFIG_FILE | cut -d' ' -f2- 1, 2, and 4 will fail if the space is replaced by, e.g. a tab 1 and 2 might fail if there are multiple spaces between config name and config value: "kdump_post /var/crash/scripts/kdump-post.sh" A space will be read instead of config value. 1, 2, 3 will fail if there are space in file path, like: "kdump_post /var/crash/scripts dir/kdump-post.sh" 4 will fail if there are trailing comments: "path /var/crash # some comment here" And all will fail if there are heading space, " path /var/crash" And all will most likely cause problems if the config file contains the same option more than once. And all of them are slower than the new sed call. Old get_option_value is also very slow and doesn't handle heading space. Although we never claim to support heading space or tailing comments before, it's harmless to be more robust on config reading, and many conf files in /etc support heading spaces. And have a faster and safer config reading helper makes it easier to clean up the code. Signed-off-by: Kairui Song Acked-by: Philipp Rudo Signed-off-by: Tao Liu --- dracut-module-setup.sh | 4 ++-- kdump-lib.sh | 20 +++++++++----------- kdumpctl | 2 +- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/dracut-module-setup.sh b/dracut-module-setup.sh index c00392b..0779224 100755 --- a/dracut-module-setup.sh +++ b/dracut-module-setup.sh @@ -935,7 +935,7 @@ get_generic_fence_kdump_nodes() { local filtered local nodes - nodes=$(get_option_value "fence_kdump_nodes") + nodes=$(kdump_get_conf_val "fence_kdump_nodes") for node in ${nodes}; do # Skip its own node name if is_localhost $node; then @@ -996,7 +996,7 @@ kdump_install_random_seed() { } kdump_install_systemd_conf() { - local failure_action=$(get_option_value "failure_action") + local failure_action=$(kdump_get_conf_val "failure_action") # Kdump turns out to require longer default systemd mount timeout # than 1st kernel(90s by default), we use default 300s for kdump. diff --git a/kdump-lib.sh b/kdump-lib.sh index ce6a38d..8244fd0 100755 --- a/kdump-lib.sh +++ b/kdump-lib.sh @@ -76,11 +76,6 @@ is_fs_dump_target() egrep -q "^ext[234]|^xfs|^btrfs|^minix" /etc/kdump.conf } -strip_comments() -{ - echo $@ | sed -e 's/\(.*\)#.*/\1/' -} - # Read kdump config in well formatted style kdump_read_conf() { @@ -89,6 +84,15 @@ kdump_read_conf() [ -f "$KDUMP_CONFIG_FILE" ] && sed -n -e "s/#.*//;s/\s*$//;s/^\s*//;s/\(\S\+\)\s*\(.*\)/\1 \2/p" $KDUMP_CONFIG_FILE } +# Retrieves config value defined in kdump.conf +# $1: config name, sed regexp compatible +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 +} + # Check if fence kdump is configured in Pacemaker cluster is_pcs_fence_kdump() { @@ -322,12 +326,6 @@ get_kdump_mntpoint_from_target() echo $_mntpoint | tr -s "/" } -# get_option_value -# retrieves value of option defined in kdump.conf -get_option_value() { - strip_comments `grep "^$1[[:space:]]\+" /etc/kdump.conf | tail -1 | cut -d\ -f2-` -} - kdump_get_persistent_dev() { local dev="${1//\"/}" diff --git a/kdumpctl b/kdumpctl index 8f70503..ef937bd 100755 --- a/kdumpctl +++ b/kdumpctl @@ -929,7 +929,7 @@ check_fence_kdump_config() { local hostname=`hostname` local ipaddrs=`hostname -I` - local nodes=$(get_option_value "fence_kdump_nodes") + local nodes=$(kdump_get_conf_val "fence_kdump_nodes") for node in $nodes; do if [ "$node" = "$hostname" ]; then