23ef29
#!/bin/bash
23ef29
KEXEC=/sbin/kexec
23ef29
23ef29
KDUMP_KERNELVER=""
23ef29
KDUMP_COMMANDLINE=""
23ef29
KEXEC_ARGS=""
23ef29
KDUMP_CONFIG_FILE="/etc/kdump.conf"
23ef29
MKDUMPRD="/sbin/mkdumprd -f"
23ef29
DRACUT_MODULES_FILE="/usr/lib/dracut/modules.txt"
23ef29
SAVE_PATH=/var/crash
23ef29
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
23ef29
INITRD_CHECKSUM_LOCATION="/boot/.fadump_initrd_checksum"
23ef29
DUMP_TARGET=""
23ef29
DEFAULT_INITRD=""
23ef29
DEFAULT_INITRD_BAK=""
23ef29
TARGET_INITRD=""
23ef29
FADUMP_REGISTER_SYS_NODE="/sys/kernel/fadump_registered"
23ef29
#kdump shall be the default dump mode
23ef29
DEFAULT_DUMP_MODE="kdump"
23ef29
image_time=0
23ef29
23ef29
. /lib/kdump/kdump-lib.sh
23ef29
23ef29
standard_kexec_args="-p"
23ef29
23ef29
# Some default values in case /etc/sysconfig/kdump doesn't include
23ef29
KDUMP_COMMANDLINE_REMOVE="hugepages hugepagesz slub_debug"
23ef29
23ef29
if [ -f /etc/sysconfig/kdump ]; then
23ef29
	. /etc/sysconfig/kdump
