35519c
#!/bin/bash
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Kdump common variables and functions
Petr Šabata f5bf49
#
Petr Šabata f5bf49
35519c
. /usr/lib/kdump/kdump-lib-initramfs.sh
35519c
Petr Šabata f5bf49
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled"
Petr Šabata f5bf49
Petr Šabata f5bf49
is_fadump_capable()
Petr Šabata f5bf49
{
d53e06
	# Check if firmware-assisted dump is enabled
d53e06
	# if no, fallback to kdump check
d53e06
	if [[ -f $FADUMP_ENABLED_SYS_NODE ]]; then
d53e06
		rc=$(< $FADUMP_ENABLED_SYS_NODE)
d53e06
		[[ $rc -eq 1 ]] && return 0
d53e06
	fi
d53e06
	return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
d53e06
is_squash_available()
d53e06
{
d53e06
	for kmodule in squashfs overlay loop; do
d53e06
		if [[ -z $KDUMP_KERNELVER ]]; then
d53e06
			modprobe --dry-run $kmodule &> /dev/null || return 1
d53e06
		else
d53e06
			modprobe -S "$KDUMP_KERNELVER" --dry-run $kmodule &> /dev/null || return 1
d53e06
		fi
d53e06
	done
Kairui Song 96a3fc
}
Kairui Song 96a3fc
a501dc
is_zstd_command_available()
a501dc
{
a501dc
	[[ -x "$(command -v zstd)" ]]
a501dc
}
a501dc
b5a9e5
dracut_have_option()
b5a9e5
{
b5a9e5
	local _option=$1
b5a9e5
	! dracut "$_option" 2>&1 | grep -q "unrecognized option"
b5a9e5
}
b5a9e5
d53e06
perror_exit()
d53e06
{
d53e06
	derror "$@"
d53e06
	exit 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Check if fence kdump is configured in Pacemaker cluster
Petr Šabata f5bf49
is_pcs_fence_kdump()
Petr Šabata f5bf49
{
d53e06
	# no pcs or fence_kdump_send executables installed?
d53e06
	type -P pcs > /dev/null || return 1
d53e06
	[[ -x $FENCE_KDUMP_SEND ]] || return 1
Petr Šabata f5bf49
d53e06
	# fence kdump not configured?
d53e06
	(pcs cluster cib | grep 'type="fence_kdump"') &> /dev/null || return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Check if fence_kdump is configured using kdump options
Petr Šabata f5bf49
is_generic_fence_kdump()
Petr Šabata f5bf49
{
d53e06
	[[ -x $FENCE_KDUMP_SEND ]] || return 1
Petr Šabata f5bf49
d53e06
	[[ $(kdump_get_conf_val fence_kdump_nodes) ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
d53e06
to_dev_name()
d53e06
{
d53e06
	local dev="${1//\"/}"
Petr Šabata f5bf49
d53e06
	case "$dev" in
d53e06
	UUID=*)
d53e06
		blkid -U "${dev#UUID=}"
d53e06
		;;
d53e06
	LABEL=*)
d53e06
		blkid -L "${dev#LABEL=}"
d53e06
		;;
d53e06
	*)
d53e06
		echo "$dev"
d53e06
		;;
d53e06
	esac
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_user_configured_dump_target()
Petr Šabata f5bf49
{
dcaec9
	[[ $(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw\|nfs\|ssh\|virtiofs") ]] || is_mount_in_dracut_args
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_block_dump_target()
Petr Šabata f5bf49
{
dcaec9
	local _target _fstype
Petr Šabata f5bf49
d53e06
	if is_ssh_dump_target || is_nfs_dump_target; then
d53e06
		return
d53e06
	fi
Petr Šabata f5bf49
dcaec9
	_target=$(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw\|virtiofs")
d53e06
	[[ -n $_target ]] && to_dev_name "$_target" && return
Petr Šabata f5bf49
dcaec9
	_target=$(get_dracut_args_target "$(kdump_get_conf_val "dracut_args")")
dcaec9
	[[ -b $_target ]] && to_dev_name "$_target" && return
dcaec9
dcaec9
	_fstype=$(get_dracut_args_fstype "$(kdump_get_conf_val "dracut_args")")
dcaec9
	is_fs_type_virtiofs "$_fstype" && echo "$_target" && return
dcaec9
dcaec9
	_target=$(get_target_from_path "$(get_save_path)")
dcaec9
	[[ -b $_target ]] && to_dev_name "$_target" && return
dcaec9
dcaec9
	_fstype=$(get_fs_type_from_target "$_target")
dcaec9
	is_fs_type_virtiofs "$_fstype" && echo "$_target" && return
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_dump_to_rootfs()
Petr Šabata f5bf49
{
90223d
	[[ $(kdump_get_conf_val 'failure_action\|default') == dump_to_rootfs ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
94988e
is_lvm2_thinp_dump_target()
94988e
{
94988e
	_target=$(get_block_dump_target)
94988e
	[ -n "$_target" ] && is_lvm2_thinp_device "$_target"
94988e
}
94988e
Petr Šabata f5bf49
get_failure_action_target()
Petr Šabata f5bf49
{
d53e06
	local _target
Petr Šabata f5bf49
d53e06
	if is_dump_to_rootfs; then
d53e06
		# Get rootfs device name
d53e06
		_target=$(get_root_fs_device)
d53e06
		[[ -b $_target ]] && to_dev_name "$_target" && return
dcaec9
		is_fs_type_virtiofs "$(get_fs_type_from_target "$_target")" && echo "$_target" && return
d53e06
		# Then, must be nfs root
d53e06
		echo "nfs"
d53e06
	fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Get kdump targets(including root in case of dump_to_rootfs).
Petr Šabata f5bf49
get_kdump_targets()
Petr Šabata f5bf49
{
d53e06
	local _target _root
d53e06
	local kdump_targets
Petr Šabata f5bf49
d53e06
	_target=$(get_block_dump_target)
d53e06
	if [[ -n $_target ]]; then
d53e06
		kdump_targets=$_target
d53e06
	elif is_ssh_dump_target; then
d53e06
		kdump_targets="ssh"
d53e06
	else
d53e06
		kdump_targets="nfs"
d53e06
	fi
Petr Šabata f5bf49
d53e06
	# Add the root device if dump_to_rootfs is specified.
d53e06
	_root=$(get_failure_action_target)
d53e06
	if [[ -n $_root ]] && [[ $kdump_targets != "$_root" ]]; then
d53e06
		kdump_targets="$kdump_targets $_root"
d53e06
	fi
Petr Šabata f5bf49
d53e06
	echo "$kdump_targets"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Return the bind mount source path, return the path itself if it's not bind mounted
Petr Šabata f5bf49
# Eg. if /path/to/src is bind mounted to /mnt/bind, then:
Petr Šabata f5bf49
# /mnt/bind -> /path/to/src, /mnt/bind/dump -> /path/to/src/dump
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# findmnt uses the option "-v, --nofsroot" to exclusive the [/dir]
DistroBaker a10140
# in the SOURCE column for bind-mounts, then if $_src equals to
DistroBaker a10140
# $_src_nofsroot, the mountpoint is not bind mounted directory.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Below is just an example for mount info
Petr Šabata f5bf49
# /dev/mapper/atomicos-root[/ostree/deploy/rhel-atomic-host/var], if the
Petr Šabata f5bf49
# directory is bind mounted. The former part represents the device path, rest
Petr Šabata f5bf49
# part is the bind mounted directory which quotes by bracket "[]".
Petr Šabata f5bf49
get_bind_mount_source()
Petr Šabata f5bf49
{
d53e06
	local _mnt _path _src _opt _fstype
d53e06
	local _fsroot _src_nofsroot
Petr Šabata f5bf49
d53e06
	_mnt=$(df "$1" | tail -1 | awk '{print $NF}')
d53e06
	_path=${1#$_mnt}
040a2e
d53e06
	_src=$(get_mount_info SOURCE target "$_mnt" -f)
d53e06
	_opt=$(get_mount_info OPTIONS target "$_mnt" -f)
d53e06
	_fstype=$(get_mount_info FSTYPE target "$_mnt" -f)
DistroBaker a10140
d53e06
	# bind mount in fstab
d53e06
	if [[ -d $_src ]] && [[ $_fstype == none ]] && (echo "$_opt" | grep -q "\bbind\b"); then
d53e06
		echo "$_src$_path" && return
d53e06
	fi
DistroBaker a10140
d53e06
	# direct mount
d53e06
	_src_nofsroot=$(get_mount_info SOURCE target "$_mnt" -v -f)
d53e06
	if [[ $_src_nofsroot == "$_src" ]]; then
d53e06
		echo "$_mnt$_path" && return
d53e06
	fi
Petr Šabata f5bf49
d53e06
	_fsroot=${_src#${_src_nofsroot}[}
d53e06
	_fsroot=${_fsroot%]}
d53e06
	_mnt=$(get_mount_info TARGET source "$_src_nofsroot" -f)
Petr Šabata f5bf49
d53e06
	# for btrfs, _fsroot will also contain the subvol value as well, strip it
d53e06
	if [[ $_fstype == btrfs ]]; then
d53e06
		local _subvol
d53e06
		_subvol=${_opt#*subvol=}
d53e06
		_subvol=${_subvol%,*}
d53e06
		_fsroot=${_fsroot#$_subvol}
d53e06
	fi
d53e06
	echo "$_mnt$_fsroot$_path"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_mntopt_from_target()
Petr Šabata f5bf49
{
d53e06
	get_mount_info OPTIONS source "$1" -f
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Get the path where the target will be mounted in kdump kernel
Petr Šabata f5bf49
# $1: kdump target device
Petr Šabata f5bf49
get_kdump_mntpoint_from_target()
Petr Šabata f5bf49
{
d53e06
	local _mntpoint
d53e06
d53e06
	_mntpoint=$(get_mntpoint_from_target "$1")
d53e06
	# mount under /sysroot if dump to root disk or mount under
d53e06
	# mount under /kdumproot if dump target is not mounted in first kernel
d53e06
	# mount under /kdumproot/$_mntpoint in other cases in 2nd kernel.
d53e06
	# systemd will be in charge to umount it.
d53e06
	if [[ -z $_mntpoint ]]; then
d53e06
		_mntpoint="/kdumproot"
d53e06
	else
d53e06
		if [[ $_mntpoint == "/" ]]; then
d53e06
			_mntpoint="/sysroot"
d53e06
		else
d53e06
			_mntpoint="/kdumproot/$_mntpoint"
d53e06
		fi
d53e06
	fi
d53e06
d53e06
	# strip duplicated "/"
d53e06
	echo $_mntpoint | tr -s "/"
d53e06
}
d53e06
d53e06
kdump_get_persistent_dev()
d53e06
{
d53e06
	local dev="${1//\"/}"
d53e06
d53e06
	case "$dev" in
d53e06
	UUID=*)
d53e06
		dev=$(blkid -U "${dev#UUID=}")
d53e06
		;;
d53e06
	LABEL=*)
d53e06
		dev=$(blkid -L "${dev#LABEL=}")
d53e06
		;;
d53e06
	esac
d53e06
	echo $(get_persistent_dev "$dev")
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Kairui Song 80c191
is_atomic()
Kairui Song 80c191
{
d53e06
	grep -q "ostree" /proc/cmdline
Kairui Song 80c191
}
Kairui Song 80c191
Petr Šabata f5bf49
# get ip address or hostname from nfs/ssh config value
Petr Šabata f5bf49
get_remote_host()
Petr Šabata f5bf49
{
d53e06
	local _config_val=$1
Petr Šabata f5bf49
d53e06
	# ipv6 address in kdump.conf is around with "[]",
d53e06
	# factor out the ipv6 address
d53e06
	_config_val=${_config_val#*@}
d53e06
	_config_val=${_config_val%:/*}
d53e06
	_config_val=${_config_val#[}
d53e06
	_config_val=${_config_val%]}
d53e06
	echo "$_config_val"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_hostname()
Petr Šabata f5bf49
{
d53e06
	local _hostname
Petr Šabata f5bf49
d53e06
	_hostname=$(echo "$1" | grep ":")
d53e06
	if [[ -n $_hostname ]]; then
d53e06
		return 1
d53e06
	fi
d53e06
	echo "$1" | grep -q "[a-zA-Z]"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Copied from "/etc/sysconfig/network-scripts/network-functions"
Petr Šabata f5bf49
get_hwaddr()
Petr Šabata f5bf49
{
d53e06
	if [[ -f "/sys/class/net/$1/address" ]]; then
d53e06
		awk '{ print toupper($0) }' < "/sys/class/net/$1/address"
d53e06
	elif [[ -d "/sys/class/net/$1" ]]; then
d53e06
		LC_ALL="" LANG="" ip -o link show "$1" 2> /dev/null |
d53e06
			awk '{ print toupper(gensub(/.*link\/[^ ]* ([[:alnum:]:]*).*/,
Petr Šabata f5bf49
                                        "\\1", 1)); }'
d53e06
	fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Coiby Xu d09dc2
# Get value by a field using "nmcli -g"
c9f583
# Usage: get_nmcli_value_by_field <field> <nmcli command>
Coiby Xu d09dc2
#
Coiby Xu d09dc2
# "nmcli --get-values" allows us to retrive value(s) by field, for example,
Coiby Xu d09dc2
# nmcli --get-values <field> connection show /org/freedesktop/NetworkManager/ActiveConnection/1
Coiby Xu d09dc2
# returns the following value for the corresponding field respectively,
Coiby Xu d09dc2
#   Field                                  Value
Coiby Xu d09dc2
#   IP4.DNS                                "10.19.42.41 | 10.11.5.19 | 10.5.30.160"
Coiby Xu d09dc2
#   802-3-ethernet.s390-subchannels        ""
Coiby Xu d09dc2
#   bond.options                           "mode=balance-rr"
Coiby Xu d09dc2
get_nmcli_value_by_field()
Coiby Xu d09dc2
{
d53e06
	LANG=C nmcli --get-values "$@"
c9f583
}
Coiby Xu d09dc2
c9f583
# Get nmcli field value of an connection apath (a D-Bus active connection path)
c9f583
# Usage: get_nmcli_field_by_apath <field> <apath>
c9f583
get_nmcli_field_by_conpath()
c9f583
{
d53e06
	local _field=$1 _apath=$2
Coiby Xu d09dc2
d53e06
	get_nmcli_value_by_field "$_field" connection show "$_apath"
Coiby Xu d09dc2
}
Coiby Xu d09dc2
Coiby Xu 2f9fc8
# Get nmcli connection apath (a D-Bus active connection path ) by ifname
Coiby Xu 2f9fc8
#
Coiby Xu 2f9fc8
# apath is used for nmcli connection operations, e.g.
Coiby Xu 2f9fc8
#  $ nmcli connection show $apath
Coiby Xu 2f9fc8
get_nmcli_connection_apath_by_ifname()
Coiby Xu 2f9fc8
{
d53e06
	local _ifname=$1
Coiby Xu b15a09
d53e06
	get_nmcli_value_by_field "GENERAL.CON-PATH" device show "$_ifname"
Coiby Xu b15a09
}
Coiby Xu b15a09
Petr Šabata f5bf49
get_ifcfg_by_device()
Petr Šabata f5bf49
{
d53e06
	grep -E -i -l "^[[:space:]]*DEVICE=\"*${1}\"*[[:space:]]*$" \
d53e06
		/etc/sysconfig/network-scripts/ifcfg-* 2> /dev/null | head -1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_ifcfg_by_hwaddr()
Petr Šabata f5bf49
{
d53e06
	grep -E -i -l "^[[:space:]]*HWADDR=\"*${1}\"*[[:space:]]*$" \
d53e06
		/etc/sysconfig/network-scripts/ifcfg-* 2> /dev/null | head -1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_ifcfg_by_uuid()
Petr Šabata f5bf49
{
d53e06
	grep -E -i -l "^[[:space:]]*UUID=\"*${1}\"*[[:space:]]*$" \
d53e06
		/etc/sysconfig/network-scripts/ifcfg-* 2> /dev/null | head -1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_ifcfg_by_name()
Petr Šabata f5bf49
{
d53e06
	grep -E -i -l "^[[:space:]]*NAME=\"*${1}\"*[[:space:]]*$" \
d53e06
		/etc/sysconfig/network-scripts/ifcfg-* 2> /dev/null | head -1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_nm_running()
Petr Šabata f5bf49
{
d53e06
	[[ "$(LANG=C nmcli -t --fields running general status 2> /dev/null)" == "running" ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_nm_handling()
Petr Šabata f5bf49
{
d53e06
	LANG=C nmcli -t --fields device,state dev status 2> /dev/null |
d53e06
		grep -q "^\(${1}:connected\)\|\(${1}:connecting.*\)$"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# $1: netdev name
Petr Šabata f5bf49
get_ifcfg_nmcli()
Petr Šabata f5bf49
{
d53e06
	local nm_uuid nm_name
d53e06
	local ifcfg_file
Petr Šabata f5bf49
d53e06
	# Get the active nmcli config name of $1
d53e06
	if is_nm_running && is_nm_handling "${1}"; then
d53e06
		# The configuration "uuid" and "name" generated by nm is wrote to
d53e06
		# the ifcfg file as "UUID=<nm_uuid>" and "NAME=<nm_name>".
d53e06
		nm_uuid=$(LANG=C nmcli -t --fields uuid,device c show --active 2> /dev/null |
d53e06
			grep "${1}" | head -1 | cut -d':' -f1)
d53e06
		nm_name=$(LANG=C nmcli -t --fields name,device c show --active 2> /dev/null |
d53e06
			grep "${1}" | head -1 | cut -d':' -f1)
d53e06
		ifcfg_file=$(get_ifcfg_by_uuid "${nm_uuid}")
d53e06
		[[ -z ${ifcfg_file} ]] && ifcfg_file=$(get_ifcfg_by_name "${nm_name}")
d53e06
	fi
Petr Šabata f5bf49
d53e06
	echo -n "${ifcfg_file}"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# $1: netdev name
Petr Šabata f5bf49
get_ifcfg_legacy()
Petr Šabata f5bf49
{
d53e06
	local ifcfg_file hwaddr
Petr Šabata f5bf49
d53e06
	ifcfg_file="/etc/sysconfig/network-scripts/ifcfg-${1}"
d53e06
	[[ -f ${ifcfg_file} ]] && echo -n "${ifcfg_file}" && return
Petr Šabata f5bf49
d53e06
	ifcfg_file=$(get_ifcfg_by_name "${1}")
d53e06
	[[ -f ${ifcfg_file} ]] && echo -n "${ifcfg_file}" && return
Petr Šabata f5bf49
d53e06
	hwaddr=$(get_hwaddr "${1}")
d53e06
	if [[ -n $hwaddr ]]; then
d53e06
		ifcfg_file=$(get_ifcfg_by_hwaddr "${hwaddr}")
d53e06
		[[ -f ${ifcfg_file} ]] && echo -n "${ifcfg_file}" && return
d53e06
	fi
Petr Šabata f5bf49
d53e06
	ifcfg_file=$(get_ifcfg_by_device "${1}")
Petr Šabata f5bf49
d53e06
	echo -n "${ifcfg_file}"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# $1: netdev name
Petr Šabata f5bf49
# Return the ifcfg file whole name(including the path) of $1 if any.
d53e06
get_ifcfg_filename()
d53e06
{
d53e06
	local ifcfg_file
Petr Šabata f5bf49
d53e06
	ifcfg_file=$(get_ifcfg_nmcli "${1}")
d53e06
	if [[ -z ${ifcfg_file} ]]; then
d53e06
		ifcfg_file=$(get_ifcfg_legacy "${1}")
d53e06
	fi
Petr Šabata f5bf49
d53e06
	echo -n "${ifcfg_file}"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
DistroBaker a10140
# returns 0 when omission of a module is desired in dracut_args
Petr Šabata f5bf49
# returns 1 otherwise
d53e06
is_dracut_mod_omitted()
d53e06
{
d53e06
	local dracut_args dracut_mod=$1
DistroBaker a10140
d53e06
	set -- $(kdump_get_conf_val dracut_args)
d53e06
	while [ $# -gt 0 ]; do
d53e06
		case $1 in
d53e06
		-o | --omit)
d53e06
			[[ " ${2//[^[:alnum:]]/ } " == *" $dracut_mod "* ]] && return 0
d53e06
			;;
d53e06
		esac
d53e06
		shift
d53e06
	done
Petr Šabata f5bf49
d53e06
	return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
d53e06
is_wdt_active()
d53e06
{
d53e06
	local active
DistroBaker 17a515
d53e06
	[[ -d /sys/class/watchdog ]] || return 1
d53e06
	for dir in /sys/class/watchdog/*; do
d53e06
		[[ -f "$dir/state" ]] || continue
d53e06
		active=$(< "$dir/state")
d53e06
		[[ $active == "active" ]] && return 0
d53e06
	done
d53e06
	return 1
DistroBaker 17a515
}
DistroBaker 17a515
96dc81
have_compression_in_dracut_args()
96dc81
{
b5a9e5
	[[ "$(kdump_get_conf_val dracut_args)" =~ (^|[[:space:]])--(gzip|bzip2|lzma|xz|lzo|lz4|zstd|no-compress|compress|squash-compressor)([[:space:]]|$) ]]
96dc81
}
96dc81
Petr Šabata f5bf49
# If "dracut_args" contains "--mount" information, use it
Petr Šabata f5bf49
# directly without any check(users are expected to ensure
Petr Šabata f5bf49
# its correctness).
Petr Šabata f5bf49
is_mount_in_dracut_args()
Petr Šabata f5bf49
{
d53e06
	[[ " $(kdump_get_conf_val dracut_args)" =~ .*[[:space:]]--mount[=[:space:]].* ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
check_crash_mem_reserved()
Petr Šabata f5bf49
{
d53e06
	local mem_reserved
Petr Šabata f5bf49
d53e06
	mem_reserved=$(< /sys/kernel/kexec_crash_size)
d53e06
	if [[ $mem_reserved -eq 0 ]]; then
d53e06
		derror "No memory reserved for crash kernel"
d53e06
		return 1
d53e06
	fi
Petr Šabata f5bf49
d53e06
	return 0
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
check_kdump_feasibility()
Petr Šabata f5bf49
{
d53e06
	if [[ ! -e /sys/kernel/kexec_crash_loaded ]]; then
d53e06
		derror "Kdump is not supported on this kernel"
d53e06
		return 1
d53e06
	fi
d53e06
	check_crash_mem_reserved
d53e06
	return $?
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
check_current_kdump_status()
Petr Šabata f5bf49
{
d53e06
	if [[ ! -f /sys/kernel/kexec_crash_loaded ]]; then
d53e06
		derror "Perhaps CONFIG_CRASH_DUMP is not enabled in kernel"
d53e06
		return 1
d53e06
	fi
Petr Šabata f5bf49
d53e06
	rc=$(< /sys/kernel/kexec_crash_loaded)
d53e06
	if [[ $rc == 1 ]]; then
d53e06
		return 0
d53e06
	else
d53e06
		return 1
d53e06
	fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# remove_cmdline_param <kernel cmdline> <param1> [<param2>] ... [<paramN>]
Petr Šabata f5bf49
# Remove a list of kernel parameters from a given kernel cmdline and print the result.
Petr Šabata f5bf49
# For each "arg" in the removing params list, "arg" and "arg=xxx" will be removed if exists.
Petr Šabata f5bf49
remove_cmdline_param()
Petr Šabata f5bf49
{
d53e06
	local cmdline=$1
d53e06
	shift
Petr Šabata f5bf49
d53e06
	for arg in "$@"; do
d53e06
		cmdline=$(echo "$cmdline" |
d53e06
			sed -e "s/\b$arg=[^ ]*//g" \
d53e06
				-e "s/^$arg\b//g" \
d53e06
				-e "s/[[:space:]]$arg\b//g" \
d53e06
				-e "s/\s\+/ /g")
d53e06
	done
d53e06
	echo "$cmdline"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# This function returns the "apicid" of the boot
Petr Šabata f5bf49
# cpu (cpu 0) if present.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
get_bootcpu_apicid()
Petr Šabata f5bf49
{
d53e06
	awk '                                                       \
Petr Šabata f5bf49
        BEGIN { CPU = "-1"; }                                   \
Petr Šabata f5bf49
        $1=="processor" && $2==":"      { CPU = $NF; }          \
Petr Šabata f5bf49
        CPU=="0" && /^apicid/           { print $NF; }          \
d53e06
        ' \
d53e06
		/proc/cpuinfo
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# append_cmdline <kernel cmdline> <parameter name> <parameter value>
Petr Šabata f5bf49
# This function appends argument "$2=$3" to string ($1) if not already present.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
append_cmdline()
Petr Šabata f5bf49
{
d53e06
	local cmdline=$1
d53e06
	local newstr=${cmdline/$2/""}
Petr Šabata f5bf49
d53e06
	# unchanged str implies argument wasn't there
d53e06
	if [[ $cmdline == "$newstr" ]]; then
d53e06
		cmdline="${cmdline} ${2}=${3}"
d53e06
	fi
Petr Šabata f5bf49
d53e06
	echo "$cmdline"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# This function check iomem and determines if we have more than
Petr Šabata f5bf49
# 4GB of ram available. Returns 1 if we do, 0 if we dont
Petr Šabata f5bf49
need_64bit_headers()
Petr Šabata f5bf49
{
d53e06
	return "$(tail -n 1 /proc/iomem | awk '{ split ($1, r, "-");
60e5a1
        print (strtonum("0x" r[2]) > strtonum("0xffffffff")); }')"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
# Check if secure boot is being enforced.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Per Peter Jones, we need check efivar SecureBoot-$(the UUID) and
Petr Šabata f5bf49
# SetupMode-$(the UUID), they are both 5 bytes binary data. The first four
Petr Šabata f5bf49
# bytes are the attributes associated with the variable and can safely be
Petr Šabata f5bf49
# ignored, the last bytes are one-byte true-or-false variables. If SecureBoot
Petr Šabata f5bf49
# is 1 and SetupMode is 0, then secure boot is being enforced.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Assume efivars is mounted at /sys/firmware/efi/efivars.
Petr Šabata f5bf49
is_secure_boot_enforced()
Petr Šabata f5bf49
{
d53e06
	local secure_boot_file setup_mode_file
d53e06
	local secure_boot_byte setup_mode_byte
Petr Šabata f5bf49
Coiby Xu eb95f9
	# On powerpc, secure boot is enforced if:
Coiby Xu eb95f9
	#   host secure boot: /ibm,secure-boot/os-secureboot-enforcing DT property exists
Coiby Xu eb95f9
	#   guest secure boot: /ibm,secure-boot >= 2
d53e06
	if [[ -f /proc/device-tree/ibm,secureboot/os-secureboot-enforcing ]]; then
Petr Šabata f5bf49
		return 0
d53e06
	fi
Coiby Xu eb95f9
	if [[ -f /proc/device-tree/ibm,secure-boot ]] &&
Coiby Xu eb95f9
		[[ $(lsprop /proc/device-tree/ibm,secure-boot | tail -1) -ge 2 ]]; then
Coiby Xu eb95f9
			return 0
Coiby Xu eb95f9
	fi
Petr Šabata f5bf49
d53e06
	# Detect secure boot on x86 and arm64
d53e06
	secure_boot_file=$(find /sys/firmware/efi/efivars -name "SecureBoot-*" 2> /dev/null)
d53e06
	setup_mode_file=$(find /sys/firmware/efi/efivars -name "SetupMode-*" 2> /dev/null)
Petr Šabata f5bf49
d53e06
	if [[ -f $secure_boot_file ]] && [[ -f $setup_mode_file ]]; then
d53e06
		secure_boot_byte=$(hexdump -v -e '/1 "%d\ "' "$secure_boot_file" | cut -d' ' -f 5)
d53e06
		setup_mode_byte=$(hexdump -v -e '/1 "%d\ "' "$setup_mode_file" | cut -d' ' -f 5)
Petr Šabata f5bf49
d53e06
		if [[ $secure_boot_byte == "1" ]] && [[ $setup_mode_byte == "0" ]]; then
d53e06
			return 0
d53e06
		fi
d53e06
	fi
Petr Šabata f5bf49
d53e06
	# Detect secure boot on s390x
d53e06
	if [[ -e "/sys/firmware/ipl/secure" && "$(< /sys/firmware/ipl/secure)" == "1" ]]; then
d53e06
		return 0
d53e06
	fi
DistroBaker 5cac7c
d53e06
	return 1
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# prepare_kexec_args <kexec args>
Petr Šabata f5bf49
# This function prepares kexec argument.
Petr Šabata f5bf49
#
Petr Šabata f5bf49
prepare_kexec_args()
Petr Šabata f5bf49
{
d53e06
	local kexec_args=$1
d53e06
	local found_elf_args
d53e06
d53e06
	ARCH=$(uname -m)
d53e06
	if [[ $ARCH == "i686" ]] || [[ $ARCH == "i386" ]]; then
d53e06
		need_64bit_headers
d53e06
		if [[ $? == 1 ]]; then
d53e06
			found_elf_args=$(echo "$kexec_args" | grep elf32-core-headers)
d53e06
			if [[ -n $found_elf_args ]]; then
d53e06
				dwarn "Warning: elf32-core-headers overrides correct elf64 setting"
d53e06
			else
d53e06
				kexec_args="$kexec_args --elf64-core-headers"
d53e06
			fi
d53e06
		else
d53e06
			found_elf_args=$(echo "$kexec_args" | grep elf64-core-headers)
d53e06
			if [[ -z $found_elf_args ]]; then
d53e06
				kexec_args="$kexec_args --elf32-core-headers"
d53e06
			fi
d53e06
		fi
d53e06
	fi
d53e06
	echo "$kexec_args"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Lichen Liu 987eda
# prepare_kdump_kernel <kdump_kernelver>
Lichen Liu 987eda
# This function return kdump_kernel given a kernel version.
Lichen Liu 987eda
prepare_kdump_kernel()
Petr Šabata f5bf49
{
Lichen Liu 987eda
	local kdump_kernelver=$1
Lichen Liu 987eda
	local dir img boot_dirlist boot_imglist kdump_kernel machine_id
d53e06
	read -r machine_id < /etc/machine-id
Lichen Liu 987eda
d53e06
	boot_dirlist=${KDUMP_BOOTDIR:-"/boot /boot/efi /efi /"}
Lichen Liu 987eda
	boot_imglist="$KDUMP_IMG-$kdump_kernelver$KDUMP_IMG_EXT $machine_id/$kdump_kernelver/$KDUMP_IMG"
d53e06
d53e06
	# Use BOOT_IMAGE as reference if possible, strip the GRUB root device prefix in (hd0,gpt1) format
d53e06
	boot_img="$(sed "s/^BOOT_IMAGE=\((\S*)\)\?\(\S*\) .*/\2/" /proc/cmdline)"
Lichen Liu 987eda
	if [[ "$boot_img" == *"$kdump_kernelver" ]]; then
d53e06
		boot_imglist="$boot_img $boot_imglist"
d53e06
	fi
d53e06
d53e06
	for dir in $boot_dirlist; do
d53e06
		for img in $boot_imglist; do
d53e06
			if [[ -f "$dir/$img" ]]; then
Lichen Liu 987eda
				kdump_kernel=$(echo "$dir/$img" | tr -s '/')
d53e06
				break 2
d53e06
			fi
d53e06
		done
d53e06
	done
Lichen Liu 987eda
	echo "$kdump_kernel"
Lichen Liu 987eda
}
Lichen Liu 987eda
Lichen Liu 987eda
#
Lichen Liu 987eda
# Detect initrd and kernel location, results are stored in global environmental variables:
Lichen Liu 987eda
# KDUMP_BOOTDIR, KDUMP_KERNELVER, KDUMP_KERNEL, DEFAULT_INITRD, and KDUMP_INITRD
Lichen Liu 987eda
#
Lichen Liu 987eda
# Expectes KDUMP_BOOTDIR, KDUMP_IMG, KDUMP_IMG_EXT, KDUMP_KERNELVER to be loaded from config already
Lichen Liu 987eda
# and will prefer already set values so user can specify custom kernel/initramfs location
Lichen Liu 987eda
#
Lichen Liu 987eda
prepare_kdump_bootinfo()
Lichen Liu 987eda
{
Lichen Liu 987eda
	local boot_initrdlist nondebug_kernelver debug_kernelver
Lichen Liu 987eda
	local default_initrd_base var_target_initrd_dir
Lichen Liu 987eda
Lichen Liu 987eda
	if [[ -z $KDUMP_KERNELVER ]]; then
Lichen Liu 987eda
		KDUMP_KERNELVER=$(uname -r)
Lichen Liu 6c26a3
Lichen Liu 6c26a3
		# Fadump uses the regular bootloader, unlike kdump. So, use the same version
Lichen Liu 6c26a3
		# for default kernel and capture kernel unless specified explicitly with
Lichen Liu 6c26a3
		# KDUMP_KERNELVER option.
Lichen Liu 6c26a3
		if ! is_fadump_capable; then
Lichen Liu 6c26a3
			nondebug_kernelver=$(sed -n -e 's/\(.*\)+debug$/\1/p' <<< "$KDUMP_KERNELVER")
Lichen Liu 6c26a3
		fi
Lichen Liu 987eda
	fi
Lichen Liu 987eda
Lichen Liu 987eda
	# Use nondebug kernel if possible, because debug kernel will consume more memory and may oom.
Lichen Liu 987eda
	if [[ -n $nondebug_kernelver ]]; then
Lichen Liu 987eda
		dinfo "Trying to use $nondebug_kernelver."
Lichen Liu 987eda
		debug_kernelver=$KDUMP_KERNELVER
Lichen Liu 987eda
		KDUMP_KERNELVER=$nondebug_kernelver
Lichen Liu 987eda
	fi
Lichen Liu 987eda
Lichen Liu 987eda
	KDUMP_KERNEL=$(prepare_kdump_kernel "$KDUMP_KERNELVER")
Lichen Liu 987eda
Lichen Liu 987eda
	if ! [[ -e $KDUMP_KERNEL ]]; then
Lichen Liu 987eda
		if [[ -n $debug_kernelver ]]; then
Lichen Liu 987eda
			dinfo "Fallback to using debug kernel"
Lichen Liu 987eda
			KDUMP_KERNELVER=$debug_kernelver
Lichen Liu 987eda
			KDUMP_KERNEL=$(prepare_kdump_kernel "$KDUMP_KERNELVER")
Lichen Liu 987eda
		fi
Lichen Liu 987eda
	fi
d53e06
d53e06
	if ! [[ -e $KDUMP_KERNEL ]]; then
d53e06
		derror "Failed to detect kdump kernel location"
d53e06
		return 1
d53e06
	fi
d53e06
Lichen Liu 987eda
	if [[ "$KDUMP_KERNEL" == *"+debug" ]]; then
Lichen Liu 987eda
		dwarn "Using debug kernel, you may need to set a larger crashkernel than the default value."
Lichen Liu 987eda
	fi
Lichen Liu 987eda
d53e06
	# Set KDUMP_BOOTDIR to where kernel image is stored
d53e06
	KDUMP_BOOTDIR=$(dirname "$KDUMP_KERNEL")
d53e06
d53e06
	# Default initrd should just stay aside of kernel image, try to find it in KDUMP_BOOTDIR
d53e06
	boot_initrdlist="initramfs-$KDUMP_KERNELVER.img initrd"
d53e06
	for initrd in $boot_initrdlist; do
d53e06
		if [[ -f "$KDUMP_BOOTDIR/$initrd" ]]; then
Lichen Liu 112d3e
			default_initrd_base="$initrd"
Lichen Liu 112d3e
			DEFAULT_INITRD="$KDUMP_BOOTDIR/$default_initrd_base"
d53e06
			break
d53e06
		fi
d53e06
	done
d53e06
d53e06
	# Create kdump initrd basename from default initrd basename
d53e06
	# initramfs-5.7.9-200.fc32.x86_64.img => initramfs-5.7.9-200.fc32.x86_64kdump.img
d53e06
	# initrd => initrdkdump
Lichen Liu 112d3e
	if [[ -z $default_initrd_base ]]; then
d53e06
		kdump_initrd_base=initramfs-${KDUMP_KERNELVER}kdump.img
Lichen Liu 112d3e
	elif [[ $default_initrd_base == *.* ]]; then
Lichen Liu 112d3e
		kdump_initrd_base=${default_initrd_base%.*}kdump.${DEFAULT_INITRD##*.}
d53e06
	else
Lichen Liu 112d3e
		kdump_initrd_base=${default_initrd_base}kdump
d53e06
	fi
d53e06
d53e06
	# Place kdump initrd in $(/var/lib/kdump) if $(KDUMP_BOOTDIR) not writable
d53e06
	if [[ ! -w $KDUMP_BOOTDIR ]]; then
d53e06
		var_target_initrd_dir="/var/lib/kdump"
d53e06
		mkdir -p "$var_target_initrd_dir"
d53e06
		KDUMP_INITRD="$var_target_initrd_dir/$kdump_initrd_base"
d53e06
	else
d53e06
		KDUMP_INITRD="$KDUMP_BOOTDIR/$kdump_initrd_base"
d53e06
	fi
Petr Šabata f5bf49
}
Petr Šabata f5bf49
DistroBaker a10140
get_watchdog_drvs()
DistroBaker a10140
{
d53e06
	local _wdtdrvs _drv _dir
DistroBaker a10140
d53e06
	for _dir in /sys/class/watchdog/*; do
d53e06
		# device/modalias will return driver of this device
d53e06
		[[ -f "$_dir/device/modalias" ]] || continue
d53e06
		_drv=$(< "$_dir/device/modalias")
d53e06
		_drv=$(modprobe --set-version "$KDUMP_KERNELVER" -R "$_drv" 2> /dev/null)
d53e06
		for i in $_drv; do
d53e06
			if ! [[ " $_wdtdrvs " == *" $i "* ]]; then
d53e06
				_wdtdrvs="$_wdtdrvs $i"
d53e06
			fi
d53e06
		done
d53e06
	done
DistroBaker a10140
d53e06
	echo "$_wdtdrvs"
DistroBaker a10140
}
DistroBaker a10140
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# prepare_cmdline <commandline> <commandline remove> <commandline append>
Petr Šabata f5bf49
# This function performs a series of edits on the command line.
Petr Šabata f5bf49
# Store the final result in global $KDUMP_COMMANDLINE.
Petr Šabata f5bf49
prepare_cmdline()
Petr Šabata f5bf49
{
d53e06
	local cmdline id arg
d53e06
d53e06
	if [[ -z $1 ]]; then
d53e06
		cmdline=$(< /proc/cmdline)
d53e06
	else
d53e06
		cmdline="$1"
d53e06
	fi
d53e06
d53e06
	# These params should always be removed
d53e06
	cmdline=$(remove_cmdline_param "$cmdline" crashkernel panic_on_warn)
d53e06
	# These params can be removed configurably
d53e06
	while read -r arg; do
d53e06
		cmdline=$(remove_cmdline_param "$cmdline" "$arg")
d53e06
	done <<< "$(echo "$2" | xargs -n 1 echo)"
d53e06
d53e06
	# Always remove "root=X", as we now explicitly generate all kinds
d53e06
	# of dump target mount information including root fs.
d53e06
	#
d53e06
	# We do this before KDUMP_COMMANDLINE_APPEND, if one really cares
d53e06
	# about it(e.g. for debug purpose), then can pass "root=X" using
d53e06
	# KDUMP_COMMANDLINE_APPEND.
d53e06
	cmdline=$(remove_cmdline_param "$cmdline" root)
d53e06
d53e06
	# With the help of "--hostonly-cmdline", we can avoid some interitage.
d53e06
	cmdline=$(remove_cmdline_param "$cmdline" rd.lvm.lv rd.luks.uuid rd.dm.uuid rd.md.uuid fcoe)
d53e06
d53e06
	# Remove netroot, rd.iscsi.initiator and iscsi_initiator since
d53e06
	# we get duplicate entries for the same in case iscsi code adds
d53e06
	# it as well.
d53e06
	cmdline=$(remove_cmdline_param "$cmdline" netroot rd.iscsi.initiator iscsi_initiator)
d53e06
d53e06
	cmdline="${cmdline} $3"
d53e06
d53e06
	id=$(get_bootcpu_apicid)
d53e06
	if [[ -n ${id} ]]; then
d53e06
		cmdline=$(append_cmdline "$cmdline" disable_cpu_apicid "$id")
d53e06
	fi
d53e06
d53e06
	# If any watchdog is used, set it's pretimeout to 0. pretimeout let
d53e06
	# watchdog panic the kernel first, and reset the system after the
d53e06
	# panic. If the system is already in kdump, panic is not helpful
d53e06
	# and only increase the chance of watchdog failure.
d53e06
	for i in $(get_watchdog_drvs); do
d53e06
		cmdline+=" $i.pretimeout=0"
d53e06
d53e06
		if [[ $i == hpwdt ]]; then
d53e06
			# hpwdt have a special parameter kdumptimeout, is's only suppose
d53e06
			# to be set to non-zero in first kernel. In kdump, non-zero
d53e06
			# value could prevent the watchdog from resetting the system.
d53e06
			cmdline+=" $i.kdumptimeout=0"
d53e06
		fi
d53e06
	done
d53e06
d53e06
	echo "$cmdline"
Petr Šabata f5bf49
}
Pingfan Liu 540d33
Coiby Xu 643999
PROC_IOMEM=/proc/iomem
Coiby Xu 643999
#get system memory size i.e. memblock.memory.total_size in the unit of GB
Pingfan Liu 540d33
get_system_size()
Pingfan Liu 540d33
{
Coiby Xu 643999
	sum=$(sed -n "s/\s*\([0-9a-fA-F]\+\)-\([0-9a-fA-F]\+\) : System RAM$/+ 0x\2 - 0x\1 + 1/p" $PROC_IOMEM)
Coiby Xu 643999
	echo $(( (sum) / 1024 / 1024 / 1024))
Pingfan Liu 540d33
}
Pingfan Liu 540d33
Coiby Xu 63deb7
# Return the recommended size for the reserved crashkernel memory
Coiby Xu 63deb7
# depending on the system memory size.
Coiby Xu 63deb7
#
Coiby Xu 63deb7
# This functions is expected to be consistent with the parse_crashkernel_mem()
Coiby Xu 63deb7
# in kernel i.e. how kernel allocates the kdump memory given the crashkernel
Coiby Xu 63deb7
# parameter crashkernel=range1:size1[,range2:size2,…] and the system memory
Coiby Xu 63deb7
# size.
Pingfan Liu 540d33
get_recommend_size()
Pingfan Liu 540d33
{
d53e06
	local mem_size=$1
d53e06
	local _ck_cmdline=$2
Coiby Xu 63deb7
	local range start start_unit end end_unit size
d53e06
Coiby Xu 63deb7
	while read -r -d , range; do
Coiby Xu 63deb7
		# need to use non-default IFS as double spaces are used as a
Coiby Xu 63deb7
		# single delimiter while commas aren't...
Coiby Xu 63deb7
		IFS=, read start start_unit end end_unit size <<< \
Coiby Xu 63deb7
			"$(echo "$range" | sed -n "s/\([0-9]\+\)\([GT]\?\)-\([0-9]*\)\([GT]\?\):\([0-9]\+[MG]\)/\1,\2,\3,\4,\5/p")"
Coiby Xu 63deb7
Coiby Xu 63deb7
		# aka. 102400T
Coiby Xu 63deb7
		end=${end:-104857600}
Coiby Xu 63deb7
		[[ "$end_unit" == T ]] && end=$((end * 1024))
Coiby Xu 63deb7
		[[ "$start_unit" == T ]] && start=$((start * 1024))
Coiby Xu 63deb7
Coiby Xu 63deb7
		if [[ $mem_size -ge $start ]] && [[ $mem_size -lt $end ]]; then
Coiby Xu 63deb7
			echo "$size"
d53e06
			return
d53e06
		fi
Coiby Xu 63deb7
Coiby Xu 63deb7
		# append a ',' as read expects the 'file' to end with a delimiter
Coiby Xu 63deb7
	done <<< "$_ck_cmdline,"
Coiby Xu 63deb7
Coiby Xu 63deb7
	# no matching range found
Coiby Xu 63deb7
	echo "0M"
Pingfan Liu 540d33
}
Pingfan Liu 540d33
Coiby Xu 1ab968
# get default crashkernel
Coiby Xu 1ab968
# $1 dump mode, if not specified, dump_mode will be judged by is_fadump_capable
Coiby Xu 1ab968
kdump_get_arch_recommend_crashkernel()
Coiby Xu 1ab968
{
Coiby Xu 1ab968
	local _arch _ck_cmdline _dump_mode
Coiby Xu 1ab968
Coiby Xu 1ab968
	if [[ -z "$1" ]]; then
Coiby Xu 1ab968
		if is_fadump_capable; then
Coiby Xu 1ab968
			_dump_mode=fadump
Coiby Xu 1ab968
		else
Coiby Xu 1ab968
			_dump_mode=kdump
Coiby Xu 1ab968
		fi
Coiby Xu 1ab968
	else
Coiby Xu 1ab968
		_dump_mode=$1
Coiby Xu 1ab968
	fi
Coiby Xu 1ab968
Coiby Xu 1ab968
	_arch=$(uname -m)
Coiby Xu 1ab968
Coiby Xu 1ab968
	if [[ $_arch == "x86_64" ]] || [[ $_arch == "s390x" ]]; then
Coiby Xu 1ab968
		_ck_cmdline="1G-4G:192M,4G-64G:256M,64G-:512M"
Coiby Xu 1ab968
	elif [[ $_arch == "aarch64" ]]; then
Pingfan Liu a31811
		# For 4KB page size, the formula is based on x86 plus extra = 64M
Pingfan Liu a31811
		_ck_cmdline="1G-4G:256M,4G-64G:320M,64G-:576M"
Coiby Xu 1ab968
	elif [[ $_arch == "ppc64le" ]]; then
Coiby Xu 1ab968
		if [[ $_dump_mode == "fadump" ]]; then
Coiby Xu 1ab968
			_ck_cmdline="4G-16G:768M,16G-64G:1G,64G-128G:2G,128G-1T:4G,1T-2T:6G,2T-4T:12G,4T-8T:20G,8T-16T:36G,16T-32T:64G,32T-64T:128G,64T-:180G"
Coiby Xu 1ab968
		else
Coiby Xu 1ab968
			_ck_cmdline="2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G"
Coiby Xu 1ab968
		fi
Coiby Xu 1ab968
	fi
Coiby Xu 1ab968
Coiby Xu 1ab968
	echo -n "$_ck_cmdline"
Coiby Xu 1ab968
}
Coiby Xu 1ab968
Pingfan Liu 540d33
# return recommended size based on current system RAM size
60e5a1
# $1: kernel version, if not set, will defaults to $(uname -r)
Pingfan Liu 540d33
kdump_get_arch_recommend_size()
Pingfan Liu 540d33
{
Coiby Xu 80a37c
	local _ck_cmdline _sys_mem
Kairui Song abb0c3
d53e06
	if ! [[ -r "/proc/iomem" ]]; then
d53e06
		echo "Error, can not access /proc/iomem."
d53e06
		return 1
d53e06
	fi
Coiby Xu 80a37c
	_sys_mem=$(get_system_size)
Coiby Xu 1ab968
	_ck_cmdline=$(kdump_get_arch_recommend_crashkernel)
Coiby Xu 4e511c
	_ck_cmdline=${_ck_cmdline//-:/-102400T:}
Coiby Xu 80a37c
	get_recommend_size "$_sys_mem" "$_ck_cmdline"
Pingfan Liu 540d33
}
Kairui Song ccdd4f
Kairui Song ccdd4f
# Print all underlying crypt devices of a block device
Kairui Song ccdd4f
# print nothing if device is not on top of a crypt device
Kairui Song ccdd4f
# $1: the block device to be checked in maj:min format
Kairui Song ccdd4f
get_luks_crypt_dev()
Kairui Song ccdd4f
{
d53e06
	local _type
040a2e
d53e06
	[[ -b /dev/block/$1 ]] || return 1
Kairui Song ccdd4f
c931d1
	_type=$(blkid -u filesystem,crypto -o export -- "/dev/block/$1" | \
c931d1
		sed -n -E "s/^TYPE=(.*)$/\1/p")
d53e06
	[[ $_type == "crypto_LUKS" ]] && echo "$1"
Kairui Song ccdd4f
d53e06
	for _x in "/sys/dev/block/$1/slaves/"*; do
d53e06
		[[ -f $_x/dev ]] || continue
d53e06
		[[ $_x/subsystem -ef /sys/class/block ]] || continue
d53e06
		get_luks_crypt_dev "$(< "$_x/dev")"
d53e06
	done
Kairui Song ccdd4f
}
Kairui Song 8387b5
Kairui Song 8387b5
# kdump_get_maj_min <device>
Kairui Song 8387b5
# Prints the major and minor of a device node.
Kairui Song 8387b5
# Example:
Kairui Song 8387b5
# $ get_maj_min /dev/sda2
Kairui Song 8387b5
# 8:2
d53e06
kdump_get_maj_min()
d53e06
{
d53e06
	local _majmin
d53e06
	_majmin="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
d53e06
	printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
Kairui Song 8387b5
}
Kairui Song 8387b5
Kairui Song 8387b5
get_all_kdump_crypt_dev()
Kairui Song 8387b5
{
d53e06
	local _dev
Kairui Song 8387b5
d53e06
	for _dev in $(get_block_dump_target); do
d53e06
		get_luks_crypt_dev "$(kdump_get_maj_min "$_dev")"
d53e06
	done
Kairui Song 8387b5
}
Kairui Song a9fda5
Kairui Song a9fda5
check_vmlinux()
Kairui Song a9fda5
{
d53e06
	# Use readelf to check if it's a valid ELF
d53e06
	readelf -h "$1" &> /dev/null || return 1
Kairui Song a9fda5
}
Kairui Song a9fda5
Kairui Song a9fda5
get_vmlinux_size()
Kairui Song a9fda5
{
d53e06
	local size=0 _msize
Kairui Song a9fda5
d53e06
	while read -r _msize; do
d53e06
		size=$((size + _msize))
d53e06
	done <<< "$(readelf -l -W "$1" | awk '/^  LOAD/{print $6}' 2> /dev/stderr)"
Kairui Song a9fda5
d53e06
	echo $size
Kairui Song a9fda5
}
Kairui Song a9fda5
Kairui Song a9fda5
try_decompress()
Kairui Song a9fda5
{
d53e06
	# The obscure use of the "tr" filter is to work around older versions of
d53e06
	# "grep" that report the byte offset of the line instead of the pattern.
Kairui Song a9fda5
d53e06
	# Try to find the header ($1) and decompress from here
d53e06
	for pos in $(tr "$1\n$2" "\n$2=" < "$4" | grep -abo "^$2"); do
d53e06
		if ! type -P "$3" > /dev/null; then
d53e06
			ddebug "Signiature detected but '$3' is missing, skip this decompressor"
d53e06
			break
d53e06
		fi
Kairui Song a9fda5
d53e06
		pos=${pos%%:*}
d53e06
		tail "-c+$pos" "$img" | $3 > "$5" 2> /dev/null
d53e06
		if check_vmlinux "$5"; then
d53e06
			ddebug "Kernel is extracted with '$3'"
d53e06
			return 0
d53e06
		fi
d53e06
	done
Kairui Song a9fda5
d53e06
	return 1
Kairui Song a9fda5
}
Kairui Song a9fda5
Kairui Song a9fda5
# Borrowed from linux/scripts/extract-vmlinux
Kairui Song a9fda5
get_kernel_size()
Kairui Song a9fda5
{
d53e06
	# Prepare temp files:
d53e06
	local tmp img=$1
d53e06
d53e06
	tmp=$(mktemp /tmp/vmlinux-XXX)
d53e06
	trap 'rm -f "$tmp"' 0
d53e06
d53e06
	# Try to check if it's a vmlinux already
d53e06
	check_vmlinux "$img" && get_vmlinux_size "$img" && return 0
d53e06
d53e06
	# That didn't work, so retry after decompression.
d53e06
	try_decompress '\037\213\010' xy gunzip "$img" "$tmp" ||
d53e06
		try_decompress '\3757zXZ\000' abcde unxz "$img" "$tmp" ||
d53e06
		try_decompress 'BZh' xy bunzip2 "$img" "$tmp" ||
d53e06
		try_decompress '\135\0\0\0' xxx unlzma "$img" "$tmp" ||
d53e06
		try_decompress '\211\114\132' xy 'lzop -d' "$img" "$tmp" ||
d53e06
		try_decompress '\002!L\030' xxx 'lz4 -d' "$img" "$tmp" ||
d53e06
		try_decompress '(\265/\375' xxx unzstd "$img" "$tmp"
d53e06
d53e06
	# Finally check for uncompressed images or objects:
d53e06
	[[ $? -eq 0 ]] && get_vmlinux_size "$tmp" && return 0
d53e06
d53e06
	# Fallback to use iomem
d53e06
	local _size=0 _seg
d53e06
	while read -r _seg; do
d53e06
		_size=$((_size + 0x${_seg#*-} - 0x${_seg%-*}))
d53e06
	done <<< "$(grep -E "Kernel (code|rodata|data|bss)" /proc/iomem | cut -d ":" -f 1)"
d53e06
	echo $_size
Kairui Song a9fda5
}