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