From 6a4d33088b57eb58eb5d33f055fac4a8985252cb Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 25 Jul 2022 16:43:13 +0200 Subject: [dev] Add dev_swap_version: Returns version number of a swap partition --- modules/dev.inc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'modules') diff --git a/modules/dev.inc b/modules/dev.inc index ce58075..7c6e1e7 100644 --- a/modules/dev.inc +++ b/modules/dev.inc @@ -183,6 +183,27 @@ dev_get_type() { return 1 } +# Get version of given swap partition. $1 will be checked for the +# swap signature, and if found, the version number will be returned. +# old-style swap will be reported as version 1, +# new-style as 1 + . +dev_swap_version() { + local sig version + sig="$( dd if="$1" bs=1 count=10 skip=4086 2> /dev/null )" + if [ "$sig" = "SWAP-SPACE" ]; then + echo 1 + return 0 + elif [ "$sig" = "SWAPSPACE2" ]; then + version="$( __read_le "$1" 1024 4 )" + # I think there is only '1' currently, but try to be clever... + if [ "$version" -gt 0 ] && [ "$version" -lt 8 ]; then + echo $(( 1 + version )) + return 0 + fi + fi + return 1 +} + # stdin = binary data, stdout = raw, unformatted hex # __hex2bin() -- cgit v1.2.3-55-g7522