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