5cf148
#!/bin/bash
5cf148
KEXEC=/sbin/kexec
5cf148
5cf148
KDUMP_KERNELVER=""
5cf148
KDUMP_KERNEL=""
5cf148
KDUMP_COMMANDLINE=""
5cf148
KEXEC_ARGS=""
5cf148
KDUMP_LOG_PATH="/var/log"
5cf148
MKDUMPRD="/sbin/mkdumprd -f"
5cf148
MKFADUMPRD="/sbin/mkfadumprd"
5cf148
DRACUT_MODULES_FILE="/usr/lib/dracut/modules.txt"
5cf148
SAVE_PATH=/var/crash
5cf148
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
5cf148
DUMP_TARGET=""
5cf148
DEFAULT_INITRD=""
5cf148
DEFAULT_INITRD_BAK=""
4e4da3
INITRD_CHECKSUM_LOCATION=""
5cf148
KDUMP_INITRD=""
5cf148
TARGET_INITRD=""
5cf148
FADUMP_REGISTER_SYS_NODE="/sys/kernel/fadump_registered"
5cf148
#kdump shall be the default dump mode
5cf148
DEFAULT_DUMP_MODE="kdump"
5cf148
image_time=0
5cf148
5cf148
standard_kexec_args="-d -p"
5cf148
5cf148
# Some default values in case /etc/sysconfig/kdump doesn't include
5cf148
KDUMP_COMMANDLINE_REMOVE="hugepages hugepagesz slub_debug"
5cf148
5cf148
if [[ -f /etc/sysconfig/kdump ]]; then
5cf148
	. /etc/sysconfig/kdump
5cf148
fi
5cf148
5cf148
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
5cf148
. $dracutbasedir/dracut-functions.sh
5cf148
. /lib/kdump/kdump-lib.sh
5cf148
. /lib/kdump/kdump-logger.sh
5cf148
5cf148
#initiate the kdump logger
5cf148
if ! dlog_init; then
5cf148
	echo "failed to initiate the kdump logger."
5cf148
	exit 1
5cf148
fi
5cf148
5cf148
single_instance_lock()
5cf148
{
4e4da3
	local rc timeout=5 lockfile
5cf148
4e4da3
	if [[ -d /run/lock ]]; then
4e4da3
		lockfile=/run/lock/kdump
4e4da3
	else
4e4da3
		# when updating package using virt-customize, /run/lock doesn't exist
4e4da3
		lockfile=/tmp/kdump.lock
4e4da3
	fi
4e4da3
4e4da3
	if ! exec 9> $lockfile; then
5cf148
		derror "Create file lock failed"
5cf148
		exit 1
5cf148
	fi
5cf148
5cf148
	flock -n 9
5cf148
	rc=$?
5cf148
5cf148
	while [[ $rc -ne 0 ]]; do
5cf148
		dinfo "Another app is currently holding the kdump lock; waiting for it to exit..."
5cf148
		flock -w $timeout 9
5cf148
		rc=$?
5cf148
	done
5cf148
}
5cf148
5cf148
determine_dump_mode()
5cf148
{
5cf148
	# Check if firmware-assisted dump is enabled
5cf148
	# if yes, set the dump mode as fadump
5cf148
	if is_fadump_capable; then
5cf148
		dinfo "Dump mode is fadump"
5cf148
		DEFAULT_DUMP_MODE="fadump"
5cf148
	fi
5cf148
	ddebug "DEFAULT_DUMP_MODE=$DEFAULT_DUMP_MODE"
5cf148
}
5cf148
5cf148
save_core()
5cf148
{
5cf148
	coredir="/var/crash/$(date +"%Y-%m-%d-%H:%M")"
5cf148
5cf148
	mkdir -p "$coredir"
5cf148
	ddebug "cp --sparse=always /proc/vmcore $coredir/vmcore-incomplete"
5cf148
	if cp --sparse=always /proc/vmcore "$coredir/vmcore-incomplete"; then
5cf148
		mv "$coredir/vmcore-incomplete" "$coredir/vmcore"
5cf148
		dinfo "saved a vmcore to $coredir"
5cf148
	else
5cf148
		derror "failed to save a vmcore to $coredir"
5cf148
	fi
5cf148
5cf148
	# pass the dmesg to Abrt tool if exists, in order
5cf148
	# to collect the kernel oops message.
5cf148
	# https://fedorahosted.org/abrt/
5cf148
	if [[ -x /usr/bin/dumpoops ]]; then
5cf148
		ddebug "makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg"
5cf148
		makedumpfile --dump-dmesg "$coredir/vmcore" "$coredir/dmesg" > /dev/null 2>&1
5cf148
		ddebug "dumpoops -d $coredir/dmesg"
5cf148
		if dumpoops -d "$coredir/dmesg" > /dev/null 2>&1; then
5cf148
			dinfo "kernel oops has been collected by abrt tool"
5cf148
		fi
5cf148
	fi
5cf148
}
5cf148
5cf148
rebuild_fadump_initrd()
5cf148
{
5cf148
	if ! $MKFADUMPRD "$DEFAULT_INITRD_BAK" "$TARGET_INITRD" --kver "$KDUMP_KERNELVER"; then
5cf148
		derror "mkfadumprd: failed to make fadump initrd"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
check_earlykdump_is_enabled()
5cf148
{
5cf148
	grep -q -w "rd.earlykdump" /proc/cmdline
5cf148
	return $?
5cf148
}
5cf148
5cf148
rebuild_kdump_initrd()
5cf148
{
5cf148
	ddebug "rebuild kdump initrd: $MKDUMPRD $TARGET_INITRD $KDUMP_KERNELVER"
5cf148
	if ! $MKDUMPRD "$TARGET_INITRD" "$KDUMP_KERNELVER"; then
5cf148
		derror "mkdumprd: failed to make kdump initrd"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	if check_earlykdump_is_enabled; then
5cf148
		dwarn "Tips: If early kdump is enabled, also require rebuilding the system initramfs to make the changes take effect for early kdump."
5cf148
	fi
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
rebuild_initrd()
5cf148
{
5cf148
	if [[ ! -w $(dirname "$TARGET_INITRD") ]]; then
5cf148
		derror "$(dirname "$TARGET_INITRD") does not have write permission. Cannot rebuild $TARGET_INITRD"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
5cf148
		rebuild_fadump_initrd
5cf148
	else
5cf148
		rebuild_kdump_initrd
5cf148
	fi
5cf148
5cf148
	return $?
5cf148
}
5cf148
5cf148
#$1: the files to be checked with IFS=' '
5cf148
check_exist()
5cf148
{
5cf148
	for file in $1; do
5cf148
		if [[ ! -e $file ]]; then
5cf148
			derror "Error: $file not found."
5cf148
			return 1
5cf148
		fi
5cf148
	done
5cf148
}
5cf148
5cf148
#$1: the files to be checked with IFS=' '
5cf148
check_executable()
5cf148
{
5cf148
	for file in $1; do
5cf148
		if [[ ! -x $file ]]; then
5cf148
			derror "Error: $file is not executable."
5cf148
			return 1
5cf148
		fi
5cf148
	done
5cf148
}
5cf148
5cf148
backup_default_initrd()
5cf148
{
5cf148
	ddebug "backup default initrd: $DEFAULT_INITRD"
5cf148
5cf148
	if [[ ! -f $DEFAULT_INITRD ]]; then
5cf148
		return
5cf148
	fi
5cf148
5cf148
	if [[ ! -e $DEFAULT_INITRD_BAK ]]; then
5cf148
		dinfo "Backing up $DEFAULT_INITRD before rebuild."
5cf148
		# save checksum to verify before restoring
5cf148
		sha1sum "$DEFAULT_INITRD" > "$INITRD_CHECKSUM_LOCATION"
5cf148
		if ! cp "$DEFAULT_INITRD" "$DEFAULT_INITRD_BAK"; then
5cf148
			dwarn "WARNING: failed to backup $DEFAULT_INITRD."
4e4da3
			rm -f -- "$INITRD_CHECKSUM_LOCATION"
4e4da3
			rm -f -- "$DEFAULT_INITRD_BAK"
5cf148
		fi
5cf148
	fi
5cf148
}
5cf148
5cf148
restore_default_initrd()
5cf148
{
5cf148
	ddebug "restore default initrd: $DEFAULT_INITRD"
5cf148
5cf148
	if [[ ! -f $DEFAULT_INITRD ]]; then
5cf148
		return
5cf148
	fi
5cf148
5cf148
	# If a backup initrd exists, we must be switching back from
5cf148
	# fadump to kdump. Restore the original default initrd.
5cf148
	if [[ -f $DEFAULT_INITRD_BAK ]] && [[ -f $INITRD_CHECKSUM_LOCATION ]]; then
5cf148
		# verify checksum before restoring
5cf148
		backup_checksum=$(sha1sum "$DEFAULT_INITRD_BAK" | awk '{ print $1 }')
5cf148
		default_checksum=$(awk '{ print $1 }' "$INITRD_CHECKSUM_LOCATION")
5cf148
		if [[ $default_checksum != "$backup_checksum" ]]; then
5cf148
			dwarn "WARNING: checksum mismatch! Can't restore original initrd.."
5cf148
		else
5cf148
			rm -f $INITRD_CHECKSUM_LOCATION
5cf148
			if mv "$DEFAULT_INITRD_BAK" "$DEFAULT_INITRD"; then
5cf148
				derror "Restoring original initrd as fadump mode is disabled."
5cf148
				sync
5cf148
			fi
5cf148
		fi
5cf148
	fi
5cf148
}
5cf148
5cf148
check_config()
5cf148
{
5cf148
	local -A _opt_rec
5cf148
	while read -r config_opt config_val; do
5cf148
		case "$config_opt" in
5cf148
		dracut_args)
5cf148
			if [[ $config_val == *--mount* ]]; then
5cf148
				if [[ $(echo "$config_val" | grep -o "\-\-mount" | wc -l) -ne 1 ]]; then
5cf148
					derror 'Multiple mount targets specified in one "dracut_args".'
5cf148
					return 1
5cf148
				fi
5cf148
				config_opt=_target
5cf148
			fi
5cf148
			;;
5cf148
		raw)
5cf148
			if [[ -d "/proc/device-tree/ibm,opal/dump" ]]; then
5cf148
				dwarn "WARNING: Won't capture opalcore when 'raw' dump target is used."
5cf148
			fi
5cf148
			config_opt=_target
5cf148
			;;
4e4da3
		ext[234] | minix | btrfs | xfs | nfs | ssh | virtiofs)
