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 value, heading spaces or tailing
comments.
And this uses "sed group command" and "sed hold buffer", make it much
faster than previous `grep <config> | 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:
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 and 2 will fail if there are multiple spaces between config name
and config value, like:
"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 tailing comments:
"path /var/crash # some comment here"
And all will fail if there are heading space,
" path /var/crash"
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 since 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 <kasong(a)redhat.com>
---
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 ff9f7fee..53abdc75 100755
--- a/dracut-module-setup.sh
+++ b/dracut-module-setup.sh
@@ -938,7 +938,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
@@ -999,7 +999,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 6ed4f5a9..aad17be3 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 <option_name>
-# 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 5d9420da..db3a3015 100755
--- a/kdumpctl
+++ b/kdumpctl
@@ -961,7 +961,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
--
2.31.1