db7d74
#!/bin/bash
db7d74
KEXEC=/sbin/kexec
db7d74
db7d74
KDUMP_KERNELVER=""
db7d74
KDUMP_KERNEL=""
db7d74
KDUMP_COMMANDLINE=""
db7d74
KEXEC_ARGS=""
db7d74
KDUMP_LOG_PATH="/var/log"
db7d74
MKDUMPRD="/sbin/mkdumprd -f"
db7d74
MKFADUMPRD="/sbin/mkfadumprd"
db7d74
DRACUT_MODULES_FILE="/usr/lib/dracut/modules.txt"
db7d74
SAVE_PATH=/var/crash
db7d74
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
db7d74
INITRD_CHECKSUM_LOCATION="/boot/.fadump_initrd_checksum"
db7d74
DUMP_TARGET=""
db7d74
DEFAULT_INITRD=""
db7d74
DEFAULT_INITRD_BAK=""
db7d74
KDUMP_INITRD=""
db7d74
TARGET_INITRD=""
db7d74
FADUMP_REGISTER_SYS_NODE="/sys/kernel/fadump_registered"
db7d74
#kdump shall be the default dump mode
db7d74
DEFAULT_DUMP_MODE="kdump"
db7d74
image_time=0
db7d74
db7d74
standard_kexec_args="-d -p"
db7d74
db7d74
# Some default values in case /etc/sysconfig/kdump doesn't include
db7d74
KDUMP_COMMANDLINE_REMOVE="hugepages hugepagesz slub_debug"
db7d74
4a6108
if [[ -f /etc/sysconfig/kdump ]]; then
db7d74
	. /etc/sysconfig/kdump
db7d74
fi
db7d74
db7d74
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
db7d74
. $dracutbasedir/dracut-functions.sh
db7d74
. /lib/kdump/kdump-lib.sh
db7d74
. /lib/kdump/kdump-logger.sh
db7d74
db7d74
#initiate the kdump logger
4a6108
if ! dlog_init; then
db7d74
	echo "failed to initiate the kdump logger."
db7d74
	exit 1
db7d74
fi
db7d74
db7d74
single_instance_lock()
db7d74
{
db7d74
	local rc timeout=5
db7d74
4a6108
	if ! exec 9> /var/lock/kdump; then
db7d74
		derror "Create file lock failed"
db7d74
		exit 1
db7d74
	fi
db7d74
db7d74
	flock -n 9
db7d74
	rc=$?
db7d74
4a6108
	while [[ $rc -ne 0 ]]; do
db7d74
		dinfo "Another app is currently holding the kdump lock; waiting for it to exit..."
db7d74
		flock -w $timeout 9
db7d74
		rc=$?
db7d74
	done
db7d74
}
db7d74
db7d74
determine_dump_mode()
db7d74
{
db7d74
	# Check if firmware-assisted dump is enabled
db7d74
	# if yes, set the dump mode as fadump
db7d74
	if is_fadump_capable; then
db7d74
		dinfo "Dump mode is fadump"
db7d74
		DEFAULT_DUMP_MODE="fadump"
db7d74
	fi
db7d74
	ddebug "DEFAULT_DUMP_MODE=$DEFAULT_DUMP_MODE"
db7d74
}
db7d74
db7d74
save_core()
db7d74
{
4a6108
	coredir="/var/crash/$(date +"%Y-%m-%d-%H:%M")"
db7d74
4a6108
	mkdir -p "$coredir"
db7d74
	ddebug "cp --sparse=always /proc/vmcore $coredir/vmcore-incomplete"
4a6108
	if cp --sparse=always /proc/vmcore "$coredir/vmcore-incomplete"; then
4a6108
		mv "$coredir/vmcore-incomplete" "$coredir/vmcore"
db7d74
		dinfo "saved a vmcore to $coredir"
db7d74
	else
db7d74
		derror "failed to save a vmcore to $coredir"
db7d74
	fi
db7d74
db7d74
	# pass the dmesg to Abrt tool if exists, in order
db7d74
	# to collect the kernel oops message.
db7d74
	# https://fedorahosted.org/abrt/
4a6108
	if [[ -x /usr/bin/dumpoops ]]; then
db7d74
		ddebug "makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg"
4a6108
		makedumpfile --dump-dmesg "$coredir/vmcore" "$coredir/dmesg" > /dev/null 2>&1
db7d74
		ddebug "dumpoops -d $coredir/dmesg"
4a6108
		if dumpoops -d "$coredir/dmesg" > /dev/null 2>&1; then
db7d74
			dinfo "kernel oops has been collected by abrt tool"
db7d74
		fi
db7d74
	fi
db7d74
}
db7d74
db7d74
rebuild_fadump_initrd()
db7d74
{
db7d74
	if ! $MKFADUMPRD "$DEFAULT_INITRD_BAK" "$TARGET_INITRD" --kver "$KDUMP_KERNELVER"; then
db7d74
		derror "mkfadumprd: failed to make fadump initrd"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	return 0
db7d74
}
db7d74
db7d74
check_earlykdump_is_enabled()
db7d74
{
db7d74
	grep -q -w "rd.earlykdump" /proc/cmdline
db7d74
	return $?
db7d74
}
db7d74
db7d74
rebuild_kdump_initrd()
db7d74
{
db7d74
	ddebug "rebuild kdump initrd: $MKDUMPRD $TARGET_INITRD $KDUMP_KERNELVER"
4a6108
	if ! $MKDUMPRD "$TARGET_INITRD" "$KDUMP_KERNELVER"; then
db7d74
		derror "mkdumprd: failed to make kdump initrd"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	if check_earlykdump_is_enabled; then
db7d74
		dwarn "Tips: If early kdump is enabled, also require rebuilding the system initramfs to make the changes take effect for early kdump."
db7d74
	fi
db7d74
db7d74
	return 0
db7d74
}
db7d74
db7d74
rebuild_initrd()
db7d74
{
4a6108
	if [[ ! -w $(dirname "$TARGET_INITRD") ]]; then
4a6108
		derror "$(dirname "$TARGET_INITRD") does not have write permission. Cannot rebuild $TARGET_INITRD"
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
db7d74
		rebuild_fadump_initrd
db7d74
	else
db7d74
		rebuild_kdump_initrd
db7d74
	fi
db7d74
db7d74
	return $?
db7d74
}
db7d74
db7d74
#$1: the files to be checked with IFS=' '
db7d74
check_exist()
db7d74
{
db7d74
	for file in $1; do
4a6108
		if [[ ! -e $file ]]; then
db7d74
			derror "Error: $file not found."
db7d74
			return 1
db7d74
		fi
db7d74
	done
db7d74
}
db7d74
db7d74
#$1: the files to be checked with IFS=' '
db7d74
check_executable()
db7d74
{
db7d74
	for file in $1; do
4a6108
		if [[ ! -x $file ]]; then
db7d74
			derror "Error: $file is not executable."
db7d74
			return 1
db7d74
		fi
db7d74
	done
db7d74
}
db7d74
db7d74
backup_default_initrd()
db7d74
{
db7d74
	ddebug "backup default initrd: $DEFAULT_INITRD"
db7d74
4a6108
	if [[ ! -f $DEFAULT_INITRD ]]; then
db7d74
		return
db7d74
	fi
db7d74
4a6108
	if [[ ! -e $DEFAULT_INITRD_BAK ]]; then
db7d74
		dinfo "Backing up $DEFAULT_INITRD before rebuild."
db7d74
		# save checksum to verify before restoring
4a6108
		sha1sum "$DEFAULT_INITRD" > "$INITRD_CHECKSUM_LOCATION"
4a6108
		if ! cp "$DEFAULT_INITRD" "$DEFAULT_INITRD_BAK"; then
db7d74
			dwarn "WARNING: failed to backup $DEFAULT_INITRD."
4a6108
			rm -f "$DEFAULT_INITRD_BAK"
db7d74
		fi
db7d74
	fi
db7d74
}
db7d74
db7d74
restore_default_initrd()
db7d74
{
db7d74
	ddebug "restore default initrd: $DEFAULT_INITRD"
db7d74
4a6108
	if [[ ! -f $DEFAULT_INITRD ]]; then
db7d74
		return
db7d74
	fi
db7d74
db7d74
	# If a backup initrd exists, we must be switching back from
db7d74
	# fadump to kdump. Restore the original default initrd.
4a6108
	if [[ -f $DEFAULT_INITRD_BAK ]] && [[ -f $INITRD_CHECKSUM_LOCATION ]]; then
db7d74
		# verify checksum before restoring
4a6108
		backup_checksum=$(sha1sum "$DEFAULT_INITRD_BAK" | awk '{ print $1 }')
4a6108
		default_checksum=$(awk '{ print $1 }' "$INITRD_CHECKSUM_LOCATION")
4a6108
		if [[ $default_checksum != "$backup_checksum" ]]; then
db7d74
			dwarn "WARNING: checksum mismatch! Can't restore original initrd.."
db7d74
		else
db7d74
			rm -f $INITRD_CHECKSUM_LOCATION
4a6108
			if mv "$DEFAULT_INITRD_BAK" "$DEFAULT_INITRD"; then
db7d74
				derror "Restoring original initrd as fadump mode is disabled."
db7d74
				sync
db7d74
			fi
db7d74
		fi
db7d74
	fi
db7d74
}
db7d74
db7d74
check_config()
db7d74
{
db7d74
	local -A _opt_rec
db7d74
	while read -r config_opt config_val; do
db7d74
		case "$config_opt" in
db7d74
		dracut_args)
