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