From 37de94d02a647263b27ad6fcc5bd30e7cf1a3def Mon Sep 17 00:00:00 2001 From: Pingfan Liu Date: Jun 15 2023 02:32:36 +0000 Subject: kdump-lib: Introduce a help function _crashkernel_add() Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2160676 Upstream: Fedora rawhide Conflict: Drop shellspec test case commit 51efbcf83e26a69fb56466a580b50e5d4867509f Author: Pingfan Liu Date: Tue Jun 13 17:43:20 2023 +0800 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 Reviewed-by: Coiby Xu Reviewed-by: Philipp Rudo Signed-off-by: Pingfan Liu --- diff --git a/kdump-lib.sh b/kdump-lib.sh index 75d55f5..6009360 100755 --- a/kdump-lib.sh +++ b/kdump-lib.sh @@ -904,6 +904,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()