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