de80c6
#! /bin/sh
de80c6
KEXEC=/sbin/kexec
de80c6
de80c6
KDUMP_KERNELVER=""
de80c6
KDUMP_COMMANDLINE=""
de80c6
KEXEC_ARGS=""
de80c6
KDUMP_CONFIG_FILE="/etc/kdump.conf"
de80c6
MKDUMPRD="/sbin/mkdumprd -f"
de80c6
SAVE_PATH=/var/crash
de80c6
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
de80c6
DUMP_TARGET=""
de80c6
TARGET_INITRD=""
de80c6
FADUMP_ENABLED_SYS_NODE="/sys/kernel/fadump_enabled"
de80c6
FADUMP_REGISTER_SYS_NODE="/sys/kernel/fadump_registered"
de80c6
#kdump shall be the default dump mode
de80c6
DEFAULT_DUMP_MODE="kdump"
de80c6
de80c6
. /lib/kdump/kdump-lib.sh
de80c6
de80c6
standard_kexec_args="-p"
de80c6
de80c6
if [ -f /etc/sysconfig/kdump ]; then
de80c6
	. /etc/sysconfig/kdump
de80c6
fi
de80c6
de80c6
single_instance_lock()
de80c6
{
de80c6
	local rc timeout=5
de80c6
de80c6
	exec 9>/var/lock/kdump
de80c6
de80c6
	flock -n 9
de80c6
	rc=$?
de80c6
de80c6
	while [ $rc -ne 0 ]; do
de80c6
		echo "Another app is currently holding the kdump lock; waiting for it to exit..."
de80c6
		flock -w $timeout 9
de80c6
		rc=$?
de80c6
	done
de80c6
}
de80c6
de80c6
determine_dump_mode()
de80c6
{
de80c6
	# Check if firmware-assisted dump is enabled
de80c6
	# if yes, set the dump mode as fadump
de80c6
	if is_fadump_capable; then
de80c6
		echo "Dump mode is fadump"
de80c6
		DEFAULT_DUMP_MODE="fadump"
de80c6
	fi
de80c6
}
de80c6
de80c6
# remove_cmdline_param <kernel cmdline> <param1> [<param2>] ... [<paramN>]
de80c6
# Remove a list of kernel parameters from a given kernel cmdline and print the result.
de80c6
# For each "arg" in the removing params list, "arg" and "arg=xxx" will be removed if exists.
de80c6
remove_cmdline_param()
de80c6
{
de80c6
	local cmdline=$1
de80c6
	shift
de80c6
de80c6
	for arg in $@; do
de80c6
		cmdline=`echo $cmdline | \
de80c6
			 sed -e "s/\b$arg=[^ ]*\b//g" \
de80c6
			     -e "s/\b$arg\b//g" \
de80c6
			     -e "s/\s\+/ /g"`
de80c6
	done
de80c6
	echo $cmdline
de80c6
}
de80c6
de80c6
#
de80c6
# This function returns the "initial apicid" of the
de80c6
# boot cpu (cpu 0) if present.
de80c6
#
de80c6
get_bootcpu_initial_apicid()
de80c6
{
de80c6
    awk '							\
de80c6
	BEGIN { CPU = "-1"; }					\
de80c6
	$1=="processor" && $2==":"	{ CPU = $NF; }		\
de80c6
	CPU=="0" && /initial apicid/	{ print $NF; }		\
de80c6
	'							\
de80c6
	/proc/cpuinfo
de80c6
}
de80c6
de80c6
#
de80c6
# This function appends argument "$2=$3" to string ($1) if not already present.
de80c6
#
de80c6
append_cmdline()
de80c6
{
de80c6
	local cmdline=$1
de80c6
	local newstr=${cmdline/$2/""}
de80c6
de80c6
	# unchanged str implies argument wasn't there
de80c6
	if [ "$cmdline" == "$newstr" ]; then
de80c6
		cmdline="${cmdline} ${2}=${3}"
de80c6
	fi
de80c6
de80c6
	echo $cmdline
de80c6
}
de80c6
de80c6
# This function performs a series of edits on the command line
de80c6
prepare_cmdline()
de80c6
{
de80c6
	local cmdline;
de80c6
	if [ -z "$KDUMP_COMMANDLINE" ]; then
de80c6
		cmdline=`cat /proc/cmdline`
de80c6
	else
de80c6
		cmdline=${KDUMP_COMMANDLINE}
de80c6
	fi
de80c6
	cmdline=`remove_cmdline_param "$cmdline" crashkernel hugepages hugepagesz`
de80c6
de80c6
de80c6
	cmdline="${cmdline} ${KDUMP_COMMANDLINE_APPEND}"
de80c6
de80c6
	local id=`get_bootcpu_initial_apicid`
de80c6
	if [ ! -z ${id} ] ; then
de80c6
		cmdline=`append_cmdline "${cmdline}" disable_cpu_apicid ${id}`
de80c6
	fi
de80c6
de80c6
	echo $cmdline
de80c6
}
de80c6
de80c6
de80c6
save_core()
de80c6
{
de80c6
	coredir="/var/crash/`date +"%Y-%m-%d-%H:%M"`"
de80c6
de80c6
	mkdir -p $coredir
de80c6
	cp --sparse=always /proc/vmcore $coredir/vmcore-incomplete
de80c6
	if [ $? == 0 ]; then
de80c6
		mv $coredir/vmcore-incomplete $coredir/vmcore
de80c6
		echo "saved a vmcore to $coredir"
de80c6
	else
de80c6
		echo "failed to save a vmcore to $coredir" >&2
de80c6
	fi
de80c6
de80c6
	# pass the dmesg to Abrt tool if exists, in order
de80c6
	# to collect the kernel oops message.
de80c6
	# https://fedorahosted.org/abrt/
de80c6
	if [ -x /usr/bin/dumpoops ]; then
de80c6
		makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg >/dev/null 2>&1
de80c6
		dumpoops -d $coredir/dmesg >/dev/null 2>&1
de80c6
		if [ $? == 0 ]; then
de80c6
			echo "kernel oops has been collected by abrt tool"
de80c6
		fi
de80c6
	fi
de80c6
}
de80c6
de80c6
rebuild_fadump_initrd()
de80c6
{
de80c6
	local target_initrd_tmp
de80c6
de80c6
	# backup fadump initrd for reference before replacing it
de80c6
	backup_initrd
de80c6
de80c6
	# this file tells the initrd is fadump enabled
de80c6
	touch /tmp/fadump.initramfs
de80c6
	target_initrd_tmp="$TARGET_INITRD.tmp"
de80c6
	$MKDUMPRD $target_initrd_tmp --rebuild $TARGET_INITRD --kver $kdump_kver \
de80c6
		-i /tmp/fadump.initramfs /etc/fadump.initramfs
de80c6
	if [ $? != 0 ]; then
de80c6
		echo "mkdumprd: failed to rebuild initrd with fadump support" >&2
de80c6
		rm -f /tmp/fadump.initramfs
de80c6
		return 1
de80c6
	fi
de80c6
	rm -f /tmp/fadump.initramfs
de80c6
de80c6
	# updating fadump initrd
de80c6
	mv $target_initrd_tmp $TARGET_INITRD
de80c6
	sync
de80c6
de80c6
	return 0
de80c6
}
de80c6
de80c6
rebuild_kdump_initrd()
de80c6
{
de80c6
	$MKDUMPRD $TARGET_INITRD $kdump_kver
de80c6
	if [ $? != 0 ]; then
de80c6
		echo "mkdumprd: failed to make kdump initrd" >&2
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	return 0
de80c6
}
de80c6
de80c6
rebuild_initrd()
de80c6
{
de80c6
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
de80c6
		rebuild_fadump_initrd
de80c6
	else
de80c6
		rebuild_kdump_initrd
de80c6
	fi
de80c6
de80c6
	return $?
de80c6
}
de80c6
de80c6
#$1: the files to be checked with IFS=' '
de80c6
check_exist()
de80c6
{
de80c6
	for file in $1; do
de80c6
		if [ ! -f "$file" ]; then
de80c6
			echo -n "Error: $file not found."; echo
de80c6
			return 1
de80c6
		fi
de80c6
	done
de80c6
}
de80c6
de80c6
#$1: the files to be checked with IFS=' '
de80c6
check_executable()
de80c6
{
de80c6
	for file in $1; do
de80c6
		if [ ! -x "$file" ]; then
de80c6
			echo -n "Error: $file is not executable."; echo
de80c6
			return 1
de80c6
		fi
de80c6
	done
de80c6
}
de80c6
de80c6
backup_initrd()
de80c6
{
de80c6
	local target_initrd_bak
de80c6
de80c6
	# Check if backup initrd is already present.
de80c6
	target_initrd_bak="$TARGET_INITRD.bak"
de80c6
	if [ ! -e $target_initrd_bak ];then
de80c6
		echo "Backing up $TARGET_INITRD"
de80c6
		cp $TARGET_INITRD $target_initrd_bak
de80c6
	fi
de80c6
}
de80c6
de80c6
check_config()
de80c6
{
de80c6
	local nr
de80c6
de80c6
	nr=$(awk 'BEGIN{cnt=0} /^raw|^ssh[[:blank:]]|^nfs|^ext[234]|^xfs|^btrfs|^minix/{cnt++} END{print cnt}' $KDUMP_CONFIG_FILE)
de80c6
	[ $nr -gt 1 ] && {
de80c6
		echo "More than one dump targets specified."
de80c6
		return 1
de80c6
	}
de80c6
de80c6
	while read config_opt config_val; do
de80c6
		# remove inline comments after the end of a directive.
de80c6
		config_val=$(strip_comments $config_val)
de80c6
		case "$config_opt" in
de80c6
		\#* | "")
de80c6
			;;
de80c6
		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)
