b9e861
#!/bin/bash
b9e861
KEXEC=/sbin/kexec
b9e861
b9e861
KDUMP_KERNELVER=""
b9e861
KDUMP_COMMANDLINE=""
b9e861
KEXEC_ARGS=""
73ea9d
KDUMP_FILE_LOAD=""
b9e861
KDUMP_CONFIG_FILE="/etc/kdump.conf"
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=""
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
b9e861
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
b9e861
. $dracutbasedir/dracut-functions.sh
b9e861
. /lib/kdump/kdump-lib.sh
b9e861
b9e861
standard_kexec_args="-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
b9e861
single_instance_lock()
b9e861
{
b9e861
	local rc timeout=5
b9e861
b9e861
	exec 9>/var/lock/kdump
b9e861
	if [ $? -ne 0 ]; then
b9e861
		echo "Create file lock failed"
b9e861
		exit 1
b9e861
	fi
b9e861
b9e861
	flock -n 9
b9e861
	rc=$?
b9e861
b9e861
	while [ $rc -ne 0 ]; do
b9e861
		echo "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
b9e861
		echo "Dump mode is fadump"
b9e861
		DEFAULT_DUMP_MODE="fadump"
b9e861
	fi
b9e861
}
b9e861
b9e861
save_core()
b9e861
{
b9e861
	coredir="/var/crash/`date +"%Y-%m-%d-%H:%M"`"
b9e861
b9e861
	mkdir -p $coredir
b9e861
	cp --sparse=always /proc/vmcore $coredir/vmcore-incomplete
b9e861
	if [ $? == 0 ]; then
b9e861
		mv $coredir/vmcore-incomplete $coredir/vmcore
b9e861
		echo "saved a vmcore to $coredir"
b9e861
	else
b9e861
		echo "failed to save a vmcore to $coredir" >&2
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
b9e861
		makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg >/dev/null 2>&1
b9e861
		dumpoops -d $coredir/dmesg >/dev/null 2>&1
b9e861
		if [ $? == 0 ]; then
b9e861
			echo "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"
b9e861
	$MKDUMPRD $target_initrd_tmp --rebuild $DEFAULT_INITRD_BAK --kver $kdump_kver \
b9e861
		-i /tmp/fadump.initramfs /etc/fadump.initramfs
b9e861
	if [ $? != 0 ]; then
b9e861
		echo "mkdumprd: failed to rebuild initrd with fadump support" >&2
b9e861
		rm -f /tmp/fadump.initramfs
b9e861
		return 1
b9e861
	fi
b9e861
	rm -f /tmp/fadump.initramfs
b9e861
b9e861
	# updating fadump 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
{
b9e861
	$MKDUMPRD $TARGET_INITRD $kdump_kver
b9e861
	if [ $? != 0 ]; then
b9e861
		echo "mkdumprd: failed to make kdump initrd" >&2
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	if check_earlykdump_is_enabled; then
b9e861
		echo "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
b9e861
		echo "$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
b9e861
		if [ ! -f "$file" ]; then
b9e861
			echo -n "Error: $file not found."; echo
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
b9e861
			echo -n "Error: $file is not executable."; echo
b9e861
			return 1
b9e861
		fi
b9e861
	done
b9e861
}
b9e861
b9e861
backup_default_initrd()
b9e861
{
b9e861
	if [ ! -f "$DEFAULT_INITRD" ]; then
b9e861
		return
b9e861
	fi
b9e861
b9e861
	if [ ! -e $DEFAULT_INITRD_BAK ]; then
b9e861
		echo "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
b9e861
			echo "WARNING: failed to backup $DEFAULT_INITRD."
b9e861
			rm -f $DEFAULT_INITRD_BAK
b9e861
		fi
b9e861
	fi
b9e861
}
b9e861
b9e861
restore_default_initrd()
b9e861
{
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
b9e861
			echo "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
b9e861
				echo -n "Restoring original initrd as fadump mode "
b9e861
				echo "is disabled."
b9e861
				sync
b9e861
			fi
b9e861
		fi
b9e861
	fi
b9e861
}
b9e861
b9e861
check_config()
b9e861
{
b9e861
	local nr
b9e861
b9e861
	nr=$(awk 'BEGIN{cnt=0} /^raw|^ssh[[:blank:]]|^nfs|^ext[234]|^xfs|^btrfs|^minix|^dracut_args .*\-\-mount/{cnt++} END{print cnt}' $KDUMP_CONFIG_FILE)
b9e861
	[ $nr -gt 1 ] && {
b9e861
		echo "More than one dump targets specified."
b9e861
		return 1
b9e861
	}
b9e861
b9e861
	nr=$(grep "^dracut_args .*\-\-mount" $KDUMP_CONFIG_FILE | grep -o "\-\-mount" | wc -l)
b9e861
	[ $nr -gt 1 ] && {
b9e861
		echo "Multiple mount targets specified in one \"dracut_args\"."
b9e861
		return 1
b9e861
	}
b9e861
b9e861
	# Check if we have any leading spaces (or tabs) before the
b9e861
	# variable name in the kdump conf file
b9e861
	if grep -E -q '^[[:blank:]]+[a-z]' $KDUMP_CONFIG_FILE; then
b9e861
		echo "No whitespaces are allowed before a kdump option name in $KDUMP_CONFIG_FILE"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	while read config_opt config_val; do
b9e861
		case "$config_opt" in
b9e861
		\#* | "")
b9e861
			;;
b9e861
		raw|ext2|ext3|ext4|minix|btrfs|xfs|nfs|ssh|sshkey|path|core_collector|kdump_post|kdump_pre|extra_bins|extra_modules|failure_action|default|final_action|force_rebuild|force_no_rebuild|dracut_args|fence_kdump_args|fence_kdump_nodes)
b9e861
			# remove inline comments after the end of a directive.
b9e861
			[ -z "$config_val" ] && {
b9e861
				echo "Invalid kdump config value for option $config_opt."
b9e861
				return 1;
b9e861
			}
73ea9d
			if [ -d "/proc/device-tree/ibm,opal/dump" ] && [ "$config_opt" == "raw" ]; then
73ea9d
				echo "WARNING: Won't capture opalcore when 'raw' dump target is used."
73ea9d
			fi
b9e861
			;;
b9e861
		net|options|link_delay|disk_timeout|debug_mem_level|blacklist)
