5cf148
#!/bin/bash --norc
5cf148
# New mkdumprd
5cf148
#
5cf148
# Copyright 2011 Red Hat, Inc.
5cf148
#
5cf148
# Written by Cong Wang <amwang@redhat.com>
5cf148
#
5cf148
5cf148
if [[ -f /etc/sysconfig/kdump ]]; then
5cf148
	. /etc/sysconfig/kdump
5cf148
fi
5cf148
5cf148
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
5cf148
. $dracutbasedir/dracut-functions.sh
5cf148
. /lib/kdump/kdump-lib.sh
5cf148
. /lib/kdump/kdump-logger.sh
5cf148
export IN_KDUMP=1
5cf148
5cf148
#initiate the kdump logger
5cf148
if ! dlog_init; then
5cf148
	echo "failed to initiate the kdump logger."
5cf148
	exit 1
5cf148
fi
5cf148
5cf148
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
5cf148
SAVE_PATH=$(get_save_path)
5cf148
OVERRIDE_RESETTABLE=0
5cf148
5cf148
extra_modules=""
5cf148
dracut_args=(--add kdumpbase --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode strict -o "plymouth resume ifcfg earlykdump")
5cf148
5cf148
MKDUMPRD_TMPDIR="$(mktemp -d -t mkdumprd.XXXXXX)"
5cf148
[ -d "$MKDUMPRD_TMPDIR" ] || perror_exit "dracut: mktemp -p -d -t dracut.XXXXXX failed."
5cf148
MKDUMPRD_TMPMNT="$MKDUMPRD_TMPDIR/target"
5cf148
5cf148
trap '
5cf148
    ret=$?;
5cf148
    is_mounted $MKDUMPRD_TMPMNT && umount -f $MKDUMPRD_TMPMNT;
5cf148
    [[ -d $MKDUMPRD_TMPDIR ]] && rm --one-file-system -rf -- "$MKDUMPRD_TMPDIR";
5cf148
    exit $ret;
5cf148
    ' EXIT
5cf148
5cf148
# clean up after ourselves no matter how we die.
5cf148
trap 'exit 1;' SIGINT
5cf148
5cf148
add_dracut_arg()
5cf148
{
5cf148
	dracut_args+=("$@")
5cf148
}
5cf148
5cf148
add_dracut_mount()
5cf148
{
5cf148
	add_dracut_arg "--mount" "$1"
5cf148
}
5cf148
5cf148
add_dracut_sshkey()
5cf148
{
5cf148
	add_dracut_arg "--sshkey" "$1"
5cf148
}
5cf148
5cf148
# caller should ensure $1 is valid and mounted in 1st kernel
5cf148
to_mount()
5cf148
{
5cf148
	local _target=$1 _fstype=$2 _options=$3 _sed_cmd _new_mntpoint _pdev
5cf148
5cf148
	_new_mntpoint=$(get_kdump_mntpoint_from_target "$_target")
5cf148
	_fstype="${_fstype:-$(get_fs_type_from_target "$_target")}"
5cf148
	_options="${_options:-$(get_mntopt_from_target "$_target")}"
5cf148
	_options="${_options:-defaults}"
5cf148
5cf148
	if [[ $_fstype == "nfs"* ]]; then
5cf148
		_pdev=$_target
5cf148
		_sed_cmd+='s/,\(mount\)\?addr=[^,]*//g;'
5cf148
		_sed_cmd+='s/,\(mount\)\?proto=[^,]*//g;'
5cf148
		_sed_cmd+='s/,clientaddr=[^,]*//;'
5cf148
	else
5cf148
		# for non-nfs _target converting to use udev persistent name
5cf148
		_pdev="$(kdump_get_persistent_dev "$_target")"
5cf148
		if [[ -z $_pdev ]]; then
5cf148
			return 1
5cf148
		fi
5cf148
	fi
5cf148
5cf148
	# mount fs target as rw in 2nd kernel
5cf148
	_sed_cmd+='s/\(^\|,\)ro\($\|,\)/\1rw\2/g;'
5cf148
	# with 'noauto' in fstab nfs and non-root disk mount will fail in 2nd
5cf148
	# kernel, filter it out here.
5cf148
	_sed_cmd+='s/\(^\|,\)noauto\($\|,\)/\1/g;'
5cf148
	# drop nofail or nobootwait
5cf148
	_sed_cmd+='s/\(^\|,\)nofail\($\|,\)/\1/g;'
5cf148
	_sed_cmd+='s/\(^\|,\)nobootwait\($\|,\)/\1/g;'
5cf148
5cf148
	_options=$(echo "$_options" | sed "$_sed_cmd")
5cf148
5cf148
	echo "$_pdev $_new_mntpoint $_fstype $_options"
5cf148
}
5cf148
5cf148
#Function: get_ssh_size
5cf148
#$1=dump target
5cf148
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
5cf148
get_ssh_size()
5cf148
{
5cf148
	local _out
5cf148
	local _opt=("-i" "$SSH_KEY_LOCATION" "-o" "BatchMode=yes" "-o" "StrictHostKeyChecking=yes")
5cf148
5cf148
	if ! _out=$(ssh -q -n "${_opt[@]}" "$1" "df" "--output=avail" "$SAVE_PATH"); then
5cf148
		perror_exit "checking remote ssh server available size failed."
5cf148
	fi
5cf148
5cf148
	echo -n "$_out" | tail -1
5cf148
}
5cf148
5cf148
#mkdir if save path does not exist on ssh dump target
5cf148
#$1=ssh dump target
5cf148
#caller should ensure write permission on $1:$SAVE_PATH
5cf148
#called from while loop and shouldn't read from stdin, so we're using "ssh -n"
5cf148
mkdir_save_path_ssh()
5cf148
{
5cf148
	local _opt _dir
5cf148
	_opt=(-i "$SSH_KEY_LOCATION" -o BatchMode=yes -o StrictHostKeyChecking=yes)
5cf148
	ssh -qn "${_opt[@]}" "$1" mkdir -p "$SAVE_PATH" &> /dev/null ||
5cf148
		perror_exit "mkdir failed on $1:$SAVE_PATH"
5cf148
5cf148
	# check whether user has write permission on $1:$SAVE_PATH
5cf148
	_dir=$(ssh -qn "${_opt[@]}" "$1" mktemp -dqp "$SAVE_PATH" 2> /dev/null) ||
5cf148
		perror_exit "Could not create temporary directory on $1:$SAVE_PATH. Make sure user has write permission on destination"
5cf148
	ssh -qn "${_opt[@]}" "$1" rmdir "$_dir"
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
#Function: get_fs_size
5cf148
#$1=dump target
5cf148
get_fs_size()
5cf148
{
5cf148
	df --output=avail "$(get_mntpoint_from_target "$1")/$SAVE_PATH" | tail -1
5cf148
}
5cf148
5cf148
#Function: get_raw_size
5cf148
#$1=dump target
5cf148
get_raw_size()
5cf148
{
5cf148
	fdisk -s "$1"
5cf148
}
5cf148
5cf148
#Function: check_size
5cf148
#$1: dump type string ('raw', 'fs', 'ssh')
5cf148
#$2: dump target
5cf148
check_size()
5cf148
{
5cf148
	local avail memtotal
5cf148
5cf148
	memtotal=$(awk '/MemTotal/{print $2}' /proc/meminfo)
5cf148
	case "$1" in
5cf148
	raw)
5cf148
		avail=$(get_raw_size "$2")
5cf148
		;;
5cf148
	ssh)
5cf148
		avail=$(get_ssh_size "$2")
5cf148
		;;
5cf148
	fs)
5cf148
		avail=$(get_fs_size "$2")
5cf148
		;;
5cf148
	*)
5cf148
		return
5cf148
		;;
5cf148
	esac || perror_exit "Check dump target size failed"
5cf148
5cf148
	if [[ $avail -lt $memtotal ]]; then
5cf148
		dwarn "Warning: There might not be enough space to save a vmcore."
5cf148
		dwarn "         The size of $2 should be greater than $memtotal kilo bytes."
5cf148
	fi
5cf148
}
5cf148
5cf148
check_save_path_fs()
5cf148
{
5cf148
	local _path=$1
5cf148
5cf148
	if [[ ! -d $_path ]]; then
5cf148
		perror_exit "Dump path $_path does not exist."
5cf148
	fi
5cf148
}
5cf148
5cf148
mount_failure()
5cf148
{
5cf148
	local _target=$1
5cf148
	local _mnt=$2
5cf148
	local _fstype=$3
5cf148
	local msg="Failed to mount $_target"
5cf148
5cf148
	if [[ -n $_mnt ]]; then
5cf148
		msg="$msg on $_mnt"
5cf148
	fi
5cf148
5cf148
	msg="$msg for kdump preflight check."
5cf148
5cf148
	if [[ $_fstype == "nfs" ]]; then
5cf148
		msg="$msg Please make sure nfs-utils has been installed."
5cf148
	fi
5cf148
5cf148
	perror_exit "$msg"
5cf148
}
5cf148
5cf148
check_user_configured_target()
5cf148
{
5cf148
	local _target=$1 _cfg_fs_type=$2 _mounted
5cf148
	local _mnt _opt _fstype
5cf148
5cf148
	_mnt=$(get_mntpoint_from_target "$_target")
5cf148
	_opt=$(get_mntopt_from_target "$_target")
5cf148
	_fstype=$(get_fs_type_from_target "$_target")
5cf148
5cf148
	if [[ -n $_fstype ]]; then
5cf148
		# In case of nfs4, nfs should be used instead, nfs* options is deprecated in kdump.conf
5cf148
		[[ $_fstype == "nfs"* ]] && _fstype=nfs
5cf148
5cf148
		if [[ -n $_cfg_fs_type ]] && [[ $_fstype != "$_cfg_fs_type" ]]; then
5cf148
			perror_exit "\"$_target\" have a wrong type config \"$_cfg_fs_type\", expected \"$_fstype\""
5cf148
		fi
5cf148
	else
5cf148
		_fstype="$_cfg_fs_type"
5cf148
		_fstype="$_cfg_fs_type"
5cf148
	fi
5cf148
5cf148
	# For noauto mount, mount it inplace with default value.
5cf148
	# Else use the temporary target directory
5cf148
	if [[ -n $_mnt ]]; then
5cf148
		if ! is_mounted "$_mnt"; then
5cf148
			if [[ $_opt == *",noauto"* ]]; then
5cf148
				mount "$_mnt" || mount_failure "$_target" "$_mnt" "$_fstype"
5cf148
				_mounted=$_mnt
5cf148
			else
5cf148
				perror_exit "Dump target \"$_target\" is neither mounted nor configured as \"noauto\""
5cf148
			fi
5cf148
		fi
5cf148
	else
5cf148
		_mnt=$MKDUMPRD_TMPMNT
5cf148
		mkdir -p "$_mnt"
5cf148
		mount "$_target" "$_mnt" -t "$_fstype" -o defaults || mount_failure "$_target" "" "$_fstype"
5cf148
		_mounted=$_mnt
5cf148
	fi
5cf148
5cf148
	# For user configured target, use $SAVE_PATH as the dump path within the target
5cf148
	if [[ ! -d "$_mnt/$SAVE_PATH" ]]; then
5cf148
		perror_exit "Dump path \"$_mnt/$SAVE_PATH\" does not exist in dump target \"$_target\""
5cf148
	fi
5cf148
5cf148
	check_size fs "$_target"
5cf148
5cf148
	# Unmount it early, if function is interrupted and didn't reach here, the shell trap will clear it up anyway
5cf148
	if [[ -n $_mounted ]]; then
5cf148
		umount -f -- "$_mounted"
5cf148
	fi
5cf148
}
5cf148
5cf148
# $1: core_collector config value
5cf148
verify_core_collector()
5cf148
{
5cf148
	local _cmd="${1%% *}"
5cf148
	local _params="${1#* }"
5cf148
5cf148
	if [[ $_cmd != "makedumpfile" ]]; then
5cf148
		if is_raw_dump_target; then
5cf148
			dwarn "Warning: specifying a non-makedumpfile core collector, you will have to recover the vmcore manually."
5cf148
		fi
5cf148
		return
5cf148
	fi
5cf148
5cf148
	if is_ssh_dump_target || is_raw_dump_target; then
5cf148
		if ! strstr "$_params" "-F"; then
5cf148
			perror_exit 'The specified dump target needs makedumpfile "-F" option.'
5cf148
		fi
5cf148
		_params="$_params vmcore"
5cf148
	else
5cf148
		_params="$_params vmcore dumpfile"
5cf148
	fi
5cf148
5cf148
	# shellcheck disable=SC2086
5cf148
	if ! $_cmd --check-params $_params; then
5cf148
		perror_exit "makedumpfile parameter check failed."
5cf148
	fi
5cf148
}
5cf148
5cf148
add_mount()
5cf148
{
5cf148
	local _mnt
5cf148
5cf148
	_mnt=$(to_mount "$@") || exit 1
5cf148
5cf148
	add_dracut_mount "$_mnt"
5cf148
}
5cf148
5cf148
#handle the case user does not specify the dump target explicitly
5cf148
handle_default_dump_target()
5cf148
{
5cf148
	local _target
5cf148
	local _mntpoint
5cf148
5cf148
	is_user_configured_dump_target && return
5cf148
5cf148
	check_save_path_fs "$SAVE_PATH"
5cf148
5cf148
	_save_path=$(get_bind_mount_source "$SAVE_PATH")
5cf148
	_target=$(get_target_from_path "$_save_path")
5cf148
	_mntpoint=$(get_mntpoint_from_target "$_target")
5cf148
5cf148
	SAVE_PATH=${_save_path##"$_mntpoint"}
5cf148
	add_mount "$_target"
5cf148
	check_size fs "$_target"
5cf148
}
5cf148
5cf148
# $1: function name
5cf148
for_each_block_target()
5cf148
{
5cf148
	local dev majmin
5cf148
5cf148
	for dev in $(get_kdump_targets); do
5cf148
		[[ -b $dev ]] || continue
5cf148
		majmin=$(get_maj_min "$dev")
5cf148
		check_block_and_slaves "$1" "$majmin" && return 1
5cf148
	done
5cf148
5cf148
	return 0
5cf148
}
5cf148
5cf148
#judge if a specific device with $1 is unresettable
5cf148
#return false if unresettable.
5cf148
is_unresettable()
5cf148
{
5cf148
	local path device resettable=1
5cf148
5cf148
	path="/sys/$(udevadm info --query=all --path="/sys/dev/block/$1" | awk '/^P:/ {print $2}' | sed -e 's/\(cciss[0-9]\+\/\).*/\1/g' -e 's/\/block\/.*$//')/resettable"
5cf148
	if [[ -f $path ]]; then
5cf148
		resettable="$(< "$path")"
5cf148
		[[ $resettable -eq 0 ]] && [[ $OVERRIDE_RESETTABLE -eq 0 ]] && {
5cf148
			device=$(udevadm info --query=all --path="/sys/dev/block/$1" | awk -F= '/DEVNAME/{print $2}')
5cf148
			derror "Error: Can not save vmcore because device $device is unresettable"
5cf148
			return 0
5cf148
		}
5cf148
	fi
5cf148
5cf148
	return 1
5cf148
}
5cf148
5cf148
#check if machine is resettable.
5cf148
#return true if resettable
5cf148
check_resettable()
5cf148
{
5cf148
	local _target _override_resettable
5cf148
5cf148
	_override_resettable=$(kdump_get_conf_val override_resettable)
5cf148
	OVERRIDE_RESETTABLE=${_override_resettable:-$OVERRIDE_RESETTABLE}
5cf148
	if [ "$OVERRIDE_RESETTABLE" != "0" ] && [ "$OVERRIDE_RESETTABLE" != "1" ]; then
5cf148
		perror_exit "override_resettable value '$OVERRIDE_RESETTABLE' is invalid"
5cf148
	fi
5cf148
5cf148
	for_each_block_target is_unresettable && return
5cf148
5cf148
	return 1
5cf148
}
5cf148
5cf148
check_crypt()
5cf148
{
5cf148
	local _dev
5cf148
5cf148
	for _dev in $(get_kdump_targets); do
5cf148
		if [[ -n $(get_luks_crypt_dev "$(get_maj_min "$_dev")") ]]; then
5cf148
			derror "Device $_dev is encrypted." && return 1
5cf148
		fi
5cf148
	done
5cf148
}
5cf148
5cf148
if ! check_resettable; then
5cf148
	exit 1
5cf148
fi
5cf148
5cf148
if ! check_crypt; then
5cf148
	dwarn "Warning: Encrypted device is in dump path, which is not recommended, see kexec-kdump-howto.txt for more details."
5cf148
fi
5cf148
5cf148
# firstly get right SSH_KEY_LOCATION
5cf148
keyfile=$(kdump_get_conf_val sshkey)
5cf148
if [[ -f $keyfile ]]; then
5cf148
	# canonicalize the path
5cf148
	SSH_KEY_LOCATION=$(/usr/bin/readlink -m "$keyfile")
5cf148
fi
5cf148
5cf148
while read -r config_opt config_val; do
5cf148
	# remove inline comments after the end of a directive.
5cf148
	case "$config_opt" in
5cf148
	extra_modules)