de80c6
			[ -z "$config_val" ] && {
de80c6
				echo "Invalid kdump config value for option $config_opt."
de80c6
				return 1;
de80c6
			}
de80c6
			;;
de80c6
		net|options|link_delay|disk_timeout|debug_mem_level|blacklist)
de80c6
			echo "Deprecated kdump config option: $config_opt. Refer to kdump.conf manpage for alternatives."
de80c6
			return 1
de80c6
			;;
de80c6
		*)
de80c6
			echo "Invalid kdump config option $config_opt"
de80c6
			return 1;
de80c6
			;;
de80c6
		esac
de80c6
	done < $KDUMP_CONFIG_FILE
de80c6
de80c6
	check_fence_kdump_config || return 1
de80c6
de80c6
	return 0
de80c6
}
de80c6
de80c6
# get_pcs_cluster_modified_files <image timestamp>
de80c6
# return list of modified file for fence_kdump modified in Pacemaker cluster
de80c6
get_pcs_cluster_modified_files()
de80c6
{
de80c6
	local image_time=$1
de80c6
	local time_stamp
de80c6
	local modified_files
de80c6
de80c6
	is_generic_fence_kdump && return 1
de80c6
	is_pcs_fence_kdump || return 1
de80c6
de80c6
	time_stamp=`pcs cluster cib | xmllint --xpath 'string(/cib/@cib-last-written)' - | \
de80c6
		xargs -0 date +%s --date`
de80c6
de80c6
	if [ -n $time_stamp -a $time_stamp -gt $image_time ]; then
de80c6
		modified_files="cluster-cib"
de80c6
	fi
de80c6
de80c6
	if [ -f $FENCE_KDUMP_CONFIG_FILE ]; then
de80c6
		time_stamp=`stat -c "%Y" $FENCE_KDUMP_CONFIG_FILE`
de80c6
		if [ "$time_stamp" -gt "$image_time" ]; then
de80c6
			modified_files="$modified_files $FENCE_KDUMP_CONFIG_FILE"
de80c6
		fi
de80c6
	fi
de80c6
de80c6
	echo $modified_files
de80c6
}
de80c6
de80c6
setup_target_initrd()
de80c6
{
de80c6
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
de80c6
		TARGET_INITRD="${KDUMP_BOOTDIR}/initramfs-${kdump_kver}.img"
de80c6
		if [ ! -s "$TARGET_INITRD" ]; then
de80c6
			echo "Error: No initrd found to rebuild!"
de80c6
			return 1
de80c6
		fi
de80c6
	else
de80c6
		TARGET_INITRD="${KDUMP_BOOTDIR}/initramfs-${kdump_kver}kdump.img"
de80c6
	fi
de80c6
}
de80c6
de80c6
check_rebuild()
de80c6
{
de80c6
	local extra_modules modified_files=""
de80c6
	local _force_rebuild force_rebuild="0"
de80c6
	local initramfs_has_fadump
de80c6
de80c6
	if [ -z "$KDUMP_KERNELVER" ]; then
de80c6
		kdump_kver=`uname -r`
de80c6
	else
de80c6
		kdump_kver=$KDUMP_KERNELVER
de80c6
	fi
de80c6
de80c6
	kdump_kernel="${KDUMP_BOOTDIR}/${KDUMP_IMG}-${kdump_kver}${KDUMP_IMG_EXT}"
de80c6
	setup_target_initrd
de80c6
	if [ $? -ne 0 ]; then
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	_force_rebuild=`grep ^force_rebuild $KDUMP_CONFIG_FILE 2>/dev/null`
de80c6
	if [ $? -eq 0 ]; then
de80c6
		force_rebuild=`echo $_force_rebuild | cut -d' '  -f2`
de80c6
		if [ "$force_rebuild" != "0" ] && [ "$force_rebuild" != "1" ];then
de80c6
			echo "Error: force_rebuild value is invalid"
de80c6
			return 1
de80c6
		fi
de80c6
	fi
de80c6
de80c6
	#will rebuild every time if extra_modules are specified
de80c6
	extra_modules=`grep ^extra_modules $KDUMP_CONFIG_FILE`
de80c6
	[ -n "$extra_modules" ] && force_rebuild="1"
de80c6
de80c6
	#check to see if dependent files has been modified
de80c6
	#since last build of the image file
de80c6
	if [ -f $TARGET_INITRD ]; then
de80c6
		image_time=`stat -c "%Y" $TARGET_INITRD 2>/dev/null`
de80c6
	else
de80c6
		image_time=0
de80c6
	fi
de80c6
de80c6
	#also rebuild when Pacemaker cluster conf is changed and fence kdump is enabled.
de80c6
	modified_files=$(get_pcs_cluster_modified_files $image_time)
de80c6
de80c6
	EXTRA_BINS=`grep ^kdump_post $KDUMP_CONFIG_FILE | cut -d\  -f2`
de80c6
	CHECK_FILES=`grep ^kdump_pre $KDUMP_CONFIG_FILE | cut -d\  -f2`
de80c6
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
de80c6
	CHECK_FILES=`grep ^extra_bins $KDUMP_CONFIG_FILE | cut -d\  -f2-`
de80c6
	EXTRA_BINS="$EXTRA_BINS $CHECK_FILES"
de80c6
	files="$KDUMP_CONFIG_FILE $kdump_kernel $EXTRA_BINS /etc/fstab"
de80c6
de80c6
	check_exist "$files" && check_executable "$EXTRA_BINS"
de80c6
	[ $? -ne 0 ] && return 1
de80c6
de80c6
	for file in $files; do
de80c6
		time_stamp=`stat -c "%Y" $file`
de80c6
		if [ "$time_stamp" -gt "$image_time" ]; then
de80c6
			modified_files="$modified_files $file"
de80c6
		fi
de80c6
	done
de80c6
de80c6
	#check if target initrd has fadump support
de80c6
	if [ "$DEFAULT_DUMP_MODE" = "fadump" ] && [ -f "$TARGET_INITRD" ]; then
de80c6
		initramfs_has_fadump=`lsinitrd -m $TARGET_INITRD | grep ^kdumpbase$ | wc -l`
de80c6
	fi
de80c6
de80c6
	if [ $image_time -eq 0 ]; then
de80c6
		echo  -n "No kdump initial ramdisk found."; echo
de80c6
	elif [ $DEFAULT_DUMP_MODE == "fadump" ] && [ "$initramfs_has_fadump" -eq "0" ]; then
de80c6
		echo "$TARGET_INITRD has no fadump support"
de80c6
	elif [ "$force_rebuild" != "0" ]; then
de80c6
		echo -n "Force rebuild $TARGET_INITRD"; echo
de80c6
	elif [ -n "$modified_files" ]; then
de80c6
		echo "Detected change(s) in the following file(s):"
de80c6
		echo -n "  "; echo "$modified_files" | sed 's/\s/\n  /g'
de80c6
	else
de80c6
		return 0
de80c6
	fi
de80c6
de80c6
	echo "Rebuilding $TARGET_INITRD"
de80c6
	rebuild_initrd
de80c6
	return $?
de80c6
}
de80c6
de80c6
# This function check iomem and determines if we have more than
de80c6
# 4GB of ram available. Returns 1 if we do, 0 if we dont
de80c6
need_64bit_headers()
de80c6
{
de80c6
	return `tail -n 1 /proc/iomem | awk '{ split ($1, r, "-"); \
de80c6
	print (strtonum("0x" r[2]) > strtonum("0xffffffff")); }'`
de80c6
}
de80c6
de80c6
# Load the kdump kerel specified in /etc/sysconfig/kdump
de80c6
# If none is specified, try to load a kdump kernel with the same version
de80c6
# as the currently running kernel.
de80c6
load_kdump()
de80c6
{
de80c6
	MEM_RESERVED=$(cat /sys/kernel/kexec_crash_size)
de80c6
	if [ $MEM_RESERVED -eq 0 ]
de80c6
	then
de80c6
		echo "No memory reserved for crash kernel." >&2
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	ARCH=`uname -m`
de80c6
	if [ "$ARCH" == "i686" -o "$ARCH" == "i386" ]
de80c6
	then
de80c6
de80c6
		need_64bit_headers
de80c6
		if [ $? == 1 ]
de80c6
		then
de80c6
			FOUND_ELF_ARGS=`echo $KEXEC_ARGS | grep elf32-core-headers`
de80c6
			if [ -n "$FOUND_ELF_ARGS" ]
de80c6
			then
de80c6
				echo -n "Warning: elf32-core-headers overrides correct elf64 setting"
de80c6
				echo
de80c6
			else	
de80c6
				KEXEC_ARGS="$KEXEC_ARGS --elf64-core-headers"
de80c6
			fi
de80c6
		else
de80c6
			FOUND_ELF_ARGS=`echo $KEXEC_ARGS | grep elf64-core-headers`
de80c6
			if [ -z "$FOUND_ELF_ARGS" ]
de80c6
			then
de80c6
				KEXEC_ARGS="$KEXEC_ARGS --elf32-core-headers"
de80c6
			fi
de80c6
		fi
de80c6
	fi
de80c6
de80c6
	KDUMP_COMMANDLINE=`prepare_cmdline`
de80c6
de80c6
	# For secureboot enabled machines, use new kexec file based syscall.
de80c6
	# Old syscall will always fail as it does not have capability to
de80c6
	# to kernel signature verification.
de80c6
	if is_secure_boot_enforced; then
de80c6
		echo "Secure Boot is enabled. Using kexec file based syscall."
de80c6
		KEXEC_ARGS="$KEXEC_ARGS -s"
de80c6
	elif is_secure_mode_enforced; then
de80c6
		echo "securelevel is set to 1 (Secure Mode). Using kexec file based syscall."
de80c6
		KEXEC_ARGS="$KEXEC_ARGS -s"
de80c6
	fi
de80c6
de80c6
	$KEXEC $KEXEC_ARGS $standard_kexec_args \
de80c6
		--command-line="$KDUMP_COMMANDLINE" \
de80c6
		--initrd=$TARGET_INITRD $kdump_kernel
de80c6
	if [ $? == 0 ]; then
de80c6
		echo "kexec: loaded kdump kernel"
de80c6
		return 0
de80c6
	else
de80c6
		echo "kexec: failed to load kdump kernel" >&2
de80c6
		return 1
de80c6
	fi
de80c6
}
de80c6
de80c6
check_ssh_config()
de80c6
{
de80c6
	while read config_opt config_val; do
de80c6
		# remove inline comments after the end of a directive.
de80c6
		config_val=$(strip_comments $config_val)
de80c6
		case "$config_opt" in
de80c6
		sshkey)