b9e861
			echo "Deprecated kdump config option: $config_opt. Refer to kdump.conf manpage for alternatives."
b9e861
			return 1
b9e861
			;;
b9e861
		*)
b9e861
			echo "Invalid kdump config option $config_opt"
b9e861
			return 1;
b9e861
			;;
b9e861
		esac
b9e861
	done <<< "$(read_strip_comments $KDUMP_CONFIG_FILE)"
b9e861
b9e861
	check_failure_action_config || return 1
b9e861
	check_final_action_config || return 1
b9e861
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
{
b9e861
	KDUMP_BOOTDIR=$(check_boot_dir "${KDUMP_BOOTDIR}")
b9e861
b9e861
	if [ -z "$KDUMP_KERNELVER" ]; then
b9e861
		kdump_kver=`uname -r`
b9e861
	else
b9e861
		kdump_kver=$KDUMP_KERNELVER
b9e861
	fi
b9e861
b9e861
	kdump_kernel="${KDUMP_BOOTDIR}/${KDUMP_IMG}-${kdump_kver}${KDUMP_IMG_EXT}"
b9e861
b9e861
	DEFAULT_INITRD="${KDUMP_BOOTDIR}/initramfs-`uname -r`.img"
b9e861
	DEFAULT_INITRD_BAK="${KDUMP_BOOTDIR}/.initramfs-`uname -r`.img.default"
b9e861
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
b9e861
		TARGET_INITRD="$DEFAULT_INITRD"
b9e861
		if [ ! -s "$TARGET_INITRD" ]; then
b9e861
			echo "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
b9e861
		TARGET_INITRD="${KDUMP_BOOTDIR}/initramfs-${kdump_kver}kdump.img"
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`
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
b9e861
	CORE_COLLECTOR=`grep ^core_collector $KDUMP_CONFIG_FILE | cut -d\  -f2`
b9e861
	CORE_COLLECTOR=`type -P $CORE_COLLECTOR`
73ea9d
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES $POST_FILES $PRE_FILES"
b9e861
	CHECK_FILES=`grep ^extra_bins $KDUMP_CONFIG_FILE | cut -d\  -f2-`
b9e861
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
b9e861
	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
b9e861
		if [ -e /lib/modules/$kdump_kver/modules.dep ]; then
b9e861
			files="$files /lib/modules/$kdump_kver/modules.dep"
b9e861
		fi
b9e861
		for _module in $EXTRA_MODULES; do
b9e861
			_module_file="$(modinfo --set-version "$kdump_kver" --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
b9e861
				    files="$files $(modinfo --set-version "$kdump_kver" --filename $_dep_modules 2>/dev/null)"
b9e861
				done
b9e861
			else
b9e861
				# If it's not a module nor builtin, give an error
b9e861
				if ! ( modprobe --set-version "$kdump_kver" --dry-run "$_module" &>/dev/null ); then
b9e861
					echo "Module $_module not found"
b9e861
				fi
b9e861
			fi
b9e861
		done
b9e861
	fi
b9e861
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
b9e861
			echo "$file doesn't exist"
b9e861
		fi
b9e861
	done
b9e861
b9e861
	if [ -n "$modified_files" ]; then
b9e861
		echo "Detected change(s) in the following file(s):"
b9e861
		echo -n "  "; echo "$modified_files" | sed 's/\s/\n  /g'
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	return 0
b9e861
}
b9e861
b9e861
check_dump_fs_modified()
b9e861
{
b9e861
	local _old_dev _old_mntpoint _old_fstype
b9e861
	local _new_dev _new_mntpoint _new_fstype
b9e861
	local _target _path _dracut_args
b9e861
	local _target_drivers _module_name
b9e861
b9e861
	local _old_drivers="$(lsinitrd $TARGET_INITRD -f /usr/lib/dracut/loaded-kernel-modules.txt | tr '\n' ' ')"
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
b9e861
	_target=$(get_user_configured_dump_disk)
b9e861
b9e861
	if [[ -n "$_target" ]]; then
b9e861
		_target=$(to_dev_name $_target)
b9e861
		_new_fstype=$(blkid $_target | awk -F"TYPE=" '{print $2}' | cut -d '"' -f 2)
b9e861
	else
b9e861
		_path=$(get_save_path)
b9e861
		_target=$(get_target_from_path $_path)
b9e861
		_target=$(to_dev_name $_target)
b9e861
		_new_fstype=$(get_fs_type_from_target $_target)
b9e861
		if [[ -z "$_target" || -z "$_new_fstype" ]];then
b9e861
			echo "Dump path $_path does not exist"
b9e861
			return 2
b9e861
		fi
b9e861
	fi
b9e861
b9e861
	_record_block_drivers() {
b9e861
		local _drivers
b9e861
		if [[ -b /dev/block/$1 ]]; then
b9e861
			_drivers=$(udevadm info -a "/dev/block/$1" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p')
b9e861
		fi
b9e861
		if [[ -b $1 ]]; then
b9e861
			_drivers=$(udevadm info -a "$1" | sed -n 's/\s*DRIVERS=="\(\S\+\)"/\1/p')
b9e861
		fi
b9e861
		for _driver in $_drivers; do
b9e861
			if ! [[ " $_target_drivers " == *" $_driver "* ]]; then
b9e861
				_target_drivers="$_target_drivers $_driver"
b9e861
			fi
b9e861
		done
b9e861
		return 1
b9e861
	}
b9e861
b9e861
	check_block_and_slaves_all _record_block_drivers "$(get_maj_min "$_target")"
b9e861
	for _driver in $_target_drivers; do
b9e861
		# Target is mounted already, if module is not included by current kernel,
b9e861
		# could be a deprecated/invalid driver name or a built-in module
b9e861
		_module_name=$(modinfo --set-version "$kdump_kver" -F name $_driver 2>/dev/null)
b9e861
		if [ $? -ne 0 ] || [ -z "$_module_name" ]; then
b9e861
			continue
b9e861
		fi
b9e861
		if ! [[ " $_old_drivers " == *" $_module_name "* ]]; then
b9e861
			echo "Detected change in block device driver, new loaded module: $_module_name"
b9e861
			return 1
b9e861
		fi
b9e861
	done
b9e861
b9e861
	if [[ $(expr substr $_new_fstype 1 3) = "nfs" ]];then
b9e861
		_new_dev=$_target
b9e861
	else
73ea9d
		_new_dev=$(kdump_get_persistent_dev $_target)
b9e861
		if [ -z "$_new_dev" ]; then
b9e861
			echo "Get persistent device name failed"
b9e861
			return 2
b9e861
		fi
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
b9e861
		echo "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
b9e861
	echo "Detected change in File System"
b9e861
	return 1
b9e861
}
b9e861
b9e861
check_wdt_modified()
b9e861
{
b9e861
	local -A _drivers
b9e861
	local _alldrivers _active _wdtdrv _wdtppath _dir
b9e861
	local wd_old wd_new
b9e861
b9e861
	is_wdt_mod_omitted
b9e861
	[[ $? -eq 0 ]] && return 0
b9e861
	[[ -d /sys/class/watchdog/ ]] || return 0
b9e861
b9e861
	# Copied logic from dracut 04watchdog/module-setup.sh::installkernel()
b9e861
	for _dir in /sys/class/watchdog/*; do
b9e861
		[[ -d "$_dir" ]] || continue
b9e861
		[[ -f "$_dir/state" ]] || continue
b9e861
		_active=$(< "$_dir/state")
b9e861
		[[ "$_active" =  "active" ]] || continue
b9e861
		# device/modalias will return driver of this device
b9e861
		_wdtdrv=$(< "$_dir/device/modalias")
b9e861
		# There can be more than one module represented by same
b9e861
		# modalias. Currently load all of them.
b9e861
		# TODO: Need to find a way to avoid any unwanted module
b9e861
		# represented by modalias
b9e861
		_wdtdrv=$(modprobe --set-version "$kdump_kver" -R $_wdtdrv 2>/dev/null)
b9e861
		if [[ $_wdtdrv ]]; then
b9e861
			for i in $_wdtdrv; do
b9e861
				_drivers[$i]=1
b9e861
			done
b9e861
		fi
b9e861
		# however in some cases, we also need to check that if there is
b9e861
		# a specific driver for the parent bus/device.  In such cases
b9e861
		# we also need to enable driver for parent bus/device.
b9e861
		_wdtppath=$(readlink -f "$_dir/device")
b9e861
		while [[ -d "$_wdtppath" ]] && [[ "$_wdtppath" != "/sys" ]]; do
b9e861
			_wdtppath=$(readlink -f "$_wdtppath/..")
b9e861
			[[ -f "$_wdtppath/modalias" ]] || continue
b9e861
b9e861
			_wdtdrv=$(< "$_wdtppath/modalias")
b9e861
			_wdtdrv=$(modprobe --set-version "$kdump_kver" -R $_wdtdrv 2>/dev/null)
b9e861
			if [[ $_wdtdrv ]]; then
b9e861
				for i in $_wdtdrv; do
b9e861
					_drivers[$i]=1
b9e861
				done
b9e861
			fi
b9e861
		done
b9e861
	done
b9e861
b9e861
	# ensure that watchdog module is loaded as early as possible
b9e861
	_alldrivers="${!_drivers[*]}"
b9e861
	[[ $_alldrivers ]] && wd_new="rd.driver.pre=${_alldrivers// /,}"
b9e861
	wd_old=$(lsinitrd $TARGET_INITRD -f etc/cmdline.d/00-watchdog.conf)
b9e861
b9e861
	[[ "$wd_old" = "$wd_new" ]] && return 0
b9e861
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
b9e861
	check_dump_fs_modified
b9e861
	ret=$?
b9e861
	if [ $ret -ne 0 ]; then
b9e861
		return $ret
b9e861
	fi
b9e861
b9e861
	check_wdt_modified
b9e861
	if [ $? -ne 0 ]; then
b9e861
		echo "Detected change in watchdog state"
b9e861
		return 1
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
b9e861
			echo "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
b9e861
			echo "Error: force_rebuild value is invalid"
b9e861
			return 1
b9e861
		fi
b9e861
	fi
b9e861
b9e861
	if [[ "$force_no_rebuild" == "1" && "$force_rebuild" == "1" ]]; then
b9e861
		echo "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
b9e861
		echo  -n "No kdump initial ramdisk found."; echo
b9e861
	elif [ "$capture_capable_initrd" == "0" ]; then
b9e861
		echo -n "Rebuild $TARGET_INITRD with dump capture support"; echo
b9e861
	elif [ "$force_rebuild" != "0" ]; then
b9e861
		echo -n "Force rebuild $TARGET_INITRD"; echo
b9e861
	elif [ "$system_modified" != "0" ]; then
b9e861
		:
b9e861
	else
b9e861
		return 0
b9e861
	fi
b9e861
b9e861
	echo "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
{
b9e861
	KEXEC_ARGS=$(prepare_kexec_args "${KEXEC_ARGS}")
b9e861
	KDUMP_COMMANDLINE=$(prepare_cmdline "${KDUMP_COMMANDLINE}" "${KDUMP_COMMANDLINE_REMOVE}" "${KDUMP_COMMANDLINE_APPEND}")
b9e861
73ea9d
	if [ "$KDUMP_FILE_LOAD" == "on" ]; then
73ea9d
		echo "Using kexec file based syscall."
b9e861
		KEXEC_ARGS="$KEXEC_ARGS -s"
b9e861
	fi
b9e861
b9e861
	$KEXEC $KEXEC_ARGS $standard_kexec_args \
b9e861
		--command-line="$KDUMP_COMMANDLINE" \
b9e861
		--initrd=$TARGET_INITRD $kdump_kernel
b9e861
	if [ $? == 0 ]; then
b9e861
		echo "kexec: loaded kdump kernel"
b9e861
		return 0
b9e861
	else
b9e861
		echo "kexec: failed to load kdump kernel" >&2
73ea9d
		if [ "$KDUMP_FILE_LOAD" == "on" ]; then
73ea9d
			echo "kexec_file_load() failed, please try kexec_load()" >&2
73ea9d
		fi
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
b9e861
				echo "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
bda30f
			echo "Could not create $DUMP_TARGET:$SAVE_PATH, you should check the privilege on server side"  >&2
bda30f
			return 1
bda30f
		fi
bda30f
bda30f
		# if server removes the authorized_keys or, no /root/.ssh/kdump_id_rsa
bda30f
		echo $errmsg | grep -q "Permission denied\|No such file or directory\|Host key verification failed"
bda30f
		if [ $? -eq 0 ]; then
bda30f
			echo "Could not create $DUMP_TARGET:$SAVE_PATH, you probably need to run \"kdumpctl propagate\""  >&2
bda30f
			return 1
bda30f
		fi
bda30f
bda30f
		if [ $warn_once -eq 1 ]; then
bda30f
			echo "Network dump target is not usable, waiting for it to be ready"
bda30f
			warn_once=0
bda30f
		fi
bda30f
		echo -n .
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
bda30f
	echo "Could not create $DUMP_TARGET:$SAVE_PATH, ipaddr is not ready yet. You should check network connection"  >&2
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
b9e861
		echo "No ssh config specified in $KDUMP_CONFIG_FILE.  Can't propagate" >&2
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
b9e861
		echo "Using existing keys..."
b9e861
	else
b9e861
		echo -n "Generating new ssh keys... "
b9e861
		/usr/bin/ssh-keygen -t rsa -f $KEYFILE -N "" 2>&1 > /dev/null
b9e861
		echo "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
b9e861
		echo $KEYFILE has been added to ~$SSH_USER/.ssh/authorized_keys on $SSH_SERVER
b9e861
		return 0
b9e861
	else
b9e861
		echo $errmsg, $KEYFILE failed in transfer to $SSH_SERVER  >&2
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
b9e861
    echo "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" ] || {
b9e861
		echo "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" ] || {
b9e861
		echo "failed to create $coredir"
b9e861
		return 1
b9e861
	}
b9e861
	if makedumpfile -R $coredir/vmcore <$raw_target >/dev/null 2>&1; then
b9e861
		# dump found
b9e861
		echo "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
b9e861
			echo "Option fence_kdump_nodes cannot contain $hostname"
b9e861
			return 1
b9e861
		fi
b9e861
		# node can be ipaddr
b9e861
		echo $ipaddrs | grep $node > /dev/null
b9e861
		if [ $? -eq 0 ]; then
b9e861
			echo "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
b9e861
		echo "fadump: failed to register"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	echo "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
b9e861
		echo "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
	  *)
b9e861
		echo $"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
		  *)
b9e861
			echo $"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
b9e861
		echo "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	check_config
b9e861
	if [ $? -ne 0 ]; then
b9e861
		echo "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
b9e861
		echo "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	check_current_status
b9e861
	if [ $? == 0 ]; then
b9e861
		echo "Kdump already running: [WARNING]"
b9e861
		return 0
b9e861
	fi
b9e861
b9e861
	if check_ssh_config; then
b9e861
		if ! check_ssh_target; then
b9e861
			echo "Starting kdump: [FAILED]"
b9e861
			return 1
b9e861
		fi
b9e861
	fi
b9e861
b9e861
	check_rebuild
b9e861
	if [ $? != 0 ]; then
b9e861
		echo "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	start_dump
b9e861
	if [ $? != 0 ]; then
b9e861
		echo "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	echo "Starting kdump: [OK]"
b9e861
}
b9e861
b9e861
reload()
b9e861
{
b9e861
	check_current_status
b9e861
	if [ $? -ne 0 ]; then
bda30f
		echo "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
b9e861
		echo "Stopping kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	echo "Stopping kdump: [OK]"
b9e861
b9e861
	setup_initrd
b9e861
	if [ $? -ne 0 ]; then
b9e861
		echo "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	start_dump
b9e861
	if [ $? -ne 0 ]; then
b9e861
		echo "Starting kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	echo "Starting kdump: [OK]"
b9e861
}
b9e861
b9e861
stop_fadump()
b9e861
{
b9e861
	echo 0 > $FADUMP_REGISTER_SYS_NODE
b9e861
	if check_current_fadump_status; then
b9e861
		echo "fadump: failed to unregister"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	echo "fadump: unregistered successfully"
b9e861
	return 0
b9e861
}
b9e861
b9e861
stop_kdump()
b9e861
{
73ea9d
	if [ "$KDUMP_FILE_LOAD" == "on" ]; then
b9e861
		$KEXEC -s -p -u
b9e861
	else
b9e861
		$KEXEC -p -u
b9e861
	fi
b9e861
b9e861
	if [ $? != 0 ]; then
b9e861
		echo "kexec: failed to unload kdump kernel"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	echo "kexec: unloaded kdump kernel"
b9e861
	return 0
b9e861
}
b9e861
b9e861
reload_fadump()
b9e861
{
b9e861
	echo 1 > $FADUMP_REGISTER_SYS_NODE
b9e861
	if [ $? == 0 ]; then
b9e861
		echo "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
b9e861
		echo "Stopping kdump: [FAILED]"
b9e861
		return 1
b9e861
	fi
b9e861
b9e861
	echo "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
b9e861
	echo "Rebuilding $TARGET_INITRD"
b9e861
	rebuild_initrd
b9e861
	return $?
b9e861
}
b9e861
b9e861
if [ ! -f "$KDUMP_CONFIG_FILE" ]; then
b9e861
	echo "Error: No kdump config file found!"  >&2
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)
b9e861
			echo "Kdump is operational"
b9e861
			EXIT_CODE=0
b9e861
			;;
b9e861
		  1)
b9e861
			echo "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
	  *)
b9e861
		echo $"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 $?