feat: high leve functions for install and update

This commit is contained in:
Peter Šurda 2021-03-02 14:39:19 +01:00
parent ef7291d9b8
commit b17988134b
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
5 changed files with 67 additions and 49 deletions

View File

@ -52,10 +52,13 @@ function find_unused
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 < min && $6 != "root unused" &&
$6 != current) {
min=$6; part=$1
}}
END {print part}')"

View File

@ -1,25 +1,18 @@
#!/bin/bash
function setup_bootloader()
{
local dev="$1"
check_if_can_overwrite
check_if_mounted_or_used_in_lvm_md_crypt
wipe_or_nvme_format
parted mklabel gpt
parted mkpart 1GB EFI
parted mkpart 1GB squashfs-1
parted mkpart 1GB squashfs-2
parted mkpart 1GB squashfs-3
. install.inc
drive="$1"
if [ ! -b "$drive" ]; then
echo "Usage install.sh /dev/wherever"
exit 1
fi
setup_drive "$drive"
get_streams
get_latest_maas_id
download_version "$release_version" "$drive"
update_syslinux_menu "$drive"
dd gptmbr.bin
mkfs.vfat "$dev"1
syslinux "$dev"1
mount
copy_efi_files
create_syslinux_cfg
umount
}
detect_disks
select_disk

View File

@ -6,6 +6,9 @@ OS_VERSION=20.04
function get_streams()
{
if [ -e "$streams" ]; then
return 0
fi
streams="$(mktemp)"
wget -qO "$streams" https://images.maas.io/ephemeral-v3/stable/streams/v1/com.ubuntu.maas:stable:v3:download.json
}
@ -23,27 +26,39 @@ function get_checksum()
{
local release_version="$1"
local filename="$2"
if [ ! -f "$streams" ]; then
return 1
fi
checksum=$(jq -r '.products[]|select(.version=="'"$OS_VERSION"'" and .arch=="amd64" and .kflavor=="generic" and .subarch=="ga-'"$OS_VERSION"'").versions."'"$release_version"'".items."'"$filename"'".sha256' \
"$streams")
}
function remove_version()
function file_url()
{
local version="$1"
local release_version="$1"
local filename="$2"
if [ ! -f "$streams" ]; then
return 1
fi
url=$(jq -r '.products[]|select(.version=="'"$OS_VERSION"'" and .arch=="amd64" and .kflavor=="generic" and .subarch=="ga-'"$OS_VERSION"'").versions."'"$release_version"'".items."'"$filename"'".path' \
"$streams")
url="https://images.maas.io/ephemeral-v3/stable/$url"
}
function download_and_verify()
{
local url="$1"
local checksum="$2"
local fname="$3"
local release_version="$1"
local fname="$2"
file_url "$release_version" "$fname"
checksum "$release_version" "$fname"
wget "$url" -O "$fname"
sha256sum=$(sha256sum "$fname")
if [ "$sha256sum" == "$checksum" ]; then
return 0
fi
rm -f "$fname"
return 1
}
@ -54,18 +69,4 @@ function cleanup_temp()
fi
}
function download_files()
{
download kernel
download initrd
download squashfs
dd if=squashfs of="$partition" bs=1M
create_options_file
update_default
delete_oldest_if_needed
update_onerror_second_youngest
}
# vim: set ft=sh:

View File

@ -1,17 +1,25 @@
#!/bin/bash
function mount_efi()
function dev_from_partnum()
{
local drive="$1"
local partition="$drive"1
local partnum="$2"
partition="$drive""$partnum"
if [ ! -b "$partition" ]; then
partition="$drive"p1
partition="$drive"p"$partnum"
if [ ! -b "$partition" ]; then
echo "Can't find first partition for \"$drive\""
exit 1
fi
fi
}
function mount_efi()
{
local drive="$1"
dev_from_partnum "$drive" 1
unset mnt
mnt="$(mktemp -d)"
@ -25,7 +33,7 @@ function mount_efi()
fi
}
function cleanup_mnt()
function umount_efi()
{
umount "$mnt"
flush
@ -36,6 +44,9 @@ function install_syslinux()
{
local drive="$1"
dd bs=440 count=1 conv=notrunc \
if=/usr/lib/syslinux/mbr/gptmbr.bin of="$drive"
mount_efi "$drive"
# legacy
@ -48,7 +59,7 @@ function install_syslinux()
cp syslinux/{mainmenu,syslinux}.cfg "$mnt"/syslinux
cp syslinux/syslx64.cfg "$mnt"/EFI/BOOT
cleanup_mnt
umount_efi
syslinux "$partition"
}

View File

@ -25,7 +25,7 @@ function update_syslinux_menu()
version_from_partnum "$drive" "$part"
versions["$version"]=1
for f in $files; do
if [ ! -e "$imagepath"/"$f""$version" ]; then
if [ ! -e "$mnt"/"$imagepath"/"$f""$version" ]; then
continue
fi
done
@ -38,7 +38,17 @@ APPEND initrd=$imagepath/boot-initrd.$version root=PARTLABEL="rootfs_$version" i
(EOL)
done
cleanup_mnt
# find expired
for f in $files; do
for i in "$mnt"/"$imagepath"/"$f"*; do
version="$(basename "$i"|cut -d_ -f2-)"
if [ -z "${versions[$version]}" ]; then
rm -f $i
fi
done
done
umount_efi
}
# vim: set ft=sh: