4a6108
#!/bin/sh
4a6108
#
4a6108
# The code in this file will be used in initramfs environment, bash may
4a6108
# not be the default shell. Any code added must be POSIX compliant.
db7d74
4a6108
DEFAULT_PATH="/var/crash/"
4a6108
KDUMP_CONFIG_FILE="/etc/kdump.conf"
455616
FENCE_KDUMP_CONFIG_FILE="/etc/sysconfig/fence_kdump"
455616
FENCE_KDUMP_SEND="/usr/libexec/fence_kdump_send"
4335b5
LVM_CONF="/etc/lvm/lvm.conf"
db7d74
4a6108
# Read kdump config in well formated style
4a6108
kdump_read_conf()
db7d74
{
4a6108
	# Following steps are applied in order: strip trailing comment, strip trailing space,
4a6108
	# strip heading space, match non-empty line, remove duplicated spaces between conf name and value
4a6108
	[ -f "$KDUMP_CONFIG_FILE" ] && sed -n -e "s/#.*//;s/\s*$//;s/^\s*//;s/\(\S\+\)\s*\(.*\)/\1 \2/p" $KDUMP_CONFIG_FILE
db7d74
}
db7d74
4a6108
# Retrieves config value defined in kdump.conf
4a6108
# $1: config name, sed regexp compatible
4a6108
kdump_get_conf_val()
db7d74
{
4a6108
	# For lines matching "^\s*$1\s+", remove matched part (config name including space),
4a6108
	# remove tailing comment, space, then store in hold space. Print out the hold buffer on last line.
4a6108
	[ -f "$KDUMP_CONFIG_FILE" ] &&
4a6108
		sed -n -e "/^\s*\($1\)\s\+/{s/^\s*\($1\)\s\+//;s/#.*//;s/\s*$//;h};\${x;p}" $KDUMP_CONFIG_FILE
db7d74
}
db7d74
4a6108
is_mounted()
db7d74
{
4a6108
	findmnt -k -n "$1" > /dev/null 2>&1
db7d74
}
db7d74
4a6108
# $1: info type
4a6108
# $2: mount source type
4a6108
# $3: mount source
4a6108
# $4: extra args
4a6108
get_mount_info()
4a6108
{
4a6108
	__kdump_mnt=$(findmnt -k -n -r -o "$1" "--$2" "$3" $4)
db7d74
4a6108
	[ -z "$__kdump_mnt" ] && [ -e "/etc/fstab" ] && __kdump_mnt=$(findmnt -s -n -r -o "$1" "--$2" "$3" $4)
db7d74
4a6108
	echo "$__kdump_mnt"
db7d74
}
db7d74
4a6108
is_ipv6_address()
4a6108
{
4a6108
	echo "$1" | grep -q ":"
4a6108
}
db7d74
4a6108
is_fs_type_nfs()
4a6108
{
4a6108
	[ "$1" = "nfs" ] || [ "$1" = "nfs4" ]
4a6108
}
db7d74
4335b5
is_fs_type_virtiofs()
4335b5
{
4335b5
	[ "$1" = "virtiofs" ]
4335b5
}
4335b5
4a6108
# If $1 contains dracut_args "--mount", return <filesystem type>
4a6108
get_dracut_args_fstype()
4a6108
{
4a6108
	echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f3
db7d74
}
db7d74
4a6108
# If $1 contains dracut_args "--mount", return <device>
4a6108
get_dracut_args_target()
db7d74
{
4a6108
	echo $1 | grep "\-\-mount" | sed "s/.*--mount .\(.*\)/\1/" | cut -d' ' -f1
4a6108
}
db7d74
4a6108
get_save_path()
4a6108
{
4a6108
	__kdump_path=$(kdump_get_conf_val path)
4a6108
	[ -z "$__kdump_path" ] && __kdump_path=$DEFAULT_PATH
db7d74
4a6108
	# strip the duplicated "/"
4a6108
	echo "$__kdump_path" | tr -s /
4a6108
}
db7d74
4a6108
get_root_fs_device()
4a6108
{
4a6108
	findmnt -k -f -n -o SOURCE /
4a6108
}
db7d74
4a6108
# Return the current underlying device of a path, ignore bind mounts
4a6108
get_target_from_path()
4a6108
{
4a6108
	__kdump_target=$(df "$1" 2> /dev/null | tail -1 | awk '{print $1}')
4a6108
	[ "$__kdump_target" = "/dev/root" ] && [ ! -e /dev/root ] && __kdump_target=$(get_root_fs_device)
4a6108
	echo "$__kdump_target"
4a6108
}
db7d74
4a6108
get_fs_type_from_target()
4a6108
{
4a6108
	get_mount_info FSTYPE source "$1" -f
db7d74
}
db7d74
4a6108
get_mntpoint_from_target()
db7d74
{
4a6108
	# --source is applied to ensure non-bind mount is returned
4a6108
	get_mount_info TARGET source "$1" -f
4a6108
}
db7d74
4a6108
is_ssh_dump_target()
4a6108
{
4a6108
	kdump_get_conf_val ssh | grep -q @
4a6108
}
db7d74
4a6108
is_raw_dump_target()
4a6108
{
4a6108
	[ -n "$(kdump_get_conf_val raw)" ]
4a6108
}
db7d74
4335b5
is_virtiofs_dump_target()
4335b5
{
4335b5
	if [ -n "$(kdump_get_conf_val virtiofs)" ]; then
4335b5
		return 0
4335b5
	fi
4335b5
4335b5
	if is_fs_type_virtiofs "$(get_dracut_args_fstype "$(kdump_get_conf_val dracut_args)")"; then
4335b5
		return 0
4335b5
	fi
4335b5
4335b5
	if is_fs_type_virtiofs "$(get_fs_type_from_target "$(get_target_from_path "$(get_save_path)")")"; then
4335b5
		return 0
4335b5
	fi
4335b5
4335b5
	return 1
4335b5
}
4335b5
4a6108
is_nfs_dump_target()
4a6108
{
4a6108
	if [ -n "$(kdump_get_conf_val nfs)" ]; then
4a6108
		return 0
4a6108
	fi
db7d74
4a6108
	if is_fs_type_nfs "$(get_dracut_args_fstype "$(kdump_get_conf_val dracut_args)")"; then
4a6108
		return 0
4a6108
	fi
db7d74
4a6108
	if is_fs_type_nfs "$(get_fs_type_from_target "$(get_target_from_path "$(get_save_path)")")"; then
4a6108
		return 0
4a6108
	fi
db7d74
4a6108
	return 1
db7d74
}
db7d74
4a6108
is_fs_dump_target()
db7d74
{
4335b5
	[ -n "$(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|virtiofs")" ]
4335b5
}
4335b5
4335b5
is_lvm2_thinp_device()
4335b5
{
4335b5
	_device_path=$1
4335b5
	_lvm2_thin_device=$(lvm lvs -S 'lv_layout=sparse && lv_layout=thin' \
4335b5
		--nosuffix --noheadings -o vg_name,lv_name "$_device_path" 2> /dev/null)
4335b5
4335b5
	[ -n "$_lvm2_thin_device" ]
4335b5
}
4335b5
4335b5
kdump_get_ip_route()
4335b5
{
4335b5
	if ! _route=$(/sbin/ip -o route get to "$1" 2>&1;; then
4335b5
		derror "Bad kdump network destination: $1"
4335b5
		exit 1
4335b5
	fi
4335b5
	echo "$_route"
4335b5
}
4335b5
4335b5
kdump_get_ip_route_field()
4335b5
{
4335b5
	echo "$1" | sed -n -e "s/^.*\<$2\>\s\+\(\S\+\).*$/\1/p"
db7d74
}