forked from Bitmessage/sysdeploy
93 lines
2.2 KiB
Bash
93 lines
2.2 KiB
Bash
#!/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 \
|
|
set 1 legacy_boot on \
|
|
mkpart '"root unused"' ext4 1025 2048 \
|
|
mkpart '"root unused"' ext4 2049 3072 \
|
|
mkpart '"root unused"' ext4 3073 4096 \
|
|
mkpart Encrypted ext4 4097 -1
|
|
}
|
|
|
|
function rename_partition
|
|
{
|
|
local drive="$1"
|
|
local partition="$2"
|
|
local new_label="$3"
|
|
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 -F: \
|
|
'{if (NR > 2)
|
|
print $1
|
|
}'|xargs echo)"
|
|
}
|
|
|
|
function find_unused
|
|
{
|
|
local drive="$1"
|
|
unused="$(parted -m "$drive" -- print|awk -F: \
|
|
'{if (NR > 2 && $1 >= 2 && $6 == "root unused") {
|
|
print $1
|
|
}}'|head -1)"
|
|
}
|
|
|
|
function find_oldest
|
|
{
|
|
local drive="$1"
|
|
local current="$(grep -E ^serial: /etc/cloud/build.info |cut -d\ -f2)"
|
|
oldest="$(parted -m "$drive" -- print|awk -F: \
|
|
-v current="root_$current" \
|
|
'BEGIN {min="zzzzz"}
|
|
{if (NR > 2 && $1 >= 2 && $1 <= 4 &&
|
|
$6 < min && $6 != "root unused" &&
|
|
$6 != current) {
|
|
min=$6; part=$1
|
|
}}
|
|
END {print part}')"
|
|
}
|
|
|
|
function find_newest
|
|
{
|
|
local drive="$1"
|
|
newest="$(parted -m "$drive" -- print|awk -F: \
|
|
'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 -F: \
|
|
-v partnum="$partnum" \
|
|
'{if (NR > 2 && $1 == partnum &&
|
|
$6 != "root unused") {
|
|
print $6
|
|
}}')"
|
|
version="$(echo "$version"|cut -d_ -f2-)"
|
|
}
|
|
|
|
# vim: set ft=sh:
|