de80c6
			if [ -f "$config_val" ]; then
de80c6
				# canonicalize the path
de80c6
				SSH_KEY_LOCATION=$(/usr/bin/readlink -m $config_val)
de80c6
			else
de80c6
				echo "WARNING: '$config_val' doesn't exist, using default value '$SSH_KEY_LOCATION'"
de80c6
			fi
de80c6
			;;
de80c6
		path)
de80c6
			SAVE_PATH=$config_val
de80c6
			;;
de80c6
		ssh)
de80c6
			DUMP_TARGET=$config_val
de80c6
			;;
de80c6
		*)
de80c6
			;;
de80c6
		esac
de80c6
	done < $KDUMP_CONFIG_FILE
de80c6
de80c6
	#make sure they've configured kdump.conf for ssh dumps
de80c6
	local SSH_TARGET=`echo -n $DUMP_TARGET | sed -n '/.*@/p'`
de80c6
	if [ -z "$SSH_TARGET" ]; then
de80c6
		return 1
de80c6
	fi
de80c6
	return 0
de80c6
}
de80c6
de80c6
check_ssh_target()
de80c6
{
de80c6
	local _ret
de80c6
	ssh -q -i $SSH_KEY_LOCATION -o BatchMode=yes $DUMP_TARGET mkdir -p $SAVE_PATH
de80c6
	_ret=$?
de80c6
	if [ $_ret -ne 0 ]; then
de80c6
		echo "Could not create $DUMP_TARGET:$SAVE_PATH, you probably need to run \"kdumpctl propagate\""  >&2
de80c6
		return 1
de80c6
	fi
de80c6
	return 0
de80c6
}
de80c6
de80c6
propagate_ssh_key()
de80c6
{
de80c6
	check_ssh_config
de80c6
	if [ $? -ne 0 ]; then
de80c6
		echo "No ssh config specified in $KDUMP_CONFIG_FILE.  Can't propagate" >&2
de80c6
		exit 1
de80c6
	fi
de80c6
de80c6
	local KEYFILE=$SSH_KEY_LOCATION
de80c6
	local errmsg="Failed to propagate ssh key"
de80c6
de80c6
	#Check to see if we already created key, if not, create it.
de80c6
	if [ -f $KEYFILE ]; then
de80c6
		echo "Using existing keys..."
de80c6
	else
de80c6
		echo -n "Generating new ssh keys... "
de80c6
		/usr/bin/ssh-keygen -t rsa -f $KEYFILE -N "" 2>&1 > /dev/null
de80c6
		echo "done."
de80c6
	fi
de80c6
de80c6
	#now find the target ssh user and server to contact.
de80c6
	SSH_USER=`echo $DUMP_TARGET | cut -d\  -f2 | cut -d@ -f1`
de80c6
	SSH_SERVER=`echo $DUMP_TARGET | sed -e's/\(.*@\)\(.*$\)/\2/'`
de80c6
de80c6
	#now send the found key to the found server
de80c6
	ssh-copy-id -i $KEYFILE $SSH_USER@$SSH_SERVER
de80c6
	RET=$?
de80c6
	if [ $RET == 0 ]; then
de80c6
		echo $KEYFILE has been added to ~$SSH_USER/.ssh/authorized_keys on $SSH_SERVER
de80c6
		return 0
de80c6
	else
de80c6
		echo $errmsg, $KEYFILE failed in transfer to $SSH_SERVER  >&2
de80c6
		exit 1
de80c6
	fi
de80c6
}
de80c6
de80c6
is_fadump_capable()
de80c6
{
de80c6
	# Check if firmware-assisted dump is enabled
de80c6
	# if no, fallback to kdump check
de80c6
	if [ -f $FADUMP_ENABLED_SYS_NODE ]; then
de80c6
		rc=`cat $FADUMP_ENABLED_SYS_NODE`
de80c6
		[ $rc -eq 1 ] && return 0
de80c6
	fi
de80c6
	return 1
de80c6
}
de80c6
de80c6
check_current_fadump_status()
de80c6
{
de80c6
	# Check if firmware-assisted dump has been registered.
de80c6
	rc=`cat $FADUMP_REGISTER_SYS_NODE`
de80c6
	[ $rc -eq 1 ] && return 0
de80c6
	return 1
de80c6
}
de80c6
de80c6
check_current_kdump_status()
de80c6
{
de80c6
	rc=`cat /sys/kernel/kexec_crash_loaded`
de80c6
	if [ $rc == 1 ]; then
de80c6
		return 0
de80c6
	else
de80c6
		return 1
de80c6
	fi
de80c6
}
de80c6
de80c6
check_current_status()
de80c6
{
de80c6
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
de80c6
		check_current_fadump_status
de80c6
	else
de80c6
		check_current_kdump_status
de80c6
	fi
de80c6
de80c6
	return $?
de80c6
}
de80c6
de80c6
save_raw()
de80c6
{
de80c6
	local kdump_dir
de80c6
	local raw_target
de80c6
de80c6
	raw_target=$(awk '$1 ~ /^raw$/ { print $2; }' $KDUMP_CONFIG_FILE)
de80c6
	[ -z "$raw_target" ] && return 0
de80c6
	[ -b "$raw_target" ] || {
de80c6
		echo "raw partition $raw_target not found"
de80c6
		return 1
de80c6
	}
de80c6
	kdump_dir=`grep ^path $KDUMP_CONFIG_FILE | cut -d' '  -f2-`
de80c6
	if [ -z "${kdump_dir}" ]; then
de80c6
		coredir="/var/crash/`date +"%Y-%m-%d-%H:%M"`"
de80c6
	else
de80c6
		coredir="${kdump_dir}/`date +"%Y-%m-%d-%H:%M"`"
de80c6
	fi
de80c6
de80c6
	mkdir -p "$coredir"
de80c6
	[ -d "$coredir" ] || {
de80c6
		echo "failed to create $coredir"
de80c6
		return 1
de80c6
	}
de80c6
	if makedumpfile -R $coredir/vmcore <$raw_target >/dev/null 2>&1; then
de80c6
		# dump found
de80c6
		echo "Dump saved to $coredir/vmcore"
de80c6
		# wipe makedumpfile header
de80c6
		dd if=/dev/zero of=$raw_target bs=1b count=1 2>/dev/null
de80c6
	else
de80c6
		rm -rf "$coredir"
de80c6
	fi
de80c6
de80c6
	return 0
de80c6
}
de80c6
de80c6
get_save_path()
de80c6
{
de80c6
	local _save_path=$(grep "^path" /etc/kdump.conf|awk '{print $2}')
de80c6
	if [ -z "$_save_path" ]; then
de80c6
		_save_path="/var/crash"
de80c6
	fi
de80c6
de80c6
	echo $_save_path
de80c6
}
de80c6
de80c6
is_dump_target_configured()
de80c6
{
de80c6
	local _target
de80c6
de80c6
	_target=$(egrep "^ext[234]|^xfs|^btrfs|^minix|^raw|^ssh|^nfs" /etc/kdump.conf)
de80c6
de80c6
	[ -n "$_target" ]
de80c6
}
de80c6
de80c6
local_fs_dump_target()
de80c6
{
de80c6
	local _target
de80c6
de80c6
	_target=$(egrep "^ext[234]|^xfs|^btrfs|^minix" /etc/kdump.conf)
de80c6
	if [ $? -eq 0 ]; then
de80c6
		echo $_target|awk '{print $2}'
de80c6
	fi
de80c6
}
de80c6
de80c6
path_to_be_relabeled()
de80c6
{
de80c6
	local _path _target _mnt="/" _rmnt
de80c6
de80c6
	if is_dump_target_configured; then
de80c6
		_target=$(local_fs_dump_target)
de80c6
		if [[ -n "$_target" ]]; then
de80c6
			_mnt=$(findmnt -k -f -n -r -o TARGET $_target)
de80c6
			if [ -z "$_mnt" ]; then
de80c6
				return
de80c6
			fi
de80c6
		else
de80c6
			return
de80c6
		fi
de80c6
	fi
de80c6
de80c6
	_path=$(get_save_path)
de80c6
	# if $_path is masked by other mount, we will not relabel it.
de80c6
	_rmnt=$(df $_mnt/$_path 2>/dev/null | tail -1 | awk '{ print $NF }')
de80c6
	if [ "$_rmnt" == "$_mnt" ]; then
de80c6
		echo $_mnt/$_path
de80c6
	fi
de80c6
}
de80c6
de80c6
selinux_relabel()
de80c6
{
de80c6
	local _path _i _attr
de80c6
de80c6
	_path=$(path_to_be_relabeled)
de80c6
	if [ -z "$_path" ] || ! [ -d "$_path" ] ; then
de80c6
		return
de80c6
	fi
de80c6
de80c6
	for _i in $(find $_path); do
de80c6
		_attr=$(getfattr -m "security.selinux" $_i 2>/dev/null)
de80c6
		if [ -z "$_attr" ]; then
de80c6
			restorecon $_i;
de80c6
		fi
de80c6
	done
de80c6
}
de80c6
de80c6
# Check if secure boot is being enforced.
de80c6
#
de80c6
# Per Peter Jones, we need check efivar SecureBoot-$(the UUID) and
de80c6
# SetupMode-$(the UUID), they are both 5 bytes binary data. The first four
de80c6
# bytes are the attributes associated with the variable and can safely be
de80c6
# ignored, the last bytes are one-byte true-or-false variables. If SecureBoot
de80c6
# is 1 and SetupMode is 0, then secure boot is being enforced.
de80c6
#
de80c6
# SecureBoot-UUID won't always be set when securelevel is 1. For legacy-mode
de80c6
# and uefi-without-seucre-enabled system, we can manually enable secure mode
de80c6
# by writing "1" to securelevel. So check both efi var and secure mode is a 
de80c6
# more sane way.
de80c6
#
de80c6
# Assume efivars is mounted at /sys/firmware/efi/efivars.
de80c6
is_secure_boot_enforced()
de80c6
{
de80c6
	local secure_boot_file setup_mode_file
de80c6
	local secure_boot_byte setup_mode_byte
de80c6
de80c6
	secure_boot_file=$(find /sys/firmware/efi/efivars -name SecureBoot-* 2>/dev/null)
de80c6
	setup_mode_file=$(find /sys/firmware/efi/efivars -name SetupMode-* 2>/dev/null)
de80c6
de80c6
	if [ -f "$secure_boot_file" ] && [ -f "$setup_mode_file" ]; then
de80c6
		secure_boot_byte=$(hexdump -v -e '/1 "%d\ "' $secure_boot_file|cut -d' ' -f 5)
de80c6
		setup_mode_byte=$(hexdump -v -e '/1 "%d\ "' $setup_mode_file|cut -d' ' -f 5)
de80c6
de80c6
		if [ "$secure_boot_byte" = "1" ] && [ "$setup_mode_byte" = "0" ]; then
de80c6
			return 0
de80c6
		fi
de80c6
	fi
de80c6
de80c6
	return 1
de80c6
}
de80c6
de80c6
# Check if secure mode is being enforced (securelevel =? 1)
de80c6
is_secure_mode_enforced()
de80c6
{
de80c6
	local secure_mode_byte
de80c6
de80c6
	secure_mode_byte=$(cat /sys/kernel/security/securelevel)
de80c6
de80c6
	if [ "$secure_mode_byte" = "1" ]; then
de80c6
		return 0
de80c6
	fi
de80c6
de80c6
	return 1
de80c6
}
de80c6
de80c6
check_kdump_feasibility()
de80c6
{
de80c6
	if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
de80c6
		echo "Kdump is not supported on this kernel"
de80c6
		return 1
de80c6
	fi
de80c6
}
de80c6
de80c6
check_fence_kdump_config()
de80c6
{
de80c6
	local hostname=`hostname`
de80c6
	local nodes=$(get_option_value "fence_kdump_nodes")
de80c6
de80c6
	for node in $nodes; do
de80c6
		if [ "$node" = "$hostname" ]; then
de80c6
			echo "Option fence_kdump_nodes cannot contain $hostname"
de80c6
			return 1
de80c6
		fi
de80c6
	done
de80c6
de80c6
	return 0
de80c6
}
de80c6
de80c6
check_dump_feasibility()
de80c6
{
de80c6
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
de80c6
		return 0
de80c6
	fi
de80c6
de80c6
	check_kdump_feasibility
de80c6
	return $?
de80c6
}
de80c6
de80c6
start_fadump()
de80c6
{
de80c6
	echo 1 > $FADUMP_REGISTER_SYS_NODE
de80c6
	if ! check_current_fadump_status; then
de80c6
		echo "fadump: failed to register"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	echo "fadump: registered successfully"
de80c6
	return 0
de80c6
}
de80c6
de80c6
start_dump()
de80c6
{
de80c6
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
de80c6
		start_fadump
de80c6
	else
de80c6
		load_kdump
de80c6
	fi
de80c6
de80c6
	return $?
de80c6
}
de80c6
de80c6
start()
de80c6
{
de80c6
	check_config
de80c6
	if [ $? -ne 0 ]; then
de80c6
		echo "Starting kdump: [FAILED]"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	if sestatus 2>/dev/null | grep -q "SELinux status.*enabled"; then
de80c6
		selinux_relabel
de80c6
	fi
de80c6
	save_raw
de80c6
	if [ $? -ne 0 ]; then
de80c6
		echo "Starting kdump: [FAILED]"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	check_dump_feasibility
de80c6
	if [ $? -ne 0 ]; then
de80c6
		echo "Starting kdump: [FAILED]"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	check_current_status
de80c6
	if [ $? == 0 ]; then
de80c6
		echo "Kdump already running: [WARNING]"
de80c6
		return 0
de80c6
	fi
de80c6
de80c6
	if check_ssh_config; then
de80c6
		if ! check_ssh_target; then
de80c6
			echo "Starting kdump: [FAILED]"
de80c6
			return 1
de80c6
		fi
de80c6
	fi
de80c6
de80c6
	check_rebuild
de80c6
	if [ $? != 0 ]; then
de80c6
		echo "Starting kdump: [FAILED]"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	start_dump
de80c6
	if [ $? != 0 ]; then
de80c6
		echo "Starting kdump: [FAILED]"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	echo "Starting kdump: [OK]"
de80c6
}
de80c6
de80c6
stop_fadump()
de80c6
{
de80c6
	echo 0 > $FADUMP_REGISTER_SYS_NODE
de80c6
	if check_current_fadump_status; then
de80c6
		echo "fadump: failed to unregister"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	echo "fadump: unregistered successfully"
de80c6
	return 0
de80c6
}
de80c6
de80c6
stop_kdump()
de80c6
{
de80c6
	if is_secure_boot_enforced; then
de80c6
		$KEXEC -s -p -u
de80c6
	else
de80c6
		$KEXEC -p -u
de80c6
	fi
de80c6
de80c6
	if [ $? != 0 ]; then
de80c6
		echo "kexec: failed to unload kdump kernel"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	echo "kexec: unloaded kdump kernel"
de80c6
	return 0
de80c6
}
de80c6
de80c6
stop()
de80c6
{
de80c6
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
de80c6
		stop_fadump
de80c6
	else
de80c6
		stop_kdump
de80c6
	fi
de80c6
de80c6
	if [ $? != 0 ]; then
de80c6
		echo "Stopping kdump: [FAILED]"
de80c6
		return 1
de80c6
	fi
de80c6
de80c6
	echo "Stopping kdump: [OK]"
de80c6
	return 0
de80c6
}
de80c6
de80c6
if [ ! -f "$KDUMP_CONFIG_FILE" ]; then
de80c6
	echo "Error: No kdump config file found!"  >&2
