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
FENCE_KDUMP_CONFIG_FILE="/etc/sysconfig/fence_kdump"
Petr Šabata f5bf49
FENCE_KDUMP_SEND="/usr/libexec/fence_kdump_send"
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
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
{
d53e06
	[[ $(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw\|nfs\|ssh") ]] || is_mount_in_dracut_args
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_user_configured_dump_disk()
Petr Šabata f5bf49
{
d53e06
	local _target
Petr Šabata f5bf49
d53e06
	_target=$(kdump_get_conf_val "ext[234]\|xfs\|btrfs\|minix\|raw")
d53e06
	[[ -n $_target ]] && echo "$_target" && return
Petr Šabata f5bf49
d53e06
	_target=$(get_dracut_args_target "$(kdump_get_conf_val "dracut_args")")
d53e06
	[[ -b $_target ]] && echo "$_target"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
get_block_dump_target()
Petr Šabata f5bf49
{
d53e06
	local _target _path
Petr Šabata f5bf49
d53e06
	if is_ssh_dump_target || is_nfs_dump_target; then
d53e06
		return
d53e06
	fi
Petr Šabata f5bf49
d53e06
	_target=$(get_user_configured_dump_disk)
d53e06
	[[ -n $_target ]] && to_dev_name "$_target" && return
Petr Šabata f5bf49
d53e06
	# Get block device name from local save path
d53e06
	_path=$(get_save_path)
d53e06
	_target=$(get_target_from_path "$_path")
d53e06
	[[ -b $_target ]] && to_dev_name "$_target"
Petr Šabata f5bf49
}
Petr Šabata f5bf49
Petr Šabata f5bf49
is_dump_to_rootfs()
Petr Šabata f5bf49
{
d53e06
	[[ $(kdump_get_conf_val "failure_action|default") == dump_to_rootfs ]]
Petr Šabata f5bf49
}
Petr Šabata f5bf49
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
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
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
d53e06
	# On powerpc, os-secureboot-enforcing DT property indicates whether secureboot
d53e06
	# is enforced. Return success, if it is found.
d53e06
	if [[ -f /proc/device-tree/ibm,secureboot/os-secureboot-enforcing ]]; then
Petr Šabata f5bf49
		return 0
d53e06
	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
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Detect initrd and kernel location, results are stored in global enviromental variables:
Petr Šabata f5bf49
# KDUMP_BOOTDIR, KDUMP_KERNELVER, KDUMP_KERNEL, DEFAULT_INITRD, and KDUMP_INITRD
Petr Šabata f5bf49
#
Petr Šabata f5bf49
# Expectes KDUMP_BOOTDIR, KDUMP_IMG, KDUMP_IMG_EXT, KDUMP_KERNELVER to be loaded from config already
Petr Šabata f5bf49
# and will prefer already set values so user can specify custom kernel/initramfs location
Petr Šabata f5bf49
#
Petr Šabata f5bf49
prepare_kdump_bootinfo()
Petr Šabata f5bf49
{
d53e06
	local boot_img boot_imglist boot_dirlist boot_initrdlist
d53e06
	local machine_id
d53e06
d53e06
	if [[ -z $KDUMP_KERNELVER ]]; then
d53e06
		KDUMP_KERNELVER="$(uname -r)"
d53e06
	fi
d53e06
d53e06
	read -r machine_id < /etc/machine-id
d53e06
	boot_dirlist=${KDUMP_BOOTDIR:-"/boot /boot/efi /efi /"}
d53e06
	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)"
d53e06
	if [[ -n $boot_img ]]; 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
d53e06
				KDUMP_KERNEL=$(echo "$dir/$img" | tr -s '/')
d53e06
				break 2
d53e06
			fi
d53e06
		done
d53e06
	done
d53e06
d53e06
	if ! [[ -e $KDUMP_KERNEL ]]; then
d53e06
		derror "Failed to detect kdump kernel location"
d53e06
		return 1
d53e06
	fi
d53e06
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
d53e06
			defaut_initrd_base="$initrd"
d53e06
			DEFAULT_INITRD="$KDUMP_BOOTDIR/$defaut_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
d53e06
	if [[ -z $defaut_initrd_base ]]; then
d53e06
		kdump_initrd_base=initramfs-${KDUMP_KERNELVER}kdump.img
d53e06
	elif [[ $defaut_initrd_base == *.* ]]; then
d53e06
		kdump_initrd_base=${defaut_initrd_base%.*}kdump.${DEFAULT_INITRD##*.}
d53e06
	else
d53e06
		kdump_initrd_base=${defaut_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
	# Remove trace_buf_size, trace_event
d53e06
	cmdline=$(remove_cmdline_param "$cmdline" trace_buf_size trace_event)
d53e06
	cmdline="${cmdline} trace_buf_size=1"
d53e06
d53e06
	echo "$cmdline"
Petr Šabata f5bf49
}
Pingfan Liu 540d33
Pingfan Liu 540d33
#get system memory size in the unit of GB
Pingfan Liu 540d33
get_system_size()
Pingfan Liu 540d33
{
d53e06
	result=$(grep "System RAM" /proc/iomem | awk -F ":" '{ print $1 }' | tr "[:lower:]" "[:upper:]" | paste -sd+)
d53e06
	result="+$result"
d53e06
	# replace '-' with '+0x' and '+' with '-0x'
d53e06
	sum=$(echo "$result" | sed -e 's/-/K0x/g' -e 's/+/-0x/g' -e 's/K/+/g')
d53e06
	size=$(printf "%d\n" $((sum)))
d53e06
	size=$((size / 1024 / 1024 / 1024))
Pingfan Liu 540d33
d53e06
	echo "$size"
Pingfan Liu 540d33
}
Pingfan Liu 540d33
Pingfan Liu 540d33
get_recommend_size()
Pingfan Liu 540d33
{
d53e06
	local mem_size=$1
d53e06
	local _ck_cmdline=$2
d53e06
	local OLDIFS="$IFS"
d53e06
d53e06
	start=${_ck_cmdline::1}
d53e06
	if [[ $mem_size -lt $start ]]; then
d53e06
		echo "0M"
d53e06
		return
d53e06
	fi
d53e06
	IFS=','
d53e06
	for i in $_ck_cmdline; do
d53e06
		end=$(echo "$i" | awk -F "-" '{ print $2 }' | awk -F ":" '{ print $1 }')
d53e06
		recommend=$(echo "$i" | awk -F "-" '{ print $2 }' | awk -F ":" '{ print $2 }')
d53e06
		size=${end::-1}
d53e06
		unit=${end: -1}
d53e06
		if [[ $unit == 'T' ]]; then
d53e06
			size=$((size * 1024))
d53e06
		fi
d53e06
		if [[ $mem_size -lt $size ]]; then
d53e06
			echo "$recommend"
d53e06
			IFS="$OLDIFS"
d53e06
			return
d53e06
		fi
d53e06
	done
d53e06
	IFS="$OLDIFS"
Pingfan Liu 540d33
}
Pingfan Liu 540d33
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
{
d53e06
	local kernel=$1 arch
Kairui Song abb0c3
d53e06
	if ! [[ -r "/proc/iomem" ]]; then
d53e06
		echo "Error, can not access /proc/iomem."
d53e06
		return 1
d53e06
	fi
Pingfan Liu 540d33
d53e06
	[[ -z $kernel ]] && kernel=$(uname -r)
d53e06
	ck_cmdline=$(cat "/usr/lib/modules/$kernel/crashkernel.default" 2> /dev/null)
Kairui Song abb0c3
d53e06
	if [[ -n $ck_cmdline ]]; then
d53e06
		ck_cmdline=${ck_cmdline#crashkernel=}
d53e06
	else
d53e06
		arch=$(lscpu | grep Architecture | awk -F ":" '{ print $2 }' | tr '[:lower:]' '[:upper:]')
d53e06
		if [[ $arch == "X86_64" ]] || [[ $arch == "S390X" ]]; then
d53e06
			ck_cmdline="1G-4G:160M,4G-64G:192M,64G-1T:256M,1T-:512M"
d53e06
		elif [[ $arch == "AARCH64" ]]; then
d53e06
			ck_cmdline="2G-:448M"
d53e06
		elif [[ $arch == "PPC64LE" ]]; then
d53e06
			if is_fadump_capable; then
d53e06
				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"
d53e06
			else
d53e06
				ck_cmdline="2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G"
d53e06
			fi
d53e06
		fi
d53e06
	fi
Pingfan Liu 540d33
d53e06
	ck_cmdline=${ck_cmdline//-:/-102400T:}
d53e06
	sys_mem=$(get_system_size)
Kairui Song abb0c3
d53e06
	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
d53e06
	_type=$(eval "$(blkid -u filesystem,crypto -o export -- "/dev/block/$1"); echo \$TYPE")
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
}