ab224c
#! /bin/sh
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"
ab224c
SAVE_PATH=/var/crash
ab224c
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
ab224c
DUMP_TARGET=""
1b417c
TARGET_INITRD=""
1b417c
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled"
1b417c
FADUMP_REGISTER_SYS_NODE="/sys/kernel/fadump_registered"
1b417c
#kdump shall be the default dump mode
1b417c
DEFAULT_DUMP_MODE="kdump"
ab224c
ab224c
. /lib/kdump/kdump-lib.sh
ab224c
ab224c
standard_kexec_args="-p"
ab224c
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
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 | \
ab224c
			 sed -e "s/\b$arg=[^ ]*\b//g" \
ab224c
			     -e "s/\b$arg\b//g" \
ab224c
			     -e "s/\s\+/ /g"`
ab224c
	done
ab224c
	echo $cmdline
ab224c
}
ab224c
765b01
#
765b01
# This function returns the "initial apicid" of the
765b01
# boot cpu (cpu 0) if present.
765b01
#
1b417c
get_bootcpu_initial_apicid()
765b01
{
765b01
    awk '							\
765b01
	BEGIN { CPU = "-1"; }					\
765b01
	$1=="processor" && $2==":"	{ CPU = $NF; }		\
765b01
	CPU=="0" && /initial 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
765b01
# This function performs a series of edits on the command line
1b417c
prepare_cmdline()
765b01
{
765b01
	local cmdline;
765b01
	if [ -z "$KDUMP_COMMANDLINE" ]; then
765b01
		cmdline=`cat /proc/cmdline`
765b01
	else
765b01
		cmdline=${KDUMP_COMMANDLINE}
765b01
	fi
a6d77e
	cmdline=`remove_cmdline_param "$cmdline" crashkernel hugepages hugepagesz panic_on_warn`
765b01
765b01
765b01
	cmdline="${cmdline} ${KDUMP_COMMANDLINE_APPEND}"
765b01
765b01
	local id=`get_bootcpu_initial_apicid`
765b01
	if [ ! -z ${id} ] ; then
765b01
		cmdline=`append_cmdline "${cmdline}" disable_cpu_apicid ${id}`
765b01
	fi
765b01
765b01
	echo $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
	# backup fadump initrd for reference before replacing it
1b417c
	backup_initrd
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
1b417c
backup_initrd()
1b417c
{
1b417c
	local target_initrd_bak
1b417c
1b417c
	# Check if backup initrd is already present.
1b417c
	target_initrd_bak="$TARGET_INITRD.bak"
1b417c
	if [ ! -e $target_initrd_bak ];then
1b417c
		echo "Backing up $TARGET_INITRD"
1b417c
		cp $TARGET_INITRD $target_initrd_bak
1b417c
	fi
1b417c
}
1b417c
1b417c
check_config()
ab224c
{
ab224c
	local nr
ab224c
ab224c
	nr=$(awk 'BEGIN{cnt=0} /^raw|^ssh[[:blank:]]|^nfs|^ext[234]|^xfs|^btrfs|^minix/{cnt++} END{print cnt}' $KDUMP_CONFIG_FILE)
ab224c
	[ $nr -gt 1 ] && {
ab224c
		echo "More than one dump targets specified."
ab224c
		return 1
ab224c
	}
ab224c
ab224c
	while read config_opt config_val; do
1b417c
		# remove inline comments after the end of a directive.
1b417c
		config_val=$(strip_comments $config_val)
ab224c
		case "$config_opt" in
ab224c
		\#* | "")
ab224c
			;;
1b417c
		raw|ext2|ext3|ext4|minix|btrfs|xfs|nfs|ssh|sshkey|path|core_collector|kdump_post|kdump_pre|extra_bins|extra_modules|default|force_rebuild|dracut_args|fence_kdump_args|fence_kdump_nodes)
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
{
765b01
	local image_time=$1
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
1b417c
setup_target_initrd()
1b417c
{
1b417c
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
1b417c
		TARGET_INITRD="${KDUMP_BOOTDIR}/initramfs-${kdump_kver}.img"
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
1b417c
check_rebuild()
ab224c
{
ab224c
	local extra_modules modified_files=""
ab224c
	local _force_rebuild force_rebuild="0"
1b417c
	local initramfs_has_fadump
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}"
1b417c
	setup_target_initrd
1b417c
	if [ $? -ne 0 ]; then
1b417c
		return 1
1b417c
	fi
ab224c
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
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`
ab224c
	else
ab224c
		image_time=0
ab224c
	fi
ab224c
1b417c
	#also rebuild when Pacemaker cluster conf is changed and fence kdump is enabled.
1b417c
	modified_files=$(get_pcs_cluster_modified_files $image_time)
