kdump-lib: Introduce a help function _crashkernel_add()

This help function can manipulate the crashkernel cmdline by adding an
number for each item. Also a basic test case for _crashkernel_add() is
provided in this patch.

Credit to Philipp, who contributes the original code.

Signed-off-by: Pingfan Liu <piliu@redhat.com>
Reviewed-by: Coiby Xu <coxu@redhat.com>
Reviewed-by: Philipp Rudo <prudo@redhat.com>
This commit is contained in:
Pingfan Liu 2023-06-13 17:43:20 +08:00 committed by Coiby Xu
parent 0471131a16
commit 51efbcf83e
2 changed files with 78 additions and 0 deletions

View File

@ -786,6 +786,64 @@ get_recommend_size()
echo "0M"
}
# $1 crashkernel=""
# $2 delta in unit of MB
_crashkernel_add()
{
local _ck _add _entry _ret
local _range _size _offset
_ck="$1"
_add="$2"
_ret=""
if [[ "$_ck" == *@* ]]; then
_offset="@${_ck##*@}"
_ck=${_ck%@*}
elif [[ "$_ck" == *,high ]] || [[ "$_ck" == *,low ]]; then
_offset=",${_ck##*,}"
_ck=${_ck%,*}
else
_offset=''
fi
while read -d , -r _entry; do
[[ -n "$_entry" ]] || continue
if [[ "$_entry" == *:* ]]; then
_range=${_entry%:*}
_size=${_entry#*:}
else
_range=""
_size=${_entry}
fi
case "${_size: -1}" in
K)
_size=${_size::-1}
_size="$((_size + (_add * 1024)))K"
;;
M)
_size=${_size::-1}
_size="$((_size + _add))M"
;;
G)
_size=${_size::-1}
_size="$((_size * 1024 + _add))M"
;;
*)
_size="$((_size + (_add * 1024 * 1024)))"
;;
esac
[[ -n "$_range" ]] && _ret+="$_range:"
_ret+="$_size,"
done <<< "$_ck,"
_ret=${_ret%,}
[[ -n "$_offset" ]] && _ret+=$_offset
echo "$_ret"
}
# get default crashkernel
# $1 dump mode, if not specified, dump_mode will be judged by is_fadump_capable
kdump_get_arch_recommend_crashkernel()

View File

@ -48,6 +48,26 @@ Describe 'kdump-lib'
End
End
Describe "_crashkernel_add()"
Context "when the input parameter is '1G-4G:256M,4G-64G:320M,64G-:576M'"
delta=100
Parameters
"1G-4G:256M,4G-64G:320M,64G-:576M" "1G-4G:356M,4G-64G:420M,64G-:676M"
"1G-4G:256M,4G-64G:320M,64G-:576M@4G" "1G-4G:356M,4G-64G:420M,64G-:676M@4G"
"1G-4G:1G,4G-64G:2G,64G-:3G@4G" "1G-4G:1124M,4G-64G:2148M,64G-:3172M@4G"
"1G-4G:10000K,4G-64G:20000K,64G-:40000K@4G" "1G-4G:112400K,4G-64G:122400K,64G-:142400K@4G"
"300M,high" "400M,high"
"300M,low" "400M,low"
"500M@1G" "600M@1G"
End
It "should add delta to the values after ':'"
When call _crashkernel_add "$1" "$delta"
The output should equal "$2"
End
End
End
Describe 'prepare_cmdline()'
get_bootcpu_apicid() {
echo 1