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