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
Lichen Liu 4a1388
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump/enabled"
Lichen Liu 4a1388
FADUMP_REGISTER_SYS_NODE="/sys/kernel/fadump/registered"
Petr Šabata f5bf49
206f59
is_uki()
206f59
{
206f59
	local img
206f59
206f59
	img="$1"
206f59
206f59
	[[ -f "$img" ]] || return
206f59
	[[ "$(file -b --mime-type "$img")" == application/x-dosexec ]] || return
206f59
	objdump -h -j .linux "$img" &> /dev/null
206f59
}
206f59
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
Baoquan He 1d7a65
is_sme_or_sev_active()
Baoquan He 1d7a65
{
Baoquan He 1d7a65
	journalctl -q --dmesg --grep "^Memory Encryption Features active: AMD (SME|SEV)$" >/dev/null 2>&1
Baoquan He 1d7a65
}
Baoquan He 1d7a65
d53e06
is_squash_available()
d53e06
{
Pingfan Liu 445416
	local _version kmodule
Pingfan Liu 445416
Pingfan Liu 445416
	_version=$(_get_kdump_kernel_version)
d53e06
	for kmodule in squashfs overlay loop; do
Pingfan Liu 445416
		modprobe -S "$_version" --dry-run $kmodule &> /dev/null || return 1
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
Lichen Liu e47ec6
is_ostree()
Kairui Song 80c191
{
Lichen Liu 0cecfa
	test -f /run/ostree-booted
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
Lichen Liu 549f1f
get_reserved_mem_size()
Lichen Liu 549f1f
{
Lichen Liu 549f1f
	local reserved_mem_size=0
Lichen Liu 549f1f
Lichen Liu 549f1f
	if is_fadump_capable; then
Lichen Liu 549f1f
		reserved_mem_size=$(< /sys/kernel/fadump/mem_reserved)
Lichen Liu 549f1f
	else
Lichen Liu 549f1f
		reserved_mem_size=$(< /sys/kernel/kexec_crash_size)
Lichen Liu 549f1f
	fi
Lichen Liu 549f1f
Lichen Liu 549f1f
	echo "$reserved_mem_size"
Lichen Liu 549f1f
}
Lichen Liu 549f1f
Petr Šabata f5bf49
check_crash_mem_reserved()
Petr Šabata f5bf49
{
d53e06
	local mem_reserved
Petr Šabata f5bf49
Lichen Liu 549f1f
	mem_reserved=$(get_reserved_mem_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
Lichen Liu cbb772
is_kernel_loaded()
Petr Šabata f5bf49
{
Lichen Liu cbb772
	local _sysfs _mode
Petr Šabata f5bf49
Lichen Liu cbb772
	_mode=$1
Lichen Liu cbb772
Lichen Liu cbb772
	case "$_mode" in
Lichen Liu cbb772
	kdump)
Lichen Liu cbb772
		_sysfs="/sys/kernel/kexec_crash_loaded"
Lichen Liu cbb772
		;;
Lichen Liu cbb772
	fadump)
Lichen Liu cbb772
		_sysfs="$FADUMP_REGISTER_SYS_NODE"
Lichen Liu cbb772
		;;
Lichen Liu cbb772
	*)
Lichen Liu cbb772
		derror "Unknown dump mode '$_mode' provided"
Lichen Liu cbb772
 		return 1
Lichen Liu cbb772
		;;
Lichen Liu cbb772
	esac
Lichen Liu cbb772
 
Lichen Liu cbb772
	if [[ ! -f $_sysfs ]]; then
Lichen Liu cbb772
		derror "$_mode is not supported on this kernel"
Lichen Liu cbb772
 		return 1
Lichen Liu cbb772
 	fi
Lichen Liu cbb772
Lichen Liu cbb772
	[[ $(< $_sysfs) -eq 1 ]]
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
# 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
Coiby Xu 24020b
Coiby Xu 24020b
	# For secureboot enabled machines, use new kexec file based syscall.
Coiby Xu 24020b
	# Old syscall will always fail as it does not have capability to do
Coiby Xu 24020b
	# kernel signature verification.
Coiby Xu 24020b
	if is_secure_boot_enforced; then
Coiby Xu 24020b
		dinfo "Secure Boot is enabled. Using kexec file based syscall."
Coiby Xu 24020b
		kexec_args="$kexec_args -s"
Coiby Xu 24020b
	fi
Coiby Xu 24020b
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 /"}
206f59
	boot_imglist="$KDUMP_IMG-$kdump_kernelver$KDUMP_IMG_EXT \
206f59
		$machine_id/$kdump_kernelver/$KDUMP_IMG \
206f59
		EFI/Linux/$machine_id-$kdump_kernelver.efi"
d53e06
Lichen Liu 67f450
	# The kernel of OSTree based systems is not in the standard locations.
Lichen Liu 67f450
	if is_ostree; then
Lichen Liu 67f450
		boot_dirlist="$(echo /boot/ostree/*) $boot_dirlist"
Lichen Liu 67f450
	fi
Lichen Liu 67f450
d53e06
	# Use BOOT_IMAGE as reference if possible, strip the GRUB root device prefix in (hd0,gpt1) format
Lichen Liu 1eb996
	boot_img="$(grep -P -o '^BOOT_IMAGE=(\S+)' /proc/cmdline | sed "s/^BOOT_IMAGE=\((\S*)\)\?\(\S*\)/\2/")"
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
Pingfan Liu 78e962
_is_valid_kver()
Pingfan Liu 78e962
{
Pingfan Liu 78e962
	[[ -f /usr/lib/modules/$1/modules.dep ]]
Pingfan Liu 78e962
}
Pingfan Liu 78e962
Pingfan Liu 78e962
# This function is introduced since 64k variant may be installed on 4k or vice versa
Pingfan Liu 78e962
# $1 the kernel path name.
Pingfan Liu 78e962
parse_kver_from_path()
Pingfan Liu 78e962
{
Pingfan Liu 78e962
	local _img _kver
Pingfan Liu 78e962
Pingfan Liu 78e962
	[[ -z "$1" ]] && return
Pingfan Liu 78e962
Pingfan Liu 78e962
	_img=$1
Pingfan Liu 78e962
	BLS_ENTRY_TOKEN=$(
Pingfan Liu 78e962
Pingfan Liu 78e962
	# Fedora standard installation, i.e. $BOOT/vmlinuz-<version>
Pingfan Liu 78e962
	_kver=${_img##*/vmlinuz-}
Pingfan Liu 78e962
	_kver=${_kver%"$KDUMP_IMG_EXT"}
Pingfan Liu 78e962
	if _is_valid_kver "$_kver"; then
Pingfan Liu 78e962
		echo "$_kver"
Pingfan Liu 78e962
		return
Pingfan Liu 78e962
	fi
Pingfan Liu 78e962
Pingfan Liu 78e962
	# BLS recommended image names, i.e. $BOOT/<token>/<version>/linux
Pingfan Liu 78e962
	_kver=${_img##*/"$BLS_ENTRY_TOKEN"/}
Pingfan Liu 78e962
	_kver=${_kver%%/*}
Pingfan Liu 78e962
	if _is_valid_kver "$_kver"; then
Pingfan Liu 78e962
		echo "$_kver"
Pingfan Liu 78e962
		return
Pingfan Liu 78e962
	fi
Pingfan Liu 78e962
Pingfan Liu 78e962
	# Fedora UKI installation, i.e. $BOOT/efi/EFI/Linux/<token>-<version>.efi
Pingfan Liu 78e962
	_kver=${_img##*/"$BLS_ENTRY_TOKEN"-}
Pingfan Liu 78e962
	_kver=${_kver%.efi}
Pingfan Liu 78e962
	if _is_valid_kver "$_kver"; then
Pingfan Liu 78e962
		echo "$_kver"
Pingfan Liu 78e962
		return
Pingfan Liu 78e962
	fi
Pingfan Liu 78e962
Pingfan Liu 78e962
	ddebug "Could not parse version from $_img"
Pingfan Liu 78e962
}
Pingfan Liu 78e962
Pingfan Liu 445416
_get_kdump_kernel_version()
Pingfan Liu 445416
{
Pingfan Liu 445416
	local _version _version_nondebug
Pingfan Liu 445416
Pingfan Liu 445416
	if [[ -n "$KDUMP_KERNELVER" ]]; then
Pingfan Liu 445416
		echo "$KDUMP_KERNELVER"
Pingfan Liu 445416
		return
Pingfan Liu 445416
	fi
Pingfan Liu 445416
Pingfan Liu 445416
	_version=$(uname -r)
Pingfan Liu 47391b
	if [[ ! "$_version" =~ [+|-]debug$ ]]; then
Pingfan Liu 445416
		echo "$_version"
Pingfan Liu 445416
		return
Pingfan Liu 445416
	fi
Pingfan Liu 445416
Pingfan Liu 445416
	_version_nondebug=${_version%+debug}
Pingfan Liu 47391b
	_version_nondebug=${_version_nondebug%-debug}
Pingfan Liu 445416
	if [[ -f "$(prepare_kdump_kernel "$_version_nondebug")" ]]; then
Pingfan Liu 445416
		dinfo "Use of debug kernel detected. Trying to use $_version_nondebug"
Pingfan Liu 445416
		echo "$_version_nondebug"
Pingfan Liu 445416
	else
Pingfan Liu 445416
		dinfo "Use of debug kernel detected but cannot find $_version_nondebug. Falling back to $_version"
Pingfan Liu 445416
		echo "$_version"
Pingfan Liu 445416
	fi
Pingfan Liu 445416
}
Pingfan Liu 445416
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
{
Pingfan Liu 445416
	local boot_initrdlist default_initrd_base var_target_initrd_dir
Lichen Liu 987eda
Pingfan Liu 445416
	KDUMP_KERNELVER=$(_get_kdump_kernel_version)
Lichen Liu 987eda
	KDUMP_KERNEL=$(prepare_kdump_kernel "$KDUMP_KERNELVER")
Lichen Liu 987eda
Lichen Liu 987eda
	if ! [[ -e $KDUMP_KERNEL ]]; then
d53e06
		derror "Failed to detect kdump kernel location"
d53e06
		return 1
d53e06
	fi
d53e06
Pingfan Liu d8ee87
	# For 64k variant, e.g. vmlinuz-5.14.0-327.el9.aarch64+64k-debug
Pingfan Liu d8ee87
	if [[ "$KDUMP_KERNEL" == *"+debug" || "$KDUMP_KERNEL" == *"64k-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
206f59
	if is_uki "$KDUMP_KERNEL"; then
206f59
		KDUMP_BOOTDIR=/boot
206f59
	else
206f59
		KDUMP_BOOTDIR=$(dirname "$KDUMP_KERNEL")
206f59
	fi
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
411b20
_cmdline_parse()
411b20
{
411b20
	local opt val
411b20
411b20
	while read -r opt; do
411b20
		if [[ $opt =~ = ]]; then
411b20
			val=${opt#*=}
411b20
			opt=${opt%%=*}
411b20
			# ignore options like 'foo='
411b20
			[[ -z $val ]] && continue
411b20
			# xargs removes quotes, add them again
411b20
			[[ $val =~ [[:space:]] ]] && val="\"$val\""
411b20
		else
411b20
			val=""
411b20
		fi
411b20
411b20
		echo "$opt $val"
411b20
	done <<< "$(echo "$1" | xargs -n 1 echo)"
411b20
}
411b20
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
{
411b20
	local in out append opt val id drv
411b20
	local -A remove
411b20
411b20
	in=${1:-$(< /proc/cmdline)}
411b20
	while read -r opt val; do
411b20
		[[ -n "$opt" ]] || continue
411b20
		remove[$opt]=1
411b20
	done <<< "$(_cmdline_parse "$2")"
411b20
	append=$3
d53e06
d53e06
d53e06
	# These params should always be removed
411b20
	remove[crashkernel]=1
411b20
	remove[panic_on_warn]=1
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.
411b20
	remove[root]=1
d53e06
d53e06
	# With the help of "--hostonly-cmdline", we can avoid some interitage.
411b20
	remove[rd.lvm.lv]=1
411b20
	remove[rd.luks.uuid]=1
411b20
	remove[rd.dm.uuid]=1
411b20
	remove[rd.md.uuid]=1
411b20
	remove[fcoe]=1
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.
411b20
	remove[netroot]=1
411b20
	remove[rd.iscsi.initiator]=1
411b20
	remove[iscsi_initiator]=1
411b20
411b20
	while read -r opt val; do
411b20
		[[ -n "$opt" ]] || continue
411b20
		[[ -n "${remove[$opt]}" ]] && continue
411b20
411b20
		if [[ -n "$val" ]]; then
411b20
			out+="$opt=$val "
411b20
		else
411b20
			out+="$opt "
411b20
		fi
411b20
	done <<< "$(_cmdline_parse "$in")"
d53e06
411b20
	out+="$append "
d53e06
d53e06
	id=$(get_bootcpu_apicid)
411b20
	if [[ -n "${id}" ]]; then
411b20
		out+="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.
411b20
	for drv in $(get_watchdog_drvs); do
411b20
		out+="$drv.pretimeout=0 "
411b20
411b20
		if [[ $drv == hpwdt ]]; then
411b20
			# hpwdt have a special parameter kdumptimeout, it is
411b20
			# only supposed to be set to non-zero in first kernel.
411b20
			# In kdump, non-zero value could prevent the watchdog
411b20
			# from resetting the system.
411b20
			out+="$drv.kdumptimeout=0 "
d53e06
		fi
d53e06
	done
d53e06
206f59
	# Always disable gpt-auto-generator as it hangs during boot of the
206f59
	# crash kernel. Furthermore we know which disk will be used for dumping
206f59
	# (if at all) and add it explicitly.
206f59
	is_uki "$KDUMP_KERNEL" && out+="rd.systemd.gpt_auto=no "
206f59
411b20
	# Trim unnecessary whitespaces
411b20
	echo "$out" | sed -e "s/^ *//g" -e "s/ *$//g" -e "s/ \+/ /g"
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
Pingfan Liu df074e
has_mlx5()
Pingfan Liu df074e
{
Pingfan Liu df074e
	[[ -d /sys/bus/pci/drivers/mlx5_core ]]
Pingfan Liu df074e
}
Pingfan Liu df074e
Pingfan Liu df074e
has_aarch64_smmu()
Pingfan Liu df074e
{
Pingfan Liu df074e
	ls /sys/devices/platform/arm-smmu-* 1> /dev/null 2>&1
Pingfan Liu df074e
}
Pingfan Liu df074e
Baoquan He 01ea01
is_memsize() { [[ "$1" =~ ^[+-]?[0-9]+[KkMmGgTtPbEe]?$ ]]; }
Baoquan He 0778b6
Baoquan He 0778b6
# range defined for crashkernel parameter
Baoquan He 0778b6
# i.e. <start>-[<end>]
Baoquan He 0778b6
is_memrange()
Baoquan He 0778b6
{
Baoquan He 0778b6
	is_memsize "${1%-*}" || return 1
Baoquan He 0778b6
	[[ -n ${1#*-} ]] || return 0
Baoquan He 0778b6
	is_memsize "${1#*-}"
Baoquan He 0778b6
}
Baoquan He 0778b6
Baoquan He 0778b6
to_bytes()
Baoquan He 0778b6
{
Baoquan He 0778b6
	local _s
Baoquan He 0778b6
Baoquan He 0778b6
	_s="$1"
Baoquan He 0778b6
	is_memsize "$_s" || return 1
Baoquan He 0778b6
Baoquan He 0778b6
	case "${_s: -1}" in
Baoquan He 0778b6
		K|k)
Baoquan He 0778b6
			_s=${_s::-1}
Baoquan He 0778b6
			_s="$((_s * 1024))"
Baoquan He 0778b6
			;;
Baoquan He 0778b6
		M|m)
Baoquan He 0778b6
			_s=${_s::-1}
Baoquan He 0778b6
			_s="$((_s * 1024 * 1024))"
Baoquan He 0778b6
			;;
Baoquan He 0778b6
		G|g)
Baoquan He 0778b6
			_s=${_s::-1}
Baoquan He 0778b6
			_s="$((_s * 1024 * 1024 * 1024))"
Baoquan He 0778b6
			;;
Baoquan He 01ea01
		T|t)
Baoquan He 01ea01
			_s=${_s::-1}
Baoquan He 01ea01
			_s="$((_s * 1024 * 1024 * 1024 * 1024))"
Baoquan He 01ea01
			;;
Baoquan He 01ea01
		P|p)
Baoquan He 01ea01
			_s=${_s::-1}
Baoquan He 01ea01
			_s="$((_s * 1024 * 1024 * 1024 * 1024 * 1024))"
Baoquan He 01ea01
			;;
Baoquan He 01ea01
		E|e)
Baoquan He 01ea01
			_s=${_s::-1}
Baoquan He 01ea01
			_s="$((_s * 1024 * 1024 * 1024 * 1024 * 1024 * 1024))"
Baoquan He 01ea01
			;;
Baoquan He 0778b6
		*)
Baoquan He 0778b6
			;;
Baoquan He 0778b6
	esac
Baoquan He 0778b6
	echo "$_s"
Baoquan He 0778b6
}
Baoquan He 0778b6
Baoquan He 0778b6
memsize_add()
Baoquan He 0778b6
{
Baoquan He 01ea01
	local -a units=("" "K" "M" "G" "T" "P" "E")
Baoquan He 0778b6
	local i a b
Baoquan He 0778b6
Baoquan He 0778b6
	a=$(to_bytes "$1") || return 1
Baoquan He 0778b6
	b=$(to_bytes "$2") || return 1
Baoquan He 0778b6
	i=0
Baoquan He 0778b6
Baoquan He 0778b6
	(( a += b ))
Baoquan He 0778b6
	while :; do
Baoquan He 0778b6
		[[ $(( a / 1024 )) -eq 0 ]] && break
Baoquan He 0778b6
		[[ $(( a % 1024 )) -ne 0 ]] && break
Baoquan He 0778b6
		[[ $(( ${#units[@]} - 1 )) -eq $i ]] && break
Baoquan He 0778b6
Baoquan He 0778b6
		(( a /= 1024 ))
Baoquan He 0778b6
		(( i += 1 ))
Baoquan He 0778b6
	done
Baoquan He 0778b6
Baoquan He 0778b6
	echo "${a}${units[$i]}"
Baoquan He 0778b6
}
Baoquan He 0778b6
Baoquan He 0778b6
_crashkernel_parse()
Pingfan Liu 37de94
{
Baoquan He 0778b6
	local ck entry
Baoquan He 0778b6
	local range size offset
Pingfan Liu 37de94
Baoquan He 0778b6
	ck="$1"
Pingfan Liu 37de94
Baoquan He 0778b6
	if [[ "$ck" == *@* ]]; then
Baoquan He 0778b6
		offset="@${ck##*@}"
Baoquan He 0778b6
		ck=${ck%@*}
Baoquan He 0778b6
	elif [[ "$ck" == *,high ]] || [[ "$ck" == *,low ]]; then
Baoquan He 0778b6
		offset=",${ck##*,}"
Baoquan He 0778b6
		ck=${ck%,*}
Pingfan Liu 37de94
	else
Baoquan He 0778b6
		offset=''
Pingfan Liu 37de94
	fi
Pingfan Liu 37de94
Baoquan He 0778b6
	while read -d , -r entry; do
Baoquan He 0778b6
		[[ -n "$entry" ]] || continue
Baoquan He 0778b6
		if [[ "$entry" == *:* ]]; then
Baoquan He 0778b6
			range=${entry%:*}
Baoquan He 0778b6
			size=${entry#*:}
Pingfan Liu 37de94
		else
Baoquan He 0778b6
			range=""
Baoquan He 0778b6
			size=${entry}
Pingfan Liu 37de94
		fi
Pingfan Liu 37de94
Baoquan He 0778b6
		echo "$size;$range;"
Baoquan He 0778b6
	done <<< "$ck,"
Baoquan He 0778b6
	echo ";;$offset"
Baoquan He 0778b6
}
Baoquan He 0778b6
Baoquan He 0778b6
# $1 crashkernel command line parameter
Baoquan He 0778b6
# $2 size to be added
Baoquan He 0778b6
_crashkernel_add()
Baoquan He 0778b6
{
Baoquan He 0778b6
	local ck delta ret
Baoquan He 0778b6
	local range size offset
Baoquan He 0778b6
Baoquan He 0778b6
	ck="$1"
Baoquan He 0778b6
	delta="$2"
Baoquan He 0778b6
	ret=""
Baoquan He 0778b6
Baoquan He 0778b6
	while IFS=';' read -r size range offset; do
Baoquan He 0778b6
		if [[ -n "$offset" ]]; then
Baoquan He 0778b6
			ret="${ret%,}$offset"
Baoquan He 0778b6
			break
Baoquan He 0778b6
		fi
Baoquan He 0778b6
Baoquan He 0778b6
		[[ -n "$size" ]] || continue
Baoquan He 0778b6
		if [[ -n "$range" ]]; then
Baoquan He 0778b6
			is_memrange "$range" || return 1
Baoquan He 0778b6
			ret+="$range:"
Baoquan He 0778b6
		fi
Pingfan Liu 37de94
Baoquan He 0778b6
		size=$(memsize_add "$size" "$delta") || return 1
Baoquan He 0778b6
		ret+="$size,"
Baoquan He 0778b6
	done < <( _crashkernel_parse "$ck")
Pingfan Liu 37de94
Baoquan He 0778b6
	echo "${ret%,}"
Pingfan Liu 37de94
}
Pingfan Liu 37de94
Coiby Xu 1ab968
# get default crashkernel
Coiby Xu 1ab968
# $1 dump mode, if not specified, dump_mode will be judged by is_fadump_capable
Pingfan Liu cde552
# $2 kernel-release, if not specified, got by _get_kdump_kernel_version
Coiby Xu 1ab968
kdump_get_arch_recommend_crashkernel()
Coiby Xu 1ab968
{
Coiby Xu 1ab968
	local _arch _ck_cmdline _dump_mode
Baoquan He 1d7a65
	local _delta=0
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"
Baoquan He 1d7a65
		is_sme_or_sev_active && ((_delta += 64))
Coiby Xu 1ab968
	elif [[ $_arch == "aarch64" ]]; then
Pingfan Liu cde552
		local _running_kernel
Pingfan Liu cde552
Pingfan Liu cde552
		# Base line for 4K variant kernel. The formula is based on x86 plus extra = 64M
Pingfan Liu a31811
		_ck_cmdline="1G-4G:256M,4G-64G:320M,64G-:576M"
Pingfan Liu cde552
		if [[ -z "$2" ]]; then
Pingfan Liu cde552
			_running_kernel=$(_get_kdump_kernel_version)
Pingfan Liu cde552
		else
Pingfan Liu cde552
			_running_kernel=$2
Pingfan Liu cde552
		fi
Pingfan Liu cde552
Pingfan Liu cde552
		# the naming convention of 64k variant suffixes with +64k, e.g. "vmlinuz-5.14.0-312.el9.aarch64+64k"
Pingfan Liu cde552
		if echo "$_running_kernel" | grep -q 64k; then
Pingfan Liu cde552
			# Without smmu, the diff of MemFree between 4K and 64K measured on a high end aarch64 machine is 82M.
Pingfan Liu cde552
			# Picking up 100M to cover this diff. And finally, we have "1G-4G:356M;4G-64G:420M;64G-:676M"
Pingfan Liu cde552
			((_delta += 100))
Pingfan Liu df074e
			# On a 64K system, the extra 384MB is calculated by: cmdq_num * 16 bytes + evtq_num * 32B + priq_num * 16B
Pingfan Liu df074e
			# While on a 4K system, it is negligible
Pingfan Liu df074e
			has_aarch64_smmu && ((_delta += 384))
Pingfan Liu df074e
			#64k kernel, mlx5 consumes extra 188M memory, and choose 200M
Pingfan Liu df074e
			has_mlx5 && ((_delta += 200))
Pingfan Liu cde552
		else
Pingfan Liu df074e
			#4k kernel, mlx5 consumes extra 124M memory, and choose 150M
Pingfan Liu df074e
			has_mlx5 && ((_delta += 150))
Pingfan Liu cde552
		fi
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
Baoquan He 1d7a65
	echo -n "$(_crashkernel_add "$_ck_cmdline" "${_delta}M")"
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
}