5cf148
		extra_modules="$extra_modules $config_val"
5cf148
		;;
5cf148
	ext[234] | xfs | btrfs | minix | nfs)
5cf148
		check_user_configured_target "$config_val" "$config_opt"
5cf148
		add_mount "$config_val" "$config_opt"
5cf148
		;;
5cf148
	raw)
5cf148
		# checking raw disk writable
5cf148
		dd if="$config_val" count=1 of=/dev/null > /dev/null 2>&1 || {
5cf148
			perror_exit "Bad raw disk $config_val"
5cf148
		}
5cf148
		_praw=$(persistent_policy="by-id" kdump_get_persistent_dev "$config_val")
5cf148
		if [[ -z $_praw ]]; then
5cf148
			exit 1
5cf148
		fi
5cf148
		add_dracut_arg "--device" "$_praw"
5cf148
		check_size raw "$config_val"
5cf148
		;;
5cf148
	ssh)
5cf148
		if strstr "$config_val" "@"; then
5cf148
			mkdir_save_path_ssh "$config_val"
5cf148
			check_size ssh "$config_val"
5cf148
			add_dracut_sshkey "$SSH_KEY_LOCATION"
5cf148
		else
5cf148
			perror_exit "Bad ssh dump target $config_val"
5cf148
		fi
5cf148
		;;
5cf148
	core_collector)
5cf148
		verify_core_collector "$config_val"
5cf148
		;;
5cf148
	dracut_args)
5cf148
		while read -r dracut_arg; do
5cf148
			add_dracut_arg "$dracut_arg"
5cf148
		done <<< "$(echo "$config_val" | xargs -n 1 echo)"
5cf148
		;;
5cf148
	*) ;;
5cf148
5cf148
	esac
5cf148
done <<< "$(kdump_read_conf)"
5cf148
5cf148
handle_default_dump_target
5cf148
5cf148
if ! have_compression_in_dracut_args; then
5cf148
	# Here zstd is set as the default compression method. If squash module
5cf148
	# is available for dracut, libzstd will be used by mksquashfs. If
5cf148
	# squash module is unavailable, command zstd will be used instead.
5cf148
	if is_squash_available || is_zstd_command_available; then
5cf148
		add_dracut_arg "--compress" "zstd"
5cf148
	fi
5cf148
fi
5cf148
5cf148
if [[ -n $extra_modules ]]; then
5cf148
	add_dracut_arg "--add-drivers" "$extra_modules"
5cf148
fi
5cf148
5cf148
# TODO: The below check is not needed anymore with the introduction of
5cf148
#       'zz-fadumpinit' module, that isolates fadump's capture kernel initrd,
5cf148
#       but still sysroot.mount unit gets generated based on 'root=' kernel
5cf148
#       parameter available in fadump case. So, find a way to fix that first
5cf148
#       before removing this check.
5cf148
if ! is_fadump_capable; then
5cf148
	# The 2nd rootfs mount stays behind the normal dump target mount,
5cf148
	# so it doesn't affect the logic of check_dump_fs_modified().
5cf148
	is_dump_to_rootfs && add_mount "$(to_dev_name "$(get_root_fs_device)")"
5cf148
5cf148
	add_dracut_arg "--no-hostonly-default-device"
5cf148
fi
5cf148
5cf148
# This is RHEL-only to work around nvme problem, then real fix should go to dracut
5cf148
if [[ -d /sys/module/nvme ]]; then
5cf148
	add_dracut_arg "--add-drivers" "nvme"
5cf148
fi
5cf148
5cf148
dracut "${dracut_args[@]}" "$@"
5cf148
5cf148
_rc=$?
5cf148
sync
5cf148
exit $_rc