From 27f67f14ee4b574fe4c42c41c1d7f6d59c9cf5aa Mon Sep 17 00:00:00 2001 From: Tao Liu Date: Wed, 31 May 2023 15:09:37 +0800 Subject: [PATCH] kdumpctl: Move get_kernel_size to kdumpctl Resolves: bz2169720 Upstream: src.fedoraproject.org/rpms/kexec-tools.git Conflicts: None commit 81d89c885f2c07d30738f2a942c23f7b81e60cd0 Author: Philipp Rudo Date: Fri May 5 17:14:40 2023 +0200 kdumpctl: Move get_kernel_size to kdumpctl The function is only used in do_estimate. Move it to kdumpctl to prevent confusion. Signed-off-by: Philipp Rudo Reviewed-by: Pingfan Liu Reviewed-by: Coiby Xu Signed-off-by: Philipp Rudo Signed-off-by: Tao Liu --- kdump-lib.sh | 72 ---------------------------------------------------- kdumpctl | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 72 deletions(-) diff --git a/kdump-lib.sh b/kdump-lib.sh index 923f7ee..dce3239 100755 --- a/kdump-lib.sh +++ b/kdump-lib.sh @@ -973,75 +973,3 @@ get_all_kdump_crypt_dev() get_luks_crypt_dev "$(kdump_get_maj_min "$_dev")" done } - -check_vmlinux() -{ - # Use readelf to check if it's a valid ELF - readelf -h "$1" &> /dev/null || return 1 -} - -get_vmlinux_size() -{ - local size=0 _msize - - while read -r _msize; do - size=$((size + _msize)) - done <<< "$(readelf -l -W "$1" | awk '/^ LOAD/{print $6}' 2> /dev/stderr)" - - echo $size -} - -try_decompress() -{ - # The obscure use of the "tr" filter is to work around older versions of - # "grep" that report the byte offset of the line instead of the pattern. - - # Try to find the header ($1) and decompress from here - for pos in $(tr "$1\n$2" "\n$2=" < "$4" | grep -abo "^$2"); do - if ! type -P "$3" > /dev/null; then - ddebug "Signiature detected but '$3' is missing, skip this decompressor" - break - fi - - pos=${pos%%:*} - tail "-c+$pos" "$img" | $3 > "$5" 2> /dev/null - if check_vmlinux "$5"; then - ddebug "Kernel is extracted with '$3'" - return 0 - fi - done - - return 1 -} - -# Borrowed from linux/scripts/extract-vmlinux -get_kernel_size() -{ - # Prepare temp files: - local tmp img=$1 - - tmp=$(mktemp /tmp/vmlinux-XXX) - trap 'rm -f "$tmp"' 0 - - # Try to check if it's a vmlinux already - check_vmlinux "$img" && get_vmlinux_size "$img" && return 0 - - # That didn't work, so retry after decompression. - try_decompress '\037\213\010' xy gunzip "$img" "$tmp" || - try_decompress '\3757zXZ\000' abcde unxz "$img" "$tmp" || - try_decompress 'BZh' xy bunzip2 "$img" "$tmp" || - try_decompress '\135\0\0\0' xxx unlzma "$img" "$tmp" || - try_decompress '\211\114\132' xy 'lzop -d' "$img" "$tmp" || - try_decompress '\002!L\030' xxx 'lz4 -d' "$img" "$tmp" || - try_decompress '(\265/\375' xxx unzstd "$img" "$tmp" - - # Finally check for uncompressed images or objects: - [[ $? -eq 0 ]] && get_vmlinux_size "$tmp" && return 0 - - # Fallback to use iomem - local _size=0 _seg - while read -r _seg; do - _size=$((_size + 0x${_seg#*-} - 0x${_seg%-*})) - done <<< "$(grep -E "Kernel (code|rodata|data|bss)" /proc/iomem | cut -d ":" -f 1)" - echo $_size -} diff --git a/kdumpctl b/kdumpctl index 359166b..942cb4c 100755 --- a/kdumpctl +++ b/kdumpctl @@ -1219,6 +1219,78 @@ rebuild() return $? } +check_vmlinux() +{ + # Use readelf to check if it's a valid ELF + readelf -h "$1" &> /dev/null || return 1 +} + +get_vmlinux_size() +{ + local size=0 _msize + + while read -r _msize; do + size=$((size + _msize)) + done <<< "$(readelf -l -W "$1" | awk '/^ LOAD/{print $6}' 2> /dev/stderr)" + + echo $size +} + +try_decompress() +{ + # The obscure use of the "tr" filter is to work around older versions of + # "grep" that report the byte offset of the line instead of the pattern. + + # Try to find the header ($1) and decompress from here + for pos in $(tr "$1\n$2" "\n$2=" < "$4" | grep -abo "^$2"); do + if ! type -P "$3" > /dev/null; then + ddebug "Signiature detected but '$3' is missing, skip this decompressor" + break + fi + + pos=${pos%%:*} + tail "-c+$pos" "$img" | $3 > "$5" 2> /dev/null + if check_vmlinux "$5"; then + ddebug "Kernel is extracted with '$3'" + return 0 + fi + done + + return 1 +} + +# Borrowed from linux/scripts/extract-vmlinux +get_kernel_size() +{ + # Prepare temp files: + local tmp img=$1 + + tmp=$(mktemp /tmp/vmlinux-XXX) + trap 'rm -f "$tmp"' 0 + + # Try to check if it's a vmlinux already + check_vmlinux "$img" && get_vmlinux_size "$img" && return 0 + + # That didn't work, so retry after decompression. + try_decompress '\037\213\010' xy gunzip "$img" "$tmp" || + try_decompress '\3757zXZ\000' abcde unxz "$img" "$tmp" || + try_decompress 'BZh' xy bunzip2 "$img" "$tmp" || + try_decompress '\135\0\0\0' xxx unlzma "$img" "$tmp" || + try_decompress '\211\114\132' xy 'lzop -d' "$img" "$tmp" || + try_decompress '\002!L\030' xxx 'lz4 -d' "$img" "$tmp" || + try_decompress '(\265/\375' xxx unzstd "$img" "$tmp" + + # Finally check for uncompressed images or objects: + [[ $? -eq 0 ]] && get_vmlinux_size "$tmp" && return 0 + + # Fallback to use iomem + local _size=0 _seg + while read -r _seg; do + _size=$((_size + 0x${_seg#*-} - 0x${_seg%-*})) + done <<< "$(grep -E "Kernel (code|rodata|data|bss)" /proc/iomem | cut -d ":" -f 1)" + echo $_size +} + do_estimate() { local kdump_mods