5cf148
			config_opt=_target
5cf148
			;;
5cf148
		sshkey | path | core_collector | kdump_post | kdump_pre | extra_bins | extra_modules | failure_action | default | final_action | force_rebuild | force_no_rebuild | fence_kdump_args | fence_kdump_nodes | auto_reset_crashkernel) ;;
5cf148
5cf148
		net | options | link_delay | disk_timeout | debug_mem_level | blacklist)
5cf148
			derror "Deprecated kdump config option: $config_opt. Refer to kdump.conf manpage for alternatives."
5cf148
			return 1
5cf148
			;;
5cf148
		'')
5cf148
			continue
5cf148
			;;
5cf148
		*)
5cf148
			derror "Invalid kdump config option $config_opt"
5cf148
			return 1
5cf148
			;;
5cf148
		esac
5cf148
5cf148
		if [[ -z $config_val ]]; then
5cf148
			derror "Invalid kdump config value for option '$config_opt'"
5cf148
			return 1
5cf148
		fi
5cf148
5cf148
		if [[ -n ${_opt_rec[$config_opt]} ]]; then
5cf148
			if [[ $config_opt == _target ]]; then
5cf148
				derror "More than one dump targets specified"
5cf148
			else
5cf148
				derror "Duplicated kdump config value of option $config_opt"
5cf148
			fi
5cf148
			return 1
5cf148
		fi
5cf148
		_opt_rec[$config_opt]="$config_val"
5cf148
	done <<< "$(kdump_read_conf)"
5cf148
5cf148
	check_failure_action_config || return 1
5cf148
	check_final_action_config || return 1
5cf148
	check_fence_kdump_config || return 1
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
# get_pcs_cluster_modified_files <image timestamp>
5cf148
# return list of modified file for fence_kdump modified in Pacemaker cluster
5cf148
get_pcs_cluster_modified_files()
5cf148
{
5cf148
	local time_stamp
5cf148
	local modified_files
5cf148
5cf148
	is_generic_fence_kdump && return 1
5cf148
	is_pcs_fence_kdump || return 1
5cf148
5cf148
	time_stamp=$(pcs cluster cib | xmllint --xpath 'string(/cib/@cib-last-written)' - | xargs -0 date +%s --date)
5cf148
5cf148
	if [[ -n $time_stamp ]] && [[ $time_stamp -gt $image_time ]]; then
5cf148
		modified_files="cluster-cib"
5cf148
	fi
5cf148
5cf148
	if [[ -f $FENCE_KDUMP_CONFIG_FILE ]]; then
5cf148
		time_stamp=$(stat -c "%Y" "$FENCE_KDUMP_CONFIG_FILE")
5cf148
		if [[ $time_stamp -gt $image_time ]]; then
5cf148
			modified_files="$modified_files $FENCE_KDUMP_CONFIG_FILE"
5cf148
		fi
5cf148
	fi
5cf148
5cf148
	echo "$modified_files"
5cf148
}
5cf148
5cf148
setup_initrd()
5cf148
{
5cf148
	if ! prepare_kdump_bootinfo; then
5cf148
		derror "failed to prepare for kdump bootinfo."
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	DEFAULT_INITRD_BAK="$KDUMP_BOOTDIR/.$(basename "$DEFAULT_INITRD").default"
4e4da3
	INITRD_CHECKSUM_LOCATION="$DEFAULT_INITRD_BAK.checksum"
5cf148
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
5cf148
		TARGET_INITRD="$DEFAULT_INITRD"
5cf148
5cf148
		# backup initrd for reference before replacing it
5cf148
		# with fadump aware initrd
5cf148
		backup_default_initrd
5cf148
	else
5cf148
		TARGET_INITRD="$KDUMP_INITRD"
5cf148
5cf148
		# check if a backup of default initrd exists. If yes,
5cf148
		# it signifies a switch from fadump mode. So, restore
5cf148
		# the backed up default initrd.
5cf148
		restore_default_initrd
5cf148
	fi
5cf148
}
5cf148
5cf148
check_files_modified()
5cf148
{
5cf148
	local modified_files=""
5cf148
5cf148
	#also rebuild when Pacemaker cluster conf is changed and fence kdump is enabled.
5cf148
	modified_files=$(get_pcs_cluster_modified_files)
5cf148
5cf148
	EXTRA_BINS=$(kdump_get_conf_val kdump_post)
5cf148
	CHECK_FILES=$(kdump_get_conf_val kdump_pre)
5cf148
	HOOKS="/etc/kdump/post.d/ /etc/kdump/pre.d/"
5cf148
	if [[ -d /etc/kdump/post.d ]]; then
5cf148
		for file in /etc/kdump/post.d/*; do
5cf148
			if [[ -x $file ]]; then
5cf148
				POST_FILES="$POST_FILES $file"
5cf148
			fi
5cf148
		done
5cf148
	fi
5cf148
	if [[ -d /etc/kdump/pre.d ]]; then
5cf148
		for file in /etc/kdump/pre.d/*; do
5cf148
			if [[ -x $file ]]; then
5cf148
				PRE_FILES="$PRE_FILES $file"
5cf148
			fi
5cf148
		done
5cf148
	fi
5cf148
	HOOKS="$HOOKS $POST_FILES $PRE_FILES"
5cf148
	CORE_COLLECTOR=$(kdump_get_conf_val core_collector | awk '{print $1}')
5cf148
	CORE_COLLECTOR=$(type -P "$CORE_COLLECTOR")
5cf148
	# POST_FILES and PRE_FILES are already checked against executable, need not to check again.
5cf148
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
5cf148
	CHECK_FILES=$(kdump_get_conf_val extra_bins)
5cf148
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
5cf148
	files="$KDUMP_CONFIG_FILE $KDUMP_KERNEL $EXTRA_BINS $CORE_COLLECTOR"
5cf148
	[[ -e /etc/fstab ]] && files="$files /etc/fstab"
5cf148
5cf148
	# Check for any updated extra module
5cf148
	EXTRA_MODULES="$(kdump_get_conf_val extra_modules)"
5cf148
	if [[ -n $EXTRA_MODULES ]]; then
5cf148
		if [[ -e /lib/modules/$KDUMP_KERNELVER/modules.dep ]]; then
5cf148
			files="$files /lib/modules/$KDUMP_KERNELVER/modules.dep"
5cf148
		fi
5cf148
		for _module in $EXTRA_MODULES; do
5cf148
			if _module_file="$(modinfo --set-version "$KDUMP_KERNELVER" --filename "$_module" 2> /dev/null)"; then
5cf148
				files="$files $_module_file"
5cf148
				for _dep_modules in $(modinfo -F depends "$_module" | tr ',' ' '); do
5cf148
					files="$files $(modinfo --set-version "$KDUMP_KERNELVER" --filename "$_dep_modules" 2> /dev/null)"
5cf148
				done
5cf148
			else
5cf148
				# If it's not a module nor builtin, give an error
5cf148
				if ! (modprobe --set-version "$KDUMP_KERNELVER" --dry-run "$_module" &> /dev/null); then
5cf148
					dwarn "Module $_module not found"
5cf148
				fi
5cf148
			fi
5cf148
		done
5cf148
	fi
5cf148
5cf148
	# HOOKS is mandatory and need to check the modification time
5cf148
	files="$files $HOOKS"
4e4da3
	is_lvm2_thinp_dump_target && files="$files $LVM_CONF"
5cf148
	check_exist "$files" && check_executable "$EXTRA_BINS" || return 2
5cf148
5cf148
	for file in $files; do
5cf148
		if [[ -e $file ]]; then
5cf148
			time_stamp=$(stat -c "%Y" "$file")
5cf148
			if [[ $time_stamp -gt $image_time ]]; then
5cf148
				modified_files="$modified_files $file"
5cf148
			fi
5cf148
			if [[ -L $file ]]; then
5cf148
				file=$(readlink -m "$file")
5cf148
				time_stamp=$(stat -c "%Y" "$file")
5cf148
				if [[ $time_stamp -gt $image_time ]]; then
5cf148
					modified_files="$modified_files $file"
5cf148
				fi
5cf148
			fi
5cf148
		else
5cf148
			dwarn "$file doesn't exist"
5cf148
		fi
5cf148
	done
5cf148
5cf148
	if [[ -n $modified_files ]]; then
5cf148
		dinfo "Detected change(s) in the following file(s): $modified_files"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
check_drivers_modified()
5cf148
{
5cf148
	local _target _new_drivers _old_drivers _module_name _module_filename
5cf148
5cf148
	# If it's dump target is on block device, detect the block driver
5cf148
	_target=$(get_block_dump_target)
5cf148
	if [[ -n $_target ]]; then
5cf148
		_record_block_drivers()
5cf148
		{
5cf148
			local _drivers
5cf148
			_drivers=$(udevadm info -a "/dev/block/$1" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p')
5cf148
			for _driver in $_drivers; do
5cf148
				if ! [[ " $_new_drivers " == *" $_driver "* ]]; then
5cf148
					_new_drivers="$_new_drivers $_driver"
5cf148
				fi
5cf148
			done
5cf148
5cf148
			ddebug "MAJ:MIN=$1 drivers='$_drivers'"
5cf148
		}
5cf148
		check_block_and_slaves_all _record_block_drivers "$(get_maj_min "$_target")"
5cf148
	fi
5cf148
5cf148
	# Include watchdog drivers if watchdog module is not omitted
5cf148
	is_dracut_mod_omitted watchdog || _new_drivers+=" $(get_watchdog_drvs)"
5cf148
	[[ -z $_new_drivers ]] && return 0
5cf148
5cf148
	if is_fadump_capable; then
5cf148
		_old_drivers="$(lsinitrd "$TARGET_INITRD" -f /usr/lib/dracut/fadump-kernel-modules.txt | tr '\n' ' ')"
5cf148
	else
5cf148
		_old_drivers="$(lsinitrd "$TARGET_INITRD" -f /usr/lib/dracut/hostonly-kernel-modules.txt | tr '\n' ' ')"
5cf148
	fi
5cf148
5cf148
	ddebug "Modules required for kdump: '$_new_drivers'"
5cf148
	ddebug "Modules included in old initramfs: '$_old_drivers'"
5cf148
	for _driver in $_new_drivers; do
5cf148
		# Skip deprecated/invalid driver name or built-in module
5cf148
		_module_name=$(modinfo --set-version "$KDUMP_KERNELVER" -F name "$_driver" 2> /dev/null)
5cf148
		_module_filename=$(modinfo --set-version "$KDUMP_KERNELVER" -n "$_driver" 2> /dev/null)
5cf148
		if [[ -z $_module_name ]] || [[ -z $_module_filename ]] || [[ $_module_filename == *"(builtin)"* ]]; then
5cf148
			continue
5cf148
		fi
5cf148
		if ! [[ " $_old_drivers " == *" $_module_name "* ]]; then
5cf148
			dinfo "Detected change in block device driver, new loaded module: $_module_name"
5cf148
			return 1
5cf148
		fi
5cf148
	done
5cf148
}
5cf148
5cf148
check_fs_modified()
5cf148
{
5cf148
	local _old_dev _old_mntpoint _old_fstype
5cf148
	local _new_dev _new_mntpoint _new_fstype
5cf148
	local _target _dracut_args
5cf148
5cf148
	# No need to check in case of mount target specified via "dracut_args".
5cf148
	if is_mount_in_dracut_args; then
5cf148
		return 0
5cf148
	fi
5cf148
5cf148
	# No need to check in case of raw target.
4e4da3
	# Currently we do not check also if ssh/nfs/virtiofs/thinp target is specified
4e4da3
	if is_ssh_dump_target || is_nfs_dump_target || is_raw_dump_target ||
4e4da3
		is_virtiofs_dump_target || is_lvm2_thinp_dump_target; then
5cf148
		return 0
5cf148
	fi
5cf148
5cf148
	_target=$(get_block_dump_target)
5cf148
	_new_fstype=$(get_fs_type_from_target "$_target")
5cf148
	if [[ -z $_target ]] || [[ -z $_new_fstype ]]; then
5cf148
		derror "Dump target is invalid"
5cf148
		return 2
5cf148
	fi
5cf148
5cf148
	ddebug "_target=$_target _new_fstype=$_new_fstype"
5cf148
	_new_dev=$(kdump_get_persistent_dev "$_target")
5cf148
	if [[ -z $_new_dev ]]; then
5cf148
		perror "Get persistent device name failed"
5cf148
		return 2
5cf148
	fi
5cf148
5cf148
	_new_mntpoint="$(get_kdump_mntpoint_from_target "$_target")"
5cf148
	_dracut_args=$(lsinitrd "$TARGET_INITRD" -f usr/lib/dracut/build-parameter.txt)
5cf148
	if [[ -z $_dracut_args ]]; then
5cf148
		dwarn "Warning: No dracut arguments found in initrd"
5cf148
		return 0
5cf148
	fi
5cf148
5cf148
	# if --mount argument present then match old and new target, mount
5cf148
	# point and file system. If any of them mismatches then rebuild
5cf148
	if echo "$_dracut_args" | grep -q "\-\-mount"; then
5cf148
		# shellcheck disable=SC2046
5cf148
		set -- $(echo "$_dracut_args" | awk -F "--mount '" '{print $2}' | cut -d' ' -f1,2,3)
5cf148
		_old_dev=$1
5cf148
		_old_mntpoint=$2
5cf148
		_old_fstype=$3
5cf148
		[[ $_new_dev == "$_old_dev" && $_new_mntpoint == "$_old_mntpoint" && $_new_fstype == "$_old_fstype" ]] && return 0
5cf148
	# otherwise rebuild if target device is not a root device
5cf148
	else
5cf148
		[[ $_target == "$(get_root_fs_device)" ]] && return 0
5cf148
	fi
5cf148
5cf148
	dinfo "Detected change in File System"
5cf148
	return 1
5cf148
}
5cf148
5cf148
# returns 0 if system is not modified
5cf148
# returns 1 if system is modified
5cf148
# returns 2 if system modification is invalid
5cf148
check_system_modified()
5cf148
{
5cf148
	local ret
5cf148
5cf148
	[[ -f $TARGET_INITRD ]] || return 1
5cf148
5cf148
	check_files_modified
5cf148
	ret=$?
5cf148
	if [[ $ret -ne 0 ]]; then
5cf148
		return $ret
5cf148
	fi
5cf148
5cf148
	check_fs_modified
5cf148
	ret=$?
5cf148
	if [[ $ret -ne 0 ]]; then
5cf148
		return $ret
5cf148
	fi
5cf148
5cf148
	check_drivers_modified
5cf148
	ret=$?
5cf148
	if [[ $ret -ne 0 ]]; then
5cf148
		return $ret
5cf148
	fi
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
check_rebuild()
5cf148
{
5cf148
	local capture_capable_initrd="1"
5cf148
	local force_rebuild force_no_rebuild
5cf148
	local ret system_modified="0"
5cf148
5cf148
	setup_initrd || return 1
5cf148
5cf148
	force_no_rebuild=$(kdump_get_conf_val force_no_rebuild)
5cf148
	force_no_rebuild=${force_no_rebuild:-0}
5cf148
	if [[ $force_no_rebuild != "0" ]] && [[ $force_no_rebuild != "1" ]]; then
5cf148
		derror "Error: force_no_rebuild value is invalid"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	force_rebuild=$(kdump_get_conf_val force_rebuild)
5cf148
	force_rebuild=${force_rebuild:-0}
5cf148
	if [[ $force_rebuild != "0" ]] && [[ $force_rebuild != "1" ]]; then
5cf148
		derror "Error: force_rebuild value is invalid"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	if [[ $force_no_rebuild == "1" && $force_rebuild == "1" ]]; then
5cf148
		derror "Error: force_rebuild and force_no_rebuild are enabled simultaneously in kdump.conf"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	# Will not rebuild kdump initrd
5cf148
	if [[ $force_no_rebuild == "1" ]]; then
5cf148
		return 0
5cf148
	fi
5cf148
5cf148
	#check to see if dependent files has been modified
5cf148
	#since last build of the image file
5cf148
	if [[ -f $TARGET_INITRD ]]; then
5cf148
		image_time=$(stat -c "%Y" "$TARGET_INITRD" 2> /dev/null)
5cf148
5cf148
		#in case of fadump mode, check whether the default/target
5cf148
		#initrd is already built with dump capture capability
5cf148
		if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
5cf148
			capture_capable_initrd=$(lsinitrd -f $DRACUT_MODULES_FILE "$TARGET_INITRD" | grep -c -e ^kdumpbase$ -e ^zz-fadumpinit$)
5cf148
		fi
5cf148
	fi
5cf148
5cf148
	check_system_modified
5cf148
	ret=$?
5cf148
	if [[ $ret -eq 2 ]]; then
5cf148
		return 1
5cf148
	elif [[ $ret -eq 1 ]]; then
5cf148
		system_modified="1"
5cf148
	fi
5cf148
5cf148
	if [[ $image_time -eq 0 ]]; then
5cf148
		dinfo "No kdump initial ramdisk found."
5cf148
	elif [[ $capture_capable_initrd == "0" ]]; then
5cf148
		dinfo "Rebuild $TARGET_INITRD with dump capture support"
5cf148
	elif [[ $force_rebuild != "0" ]]; then
5cf148
		dinfo "Force rebuild $TARGET_INITRD"
5cf148
	elif [[ $system_modified != "0" ]]; then
5cf148
		:
5cf148
	else
5cf148
		return 0
5cf148
	fi
5cf148
5cf148
	dinfo "Rebuilding $TARGET_INITRD"
5cf148
	rebuild_initrd
5cf148
	return $?
5cf148
}
5cf148
5cf148
# On ppc64le LPARs, the keys trusted by firmware do not end up in
5cf148
# .builtin_trusted_keys. So instead, add the key to the .ima keyring
5cf148
function load_kdump_kernel_key()
5cf148
{
5cf148
	# this is only called inside is_secure_boot_enforced,
5cf148
	# no need to retest
5cf148
5cf148
        # this is only required if DT /ibm,secure-boot is a file.
5cf148
        # if it is a dir, we are on OpenPower and don't need this.
5cf148
        if ! [[ -f /proc/device-tree/ibm,secure-boot ]]; then
5cf148
                return
5cf148
        fi
5cf148
5cf148
	KDUMP_KEY_ID=$(keyctl padd asymmetric kernelkey-$RANDOM %:.ima < "/usr/share/doc/kernel-keys/$KDUMP_KERNELVER/kernel-signing-ppc.cer")
5cf148
}
5cf148
5cf148
# remove a previously loaded key. There's no real security implication
5cf148
# to leaving it around, we choose to do this because it makes it easier
5cf148
# to be idempotent and so as to reduce the potential for confusion.
5cf148
function remove_kdump_kernel_key()
5cf148
{
5cf148
	if [[ -z $KDUMP_KEY_ID ]]; then
5cf148
		return
5cf148
	fi
5cf148
5cf148
	keyctl unlink "$KDUMP_KEY_ID" %:.ima
5cf148
}
5cf148
5cf148
# Load the kdump kernel specified in /etc/sysconfig/kdump
5cf148
# If none is specified, try to load a kdump kernel with the same version
5cf148
# as the currently running kernel.
5cf148
load_kdump()
5cf148
{
5cf148
	local ret
5cf148
5cf148
	KEXEC_ARGS=$(prepare_kexec_args "${KEXEC_ARGS}")
5cf148
	KDUMP_COMMANDLINE=$(prepare_cmdline "${KDUMP_COMMANDLINE}" "${KDUMP_COMMANDLINE_REMOVE}" "${KDUMP_COMMANDLINE_APPEND}")
5cf148
5cf148
	# For secureboot enabled machines, use new kexec file based syscall.
5cf148
	# Old syscall will always fail as it does not have capability to
5cf148
	# to kernel signature verification.
5cf148
	if is_secure_boot_enforced; then
5cf148
		dinfo "Secure Boot is enabled. Using kexec file based syscall."
5cf148
		KEXEC_ARGS="$KEXEC_ARGS -s"
5cf148
		load_kdump_kernel_key
5cf148
	fi
5cf148
5cf148
	ddebug "$KEXEC $KEXEC_ARGS $standard_kexec_args --command-line=$KDUMP_COMMANDLINE --initrd=$TARGET_INITRD $KDUMP_KERNEL"
5cf148
5cf148
	# The '12' represents an intermediate temporary file descriptor
5cf148
	# to store the standard error file descriptor '2', and later
5cf148
	# restore the error file descriptor with the file descriptor '12'
5cf148
	# and release it.
5cf148
	exec 12>&2
5cf148
	exec 2>> $KDUMP_LOG_PATH/kdump.log
4e4da3
	chmod 600 $KDUMP_LOG_PATH/kdump.log
5cf148
	PS4='+ $(date "+%Y-%m-%d %H:%M:%S") ${BASH_SOURCE}@${LINENO}: '
5cf148
	set -x
5cf148
5cf148
	# shellcheck disable=SC2086
5cf148
	$KEXEC $KEXEC_ARGS $standard_kexec_args \
5cf148
		--command-line="$KDUMP_COMMANDLINE" \
5cf148
		--initrd="$TARGET_INITRD" "$KDUMP_KERNEL"
5cf148
5cf148
	ret=$?
5cf148
	set +x
5cf148
	exec 2>&12 12>&-
5cf148
5cf148
	remove_kdump_kernel_key
5cf148
5cf148
	if [[ $ret == 0 ]]; then
5cf148
		dinfo "kexec: loaded kdump kernel"
5cf148
		return 0
5cf148
	else
5cf148
		derror "kexec: failed to load kdump kernel"
5cf148
		return 1
5cf148
	fi
5cf148
}
5cf148
5cf148
check_ssh_config()
5cf148
{
5cf148
	local SSH_TARGET
5cf148
5cf148
	while read -r config_opt config_val; do
5cf148
		case "$config_opt" in
5cf148
		sshkey)
5cf148
			# remove inline comments after the end of a directive.
5cf148
			if [[ -f $config_val ]]; then
5cf148
				# canonicalize the path
5cf148
				SSH_KEY_LOCATION=$(/usr/bin/readlink -m "$config_val")
5cf148
			else
5cf148
				dwarn "WARNING: '$config_val' doesn't exist, using default value '$SSH_KEY_LOCATION'"
5cf148
			fi
5cf148
			;;
5cf148
		path)
5cf148
			SAVE_PATH=$config_val
5cf148
			;;
5cf148
		ssh)
5cf148
			DUMP_TARGET=$config_val
5cf148
			;;
5cf148
		*) ;;
5cf148
5cf148
		esac
5cf148
	done <<< "$(kdump_read_conf)"
5cf148
5cf148
	#make sure they've configured kdump.conf for ssh dumps
5cf148
	SSH_TARGET=$(echo -n "$DUMP_TARGET" | sed -n '/.*@/p')
5cf148
	if [[ -z $SSH_TARGET ]]; then
5cf148
		return 1
5cf148
	fi
5cf148
	return 0
5cf148
}
5cf148
5cf148
# ipv6 host address may takes a long time to be ready.
5cf148
# Instead of checking against ipv6 address, we just check the network reachable
5cf148
# by the return val of 'ssh'
5cf148
check_and_wait_network_ready()
5cf148
{
5cf148
	local start_time
5cf148
	local warn_once=1
5cf148
	local cur
5cf148
	local diff
5cf148
	local retval
5cf148
	local errmsg
5cf148
5cf148
	start_time=$(date +%s)
5cf148
	while true; do
5cf148
		errmsg=$(ssh -i "$SSH_KEY_LOCATION" -o BatchMode=yes "$DUMP_TARGET" mkdir -p "$SAVE_PATH" 2>&1)
5cf148
		retval=$?
5cf148
5cf148
		# ssh exits with the exit status of the remote command or with 255 if an error occurred
5cf148
		if [[ $retval -eq 0 ]]; then
5cf148
			return 0
5cf148
		elif [[ $retval -ne 255 ]]; then
5cf148
			derror "Could not create $DUMP_TARGET:$SAVE_PATH, you should check the privilege on server side"
5cf148
			return 1
5cf148
		fi
5cf148
5cf148
		# if server removes the authorized_keys or, no /root/.ssh/kdump_id_rsa
5cf148
		ddebug "$errmsg"
5cf148
		if echo "$errmsg" | grep -q "Permission denied\|No such file or directory\|Host key verification failed"; then
5cf148
			derror "Could not create $DUMP_TARGET:$SAVE_PATH, you probably need to run \"kdumpctl propagate\""
5cf148
			return 1
5cf148
		fi
5cf148
5cf148
		if [[ $warn_once -eq 1 ]]; then
5cf148
			dwarn "Network dump target is not usable, waiting for it to be ready..."
5cf148
			warn_once=0
5cf148
		fi
5cf148
5cf148
		cur=$(date +%s)
5cf148
		diff=$((cur - start_time))
5cf148
		# 60s time out
5cf148
		if [[ $diff -gt 180 ]]; then
5cf148
			break
5cf148
		fi
5cf148
		sleep 1
5cf148
	done
5cf148
5cf148
	dinfo "Could not create $DUMP_TARGET:$SAVE_PATH, ipaddr is not ready yet. You should check network connection"
5cf148
	return 1
5cf148
}
5cf148
5cf148
check_ssh_target()
5cf148
{
5cf148
	check_and_wait_network_ready
5cf148
}
5cf148
5cf148
propagate_ssh_key()
5cf148
{
5cf148
	if ! check_ssh_config; then
5cf148
		derror "No ssh config specified in $KDUMP_CONFIG_FILE.  Can't propagate"
5cf148
		exit 1
5cf148
	fi
5cf148
5cf148
	local KEYFILE=$SSH_KEY_LOCATION
5cf148
	local errmsg="Failed to propagate ssh key"
5cf148
5cf148
	#Check to see if we already created key, if not, create it.
5cf148
	if [[ -f $KEYFILE ]]; then
5cf148
		dinfo "Using existing keys..."
5cf148
	else
5cf148
		dinfo "Generating new ssh keys... "
5cf148
		/usr/bin/ssh-keygen -t rsa -f "$KEYFILE" -N "" 2>&1 > /dev/null
5cf148
		dinfo "done."
5cf148
	fi
5cf148
5cf148
	#now find the target ssh user and server to contact.
5cf148
	SSH_USER=$(echo "$DUMP_TARGET" | cut -d@ -f1)
5cf148
	SSH_SERVER=$(echo "$DUMP_TARGET" | sed -e's/\(.*@\)\(.*$\)/\2/')
5cf148
5cf148
	#now send the found key to the found server
5cf148
	ssh-copy-id -i "$KEYFILE" "$SSH_USER@$SSH_SERVER"
5cf148
	RET=$?
5cf148
	if [[ $RET == 0 ]]; then
5cf148
		dinfo "$KEYFILE has been added to ~$SSH_USER/.ssh/authorized_keys on $SSH_SERVER"
5cf148
		return 0
5cf148
	else
5cf148
		derror "$errmsg, $KEYFILE failed in transfer to $SSH_SERVER"
5cf148
		exit 1
5cf148
	fi
5cf148
}
5cf148
5cf148
show_reserved_mem()
5cf148
{
5cf148
	local mem
5cf148
	local mem_mb
5cf148
5cf148
	mem=$(< /sys/kernel/kexec_crash_size)
5cf148
	mem_mb=$((mem / 1024 / 1024))
5cf148
5cf148
	dinfo "Reserved ${mem_mb}MB memory for crash kernel"
5cf148
}
5cf148
5cf148
check_current_fadump_status()
5cf148
{
5cf148
	# Check if firmware-assisted dump has been registered.
5cf148
	rc=$(< $FADUMP_REGISTER_SYS_NODE)
5cf148
	[[ $rc -eq 1 ]] && return 0
5cf148
	return 1
5cf148
}
5cf148
5cf148
check_current_status()
5cf148
{
5cf148
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
5cf148
		check_current_fadump_status
5cf148
	else
5cf148
		check_current_kdump_status
5cf148
	fi
5cf148
5cf148
	return $?
5cf148
}
5cf148
5cf148
save_raw()
5cf148
{
5cf148
	local kdump_dir
5cf148
	local raw_target
5cf148
5cf148
	raw_target=$(kdump_get_conf_val raw)
5cf148
	[[ -z $raw_target ]] && return 0
5cf148
	[[ -b $raw_target ]] || {
5cf148
		derror "raw partition $raw_target not found"
5cf148
		return 1
5cf148
	}
5cf148
	check_fs=$(lsblk --nodeps -npo FSTYPE "$raw_target")
5cf148
	if [[ $(echo "$check_fs" | wc -w) -ne 0 ]]; then
5cf148
		dwarn "Warning: Detected '$check_fs' signature on $raw_target, data loss is expected."
5cf148
		return 0
5cf148
	fi
5cf148
	kdump_dir=$(kdump_get_conf_val path)
5cf148
	if [[ -z ${kdump_dir} ]]; then
5cf148
		coredir="/var/crash/$(date +"%Y-%m-%d-%H:%M")"
5cf148
	else
5cf148
		coredir="${kdump_dir}/$(date +"%Y-%m-%d-%H:%M")"
5cf148
	fi
5cf148
5cf148
	mkdir -p "$coredir"
5cf148
	[[ -d $coredir ]] || {
5cf148
		derror "failed to create $coredir"
5cf148
		return 1
5cf148
	}
5cf148
	if makedumpfile -R "$coredir/vmcore" < "$raw_target" > /dev/null 2>&1; then
5cf148
		# dump found
5cf148
		dinfo "Dump saved to $coredir/vmcore"
5cf148
		# wipe makedumpfile header
5cf148
		dd if=/dev/zero of="$raw_target" bs=1b count=1 2> /dev/null
5cf148
	else
5cf148
		rm -rf "$coredir"
5cf148
	fi
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
local_fs_dump_target()
5cf148
{
5cf148
	local _target
5cf148
5cf148
	if _target=$(grep -E "^ext[234]|^xfs|^btrfs|^minix" /etc/kdump.conf); then
5cf148
		echo "$_target" | awk '{print $2}'
5cf148
	fi
5cf148
}
5cf148
5cf148
path_to_be_relabeled()
5cf148
{
5cf148
	local _path _target _mnt="/" _rmnt
5cf148
5cf148
	if is_user_configured_dump_target; then
5cf148
		if is_mount_in_dracut_args; then
5cf148
			return
5cf148
		fi
5cf148
5cf148
		_target=$(local_fs_dump_target)
5cf148
		if [[ -n $_target ]]; then
5cf148
			_mnt=$(get_mntpoint_from_target "$_target")
5cf148
			if ! is_mounted "$_mnt"; then
5cf148
				return
5cf148
			fi
5cf148
		else
5cf148
			return
5cf148
		fi
5cf148
	fi
5cf148
5cf148
	_path=$(get_save_path)
5cf148
	# if $_path is masked by other mount, we will not relabel it.
5cf148
	_rmnt=$(df "$_mnt/$_path" 2> /dev/null | tail -1 | awk '{ print $NF }')
5cf148
	if [[ $_rmnt == "$_mnt" ]]; then
5cf148
		echo "$_mnt/$_path"
5cf148
	fi
5cf148
}
5cf148
5cf148
selinux_relabel()
5cf148
{
5cf148
	local _path _i _attr
5cf148
5cf148
	_path=$(path_to_be_relabeled)
5cf148
	if [[ -z $_path ]] || ! [[ -d $_path ]]; then
5cf148
		return
5cf148
	fi
5cf148
5cf148
	while IFS= read -r -d '' _i; do
5cf148
		_attr=$(getfattr -m "security.selinux" "$_i" 2> /dev/null)
5cf148
		if [[ -z $_attr ]]; then
5cf148
			restorecon "$_i"
5cf148
		fi
5cf148
	done < <(find "$_path" -print0)
5cf148
}
5cf148
5cf148
check_fence_kdump_config()
5cf148
{
5cf148
	local hostname
5cf148
	local ipaddrs
5cf148
	local nodes
5cf148
5cf148
	hostname=$(hostname)
5cf148
	ipaddrs=$(hostname -I)
5cf148
	nodes=$(kdump_get_conf_val "fence_kdump_nodes")
5cf148
5cf148
	for node in $nodes; do
5cf148
		if [[ $node == "$hostname" ]]; then
5cf148
			derror "Option fence_kdump_nodes cannot contain $hostname"
5cf148
			return 1
5cf148
		fi
5cf148
		# node can be ipaddr
5cf148
		if echo "$ipaddrs " | grep -q "$node "; then
5cf148
			derror "Option fence_kdump_nodes cannot contain $node"
5cf148
			return 1
5cf148
		fi
5cf148
	done
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
check_dump_feasibility()
5cf148
{
5cf148
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
5cf148
		return 0
5cf148
	fi
5cf148
5cf148
	check_kdump_feasibility
5cf148
	return $?
5cf148
}
5cf148
5cf148
start_fadump()
5cf148
{
5cf148
	echo 1 > $FADUMP_REGISTER_SYS_NODE
5cf148
	if ! check_current_fadump_status; then
5cf148
		derror "fadump: failed to register"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	dinfo "fadump: registered successfully"
5cf148
	return 0
5cf148
}
5cf148
5cf148
start_dump()
5cf148
{
5cf148
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
5cf148
		start_fadump
5cf148
	else
5cf148
		load_kdump
5cf148
	fi
5cf148
5cf148
	return $?
5cf148
}
5cf148
5cf148
check_failure_action_config()
5cf148
{
5cf148
	local default_option
5cf148
	local failure_action
5cf148
	local option="failure_action"
5cf148
5cf148
	default_option=$(kdump_get_conf_val default)
5cf148
	failure_action=$(kdump_get_conf_val failure_action)
5cf148
5cf148
	if [[ -z $failure_action ]] && [[ -z $default_option ]]; then
5cf148
		return 0
5cf148
	elif [[ -n $failure_action ]] && [[ -n $default_option ]]; then
5cf148
		derror "Cannot specify 'failure_action' and 'default' option together"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	if [[ -n $default_option ]]; then
5cf148
		option="default"
5cf148
		failure_action="$default_option"
5cf148
	fi
5cf148
5cf148
	case "$failure_action" in
5cf148
	reboot | halt | poweroff | shell | dump_to_rootfs)
5cf148
		return 0
5cf148
		;;
5cf148
	*)
5cf148
		dinfo $"Usage kdump.conf: $option {reboot|halt|poweroff|shell|dump_to_rootfs}"
5cf148
		return 1
5cf148
		;;
5cf148
	esac
5cf148
}
5cf148
5cf148
check_final_action_config()
5cf148
{
5cf148
	local final_action
5cf148
5cf148
	final_action=$(kdump_get_conf_val final_action)
5cf148
	if [[ -z $final_action ]]; then
5cf148
		return 0
5cf148
	else
5cf148
		case "$final_action" in
5cf148
		reboot | halt | poweroff)
5cf148
			return 0
5cf148
			;;
5cf148
		*)
5cf148
			dinfo $"Usage kdump.conf: final_action {reboot|halt|poweroff}"
5cf148
			return 1
5cf148
			;;
5cf148
		esac
5cf148
	fi
5cf148
}
5cf148
5cf148
start()
5cf148
{
5cf148
	if ! check_dump_feasibility; then
5cf148
		derror "Starting kdump: [FAILED]"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	if ! check_config; then
5cf148
		derror "Starting kdump: [FAILED]"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	if sestatus 2> /dev/null | grep -q "SELinux status.*enabled"; then
5cf148
		selinux_relabel
5cf148
	fi
5cf148
5cf148
	if ! save_raw; then
5cf148
		derror "Starting kdump: [FAILED]"
5cf148
		return 1
5cf148
	fi
5cf148
4e4da3
	if [[ $DEFAULT_DUMP_MODE == "kdump" ]] && check_current_kdump_status; then
5cf148
		dwarn "Kdump already running: [WARNING]"
5cf148
		return 0
5cf148
	fi
5cf148
5cf148
	if check_ssh_config; then
5cf148
		if ! check_ssh_target; then
5cf148
			derror "Starting kdump: [FAILED]"
5cf148
			return 1
5cf148
		fi
5cf148
	fi
5cf148
5cf148
	if ! check_rebuild; then
5cf148
		derror "Starting kdump: [FAILED]"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	if ! start_dump; then
5cf148
		derror "Starting kdump: [FAILED]"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	dinfo "Starting kdump: [OK]"
5cf148
}
5cf148
5cf148
reload()
5cf148
{
5cf148
	if ! check_current_status; then
5cf148
		dwarn "Kdump was not running: [WARNING]"
5cf148
	fi
5cf148
5cf148
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
5cf148
		reload_fadump
5cf148
		return $?
5cf148
	else
5cf148
		if ! stop_kdump; then
5cf148
			derror "Stopping kdump: [FAILED]"
5cf148
			return 1
5cf148
		fi
5cf148
	fi
5cf148
5cf148
	dinfo "Stopping kdump: [OK]"
5cf148
5cf148
	if ! setup_initrd; then
5cf148
		derror "Starting kdump: [FAILED]"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	if ! start_dump; then
5cf148
		derror "Starting kdump: [FAILED]"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	dinfo "Starting kdump: [OK]"
5cf148
}
5cf148
5cf148
stop_fadump()
5cf148
{
5cf148
	echo 0 > $FADUMP_REGISTER_SYS_NODE
5cf148
	if check_current_fadump_status; then
5cf148
		derror "fadump: failed to unregister"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	dinfo "fadump: unregistered successfully"
5cf148
	return 0
5cf148
}
5cf148
5cf148
stop_kdump()
5cf148
{
5cf148
	if is_secure_boot_enforced; then
5cf148
		$KEXEC -s -p -u
5cf148
	else
5cf148
		$KEXEC -p -u
5cf148
	fi
5cf148
5cf148
	# shellcheck disable=SC2181
5cf148
	if [[ $? != 0 ]]; then
5cf148
		derror "kexec: failed to unload kdump kernel"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	dinfo "kexec: unloaded kdump kernel"
5cf148
	return 0
5cf148
}
5cf148
5cf148
reload_fadump()
5cf148
{
5cf148
	if echo 1 > $FADUMP_REGISTER_SYS_NODE; then
5cf148
		dinfo "fadump: re-registered successfully"
5cf148
		return 0
5cf148
	else
5cf148
		# FADump could fail on older kernel where re-register
5cf148
		# support is not enabled. Try stop/start from userspace
5cf148
		# to handle such scenario.
5cf148
		if stop_fadump; then
5cf148
			start_fadump
5cf148
			return $?
5cf148
		fi
5cf148
	fi
5cf148
5cf148
	return 1
5cf148
}
5cf148
5cf148
stop()
5cf148
{
5cf148
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
5cf148
		stop_fadump
5cf148
	else
5cf148
		stop_kdump
5cf148
	fi
5cf148
5cf148
	# shellcheck disable=SC2181
5cf148
	if [[ $? != 0 ]]; then
5cf148
		derror "Stopping kdump: [FAILED]"
5cf148
		return 1
5cf148
	fi
5cf148
5cf148
	dinfo "Stopping kdump: [OK]"
5cf148
	return 0
5cf148
}
5cf148
5cf148
rebuild()
5cf148
{
5cf148
	check_config || return 1
5cf148
5cf148
	if check_ssh_config; then
5cf148
		if ! check_ssh_target; then
5cf148
			return 1
5cf148
		fi
5cf148
	fi
5cf148
5cf148
	setup_initrd || return 1
5cf148
5cf148
	dinfo "Rebuilding $TARGET_INITRD"
5cf148
	rebuild_initrd
5cf148
	return $?
5cf148
}
5cf148
5cf148
do_estimate()
5cf148
{
5cf148
	local kdump_mods
5cf148
	local -A large_mods
5cf148
	local baseline
4e4da3
	local kernel_size mod_size initrd_size baseline_size runtime_size reserved_size estimated_size recommended_size _cryptsetup_overhead
5cf148
	local size_mb=$((1024 * 1024))
5cf148
5cf148
	setup_initrd
5cf148
	if [[ ! -f $TARGET_INITRD ]]; then
5cf148
		derror "kdumpctl estimate: kdump initramfs is not built yet."
5cf148
		exit 1
5cf148
	fi
5cf148
5cf148
	kdump_mods="$(lsinitrd "$TARGET_INITRD" -f /usr/lib/dracut/hostonly-kernel-modules.txt | tr '\n' ' ')"
5cf148
	baseline=$(kdump_get_arch_recommend_size)
5cf148
	if [[ ${baseline: -1} == "M" ]]; then
5cf148
		baseline=${baseline%M}
5cf148
	elif [[ ${baseline: -1} == "G" ]]; then
5cf148
		baseline=$((${baseline%G} * 1024))
5cf148
	elif [[ ${baseline: -1} == "T" ]]; then
5cf148
		baseline=$((${baseline%Y} * 1048576))
5cf148
	fi
5cf148
5cf148
	# The default pre-reserved crashkernel value
5cf148
	baseline_size=$((baseline * size_mb))
5cf148
	# Current reserved crashkernel size
5cf148
	reserved_size=$(< /sys/kernel/kexec_crash_size)
5cf148
	# A pre-estimated value for userspace usage and kernel
5cf148
	# runtime allocation, 64M should good for most cases
5cf148
	runtime_size=$((64 * size_mb))
5cf148
	# Kernel image size
5cf148
	kernel_size=$(get_kernel_size "$KDUMP_KERNEL")
5cf148
	# Kdump initramfs size
5cf148
	initrd_size=$(du -b "$TARGET_INITRD" | awk '{print $1}')
5cf148
	# Kernel modules static size after loaded
5cf148
	mod_size=0
5cf148
	while read -r _name _size _; do
5cf148
		if [[ " $kdump_mods " != *" $_name "* ]]; then
5cf148
			continue
5cf148
		fi
5cf148
		mod_size=$((mod_size + _size))
5cf148
5cf148
		# Mark module with static size larger than 2M as large module
5cf148
		if [[ $((_size / size_mb)) -ge 1 ]]; then
5cf148
			large_mods[$_name]=$_size
5cf148
		fi
5cf148
	done <<< "$(< /proc/modules)"
5cf148
5cf148
	# Extra memory usage required for LUKS2 decryption
5cf148
	crypt_size=0
5cf148
	for _dev in $(get_all_kdump_crypt_dev); do
5cf148
		_crypt_info=$(cryptsetup luksDump "/dev/block/$_dev")
5cf148
		[[ $(echo "$_crypt_info" | sed -n "s/^Version:\s*\(.*\)/\1/p") == "2" ]] || continue
4e4da3
		for _mem in $(echo "$_crypt_info" | sed -n "s/\sMemory:\s*\(.*\)/\1/p" | sort -n -r); do
5cf148
			crypt_size=$((crypt_size + _mem * 1024))
5cf148
			break
5cf148
		done
5cf148
	done
4e4da3
4e4da3
	if [[ $crypt_size -ne 0 ]]; then
4e4da3
		if [[ $(uname -m) == aarch64 ]]; then
4e4da3
			_cryptsetup_overhead=50
4e4da3
		else
4e4da3
			_cryptsetup_overhead=20
4e4da3
		fi
4e4da3
4e4da3
		crypt_size=$((crypt_size + _cryptsetup_overhead * size_mb))
4e4da3
		echo -e "Encrypted kdump target requires extra memory, assuming using the keyslot with maximum memory requirement\n"
4e4da3
	fi
5cf148
5cf148
	estimated_size=$((kernel_size + mod_size + initrd_size + runtime_size + crypt_size))
5cf148
	if [[ $baseline_size -gt $estimated_size ]]; then
5cf148
		recommended_size=$baseline_size
5cf148
	else
5cf148
		recommended_size=$estimated_size
5cf148
	fi
5cf148
5cf148
	echo "Reserved crashkernel:    $((reserved_size / size_mb))M"
5cf148
	echo "Recommended crashkernel: $((recommended_size / size_mb))M"
5cf148
	echo
5cf148
	echo "Kernel image size:   $((kernel_size / size_mb))M"
5cf148
	echo "Kernel modules size: $((mod_size / size_mb))M"
5cf148
	echo "Initramfs size:      $((initrd_size / size_mb))M"
5cf148
	echo "Runtime reservation: $((runtime_size / size_mb))M"
5cf148
	[[ $crypt_size -ne 0 ]] &&
5cf148
		echo "LUKS required size:  $((crypt_size / size_mb))M"
5cf148
	echo -n "Large modules:"
5cf148
	if [[ ${#large_mods[@]} -eq 0 ]]; then
5cf148
		echo " <none>"
5cf148
	else
5cf148
		echo ""
5cf148
		for _mod in "${!large_mods[@]}"; do
5cf148
			echo "    $_mod: ${large_mods[$_mod]}"
5cf148
		done
5cf148
	fi
5cf148
5cf148
	if [[ $reserved_size -lt $recommended_size ]]; then
5cf148
		echo "WARNING: Current crashkernel size is lower than recommended size $((recommended_size / size_mb))M."
5cf148
	fi
5cf148
}
5cf148
5cf148
get_default_crashkernel()
5cf148
{
5cf148
	local _dump_mode=$1
5cf148
5cf148
	kdump_get_arch_recommend_crashkernel "$_dump_mode"
5cf148
}
5cf148
5cf148
# Read kernel cmdline parameter for a specific kernel
5cf148
# $1: kernel path, DEFAULT or kernel path, ALL not accepted
5cf148
# $2: kernel cmldine parameter
5cf148
get_grub_kernel_boot_parameter()
5cf148
{
5cf148
	local _kernel_path=$1 _para=$2
5cf148
5cf148
	[[ $_kernel_path == ALL ]] && derror "kernel_path=ALL invalid for get_grub_kernel_boot_parameter" && return 1
5cf148
	grubby --info="$_kernel_path" | sed -En -e "/^args=.*$/{s/^.*(\s|\")${_para}=(\S*).*\"$/\2/p;q}"
5cf148
}
5cf148
5cf148
# get dump mode by fadump value
5cf148
# return
5cf148
#  - fadump, if fadump=on or fadump=nocma
5cf148
#  - kdump, if fadump=off or empty fadump, return kdump
5cf148
#  - error if otherwise
5cf148
get_dump_mode_by_fadump_val()
5cf148
{
5cf148
	local _fadump_val=$1
5cf148
5cf148
	if [[ -z $_fadump_val ]] || [[ $_fadump_val == off ]]; then
5cf148
		echo -n kdump
5cf148
	elif [[ $_fadump_val == on ]] || [[ $_fadump_val == nocma ]]; then
5cf148
		echo -n fadump
5cf148
	else
5cf148
		derror "invalid fadump=$_fadump_val"
5cf148
		return 1
5cf148
	fi
5cf148
}
5cf148
5cf148
# get dump mode of a specific kernel
5cf148
# based on its fadump kernel cmdline parameter
5cf148
get_dump_mode_by_kernel()
5cf148
{
5cf148
	local _kernel_path=$1 _fadump_val _dump_mode
5cf148
5cf148
	_fadump_val=$(get_grub_kernel_boot_parameter "$_kernel_path" fadump)
5cf148
	if _dump_mode=$(get_dump_mode_by_fadump_val "$_fadump_val"); then
5cf148
		echo -n "$_dump_mode"
5cf148
	else
5cf148
		derror "failed to get dump mode for kernel $_kernel_path"
5cf148
		exit
5cf148
	fi
5cf148
}
5cf148
5cf148
_filter_grubby_kernel_str()
5cf148
{
5cf148
	local _grubby_kernel_str=$1
5cf148
	echo -n "$_grubby_kernel_str" | sed -n -e 's/^kernel="\(.*\)"/\1/p'
5cf148
}
5cf148
5cf148
_find_kernel_path_by_release()
5cf148
{
5cf148
	local _release="$1" _grubby_kernel_str _kernel_path
4e4da3
	_grubby_kernel_str=$(grubby --info ALL | grep "^kernel=.*$_release\"$")
5cf148
	_kernel_path=$(_filter_grubby_kernel_str "$_grubby_kernel_str")
5cf148
	if [[ -z $_kernel_path ]]; then
5cf148
		derror "kernel $_release doesn't exist"
5cf148
		return 1
5cf148
	fi
5cf148
	echo -n "$_kernel_path"
5cf148
}
5cf148
5cf148
_get_current_running_kernel_path()
5cf148
{
5cf148
	local _release _path
5cf148
5cf148
	_release=$(uname -r)
5cf148
	if _path=$(_find_kernel_path_by_release "$_release"); then
5cf148
		echo -n "$_path"
5cf148
	else
5cf148
		return 1
5cf148
	fi
5cf148
}
5cf148
4e4da3
_update_kernel_cmdline()
5cf148
{
5cf148
	local _kernel_path=$1 _crashkernel=$2 _dump_mode=$3 _fadump_val=$4
5cf148
4e4da3
	if is_ostree; then
5cf148
		if rpm-ostree kargs | grep -q "crashkernel="; then
5cf148
			rpm-ostree kargs --replace="crashkernel=$_crashkernel"
5cf148
		else
5cf148
			rpm-ostree kargs --append="crashkernel=$_crashkernel"
5cf148
		fi
5cf148
	else
4e4da3
		grubby --args "crashkernel=$_crashkernel" --update-kernel "$_kernel_path"
5cf148
		if [[ $_dump_mode == kdump ]]; then
5cf148
			grubby --remove-args="fadump" --update-kernel "$_kernel_path"
5cf148
		else
5cf148
			grubby --args="fadump=$_fadump_val" --update-kernel "$_kernel_path"
5cf148
		fi
5cf148
	fi
4e4da3
	[[ -f /etc/zipl.conf ]] && zipl > /dev/null
5cf148
}
5cf148
5cf148
_valid_grubby_kernel_path()
5cf148
{
5cf148
	[[ -n "$1" ]] && grubby --info="$1" > /dev/null 2>&1
5cf148
}
5cf148
5cf148
# return all the kernel paths given a grubby kernel-path
5cf148
#
5cf148
# $1: kernel path accepted by grubby, e.g. DEFAULT, ALL,
5cf148
#     /boot/vmlinuz-`uname -r`
5cf148
# return: kernel paths separated by space
5cf148
_get_all_kernels_from_grubby()
5cf148
{
5cf148
	local _kernels _line _kernel_path _grubby_kernel_path=$1
5cf148
5cf148
	for _line in $(grubby --info "$_grubby_kernel_path" | grep "^kernel="); do
5cf148
		_kernel_path=$(_filter_grubby_kernel_str "$_line")
5cf148
		_kernels="$_kernels $_kernel_path"
5cf148
	done
5cf148
	echo -n "$_kernels"
5cf148
}
5cf148
5cf148
GRUB_ETC_DEFAULT="/etc/default/grub"
5cf148
# Update a kernel parameter in default grub conf
5cf148
#
5cf148
# If a value is specified, it will be inserted in the end. Otherwise it
5cf148
# would remove given kernel parameter.
5cf148
#
5cf148
# Note this function doesn't address the following cases,
5cf148
# 1. The kernel ignores everything on the command line after a '--'. So
5cf148
#    simply adding the new entry to the end will fail if the cmdline
5cf148
#    contains a --.
5cf148
# 2. If the value for a parameter contains spaces it can be quoted using
5cf148
#    double quotes, for example param="value with spaces". This will
5cf148
#    break the [^[:space:]\"] regex for the value.
5cf148
# 3. Dashes and underscores in the parameter name are equivalent. So
5cf148
#    some_parameter and some-parameter are identical.
5cf148
# 4. Some parameters, e.g. efivar_ssdt, can be given multiple times.
5cf148
# 5. Some kernel parameters, e.g. quiet, doesn't have value
5cf148
#
5cf148
# $1: the name of the kernel command line parameter
5cf148
# $2: new value. If empty, given parameter would be removed
5cf148
_update_kernel_arg_in_grub_etc_default()
5cf148
{
5cf148
	local _para=$1 _val=$2 _para_val
5cf148
4e4da3
	if [[ $(uname -m) == s390x ]]; then
4e4da3
		return
4e4da3
	fi
4e4da3
5cf148
	if [[ -n $_val ]]; then
5cf148
		_para_val="$_para=$_val"
5cf148
	fi
5cf148
5cf148
	# Update the command line /etc/default/grub, i.e.
5cf148
	# on the line that starts with 'GRUB_CMDLINE_LINUX=',
5cf148
	#       1) remove $para=$val if the it's the first arg
5cf148
	#       2) remove all occurences of $para=$val
5cf148
	#       3) insert $_para_val to end
5cf148
	#       4) remove duplicate spaces left over by 1) or 2) or 3)
5cf148
	#       5) remove space at the beginning of the string left over by 1) or 2) or 3)
5cf148
	#       6) remove space at the end of the string left over by 1) or 2) or 3)
5cf148
	sed -i -E "/^GRUB_CMDLINE_LINUX=/ {
5cf148
         s/\"${_para}=[^[:space:]\"]*/\"/g;
5cf148
         s/[[:space:]]+${_para}=[^[:space:]\"]*/ /g;
5cf148
         s/\"$/ ${_para_val}\"/
5cf148
         s/[[:space:]]+/ /g;
5cf148
         s/(\")[[:space:]]+/\1/g;
5cf148
         s/[[:space:]]+(\")/\1/g;
5cf148
         }" "$GRUB_ETC_DEFAULT"
5cf148
}
5cf148
5cf148
# Read the kernel arg in default grub conf.
5cf148
5cf148
# Note reading a kernel parameter that doesn't have a value isn't supported.
5cf148
#
5cf148
# $1: the name of the kernel command line parameter
5cf148
_read_kernel_arg_in_grub_etc_default()
5cf148
{
5cf148
	sed -n -E "s/^GRUB_CMDLINE_LINUX=.*[[:space:]\"]${1}=([^[:space:]\"]*).*$/\1/p" "$GRUB_ETC_DEFAULT"
5cf148
}
5cf148
5cf148
reset_crashkernel()
5cf148
{
5cf148
	local _opt _val _dump_mode _fadump_val _reboot _grubby_kernel_path _kernel _kernels
5cf148
	local _old_crashkernel _new_crashkernel _new_dump_mode _crashkernel_changed
5cf148
	local _new_fadump_val _old_fadump_val _what_is_updated
5cf148
5cf148
	for _opt in "$@"; do
5cf148
		case "$_opt" in
5cf148
			--fadump=*)
5cf148
				_val=${_opt#*=}
5cf148
				if _dump_mode=$(get_dump_mode_by_fadump_val $_val); then
5cf148
					_fadump_val=$_val
5cf148
				else
5cf148
					derror "failed to determine dump mode"
5cf148
					exit
5cf148
				fi
5cf148
				;;
5cf148
			--kernel=*)
5cf148
				_val=${_opt#*=}
5cf148
				if ! _valid_grubby_kernel_path $_val; then
5cf148
					derror "Invalid $_opt, please specify a valid kernel path, ALL or DEFAULT"
5cf148
					exit
5cf148
				fi
5cf148
				_grubby_kernel_path=$_val
5cf148
				;;
5cf148
			--reboot)
5cf148
				_reboot=yes
5cf148
				;;
5cf148
			*)
5cf148
				derror "$_opt not recognized"
5cf148
				exit 1
5cf148
				;;
5cf148
		esac
5cf148
	done
5cf148
4e4da3
	# 1. OSTree systems use "rpm-ostree kargs" instead of grubby to manage kernel command
4e4da3
	#    line. --kernel=ALL doesn't make sense for OStree.
4e4da3
	# 2. We don't have any OSTree POWER systems so the dump mode is always kdump.
5cf148
	# 3. "rpm-ostree kargs" would prompt the user to reboot the system after
5cf148
	#    modifying the kernel command line so there is no need for kexec-tools
5cf148
	#    to repeat it.
4e4da3
	if is_ostree; then
5cf148
		_old_crashkernel=$(rpm-ostree kargs | sed -n -E 's/.*(^|\s)crashkernel=(\S*).*/\2/p')
5cf148
		_new_dump_mode=kdump
5cf148
		_new_crashkernel=$(kdump_get_arch_recommend_crashkernel "$_new_dump_mode")
5cf148
		if [[ $_old_crashkernel != "$_new_crashkernel" ]]; then
4e4da3
			_update_kernel_cmdline "" "$_new_crashkernel" "$_new_dump_mode" ""
5cf148
			if [[ $_reboot == yes ]]; then
5cf148
				systemctl reboot
5cf148
			fi
5cf148
		fi
5cf148
		return
5cf148
	fi
5cf148
5cf148
	# For non-ppc64le systems, the dump mode is always kdump since only ppc64le
5cf148
	# has FADump.
5cf148
	if [[ -z $_dump_mode && $(uname -m) != ppc64le ]]; then
5cf148
		_dump_mode=kdump
5cf148
		_fadump_val=off
5cf148
	fi
5cf148
5cf148
	# If the dump mode is determined, we can also know the default crashkernel value
5cf148
	if [[ -n $_dump_mode ]]; then
5cf148
		_crashkernel=$(kdump_get_arch_recommend_crashkernel "$_dump_mode")
5cf148
	fi
5cf148
5cf148
	# If --kernel-path=ALL, update GRUB_CMDLINE_LINUX in /etc/default/grub.
5cf148
	#
5cf148
	# An exception case is when the ppc64le user doesn't specify the fadump value.
5cf148
	# In this case, the dump mode would be determined by parsing the kernel
5cf148
	# command line of the kernel(s) to be updated thus don't update GRUB_CMDLINE_LINUX.
5cf148
	#
5cf148
	# The following code has been simplified because of what has been done early,
5cf148
	#   - set the dump mode as kdump for non-ppc64le cases
5cf148
	#   - retrieved the default crashkernel value for given dump mode
5cf148
	if [[ $_grubby_kernel_path == ALL && -n $_dump_mode ]]; then
5cf148
		_update_kernel_arg_in_grub_etc_default crashkernel "$_crashkernel"
5cf148
		# remove the fadump if fadump is disabled
5cf148
		if [[ $_fadump_val == off ]]; then
5cf148
			_fadump_val=""
5cf148
		fi
5cf148
		_update_kernel_arg_in_grub_etc_default fadump "$_fadump_val"
5cf148
	fi
5cf148
5cf148
	# If kernel-path not specified, either
5cf148
	#  - use KDUMP_KERNELVER if it's defined
5cf148
	#  - use current running kernel
5cf148
	if [[ -z $_grubby_kernel_path ]]; then
5cf148
		if [[ -z $KDUMP_KERNELVER ]] ||
5cf148
			! _kernel_path=$(_find_kernel_path_by_release "$KDUMP_KERNELVER"); then
5cf148
					if ! _kernel_path=$(_get_current_running_kernel_path); then
5cf148
						derror "no running kernel found"
5cf148
						exit 1
5cf148
					fi
5cf148
		fi
5cf148
		_kernels=$_kernel_path
5cf148
	else
5cf148
		_kernels=$(_get_all_kernels_from_grubby "$_grubby_kernel_path")
5cf148
	fi
5cf148
5cf148
	for _kernel in $_kernels; do
5cf148
		if [[ -z $_dump_mode ]]; then
5cf148
			_new_dump_mode=$(get_dump_mode_by_kernel "$_kernel")
5cf148
			_new_crashkernel=$(kdump_get_arch_recommend_crashkernel "$_new_dump_mode")
5cf148
			_new_fadump_val=$(get_grub_kernel_boot_parameter "$_kernel" fadump)
5cf148
		else
5cf148
			_new_dump_mode=$_dump_mode
5cf148
			_new_crashkernel=$_crashkernel
5cf148
			_new_fadump_val=$_fadump_val
5cf148
		fi
5cf148
5cf148
		_old_crashkernel=$(get_grub_kernel_boot_parameter "$_kernel" crashkernel)
5cf148
		_old_fadump_val=$(get_grub_kernel_boot_parameter "$_kernel" fadump)
5cf148
		if [[ $_old_crashkernel != "$_new_crashkernel" || $_old_fadump_val != "$_new_fadump_val" ]]; then
4e4da3
			_update_kernel_cmdline "$_kernel" "$_new_crashkernel" "$_new_dump_mode" "$_new_fadump_val"
5cf148
			if [[ $_reboot != yes ]]; then
5cf148
				if [[ $_old_crashkernel != "$_new_crashkernel" ]]; then
5cf148
					_what_is_updated="Updated crashkernel=$_new_crashkernel"
5cf148
				else
5cf148
					# This case happens only when switching between fadump=on and fadump=nocma
5cf148
					_what_is_updated="Updated fadump=$_new_fadump_val"
5cf148
				fi
5cf148
				dwarn "$_what_is_updated for kernel=$_kernel. Please reboot the system for the change to take effect."
5cf148
			fi
5cf148
			_crashkernel_changed=yes
5cf148
		fi
5cf148
	done
5cf148
5cf148
	if [[ $_reboot == yes && $_crashkernel_changed == yes ]]; then
5cf148
		reboot
5cf148
	fi
5cf148
}
5cf148
4e4da3
_is_bootloader_installed()
4e4da3
{
4e4da3
	if [[ $(uname -m) == s390x ]]; then
4e4da3
		test -f /etc/zipl.conf
4e4da3
	else
4e4da3
		test -f /boot/grub2/grub.cfg
4e4da3
	fi
4e4da3
}
4e4da3
5cf148
# update the crashkernel value in GRUB_ETC_DEFAULT if necessary
5cf148
#
5cf148
# called by reset_crashkernel_after_update and inherit its array variable
5cf148
# _crashkernel_vals
5cf148
update_crashkernel_in_grub_etc_default_after_update()
5cf148
{
5cf148
	local _crashkernel _fadump_val
5cf148
	local _dump_mode _old_default_crashkernel _new_default_crashkernel
5cf148
4e4da3
	if [[ $(uname -m) == s390x ]]; then
4e4da3
		return
4e4da3
	fi
4e4da3
5cf148
	_crashkernel=$(_read_kernel_arg_in_grub_etc_default crashkernel)
5cf148
5cf148
	if [[ -z $_crashkernel ]]; then
5cf148
		return
5cf148
	fi
5cf148
5cf148
	_fadump_val=$(_read_kernel_arg_in_grub_etc_default fadump)
5cf148
	_dump_mode=$(get_dump_mode_by_fadump_val "$_fadump_val")
5cf148
5cf148
	_old_default_crashkernel=${_crashkernel_vals[old_${_dump_mode}]}
5cf148
	_new_default_crashkernel=${_crashkernel_vals[new_${_dump_mode}]}
5cf148
5cf148
	if [[ $_crashkernel == auto ]] ||
5cf148
		  [[ $_crashkernel == "$_old_default_crashkernel" &&
5cf148
		     $_new_default_crashkernel != "$_old_default_crashkernel" ]]; then
5cf148
			_update_kernel_arg_in_grub_etc_default crashkernel "$_new_default_crashkernel"
5cf148
	fi
5cf148
}
5cf148
5cf148
# shellcheck disable=SC2154 # false positive when dereferencing an array
5cf148
reset_crashkernel_after_update()
5cf148
{
5cf148
	local _kernel _crashkernel _dump_mode _fadump_val _old_default_crashkernel _new_default_crashkernel
5cf148
	declare -A _crashkernel_vals
5cf148
4e4da3
	if ! _is_bootloader_installed; then
4e4da3
		return
4e4da3
	fi
4e4da3
5cf148
	_crashkernel_vals[old_kdump]=$(cat /tmp/old_default_crashkernel 2> /dev/null)
5cf148
	_crashkernel_vals[old_fadump]=$(cat /tmp/old_default_crashkernel_fadump 2> /dev/null)
5cf148
	_crashkernel_vals[new_kdump]=$(get_default_crashkernel kdump)
5cf148
	_crashkernel_vals[new_fadump]=$(get_default_crashkernel fadump)
5cf148
5cf148
	for _kernel in $(_get_all_kernels_from_grubby ALL); do
5cf148
		_crashkernel=$(get_grub_kernel_boot_parameter "$_kernel" crashkernel)
5cf148
		if [[ $_crashkernel == auto ]]; then
5cf148
			reset_crashkernel "--kernel=$_kernel"
5cf148
		elif [[ -n $_crashkernel ]]; then
5cf148
			_dump_mode=$(get_dump_mode_by_kernel "$_kernel")
5cf148
			_old_default_crashkernel=${_crashkernel_vals[old_${_dump_mode}]}
5cf148
			_new_default_crashkernel=${_crashkernel_vals[new_${_dump_mode}]}
5cf148
			if [[ $_crashkernel == "$_old_default_crashkernel" ]] &&
5cf148
				[[ $_new_default_crashkernel != "$_old_default_crashkernel" ]]; then
5cf148
				_fadump_val=$(get_grub_kernel_boot_parameter "$_kernel" fadump)
4e4da3
				if _update_kernel_cmdline "$_kernel" "$_new_default_crashkernel" "$_dump_mode" "$_fadump_val"; then
5cf148
					echo "For kernel=$_kernel, crashkernel=$_new_default_crashkernel now."
5cf148
				fi
5cf148
			fi
5cf148
		fi
5cf148
	done
5cf148
5cf148
	update_crashkernel_in_grub_etc_default_after_update
5cf148
}
5cf148
5cf148
# read the value of an environ variable from given environ file path
5cf148
#
5cf148
# The environment variable entries in /proc/[pid]/environ are separated
5cf148
# by null bytes instead of by spaces.
5cf148
#
5cf148
# $1: environment variable
5cf148
# $2: environ file path
5cf148
read_proc_environ_var()
5cf148
{
5cf148
	local _var=$1 _environ_path=$2
5cf148
	sed -n -E "s/.*(^|\x00)${_var}=([^\x00]*).*/\2/p" < "$_environ_path"
5cf148
}
5cf148
5cf148
_OSBUILD_ENVIRON_PATH='/proc/1/environ'
5cf148
_is_osbuild()
5cf148
{
5cf148
	[[ $(read_proc_environ_var container "$_OSBUILD_ENVIRON_PATH") == bwrap-osbuild ]]
5cf148
}
5cf148
5cf148
reset_crashkernel_for_installed_kernel()
5cf148
{
5cf148
	local _installed_kernel _running_kernel _crashkernel _crashkernel_running
5cf148
	local _dump_mode_running _fadump_val_running
5cf148
4e4da3
	# During package install, only try to reset crashkernel for osbuild
4e4da3
	# thus to avoid calling grubby when installing os via anaconda
4e4da3
	if ! _is_bootloader_installed && ! _is_osbuild; then
4e4da3
		return
4e4da3
	fi
4e4da3
5cf148
	if ! _installed_kernel=$(_find_kernel_path_by_release "$1"); then
5cf148
		exit 1
5cf148
	fi
5cf148
5cf148
	if _is_osbuild; then
5cf148
		if ! grep -qs crashkernel= /etc/kernel/cmdline; then
5cf148
			reset_crashkernel "--kernel=$_installed_kernel"
5cf148
		fi
5cf148
		return
5cf148
	fi
5cf148
5cf148
	if ! _running_kernel=$(_get_current_running_kernel_path); then
5cf148
		derror "Couldn't find current running kernel"
5cf148
		exit
5cf148
	fi
5cf148
5cf148
	_crashkernel=$(get_grub_kernel_boot_parameter "$_installed_kernel" crashkernel)
5cf148
	_crashkernel_running=$(get_grub_kernel_boot_parameter "$_running_kernel" crashkernel)
5cf148
	_dump_mode_running=$(get_dump_mode_by_kernel "$_running_kernel")
5cf148
	_fadump_val_running=$(get_grub_kernel_boot_parameter "$_kernel" fadump)
5cf148
5cf148
	if [[ $_crashkernel != "$_crashkernel_running" ]]; then
4e4da3
		if _update_kernel_cmdline "$_installed_kernel" "$_crashkernel_running" "$_dump_mode_running" "$_fadump_val_running"; then
5cf148
			echo "kexec-tools has reset $_installed_kernel to use the new default crashkernel value $_crashkernel_running"
5cf148
		fi
4e4da3
	elif [[ $_crashkernel == auto ]]; then
4e4da3
		reset_crashkernel "--kernel=$_installed_kernel"
5cf148
	fi
5cf148
}
5cf148
5cf148
if [[ ! -f $KDUMP_CONFIG_FILE ]]; then
5cf148
	derror "Error: No kdump config file found!"
