Hi Kairui,
On Thu, 12 Aug 2021 13:47:59 +0800
Kairui Song <kasong(a)redhat.com> wrote:
Properly quote strings, remove duplicated echo / cat, and seperate
some
^^^^^^^^
typo separate
variable decleration and assignment, remove unused variable and
update
^^^^^^^^^^^
typo declaration
legacy syntax.
Signed-off-by: Kairui Song <kasong(a)redhat.com>
---
kdump-lib.sh | 201 ++++++++++++++++++++++++++-------------------------
1 file changed, 101 insertions(+), 100 deletions(-)
diff --git a/kdump-lib.sh b/kdump-lib.sh
index 125423f5..11e8343b 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
[...]
@@ -765,18 +766,18 @@ prepare_cmdline()
cmdline=$(remove_cmdline_param "$cmdline" trace_buf_size trace_event)
cmdline="${cmdline} trace_buf_size=1"
- echo ${cmdline}
+ echo "${cmdline}"
}
#get system memory size in the unit of GB
get_system_size()
{
- result=$(cat /proc/iomem | grep "System RAM" | awk -F ":"
'{ print $1 }' | tr [:lower:] [:upper:] | paste -sd+)
+ result=$(grep "System RAM" /proc/iomem | awk -F ":" '{ print
$1 }' | tr "[:lower:]" "[:upper:]" | paste -sd+)
result="+$result"
# replace '-' with '+0x' and '+' with '-0x'
- sum=$( echo $result | sed -e 's/-/K0x/g' | sed -e 's/+/-0x/g' | sed
-e 's/K/+/g' )
- size=$(printf "%d\n" $(($sum)))
- let size=$size/1024/1024/1024
+ sum=$(echo "$result" | sed -e 's/-/K0x/g' | sed -e
's/+/-0x/g' | sed -e 's/K/+/g' )
not actually part of the patch but you could combine the three calls to
sed into one.
Thanks
Philipp
+ size=$(printf "%d\n" $((sum)))
+ size=$((size/1024/1024/1024))
echo $size
}
@@ -787,9 +788,6 @@ get_recommend_size()
local _ck_cmdline=$2
local OLDIFS="$IFS"
- last_sz=""
- last_unit=""
-
start=${_ck_cmdline: :1}
if [[ $mem_size -lt $start ]]; then
echo "0M"
@@ -797,15 +795,15 @@ get_recommend_size()
fi
IFS=','
for i in $_ck_cmdline; do
- end=$(echo $i | awk -F "-" '{ print $2 }' | awk -F
":" '{ print $1 }')
- recommend=$(echo $i | awk -F "-" '{ print $2 }' | awk -F
":" '{ print $2 }')
+ end=$(echo "$i" | awk -F "-" '{ print $2 }' | awk -F
":" '{ print $1 }')
+ recommend=$(echo "$i" | awk -F "-" '{ print $2 }' |
awk -F ":" '{ print $2 }')
size=${end: : -1}
unit=${end: -1}
if [[ $unit == 'T' ]]; then
- let size=$size*1024
+ size=$((size*1024))
fi
if [[ $mem_size -lt $size ]]; then
- echo $recommend
+ echo "$recommend"
IFS="$OLDIFS"
return
fi
@@ -844,7 +842,7 @@ kdump_get_arch_recommend_size()
fi
fi
- ck_cmdline=$(echo $ck_cmdline | sed -e 's/-:/-102400T:/g')
+ ck_cmdline=$(echo "$ck_cmdline" | sed -e 's/-:/-102400T:/g')
sys_mem=$(get_system_size)
get_recommend_size "$sys_mem" "$ck_cmdline"
@@ -855,12 +853,13 @@ kdump_get_arch_recommend_size()
# $1: the block device to be checked in maj:min format
get_luks_crypt_dev()
{
+ local _type
[[ -b /dev/block/$1 ]] || return 1
- local _type=$(eval "$(blkid -u filesystem,crypto -o export -- /dev/block/$1);
echo \$TYPE")
- [[ $_type == "crypto_LUKS" ]] && echo $1
+ _type=$(eval "$(blkid -u filesystem,crypto -o export --
"/dev/block/$1"); echo \$TYPE")
+ [[ $_type == "crypto_LUKS" ]] && echo "$1"
- for _x in /sys/dev/block/$1/slaves/*; do
+ for _x in "/sys/dev/block/$1/slaves/"*; do
[[ -f $_x/dev ]] || continue
[[ $_x/subsystem -ef /sys/class/block ]] || continue
get_luks_crypt_dev "$(< "$_x/dev")"
@@ -883,24 +882,24 @@ get_all_kdump_crypt_dev()
local _dev _crypt
for _dev in $(get_block_dump_target); do
- _crypt=$(get_luks_crypt_dev $(kdump_get_maj_min "$_dev"))
- [[ -n "$_crypt" ]] && echo $_crypt
+ _crypt=$(get_luks_crypt_dev "$(kdump_get_maj_min "$_dev")")
+ [[ -n "$_crypt" ]] && echo "$_crypt"
done
}
check_vmlinux()
{
# Use readelf to check if it's a valid ELF
- readelf -h $1 &>/dev/null || return 1
+ readelf -h "$1" &>/dev/null || return 1
}
get_vmlinux_size()
{
local size=0
- while read _type _offset _virtaddr _physaddr _fsize _msize _flg _aln; do
- size=$(( $size + $_msize ))
- done <<< $(readelf -l -W $1 | grep "^ LOAD" 2>/dev/stderr)
+ while read -r _type _offset _virtaddr _physaddr _fsize _msize _flg _aln; do
+ size=$(( size + _msize ))
+ done <<< "$(readelf -l -W "$1" | grep "^ LOAD"
2>/dev/stderr)"
echo $size
}
@@ -913,14 +912,14 @@ try_decompress()
# 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
+ 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
+ tail -c+$pos "$img" | $3 > "$5" 2> /dev/null
+ if check_vmlinux "$5"; then
ddebug "Kernel is extracted with '$3'"
return 0
fi
@@ -933,28 +932,30 @@ try_decompress()
get_kernel_size()
{
# Prepare temp files:
- local img=$1 tmp=$(mktemp /tmp/vmlinux-XXX)
- trap "rm -f $tmp" 0
+ local img=$1 tmp
+
+ 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
+ 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
+ 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
+ [[ $? -eq 0 ]] && get_vmlinux_size "$tmp" && return 0
# Fallback to use iomem
local _size=0
- for _seg in $(cat /proc/iomem | grep -E "Kernel (code|rodata|data|bss)" |
cut -d ":" -f 1); do
- _size=$(( $_size + 0x${_seg#*-} - 0x${_seg%-*} ))
- done
+ 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
}