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