23ef29
fi
23ef29
23ef29
single_instance_lock()
23ef29
{
23ef29
	local rc timeout=5
23ef29
23ef29
	exec 9>/var/lock/kdump
23ef29
1d9674
	if [ $? -ne 0 ]; then
1d9674
		echo "Create file lock failed"
1d9674
		exit 1
1d9674
	fi
1d9674
23ef29
	flock -n 9
23ef29
	rc=$?
23ef29
23ef29
	while [ $rc -ne 0 ]; do
23ef29
		echo "Another app is currently holding the kdump lock; waiting for it to exit..."
23ef29
		flock -w $timeout 9
23ef29
		rc=$?
23ef29
	done
23ef29
}
23ef29
23ef29
determine_dump_mode()
23ef29
{
23ef29
	# Check if firmware-assisted dump is enabled
23ef29
	# if yes, set the dump mode as fadump
23ef29
	if is_fadump_capable; then
23ef29
		echo "Dump mode is fadump"
23ef29
		DEFAULT_DUMP_MODE="fadump"
23ef29
	fi
23ef29
}
23ef29
23ef29
# remove_cmdline_param <kernel cmdline> <param1> [<param2>] ... [<paramN>]
23ef29
# Remove a list of kernel parameters from a given kernel cmdline and print the result.
23ef29
# For each "arg" in the removing params list, "arg" and "arg=xxx" will be removed if exists.
23ef29
remove_cmdline_param()
23ef29
{
23ef29
	local cmdline=$1
23ef29
	shift
23ef29
23ef29
	for arg in $@; do
23ef29
		cmdline=`echo $cmdline | \
1d9674
			 sed -e "s/\b$arg=[^ ]*//g" \
1d9674
			     -e "s/^$arg\b//g" \
1d9674
			     -e "s/[[:space:]]$arg\b//g" \
23ef29
			     -e "s/\s\+/ /g"`
23ef29
	done
23ef29
	echo $cmdline
23ef29
}
23ef29
23ef29
#
1d9674
# This function returns the "apicid" of the boot
1d9674
# cpu (cpu 0) if present.
23ef29
#
1d9674
get_bootcpu_apicid()
23ef29
{
23ef29
    awk '							\
23ef29
	BEGIN { CPU = "-1"; }					\
23ef29
	$1=="processor" && $2==":"	{ CPU = $NF; }		\
1d9674
	CPU=="0" && /^apicid/		{ print $NF; }		\
23ef29
	'							\
23ef29
	/proc/cpuinfo
23ef29
}
23ef29
23ef29
#
23ef29
# This function appends argument "$2=$3" to string ($1) if not already present.
23ef29
#
23ef29
append_cmdline()
23ef29
{
23ef29
	local cmdline=$1
23ef29
	local newstr=${cmdline/$2/""}
23ef29
23ef29
	# unchanged str implies argument wasn't there
23ef29
	if [ "$cmdline" == "$newstr" ]; then
23ef29
		cmdline="${cmdline} ${2}=${3}"
23ef29
	fi
23ef29
23ef29
	echo $cmdline
23ef29
}
23ef29
1d9674
# This function performs a series of edits on the command line.
1d9674
# Store the final result in global $KDUMP_COMMANDLINE.
23ef29
prepare_cmdline()
23ef29
{
1d9674
	local cmdline id
1d9674
23ef29
	if [ -z "$KDUMP_COMMANDLINE" ]; then
1d9674
		cmdline=$(cat /proc/cmdline)
23ef29
	else
23ef29
		cmdline=${KDUMP_COMMANDLINE}
23ef29
	fi
1d9674
23ef29
	# These params should always be removed
1d9674
	cmdline=$(remove_cmdline_param "$cmdline" crashkernel panic_on_warn)
23ef29
	# These params can be removed configurably
1d9674
	cmdline=$(remove_cmdline_param "$cmdline" ${KDUMP_COMMANDLINE_REMOVE})
1d9674
1d9674
	# Always remove "root=X", as we now explicitly generate all kinds
1d9674
	# of dump target mount information including root fs.
1d9674
	#
1d9674
	# We do this before KDUMP_COMMANDLINE_APPEND, if one really cares
1d9674
	# about it(e.g. for debug purpose), then can pass "root=X" using
1d9674
	# KDUMP_COMMANDLINE_APPEND.
1d9674
	cmdline=$(remove_cmdline_param "$cmdline" root)
23ef29
23ef29
	cmdline="${cmdline} ${KDUMP_COMMANDLINE_APPEND}"
23ef29
1d9674
	id=$(get_bootcpu_apicid)
23ef29
	if [ ! -z ${id} ] ; then
1d9674
		cmdline=$(append_cmdline "${cmdline}" disable_cpu_apicid ${id})
23ef29
	fi
23ef29
1d9674
	KDUMP_COMMANDLINE=$cmdline
23ef29
}
23ef29
23ef29
23ef29
save_core()
23ef29
{
23ef29
	coredir="/var/crash/`date +"%Y-%m-%d-%H:%M"`"
23ef29
23ef29
	mkdir -p $coredir
23ef29
	cp --sparse=always /proc/vmcore $coredir/vmcore-incomplete
23ef29
	if [ $? == 0 ]; then
23ef29
		mv $coredir/vmcore-incomplete $coredir/vmcore
23ef29
		echo "saved a vmcore to $coredir"
23ef29
	else
23ef29
		echo "failed to save a vmcore to $coredir" >&2
23ef29
	fi
23ef29
23ef29
	# pass the dmesg to Abrt tool if exists, in order
23ef29
	# to collect the kernel oops message.
23ef29
	# https://fedorahosted.org/abrt/
23ef29
	if [ -x /usr/bin/dumpoops ]; then
23ef29
		makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg >/dev/null 2>&1
23ef29
		dumpoops -d $coredir/dmesg >/dev/null 2>&1
23ef29
		if [ $? == 0 ]; then
23ef29
			echo "kernel oops has been collected by abrt tool"
23ef29
		fi
23ef29
	fi
23ef29
}
23ef29
23ef29
rebuild_fadump_initrd()
23ef29
{
23ef29
	local target_initrd_tmp
23ef29
23ef29
	# this file tells the initrd is fadump enabled
23ef29
	touch /tmp/fadump.initramfs
23ef29
	target_initrd_tmp="$TARGET_INITRD.tmp"
23ef29
	$MKDUMPRD $target_initrd_tmp --rebuild $TARGET_INITRD --kver $kdump_kver \
23ef29
		-i /tmp/fadump.initramfs /etc/fadump.initramfs
23ef29
	if [ $? != 0 ]; then
23ef29
		echo "mkdumprd: failed to rebuild initrd with fadump support" >&2
23ef29
		rm -f /tmp/fadump.initramfs
23ef29
		return 1
23ef29
	fi
23ef29
	rm -f /tmp/fadump.initramfs
23ef29
23ef29
	# updating fadump initrd
23ef29
	mv $target_initrd_tmp $TARGET_INITRD
23ef29
	sync
23ef29
23ef29
	return 0
23ef29
}
23ef29
23ef29
rebuild_kdump_initrd()
23ef29
{
23ef29
	$MKDUMPRD $TARGET_INITRD $kdump_kver
23ef29
	if [ $? != 0 ]; then
23ef29
		echo "mkdumprd: failed to make kdump initrd" >&2
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	return 0
23ef29
}
23ef29
23ef29
rebuild_initrd()
23ef29
{
23ef29
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
23ef29
		rebuild_fadump_initrd
23ef29
	else
23ef29
		rebuild_kdump_initrd
23ef29
	fi
23ef29
23ef29
	return $?
23ef29
}
23ef29
23ef29
#$1: the files to be checked with IFS=' '
23ef29
check_exist()
23ef29
{
23ef29
	for file in $1; do
23ef29
		if [ ! -f "$file" ]; then
23ef29
			echo -n "Error: $file not found."; echo
23ef29
			return 1
23ef29
		fi
23ef29
	done
23ef29
}
23ef29
23ef29
#$1: the files to be checked with IFS=' '
23ef29
check_executable()
23ef29
{
23ef29
	for file in $1; do
23ef29
		if [ ! -x "$file" ]; then
23ef29
			echo -n "Error: $file is not executable."; echo
23ef29
			return 1
23ef29
		fi
23ef29
	done
23ef29
}
23ef29
23ef29
backup_default_initrd()
23ef29
{
23ef29
	if [ ! -f "$DEFAULT_INITRD" ]; then
23ef29
		return
23ef29
	fi
23ef29
23ef29
	if [ ! -e $DEFAULT_INITRD_BAK ]; then
23ef29
		echo "Backing up $DEFAULT_INITRD before rebuild."
23ef29
		# save checksum to verify before restoring
23ef29
		sha1sum $DEFAULT_INITRD > $INITRD_CHECKSUM_LOCATION
23ef29
		cp $DEFAULT_INITRD $DEFAULT_INITRD_BAK
23ef29
		if [ $? -ne 0 ]; then
23ef29
			echo "WARNING: failed to backup $DEFAULT_INITRD."
23ef29
			rm -f $DEFAULT_INITRD_BAK
23ef29
		fi
23ef29
	fi
23ef29
}
23ef29
23ef29
restore_default_initrd()
23ef29
{
23ef29
	# If a backup initrd exists, we must be switching back from
23ef29
	# fadump to kdump. Restore the original default initrd.
23ef29
	if [ -f $DEFAULT_INITRD_BAK ] && [ -f $INITRD_CHECKSUM_LOCATION ]; then
23ef29
		# verify checksum before restoring
23ef29
		backup_checksum=`sha1sum $DEFAULT_INITRD_BAK | awk '{ print $1 }'`
23ef29
		default_checksum=`cat $INITRD_CHECKSUM_LOCATION | awk '{ print $1 }'`
23ef29
		if [ "$default_checksum" != "$backup_checksum" ]; then
23ef29
			echo "WARNING: checksum mismatch! Can't restore original initrd.."
23ef29
		else
23ef29
			rm -f $INITRD_CHECKSUM_LOCATION
23ef29
			mv $DEFAULT_INITRD_BAK $DEFAULT_INITRD
23ef29
			if [[ $? -eq 0 ]]; then
23ef29
				echo -n "Restoring original initrd as fadump mode "
23ef29
				echo "is disabled."
23ef29
				sync
23ef29
			fi
23ef29
		fi
23ef29
	fi
23ef29
}
23ef29
23ef29
check_config()
23ef29
{
23ef29
	local nr
23ef29
23ef29
	nr=$(awk 'BEGIN{cnt=0} /^raw|^ssh[[:blank:]]|^nfs|^ext[234]|^xfs|^btrfs|^minix|^dracut_args .*\-\-mount/{cnt++} END{print cnt}' $KDUMP_CONFIG_FILE)
23ef29
	[ $nr -gt 1 ] && {
23ef29
		echo "More than one dump targets specified."
23ef29
		return 1
23ef29
	}
23ef29
23ef29
	nr=$(grep "^dracut_args .*\-\-mount" $KDUMP_CONFIG_FILE | grep -o "\-\-mount" | wc -l)
23ef29
	[ $nr -gt 1 ] && {
23ef29
		echo "Multiple mount targets specified in one \"dracut_args\"."
23ef29
		return 1
23ef29
	}
23ef29
1d9674
	# Check if we have any leading spaces (or tabs) before the
1d9674
	# variable name in the kdump conf file
1d9674
	if grep -E -q '^[[:blank:]]+[a-z]' $KDUMP_CONFIG_FILE; then
1d9674
		echo "No whitespaces are allowed before a kdump option name in $KDUMP_CONFIG_FILE"
1d9674
		return 1
1d9674
	fi
1d9674
23ef29
	while read config_opt config_val; do
23ef29
		case "$config_opt" in
23ef29
		\#* | "")
23ef29
			;;
23ef29
		raw|ext2|ext3|ext4|minix|btrfs|xfs|nfs|ssh|sshkey|path|core_collector|kdump_post|kdump_pre|extra_bins|extra_modules|default|force_rebuild|force_no_rebuild|dracut_args|fence_kdump_args|fence_kdump_nodes)