5cf148
	exit 1
5cf148
fi
5cf148
5cf148
main()
5cf148
{
5cf148
	# Determine if the dump mode is kdump or fadump
5cf148
	determine_dump_mode
5cf148
5cf148
	case "$1" in
5cf148
	start)
5cf148
		if [[ -s /proc/vmcore ]]; then
5cf148
			save_core
5cf148
			reboot
5cf148
		else
5cf148
			start
5cf148
		fi
5cf148
		;;
5cf148
	stop)
5cf148
		stop
5cf148
		;;
5cf148
	status)
5cf148
		EXIT_CODE=0
5cf148
		check_current_status
5cf148
		case "$?" in
5cf148
		0)
5cf148
			dinfo "Kdump is operational"
5cf148
			EXIT_CODE=0
5cf148
			;;
5cf148
		1)
5cf148
			dinfo "Kdump is not operational"
5cf148
			EXIT_CODE=3
5cf148
			;;
5cf148
		esac
5cf148
		exit $EXIT_CODE
5cf148
		;;
5cf148
	reload)
5cf148
		reload
5cf148
		;;
5cf148
	restart)
5cf148
		stop
5cf148
		start
5cf148
		;;
5cf148
	rebuild)
5cf148
		rebuild
5cf148
		;;
5cf148
	condrestart) ;;
5cf148
5cf148
	propagate)
5cf148
		propagate_ssh_key
5cf148
		;;
5cf148
	showmem)
5cf148
		show_reserved_mem
5cf148
		;;
5cf148
	estimate)
5cf148
		do_estimate
5cf148
		;;
5cf148
	get-default-crashkernel)
5cf148
		get_default_crashkernel "$2"
5cf148
		;;
5cf148
	reset-crashkernel)
5cf148
		shift
5cf148
		reset_crashkernel "$@"
5cf148
		;;
4e4da3
	_reset-crashkernel-after-update)
5cf148
		if [[ $(kdump_get_conf_val auto_reset_crashkernel) != no ]]; then
5cf148
			reset_crashkernel_after_update
5cf148
		fi
5cf148
		;;
4e4da3
	_reset-crashkernel-for-installed_kernel)
5cf148
		if [[ $(kdump_get_conf_val auto_reset_crashkernel) != no ]]; then
5cf148
			reset_crashkernel_for_installed_kernel "$2"
5cf148
		fi
5cf148
		;;
5cf148
	*)
5cf148
		dinfo $"Usage: $0 {estimate|start|stop|status|restart|reload|rebuild|reset-crashkernel|propagate|showmem}"
5cf148
		exit 1
5cf148
		;;
5cf148
	esac
5cf148
}
5cf148
5cf148
# Other kdumpctl instances will block in queue, until this one exits
5cf148
single_instance_lock
5cf148
5cf148
# To avoid fd 9 leaking, we invoke a subshell, close fd 9 and call main.
5cf148
# So that fd isn't leaking when main is invoking a subshell.
5cf148
(
5cf148
	exec 9<&-
5cf148
	main "$@"
5cf148
)
5cf148
5cf148
exit $?