From 67b8dd1cb8a8d57be9415de61b02abe229bd9bcf Mon Sep 17 00:00:00 2001 From: Tao Liu Date: Nov 03 2021 08:08:22 +0000 Subject: kdump-lib.sh: add a config format and read helper upstream: fedora resolves: bz2003832 conflict: none commit a0282ab22c4e61294346c8ab9c42ab87b8bdfbb0 Author: Kairui Song Date: Tue Aug 3 19:49:51 2021 +0800 kdump-lib.sh: add a config format and read helper Add a helper `kdump_read_conf` to replace read_strip_comments. `kdump_read_conf` does a few more things: - remove trailing spaces. - format the content, remove duplicated spaces between name and value. - read from KDUMP_CONFIG_FILE (/etc/kdump.conf) directly, avoid pasting "/etc/kdump.conf" path everywhere in the code. - check if config file exists, just in case. Also unify the environmental variable, now KDUMP_CONFIG_FILE stands for the default config location. This helps avoid some shell pitfalls about spaces when reading config. Signed-off-by: Kairui Song Acked-by: Philipp Rudo Signed-off-by: Tao Liu --- diff --git a/dracut-kdump.sh b/dracut-kdump.sh index 3e65c44..3c165b3 100755 --- a/dracut-kdump.sh +++ b/dracut-kdump.sh @@ -246,10 +246,10 @@ get_host_ip() return 0 } -read_kdump_conf() +read_kdump_confs() { - if [ ! -f "$KDUMP_CONF" ]; then - derror "$KDUMP_CONF not found" + if [ ! -f "$KDUMP_CONFIG_FILE" ]; then + derror "$KDUMP_CONFIG_FILE not found" return fi @@ -278,7 +278,7 @@ read_kdump_conf() add_dump_code "dump_ssh $SSH_KEY_LOCATION $config_val" ;; esac - done <<< "$(read_strip_comments $KDUMP_CONF)" + done <<< "$(kdump_read_conf)" } fence_kdump_notify() @@ -288,7 +288,7 @@ fence_kdump_notify() fi } -read_kdump_conf +read_kdump_confs fence_kdump_notify get_host_ip diff --git a/dracut-module-setup.sh b/dracut-module-setup.sh index c6d6fee..c00392b 100755 --- a/dracut-module-setup.sh +++ b/dracut-module-setup.sh @@ -679,7 +679,8 @@ default_dump_target_install_conf() #install kdump.conf and what user specifies in kdump.conf kdump_install_conf() { local _opt _val _pdev - (read_strip_comments /etc/kdump.conf) > ${initdir}/tmp/$$-kdump.conf + + kdump_read_conf > "${initdir}/tmp/$$-kdump.conf" while read _opt _val; do @@ -708,7 +709,7 @@ kdump_install_conf() { dracut_install "${_val%%[[:blank:]]*}" ;; esac - done <<< "$(read_strip_comments /etc/kdump.conf)" + done <<< "$(kdump_read_conf)" kdump_install_pre_post_conf diff --git a/kdump-lib-initramfs.sh b/kdump-lib-initramfs.sh index 319f9a0..50443e5 100755 --- a/kdump-lib-initramfs.sh +++ b/kdump-lib-initramfs.sh @@ -16,7 +16,6 @@ SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa" KDUMP_SCRIPT_DIR="/kdumpscripts" DD_BLKSIZE=512 FINAL_ACTION="systemctl reboot -f" -KDUMP_CONF="/etc/kdump.conf" KDUMP_PRE="" KDUMP_POST="" NEWROOT="/sysroot" @@ -93,7 +92,7 @@ get_kdump_confs() esac ;; esac - done <<< "$(read_strip_comments $KDUMP_CONF)" + done <<< "$(kdump_read_conf)" if [ -z "$CORE_COLLECTOR" ]; then CORE_COLLECTOR="$DEFAULT_CORE_COLLECTOR" diff --git a/kdump-lib.sh b/kdump-lib.sh index 861f6e4..ce6a38d 100755 --- a/kdump-lib.sh +++ b/kdump-lib.sh @@ -7,6 +7,7 @@ DEFAULT_PATH="/var/crash/" FENCE_KDUMP_CONFIG_FILE="/etc/sysconfig/fence_kdump" FENCE_KDUMP_SEND="/usr/libexec/fence_kdump_send" FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled" +KDUMP_CONFIG_FILE="/etc/kdump.conf" is_fadump_capable() { @@ -80,12 +81,12 @@ strip_comments() echo $@ | sed -e 's/\(.*\)#.*/\1/' } -# Read from kdump config file stripping all comments -read_strip_comments() +# Read kdump config in well formatted style +kdump_read_conf() { - # strip heading spaces, and print any content starting with - # neither space or #, and strip everything after # - sed -n -e "s/^\s*\([^# \t][^#]\+\).*/\1/gp" $1 + # 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 } # Check if fence kdump is configured in Pacemaker cluster diff --git a/kdumpctl b/kdumpctl index e132d04..8f70503 100755 --- a/kdumpctl +++ b/kdumpctl @@ -5,7 +5,6 @@ KDUMP_KERNELVER="" KDUMP_KERNEL="" KDUMP_COMMANDLINE="" KEXEC_ARGS="" -KDUMP_CONFIG_FILE="/etc/kdump.conf" KDUMP_LOG_PATH="/var/log" MKDUMPRD="/sbin/mkdumprd -f" MKFADUMPRD="/sbin/mkfadumprd" @@ -272,7 +271,7 @@ check_config() return 1 fi _opt_rec[$config_opt]="$config_val" - done <<< "$(read_strip_comments $KDUMP_CONFIG_FILE)" + done <<< "$(kdump_read_conf)" check_failure_action_config || return 1 check_final_action_config || return 1 @@ -699,7 +698,7 @@ check_ssh_config() *) ;; esac - done <<< "$(read_strip_comments $KDUMP_CONFIG_FILE)" + done <<< "$(kdump_read_conf)" #make sure they've configured kdump.conf for ssh dumps local SSH_TARGET=`echo -n $DUMP_TARGET | sed -n '/.*@/p'` @@ -1330,7 +1329,7 @@ reset_crashkernel() { grubby --args "$crashkernel_default" --update-kernel "$entry" $zipl_arg [[ $zipl_arg ]] && zipl > /dev/null fi - } +} if [ ! -f "$KDUMP_CONFIG_FILE" ]; then derror "Error: No kdump config file found!" diff --git a/mkdumprd b/mkdumprd index ead2d13..f856773 100644 --- a/mkdumprd +++ b/mkdumprd @@ -423,7 +423,7 @@ do *) ;; esac -done <<< "$(read_strip_comments $conf_file)" +done <<< "$(kdump_read_conf)" handle_default_dump_target