sysdeploy/drive.inc

93 lines
2.2 KiB
PHP
Raw Normal View History

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
}
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
2021-02-28 09:31:56 +01:00
}'|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
2021-02-28 09:31:56 +01:00
}}'|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" \
2021-02-28 09:58:49 +01:00
'BEGIN {min="zzzzz"}
{if (NR > 2 && $1 >= 2 && $1 <= 4 &&
$6 < min && $6 != "root unused" &&
$6 != current) {
2021-02-28 09:58:49 +01:00
min=$6; part=$1
}}
END {print part}')"
}
function find_newest
{
local drive="$1"
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}')"
}
function version_from_partnum
{
local drive="$1"
local partnum="$2"
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 &&
$6 != "root unused") {
print $6
}}')"
2021-03-02 12:12:10 +01:00
version="$(echo "$version"|cut -d_ -f2-)"
}
2021-02-26 22:19:36 +01:00
# vim: set ft=sh: