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=""
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
{
ab224c
	exec 9>/var/lock/kdump
ab224c
	flock 9
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.
ab224c
function 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
#
765b01
function 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
#
765b01
function append_cmdline()
765b01
{
765b01
    local cmdline=$1
765b01
    local newstr=${cmdline/$2/""}
765b01
765b01
    # unchanged str implies argument wasn't there
765b01
    if [ "$cmdline" == "$newstr" ]; then
765b01
	cmdline="${cmdline} ${2}=${3}"
765b01
    fi
765b01
765b01
    echo $cmdline
765b01
}
765b01
765b01
# This function performs a series of edits on the command line
765b01
function 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
765b01
	cmdline=`remove_cmdline_param "$cmdline" crashkernel hugepages hugepagesz`
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
ab224c
function 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
ab224c
function rebuild_initrd()
ab224c
{
ab224c
	$MKDUMPRD $kdump_initrd $kdump_kver
ab224c
	if [ $? != 0 ]; then
ab224c
		echo "mkdumprd: failed to make kdump initrd" >&2
ab224c
		return 1
ab224c
	fi
ab224c
}
ab224c
ab224c
#$1: the files to be checked with IFS=' '
ab224c
function 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=' '
ab224c
function 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
ab224c
function 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
ab224c
               # remove inline comments after the end of a directive.                                                                      
ab224c
               config_val=$(strip_comments $config_val)
ab224c
		case "$config_opt" in
ab224c
		\#* | "")
ab224c
			;;
ab224c
		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)
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
ab224c
	return 0
ab224c
}
ab224c
765b01
# check_fence_kdump <image timestamp>
765b01
# return 0 if fence_kdump is configured and kdump initrd needs to be rebuilt
765b01
function check_fence_kdump()
765b01
{
765b01
	local image_time=$1
765b01
	local cib_time
765b01
765b01
	is_fence_kdump || return 1
765b01
765b01
	cib_time=`pcs cluster cib | xmllint --xpath 'string(/cib/@cib-last-written)' - | \
765b01
		  xargs -0 date +%s --date`
765b01
765b01
	if [ -z $cib_time -o $cib_time -le $image_time ]; then
765b01
		return 1
765b01
	fi
765b01
765b01
	return 0
765b01
}
765b01
ab224c
function check_rebuild()
ab224c
{
ab224c
	local extra_modules modified_files=""
ab224c
	local _force_rebuild force_rebuild="0"
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}"
ab224c
	kdump_initrd="${KDUMP_BOOTDIR}/initramfs-${kdump_kver}kdump.img"
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
ab224c
	if [ -f $kdump_initrd ]; then
ab224c
		image_time=`stat -c "%Y" $kdump_initrd 2>/dev/null`
ab224c
	else
ab224c
		image_time=0
ab224c
	fi
ab224c
765b01
	#also rebuild when cluster conf is changed and fence kdump is enabled.
765b01
	check_fence_kdump $image_time && modified_files="cluster-cib"
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"
ab224c
	files="$KDUMP_CONFIG_FILE $kdump_kernel $EXTRA_BINS"
ab224c
765b01
	if [ -f $FENCE_KDUMP_CONFIG ]; then
765b01
		files="$files $FENCE_KDUMP_CONFIG"
765b01
	fi
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
ab224c
	if [ $image_time -eq 0 ]; then
ab224c
		echo  -n "No kdump initial ramdisk found."; echo
ab224c
	elif [ "$force_rebuild" != "0" ]; then
ab224c
		echo -n "Force rebuild $kdump_initrd"; echo
ab224c
	elif [ -n "$modified_files" ]; then
ab224c
		echo "Detected change(s) the following file(s):"
ab224c
		echo -n "  "; echo "$modified_files" | sed 's/\s/\n  /g'
ab224c
	else
ab224c
		return 0
ab224c
	fi
ab224c
ab224c
	echo "Rebuilding $kdump_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
ab224c
function need_64bit_headers()
ab224c
{
ab224c
    return `tail -n 1 /proc/iomem | awk '{ split ($1, r, "-"); \
ab224c
    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.
ab224c
function 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
ab224c
	$KEXEC $KEXEC_ARGS $standard_kexec_args \
ab224c
		--command-line="$KDUMP_COMMANDLINE" \
ab224c
		--initrd=$kdump_initrd $kdump_kernel 2>/dev/null
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
ab224c
function check_ssh_config()
ab224c
{
ab224c
	while read config_opt config_val; do
ab224c
               # remove inline comments after the end of a directive.
ab224c
               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
ab224c
function 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
ab224c
function 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/'`
ab224c
 
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
}
ab224c
765b01
function 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
ab224c
function 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
ab224c
get_save_path() {
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
ab224c
is_dump_target_configured() {
ab224c
    local _target
ab224c
ab224c
    _target=$(egrep "^ext[234]|^xfs|^btrfs|^minix|^raw|^ssh|^nfs" /etc/kdump.conf)
ab224c
ab224c
     [ -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
ab224c
path_to_be_relabeled() {
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.
765b01
function 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)
765b01
function 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
765b01
function check_kdump_feasibility()
765b01
{
765b01
	if is_secure_boot_enforced; then
765b01
		echo "Secure Boot is Enabled. Kdump service can't be started. Disable Secure Boot and retry"
765b01
		return 1;
765b01
	elif is_secure_mode_enforced; then
765b01
		echo "securelevel is set to 1 (Secure Mode). Kdump service can't be started."
765b01
		return 1
765b01
	fi
765b01
765b01
	if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
765b01
		echo "Kdump is not supported on this kernel"
765b01
		return 1
765b01
	fi
765b01
}
765b01
ab224c
function 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
765b01
	check_kdump_feasibility
765b01
	if [ $? -ne 0 ]; then
765b01
		echo "Starting kdump: [FAILED]"
765b01
		return 1
765b01
	fi
765b01
765b01
	check_current_kdump_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
ab224c
	load_kdump
ab224c
	if [ $? != 0 ]; then
ab224c
		echo "Starting kdump: [FAILED]"
ab224c
		return 1
ab224c
	fi
ab224c
ab224c
	echo "Starting kdump: [OK]"
ab224c
}
ab224c
ab224c
function stop()
ab224c
{
ab224c
	$KEXEC -p -u 2>/dev/null
ab224c
	if [ $? == 0 ]; then
ab224c
		echo "kexec: unloaded kdump kernel"
ab224c
		echo "Stopping kdump: [OK]"
ab224c
		return 0
ab224c
	else
ab224c
		echo "kexec: failed to unloaded kdump kernel"
ab224c
		echo "Stopping kdump: [FAILED]"
ab224c
		return 1
ab224c
	fi
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
{
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
765b01
		check_current_kdump_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
		;;
765b01
	  *)
765b01
		echo $"Usage: $0 {start|stop|status|restart|propagate}"
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 $?