243
  #   ?ˆl–äY>”ÊC]Šq@½p!¸ÓjbÑ¿Ó„p"uÏÀÑ ?÷     ?#!/usr/bin/bash
KEXEC=/sbin/kexec

KDUMP_KERNELVER=""
KDUMP_KERNEL=""
KDUMP_COMMANDLINE=""
KEXEC_ARGS=""
MKDUMPRD="/sbin/mkdumprd -f"
MKFADUMPRD="/sbin/mkfadumprd"
DRACUT_MODULES_FILE="/usr/lib/dracut/modules.txt"
SAVE_PATH=/var/crash
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
DUMP_TARGET=""
DEFAULT_INITRD=""
DEFAULT_INITRD_BAK=""
INITRD_CHECKSUM_LOCATION=""
KDUMP_INITRD=""
TARGET_INITRD=""
#kdump shall be the default dump mode
DEFAULT_DUMP_MODE="kdump"
VMCORE_CREATION_STATUS="/var/lib/kdump/vmcore-creation.status"
image_time=0

standard_kexec_args="-p"

# Some default values in case /etc/sysconfig/kdump doesn't include
KDUMP_COMMANDLINE_REMOVE="hugepages hugepagesz slub_debug"

if [[ -f /etc/sysconfig/kdump ]]; then
	. /etc/sysconfig/kdump
fi

[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
. $dracutbasedir/dracut-functions.sh
. /lib/kdump/kdump-lib.sh
. /lib/kdump/kdump-logger.sh

#initiate the kdump logger
if ! dlog_init; then
	echo "failed to initiate the kdump logger."
	exit 1
fi

KDUMP_TMPDIR=$(mktemp --tmpdir -d kdump.XXXX)
TMPMNT="$KDUMP_TMPDIR/target"
trap '
    ret=$?;
    is_mounted $TMPMNT && umount -f $TMPMNT;
    rm -rf "$KDUMP_TMPDIR"
    [[ -d $LUKS_CONFIGFS ]] && find $LUKS_CONFIGFS/ -mindepth 1 -type d -delete
    exit $ret;
    ' EXIT

single_instance_lock()
{
	local rc timeout=5 lockfile

	if [[ -d /run/lock ]]; then
		lockfile=/run/lock/kdump
	else
		# when updating package using virt-customize, /run/lock doesn't exist
		lockfile=/tmp/kdump.lock
	fi

	if ! exec 9> $lockfile; then
		derror "Create file lock failed"
		exit 1
	fi

	flock -n 9
	rc=$?

	while [[ $rc -ne 0 ]]; do
		dinfo "Another app is currently holding the kdump lock; waiting for it to exit..."
		flock -w $timeout 9
		rc=$?
	done
}

determine_dump_mode()
{
	# Check if firmware-assisted dump is enabled
	# if yes, set the dump mode as fadump
	if is_fadump_capable; then
		dinfo "Dump mode is fadump"
		DEFAULT_DUMP_MODE="fadump"
	fi
	ddebug "DEFAULT_DUMP_MODE=$DEFAULT_DUMP_MODE"
}

save_core()
{
	coredir="/var/crash/$(date +"%Y-%m-%d-%H:%M")"

	mkdir -p "$coredir"
	ddebug "cp --sparse=always /proc/vmcore $coredir/vmcore-incomplete"
	if cp --sparse=always /proc/vmcore "$coredir/vmcore-incomplete"; then
		mv "$coredir/vmcore-incomplete" "$coredir/vmcore"
		dinfo "saved a vmcore to $coredir"
	else
		derror "failed to save a vmcore to $coredir"
	fi

	# pass the dmesg to Abrt tool if exists, in order
	# to collect the kernel oops message.
	# https://fedorahosted.org/abrt/
	if [[ -x /usr/bin/dumpoops ]]; then
		ddebug "makedumpfile --dump-dmesg $coredir/vmcore $coredir/dmesg"
		makedumpfile --dump-dmesg "$coredir/vmcore" "$coredir/dmesg" > /dev/null 2>&1
		ddebug "dumpoops -d $coredir/dmesg"
		if dumpoops -d "$coredir/dmesg" > /dev/null 2>&1; then
			dinfo "kernel oops has been collected by abrt tool"
		fi
	fi
}

rebuild_fadump_initrd()
{
	if ! $MKFADUMPRD "$DEFAULT_INITRD_BAK" "$TARGET_INITRD" --kver "$KDUMP_KERNELVER"; then
		derror "mkfadumprd: failed to make fadump initrd"
		return 1
	fi

	return 0
}

check_earlykdump_is_enabled()
{
	grep -q -w "rd.earlykdump" /proc/cmdline
}

rebuild_kdump_initrd()
{
	ddebug "rebuild kdump initrd: $MKDUMPRD $TARGET_INITRD $KDUMP_KERNELVER"
	if ! $MKDUMPRD "$TARGET_INITRD" "$KDUMP_KERNELVER"; then
		derror "mkdumprd: failed to make kdump initrd"
		return 1
	fi

	if check_earlykdump_is_enabled; then
		dwarn "Tips: If early kdump is enabled, also require rebuilding the system initramfs to make the changes take effect for early kdump."
	fi

	return 0
}

check_and_generate_vconsole_conf()
{
	if [[ ! -e /etc/vconsole.conf  ]]; then
		dwarn "/etc/vconsole.conf does not exist, trying to set keymap to us to reduce the initramfs size."
		if [[ -x "$(command -v localectl)" ]] ; then
			if localectl list-keymaps | grep -q "^us$"; then
				localectl set-keymap us 2>/dev/null
			fi
		fi
	fi
}

rebuild_initrd()
{
	local _ret

	if [[ ! -w $(dirname "$TARGET_INITRD") ]]; then
		derror "$(dirname "$TARGET_INITRD") does not have write permission. Cannot rebuild $TAR