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"
893e0b
	$MKDUMPRD $target_initrd_tmp --rebuild $TARGET_INITRD --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
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
893e0b
	return 0
893e0b
}
893e0b
893e0b
rebuild_initrd()
893e0b
{
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
893e0b
		if [ ! -f "$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
			;;
893e0b
		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)
893e0b
			# remove inline comments after the end of a directive.
893e0b
			config_val=$(strip_comments $config_val)
893e0b
			[ -z "$config_val" ] && {
893e0b
				echo "Invalid kdump config value for option $config_opt."
893e0b
				return 1;
893e0b
			}
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
893e0b
	done < $KDUMP_CONFIG_FILE
893e0b
893e0b
	check_default_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
893e0b
	else
893e0b
		TARGET_INITRD="${KDUMP_BOOTDIR}/initramfs-${kdump_kver}kdump.img"
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`
893e0b
	CORE_COLLECTOR=`grep ^core_collector $KDUMP_CONFIG_FILE | cut -d\  -f2`
893e0b
	CORE_COLLECTOR=`type -P $CORE_COLLECTOR`
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
893e0b
	check_exist "$files" && check_executable "$EXTRA_BINS"
893e0b
	[ $? -ne 0 ] && return 2
893e0b
893e0b
	for file in $files; do
893e0b
		time_stamp=`stat -c "%Y" $file`
893e0b
		if [ "$time_stamp" -gt "$image_time" ]; then
893e0b
			modified_files="$modified_files $file"
893e0b
		fi
893e0b
	done
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
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
893e0b
	if [[ $(expr substr $_new_fstype 1 3) = "nfs" ]];then
893e0b
		_new_dev=$_target
893e0b
	else
893e0b
		_new_dev=$(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
893e0b
	if ! findmnt $_target >/dev/null; then
893e0b
		echo "Dump target $_target is probably not mounted."
893e0b
		return 2
893e0b
	fi
893e0b
893e0b
	if [[ "$_target" = "$(get_root_fs_device)" ]]; then
893e0b
		_new_mntpoint="/sysroot"
893e0b
	else
893e0b
		_new_mntpoint="/kdumproot/$(get_mntpoint_from_target $_target)"
893e0b
	fi
893e0b
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
check_kmodules_modified()
893e0b
{
893e0b
	# always sort again to avoid LANG/LC inconsistent problem
893e0b
	local _old_modules="$(lsinitrd $TARGET_INITRD -f /usr/lib/dracut/loaded-kernel-modules.txt | sort)"
893e0b
	local _new_modules="$(get_loaded_kernel_modules | sort)"
893e0b
893e0b
	[[ -z $_old_modules ]] && echo "Warning: Previous loaded kernel module list is absent or empty"
893e0b
893e0b
	local _added_modules=$(comm -13 <(echo "$_old_modules") <(echo "$_new_modules"))
893e0b
	local _dropped_modules=$(comm -23 <(echo "$_old_modules") <(echo "$_new_modules"))
893e0b
893e0b
	if [ "$_old_modules" != "$_new_modules" ]; then
893e0b
		echo "Detected change(s) of loaded kernel modules list:"
893e0b
		[[ -n $_added_modules ]] && for _module in $_added_modules; do
893e0b
			echo "	+$_module"
893e0b
		done
893e0b
		[[ -n $_dropped_modules ]] && for _module in $_dropped_modules; do
893e0b
			echo "	-$_module"
893e0b
		done
893e0b
		return 1
893e0b
	fi
893e0b
893e0b
	return 0
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
	check_kmodules_modified
893e0b
	if [ $? -ne 0 ]; then
893e0b
		return 1
893e0b
	fi
893e0b
893e0b
	return 0
893e0b
}
893e0b
893e0b
check_rebuild()
893e0b
{
893e0b
	local extra_modules
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
	#will rebuild every time if extra_modules are specified
893e0b
	extra_modules=`grep ^extra_modules $KDUMP_CONFIG_FILE`
893e0b
	[ -n "$extra_modules" ] && force_rebuild="1"
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
	handle_mode_switch
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
	if [[ ! -w "$KDUMP_BOOTDIR" ]];then
893e0b
		echo "$KDUMP_BOOTDIR does not have write permission. Can not rebuild $TARGET_INITRD"
893e0b
		return 1
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
			config_val=$(strip_comments $config_val)
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
			config_val=$(strip_comments $config_val)
893e0b
			SAVE_PATH=$config_val
893e0b
			;;
893e0b
		ssh)
893e0b
			config_val=$(strip_comments $config_val)
893e0b
			DUMP_TARGET=$config_val
893e0b
			;;
893e0b
		*)
893e0b
			;;
893e0b
		esac
893e0b
	done < $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
893e0b
check_ssh_target()
893e0b
{
893e0b
	local _ret
893e0b
	ssh -q -i $SSH_KEY_LOCATION -o BatchMode=yes $DUMP_TARGET mkdir -p $SAVE_PATH
893e0b
	_ret=$?
893e0b
	if [ $_ret -ne 0 ]; then
893e0b
		echo "Could not create $DUMP_TARGET:$SAVE_PATH, you probably need to run \"kdumpctl propagate\""  >&2
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
handle_mode_switch()
893e0b
{
893e0b
	if [ "$DEFAULT_DUMP_MODE" == "fadump" ]; then
893e0b
		# backup initrd for reference before replacing it
893e0b
		# with fadump aware initrd
893e0b
		backup_default_initrd
893e0b
	else
893e0b
		# check if a backup of default initrd exists. If yes,
893e0b
		# it signifies a switch from fadump mode. So, restore
893e0b
		# the backed up default initrd.
893e0b
		restore_default_initrd
893e0b
	fi
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
is_dump_target_configured()
893e0b
{
893e0b
	local _target
893e0b
893e0b
	_target=$(egrep "^ext[234]|^xfs|^btrfs|^minix|^raw|^ssh|^nfs" /etc/kdump.conf)
893e0b
893e0b
	[ -n "$_target" ]
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
893e0b
	if is_dump_target_configured; then
893e0b
		_target=$(local_fs_dump_target)
893e0b
		if [[ -n "$_target" ]]; then
893e0b
			_mnt=$(findmnt -k -f -n -r -o TARGET $_target)
893e0b
			if [ -z "$_mnt" ]; then
893e0b
				return
893e0b
			fi
893e0b
		else
893e0b
			return
893e0b
		fi
893e0b
	fi
893e0b
893e0b
	if is_mount_in_dracut_args; then
893e0b
		return;
893e0b
	fi
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
893e0b
check_default_config()
893e0b
{
893e0b
	local default_option
893e0b
893e0b
	default_option=$(awk '$1 ~ /^default$/ {print $2;}' $KDUMP_CONFIG_FILE)
893e0b
	if [ -z "$default_option" ]; then
893e0b
		return 0
893e0b
	else
893e0b
		case "$default_option" in
893e0b
		  reboot|halt|poweroff|shell|dump_to_rootfs)
893e0b
			return 0
893e0b
		  ;;
893e0b
		  *)
893e0b
			echo $"Usage kdump.conf: default {reboot|halt|poweroff|shell|dump_to_rootfs}"
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
893e0b
		echo "Kdump is not running: [WARNING]"
893e0b
		return 0
893e0b
	fi
893e0b
893e0b
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
893e0b
		stop_fadump
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
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
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
		;;
893e0b
	  condrestart)
893e0b
		;;
893e0b
	  propagate)
893e0b
		propagate_ssh_key
893e0b
		;;
893e0b
	  showmem)
893e0b
		show_reserved_mem
893e0b
		;;
893e0b
	  *)
893e0b
		echo $"Usage: $0 {start|stop|status|restart|reload|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 $?