Kairui Song 96a3fc
#!/bin/bash --norc
Kairui Song 96a3fc
# Generate an initramfs image that isolates dump capture capability within
Kairui Song 96a3fc
# the default initramfs using zz-fadumpinit dracut module.
Kairui Song 96a3fc
dcb59c
if [[ -f /etc/sysconfig/kdump ]]; then
Kairui Song 96a3fc
	. /etc/sysconfig/kdump
Kairui Song 96a3fc
fi
Kairui Song 96a3fc
Kairui Song 96a3fc
[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
Kairui Song 96a3fc
. $dracutbasedir/dracut-functions.sh
Kairui Song 96a3fc
. /lib/kdump/kdump-lib.sh
Kairui Song 96a3fc
. /lib/kdump/kdump-logger.sh
Kairui Song 96a3fc
Kairui Song 96a3fc
#initiate the kdump logger
Kairui Song 96a3fc
if ! dlog_init; then
Kairui Song 96a3fc
	echo "mkfadumprd: failed to initiate the kdump logger."
Kairui Song 96a3fc
	exit 1
Kairui Song 96a3fc
fi
Kairui Song 96a3fc
633084
MKFADUMPRD_TMPDIR="$(mktemp -d -t mkfadumprd.XXXXXX)"
Kairui Song 96a3fc
[ -d "$MKFADUMPRD_TMPDIR" ] || perror_exit "mkfadumprd: mktemp -d -t mkfadumprd.XXXXXX failed."
Kairui Song 96a3fc
trap '
Kairui Song 96a3fc
    ret=$?;
Kairui Song 96a3fc
    [[ -d $MKFADUMPRD_TMPDIR ]] && rm --one-file-system -rf -- "$MKFADUMPRD_TMPDIR";
Kairui Song 96a3fc
    exit $ret;
Kairui Song 96a3fc
    ' EXIT
Kairui Song 96a3fc
Kairui Song 96a3fc
# clean up after ourselves no matter how we die.
Kairui Song 96a3fc
trap 'exit 1;' SIGINT
Kairui Song 96a3fc
Kairui Song 96a3fc
MKDUMPRD="/sbin/mkdumprd -f"
Kairui Song 96a3fc
# Default boot initramfs to be rebuilt
Kairui Song 96a3fc
REBUILD_INITRD="$1" && shift
Kairui Song 96a3fc
TARGET_INITRD="$1" && shift
Kairui Song 96a3fc
FADUMP_INITRD="$MKFADUMPRD_TMPDIR/fadump.img"
Kairui Song 96a3fc
Kairui Song 96a3fc
### First build an initramfs with dump capture capability
Kairui Song 96a3fc
# this file tells the initrd is fadump enabled
Kairui Song 96a3fc
touch "$MKFADUMPRD_TMPDIR/fadump.initramfs"
Kairui Song 96a3fc
ddebug "rebuild fadump initrd: $FADUMP_INITRD $DEFAULT_INITRD $KDUMP_KERNELVER"
Lichen Liu 878faf
# Don't use squash for capture image or default image as it negatively impacts
Lichen Liu 878faf
# compression ratio and increases the size of the initramfs image.
Lichen Liu a74225
# Don't compress the capture image as uncompressed image is needed immediately.
Lichen Liu a74225
# Also, early microcode would not be needed here.
Lichen Liu a74225
if ! $MKDUMPRD "$FADUMP_INITRD" -i "$MKFADUMPRD_TMPDIR/fadump.initramfs" /etc/fadump.initramfs --omit squash --no-compress --no-early-microcode; then
Kairui Song 96a3fc
	perror_exit "mkfadumprd: failed to build image with dump capture support"
Kairui Song 96a3fc
fi
Kairui Song 96a3fc
Lichen Liu a74225
### Unpack the initramfs having dump capture capability retaining previous file modification time.
Lichen Liu a74225
# This helps in saving space by hardlinking identical files.
Kairui Song 96a3fc
mkdir -p "$MKFADUMPRD_TMPDIR/fadumproot"
Lichen Liu a74225
if ! cpio -id --preserve-modification-time --quiet -D "$MKFADUMPRD_TMPDIR/fadumproot" < "$FADUMP_INITRD"; then
Kairui Song 96a3fc
	derror "mkfadumprd: failed to unpack '$MKFADUMPRD_TMPDIR'"
Kairui Song 96a3fc
	exit 1
Kairui Song 96a3fc
fi
Kairui Song 96a3fc
Kairui Song 96a3fc
### Pack it into the normal boot initramfs with zz-fadumpinit module
b494b7
_dracut_isolate_args=(
b494b7
	--rebuild "$REBUILD_INITRD" --add zz-fadumpinit
caa240
	-i "$MKFADUMPRD_TMPDIR/fadumproot" /fadumproot
caa240
	-i "$MKFADUMPRD_TMPDIR/fadumproot/usr/lib/dracut/hostonly-kernel-modules.txt"
caa240
	/usr/lib/dracut/fadump-kernel-modules.txt
caa240
)
Kairui Song 8e51eb
Lichen Liu 77ca80
# Use zstd compression method, if available
96dc81
if ! have_compression_in_dracut_args; then
Lichen Liu 77ca80
	if is_zstd_command_available; then
a501dc
		_dracut_isolate_args+=(--compress zstd)
a501dc
	fi
96dc81
fi
96dc81
caa240
if ! dracut --force --quiet "${_dracut_isolate_args[@]}" "$@" "$TARGET_INITRD"; then
Kairui Song 96a3fc
	perror_exit "mkfadumprd: failed to setup '$TARGET_INITRD' with dump capture capability"
Kairui Song 96a3fc
fi