From 5b31b099ae9b40a8f832b07e8364d7b08025fdd6 Mon Sep 17 00:00:00 2001 From: Coiby Xu Date: Wed, 26 Apr 2023 04:48:25 +0800 Subject: [PATCH] Simplify the management of the kernel parameter crashkernel Currently, kexec-tools only updates the crashkernel to a new default value only when both two conditions are met, - auto_reset_crashkernel=yes in kdump.conf - existing kernels or current running kernel should use the old default value. To address seen corner cases, the logic to tell if the second condition is met becomes quite complex. Instead of making the logic more complex to support aarch64-64k, this patch drops the second condition to simplify the management of the crashkernel kernel parameter. Another change brought by this simplification is kexec-tools will also set up the kernel crashkernel parameter for a fresh install (previously it's limited to osbuild). Note 1. This patch also stop trying to update /etc/default/grub because a) it only affects the static file /boot/grub2/grub.cfg b) grubby is recommended to change the kernel command-line parameters for both Fedora [1] and RHEL9 [2][3] c) For the cases of aarch64 and POWER, different kernels could have different default crashkernel value. 2. Starting with Fedora 37, posttrans rpm scriplet distinguish between package install and upgrade. [1] https://fedoraproject.org/wiki/GRUB_2 [2] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/managing_monitoring_and_updating_the_kernel/configuring-kernel-command-line-parameters_managing-monitoring-and-updating-the-kernel#changing-kernel-command-line-parameters-for-all-boot-entries_configuring-kernel-command-line-parameters [3] https://access.redhat.com/solutions/1136173 Signed-off-by: Coiby Xu Reviewed-by: Philipp Rudo --- kdump.conf.5 | 5 ++- kdumpctl | 86 ++++++++++-------------------------------------- kexec-tools.spec | 31 +++-------------- 3 files changed, 25 insertions(+), 97 deletions(-) diff --git a/kdump.conf.5 b/kdump.conf.5 index e3e9900..ec28552 100644 --- a/kdump.conf.5 +++ b/kdump.conf.5 @@ -28,9 +28,8 @@ understand how this configuration file affects the behavior of kdump. .B auto_reset_crashkernel .RS -determine whether to reset kernel crashkernel to new default value -or not when kexec-tools updates the default crashkernel value and -existing kernels using the old default kernel crashkernel value +determine whether to reset kernel crashkernel parameter to the default value +or not when kexec-tools is updated or a new kernel is installed. .B raw .RS diff --git a/kdumpctl b/kdumpctl index 9c109da..57da1af 100755 --- a/kdumpctl +++ b/kdumpctl @@ -1675,72 +1675,37 @@ _is_bootloader_installed() fi } -# update the crashkernel value in GRUB_ETC_DEFAULT if necessary -# -# called by reset_crashkernel_after_update and inherit its array variable -# _crashkernel_vals -update_crashkernel_in_grub_etc_default_after_update() +_update_crashkernel() { - local _crashkernel _fadump_val - local _dump_mode _old_default_crashkernel _new_default_crashkernel + local _kernel _dump_mode _old_default_crashkernel _new_default_crashkernel _fadump_val _msg - if [[ $(uname -m) == s390x ]]; then - return - fi - - _crashkernel=$(_read_kernel_arg_in_grub_etc_default crashkernel) - - if [[ -z $_crashkernel ]]; then - return - fi - - _fadump_val=$(_read_kernel_arg_in_grub_etc_default fadump) - _dump_mode=$(get_dump_mode_by_fadump_val "$_fadump_val") - - _old_default_crashkernel=${_crashkernel_vals[old_${_dump_mode}]} - _new_default_crashkernel=${_crashkernel_vals[new_${_dump_mode}]} - - if [[ $_crashkernel == auto ]] || - [[ $_crashkernel == "$_old_default_crashkernel" && - $_new_default_crashkernel != "$_old_default_crashkernel" ]]; then - _update_kernel_arg_in_grub_etc_default crashkernel "$_new_default_crashkernel" + _kernel=$1 + _dump_mode=$(get_dump_mode_by_kernel "$_kernel") + _old_default_crashkernel=$(get_grub_kernel_boot_parameter "$_kernel" crashkernel) + _new_default_crashkernel=$(kdump_get_arch_recommend_crashkernel "$_dump_mode") + if [[ $_old_default_crashkernel != "$_new_default_crashkernel" ]]; then + _fadump_val=$(get_grub_kernel_boot_parameter "$_kernel" fadump) + if _update_kernel_cmdline "$_kernel" "$_new_default_crashkernel" "$_dump_mode" "$_fadump_val"; then + _msg="For kernel=$_kernel, crashkernel=$_new_default_crashkernel now. Please reboot the system for the change to take effet." + _msg+=" Note if you don't want kexec-tools to manage the crashkernel kernel parameter, please set auto_reset_crashkernel=no in /etc/kdump.conf." + dinfo "$_msg" + fi fi } + # shellcheck disable=SC2154 # false positive when dereferencing an array reset_crashkernel_after_update() { - local _kernel _crashkernel _dump_mode _fadump_val _old_default_crashkernel _new_default_crashkernel - declare -A _crashkernel_vals + local _kernel if ! _is_bootloader_installed; then return fi - _crashkernel_vals[old_kdump]=$(cat /tmp/old_default_crashkernel 2> /dev/null) - _crashkernel_vals[old_fadump]=$(cat /tmp/old_default_crashkernel_fadump 2> /dev/null) - _crashkernel_vals[new_kdump]=$(get_default_crashkernel kdump) - _crashkernel_vals[new_fadump]=$(get_default_crashkernel fadump) - for _kernel in $(_get_all_kernels_from_grubby ALL); do - _crashkernel=$(get_grub_kernel_boot_parameter "$_kernel" crashkernel) - if [[ $_crashkernel == auto ]]; then - reset_crashkernel "--kernel=$_kernel" - elif [[ -n $_crashkernel ]]; then - _dump_mode=$(get_dump_mode_by_kernel "$_kernel") - _old_default_crashkernel=${_crashkernel_vals[old_${_dump_mode}]} - _new_default_crashkernel=${_crashkernel_vals[new_${_dump_mode}]} - if [[ $_crashkernel == "$_old_default_crashkernel" ]] && - [[ $_new_default_crashkernel != "$_old_default_crashkernel" ]]; then - _fadump_val=$(get_grub_kernel_boot_parameter "$_kernel" fadump) - if _update_kernel_cmdline "$_kernel" "$_new_default_crashkernel" "$_dump_mode" "$_fadump_val"; then - echo "For kernel=$_kernel, crashkernel=$_new_default_crashkernel now." - fi - fi - fi + _update_crashkernel "$_kernel" done - - update_crashkernel_in_grub_etc_default_after_update } # read the value of an environ variable from given environ file path @@ -1764,8 +1729,7 @@ _is_osbuild() reset_crashkernel_for_installed_kernel() { - local _installed_kernel _running_kernel _crashkernel _crashkernel_running - local _dump_mode_running _fadump_val_running + local _installed_kernel # During package install, only try to reset crashkernel for osbuild # thus to avoid calling grubby when installing os via anaconda @@ -1784,21 +1748,7 @@ reset_crashkernel_for_installed_kernel() return fi - if ! _running_kernel=$(_get_current_running_kernel_path); then - ddebug "Couldn't find current running kernel" - exit - fi - - _crashkernel=$(get_grub_kernel_boot_parameter "$_installed_kernel" crashkernel) - _crashkernel_running=$(get_grub_kernel_boot_parameter "$_running_kernel" crashkernel) - _dump_mode_running=$(get_dump_mode_by_kernel "$_running_kernel") - _fadump_val_running=$(get_grub_kernel_boot_parameter "$_kernel" fadump) - - if [[ $_crashkernel != "$_crashkernel_running" ]]; then - if _update_kernel_cmdline "$_installed_kernel" "$_crashkernel_running" "$_dump_mode_running" "$_fadump_val_running"; then - echo "kexec-tools has reset $_installed_kernel to use the new default crashkernel value $_crashkernel_running" - fi - fi + _update_crashkernel "$_installed_kernel" } main() diff --git a/kexec-tools.spec b/kexec-tools.spec index ee06f87..64db9e2 100644 --- a/kexec-tools.spec +++ b/kexec-tools.spec @@ -260,21 +260,6 @@ chmod 755 $RPM_BUILD_ROOT/etc/kdump-adv-conf/kdump_dracut_modules/99zz-fadumpini mkdir -p $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/ mv $RPM_BUILD_ROOT/etc/kdump-adv-conf/kdump_dracut_modules/* $RPM_BUILD_ROOT/%{dracutlibdir}/modules.d/ -%pre -# Save the old default crashkernel values to /tmp/ when upgrading the package -# so kdumpctl later can tell if it should update the kernel crashkernel -# parameter in the posttrans scriptlet. Note this feauture of auto-updating -# the kernel crashkernel parameter currently doesn't support ostree, so skip it -# for ostree. -if [ ! -f /run/ostree-booted ] && [ $1 == 2 ] && grep -q get-default-crashkernel /usr/bin/kdumpctl; then - kdumpctl get-default-crashkernel kdump > /tmp/old_default_crashkernel 2>/dev/null -%ifarch ppc64 ppc64le - kdumpctl get-default-crashkernel fadump > /tmp/old_default_crashkernel_fadump 2>/dev/null -%endif -fi -# don't block package update -: - %post # Initial installation %systemd_post kdump.service @@ -341,21 +326,15 @@ do done %posttrans -# Try to reset kernel crashkernel value to new default value based on the old -# default value or set up crasherkernel value for osbuild +# Try to reset kernel crashkernel value to new default value or set up +# crasherkernel value for new install # # Note # 1. Skip ostree systems as they are not supported. -# 2. "[ $1 == 1 ]" in posttrans scriptlet means both install and upgrade. The -# former case is used to set up crashkernel for osbuild -if [ ! -f /run/ostree-booted ] && [ $1 == 1 ]; then +# 2. For Fedora 36 and RHEL9, "[ $1 == 1 ]" in posttrans scriptlet means both install and upgrade; +# For Fedora > 36, "[ $1 == 1 ]" only means install and "[ $1 == 2 ]" means upgrade +if [ ! -f /run/ostree-booted ] && [ $1 == 1 -o $1 == 2 ]; then kdumpctl _reset-crashkernel-after-update - rm /tmp/old_default_crashkernel 2>/dev/null -%ifarch ppc64 ppc64le - rm /tmp/old_default_crashkernel_fadump 2>/dev/null -%endif - # dnf would complain about the exit code not being 0. To keep it happy, - # always return 0 : fi