metropolis: implement A/B updates

This implements an A/B update mechanism using two slots, A and B.
This is realized with two system partitions as well as two EFI
loaders/kernels.

The A/B system relies on two EFI loader entries. This has the advantage
that there is no preloader required, which makes the system more
reliable as well as avoiding the complexity of having an un-updatable
preloader (CoreOS has this issue where their GRUB2 crashed booting newer
kernels, sadly the issue seems lost with the migration to Fedora
CoreOS). It also means that the operator can easily override the slot
being booted via the boot loader entries. Primary disadvantage is that
it relies on EFI working somewhat to spec.

New versions are booted into only once by setting NextBoot, if the
bootup doesn't succeed, i.e. if the boot doesn't get to a cluster rejoin
the next boot will be the old slot. Once it gets to this stage the
permanent BootOrder is changed.

The EFI loaders don't know if they are slot A or B because they are
identical and relying on OptionalData in the boot entry to indicate the
slot means that if the EFI boot entries go away, recovering is very hard.
Thus the loaders look at their own file name to determine what slot they
are in. If no slot could be determined, they default to booting slot A.
It is planned to eventually use Authenticode Stamping (passing data in
fake certificates) to stamp the slot into the loader without affecting
the TPM hash logged.

Change-Id: I40de2df8ff7ff660c17d2c97f3d9eb1bd4ddf5bc
Reviewed-on: https://review.monogon.dev/c/monogon/+/1874
Tested-by: Jenkins CI
Reviewed-by: Serge Bazanski <serge@monogon.tech>
diff --git a/metropolis/node/build/mkimage/osimage/osimage.go b/metropolis/node/build/mkimage/osimage/osimage.go
index 7098439..01c13ac 100644
--- a/metropolis/node/build/mkimage/osimage/osimage.go
+++ b/metropolis/node/build/mkimage/osimage/osimage.go
@@ -19,6 +19,7 @@
 package osimage
 
 import (
+	"bytes"
 	"fmt"
 	"io"
 	"strings"
@@ -39,11 +40,14 @@
 )
 
 const (
-	SystemLabel = "METROPOLIS-SYSTEM"
-	DataLabel   = "METROPOLIS-NODE-DATA"
-	ESPLabel    = "ESP"
+	SystemALabel = "METROPOLIS-SYSTEM-A"
+	SystemBLabel = "METROPOLIS-SYSTEM-B"
+	DataLabel    = "METROPOLIS-NODE-DATA"
+	ESPLabel     = "ESP"
 
 	EFIPayloadPath = "/EFI/BOOT/BOOTx64.EFI"
+	EFIBootAPath   = "/EFI/metropolis/boot-a.efi"
+	EFIBootBPath   = "/EFI/metropolis/boot-b.efi"
 	nodeParamsPath = "metropolis/parameters.pb"
 )
 
@@ -112,7 +116,16 @@
 	rootInode := fat32.Inode{
 		Attrs: fat32.AttrDirectory,
 	}
-	if err := rootInode.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), params.EFIPayload); err != nil {
+	efiPayload, err := io.ReadAll(params.EFIPayload)
+	if err != nil {
+		return nil, fmt.Errorf("while reading EFIPayload: %w", err)
+	}
+	if err := rootInode.PlaceFile(strings.TrimPrefix(EFIBootAPath, "/"), bytes.NewReader(efiPayload)); err != nil {
+		return nil, err
+	}
+	// Also place a copy of the boot file at the autodiscovery path. This will
+	// always boot slot A.
+	if err := rootInode.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), bytes.NewReader(efiPayload)); err != nil {
 		return nil, err
 	}
 	if params.NodeParameters != nil {
@@ -132,7 +145,7 @@
 	if params.PartitionSize.System != 0 && params.SystemImage != nil {
 		systemPartitionA := gpt.Partition{
 			Type: SystemAType,
-			Name: SystemLabel,
+			Name: SystemALabel,
 		}
 		if err := tbl.AddPartition(&systemPartitionA, params.PartitionSize.System*Mi); err != nil {
 			return nil, fmt.Errorf("failed to allocate system partition A: %w", err)
@@ -140,6 +153,13 @@
 		if _, err := io.Copy(blockdev.NewRWS(systemPartitionA), params.SystemImage); err != nil {
 			return nil, fmt.Errorf("failed to write system partition A: %w", err)
 		}
+		systemPartitionB := gpt.Partition{
+			Type: SystemBType,
+			Name: SystemBLabel,
+		}
+		if err := tbl.AddPartition(&systemPartitionB, params.PartitionSize.System*Mi); err != nil {
+			return nil, fmt.Errorf("failed to allocate system partition B: %w", err)
+		}
 	} else if params.PartitionSize.System == 0 && params.SystemImage != nil {
 		// Safeguard against contradicting parameters.
 		return nil, fmt.Errorf("the system image parameter was passed while the associated partition size is zero")
@@ -171,7 +191,7 @@
 					PartitionUUID: esp.ID,
 				},
 			},
-			efivarfs.FilePath(EFIPayloadPath),
+			efivarfs.FilePath(EFIBootAPath),
 		},
 	}, nil
 }