[PATCH v2 1/2] common/rc: add _parse_size_string

An Long
2022-06-16T04:39:05Z

Add a helper to convert size value to bytes. This is used to handle
value as bytes, such as 4k to 4096.

Signed-off-by: An Long <lan@suse.com>
---
V1 -> V2:
- Rename _parse_size_from_string to _parse_size_string
- Remove unnecessary '$' sign
---
common/rc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)

diff --git a/common/rc b/common/rc
index 3c072c16..09ffafa4 100644
--- a/common/rc
+++ b/common/rc
@@ -1028,6 +1028,54 @@ _check_minimal_fs_size()
fi
}

+# Convert size value to bytes
+# _parse_size_string <size>
+_parse_size_string()
+{
+ local str=$1
+ local mult=1
+ local size
+ local endchar
+
+ if [[ $str =~ ^[0-9]+[a-zA-Z]$ ]] ; then
+ size=${str:: -1}
+ endchar=${str: -1}
+ case $endchar in
+ e|E)
+ mult=$((mult * 1024))
+ ;&
+ p|P)
+ mult=$((mult * 1024))
+ ;&
+ t|T)
+ mult=$((mult * 1024))
+ ;&
+ g|G)
+ mult=$((mult * 1024))
+ ;&
+ m|M)
+ mult=$((mult * 1024))
+ ;&
+ k|K)
+ mult=$((mult * 1024))
+ ;&
+ b|B)
+ ;;
+ *)
+ echo "unknown size descriptor $endchar"
+ exit 1
+ esac
+ elif [[ $str =~ ^[0-9]+$ ]] ; then
+ size=$str
+ else
+ echo "size value $str is invalid"
+ exit 1
+ fi
+
+ size=$((size * mult))
+ echo $size
+}
+
# Create fs of certain size on scratch device
# _scratch_mkfs_sized <size in bytes> [optional blocksize]
_scratch_mkfs_sized()
--
2.35.3