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