b9e861
#!/bin/bash
b9e861
KEXEC=/sbin/kexec
b9e861
b9e861
KDUMP_KERNELVER=""
fe2ad6
KDUMP_KERNEL=""
b9e861
KDUMP_COMMANDLINE=""
b9e861
KEXEC_ARGS=""
b9e861
KDUMP_CONFIG_FILE="/etc/kdump.conf"
fe2ad6
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=""
fe2ad6
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
fe2ad6
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
fe2ad6
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
fe2ad6
. $dracutbasedir/dracut-functions.sh
fe2ad6
. /lib/kdump/kdump-lib.sh
fe2ad6
. /lib/kdump/kdump-logger.sh
fe2ad6
fe2ad6
#initiate the kdump logger
fe2ad6
dlog_init
fe2ad6
if [ $? -ne 0 ]; then
fe2ad6
	echo "failed to initiate the kdump logger."
fe2ad6
	exit 1
fe2ad6
fi
fe2ad6
b9e861
single_instance_lock()
b9e861
{
b9e861
	local rc timeout=5
b9e861
b9e861
	exec 9>/var/lock/kdump
b9e861
	if [ $? -ne 0 ]; then
fe2ad6
		derror "Create file lock failed"
b9e861
		exit 1
b9e861
	fi
b9e861
b9e861
	flock -n 9
b9e861
	rc=$?
b9e861
b9e861
	while [ $rc -ne 0 ]; do
fe2ad6
		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
fe2ad6
		dinfo "Dump mode is fadump"
b9e861
		DEFAULT_DUMP_MODE="fadump"
b9e861
	fi
fe2ad6
	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
fe2ad6
	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
fe2ad6
		dinfo "saved a vmcore to $coredir"
b9e861
	else
fe2ad6
		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
fe2ad6
		ddebug "makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg"
b9e861
		makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg >/dev/null 2>&1
fe2ad6
		ddebug "dumpoops -d $coredir/dmesg"
b9e861
		dumpoops -d $coredir/dmesg >/dev/null 2>&1
b9e861
		if [ $? == 0 ]; then
fe2ad6
			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"
fe2ad6
	ddebug "rebuild fadump initrd: $target_initrd_tmp $DEFAULT_INITRD_BAK $KDUMP_KERNELVER"
fe2ad6
	$MKDUMPRD $target_initrd_tmp --rebuild $DEFAULT_INITRD_BAK --kver $KDUMP_KERNELVER \
b9e861
		-i /tmp/fadump.initramfs /etc/fadump.initramfs
b9e861
	if [ $? != 0 ]; then
fe2ad6
		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
fe2ad6
	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
{
fe2ad6
	ddebug "rebuild kdump initrd: $MKDUMPRD $TARGET_INITRD $KDUMP_KERNELVER"
fe2ad6
	$MKDUMPRD $TARGET_INITRD $KDUMP_KERNELVER
b9e861
	if [ $? != 0 ]; then
fe2ad6
		derror "mkdumprd: failed to make kdump initrd"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	if check_earlykdump_is_enabled; then
fe2ad6
		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
fe2ad6
		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
fe2ad6
		if [ ! -e "$file" ]; then
fe2ad6
			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
fe2ad6
			derror "Error: $file is not executable."
b9e861
			return 1
b9e861
		fi
b9e861
	done
b9e861
}
b9e861
b9e861
backup_default_initrd()
b9e861
{
fe2ad6
	ddebug "backup default initrd: $DEFAULT_INITRD"
fe2ad6
b9e861
	if [ ! -f "$DEFAULT_INITRD" ]; then
b9e861
		return
b9e861
	fi
b9e861
b9e861
	if [ ! -e $DEFAULT_INITRD_BAK ]; then
fe2ad6
		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
fe2ad6
			dwarn "WARNING: failed to backup $DEFAULT_INITRD."
b9e861
			rm -f $DEFAULT_INITRD_BAK
b9e861
		fi
b9e861
	fi
b9e861
}
b9e861
b9e861
restore_default_initrd()
b9e861
{
fe2ad6
	ddebug "restore default initrd: $DEFAULT_INITRD"
fe2ad6
fe2ad6
	if [ ! -f "$DEFAULT_INITRD" ]; then
fe2ad6
		return
fe2ad6
	fi
fe2ad6
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
fe2ad6
			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
fe2ad6
				derror "Restoring original initrd as fadump mode is disabled."
b9e861
				sync
b9e861
			fi
b9e861
		fi
b9e861
	fi
b9e861
}
b9e861
b9e861
check_config()
b9e861
{
fe2ad6
	local -A _opt_rec
b9e861
	while read config_opt config_val; do
fe2ad6
		if [ -z "$config_val" ]; then
fe2ad6
			derror "Invalid kdump config value for option $config_opt"
fe2ad6
			return 1
fe2ad6
		fi
fe2ad6
b9e861
		case "$config_opt" in
fe2ad6
		dracut_args)
fe2ad6
			if [[ $config_val == *--mount* ]]; then
fe2ad6
				if [ $(echo $config_val | grep -o "\-\-mount" | wc -l) -ne 1 ]; then
fe2ad6
					derror "Multiple mount targets specified in one \"dracut_args\"."
fe2ad6
					return 1
fe2ad6
				fi
fe2ad6
				config_opt=_target
fe2ad6
			fi
b9e861
			;;
fe2ad6
		raw)
