From b17988134bc1449c8f4c1c8319e463fae5594b0c Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Tue, 2 Mar 2021 14:39:19 +0100 Subject: [PATCH] feat: high leve functions for install and update --- drive.inc | 5 ++++- install.sh | 35 ++++++++++++++--------------------- maas-images.inc | 41 +++++++++++++++++++++-------------------- syslinux.inc | 21 ++++++++++++++++----- syslinux_menu.inc | 14 ++++++++++++-- 5 files changed, 67 insertions(+), 49 deletions(-) diff --git a/drive.inc b/drive.inc index fe75b43..e06a00c 100644 --- a/drive.inc +++ b/drive.inc @@ -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}')" diff --git a/install.sh b/install.sh index b6b4450..18ca160 100644 --- a/install.sh +++ b/install.sh @@ -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 diff --git a/maas-images.inc b/maas-images.inc index ce5f181..a3280b2 100644 --- a/maas-images.inc +++ b/maas-images.inc @@ -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: diff --git a/syslinux.inc b/syslinux.inc index fa45cb6..c3cd46e 100644 --- a/syslinux.inc +++ b/syslinux.inc @@ -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" } diff --git a/syslinux_menu.inc b/syslinux_menu.inc index e218dea..593b8a2 100644 --- a/syslinux_menu.inc +++ b/syslinux_menu.inc @@ -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: