kdump-lib-initramfs.sh: prepare to be a POSIX compatible lib

Move all functions needed in the second kernel from kdump-lib.sh
to kdump-lib-initramfs.sh, and update shebang headers.

Now, kdump-lib-initramfs.sh is an independent lib script, no longer
depend on kdump-lib.sh, and kdump-lib.sh is no longer needed for
the 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 the most complex helper and codes.

kdump-lib-initramfs.sh will contain the minimum set of helpers,
and be shared by both the first and second kernel.

Signed-off-by: Kairui Song <kasong@redhat.com>
Acked-by: Philipp Rudo <prudo@redhat.com>
This commit is contained in:
Kairui Song 2021-09-14 03:25:46 +08:00
parent 0e4b66b1ab
commit a5faa052d4
4 changed files with 135 additions and 132 deletions

View File

@ -18,7 +18,7 @@ binary_next_line = false
space_redirects = true space_redirects = true
# Some scripts will only run with bash # Some scripts will only run with bash
[{mkfadumprd,mkdumprd,kdumpctl}] [{mkfadumprd,mkdumprd,kdumpctl,kdump-lib.sh}]
shell_variant = bash shell_variant = bash
# Use dracut code style for *-module-setup.sh # Use dracut code style for *-module-setup.sh

View File

@ -1034,7 +1034,6 @@ install() {
inst "/usr/bin/printf" "/sbin/printf" inst "/usr/bin/printf" "/sbin/printf"
inst "/usr/bin/logger" "/sbin/logger" inst "/usr/bin/logger" "/sbin/logger"
inst "/usr/bin/chmod" "/sbin/chmod" 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-lib-initramfs.sh" "/lib/kdump-lib-initramfs.sh"
inst "/lib/kdump/kdump-logger.sh" "/lib/kdump-logger.sh" inst "/lib/kdump/kdump-logger.sh" "/lib/kdump-logger.sh"
inst "$moddir/kdump.sh" "/usr/bin/kdump.sh" inst "$moddir/kdump.sh" "/usr/bin/kdump.sh"

View File

@ -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 . /lib/kdump-logger.sh
DEFAULT_PATH="/var/crash/"
KDUMP_PATH="/var/crash" KDUMP_PATH="/var/crash"
KDUMP_LOG_FILE="/run/initramfs/kexec-dmesg.log" KDUMP_LOG_FILE="/run/initramfs/kexec-dmesg.log"
CORE_COLLECTOR="" CORE_COLLECTOR=""
@ -20,6 +23,7 @@ KDUMP_PRE=""
KDUMP_POST="" KDUMP_POST=""
NEWROOT="/sysroot" NEWROOT="/sysroot"
OPALCORE="/sys/firmware/opal/mpipl/core" OPALCORE="/sys/firmware/opal/mpipl/core"
KDUMP_CONFIG_FILE="/etc/kdump.conf"
#initiate the kdump logger #initiate the kdump logger
dlog_init dlog_init
@ -28,6 +32,131 @@ if [ $? -ne 0 ]; then
exit 1 exit 1
fi fi
# 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
}
# 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
}
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() get_kdump_confs()
{ {
local config_opt config_val local config_opt config_val

View File

@ -1,13 +1,13 @@
#!/bin/sh #!/bin/bash
# #
# Kdump common variables and functions # 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_CONFIG_FILE="/etc/sysconfig/fence_kdump"
FENCE_KDUMP_SEND="/usr/libexec/fence_kdump_send" FENCE_KDUMP_SEND="/usr/libexec/fence_kdump_send"
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled" FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled"
KDUMP_CONFIG_FILE="/etc/kdump.conf"
is_fadump_capable() is_fadump_capable()
{ {
@ -35,64 +35,6 @@ perror_exit() {
exit 1 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 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
}
# 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 # Check if fence kdump is configured in Pacemaker cluster
is_pcs_fence_kdump() is_pcs_fence_kdump()
{ {
@ -142,20 +84,6 @@ get_user_configured_dump_disk()
[ -b "$_target" ] && echo $_target [ -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() get_block_dump_target()
{ {
local _target _path local _target _path
@ -261,46 +189,10 @@ get_bind_mount_source()
echo $_mnt$_fsroot$_path 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_mntopt_from_target()
{ {
get_mount_info OPTIONS source $1 -f 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 # Get the path where the target will be mounted in kdump kernel
# $1: kdump target device # $1: kdump target device
@ -345,11 +237,6 @@ is_atomic()
grep -q "ostree" /proc/cmdline grep -q "ostree" /proc/cmdline
} }
is_ipv6_address()
{
echo $1 | grep -q ":"
}
# get ip address or hostname from nfs/ssh config value # get ip address or hostname from nfs/ssh config value
get_remote_host() get_remote_host()
{ {
@ -562,18 +449,6 @@ is_mount_in_dracut_args()
[[ " $(kdump_get_conf_val dracut_args)" =~ .*[[:space:]]--mount[=[:space:]].* ]] [[ " $(kdump_get_conf_val dracut_args)" =~ .*[[:space:]]--mount[=[:space:]].* ]]
} }
# 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() check_crash_mem_reserved()
{ {
local mem_reserved local mem_reserved