de80c6
	exit 1
de80c6
fi
de80c6
de80c6
main ()
de80c6
{
de80c6
	# Determine if the dump mode is kdump or fadump
de80c6
	determine_dump_mode
de80c6
de80c6
	case "$1" in
de80c6
	  start)
de80c6
		if [ -s /proc/vmcore ]; then
de80c6
			save_core
de80c6
			reboot
de80c6
		else
de80c6
			start
de80c6
		fi
de80c6
		;;
de80c6
	  stop)
de80c6
		stop
de80c6
		;;
de80c6
	  status)
de80c6
		EXIT_CODE=0
de80c6
		check_current_status
de80c6
		case "$?" in
de80c6
		  0)
de80c6
			echo "Kdump is operational"
de80c6
			EXIT_CODE=0
de80c6
			;;
de80c6
		  1)
de80c6
			echo "Kdump is not operational"
de80c6
			EXIT_CODE=3
de80c6
			;;
de80c6
		esac
de80c6
		exit $EXIT_CODE
de80c6
		;;
de80c6
	  restart)
de80c6
		stop
de80c6
		start
de80c6
		;;
de80c6
	  condrestart)
de80c6
		;;
de80c6
	  propagate)
de80c6
		propagate_ssh_key
de80c6
		;;
de80c6
	  *)
de80c6
		echo $"Usage: $0 {start|stop|status|restart|propagate}"
de80c6
		exit 1
de80c6
	esac
de80c6
}
de80c6
de80c6
# Other kdumpctl instances will block in queue, until this one exits
de80c6
single_instance_lock
de80c6
de80c6
# To avoid fd 9 leaking, we invoke a subshell, close fd 9 and call main.
de80c6
# So that fd isn't leaking when main is invoking a subshell.
de80c6
(exec 9<&-; main $1)
de80c6
de80c6
exit $?