#!/bin/bash

# kdumptool used be a swiss-army knife program with lots of subcommands.
# Now the only remaining functionality is "kdumptool calibrate" used by YaST 
# and possibly other tools to get crashkernel reservation info.
# So this is now a wrapper for the calibrate binary, carved out of the
# original kdumptool.


function usage()
{
	cat  >&2 <<-__END
	Usage: kdumptool [--configfile f] calibrate [-s | --shrink] [-d]
	Outputs possible and suggested memory reservation values.
	Options:
	    --configfile f	use f as alternative configfile
	    -d			turn on debugging
	    -s or --shrink	shrink the current reservation to the calculated value
	__END
	exit 1
}

if [[ "$1" == "--configfile" ]]; then
       	export KDUMP_CONF="$2"
	shift 2
fi

[[ "$1" == "calibrate" ]] || usage

# export all config values to the binary
set -a
. /usr/lib/kdump/kdump-read-config.sh
. /usr/lib/kdump/calibrate.conf

# find possible LUKS memory requirement
# and export it in KDUMP_LUKS_MEMORY
KDUMP_LUKS_MEMORY=0
if [[ "${KDUMP_PROTO}" == "file" ]]; then
	KDUMP_SAVEDIR_REALPATH=$(realpath -m "${KDUMP_SAVEDIR#*://}")
	mkdir -p "$KDUMP_SAVEDIR_REALPATH"
	MOUNT_SOURCE=$(findmnt -nvr -o SOURCE --target "${KDUMP_SAVEDIR_REALPATH}")


	# find which device is the encrypted device for MOUNT_SOURCE
	CRYPTO_SOURCE=""
	while read SOURCE FSTYPE; do
		[[ "${FSTYPE}" == crypto_LUKS ]] && CRYPTO_SOURCE="${SOURCE}"
	done < <(lsblk -n -l -s -o PATH,FSTYPE "${MOUNT_SOURCE}")

	if [[ -n "${CRYPTO_SOURCE}" ]]; then
		while read KEY VALUE; do
			[[ "$KEY" == "Memory:" ]] && KDUMP_LUKS_MEMORY=$((KDUMP_LUKS_MEMORY + VALUE))
		done < <(cryptsetup luksDump "${CRYPTO_SOURCE}")
	fi
fi

# skip over the "calibrate" argument and pass the rest to the binary
shift
/usr/lib/kdump/calibrate "$@"
RET=$?
# exit code 2 means bad arguments
[[ $RET -eq 2 ]] && usage
exit $RET