fe2ad6
			if [ -d "/proc/device-tree/ibm,opal/dump" ]; then
fe2ad6
				derror "WARNING: Won't capture opalcore when 'raw' dump target is used."
fe2ad6
				return 1
73ea9d
			fi
fe2ad6
			config_opt=_target
fe2ad6
			;;
fe2ad6
		ext[234]|minix|btrfs|xfs|nfs|ssh)
fe2ad6
			config_opt=_target
fe2ad6
			;;
fe2ad6
		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)
fe2ad6
			derror "Deprecated kdump config option: $config_opt. Refer to kdump.conf manpage for alternatives."
b9e861
			return 1
b9e861
			;;
b9e861
		*)
fe2ad6
			derror "Invalid kdump config option $config_opt"
fe2ad6
			return 1
b9e861
			;;
b9e861
		esac
fe2ad6
fe2ad6
		if [ -n "${_opt_rec[$config_opt]}" ]; then
fe2ad6
			if [ $config_opt == _target ]; then
fe2ad6
				derror "More than one dump targets specified"
fe2ad6
			else
fe2ad6
				derror "Duplicated kdump config value of option $config_opt"
fe2ad6
			fi
fe2ad6
			return 1
fe2ad6
		fi
fe2ad6
		_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
{
fe2ad6
	prepare_kdump_bootinfo
fe2ad6
	if [ $? -ne 0 ]; then
fe2ad6
		derror "failed to prepare for kdump bootinfo."
fe2ad6
		return 1
b9e861
	fi
b9e861
fe2ad6
	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
fe2ad6
			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
fe2ad6
		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`
fe2ad6
	HOOKS="/etc/kdump/post.d/ /etc/kdump/pre.d/"
73ea9d
	if [ -d /etc/kdump/post.d ]; then
73ea9d
		for file in /etc/kdump/post.d/*; do
73ea9d
			if [ -x "$file" ]; then
73ea9d
				POST_FILES="$POST_FILES $file"
73ea9d
			fi
73ea9d
		done
73ea9d
	fi
73ea9d
	if [ -d /etc/kdump/pre.d ]; then
73ea9d
		for file in /etc/kdump/pre.d/*; do
73ea9d
			if [ -x "$file" ]; then
73ea9d
				PRE_FILES="$PRE_FILES $file"
73ea9d
			fi
73ea9d
		done
73ea9d
	fi
fe2ad6
	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`
fe2ad6
	# POST_FILES and PRE_FILES are already checked against executable, need not to check again.
fe2ad6
	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"
fe2ad6
	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
fe2ad6
		if [ -e /lib/modules/$KDUMP_KERNELVER/modules.dep ]; then
fe2ad6
			files="$files /lib/modules/$KDUMP_KERNELVER/modules.dep"
b9e861
		fi
b9e861
		for _module in $EXTRA_MODULES; do
fe2ad6
			_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
fe2ad6
				    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
fe2ad6
				if ! ( modprobe --set-version "$KDUMP_KERNELVER" --dry-run "$_module" &>/dev/null ); then
fe2ad6
					dwarn "Module $_module not found"
b9e861
				fi
b9e861
			fi
b9e861
		done
b9e861
	fi
b9e861
fe2ad6
	# HOOKS is mandatory and need to check the modification time
fe2ad6
	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
fe2ad6
			dwarn "$file doesn't exist"
b9e861
		fi
b9e861
	done
b9e861
b9e861
	if [ -n "$modified_files" ]; then
fe2ad6
		dinfo "Detected change(s) in the following file(s): $modified_files"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	return 0
b9e861
}
b9e861
fe2ad6
check_drivers_modified()
fe2ad6
{
fe2ad6
	local _target _new_drivers _old_drivers _module_name _module_filename
fe2ad6
fe2ad6
	# If it's dump target is on block device, detect the block driver
fe2ad6
	_target=$(get_block_dump_target)
fe2ad6
	if [[ -n "$_target" ]]; then
fe2ad6
		_record_block_drivers() {
fe2ad6
			local _drivers
fe2ad6
			_drivers=$(udevadm info -a "/dev/block/$1" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p')
fe2ad6
			for _driver in $_drivers; do
fe2ad6
				if ! [[ " $_new_drivers " == *" $_driver "* ]]; then
fe2ad6
					_new_drivers="$_new_drivers $_driver"
fe2ad6
				fi
fe2ad6
			done
fe2ad6
fe2ad6
			ddebug "MAJ:MIN=$1 drivers='$_drivers'"
fe2ad6
		}
fe2ad6
		check_block_and_slaves_all _record_block_drivers "$(get_maj_min "$_target")"
fe2ad6
	fi
fe2ad6
fe2ad6
	# Include watchdog drivers if watchdog module is not omitted
fe2ad6
	is_dracut_mod_omitted watchdog || is_dracut_mod_omitted watchdog-modules || _new_drivers+=" $(get_watchdog_drvs)"
fe2ad6
fe2ad6
	[ -z "$_new_drivers" ] && return 0
fe2ad6
	_old_drivers="$(lsinitrd $TARGET_INITRD -f /usr/lib/dracut/loaded-kernel-modules.txt | tr '\n' ' ')"
fe2ad6
fe2ad6
	ddebug "Modules required for kdump: '$_new_drivers'"
fe2ad6
	ddebug "Modules included in old initramfs: '$_old_drivers'"
fe2ad6
	for _driver in $_new_drivers; do
fe2ad6
		# Skip deprecated/invalid driver name or built-in module
fe2ad6
		_module_name=$(modinfo --set-version "$KDUMP_KERNELVER" -F name $_driver 2>/dev/null)
fe2ad6
		_module_filename=$(modinfo --set-version "$KDUMP_KERNELVER" -n $_driver 2>/dev/null)
fe2ad6
		if [ $? -ne 0 ] || [ -z "$_module_name" ] || [[ "$_module_filename" = *"(builtin)"* ]]; then
fe2ad6
			continue
fe2ad6
		fi
fe2ad6
		if ! [[ " $_old_drivers " == *" $_module_name "* ]]; then
fe2ad6
			dinfo "Detected change in block device driver, new loaded module: $_module_name"
fe2ad6
			return 1
fe2ad6
		fi
fe2ad6
	done
fe2ad6
}
fe2ad6
fe2ad6
check_fs_modified()
b9e861
{
b9e861
	local _old_dev _old_mntpoint _old_fstype
b9e861
	local _new_dev _new_mntpoint _new_fstype
fe2ad6
	local _target _dracut_args
b9e861
b9e861
	# No need to check in case of mount target specified via "dracut_args".
b9e861
	if is_mount_in_dracut_args; then
b9e861
		return 0
b9e861
	fi
b9e861
b9e861
	# No need to check in case of raw target.
b9e861
	# Currently we do not check also if ssh/nfs target is specified
b9e861
	if is_ssh_dump_target || is_nfs_dump_target || is_raw_dump_target; then
b9e861
		return 0
b9e861
	fi
b9e861
fe2ad6
	_target=$(get_block_dump_target)
fe2ad6
	_new_fstype=$(get_fs_type_from_target $_target)
fe2ad6
	if [[ -z "$_target" ]] || [[ -z "$_new_fstype" ]];then
fe2ad6
		derror "Dump target is invalid"
fe2ad6
		return 2
b9e861
	fi
b9e861
fe2ad6
	ddebug "_target=$_target _new_fstype=$_new_fstype"
fe2ad6
	_new_dev=$(kdump_get_persistent_dev $_target)
fe2ad6
	if [ -z "$_new_dev" ]; then
fe2ad6
		perror "Get persistent device name failed"
fe2ad6
		return 2
b9e861
	fi
b9e861
73ea9d
	_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
fe2ad6
		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
fe2ad6
	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
fe2ad6
	check_fs_modified
b9e861
	ret=$?
b9e861
	if [ $ret -ne 0 ]; then
b9e861
		return $ret
b9e861
	fi
b9e861
fe2ad6
	check_drivers_modified
fe2ad6
	ret=$?
fe2ad6
	if [ $ret -ne 0 ]; then
fe2ad6
		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
fe2ad6
			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
fe2ad6
			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
fe2ad6
		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
fe2ad6
		dinfo "No kdump initial ramdisk found."
b9e861
	elif [ "$capture_capable_initrd" == "0" ]; then
fe2ad6
		dinfo "Rebuild $TARGET_INITRD with dump capture support"
b9e861
	elif [ "$force_rebuild" != "0" ]; then
fe2ad6
		dinfo "Force rebuild $TARGET_INITRD"
b9e861
	elif [ "$system_modified" != "0" ]; then
b9e861
		:
b9e861
	else
b9e861
		return 0
b9e861
	fi
b9e861
fe2ad6
	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
{
fe2ad6
	local ret
fe2ad6
b9e861
	KEXEC_ARGS=$(prepare_kexec_args "${KEXEC_ARGS}")
b9e861
	KDUMP_COMMANDLINE=$(prepare_cmdline "${KDUMP_COMMANDLINE}" "${KDUMP_COMMANDLINE_REMOVE}" "${KDUMP_COMMANDLINE_APPEND}")
b9e861
fe2ad6
	# For secureboot enabled machines, use new kexec file based syscall.
fe2ad6
	# Old syscall will always fail as it does not have capability to
fe2ad6
	# to kernel signature verification.
fe2ad6
	if is_secure_boot_enforced; then
fe2ad6
		dinfo "Secure Boot is enabled. Using kexec file based syscall."
b9e861
		KEXEC_ARGS="$KEXEC_ARGS -s"
b9e861
	fi
b9e861
fe2ad6
	ddebug "$KEXEC $KEXEC_ARGS $standard_kexec_args --command-line=$KDUMP_COMMANDLINE --initrd=$TARGET_INITRD $KDUMP_KERNEL"
fe2ad6
fe2ad6
	# The '12' represents an intermediate temporary file descriptor
fe2ad6
	# to store the standard error file descriptor '2', and later
fe2ad6
	# restore the error file descriptor with the file descriptor '12'
fe2ad6
	# and release it.
fe2ad6
	exec 12>&2
fe2ad6
	exec 2>> $KDUMP_LOG_PATH/kdump.log
fe2ad6
	PS4='+ $(date "+%Y-%m-%d %H:%M:%S") ${BASH_SOURCE}@${LINENO}: '
fe2ad6
	set -x
fe2ad6
b9e861
	$KEXEC $KEXEC_ARGS $standard_kexec_args \
b9e861
		--command-line="$KDUMP_COMMANDLINE" \
fe2ad6
		--initrd=$TARGET_INITRD $KDUMP_KERNEL
fe2ad6
fe2ad6
	ret=$?
fe2ad6
	set +x
fe2ad6
	exec 2>&12 12>&-
fe2ad6
fe2ad6
	if [ $ret == 0 ]; then
fe2ad6
		dinfo "kexec: loaded kdump kernel"
b9e861
		return 0
b9e861
	else
fe2ad6
		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
fe2ad6
				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
fe2ad6
			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
fe2ad6
		ddebug "$errmsg"
fe2ad6
		echo $errmsg | grep -q "Permission denied\|No such file or directory\|Host key verification failed" &> /dev/null
bda30f
		if [ $? -eq 0 ]; then
fe2ad6
			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
fe2ad6
			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
fe2ad6
	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
fe2ad6
		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
fe2ad6
		dinfo "Using existing keys..."
b9e861
	else
fe2ad6
		dinfo "Generating new ssh keys... "
b9e861
		/usr/bin/ssh-keygen -t rsa -f $KEYFILE -N "" 2>&1 > /dev/null
fe2ad6
		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
fe2ad6
		dinfo "$KEYFILE has been added to ~$SSH_USER/.ssh/authorized_keys on $SSH_SERVER"
b9e861
		return 0
b9e861
	else
fe2ad6
		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
fe2ad6
    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" ] || {
fe2ad6
		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" ] || {
fe2ad6
		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
fe2ad6
		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
73ea9d
	if is_user_configured_dump_target; then
73ea9d
		if is_mount_in_dracut_args; then
73ea9d
		    return;
73ea9d
		fi
73ea9d
b9e861
		_target=$(local_fs_dump_target)
b9e861
		if [[ -n "$_target" ]]; then
73ea9d
			_mnt=$(get_mntpoint_from_target $_target)
73ea9d
			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
fe2ad6
			derror "Option fence_kdump_nodes cannot contain $hostname"
b9e861
			return 1
b9e861
		fi
b9e861
		# node can be ipaddr
fe2ad6
		echo "$ipaddrs " | grep "$node " > /dev/null
b9e861
		if [ $? -eq 0 ]; then
fe2ad6
			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
fe2ad6
		derror "fadump: failed to register"
b9e861
		return 1
b9e861
	fi
b9e861
fe2ad6
	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
fe2ad6
		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
	  *)
fe2ad6
		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
		  *)
fe2ad6
			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
fe2ad6
		derror "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	check_config
b9e861
	if [ $? -ne 0 ]; then
fe2ad6
		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
fe2ad6
		derror "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	check_current_status
b9e861
	if [ $? == 0 ]; then
fe2ad6
		dwarn "Kdump already running: [WARNING]"
b9e861
		return 0
b9e861
	fi
b9e861
b9e861
	if check_ssh_config; then
b9e861
		if ! check_ssh_target; then
fe2ad6
			derror "Starting kdump: [FAILED]"
b9e861
			return 1
b9e861
		fi
b9e861
	fi
b9e861
b9e861
	check_rebuild
b9e861
	if [ $? != 0 ]; then
fe2ad6
		derror "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	start_dump
b9e861
	if [ $? != 0 ]; then
fe2ad6
		derror "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
fe2ad6
	dinfo "Starting kdump: [OK]"
b9e861
}
b9e861
b9e861
reload()
b9e861
{
b9e861
	check_current_status
b9e861
	if [ $? -ne 0 ]; then
fe2ad6
		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
fe2ad6
		derror "Stopping kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
fe2ad6
	dinfo "Stopping kdump: [OK]"
b9e861
b9e861
	setup_initrd
b9e861
	if [ $? -ne 0 ]; then
fe2ad6
		derror "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	start_dump
b9e861
	if [ $? -ne 0 ]; then
fe2ad6
		derror "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
fe2ad6
	dinfo "Starting kdump: [OK]"
b9e861
}
b9e861
b9e861
stop_fadump()
b9e861
{
b9e861
	echo 0 > $FADUMP_REGISTER_SYS_NODE
b9e861
	if check_current_fadump_status; then
fe2ad6
		derror "fadump: failed to unregister"
b9e861
		return 1
b9e861
	fi
b9e861
fe2ad6
	dinfo "fadump: unregistered successfully"
b9e861
	return 0
b9e861
}
b9e861
b9e861
stop_kdump()
b9e861
{
fe2ad6
	if is_secure_boot_enforced; then
b9e861
		$KEXEC -s -p -u
b9e861
	else
b9e861
		$KEXEC -p -u
b9e861
	fi
b9e861
b9e861
	if [ $? != 0 ]; then
fe2ad6
		derror "kexec: failed to unload kdump kernel"
b9e861
		return 1
b9e861
	fi
b9e861
fe2ad6
	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
fe2ad6
		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
fe2ad6
		derror "Stopping kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
fe2ad6
	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
fe2ad6
	dinfo "Rebuilding $TARGET_INITRD"
b9e861
	rebuild_initrd
b9e861
	return $?
b9e861
}
b9e861
b9e861
if [ ! -f "$KDUMP_CONFIG_FILE" ]; then
fe2ad6
	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)
fe2ad6
			dinfo "Kdump is operational"
b9e861
			EXIT_CODE=0
b9e861
			;;
b9e861
		  1)
fe2ad6
			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
		;;
b9e861
	  *)
fe2ad6
		dinfo $"Usage: $0 {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 $?