On Thu, 19 Aug 2021 19:39:30 +0800
Kairui Song <kasong(a)redhat.com> wrote:
Move all function needed in second kernel from kdump-lib.sh
to kdump-lib-initramfs.sh, and update shebang headers.
Now kdump-lib-initramfs.sh is an independent lib scirpt, no longer
s/scirpt/script
depend on kdump-lib.sh, and kdump-lib.sh is no longer needed for
second kernel.
In later commits, functions in kdump-lib-initramfs.sh will be reworked
to be POSIX compatible, kdump-lib.sh will contain bash only functions.
POSIX shell have very limited features, eg. `local` keyword doesn't
exist in POSIX but we rely on that heavily. So kdump-lib.sh will
use bash syntax and contain most complex helper and codes.
kdump-lib-initramfs.sh will contain minimum set of helpers, and
shared by both first and second kernel.
Signed-off-by: Kairui Song <kasong(a)redhat.com>
---
.editorconfig | 2 +-
dracut-module-setup.sh | 1 -
kdump-lib-initramfs.sh | 133 ++++++++++++++++++++++++++++++++++++++++-
kdump-lib.sh | 131 +---------------------------------------
4 files changed, 135 insertions(+), 132 deletions(-)
diff --git a/.editorconfig b/.editorconfig
index 52aadba3..d65868c4 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -18,7 +18,7 @@ binary_next_line = false
space_redirects = true
# Some scirpts will only run with bash
-[{mkfadumprd,mkdumprd,kdumpctl}]
+[{mkfadumprd,mkdumprd,kdumpctl,kdump-lib.sh}]
shell_variant = bash
# Use dracut code style for *-module-setup.sh
diff --git a/dracut-module-setup.sh b/dracut-module-setup.sh
index 11db5aa3..d5a710b9 100755
--- a/dracut-module-setup.sh
+++ b/dracut-module-setup.sh
@@ -1033,7 +1033,6 @@ install() {
inst "/usr/bin/printf" "/sbin/printf"
inst "/usr/bin/logger" "/sbin/logger"
inst "/usr/bin/chmod" "/sbin/chmod"
- inst "/lib/kdump/kdump-lib.sh" "/lib/kdump-lib.sh"
inst "/lib/kdump/kdump-lib-initramfs.sh"
"/lib/kdump-lib-initramfs.sh"
inst "/lib/kdump/kdump-logger.sh" "/lib/kdump-logger.sh"
inst "$moddir/kdump.sh" "/usr/bin/kdump.sh"
diff --git a/kdump-lib-initramfs.sh b/kdump-lib-initramfs.sh
index 50443e55..e7f623c4 100755
--- a/kdump-lib-initramfs.sh
+++ b/kdump-lib-initramfs.sh
@@ -1,8 +1,11 @@
-# These variables and functions are useful in 2nd kernel
+#!/bin/sh
+#
+# Function and variables used in initramfs environment, POSIX compatible
+#
-. /lib/kdump-lib.sh
. /lib/kdump-logger.sh
+DEFAULT_PATH="/var/crash/"
KDUMP_PATH="/var/crash"
KDUMP_LOG_FILE="/run/initramfs/kexec-dmesg.log"
CORE_COLLECTOR=""
@@ -20,6 +23,7 @@ KDUMP_PRE=""
KDUMP_POST=""
NEWROOT="/sysroot"
OPALCORE="/sys/firmware/opal/mpipl/core"
+KDUMP_CONFIG_FILE="/etc/kdump.conf"
#initiate the kdump logger
dlog_init
@@ -28,6 +32,131 @@ if [ $? -ne 0 ]; then
exit 1
fi
+# Read kdump config in well formated style
+kdump_read_conf()
+{
+ # Following steps are applied in order: strip tailing comment, strip tailing 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
+}
+
+# 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
+}
+
+is_mounted()
+{
+ findmnt -k -n $1 &>/dev/null
+}
+
+get_mount_info()
+{
+ local _info_type=$1 _src_type=$2 _src=$3; shift 3
+ local _info=$(findmnt -k -n -r -o $_info_type --$_src_type $_src $@)
+
+ [ -z "$_info" ] && [ -e "/etc/fstab" ] &&
_info=$(findmnt -s -n -r -o $_info_type --$_src_type $_src $@)
+
+ echo $_info
+}
+
+is_ipv6_address()
+{
+ echo $1 | grep -q ":"
+}
+
+is_fs_type_nfs()
+{
+ [ "$1" = "nfs" ] || [ "$1" = "nfs4" ]
+}
+
+# If $1 contains dracut_args "--mount", return <filesystem type>
+get_dracut_args_fstype()
+{
+ echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut
-d' ' -f3
+}
+
+# If $1 contains dracut_args "--mount", return <device>
+get_dracut_args_target()
+{
+ echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut
-d' ' -f1
+}
Are these greps really necessary? Isn't sed able to handle this on its
own?
Thanks
Philipp
+
+get_save_path()
+{
+ local _save_path=$(kdump_get_conf_val path)
+ [ -z "$_save_path" ] && _save_path=$DEFAULT_PATH
+
+ # strip the duplicated "/"
+ echo $_save_path | tr -s /
+}
+
+get_root_fs_device()
+{
+ findmnt -k -f -n -o SOURCE /
+}
+
+# Return the current underlying device of a path, ignore bind mounts
+get_target_from_path()
+{
+ local _target
+
+ _target=$(df $1 2>/dev/null | tail -1 | awk '{print $1}')
+ [[ "$_target" == "/dev/root" ]] && [[ ! -e /dev/root ]]
&& _target=$(get_root_fs_device)
+ echo $_target
+}
+
+get_fs_type_from_target()
+{
+ get_mount_info FSTYPE source $1 -f
+}
+
+get_mntpoint_from_target()
+{
+ # --source is applied to ensure non-bind mount is returned
+ get_mount_info TARGET source $1 -f
+}
+
+is_ssh_dump_target()
+{
+ [[ $(kdump_get_conf_val ssh) == *@* ]]
+}
+
+is_raw_dump_target()
+{
+ [[ $(kdump_get_conf_val raw) ]]
+}
+
+is_nfs_dump_target()
+{
+ if [[ $(kdump_get_conf_val nfs) ]]; then
+ return 0;
+ fi
+
+ if is_fs_type_nfs $(get_dracut_args_fstype "$(kdump_get_conf_val
dracut_args)"); then
+ return 0
+ fi
+
+ local _save_path=$(get_save_path)
+ local _target=$(get_target_from_path $_save_path)
+ local _fstype=$(get_fs_type_from_target $_target)
+
+ if is_fs_type_nfs $_fstype; then
+ return 0
+ fi
+
+ return 1
+}
+
+is_fs_dump_target()
+{
+ [[ $(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix") ]]
+}
+
get_kdump_confs()
{
local config_opt config_val
diff --git a/kdump-lib.sh b/kdump-lib.sh
index 58422d8b..5a1fcf02 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
@@ -1,13 +1,13 @@
-#!/bin/sh
+#!/bin/bash
#
# Kdump common variables and functions
#
-DEFAULT_PATH="/var/crash/"
+. /usr/lib/kdump/kdump-lib-initramfs.sh
+
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()
{
@@ -35,64 +35,6 @@ perror_exit() {
exit 1
}
-is_fs_type_nfs()
-{
- [ "$1" = "nfs" ] || [ "$1" = "nfs4" ]
-}
-
-is_ssh_dump_target()
-{
- [[ $(kdump_get_conf_val ssh) == *@* ]]
-}
-
-is_nfs_dump_target()
-{
- if [[ $(kdump_get_conf_val nfs) ]]; then
- return 0;
- fi
-
- if is_fs_type_nfs $(get_dracut_args_fstype "$(kdump_get_conf_val
dracut_args)"); then
- return 0
- fi
-
- local _save_path=$(get_save_path)
- local _target=$(get_target_from_path $_save_path)
- local _fstype=$(get_fs_type_from_target $_target)
-
- if is_fs_type_nfs $_fstype; then
- return 0
- fi
-
- return 1
-}
-
-is_raw_dump_target()
-{
- [[ $(kdump_get_conf_val raw) ]]
-}
-
-is_fs_dump_target()
-{
- [[ $(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix") ]]
-}
-
-# Read kdump config in well formatted style
-kdump_read_conf()
-{
- # Following steps are applied in order: strip tailing comment, strip tailing 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
-}
-
-# 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()
{
@@ -142,20 +84,6 @@ get_user_configured_dump_disk()
[ -b "$_target" ] && echo $_target
}
-get_root_fs_device()
-{
- findmnt -k -f -n -o SOURCE /
-}
-
-get_save_path()
-{
- local _save_path=$(kdump_get_conf_val path)
- [ -z "$_save_path" ] && _save_path=$DEFAULT_PATH
-
- # strip the duplicated "/"
- echo $_save_path | tr -s /
-}
-
get_block_dump_target()
{
local _target _path
@@ -261,46 +189,10 @@ get_bind_mount_source()
echo $_mnt$_fsroot$_path
}
-# Return the current underlaying device of a path, ignore bind mounts
-get_target_from_path()
-{
- local _target
-
- _target=$(df $1 2>/dev/null | tail -1 | awk '{print $1}')
- [[ "$_target" == "/dev/root" ]] && [[ ! -e /dev/root ]]
&& _target=$(get_root_fs_device)
- echo $_target
-}
-
-is_mounted()
-{
- findmnt -k -n $1 &>/dev/null
-}
-
-get_mount_info()
-{
- local _info_type=$1 _src_type=$2 _src=$3; shift 3
- local _info=$(findmnt -k -n -r -o $_info_type --$_src_type $_src $@)
-
- [ -z "$_info" ] && [ -e "/etc/fstab" ] &&
_info=$(findmnt -s -n -r -o $_info_type --$_src_type $_src $@)
-
- echo $_info
-}
-
-get_fs_type_from_target()
-{
- get_mount_info FSTYPE source $1 -f
-}
-
get_mntopt_from_target()
{
get_mount_info OPTIONS source $1 -f
}
-# Find the general mount point of a dump target, not the bind mount point
-get_mntpoint_from_target()
-{
- # Expcilitly specify --source to findmnt could ensure non-bind mount is returned
- get_mount_info TARGET source $1 -f
-}
# Get the path where the target will be mounted in kdump kernel
# $1: kdump target device
@@ -345,11 +237,6 @@ is_atomic()
grep -q "ostree" /proc/cmdline
}
-is_ipv6_address()
-{
- echo $1 | grep -q ":"
-}
-
# get ip address or hostname from nfs/ssh config value
get_remote_host()
{
@@ -562,18 +449,6 @@ is_mount_in_dracut_args()
[[ $(kdump_get_conf_val dracut_args) == *" --mount "* ]]
}
-# If $1 contains dracut_args "--mount", return <filesystem type>
-get_dracut_args_fstype()
-{
- echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut
-d' ' -f3
-}
-
-# If $1 contains dracut_args "--mount", return <device>
-get_dracut_args_target()
-{
- echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut
-d' ' -f1
-}
-
check_crash_mem_reserved()
{
local mem_reserved