add: more partition related functions

- drive_list
- partition_list
- find_unused
- find_oldest
- find_newest
- version_from_partnum
This commit is contained in:
Peter Šurda 2021-02-28 09:22:13 +01:00
parent 59fae65452
commit 586cccf0d4
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
3 changed files with 97 additions and 2 deletions

View File

@ -23,4 +23,69 @@ function rename_partition
parted -s "$drive" -- name "$partition" '"'"$new_label"'"'
}
function drive_list
{
drives="$(lsblk -nd -o NAME,TYPE \
/dev/nvme+([0-9])n+([0-9]) /dev/sd+([a-z]) | \
grep ' disk' |cut -d\ -f1)"
}
function partition_list
{
local drive="$1"
partitions="$(parted -m "$drive" -- print| \
awk --field-separator=":" \
'{if (NR > 2)
print $1
}')"
}
function find_unused
{
local drive="$1"
unused="$(parted -m "$drive" -- print|awk --field-separator=":" \
'{if (NR > 2 && $1 >= 2 && $6 == "root unused") {
print $1
}}')|head -1"
}
function find_oldest
{
local drive="$1"
oldest="$(parted -m "$drive" -- print|awk --field-separator=":" \
'BEGIN
{min="ZZZZ"}
{if (NR > 2 && $1 >= 2 && $1 <= 4 &&
$6 < min && $6 != "root unused") {
min=$6; part=$1
}}
END
{print part}')"
}
function find_newest
{
local drive="$1"
newest="$(parted -m "$drive" -- print|awk --field-separator=":" \
'BEGIN
{max=""}
{if (NR > 2 && $1 >= 2 && $1 <= 4 &&
$6 > max && $6 != "root unused") {
max=$6; part=$1
}}
END
{print part}')"
}
function version_from_partnum
{
local drive="$1"
local partnum="$2"
version="$(parted -m "$drive" -- print|awk --field-separator=":" \
'{if (NR > 2 && $1 == "'"$partnum"') {
print $6
}}')"
version="$(echo "$version"|cut -d\ -f2)"
}
# vim: set ft=sh:

View File

@ -1,7 +1,7 @@
BYT;
T_E_M_P:17.2GB:file:512:512:gpt::;
1:1049kB:1074MB:1073MB::EFI system partition:boot, legacy_boot, esp;
2:1075MB:2147MB:1073MB::root renamed:;
2:1075MB:2147MB:1073MB::root 20210228:;
3:2149MB:3221MB:1073MB::root unused:;
4:3222MB:4295MB:1073MB::root unused:;
5:4296MB:17.2GB:12.9GB::Encrypted:;

View File

@ -3,16 +3,46 @@
function test_partitioning()
{
local img
local test_version=20210228
local min_version=20210101
local max_version=20211231
img=$(mktemp -u)
dd if=/dev/zero of="$img" bs=1M seek=16384 count=0 2> /dev/null
setup_drive "$img"
output=$(parted -ms "$img" -- print)
sed -i "s|T_E_M_P|$img|g;" tests/data/parted.dat
assertEquals "$(cat tests/data/parted.dat)" "$output"
rename_partition "$img" 2 "root renamed"
rename_partition "$img" 2 "root $test_version"
output=$(parted -ms "$img" -- print)
sed -i "s|T_E_M_P|$img|g;" tests/data/rename.dat
assertEquals "$(cat tests/data/rename.dat)" "$output"
version_from_partnum "$img" 2
assertEquals "$test_version" "$version"
version_from_partnum "$img" 3
assertEquals "$min_version" "$version"
version_from_partnum "$img" 4
assertEquals "$max_version" "$version"
find_unused "$img"
assertContains " 3 4 " "$unused"
rename_partition "$img" 3 "root $min_version"
find_oldest "$img"
assertEquals "$min_version" "$oldest"
rename_partition "$img" 4 "root $max_version"
find_newest "$img"
assertEquals "$max_version" "$newest"
partition-list "$img"
assertEquals "1 2 3 4 5" "$partitions"
rm -f "$img"
}