Write to /var/lib/kdump if $KDUMP_BOOTDIR not writable

The `/boot` directory on some operating systems might be read-only.
If we cannot write to `$KDUMP_BOOTDIR` when generating the kdump
initrd, attempt to place the generated initrd at `/var/lib/kdump`
instead.

Signed-off by: Kelvin Fan <kelvinfan001@gmail.com>
Acked-by: Kairui Song <kasong@redhat.com>
This commit is contained in:
Kelvin Fan 2021-04-16 22:31:13 +00:00 committed by Kairui Song
parent eca7711706
commit 75bdcb7399
4 changed files with 31 additions and 13 deletions

View File

@ -2,17 +2,23 @@
COMMAND="$1"
KERNEL_VERSION="$2"
BOOT_DIR_ABS="$3"
KDUMP_INITRD_DIR_ABS="$3"
KERNEL_IMAGE="$4"
if ! [[ ${KERNEL_INSTALL_MACHINE_ID-x} ]]; then
exit 0
fi
if [[ -d "$BOOT_DIR_ABS" ]]; then
if [[ -d "$KDUMP_INITRD_DIR_ABS" ]]; then
KDUMP_INITRD="initrdkdump"
else
BOOT_DIR_ABS="/boot"
# If `KDUMP_BOOTDIR` is not writable, then the kdump
# initrd must have been placed at `/var/lib/kdump`
if [[ ! -w "/boot" ]]; then
KDUMP_INITRD_DIR_ABS="/var/lib/kdump"
else
KDUMP_INITRD_DIR_ABS="/boot"
fi
KDUMP_INITRD="initramfs-${KERNEL_VERSION}kdump.img"
fi
@ -23,7 +29,7 @@ case "$COMMAND" in
# and managed by kdump service
;;
remove)
rm -f -- "$BOOT_DIR_ABS/$KDUMP_INITRD"
rm -f -- "$KDUMP_INITRD_DIR_ABS/$KDUMP_INITRD"
ret=$?
;;
esac

View File

@ -733,20 +733,30 @@ prepare_kdump_bootinfo()
boot_initrdlist="initramfs-$KDUMP_KERNELVER.img initrd"
for initrd in $boot_initrdlist; do
if [ -f "$KDUMP_BOOTDIR/$initrd" ]; then
DEFAULT_INITRD="$KDUMP_BOOTDIR/$initrd"
defaut_initrd_base="$initrd"
DEFAULT_INITRD="$KDUMP_BOOTDIR/$defaut_initrd_base"
break
fi
done
# Get kdump initrd from default initrd filename
# Create kdump initrd basename from default initrd basename
# initramfs-5.7.9-200.fc32.x86_64.img => initramfs-5.7.9-200.fc32.x86_64kdump.img
# initrd => initrdkdump
if [[ -z "$DEFAULT_INITRD" ]]; then
KDUMP_INITRD=${KDUMP_BOOTDIR}/initramfs-${KDUMP_KERNELVER}kdump.img
elif [[ $(basename $DEFAULT_INITRD) == *.* ]]; then
KDUMP_INITRD=${DEFAULT_INITRD%.*}kdump.${DEFAULT_INITRD##*.}
if [[ -z "$defaut_initrd_base" ]]; then
kdump_initrd_base=initramfs-${KDUMP_KERNELVER}kdump.img
elif [[ $defaut_initrd_base == *.* ]]; then
kdump_initrd_base=${defaut_initrd_base%.*}kdump.${DEFAULT_INITRD##*.}
else
KDUMP_INITRD=${DEFAULT_INITRD}kdump
kdump_initrd_base=${defaut_initrd_base}kdump
fi
# Place kdump initrd in `/var/lib/kdump` if `KDUMP_BOOTDIR` not writable
if [[ ! -w "$KDUMP_BOOTDIR" ]];then
var_target_initrd_dir="/var/lib/kdump"
mkdir -p "$var_target_initrd_dir"
KDUMP_INITRD="$var_target_initrd_dir/$kdump_initrd_base"
else
KDUMP_INITRD="$KDUMP_BOOTDIR/$kdump_initrd_base"
fi
}

View File

@ -151,8 +151,8 @@ rebuild_kdump_initrd()
rebuild_initrd()
{
if [[ ! -w "$KDUMP_BOOTDIR" ]];then
derror "$KDUMP_BOOTDIR does not have write permission. Can not rebuild $TARGET_INITRD"
if [[ ! -w $(dirname $TARGET_INITRD) ]];then
derror "$(dirname $TARGET_INITRD) does not have write permission. Cannot rebuild $TARGET_INITRD"
return 1
fi

View File

@ -169,6 +169,7 @@ mkdir -p $RPM_BUILD_ROOT%{_unitdir}
mkdir -p -m755 $RPM_BUILD_ROOT%{_bindir}
mkdir -p -m755 $RPM_BUILD_ROOT%{_libdir}
mkdir -p -m755 $RPM_BUILD_ROOT%{_prefix}/lib/kdump
mkdir -p -m755 $RPM_BUILD_ROOT%{_sharedstatedir}/kdump
install -m 755 %{SOURCE1} $RPM_BUILD_ROOT%{_bindir}/kdumpctl
install -m 755 build/sbin/kexec $RPM_BUILD_ROOT/usr/sbin/kexec
@ -334,6 +335,7 @@ done
%dir %{_sysconfdir}/kdump
%dir %{_sysconfdir}/kdump/pre.d
%dir %{_sysconfdir}/kdump/post.d
%dir %{_sharedstatedir}/kdump
%{_mandir}/man8/kdumpctl.8.gz
%{_mandir}/man8/kexec.8.gz
%ifarch %{ix86} x86_64 ppc64 s390x ppc64le aarch64