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