23ef29
			# remove inline comments after the end of a directive.
23ef29
			config_val=$(strip_comments $config_val)
23ef29
			[ -z "$config_val" ] && {
23ef29
				echo "Invalid kdump config value for option $config_opt."
23ef29
				return 1;
23ef29
			}
23ef29
			;;
23ef29
		net|options|link_delay|disk_timeout|debug_mem_level|blacklist)
23ef29
			echo "Deprecated kdump config option: $config_opt. Refer to kdump.conf manpage for alternatives."
23ef29
			return 1
23ef29
			;;
23ef29
		*)
23ef29
			echo "Invalid kdump config option $config_opt"
23ef29
			return 1;
23ef29
			;;
23ef29
		esac
23ef29
	done < $KDUMP_CONFIG_FILE
23ef29
23ef29
	check_default_config || return 1
23ef29
23ef29
	check_fence_kdump_config || return 1
23ef29
23ef29
	return 0
23ef29
}
23ef29
23ef29
# get_pcs_cluster_modified_files <image timestamp>
23ef29
# return list of modified file for fence_kdump modified in Pacemaker cluster
23ef29
get_pcs_cluster_modified_files()
23ef29
{
23ef29
	local time_stamp
23ef29
	local modified_files
23ef29
23ef29
	is_generic_fence_kdump && return 1
23ef29
	is_pcs_fence_kdump || return 1
23ef29
23ef29
	time_stamp=`pcs cluster cib | xmllint --xpath 'string(/cib/@cib-last-written)' - | \
23ef29
		xargs -0 date +%s --date`
23ef29
23ef29
	if [ -n $time_stamp -a $time_stamp -gt $image_time ]; then
23ef29
		modified_files="cluster-cib"
23ef29
	fi
23ef29
23ef29
	if [ -f $FENCE_KDUMP_CONFIG_FILE ]; then
23ef29
		time_stamp=`stat -c "%Y" $FENCE_KDUMP_CONFIG_FILE`
23ef29
		if [ "$time_stamp" -gt "$image_time" ]; then
23ef29
			modified_files="$modified_files $FENCE_KDUMP_CONFIG_FILE"
23ef29
		fi
23ef29
	fi
23ef29
23ef29
	echo $modified_files
23ef29
}
23ef29
23ef29
check_boot_dir()
23ef29
{
23ef29
	#If user specify a boot dir for kdump kernel, let's use it. Otherwise
23ef29
	#check whether it's a atomic host. If yes parse the subdirectory under
23ef29
	#/boot; If not just find it under /boot.
23ef29
	[ -n "$KDUMP_BOOTDIR" ] && return
23ef29
23ef29
	if ! is_atomic || [ "$(uname -m)" = "s390x" ]; then
23ef29
		KDUMP_BOOTDIR="/boot"
23ef29
	else
23ef29
		eval $(cat /proc/cmdline| grep "BOOT_IMAGE" | cut -d' ' -f1)
23ef29
		KDUMP_BOOTDIR="/boot"$(dirname $BOOT_IMAGE)
23ef29
	fi
23ef29
}
23ef29
23ef29
setup_initrd()
23ef29
{
23ef29
	DEFAULT_INITRD="${KDUMP_BOOTDIR}/initramfs-`uname -r`.img"
23ef29
	DEFAULT_INITRD_BAK="${KDUMP_BOOTDIR}/.initramfs-`uname -r`.img.default"
23ef29
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
23ef29
		TARGET_INITRD="$DEFAULT_INITRD"
23ef29
		if [ ! -s "$TARGET_INITRD" ]; then
23ef29
			echo "Error: No initrd found to rebuild!"
23ef29
			return 1
23ef29
		fi
23ef29
	else
23ef29
		TARGET_INITRD="${KDUMP_BOOTDIR}/initramfs-${kdump_kver}kdump.img"
23ef29
	fi
23ef29
}
23ef29
23ef29
check_files_modified()
23ef29
{
23ef29
	local modified_files=""
23ef29
23ef29
	#also rebuild when Pacemaker cluster conf is changed and fence kdump is enabled.
23ef29
	modified_files=$(get_pcs_cluster_modified_files)
23ef29
23ef29
	EXTRA_BINS=`grep ^kdump_post $KDUMP_CONFIG_FILE | cut -d\  -f2`
23ef29
	CHECK_FILES=`grep ^kdump_pre $KDUMP_CONFIG_FILE | cut -d\  -f2`
23ef29
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
23ef29
	CHECK_FILES=`grep ^extra_bins $KDUMP_CONFIG_FILE | cut -d\  -f2-`
23ef29
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
23ef29
	files="$KDUMP_CONFIG_FILE $kdump_kernel $EXTRA_BINS"
23ef29
	[[ -e /etc/fstab ]] && files="$files /etc/fstab"
23ef29
23ef29
	check_exist "$files" && check_executable "$EXTRA_BINS"
23ef29
	[ $? -ne 0 ] && return 2
23ef29
23ef29
	for file in $files; do
23ef29
		time_stamp=`stat -c "%Y" $file`
23ef29
		if [ "$time_stamp" -gt "$image_time" ]; then
23ef29
			modified_files="$modified_files $file"
23ef29
		fi
23ef29
	done
23ef29
	if [ -n "$modified_files" ]; then
23ef29
		echo "Detected change(s) in the following file(s):"
23ef29
		echo -n "  "; echo "$modified_files" | sed 's/\s/\n  /g'
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	return 0
23ef29
}
23ef29
23ef29
check_dump_fs_modified()
23ef29
{
23ef29
	local _old_dev _old_mntpoint _old_fstype
23ef29
	local _new_dev _new_mntpoint _new_fstype
23ef29
	local _target _path _dracut_args
23ef29
23ef29
	# No need to check in case of mount target specified via "dracut_args".
23ef29
	if is_mount_in_dracut_args; then
23ef29
		return 0
23ef29
	fi
23ef29
23ef29
	# No need to check in case of raw target.
23ef29
	# Currently we do not check also if ssh/nfs target is specified
23ef29
	if is_ssh_dump_target || is_nfs_dump_target || is_raw_dump_target; then
23ef29
		return 0
23ef29
	fi
23ef29
23ef29
	_target=$(get_user_configured_dump_disk)
23ef29
23ef29
	if [[ -n "$_target" ]]; then
23ef29
		_target=$(to_dev_name $_target)
23ef29
		_new_fstype=$(blkid $_target | awk -F"TYPE=" '{print $2}' | cut -d '"' -f 2)
23ef29
	else
23ef29
		_path=$(get_save_path)
23ef29
		set -- $(df -T $_path 2>/dev/null | tail -1 | awk '{ print $1, $2}')
23ef29
		_target=$(to_dev_name $1)
23ef29
		_new_fstype=$2
23ef29
		if [[ -z "$_target" || -z "$_new_fstype" ]];then
23ef29
			echo "Dump path $_path does not exist"
23ef29
			return 2
23ef29
		fi
23ef29
	fi
23ef29
23ef29
	if [[ $(expr substr $_new_fstype 1 3) = "nfs" ]];then
23ef29
		_new_dev=$_target
23ef29
	else
23ef29
		_new_dev=$(kdump_get_persistent_dev $_target $_new_fstype)
23ef29
	fi
23ef29
23ef29
	if ! findmnt $_target >/dev/null; then
1d9674
		echo "Dump target $_target is probably not mounted."
1d9674
		return 2
1d9674
	fi
1d9674
1d9674
	if [[ "$_target" = "$(get_root_fs_device)" ]]; then
1d9674
		_new_mntpoint="/sysroot"
1d9674
	else
1d9674
		_new_mntpoint="/kdumproot/$(get_mntpoint_from_target $_target)"
1d9674
	fi
23ef29
23ef29
	_dracut_args=$(lsinitrd $TARGET_INITRD -f usr/lib/dracut/build-parameter.txt)
23ef29
	if [[ -z "$_dracut_args" ]];then
23ef29
		echo "Warning: No dracut arguments found in initrd"
23ef29
		return 0
23ef29
	fi
23ef29
23ef29
	# if --mount argument present then match old and new target, mount
23ef29
	# point and file system. If any of them mismatches then rebuild
23ef29
	echo $_dracut_args | grep "\-\-mount" &> /dev/null
23ef29
	if [[ $? -eq 0 ]];then
23ef29
		set -- $(echo $_dracut_args | awk -F "--mount '" '{print $2}' | cut -d' ' -f1,2,3)
23ef29
		_old_dev=$1
23ef29
		_old_mntpoint=$2
23ef29
		_old_fstype=$3
23ef29
		[[ $_new_dev = $_old_dev && $_new_mntpoint = $_old_mntpoint && $_new_fstype = $_old_fstype ]] && return 0
23ef29
	# otherwise rebuild if target device is not a root device
23ef29
	else
23ef29
		[[ "$_target" = "$(get_root_fs_device)" ]] && return 0
23ef29
	fi
23ef29
23ef29
	echo "Detected change in File System"
23ef29
	return 1
23ef29
}
23ef29
23ef29
check_wdt_modified()
23ef29
{
23ef29
	local -A _drivers
23ef29
	local _alldrivers _active _wdtdrv _wdtppath _dir
23ef29
	local wd_old wd_new
23ef29
23ef29
	is_wdt_mod_omitted
23ef29
	[[ $? -eq 0 ]] && return 0
23ef29
	[[ -d /sys/class/watchdog/ ]] || return 0
23ef29
23ef29
	# Copied logic from dracut 04watchdog/module-setup.sh::installkernel()
23ef29
	for _dir in /sys/class/watchdog/*; do
23ef29
		[[ -d "$_dir" ]] || continue
23ef29
		[[ -f "$_dir/state" ]] || continue
23ef29
		_active=$(< "$_dir/state")
23ef29
		[[ "$_active" =  "active" ]] || continue
23ef29
		# device/modalias will return driver of this device
23ef29
		_wdtdrv=$(< "$_dir/device/modalias")
23ef29
		# There can be more than one module represented by same
23ef29
		# modalias. Currently load all of them.
23ef29
		# TODO: Need to find a way to avoid any unwanted module
23ef29
		# represented by modalias
23ef29
		_wdtdrv=$(modprobe --set-version "$kdump_kver" -R $_wdtdrv 2>/dev/null)
23ef29
		if [[ $_wdtdrv ]]; then
23ef29
			for i in $_wdtdrv; do
23ef29
				_drivers[$i]=1
23ef29
			done
23ef29
		fi
23ef29
		# however in some cases, we also need to check that if there is
23ef29
		# a specific driver for the parent bus/device.  In such cases
23ef29
		# we also need to enable driver for parent bus/device.
23ef29
		_wdtppath=$(readlink -f "$_dir/device")
23ef29
		while [[ -d "$_wdtppath" ]] && [[ "$_wdtppath" != "/sys" ]]; do
23ef29
			_wdtppath=$(readlink -f "$_wdtppath/..")
23ef29
			[[ -f "$_wdtppath/modalias" ]] || continue
23ef29
23ef29
			_wdtdrv=$(< "$_wdtppath/modalias")
23ef29
			_wdtdrv=$(modprobe --set-version "$kdump_kver" -R $_wdtdrv 2>/dev/null)
23ef29
			if [[ $_wdtdrv ]]; then
23ef29
				for i in $_wdtdrv; do
23ef29
					_drivers[$i]=1
23ef29
				done
23ef29
			fi
23ef29
		done
23ef29
	done
23ef29
23ef29
	# ensure that watchdog module is loaded as early as possible
23ef29
	_alldrivers="${!_drivers[*]}"
23ef29
	[[ $_alldrivers ]] && wd_new="rd.driver.pre=${_alldrivers// /,}"
23ef29
	wd_old=$(lsinitrd $TARGET_INITRD -f etc/cmdline.d/00-watchdog.conf)
23ef29
23ef29
	[[ "$wd_old" = "$wd_new" ]] && return 0
23ef29
23ef29
	return 1
23ef29
}
23ef29
23ef29
# returns 0 if system is not modified
23ef29
# returns 1 if system is modified
23ef29
# returns 2 if system modification is invalid
23ef29
check_system_modified()
23ef29
{
23ef29
	local ret
23ef29
23ef29
	[[ -f $TARGET_INITRD ]] || return 1
23ef29
23ef29
	check_files_modified
23ef29
	ret=$?
23ef29
	if [ $ret -ne 0 ]; then
23ef29
		return $ret
23ef29
	fi
23ef29
23ef29
	check_dump_fs_modified
23ef29
	ret=$?
23ef29
	if [ $ret -ne 0 ]; then
23ef29
		return $ret
23ef29
	fi
23ef29
23ef29
	check_wdt_modified
23ef29
	if [ $? -ne 0 ]; then
23ef29
		echo "Detected change in watchdog state"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	return 0
23ef29
}
23ef29
23ef29
check_rebuild()
23ef29
{
23ef29
	local extra_modules
23ef29
	local capture_capable_initrd="1"
23ef29
	local _force_rebuild force_rebuild="0"
23ef29
	local _force_no_rebuild force_no_rebuild="0"
23ef29
	local ret system_modified="0"
23ef29
23ef29
	check_boot_dir
23ef29
23ef29
	if [ -z "$KDUMP_KERNELVER" ]; then
23ef29
		kdump_kver=`uname -r`
23ef29
	else
23ef29
		kdump_kver=$KDUMP_KERNELVER
23ef29
	fi
23ef29
23ef29
	kdump_kernel="${KDUMP_BOOTDIR}/${KDUMP_IMG}-${kdump_kver}${KDUMP_IMG_EXT}"
23ef29
	setup_initrd
23ef29
	if [ $? -ne 0 ]; then
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	_force_no_rebuild=`grep ^force_no_rebuild $KDUMP_CONFIG_FILE 2>/dev/null`
23ef29
	if [ $? -eq 0 ]; then
23ef29
		force_no_rebuild=`echo $_force_no_rebuild | cut -d' '  -f2`
23ef29
		if [ "$force_no_rebuild" != "0" ] && [ "$force_no_rebuild" != "1" ];then
23ef29
			echo "Error: force_no_rebuild value is invalid"
23ef29
			return 1
23ef29
		fi
23ef29
	fi
23ef29
23ef29
	_force_rebuild=`grep ^force_rebuild $KDUMP_CONFIG_FILE 2>/dev/null`
23ef29
	if [ $? -eq 0 ]; then
23ef29
		force_rebuild=`echo $_force_rebuild | cut -d' '  -f2`
23ef29
		if [ "$force_rebuild" != "0" ] && [ "$force_rebuild" != "1" ];then
23ef29
			echo "Error: force_rebuild value is invalid"
23ef29
			return 1
23ef29
		fi
23ef29
	fi
23ef29
23ef29
	if [[ "$force_no_rebuild" == "1" && "$force_rebuild" == "1" ]]; then
23ef29
		echo "Error: force_rebuild and force_no_rebuild are enabled simultaneously in kdump.conf"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	# Will not rebuild kdump initrd
23ef29
	if [ "$force_no_rebuild" == "1" ]; then
23ef29
		return 0
23ef29
	fi
23ef29
23ef29
	#will rebuild every time if extra_modules are specified
23ef29
	extra_modules=`grep ^extra_modules $KDUMP_CONFIG_FILE`
23ef29
	[ -n "$extra_modules" ] && force_rebuild="1"
23ef29
23ef29
	#check to see if dependent files has been modified
23ef29
	#since last build of the image file
23ef29
	if [ -f $TARGET_INITRD ]; then
23ef29
		image_time=`stat -c "%Y" $TARGET_INITRD 2>/dev/null`
23ef29
23ef29
		#in case of fadump mode, check whether the default/target
23ef29
		#initrd is already built with dump capture capability
23ef29
		if [ "$DEFAULT_DUMP_MODE" == "fadump" ]; then
23ef29
			capture_capable_initrd=$(lsinitrd -f $DRACUT_MODULES_FILE $TARGET_INITRD | grep ^kdumpbase$ | wc -l)
23ef29
		fi
23ef29
	fi
23ef29
23ef29
	check_system_modified
23ef29
	ret=$?
23ef29
	if [ $ret -eq 2 ]; then
23ef29
		return 1
23ef29
	elif [ $ret -eq 1 ];then
23ef29
		system_modified="1"
23ef29
	fi
23ef29
23ef29
	handle_mode_switch
23ef29
23ef29
	if [ $image_time -eq 0 ]; then
23ef29
		echo  -n "No kdump initial ramdisk found."; echo
23ef29
	elif [ "$capture_capable_initrd" == "0" ]; then
23ef29
		echo -n "Rebuild $TARGET_INITRD with dump capture support"; echo
23ef29
	elif [ "$force_rebuild" != "0" ]; then
23ef29
		echo -n "Force rebuild $TARGET_INITRD"; echo
23ef29
	elif [ "$system_modified" != "0" ]; then
23ef29
		:
23ef29
	else
23ef29
		return 0
23ef29
	fi
23ef29
23ef29
	if [[ ! -w "$KDUMP_BOOTDIR" ]];then
23ef29
		echo "$KDUMP_BOOTDIR does not have write permission. Can not rebuild $TARGET_INITRD"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	echo "Rebuilding $TARGET_INITRD"
23ef29
	rebuild_initrd
23ef29
	return $?
23ef29
}
23ef29
23ef29
# This function check iomem and determines if we have more than
23ef29
# 4GB of ram available. Returns 1 if we do, 0 if we dont
23ef29
need_64bit_headers()
23ef29
{
23ef29
	return `tail -n 1 /proc/iomem | awk '{ split ($1, r, "-"); \
23ef29
	print (strtonum("0x" r[2]) > strtonum("0xffffffff")); }'`
23ef29
}
23ef29
23ef29
# Load the kdump kernel specified in /etc/sysconfig/kdump
23ef29
# If none is specified, try to load a kdump kernel with the same version
23ef29
# as the currently running kernel.
23ef29
load_kdump()
23ef29
{
23ef29
	ARCH=`uname -m`
23ef29
	if [ "$ARCH" == "i686" -o "$ARCH" == "i386" ]
23ef29
	then
23ef29
23ef29
		need_64bit_headers
23ef29
		if [ $? == 1 ]
23ef29
		then
23ef29
			FOUND_ELF_ARGS=`echo $KEXEC_ARGS | grep elf32-core-headers`
23ef29
			if [ -n "$FOUND_ELF_ARGS" ]
23ef29
			then
23ef29
				echo -n "Warning: elf32-core-headers overrides correct elf64 setting"
23ef29
				echo
23ef29
			else	
23ef29
				KEXEC_ARGS="$KEXEC_ARGS --elf64-core-headers"
23ef29
			fi
23ef29
		else
23ef29
			FOUND_ELF_ARGS=`echo $KEXEC_ARGS | grep elf64-core-headers`
23ef29
			if [ -z "$FOUND_ELF_ARGS" ]
23ef29
			then
23ef29
				KEXEC_ARGS="$KEXEC_ARGS --elf32-core-headers"
23ef29
			fi
23ef29
		fi
23ef29
	fi
23ef29
1d9674
	prepare_cmdline
23ef29
23ef29
	# For secureboot enabled machines, use new kexec file based syscall.
23ef29
	# Old syscall will always fail as it does not have capability to
23ef29
	# to kernel signature verification.
23ef29
	if is_secure_boot_enforced; then
23ef29
		echo "Secure Boot is enabled. Using kexec file based syscall."
23ef29
		KEXEC_ARGS="$KEXEC_ARGS -s"
23ef29
	elif is_secure_mode_enforced; then
23ef29
		echo "securelevel is set to 1 (Secure Mode). Using kexec file based syscall."
23ef29
		KEXEC_ARGS="$KEXEC_ARGS -s"
23ef29
	fi
23ef29
23ef29
	$KEXEC $KEXEC_ARGS $standard_kexec_args \
23ef29
		--command-line="$KDUMP_COMMANDLINE" \
23ef29
		--initrd=$TARGET_INITRD $kdump_kernel
23ef29
	if [ $? == 0 ]; then
23ef29
		echo "kexec: loaded kdump kernel"
23ef29
		return 0
23ef29
	else
23ef29
		echo "kexec: failed to load kdump kernel" >&2
23ef29
		return 1
23ef29
	fi
23ef29
}
23ef29
23ef29
check_ssh_config()
23ef29
{
23ef29
	while read config_opt config_val; do
23ef29
		case "$config_opt" in
23ef29
		sshkey)
23ef29
			# remove inline comments after the end of a directive.
23ef29
			config_val=$(strip_comments $config_val)
23ef29
			if [ -f "$config_val" ]; then
23ef29
				# canonicalize the path
23ef29
				SSH_KEY_LOCATION=$(/usr/bin/readlink -m $config_val)
23ef29
			else
23ef29
				echo "WARNING: '$config_val' doesn't exist, using default value '$SSH_KEY_LOCATION'"
23ef29
			fi
23ef29
			;;
23ef29
		path)
23ef29
			config_val=$(strip_comments $config_val)
23ef29
			SAVE_PATH=$config_val
23ef29
			;;
23ef29
		ssh)
23ef29
			config_val=$(strip_comments $config_val)
23ef29
			DUMP_TARGET=$config_val
23ef29
			;;
23ef29
		*)
23ef29
			;;
23ef29
		esac
23ef29
	done < $KDUMP_CONFIG_FILE
23ef29
23ef29
	#make sure they've configured kdump.conf for ssh dumps
23ef29
	local SSH_TARGET=`echo -n $DUMP_TARGET | sed -n '/.*@/p'`
23ef29
	if [ -z "$SSH_TARGET" ]; then
23ef29
		return 1
23ef29
	fi
23ef29
	return 0
23ef29
}
23ef29
23ef29
check_ssh_target()
23ef29
{
23ef29
	local _ret
23ef29
	ssh -q -i $SSH_KEY_LOCATION -o BatchMode=yes $DUMP_TARGET mkdir -p $SAVE_PATH
23ef29
	_ret=$?
23ef29
	if [ $_ret -ne 0 ]; then
23ef29
		echo "Could not create $DUMP_TARGET:$SAVE_PATH, you probably need to run \"kdumpctl propagate\""  >&2
23ef29
		return 1
23ef29
	fi
23ef29
	return 0
23ef29
}
23ef29
23ef29
propagate_ssh_key()
23ef29
{
23ef29
	check_ssh_config
23ef29
	if [ $? -ne 0 ]; then
23ef29
		echo "No ssh config specified in $KDUMP_CONFIG_FILE.  Can't propagate" >&2
23ef29
		exit 1
23ef29
	fi
23ef29
23ef29
	local KEYFILE=$SSH_KEY_LOCATION
23ef29
	local errmsg="Failed to propagate ssh key"
23ef29
23ef29
	#Check to see if we already created key, if not, create it.
23ef29
	if [ -f $KEYFILE ]; then
23ef29
		echo "Using existing keys..."
23ef29
	else
23ef29
		echo -n "Generating new ssh keys... "
23ef29
		/usr/bin/ssh-keygen -t rsa -f $KEYFILE -N "" 2>&1 > /dev/null
23ef29
		echo "done."
23ef29
	fi
23ef29
23ef29
	#now find the target ssh user and server to contact.
23ef29
	SSH_USER=`echo $DUMP_TARGET | cut -d\  -f2 | cut -d@ -f1`
23ef29
	SSH_SERVER=`echo $DUMP_TARGET | sed -e's/\(.*@\)\(.*$\)/\2/'`
23ef29
23ef29
	#now send the found key to the found server
23ef29
	ssh-copy-id -i $KEYFILE $SSH_USER@$SSH_SERVER
23ef29
	RET=$?
23ef29
	if [ $RET == 0 ]; then
23ef29
		echo $KEYFILE has been added to ~$SSH_USER/.ssh/authorized_keys on $SSH_SERVER
23ef29
		return 0
23ef29
	else
23ef29
		echo $errmsg, $KEYFILE failed in transfer to $SSH_SERVER  >&2
23ef29
		exit 1
23ef29
	fi
23ef29
}
23ef29
23ef29
show_reserved_mem()
23ef29
{
23ef29
    local mem=$(cat /sys/kernel/kexec_crash_size)
23ef29
    local mem_mb=$(expr $mem / 1024 / 1024)
23ef29
23ef29
    echo "Reserved "$mem_mb"MB memory for crash kernel"
23ef29
}
23ef29
23ef29
handle_mode_switch()
23ef29
{
23ef29
	if [ "$DEFAULT_DUMP_MODE" == "fadump" ]; then
23ef29
		# backup initrd for reference before replacing it
23ef29
		# with fadump aware initrd
23ef29
		backup_default_initrd
23ef29
	else
23ef29
		# check if a backup of default initrd exists. If yes,
23ef29
		# it signifies a switch from fadump mode. So, restore
23ef29
		# the backed up default initrd.
23ef29
		restore_default_initrd
23ef29
	fi
23ef29
}
23ef29
23ef29
check_current_fadump_status()
23ef29
{
23ef29
	# Check if firmware-assisted dump has been registered.
23ef29
	rc=`cat $FADUMP_REGISTER_SYS_NODE`
23ef29
	[ $rc -eq 1 ] && return 0
23ef29
	return 1
23ef29
}
23ef29
23ef29
check_current_kdump_status()
23ef29
{
23ef29
	rc=`cat /sys/kernel/kexec_crash_loaded`
23ef29
	if [ $rc == 1 ]; then
23ef29
		return 0
23ef29
	else
23ef29
		return 1
23ef29
	fi
23ef29
}
23ef29
23ef29
check_current_status()
23ef29
{
23ef29
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
23ef29
		check_current_fadump_status
23ef29
	else
23ef29
		check_current_kdump_status
23ef29
	fi
23ef29
23ef29
	return $?
23ef29
}
23ef29
23ef29
save_raw()
23ef29
{
23ef29
	local kdump_dir
23ef29
	local raw_target
23ef29
23ef29
	raw_target=$(awk '$1 ~ /^raw$/ { print $2; }' $KDUMP_CONFIG_FILE)
23ef29
	[ -z "$raw_target" ] && return 0
23ef29
	[ -b "$raw_target" ] || {
23ef29
		echo "raw partition $raw_target not found"
23ef29
		return 1
23ef29
	}
23ef29
	kdump_dir=`grep ^path $KDUMP_CONFIG_FILE | cut -d' '  -f2-`
23ef29
	if [ -z "${kdump_dir}" ]; then
23ef29
		coredir="/var/crash/`date +"%Y-%m-%d-%H:%M"`"
23ef29
	else
23ef29
		coredir="${kdump_dir}/`date +"%Y-%m-%d-%H:%M"`"
23ef29
	fi
23ef29
23ef29
	mkdir -p "$coredir"
23ef29
	[ -d "$coredir" ] || {
23ef29
		echo "failed to create $coredir"
23ef29
		return 1
23ef29
	}
23ef29
	if makedumpfile -R $coredir/vmcore <$raw_target >/dev/null 2>&1; then
23ef29
		# dump found
23ef29
		echo "Dump saved to $coredir/vmcore"
23ef29
		# wipe makedumpfile header
23ef29
		dd if=/dev/zero of=$raw_target bs=1b count=1 2>/dev/null
23ef29
	else
23ef29
		rm -rf "$coredir"
23ef29
	fi
23ef29
23ef29
	return 0
23ef29
}
23ef29
23ef29
is_dump_target_configured()
23ef29
{
23ef29
	local _target
23ef29
23ef29
	_target=$(egrep "^ext[234]|^xfs|^btrfs|^minix|^raw|^ssh|^nfs" /etc/kdump.conf)
23ef29
23ef29
	[ -n "$_target" ]
23ef29
}
23ef29
23ef29
local_fs_dump_target()
23ef29
{
23ef29
	local _target
23ef29
23ef29
	_target=$(egrep "^ext[234]|^xfs|^btrfs|^minix" /etc/kdump.conf)
23ef29
	if [ $? -eq 0 ]; then
23ef29
		echo $_target|awk '{print $2}'
23ef29
	fi
23ef29
}
23ef29
23ef29
path_to_be_relabeled()
23ef29
{
23ef29
	local _path _target _mnt="/" _rmnt
23ef29
23ef29
	if is_dump_target_configured; then
23ef29
		_target=$(local_fs_dump_target)
23ef29
		if [[ -n "$_target" ]]; then
23ef29
			_mnt=$(findmnt -k -f -n -r -o TARGET $_target)
23ef29
			if [ -z "$_mnt" ]; then
23ef29
				return
23ef29
			fi
23ef29
		else
23ef29
			return
23ef29
		fi
23ef29
	fi
23ef29
1d9674
	if is_mount_in_dracut_args; then
1d9674
		return;
1d9674
	fi
23ef29
	_path=$(get_save_path)
23ef29
	# if $_path is masked by other mount, we will not relabel it.
23ef29
	_rmnt=$(df $_mnt/$_path 2>/dev/null | tail -1 | awk '{ print $NF }')
23ef29
	if [ "$_rmnt" == "$_mnt" ]; then
23ef29
		echo $_mnt/$_path
23ef29
	fi
23ef29
}
23ef29
23ef29
selinux_relabel()
23ef29
{
23ef29
	local _path _i _attr
23ef29
23ef29
	_path=$(path_to_be_relabeled)
23ef29
	if [ -z "$_path" ] || ! [ -d "$_path" ] ; then
23ef29
		return
23ef29
	fi
23ef29
23ef29
	for _i in $(find $_path); do
23ef29
		_attr=$(getfattr -m "security.selinux" $_i 2>/dev/null)
23ef29
		if [ -z "$_attr" ]; then
23ef29
			restorecon $_i;
23ef29
		fi
23ef29
	done
23ef29
}
23ef29
23ef29
# Check if secure boot is being enforced.
23ef29
#
23ef29
# Per Peter Jones, we need check efivar SecureBoot-$(the UUID) and
23ef29
# SetupMode-$(the UUID), they are both 5 bytes binary data. The first four
23ef29
# bytes are the attributes associated with the variable and can safely be
23ef29
# ignored, the last bytes are one-byte true-or-false variables. If SecureBoot
23ef29
# is 1 and SetupMode is 0, then secure boot is being enforced.
23ef29
#
23ef29
# SecureBoot-UUID won't always be set when securelevel is 1. For legacy-mode
23ef29
# and uefi-without-seucre-enabled system, we can manually enable secure mode
23ef29
# by writing "1" to securelevel. So check both efi var and secure mode is a 
23ef29
# more sane way.
23ef29
#
23ef29
# Assume efivars is mounted at /sys/firmware/efi/efivars.
23ef29
is_secure_boot_enforced()
23ef29
{
23ef29
	local secure_boot_file setup_mode_file
23ef29
	local secure_boot_byte setup_mode_byte
23ef29
23ef29
	secure_boot_file=$(find /sys/firmware/efi/efivars -name SecureBoot-* 2>/dev/null)
23ef29
	setup_mode_file=$(find /sys/firmware/efi/efivars -name SetupMode-* 2>/dev/null)
23ef29
23ef29
	if [ -f "$secure_boot_file" ] && [ -f "$setup_mode_file" ]; then
23ef29
		secure_boot_byte=$(hexdump -v -e '/1 "%d\ "' $secure_boot_file|cut -d' ' -f 5)
23ef29
		setup_mode_byte=$(hexdump -v -e '/1 "%d\ "' $setup_mode_file|cut -d' ' -f 5)
23ef29
23ef29
		if [ "$secure_boot_byte" = "1" ] && [ "$setup_mode_byte" = "0" ]; then
23ef29
			return 0
23ef29
		fi
23ef29
	fi
23ef29
23ef29
	return 1
23ef29
}
23ef29
23ef29
# Check if secure mode is being enforced (securelevel =? 1)
23ef29
is_secure_mode_enforced()
23ef29
{
23ef29
	local secure_mode_byte
23ef29
23ef29
	if [ ! -f /sys/kernel/security/securelevel ]; then
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	secure_mode_byte=$(cat /sys/kernel/security/securelevel)
23ef29
	if [ "$secure_mode_byte" = "1" ]; then
23ef29
		return 0
23ef29
	fi
23ef29
23ef29
	return 1
23ef29
}
23ef29
23ef29
check_crash_mem_reserved()
23ef29
{
23ef29
	local mem_reserved
23ef29
23ef29
	mem_reserved=$(cat /sys/kernel/kexec_crash_size)
23ef29
	if [ $mem_reserved -eq 0 ]; then
23ef29
		echo "No memory reserved for crash kernel"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	return 0
23ef29
}
23ef29
23ef29
check_kdump_feasibility()
23ef29
{
23ef29
	if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
23ef29
		echo "Kdump is not supported on this kernel"
23ef29
		return 1
23ef29
	fi
23ef29
	check_crash_mem_reserved
23ef29
	return $?
23ef29
}
23ef29
23ef29
check_fence_kdump_config()
23ef29
{
23ef29
	local hostname=`hostname`
23ef29
	local ipaddrs=`hostname -I`
23ef29
	local nodes=$(get_option_value "fence_kdump_nodes")
23ef29
23ef29
	for node in $nodes; do
23ef29
		if [ "$node" = "$hostname" ]; then
23ef29
			echo "Option fence_kdump_nodes cannot contain $hostname"
23ef29
			return 1
23ef29
		fi
23ef29
		# node can be ipaddr
23ef29
		echo $ipaddrs | grep $node > /dev/null
23ef29
		if [ $? -eq 0 ]; then
23ef29
			echo "Option fence_kdump_nodes cannot contain $node"
23ef29
			return 1
23ef29
		fi
23ef29
	done
23ef29
23ef29
	return 0
23ef29
}
23ef29
23ef29
check_dump_feasibility()
23ef29
{
23ef29
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
23ef29
		return 0
23ef29
	fi
23ef29
23ef29
	check_kdump_feasibility
23ef29
	return $?
23ef29
}
23ef29
23ef29
start_fadump()
23ef29
{
23ef29
	echo 1 > $FADUMP_REGISTER_SYS_NODE
23ef29
	if ! check_current_fadump_status; then
23ef29
		echo "fadump: failed to register"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	echo "fadump: registered successfully"
23ef29
	return 0
23ef29
}
23ef29
23ef29
start_dump()
23ef29
{
23ef29
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
23ef29
		start_fadump
23ef29
	else
23ef29
		load_kdump
23ef29
	fi
23ef29
23ef29
	return $?
23ef29
}
23ef29
23ef29
check_default_config()
23ef29
{
23ef29
	local default_option
23ef29
23ef29
	default_option=$(awk '$1 ~ /^default$/ {print $2;}' $KDUMP_CONFIG_FILE)
23ef29
	if [ -z "$default_option" ]; then
23ef29
		return 0
23ef29
	else
23ef29
		case "$default_option" in
23ef29
		  reboot|halt|poweroff|shell|dump_to_rootfs)
23ef29
			return 0
23ef29
		  ;;
23ef29
		  *)
23ef29
			echo $"Usage kdump.conf: default {reboot|halt|poweroff|shell|dump_to_rootfs}"
23ef29
			return 1
23ef29
		esac
23ef29
	fi
23ef29
}
23ef29
23ef29
start()
23ef29
{
23ef29
	check_dump_feasibility
23ef29
	if [ $? -ne 0 ]; then
23ef29
		echo "Starting kdump: [FAILED]"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	check_config
23ef29
	if [ $? -ne 0 ]; then
23ef29
		echo "Starting kdump: [FAILED]"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	if sestatus 2>/dev/null | grep -q "SELinux status.*enabled"; then
23ef29
		selinux_relabel
23ef29
	fi
23ef29
23ef29
	save_raw
23ef29
	if [ $? -ne 0 ]; then
23ef29
		echo "Starting kdump: [FAILED]"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	check_current_status
23ef29
	if [ $? == 0 ]; then
23ef29
		echo "Kdump already running: [WARNING]"
23ef29
		return 0
23ef29
	fi
23ef29
23ef29
	if check_ssh_config; then
23ef29
		if ! check_ssh_target; then
23ef29
			echo "Starting kdump: [FAILED]"
23ef29
			return 1
23ef29
		fi
23ef29
	fi
23ef29
23ef29
	check_rebuild
23ef29
	if [ $? != 0 ]; then
23ef29
		echo "Starting kdump: [FAILED]"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	start_dump
23ef29
	if [ $? != 0 ]; then
23ef29
		echo "Starting kdump: [FAILED]"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	echo "Starting kdump: [OK]"
23ef29
}
23ef29
23ef29
stop_fadump()
23ef29
{
23ef29
	echo 0 > $FADUMP_REGISTER_SYS_NODE
23ef29
	if check_current_fadump_status; then
23ef29
		echo "fadump: failed to unregister"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	echo "fadump: unregistered successfully"
23ef29
	return 0
23ef29
}
23ef29
23ef29
stop_kdump()
23ef29
{
23ef29
	if is_secure_boot_enforced; then
23ef29
		$KEXEC -s -p -u
23ef29
	else
23ef29
		$KEXEC -p -u
23ef29
	fi
23ef29
23ef29
	if [ $? != 0 ]; then
23ef29
		echo "kexec: failed to unload kdump kernel"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	echo "kexec: unloaded kdump kernel"
23ef29
	return 0
23ef29
}
23ef29
23ef29
stop()
23ef29
{
23ef29
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
23ef29
		stop_fadump
23ef29
	else
23ef29
		stop_kdump
23ef29
	fi
23ef29
23ef29
	if [ $? != 0 ]; then
23ef29
		echo "Stopping kdump: [FAILED]"
23ef29
		return 1
23ef29
	fi
23ef29
23ef29
	echo "Stopping kdump: [OK]"
23ef29
	return 0
23ef29
}
23ef29
23ef29
if [ ! -f "$KDUMP_CONFIG_FILE" ]; then
23ef29
	echo "Error: No kdump config file found!"  >&2
23ef29
	exit 1
23ef29
fi
23ef29
23ef29
main ()
23ef29
{
23ef29
	# Determine if the dump mode is kdump or fadump
23ef29
	determine_dump_mode
23ef29
23ef29
	case "$1" in
23ef29
	  start)
23ef29
		if [ -s /proc/vmcore ]; then
23ef29
			save_core
23ef29
			reboot
23ef29
		else
23ef29
			start
23ef29
		fi
23ef29
		;;
23ef29
	  stop)
23ef29
		stop
23ef29
		;;
23ef29
	  status)
23ef29
		EXIT_CODE=0
23ef29
		check_current_status
23ef29
		case "$?" in
23ef29
		  0)
23ef29
			echo "Kdump is operational"
23ef29
			EXIT_CODE=0
23ef29
			;;
23ef29
		  1)
23ef29
			echo "Kdump is not operational"
23ef29
			EXIT_CODE=3
23ef29
			;;
23ef29
		esac
23ef29
		exit $EXIT_CODE
23ef29
		;;
23ef29
	  restart)
23ef29
		stop
23ef29
		start
23ef29
		;;
23ef29
	  condrestart)
23ef29
		;;
23ef29
	  propagate)
23ef29
		propagate_ssh_key
23ef29
		;;
23ef29
	  showmem)
23ef29
		show_reserved_mem
23ef29
		;;
23ef29
	  *)
23ef29
		echo $"Usage: $0 {start|stop|status|restart|propagate|showmem}"
23ef29
		exit 1
23ef29
	esac
23ef29
}
23ef29
23ef29
# Other kdumpctl instances will block in queue, until this one exits
23ef29
single_instance_lock
23ef29
23ef29
# To avoid fd 9 leaking, we invoke a subshell, close fd 9 and call main.
23ef29
# So that fd isn't leaking when main is invoking a subshell.
23ef29
(exec 9<&-; main $1)
23ef29
23ef29
exit $?