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