765b01
ab224c
	EXTRA_BINS=`grep ^kdump_post $KDUMP_CONFIG_FILE | cut -d\  -f2`
ab224c
	CHECK_FILES=`grep ^kdump_pre $KDUMP_CONFIG_FILE | cut -d\  -f2`
ab224c
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
ab224c
	CHECK_FILES=`grep ^extra_bins $KDUMP_CONFIG_FILE | cut -d\  -f2-`
ab224c
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
1b417c
	files="$KDUMP_CONFIG_FILE $kdump_kernel $EXTRA_BINS /etc/fstab"
765b01
ab224c
	check_exist "$files" && check_executable "$EXTRA_BINS"
ab224c
	[ $? -ne 0 ] && return 1
ab224c
ab224c
	for file in $files; do
ab224c
		time_stamp=`stat -c "%Y" $file`
ab224c
		if [ "$time_stamp" -gt "$image_time" ]; then
ab224c
			modified_files="$modified_files $file"
ab224c
		fi
ab224c
	done
ab224c
1b417c
	#check if target initrd has fadump support
1b417c
	if [ "$DEFAULT_DUMP_MODE" = "fadump" ] && [ -f "$TARGET_INITRD" ]; then
1b417c
		initramfs_has_fadump=`lsinitrd -m $TARGET_INITRD | grep ^kdumpbase$ | wc -l`
1b417c
	fi
1b417c
ab224c
	if [ $image_time -eq 0 ]; then
ab224c
		echo  -n "No kdump initial ramdisk found."; echo
1b417c
	elif [ $DEFAULT_DUMP_MODE == "fadump" ] && [ "$initramfs_has_fadump" -eq "0" ]; then
1b417c
		echo "$TARGET_INITRD has no fadump support"
ab224c
	elif [ "$force_rebuild" != "0" ]; then
1b417c
		echo -n "Force rebuild $TARGET_INITRD"; echo
ab224c
	elif [ -n "$modified_files" ]; then
1b417c
		echo "Detected change(s) in the following file(s):"
ab224c
		echo -n "  "; echo "$modified_files" | sed 's/\s/\n  /g'
ab224c
	else
ab224c
		return 0
ab224c
	fi
ab224c
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
ab224c
# Load the kdump kerel 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
	MEM_RESERVED=$(cat /sys/kernel/kexec_crash_size)
ab224c
	if [ $MEM_RESERVED -eq 0 ]
ab224c
	then
ab224c
		echo "No memory reserved for crash kernel." >&2
ab224c
		return 1
ab224c
	fi
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
765b01
	KDUMP_COMMANDLINE=`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
1b417c
		# remove inline comments after the end of a directive.
1b417c
		config_val=$(strip_comments $config_val)
ab224c
		case "$config_opt" in
ab224c
		sshkey)
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)
ab224c
			SAVE_PATH=$config_val
ab224c
			;;
ab224c
		ssh)
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
1b417c
is_fadump_capable()
1b417c
{
1b417c
	# Check if firmware-assisted dump is enabled
1b417c
	# if no, fallback to kdump check
1b417c
	if [ -f $FADUMP_ENABLED_SYS_NODE ]; then
1b417c
		rc=`cat $FADUMP_ENABLED_SYS_NODE`
1b417c
		[ $rc -eq 1 ] && return 0
1b417c
	fi
1b417c
	return 1
1b417c
}
1b417c
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
get_save_path()
1b417c
{
ab224c
	local _save_path=$(grep "^path" /etc/kdump.conf|awk '{print $2}')
ab224c
	if [ -z "$_save_path" ]; then
ab224c
		_save_path="/var/crash"
ab224c
	fi
ab224c
ab224c
	echo $_save_path
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
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
765b01
	secure_mode_byte=$(cat /sys/kernel/security/securelevel)
765b01
765b01
	if [ "$secure_mode_byte" = "1" ]; then
765b01
		return 0
765b01
	fi
765b01
765b01
	return 1
765b01
}
765b01
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
1b417c
}
765b01
1b417c
check_fence_kdump_config()
1b417c
{
1b417c
	local hostname=`hostname`
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
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
{
ab224c
	check_config
ab224c
	if [ $? -ne 0 ]; then
ab224c
		echo "Starting kdump: [FAILED]"
ab224c
		return 1
ab224c
	fi
ab224c
ab224c
	if sestatus 2>/dev/null | grep -q "SELinux status.*enabled"; then
ab224c
		selinux_relabel
ab224c
	fi
ab224c
	save_raw
ab224c
	if [ $? -ne 0 ]; then
ab224c
		echo "Starting kdump: [FAILED]"
ab224c
		return 1
ab224c
	fi
ab224c
1b417c
	check_dump_feasibility
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 $?