improve get_recommend_size

This patch rewrites get_recommend_size to get rid of the following
limitations,
1. only supports ranges in crashkernel sorted in increasing order
2. the first entry of crashkernel should have only a single digit and
   it's in gigabytes

Suggested-by: Philipp Rudo <prudo@redhat.com>
Signed-off-by: Coiby Xu <coxu@redhat.com>
Reviewed-by: Philipp Rudo <prudo@redhat.com>
This commit is contained in:
Coiby Xu 2022-05-12 10:48:31 +08:00
parent 5c23b6ebb7
commit 4f702c81e9
1 changed files with 27 additions and 20 deletions

View File

@ -796,33 +796,40 @@ get_system_size()
echo $(( (sum) / 1024 / 1024 / 1024)) echo $(( (sum) / 1024 / 1024 / 1024))
} }
# Return the recommended size for the reserved crashkernel memory
# depending on the system memory size.
#
# This functions is expected to be consistent with the parse_crashkernel_mem()
# in kernel i.e. how kernel allocates the kdump memory given the crashkernel
# parameter crashkernel=range1:size1[,range2:size2,…] and the system memory
# size.
get_recommend_size() get_recommend_size()
{ {
local mem_size=$1 local mem_size=$1
local _ck_cmdline=$2 local _ck_cmdline=$2
local OLDIFS="$IFS" local range start start_unit end end_unit size
start=${_ck_cmdline::1} while read -r -d , range; do
if [[ $mem_size -lt $start ]]; then # need to use non-default IFS as double spaces are used as a
echo "0M" # single delimiter while commas aren't...
return IFS=, read start start_unit end end_unit size <<< \
fi "$(echo "$range" | sed -n "s/\([0-9]\+\)\([GT]\?\)-\([0-9]*\)\([GT]\?\):\([0-9]\+[MG]\)/\1,\2,\3,\4,\5/p")"
IFS=','
for i in $_ck_cmdline; do # aka. 102400T
end=$(echo "$i" | awk -F "-" '{ print $2 }' | awk -F ":" '{ print $1 }') end=${end:-104857600}
recommend=$(echo "$i" | awk -F "-" '{ print $2 }' | awk -F ":" '{ print $2 }') [[ "$end_unit" == T ]] && end=$((end * 1024))
size=${end::-1} [[ "$start_unit" == T ]] && start=$((start * 1024))
unit=${end: -1}
if [[ $unit == 'T' ]]; then if [[ $mem_size -ge $start ]] && [[ $mem_size -lt $end ]]; then
size=$((size * 1024)) echo "$size"
fi
if [[ $mem_size -lt $size ]]; then
echo "$recommend"
IFS="$OLDIFS"
return return
fi fi
done
IFS="$OLDIFS" # append a ',' as read expects the 'file' to end with a delimiter
done <<< "$_ck_cmdline,"
# no matching range found
echo "0M"
} }
# get default crashkernel # get default crashkernel