auto
  #   ˆl–äY>”ÊC]Šq@½p!¸ÓjbÑ¿Á„šßÀ¿ 0‚    #!/usr/bin/bash --norc
# New mkdumprd
#
# Copyright 2011 Red Hat, Inc.
#
# Written by Cong Wang <amwang@redhat.com>
#

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
export IN_KDUMP=1

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

SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
SAVE_PATH=$(get_save_path)
OVERRIDE_RESETTABLE=0

extra_modules=""
dracut_args=(--quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode strict --hostonly-nics '')

MKDUMPRD_TMPDIR="$(mktemp -d -t mkdumprd.XXXXXX)"
[ -d "$MKDUMPRD_TMPDIR" ] || perror_exit "dracut: mktemp -p -d -t dracut.XXXXXX failed."
MKDUMPRD_TMPMNT="$MKDUMPRD_TMPDIR/target"

trap '
    ret=$?;
    is_mounted $MKDUMPRD_TMPMNT && umount -f $MKDUMPRD_TMPMNT;
    [[ -d $MKDUMPRD_TMPDIR ]] && rm --one-file-system -rf -- "$MKDUMPRD_TMPDIR";
    exit $ret;
    ' EXIT

# clean up after ourselves no matter how we die.
trap 'exit 1;' SIGINT

add_dracut_arg()
{
	dracut_args+=("$@")
}

add_dracut_mount()
{
	add_dracut_arg "--mount" "$1"
}

add_dracut_sshkey()
{
	add_dracut_arg "--sshkey" "$1"
}

# caller should ensure $1 is valid and mounted in 1st kernel
to_mount()
{
	local _target=$1 _fstype=$2 _options=$3 _sed_cmd _new_mntpoint _pdev

	_new_mntpoint=$(get_kdump_mntpoint_from_target "$_target")
	_fstype="${_fstype:-$(get_fs_type_from_target "$_target")}"
	_options="${_options:-$(get_mntopt_from_target "$_target")}"
	_options="${_options:-defaults}"

	if [[ $_fstype == "nfs"* ]]; then
		_pdev=$_target
		_sed_cmd+='s/,\(mount\)\?addr=[^,]*//g;'
		_sed_cmd+='s/,\(mount\)\?proto=[^,]*//g;'
		_sed_cmd+='s/,clientaddr=[^,]*//;'
	else
		# for non-nfs _target converting to use udev persistent name
		_pdev="$(kdump_get_persistent_dev "$_target")"
		if [[ -z $_pdev ]]; then
			return 1
		fi
	fi

	# mount fs target as rw in 2nd kernel
	_sed_cmd+='s/\(^\|,\)ro\($\|,\)/\1rw\2/g;'
	# with 'noauto' in fstab nfs and non-root disk mount will fail in 2nd
	# kernel, filter it out here.
	_sed_cmd+='s/\(^\|,\)noauto\($\|,\)/\1/g;'
	# drop nofail or nobootwait
	_sed_cmd+='s/\(^\|,\)nofail\($\|,\)/\1/g;'
	_sed_cmd+='s/\(^\|,\)nobootwait\($\|,\)/\1/g;'

	_options=$(echo "$_options" | sed "$_sed_cmd")

	echo "$_pdev $_new_mntpoint $_fstype $_options"
}

#Function: get_ssh_size
#$1=dump target
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
get_ssh_size()
{
	local _out
	local _opt=("-i" "$SSH_KEY_LOCATION" "-o" "BatchMode=yes" "-o" "StrictHostKeyChecking=yes")

	if ! _out=$(ssh -q -n "${_opt[@]}" "$1" "df" "--output=avail" "$SAVE_PATH"); then
		perror_exit "checking remote ssh server available size failed."
	fi

	echo -n "$_out" | tail -1
}

#mkdir if save path does not exist on ssh dump target
#$1=ssh dump target
#caller should ensure write permission on $1:$SAVE_PATH
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
mkdir_save_path_ssh()
{
	local _opt _dir
	_opt=(-i "$SSH_KEY_LOCATION" -o BatchMode=yes -o StrictHostKeyChecking=yes)
	ssh -qn "${_opt[@]}" "$1" mkdir -p "$SAVE_PATH" &> /dev/null ||
		perror_exit "mkdir failed on $1:$SAVE_PATH"

	# check whether user has write permission on $1:$SAVE_PATH
	_dir=$(ssh -qn "${_opt[@]}" "$1" mktemp -dqp "$SAVE_PATH" 2> /dev/null) ||
		perror_exit "Could not create temporary directory on $1:$SAVE_PATH. Make sure user has write permission on destination"
	ssh -qn "${_opt[@]}" "$1" rmdir "$_dir"

	return 0
}

#Function: get_fs_size
#$1=dump target
get_fs_size()
{
	df --output=avail "$(get_mntpoint_from_target "$1")/$SAVE_PATH" | tail -1
}

#Function: get_raw_size
#$1=dump target
get_raw_size()
{
	fdisk -s "$1"
}

#Function: check_size
#$1: dump type string ('raw', 'fs', 'ssh')
#$2: dump target
check_size()
{
	local avail memtotal

	memtotal=$(awk '/MemTotal/{print $2}' /proc/meminfo)
	case "$1" in
	raw)
		avail=$(get_raw_size "