db7d74
			if [[ $config_val == *--mount* ]]; then
4a6108
				if [[ $(echo "$config_val" | grep -o "\-\-mount" | wc -l) -ne 1 ]]; then
4a6108
					derror 'Multiple mount targets specified in one "dracut_args".'
db7d74
					return 1
db7d74
				fi
db7d74
				config_opt=_target
db7d74
			fi
db7d74
			;;
db7d74
		raw)
4a6108
			if [[ -d "/proc/device-tree/ibm,opal/dump" ]]; then
db7d74
				dwarn "WARNING: Won't capture opalcore when 'raw' dump target is used."
db7d74
			fi
db7d74
			config_opt=_target
db7d74
			;;
4a6108
		ext[234] | minix | btrfs | xfs | nfs | ssh)
db7d74
			config_opt=_target
db7d74
			;;
4a6108
		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) ;;
4a6108
4a6108
		net | options | link_delay | disk_timeout | debug_mem_level | blacklist)
db7d74
			derror "Deprecated kdump config option: $config_opt. Refer to kdump.conf manpage for alternatives."
db7d74
			return 1
db7d74
			;;
db7d74
		'')
db7d74
			continue
db7d74
			;;
db7d74
		*)
db7d74
			derror "Invalid kdump config option $config_opt"
db7d74
			return 1
db7d74
			;;
db7d74
		esac
db7d74
4a6108
		if [[ -z $config_val ]]; then
db7d74
			derror "Invalid kdump config value for option '$config_opt'"
db7d74
			return 1
db7d74
		fi
db7d74
4a6108
		if [[ -n ${_opt_rec[$config_opt]} ]]; then
4a6108
			if [[ $config_opt == _target ]]; then
db7d74
				derror "More than one dump targets specified"
db7d74
			else
db7d74
				derror "Duplicated kdump config value of option $config_opt"
db7d74
			fi
db7d74
			return 1
db7d74
		fi
db7d74
		_opt_rec[$config_opt]="$config_val"
4a6108
	done <<< "$(kdump_read_conf)"
db7d74
db7d74
	check_failure_action_config || return 1
db7d74
	check_final_action_config || return 1
db7d74
	check_fence_kdump_config || return 1
db7d74
db7d74
	return 0
