From 945cbbd59b34739c4a6b2f228c33f76e2981a18a Mon Sep 17 00:00:00 2001 From: Coiby Xu Date: Tue, 7 Dec 2021 15:16:07 +0800 Subject: [PATCH] add helper functions to get kernel path by kernel release and the path of current running kernel grubby --info=kernel-path or --add-kernel=kernel-path accepts a kernel path (e.g. /boot/vmlinuz-5.14.14-200.fc34.x86_64) instead of kernel release (e.g 5.14.14-200.fc34.x86_64). So we need to know the kernel path given a kernel release. Although for Fedora/RHEL, the kernel path is "/boot/vmlinuz-", a path kernel could also be /boot///vmlinuz. So the most reliable way to find the kernel path given a kernel release is to use "grubby --info". For osbuild, a kernel path may not yet exist but it's valid for "grubby --update-kernel=KERNEL_PATH". For example, "grubby -info" may output something as follows, index=0 kernel="/var/cache/osbuild-worker/osbuild-store/tmp/tmp2prywdy5object/tree/boot/vmlinuz-5.15.10-100.fc34.x86_64" args="ro no_timer_check net.ifnames=0 console=tty1 console=ttyS0,115200n8" root="UUID=76a22bf4-f153-4541-b6c7-0332c0dfaeac" initrd="/var/cache/osbuild-worker/osbuild-store/tmp/tmp2prywdy5object/tree/boot/initramfs-5.15.10-100.fc34.x86_64.img" There is no need to check if path like /var/cache/osbuild-worker/osbuild-store/tmp/tmp2prywdy5object/tree/boot/vmlinuz-5.15.10-100.fc34.x86_64 physically exists. Note these helper functions doesn't support CoreOS/Atomic/Silverblue since grubby isn't used by them. Reviewed-by: Pingfan Liu Reviewed-by: Philipp Rudo Signed-off-by: Coiby Xu --- kdumpctl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/kdumpctl b/kdumpctl index a72b71b..2ace803 100755 --- a/kdumpctl +++ b/kdumpctl @@ -1325,6 +1325,36 @@ get_dump_mode_by_kernel() fi } +_filter_grubby_kernel_str() +{ + local _grubby_kernel_str=$1 + echo -n "$_grubby_kernel_str" | sed -n -e 's/^kernel="\(.*\)"/\1/p' +} + +_find_kernel_path_by_release() +{ + local _release="$1" _grubby_kernel_str _kernel_path + _grubby_kernel_str=$(grubby --info ALL | grep "^kernel=.*$_release") + _kernel_path=$(_filter_grubby_kernel_str "$_grubby_kernel_str") + if [[ -z $_kernel_path ]]; then + derror "kernel $_release doesn't exist" + return 1 + fi + echo -n "$_kernel_path" +} + +_get_current_running_kernel_path() +{ + local _release _path + + _release=$(uname -r) + if _path=$(_find_kernel_path_by_release "$_release"); then + echo -n "$_path" + else + return 1 + fi +} + reset_crashkernel() { local kernel=$1 entry crashkernel_default