5cf148
#!/bin/bash --norc
5cf148
# Generate an initramfs image that isolates dump capture capability within
5cf148
# the default initramfs using zz-fadumpinit dracut module.
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
5cf148
#initiate the kdump logger
5cf148
if ! dlog_init; then
5cf148
	echo "mkfadumprd: failed to initiate the kdump logger."
5cf148
	exit 1
5cf148
fi
5cf148
5cf148
MKFADUMPRD_TMPDIR="$(mktemp -d -t mkfadumprd.XXXXXX)"
5cf148
[ -d "$MKFADUMPRD_TMPDIR" ] || perror_exit "mkfadumprd: mktemp -d -t mkfadumprd.XXXXXX failed."
5cf148
trap '
5cf148
    ret=$?;
5cf148
    [[ -d $MKFADUMPRD_TMPDIR ]] && rm --one-file-system -rf -- "$MKFADUMPRD_TMPDIR";
5cf148
    exit $ret;
5cf148
    ' EXIT
5cf148
5cf148
# clean up after ourselves no matter how we die.
5cf148
trap 'exit 1;' SIGINT
5cf148
5cf148
MKDUMPRD="/sbin/mkdumprd -f"
5cf148
# Default boot initramfs to be rebuilt
5cf148
REBUILD_INITRD="$1" && shift
5cf148
TARGET_INITRD="$1" && shift
5cf148
FADUMP_INITRD="$MKFADUMPRD_TMPDIR/fadump.img"
5cf148
5cf148
### First build an initramfs with dump capture capability
5cf148
# this file tells the initrd is fadump enabled
5cf148
touch "$MKFADUMPRD_TMPDIR/fadump.initramfs"
5cf148
ddebug "rebuild fadump initrd: $FADUMP_INITRD $DEFAULT_INITRD $KDUMP_KERNELVER"
5cf148
if ! $MKDUMPRD "$FADUMP_INITRD" -i "$MKFADUMPRD_TMPDIR/fadump.initramfs" /etc/fadump.initramfs; then
5cf148
	perror_exit "mkfadumprd: failed to build image with dump capture support"
5cf148
fi
5cf148
5cf148
### Unpack the initramfs having dump capture capability
5cf148
mkdir -p "$MKFADUMPRD_TMPDIR/fadumproot"
5cf148
if ! (pushd "$MKFADUMPRD_TMPDIR/fadumproot" > /dev/null && lsinitrd --unpack "$FADUMP_INITRD" &&
5cf148
	popd > /dev/null); then
5cf148
	derror "mkfadumprd: failed to unpack '$MKFADUMPRD_TMPDIR'"
5cf148
	exit 1
5cf148
fi
5cf148
5cf148
### Pack it into the normal boot initramfs with zz-fadumpinit module
5cf148
_dracut_isolate_args=(
5cf148
	--rebuild "$REBUILD_INITRD" --add zz-fadumpinit
5cf148
	-i "$MKFADUMPRD_TMPDIR/fadumproot" /fadumproot
5cf148
	-i "$MKFADUMPRD_TMPDIR/fadumproot/usr/lib/dracut/hostonly-kernel-modules.txt"
5cf148
	/usr/lib/dracut/fadump-kernel-modules.txt
5cf148
)
5cf148
5cf148
if is_squash_available; then
5cf148
	_dracut_isolate_args+=(--add squash)
5cf148
fi
5cf148
5cf148
# Same as setting zstd in mkdumprd
5cf148
if ! have_compression_in_dracut_args; then
5cf148
	if is_squash_available || is_zstd_command_available; then
5cf148
		_dracut_isolate_args+=(--compress zstd)
5cf148
	fi
5cf148
fi
5cf148
5cf148
if ! dracut --force --quiet "${_dracut_isolate_args[@]}" "$@" "$TARGET_INITRD"; then
5cf148
	perror_exit "mkfadumprd: failed to setup '$TARGET_INITRD' with dump capture capability"
5cf148
fi