2021-02-26 22:19:36 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
function setup_drive
|
|
|
|
{
|
|
|
|
local drive="$1"
|
|
|
|
parted -s "$drive" -- \
|
|
|
|
mklabel gpt \
|
|
|
|
unit MiB \
|
|
|
|
mkpart '"EFI system partition"' fat32 1 1024 \
|
|
|
|
set 1 esp on \
|
2021-02-27 09:54:31 +01:00
|
|
|
set 1 legacy_boot on \
|
2021-02-26 22:19:36 +01:00
|
|
|
mkpart '"root unused"' ext4 1025 2048 \
|
|
|
|
mkpart '"root unused"' ext4 2049 3072 \
|
|
|
|
mkpart '"root unused"' ext4 3073 4096 \
|
|
|
|
mkpart Encrypted ext4 4097 -1
|
2021-03-02 22:02:55 +01:00
|
|
|
until [ -b "$drive"1 ] || [ -b "$drive"p1 ]; do
|
|
|
|
sleep 1
|
|
|
|
done
|
2021-02-26 22:19:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function rename_partition
|
|
|
|
{
|
|
|
|
local drive="$1"
|
|
|
|
local partition="$2"
|
|
|
|
local new_label="$3"
|
|
|
|
parted -s "$drive" -- name "$partition" '"'"$new_label"'"'
|
|
|
|
}
|
|
|
|
|
2021-02-28 09:22:13 +01:00
|
|
|
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| \
|
2021-02-28 09:28:37 +01:00
|
|
|
awk -F: \
|
2021-02-28 09:22:13 +01:00
|
|
|
'{if (NR > 2)
|
|
|
|
print $1
|
2021-02-28 09:31:56 +01:00
|
|
|
}'|xargs echo)"
|
2021-02-28 09:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function find_unused
|
|
|
|
{
|
|
|
|
local drive="$1"
|
2021-02-28 09:28:37 +01:00
|
|
|
unused="$(parted -m "$drive" -- print|awk -F: \
|
2021-02-28 09:22:13 +01:00
|
|
|
'{if (NR > 2 && $1 >= 2 && $6 == "root unused") {
|
|
|
|
print $1
|
2021-02-28 09:31:56 +01:00
|
|
|
}}'|head -1)"
|
2021-02-28 09:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function find_oldest
|
|
|
|
{
|
|
|
|
local drive="$1"
|
2021-03-02 14:39:19 +01:00
|
|
|
local current="$(grep -E ^serial: /etc/cloud/build.info |cut -d\ -f2)"
|
2021-02-28 09:28:37 +01:00
|
|
|
oldest="$(parted -m "$drive" -- print|awk -F: \
|
2021-03-02 14:39:19 +01:00
|
|
|
-v current="root_$current" \
|
2021-02-28 09:58:49 +01:00
|
|
|
'BEGIN {min="zzzzz"}
|
|
|
|
{if (NR > 2 && $1 >= 2 && $1 <= 4 &&
|
2021-03-02 14:39:19 +01:00
|
|
|
$6 < min && $6 != "root unused" &&
|
|
|
|
$6 != current) {
|
2021-02-28 09:58:49 +01:00
|
|
|
min=$6; part=$1
|
|
|
|
}}
|
|
|
|
END {print part}')"
|
2021-02-28 09:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function find_newest
|
|
|
|
{
|
|
|
|
local drive="$1"
|
2021-02-28 09:28:37 +01:00
|
|
|
newest="$(parted -m "$drive" -- print|awk -F: \
|
2021-02-28 09:58:49 +01:00
|
|
|
'BEGIN {max=""}
|
|
|
|
{if (NR > 2 && $1 >= 2 && $1 <= 4 &&
|
|
|
|
$6 > max && $6 != "root unused") {
|
|
|
|
max=$6; part=$1
|
|
|
|
}}
|
|
|
|
END {print part}')"
|
2021-02-28 09:22:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function version_from_partnum
|
|
|
|
{
|
|
|
|
local drive="$1"
|
|
|
|
local partnum="$2"
|
2021-02-28 09:28:37 +01:00
|
|
|
version="$(parted -m "$drive" -- print|awk -F: \
|
2021-02-28 09:35:39 +01:00
|
|
|
-v partnum="$partnum" \
|
2021-02-28 10:06:46 +01:00
|
|
|
'{if (NR > 2 && $1 == partnum &&
|
2021-02-28 10:02:59 +01:00
|
|
|
$6 != "root unused") {
|
2021-02-28 09:22:13 +01:00
|
|
|
print $6
|
|
|
|
}}')"
|
2021-03-02 12:12:10 +01:00
|
|
|
version="$(echo "$version"|cut -d_ -f2-)"
|
2021-02-28 09:22:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:19:36 +01:00
|
|
|
# vim: set ft=sh:
|