db7d74
}
db7d74
db7d74
# get_pcs_cluster_modified_files <image timestamp>
db7d74
# return list of modified file for fence_kdump modified in Pacemaker cluster
db7d74
get_pcs_cluster_modified_files()
db7d74
{
db7d74
	local time_stamp
db7d74
	local modified_files
db7d74
db7d74
	is_generic_fence_kdump && return 1
db7d74
	is_pcs_fence_kdump || return 1
db7d74
4a6108
	time_stamp=$(pcs cluster cib | xmllint --xpath 'string(/cib/@cib-last-written)' - | xargs -0 date +%s --date)
db7d74
4a6108
	if [[ -n $time_stamp ]] && [[ $time_stamp -gt $image_time ]]; then
db7d74
		modified_files="cluster-cib"
db7d74
	fi
db7d74
4a6108
	if [[ -f $FENCE_KDUMP_CONFIG_FILE ]]; then
4a6108
		time_stamp=$(stat -c "%Y" "$FENCE_KDUMP_CONFIG_FILE")
4a6108
		if [[ $time_stamp -gt $image_time ]]; then
db7d74
			modified_files="$modified_files $FENCE_KDUMP_CONFIG_FILE"
db7d74
		fi
db7d74
	fi
db7d74
4a6108
	echo "$modified_files"
db7d74
}
db7d74
db7d74
setup_initrd()
db7d74
{
4a6108
	if ! prepare_kdump_bootinfo; then
db7d74
		derror "failed to prepare for kdump bootinfo."
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	DEFAULT_INITRD_BAK="$KDUMP_BOOTDIR/.$(basename "$DEFAULT_INITRD").default"
4a6108
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
db7d74
		TARGET_INITRD="$DEFAULT_INITRD"
db7d74
db7d74
		# backup initrd for reference before replacing it
db7d74
		# with fadump aware initrd
db7d74
		backup_default_initrd
db7d74
	else
db7d74
		TARGET_INITRD="$KDUMP_INITRD"
db7d74
db7d74
		# check if a backup of default initrd exists. If yes,
db7d74
		# it signifies a switch from fadump mode. So, restore
db7d74
		# the backed up default initrd.
db7d74
		restore_default_initrd
db7d74
	fi
db7d74
}
db7d74
db7d74
check_files_modified()
db7d74
{
db7d74
	local modified_files=""
db7d74
db7d74
	#also rebuild when Pacemaker cluster conf is changed and fence kdump is enabled.
db7d74
	modified_files=$(get_pcs_cluster_modified_files)
db7d74
4a6108
	EXTRA_BINS=$(kdump_get_conf_val kdump_post)
4a6108
	CHECK_FILES=$(kdump_get_conf_val kdump_pre)
db7d74
	HOOKS="/etc/kdump/post.d/ /etc/kdump/pre.d/"
4a6108
	if [[ -d /etc/kdump/post.d ]]; then
db7d74
		for file in /etc/kdump/post.d/*; do
4a6108
			if [[ -x $file ]]; then
db7d74
				POST_FILES="$POST_FILES $file"
db7d74
			fi
db7d74
		done
db7d74
	fi
4a6108
	if [[ -d /etc/kdump/pre.d ]]; then
db7d74
		for file in /etc/kdump/pre.d/*; do
4a6108
			if [[ -x $file ]]; then
db7d74
				PRE_FILES="$PRE_FILES $file"
db7d74
			fi
db7d74
		done
db7d74
	fi
db7d74
	HOOKS="$HOOKS $POST_FILES $PRE_FILES"
4a6108
	CORE_COLLECTOR=$(kdump_get_conf_val core_collector | awk '{print $1}')
4a6108
	CORE_COLLECTOR=$(type -P "$CORE_COLLECTOR")
db7d74
	# POST_FILES and PRE_FILES are already checked against executable, need not to check again.
db7d74
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
4a6108
	CHECK_FILES=$(kdump_get_conf_val extra_bins)
db7d74
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
db7d74
	files="$KDUMP_CONFIG_FILE $KDUMP_KERNEL $EXTRA_BINS $CORE_COLLECTOR"
db7d74
	[[ -e /etc/fstab ]] && files="$files /etc/fstab"
db7d74
db7d74
	# Check for any updated extra module
4a6108
	EXTRA_MODULES="$(kdump_get_conf_val extra_modules)"
4a6108
	if [[ -n $EXTRA_MODULES ]]; then
4a6108
		if [[ -e /lib/modules/$KDUMP_KERNELVER/modules.dep ]]; then
db7d74
			files="$files /lib/modules/$KDUMP_KERNELVER/modules.dep"
db7d74
		fi
db7d74
		for _module in $EXTRA_MODULES; do
4a6108
			if _module_file="$(modinfo --set-version "$KDUMP_KERNELVER" --filename "$_module" 2> /dev/null)"; then
db7d74
				files="$files $_module_file"
4a6108
				for _dep_modules in $(modinfo -F depends "$_module" | tr ',' ' '); do
4a6108
					files="$files $(modinfo --set-version "$KDUMP_KERNELVER" --filename "$_dep_modules" 2> /dev/null)"
db7d74
				done
db7d74
			else
db7d74
				# If it's not a module nor builtin, give an error
4a6108
				if ! (modprobe --set-version "$KDUMP_KERNELVER" --dry-run "$_module" &> /dev/null); then
db7d74
					dwarn "Module $_module not found"
db7d74
				fi
db7d74
			fi
db7d74
		done
db7d74
	fi
db7d74
db7d74
	# HOOKS is mandatory and need to check the modification time
db7d74
	files="$files $HOOKS"
4a6108
	check_exist "$files" && check_executable "$EXTRA_BINS" || return 2
db7d74
db7d74
	for file in $files; do
4a6108
		if [[ -e $file ]]; then
4a6108
			time_stamp=$(stat -c "%Y" "$file")
4a6108
			if [[ $time_stamp -gt $image_time ]]; then
db7d74
				modified_files="$modified_files $file"
db7d74
			fi
4a6108
			if [[ -L $file ]]; then
4a6108
				file=$(readlink -m "$file")
4a6108
				time_stamp=$(stat -c "%Y" "$file")
4a6108
				if [[ $time_stamp -gt $image_time ]]; then
db7d74
					modified_files="$modified_files $file"
db7d74
				fi
db7d74
			fi
db7d74
		else
db7d74
			dwarn "$file doesn't exist"
db7d74
		fi
db7d74
	done
db7d74
4a6108
	if [[ -n $modified_files ]]; then
db7d74
		dinfo "Detected change(s) in the following file(s): $modified_files"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	return 0
db7d74
}
db7d74
db7d74
check_drivers_modified()
db7d74
{
db7d74
	local _target _new_drivers _old_drivers _module_name _module_filename
db7d74
db7d74
	# If it's dump target is on block device, detect the block driver
db7d74
	_target=$(get_block_dump_target)
4a6108
	if [[ -n $_target ]]; then
4a6108
		_record_block_drivers()
4a6108
		{
db7d74
			local _drivers
db7d74
			_drivers=$(udevadm info -a "/dev/block/$1" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p')
db7d74
			for _driver in $_drivers; do
db7d74
				if ! [[ " $_new_drivers " == *" $_driver "* ]]; then
db7d74
					_new_drivers="$_new_drivers $_driver"
db7d74
				fi
db7d74
			done
db7d74
db7d74
			ddebug "MAJ:MIN=$1 drivers='$_drivers'"
db7d74
		}
db7d74
		check_block_and_slaves_all _record_block_drivers "$(get_maj_min "$_target")"
db7d74
	fi
db7d74
db7d74
	# Include watchdog drivers if watchdog module is not omitted
db7d74
	is_dracut_mod_omitted watchdog || _new_drivers+=" $(get_watchdog_drvs)"
4a6108
	[[ -z $_new_drivers ]] && return 0
db7d74
db7d74
	if is_fadump_capable; then
db7d74
		_old_drivers="$(lsinitrd "$TARGET_INITRD" -f /usr/lib/dracut/fadump-kernel-modules.txt | tr '\n' ' ')"
db7d74
	else
db7d74
		_old_drivers="$(lsinitrd "$TARGET_INITRD" -f /usr/lib/dracut/hostonly-kernel-modules.txt | tr '\n' ' ')"
db7d74
	fi
db7d74
db7d74
	ddebug "Modules required for kdump: '$_new_drivers'"
db7d74
	ddebug "Modules included in old initramfs: '$_old_drivers'"
db7d74
	for _driver in $_new_drivers; do
db7d74
		# Skip deprecated/invalid driver name or built-in module
4a6108
		_module_name=$(modinfo --set-version "$KDUMP_KERNELVER" -F name "$_driver" 2> /dev/null)
4a6108
		_module_filename=$(modinfo --set-version "$KDUMP_KERNELVER" -n "$_driver" 2> /dev/null)
4a6108
		if [[ -z $_module_name ]] || [[ -z $_module_filename ]] || [[ $_module_filename == *"(builtin)"* ]]; then
db7d74
			continue
db7d74
		fi
db7d74
		if ! [[ " $_old_drivers " == *" $_module_name "* ]]; then
db7d74
			dinfo "Detected change in block device driver, new loaded module: $_module_name"
db7d74
			return 1
db7d74
		fi
db7d74
	done
db7d74
}
db7d74
db7d74
check_fs_modified()
db7d74
{
db7d74
	local _old_dev _old_mntpoint _old_fstype
db7d74
	local _new_dev _new_mntpoint _new_fstype
db7d74
	local _target _dracut_args
db7d74
db7d74
	# No need to check in case of mount target specified via "dracut_args".
db7d74
	if is_mount_in_dracut_args; then
db7d74
		return 0
db7d74
	fi
db7d74
db7d74
	# No need to check in case of raw target.
db7d74
	# Currently we do not check also if ssh/nfs target is specified
db7d74
	if is_ssh_dump_target || is_nfs_dump_target || is_raw_dump_target; then
db7d74
		return 0
db7d74
	fi
db7d74
db7d74
	_target=$(get_block_dump_target)
4a6108
	_new_fstype=$(get_fs_type_from_target "$_target")
4a6108
	if [[ -z $_target ]] || [[ -z $_new_fstype ]]; then
db7d74
		derror "Dump target is invalid"
db7d74
		return 2
db7d74
	fi
db7d74
db7d74
	ddebug "_target=$_target _new_fstype=$_new_fstype"
4a6108
	_new_dev=$(kdump_get_persistent_dev "$_target")
4a6108
	if [[ -z $_new_dev ]]; then
db7d74
		perror "Get persistent device name failed"
db7d74
		return 2
db7d74
	fi
db7d74
4a6108
	_new_mntpoint="$(get_kdump_mntpoint_from_target "$_target")"
4a6108
	_dracut_args=$(lsinitrd "$TARGET_INITRD" -f usr/lib/dracut/build-parameter.txt)
4a6108
	if [[ -z $_dracut_args ]]; then
db7d74
		dwarn "Warning: No dracut arguments found in initrd"
db7d74
		return 0
db7d74
	fi
db7d74
db7d74
	# if --mount argument present then match old and new target, mount
db7d74
	# point and file system. If any of them mismatches then rebuild
4a6108
	if echo "$_dracut_args" | grep -q "\-\-mount"; then
4a6108
		# shellcheck disable=SC2046
4a6108
		set -- $(echo "$_dracut_args" | awk -F "--mount '" '{print $2}' | cut -d' ' -f1,2,3)
db7d74
		_old_dev=$1
db7d74
		_old_mntpoint=$2
db7d74
		_old_fstype=$3
4a6108
		[[ $_new_dev == "$_old_dev" && $_new_mntpoint == "$_old_mntpoint" && $_new_fstype == "$_old_fstype" ]] && return 0
db7d74
	# otherwise rebuild if target device is not a root device
db7d74
	else
4a6108
		[[ $_target == "$(get_root_fs_device)" ]] && return 0
db7d74
	fi
db7d74
db7d74
	dinfo "Detected change in File System"
db7d74
	return 1
db7d74
}
db7d74
db7d74
# returns 0 if system is not modified
db7d74
# returns 1 if system is modified
db7d74
# returns 2 if system modification is invalid
db7d74
check_system_modified()
db7d74
{
db7d74
	local ret
db7d74
db7d74
	[[ -f $TARGET_INITRD ]] || return 1
db7d74
db7d74
	check_files_modified
db7d74
	ret=$?
4a6108
	if [[ $ret -ne 0 ]]; then
db7d74
		return $ret
db7d74
	fi
db7d74
db7d74
	check_fs_modified
db7d74
	ret=$?
4a6108
	if [[ $ret -ne 0 ]]; then
db7d74
		return $ret
db7d74
	fi
db7d74
db7d74
	check_drivers_modified
db7d74
	ret=$?
4a6108
	if [[ $ret -ne 0 ]]; then
db7d74
		return $ret
db7d74
	fi
db7d74
db7d74
	return 0
db7d74
}
db7d74
db7d74
check_rebuild()
db7d74
{
db7d74
	local capture_capable_initrd="1"
4a6108
	local force_rebuild force_no_rebuild
db7d74
	local ret system_modified="0"
db7d74
4a6108
	setup_initrd || return 1
db7d74
4a6108
	force_no_rebuild=$(kdump_get_conf_val force_no_rebuild)
4a6108
	force_no_rebuild=${force_no_rebuild:-0}
4a6108
	if [[ $force_no_rebuild != "0" ]] && [[ $force_no_rebuild != "1" ]]; then
4a6108
		derror "Error: force_no_rebuild value is invalid"
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	force_rebuild=$(kdump_get_conf_val force_rebuild)
4a6108
	force_rebuild=${force_rebuild:-0}
4a6108
	if [[ $force_rebuild != "0" ]] && [[ $force_rebuild != "1" ]]; then
4a6108
		derror "Error: force_rebuild value is invalid"
4a6108
		return 1
db7d74
	fi
db7d74
4a6108
	if [[ $force_no_rebuild == "1" && $force_rebuild == "1" ]]; then
db7d74
		derror "Error: force_rebuild and force_no_rebuild are enabled simultaneously in kdump.conf"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	# Will not rebuild kdump initrd
4a6108
	if [[ $force_no_rebuild == "1" ]]; then
db7d74
		return 0
db7d74
	fi
db7d74
db7d74
	#check to see if dependent files has been modified
db7d74
	#since last build of the image file
4a6108
	if [[ -f $TARGET_INITRD ]]; then
4a6108
		image_time=$(stat -c "%Y" "$TARGET_INITRD" 2> /dev/null)
db7d74
db7d74
		#in case of fadump mode, check whether the default/target
db7d74
		#initrd is already built with dump capture capability
4a6108
		if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
4a6108
			capture_capable_initrd=$(lsinitrd -f $DRACUT_MODULES_FILE "$TARGET_INITRD" | grep -c -e ^kdumpbase$ -e ^zz-fadumpinit$)
db7d74
		fi
db7d74
	fi
db7d74
db7d74
	check_system_modified
db7d74
	ret=$?
4a6108
	if [[ $ret -eq 2 ]]; then
db7d74
		return 1
4a6108
	elif [[ $ret -eq 1 ]]; then
db7d74
		system_modified="1"
db7d74
	fi
db7d74
4a6108
	if [[ $image_time -eq 0 ]]; then
db7d74
		dinfo "No kdump initial ramdisk found."
4a6108
	elif [[ $capture_capable_initrd == "0" ]]; then
db7d74
		dinfo "Rebuild $TARGET_INITRD with dump capture support"
4a6108
	elif [[ $force_rebuild != "0" ]]; then
db7d74
		dinfo "Force rebuild $TARGET_INITRD"
4a6108
	elif [[ $system_modified != "0" ]]; then
db7d74
		:
db7d74
	else
db7d74
		return 0
db7d74
	fi
db7d74
db7d74
	dinfo "Rebuilding $TARGET_INITRD"
db7d74
	rebuild_initrd
db7d74
	return $?
db7d74
}
db7d74
db7d74
# Load the kdump kernel specified in /etc/sysconfig/kdump
db7d74
# If none is specified, try to load a kdump kernel with the same version
db7d74
# as the currently running kernel.
db7d74
load_kdump()
db7d74
{
db7d74
	local ret
db7d74
db7d74
	KEXEC_ARGS=$(prepare_kexec_args "${KEXEC_ARGS}")
db7d74
	KDUMP_COMMANDLINE=$(prepare_cmdline "${KDUMP_COMMANDLINE}" "${KDUMP_COMMANDLINE_REMOVE}" "${KDUMP_COMMANDLINE_APPEND}")
db7d74
db7d74
	# For secureboot enabled machines, use new kexec file based syscall.
db7d74
	# Old syscall will always fail as it does not have capability to
db7d74
	# to kernel signature verification.
db7d74
	if is_secure_boot_enforced; then
db7d74
		dinfo "Secure Boot is enabled. Using kexec file based syscall."
db7d74
		KEXEC_ARGS="$KEXEC_ARGS -s"
db7d74
	fi
db7d74
db7d74
	ddebug "$KEXEC $KEXEC_ARGS $standard_kexec_args --command-line=$KDUMP_COMMANDLINE --initrd=$TARGET_INITRD $KDUMP_KERNEL"
db7d74
db7d74
	# The '12' represents an intermediate temporary file descriptor
db7d74
	# to store the standard error file descriptor '2', and later
db7d74
	# restore the error file descriptor with the file descriptor '12'
db7d74
	# and release it.
db7d74
	exec 12>&2
db7d74
	exec 2>> $KDUMP_LOG_PATH/kdump.log
db7d74
	PS4='+ $(date "+%Y-%m-%d %H:%M:%S") ${BASH_SOURCE}@${LINENO}: '
db7d74
	set -x
db7d74
4a6108
	# shellcheck disable=SC2086
db7d74
	$KEXEC $KEXEC_ARGS $standard_kexec_args \
db7d74
		--command-line="$KDUMP_COMMANDLINE" \
4a6108
		--initrd="$TARGET_INITRD" "$KDUMP_KERNEL"
db7d74
db7d74
	ret=$?
db7d74
	set +x
db7d74
	exec 2>&12 12>&-
db7d74
4a6108
	if [[ $ret == 0 ]]; then
db7d74
		dinfo "kexec: loaded kdump kernel"
db7d74
		return 0
db7d74
	else
db7d74
		derror "kexec: failed to load kdump kernel"
db7d74
		return 1
db7d74
	fi
db7d74
}
db7d74
db7d74
check_ssh_config()
db7d74
{
4a6108
	local SSH_TARGET
4a6108
4a6108
	while read -r config_opt config_val; do
db7d74
		case "$config_opt" in
db7d74
		sshkey)
db7d74
			# remove inline comments after the end of a directive.
4a6108
			if [[ -f $config_val ]]; then
db7d74
				# canonicalize the path
4a6108
				SSH_KEY_LOCATION=$(/usr/bin/readlink -m "$config_val")
db7d74
			else
db7d74
				dwarn "WARNING: '$config_val' doesn't exist, using default value '$SSH_KEY_LOCATION'"
db7d74
			fi
db7d74
			;;
db7d74
		path)
db7d74
			SAVE_PATH=$config_val
db7d74
			;;
db7d74
		ssh)
db7d74
			DUMP_TARGET=$config_val
db7d74
			;;
4a6108
		*) ;;
4a6108
db7d74
		esac
4a6108
	done <<< "$(kdump_read_conf)"
db7d74
db7d74
	#make sure they've configured kdump.conf for ssh dumps
4a6108
	SSH_TARGET=$(echo -n "$DUMP_TARGET" | sed -n '/.*@/p')
4a6108
	if [[ -z $SSH_TARGET ]]; then
db7d74
		return 1
db7d74
	fi
db7d74
	return 0
db7d74
}
db7d74
db7d74
# ipv6 host address may takes a long time to be ready.
db7d74
# Instead of checking against ipv6 address, we just check the network reachable
db7d74
# by the return val of 'ssh'
db7d74
check_and_wait_network_ready()
db7d74
{
4a6108
	local start_time
db7d74
	local warn_once=1
db7d74
	local cur
db7d74
	local diff
db7d74
	local retval
db7d74
	local errmsg
db7d74
4a6108
	start_time=$(date +%s)
db7d74
	while true; do
4a6108
		errmsg=$(ssh -i "$SSH_KEY_LOCATION" -o BatchMode=yes "$DUMP_TARGET" mkdir -p "$SAVE_PATH" 2>&1)
db7d74
		retval=$?
db7d74
db7d74
		# ssh exits with the exit status of the remote command or with 255 if an error occurred
4a6108
		if [[ $retval -eq 0 ]]; then
db7d74
			return 0
4a6108
		elif [[ $retval -ne 255 ]]; then
db7d74
			derror "Could not create $DUMP_TARGET:$SAVE_PATH, you should check the privilege on server side"
db7d74
			return 1
db7d74
		fi
db7d74
db7d74
		# if server removes the authorized_keys or, no /root/.ssh/kdump_id_rsa
db7d74
		ddebug "$errmsg"
4a6108
		if echo "$errmsg" | grep -q "Permission denied\|No such file or directory\|Host key verification failed"; then
db7d74
			derror "Could not create $DUMP_TARGET:$SAVE_PATH, you probably need to run \"kdumpctl propagate\""
db7d74
			return 1
db7d74
		fi
db7d74
4a6108
		if [[ $warn_once -eq 1 ]]; then
db7d74
			dwarn "Network dump target is not usable, waiting for it to be ready..."
db7d74
			warn_once=0
db7d74
		fi
db7d74
db7d74
		cur=$(date +%s)
4a6108
		diff=$((cur - start_time))
db7d74
		# 60s time out
4a6108
		if [[ $diff -gt 180 ]]; then
4a6108
			break
db7d74
		fi
db7d74
		sleep 1
db7d74
	done
db7d74
db7d74
	dinfo "Could not create $DUMP_TARGET:$SAVE_PATH, ipaddr is not ready yet. You should check network connection"
db7d74
	return 1
db7d74
}
db7d74
db7d74
check_ssh_target()
db7d74
{
db7d74
	check_and_wait_network_ready
db7d74
}
db7d74
db7d74
propagate_ssh_key()
db7d74
{
4a6108
	if ! check_ssh_config; then
db7d74
		derror "No ssh config specified in $KDUMP_CONFIG_FILE.  Can't propagate"
db7d74
		exit 1
db7d74
	fi
db7d74
db7d74
	local KEYFILE=$SSH_KEY_LOCATION
db7d74
	local errmsg="Failed to propagate ssh key"
db7d74
db7d74
	#Check to see if we already created key, if not, create it.
4a6108
	if [[ -f $KEYFILE ]]; then
db7d74
		dinfo "Using existing keys..."
db7d74
	else
db7d74
		dinfo "Generating new ssh keys... "
4a6108
		/usr/bin/ssh-keygen -t rsa -f "$KEYFILE" -N "" 2>&1 > /dev/null
db7d74
		dinfo "done."
db7d74
	fi
db7d74
db7d74
	#now find the target ssh user and server to contact.
4a6108
	SSH_USER=$(echo "$DUMP_TARGET" | cut -d@ -f1)
4a6108
	SSH_SERVER=$(echo "$DUMP_TARGET" | sed -e's/\(.*@\)\(.*$\)/\2/')
db7d74
db7d74
	#now send the found key to the found server
4a6108
	ssh-copy-id -i "$KEYFILE" "$SSH_USER@$SSH_SERVER"
db7d74
	RET=$?
4a6108
	if [[ $RET == 0 ]]; then
db7d74
		dinfo "$KEYFILE has been added to ~$SSH_USER/.ssh/authorized_keys on $SSH_SERVER"
db7d74
		return 0
db7d74
	else
db7d74
		derror "$errmsg, $KEYFILE failed in transfer to $SSH_SERVER"
db7d74
		exit 1
db7d74
	fi
db7d74
}
db7d74
db7d74
show_reserved_mem()
db7d74
{
4a6108
	local mem
4a6108
	local mem_mb
db7d74
4a6108
	mem=$(< /sys/kernel/kexec_crash_size)
4a6108
	mem_mb=$((mem / 1024 / 1024))
4a6108
4a6108
	dinfo "Reserved ${mem_mb}MB memory for crash kernel"
db7d74
}
db7d74
db7d74
check_current_fadump_status()
db7d74
{
db7d74
	# Check if firmware-assisted dump has been registered.
4a6108
	rc=$(< $FADUMP_REGISTER_SYS_NODE)
4a6108
	[[ $rc -eq 1 ]] && return 0
db7d74
	return 1
db7d74
}
db7d74
db7d74
check_current_status()
db7d74
{
4a6108
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
db7d74
		check_current_fadump_status
db7d74
	else
db7d74
		check_current_kdump_status
db7d74
	fi
db7d74
db7d74
	return $?
db7d74
}
db7d74
db7d74
save_raw()
db7d74
{
db7d74
	local kdump_dir
db7d74
	local raw_target
db7d74
4a6108
	raw_target=$(kdump_get_conf_val raw)
4a6108
	[[ -z $raw_target ]] && return 0
4a6108
	[[ -b $raw_target ]] || {
db7d74
		derror "raw partition $raw_target not found"
db7d74
		return 1
db7d74
	}
4a6108
	check_fs=$(lsblk --nodeps -npo FSTYPE "$raw_target")
4a6108
	if [[ $(echo "$check_fs" | wc -w) -ne 0 ]]; then
db7d74
		dwarn "Warning: Detected '$check_fs' signature on $raw_target, data loss is expected."
db7d74
		return 0
db7d74
	fi
4a6108
	kdump_dir=$(kdump_get_conf_val path)
4a6108
	if [[ -z ${kdump_dir} ]]; then
4a6108
		coredir="/var/crash/$(date +"%Y-%m-%d-%H:%M")"
db7d74
	else
4a6108
		coredir="${kdump_dir}/$(date +"%Y-%m-%d-%H:%M")"
db7d74
	fi
db7d74
db7d74
	mkdir -p "$coredir"
4a6108
	[[ -d $coredir ]] || {
db7d74
		derror "failed to create $coredir"
db7d74
		return 1
db7d74
	}
4a6108
	if makedumpfile -R "$coredir/vmcore" < "$raw_target" > /dev/null 2>&1; then
db7d74
		# dump found
db7d74
		dinfo "Dump saved to $coredir/vmcore"
db7d74
		# wipe makedumpfile header
4a6108
		dd if=/dev/zero of="$raw_target" bs=1b count=1 2> /dev/null
db7d74
	else
db7d74
		rm -rf "$coredir"
db7d74
	fi
db7d74
db7d74
	return 0
db7d74
}
db7d74
db7d74
local_fs_dump_target()
db7d74
{
db7d74
	local _target
db7d74
4a6108
	if _target=$(grep -E "^ext[234]|^xfs|^btrfs|^minix" /etc/kdump.conf); then
4a6108
		echo "$_target" | awk '{print $2}'
db7d74
	fi
db7d74
}
db7d74
db7d74
path_to_be_relabeled()
db7d74
{
db7d74
	local _path _target _mnt="/" _rmnt
db7d74
db7d74
	if is_user_configured_dump_target; then
db7d74
		if is_mount_in_dracut_args; then
4a6108
			return
db7d74
		fi
db7d74
db7d74
		_target=$(local_fs_dump_target)
4a6108
		if [[ -n $_target ]]; then
4a6108
			_mnt=$(get_mntpoint_from_target "$_target")
db7d74
			if ! is_mounted "$_mnt"; then
db7d74
				return
db7d74
			fi
db7d74
		else
db7d74
			return
db7d74
		fi
db7d74
	fi
db7d74
db7d74
	_path=$(get_save_path)
db7d74
	# if $_path is masked by other mount, we will not relabel it.
4a6108
	_rmnt=$(df "$_mnt/$_path" 2> /dev/null | tail -1 | awk '{ print $NF }')
4a6108
	if [[ $_rmnt == "$_mnt" ]]; then
4a6108
		echo "$_mnt/$_path"
db7d74
	fi
db7d74
}
db7d74
db7d74
selinux_relabel()
db7d74
{
db7d74
	local _path _i _attr
db7d74
db7d74
	_path=$(path_to_be_relabeled)
4a6108
	if [[ -z $_path ]] || ! [[ -d $_path ]]; then
db7d74
		return
db7d74
	fi
db7d74
4a6108
	while IFS= read -r -d '' _i; do
4a6108
		_attr=$(getfattr -m "security.selinux" "$_i" 2> /dev/null)
4a6108
		if [[ -z $_attr ]]; then
4a6108
			restorecon "$_i"
db7d74
		fi
4a6108
	done < <(find "$_path" -print0)
db7d74
}
db7d74
db7d74
check_fence_kdump_config()
db7d74
{
4a6108
	local hostname
4a6108
	local ipaddrs
4a6108
	local nodes
4a6108
4a6108
	hostname=$(hostname)
4a6108
	ipaddrs=$(hostname -I)
4a6108
	nodes=$(kdump_get_conf_val "fence_kdump_nodes")
db7d74
db7d74
	for node in $nodes; do
4a6108
		if [[ $node == "$hostname" ]]; then
db7d74
			derror "Option fence_kdump_nodes cannot contain $hostname"
db7d74
			return 1
db7d74
		fi
db7d74
		# node can be ipaddr
4a6108
		if echo "$ipaddrs " | grep -q "$node "; then
db7d74
			derror "Option fence_kdump_nodes cannot contain $node"
db7d74
			return 1
db7d74
		fi
db7d74
	done
db7d74
db7d74
	return 0
db7d74
}
db7d74
db7d74
check_dump_feasibility()
db7d74
{
4a6108
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
db7d74
		return 0
db7d74
	fi
db7d74
db7d74
	check_kdump_feasibility
db7d74
	return $?
db7d74
}
db7d74
db7d74
start_fadump()
db7d74
{
db7d74
	echo 1 > $FADUMP_REGISTER_SYS_NODE
db7d74
	if ! check_current_fadump_status; then
db7d74
		derror "fadump: failed to register"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	dinfo "fadump: registered successfully"
db7d74
	return 0
db7d74
}
db7d74
db7d74
start_dump()
db7d74
{
4a6108
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
db7d74
		start_fadump
db7d74
	else
db7d74
		load_kdump
db7d74
	fi
db7d74
db7d74
	return $?
db7d74
}
db7d74
db7d74
check_failure_action_config()
db7d74
{
db7d74
	local default_option
db7d74
	local failure_action
db7d74
	local option="failure_action"
db7d74
4a6108
	default_option=$(kdump_get_conf_val default)
4a6108
	failure_action=$(kdump_get_conf_val failure_action)
db7d74
4a6108
	if [[ -z $failure_action ]] && [[ -z $default_option ]]; then
db7d74
		return 0
4a6108
	elif [[ -n $failure_action ]] && [[ -n $default_option ]]; then
db7d74
		derror "Cannot specify 'failure_action' and 'default' option together"
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	if [[ -n $default_option ]]; then
db7d74
		option="default"
db7d74
		failure_action="$default_option"
db7d74
	fi
db7d74
db7d74
	case "$failure_action" in
4a6108
	reboot | halt | poweroff | shell | dump_to_rootfs)
db7d74
		return 0
4a6108
		;;
4a6108
	*)
db7d74
		dinfo $"Usage kdump.conf: $option {reboot|halt|poweroff|shell|dump_to_rootfs}"
db7d74
		return 1
4a6108
		;;
db7d74
	esac
db7d74
}
db7d74
db7d74
check_final_action_config()
db7d74
{
db7d74
	local final_action
db7d74
4a6108
	final_action=$(kdump_get_conf_val final_action)
4a6108
	if [[ -z $final_action ]]; then
db7d74
		return 0
db7d74
	else
db7d74
		case "$final_action" in
4a6108
		reboot | halt | poweroff)
db7d74
			return 0
4a6108
			;;
4a6108
		*)
db7d74
			dinfo $"Usage kdump.conf: final_action {reboot|halt|poweroff}"
db7d74
			return 1
4a6108
			;;
db7d74
		esac
db7d74
	fi
db7d74
}
db7d74
db7d74
start()
db7d74
{
4a6108
	if ! check_dump_feasibility; then
db7d74
		derror "Starting kdump: [FAILED]"
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	if ! check_config; then
db7d74
		derror "Starting kdump: [FAILED]"
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	if sestatus 2> /dev/null | grep -q "SELinux status.*enabled"; then
db7d74
		selinux_relabel
db7d74
	fi
db7d74
4a6108
	if ! save_raw; then
db7d74
		derror "Starting kdump: [FAILED]"
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	if check_current_status; then
db7d74
		dwarn "Kdump already running: [WARNING]"
db7d74
		return 0
db7d74
	fi
db7d74
db7d74
	if check_ssh_config; then
db7d74
		if ! check_ssh_target; then
db7d74
			derror "Starting kdump: [FAILED]"
db7d74
			return 1
db7d74
		fi
db7d74
	fi
db7d74
4a6108
	if ! check_rebuild; then
db7d74
		derror "Starting kdump: [FAILED]"
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	if ! start_dump; then
db7d74
		derror "Starting kdump: [FAILED]"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	dinfo "Starting kdump: [OK]"
db7d74
}
db7d74
db7d74
reload()
db7d74
{
4a6108
	if ! check_current_status; then
db7d74
		dwarn "Kdump was not running: [WARNING]"
db7d74
	fi
db7d74
4a6108
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
db7d74
		reload_fadump
db7d74
		return $?
db7d74
	else
4a6108
		if ! stop_kdump; then
4a6108
			derror "Stopping kdump: [FAILED]"
4a6108
			return 1
4a6108
		fi
db7d74
	fi
db7d74
db7d74
	dinfo "Stopping kdump: [OK]"
db7d74
4a6108
	if ! setup_initrd; then
db7d74
		derror "Starting kdump: [FAILED]"
db7d74
		return 1
db7d74
	fi
db7d74
4a6108
	if ! start_dump; then
db7d74
		derror "Starting kdump: [FAILED]"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	dinfo "Starting kdump: [OK]"
db7d74
}
db7d74
db7d74
stop_fadump()
db7d74
{
db7d74
	echo 0 > $FADUMP_REGISTER_SYS_NODE
db7d74
	if check_current_fadump_status; then
db7d74
		derror "fadump: failed to unregister"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	dinfo "fadump: unregistered successfully"
db7d74
	return 0
db7d74
}
db7d74
db7d74
stop_kdump()
db7d74
{
db7d74
	if is_secure_boot_enforced; then
db7d74
		$KEXEC -s -p -u
db7d74
	else
db7d74
		$KEXEC -p -u
db7d74
	fi
db7d74
4a6108
	# shellcheck disable=SC2181
4a6108
	if [[ $? != 0 ]]; then
db7d74
		derror "kexec: failed to unload kdump kernel"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	dinfo "kexec: unloaded kdump kernel"
db7d74
	return 0
db7d74
}
db7d74
db7d74
reload_fadump()
db7d74
{
4a6108
	if echo 1 > $FADUMP_REGISTER_SYS_NODE; then
db7d74
		dinfo "fadump: re-registered successfully"
db7d74
		return 0
db7d74
	else
db7d74
		# FADump could fail on older kernel where re-register
db7d74
		# support is not enabled. Try stop/start from userspace
db7d74
		# to handle such scenario.
4a6108
		if stop_fadump; then
db7d74
			start_fadump
db7d74
			return $?
db7d74
		fi
db7d74
	fi
db7d74
db7d74
	return 1
db7d74
}
db7d74
db7d74
stop()
db7d74
{
4a6108
	if [[ $DEFAULT_DUMP_MODE == "fadump" ]]; then
db7d74
		stop_fadump
db7d74
	else
db7d74
		stop_kdump
db7d74
	fi
db7d74
4a6108
	# shellcheck disable=SC2181
4a6108
	if [[ $? != 0 ]]; then
db7d74
		derror "Stopping kdump: [FAILED]"
db7d74
		return 1
db7d74
	fi
db7d74
db7d74
	dinfo "Stopping kdump: [OK]"
db7d74
	return 0
db7d74
}
db7d74
4a6108
rebuild()
4a6108
{
4a6108
	check_config || return 1
db7d74
db7d74
	if check_ssh_config; then
db7d74
		if ! check_ssh_target; then
db7d74
			return 1
db7d74
		fi
db7d74
	fi
db7d74
4a6108
	setup_initrd || return 1
db7d74
db7d74
	dinfo "Rebuilding $TARGET_INITRD"
db7d74
	rebuild_initrd
db7d74
	return $?
db7d74
}
db7d74
4a6108
do_estimate()
4a6108
{
db7d74
	local kdump_mods
db7d74
	local -A large_mods
db7d74
	local baseline
db7d74
	local kernel_size mod_size initrd_size baseline_size runtime_size reserved_size estimated_size recommended_size
4a6108
	local size_mb=$((1024 * 1024))
db7d74
db7d74
	setup_initrd
4a6108
	if [[ ! -f $TARGET_INITRD ]]; then
db7d74
		derror "kdumpctl estimate: kdump initramfs is not built yet."
db7d74
		exit 1
db7d74
	fi
db7d74
db7d74
	kdump_mods="$(lsinitrd "$TARGET_INITRD" -f /usr/lib/dracut/hostonly-kernel-modules.txt | tr '\n' ' ')"
db7d74
	baseline=$(kdump_get_arch_recommend_size)
4a6108
	if [[ ${baseline: -1} == "M" ]]; then
db7d74
		baseline=${baseline%M}
4a6108
	elif [[ ${baseline: -1} == "G" ]]; then
4a6108
		baseline=$((${baseline%G} * 1024))
4a6108
	elif [[ ${baseline: -1} == "T" ]]; then
4a6108
		baseline=$((${baseline%Y} * 1048576))
db7d74
	fi
db7d74
db7d74
	# The default pre-reserved crashkernel value
db7d74
	baseline_size=$((baseline * size_mb))
db7d74
	# Current reserved crashkernel size
4a6108
	reserved_size=$(< /sys/kernel/kexec_crash_size)
db7d74
	# A pre-estimated value for userspace usage and kernel
db7d74
	# runtime allocation, 64M should good for most cases
db7d74
	runtime_size=$((64 * size_mb))
db7d74
	# Kernel image size
db7d74
	kernel_size=$(get_kernel_size "$KDUMP_KERNEL")
db7d74
	# Kdump initramfs size
db7d74
	initrd_size=$(du -b "$TARGET_INITRD" | awk '{print $1}')
db7d74
	# Kernel modules static size after loaded
db7d74
	mod_size=0
db7d74
	while read -r _name _size _; do
4a6108
		if [[ " $kdump_mods " != *" $_name "* ]]; then
db7d74
			continue
db7d74
		fi
db7d74
		mod_size=$((mod_size + _size))
db7d74
db7d74
		# Mark module with static size larger than 2M as large module
db7d74
		if [[ $((_size / size_mb)) -ge 1 ]]; then
db7d74
			large_mods[$_name]=$_size
db7d74
		fi
db7d74
	done <<< "$(< /proc/modules)"
db7d74
db7d74
	# Extra memory usage required for LUKS2 decryption
db7d74
	crypt_size=0
db7d74
	for _dev in $(get_all_kdump_crypt_dev); do
db7d74
		_crypt_info=$(cryptsetup luksDump "/dev/block/$_dev")
4a6108
		[[ $(echo "$_crypt_info" | sed -n "s/^Version:\s*\(.*\)/\1/p") == "2" ]] || continue
4a6108
		for _mem in $(echo "$_crypt_info" | sed -n "s/\sMemory:\s*\(.*\)/\1/p" | sort -n); do
db7d74
			crypt_size=$((crypt_size + _mem * 1024))
db7d74
			break
db7d74
		done
db7d74
	done
db7d74
	[[ $crypt_size -ne 0 ]] && echo -e "Encrypted kdump target requires extra memory, assuming using the keyslot with minimun memory requirement\n"
db7d74
db7d74
	estimated_size=$((kernel_size + mod_size + initrd_size + runtime_size + crypt_size))
db7d74
	if [[ $baseline_size -gt $estimated_size ]]; then
db7d74
		recommended_size=$baseline_size
db7d74
	else
db7d74
		recommended_size=$estimated_size
db7d74
	fi
db7d74
db7d74
	echo "Reserved crashkernel:    $((reserved_size / size_mb))M"
db7d74
	echo "Recommended crashkernel: $((recommended_size / size_mb))M"
db7d74
	echo
db7d74
	echo "Kernel image size:   $((kernel_size / size_mb))M"
db7d74
	echo "Kernel modules size: $((mod_size / size_mb))M"
db7d74
	echo "Initramfs size:      $((initrd_size / size_mb))M"
db7d74
	echo "Runtime reservation: $((runtime_size / size_mb))M"
4a6108
	[[ $crypt_size -ne 0 ]] &&
4a6108
		echo "LUKS required size:  $((crypt_size / size_mb))M"
db7d74
	echo -n "Large modules:"
4a6108
	if [[ ${#large_mods[@]} -eq 0 ]]; then
db7d74
		echo " <none>"
db7d74
	else
db7d74
		echo ""
db7d74
		for _mod in "${!large_mods[@]}"; do
db7d74
			echo "    $_mod: ${large_mods[$_mod]}"
db7d74
		done
db7d74
	fi
db7d74
db7d74
	if [[ $reserved_size -lt $recommended_size ]]; then
db7d74
		echo "WARNING: Current crashkernel size is lower than recommended size $((recommended_size / size_mb))M."
db7d74
	fi
db7d74
}
db7d74
4a6108
reset_crashkernel()
4a6108
{
db7d74
	local kernel=$1 entry crashkernel_default
db7d74
	local grub_etc_default="/etc/default/grub"
db7d74
4a6108
	[[ -z $kernel ]] && kernel=$(uname -r)
4a6108
	crashkernel_default=$(cat "/usr/lib/modules/$kernel/crashkernel.default" 2> /dev/null)
db7d74
4a6108
	if [[ -z $crashkernel_default ]]; then
db7d74
		derror "$kernel doesn't have a crashkernel.default"
db7d74
		exit 1
db7d74
	fi
db7d74
db7d74
	if is_atomic; then
db7d74
		if rpm-ostree kargs | grep -q "crashkernel="; then
db7d74
			rpm-ostree --replace="crashkernel=$crashkernel_default"
db7d74
		else
db7d74
			rpm-ostree --append="crashkernel=$crashkernel_default"
db7d74
		fi
db7d74
	else
db7d74
		entry=$(grubby --info ALL | grep "^kernel=.*$kernel")
db7d74
		entry=${entry#kernel=}
db7d74
		entry=${entry#\"}
db7d74
		entry=${entry%\"}
db7d74
4a6108
		if [[ -f $grub_etc_default ]]; then
db7d74
			sed -i -e "s/^\(GRUB_CMDLINE_LINUX=.*\)crashkernel=[^\ \"]*\([\ \"].*\)$/\1$crashkernel_default\2/" "$grub_etc_default"
db7d74
		fi
db7d74
db7d74
		[[ -f /etc/zipl.conf ]] && zipl_arg="--zipl"
db7d74
		grubby --args "$crashkernel_default" --update-kernel "$entry" $zipl_arg
db7d74
		[[ $zipl_arg ]] && zipl > /dev/null
db7d74
	fi
4a6108
}
db7d74
4a6108
if [[ ! -f $KDUMP_CONFIG_FILE ]]; then
db7d74
	derror "Error: No kdump config file found!"
db7d74
	exit 1
db7d74
fi
db7d74
4a6108
main()
db7d74
{
db7d74
	# Determine if the dump mode is kdump or fadump
db7d74
	determine_dump_mode
db7d74
db7d74
	case "$1" in
4a6108
	start)
4a6108
		if [[ -s /proc/vmcore ]]; then
db7d74
			save_core
db7d74
			reboot
db7d74
		else
db7d74
			start
db7d74
		fi
db7d74
		;;
4a6108
	stop)
db7d74
		stop
db7d74
		;;
4a6108
	status)
db7d74
		EXIT_CODE=0
db7d74
		check_current_status
db7d74
		case "$?" in
4a6108
		0)
db7d74
			dinfo "Kdump is operational"
db7d74
			EXIT_CODE=0
db7d74
			;;
4a6108
		1)
db7d74
			dinfo "Kdump is not operational"
db7d74
			EXIT_CODE=3
db7d74
			;;
db7d74
		esac
db7d74
		exit $EXIT_CODE
db7d74
		;;
4a6108
	reload)
db7d74
		reload
db7d74
		;;
4a6108
	restart)
db7d74
		stop
db7d74
		start
db7d74
		;;
4a6108
	rebuild)
db7d74
		rebuild
db7d74
		;;
4a6108
	condrestart) ;;
4a6108
4a6108
	propagate)
db7d74
		propagate_ssh_key
db7d74
		;;
4a6108
	showmem)
db7d74
		show_reserved_mem
db7d74
		;;
4a6108
	estimate)
db7d74
		do_estimate
db7d74
		;;
4a6108
	reset-crashkernel)
db7d74
		reset_crashkernel "$2"
db7d74
		;;
4a6108
	*)
db7d74
		dinfo $"Usage: $0 {estimate|start|stop|status|restart|reload|rebuild|reset-crashkernel|propagate|showmem}"
db7d74
		exit 1
4a6108
		;;
db7d74
	esac
db7d74
}
db7d74
db7d74
# Other kdumpctl instances will block in queue, until this one exits
db7d74
single_instance_lock
db7d74
db7d74
# To avoid fd 9 leaking, we invoke a subshell, close fd 9 and call main.
db7d74
# So that fd isn't leaking when main is invoking a subshell.
4a6108
(
4a6108
	exec 9<&-
4a6108
	main "$@"
4a6108
)
db7d74
db7d74
exit $?