m/installer/install: move from osbase/build/mkimage/osimage
Move the package osbase/build/mkimage/osimage to
metropolis/installer/install. The package contains "metropolis" in
various strings, so it fits better in //metropolis than in //osbase.
The parent package mkimage no longer exists.
The rename also removes the name clash with osbase/oci/osimage.
Change-Id: Ia228ab94c937f4c7a5b6eb1fcc77993a594d64b6
Reviewed-on: https://review.monogon.dev/c/monogon/+/4296
Tested-by: Jenkins CI
Reviewed-by: Tim Windelschmidt <tim@monogon.tech>
diff --git a/metropolis/installer/BUILD.bazel b/metropolis/installer/BUILD.bazel
index 29eb04c..72fee1a 100644
--- a/metropolis/installer/BUILD.bazel
+++ b/metropolis/installer/BUILD.bazel
@@ -13,9 +13,9 @@
importpath = "source.monogon.dev/metropolis/installer",
visibility = ["//visibility:private"],
deps = [
+ "//metropolis/installer/install",
"//osbase/blockdev",
"//osbase/bringup",
- "//osbase/build/mkimage/osimage",
"//osbase/efivarfs",
"//osbase/oci",
"//osbase/oci/osimage",
diff --git a/metropolis/installer/install/BUILD.bazel b/metropolis/installer/install/BUILD.bazel
new file mode 100644
index 0000000..1532198
--- /dev/null
+++ b/metropolis/installer/install/BUILD.bazel
@@ -0,0 +1,16 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_library")
+
+go_library(
+ name = "install",
+ srcs = ["install.go"],
+ importpath = "source.monogon.dev/metropolis/installer/install",
+ visibility = ["//visibility:public"],
+ deps = [
+ "//osbase/blockdev",
+ "//osbase/efivarfs",
+ "//osbase/fat32",
+ "//osbase/gpt",
+ "//osbase/structfs",
+ "@com_github_google_uuid//:uuid",
+ ],
+)
diff --git a/metropolis/installer/install/install.go b/metropolis/installer/install/install.go
new file mode 100644
index 0000000..a2b9ff8
--- /dev/null
+++ b/metropolis/installer/install/install.go
@@ -0,0 +1,252 @@
+// Copyright The Monogon Project Authors.
+// SPDX-License-Identifier: Apache-2.0
+
+// Package install allows planning and executing the installation of Metropolis
+// to a block device.
+package install
+
+import (
+ "fmt"
+ "io"
+
+ "github.com/google/uuid"
+
+ "source.monogon.dev/osbase/blockdev"
+ "source.monogon.dev/osbase/efivarfs"
+ "source.monogon.dev/osbase/fat32"
+ "source.monogon.dev/osbase/gpt"
+ "source.monogon.dev/osbase/structfs"
+)
+
+var (
+ SystemAType = uuid.MustParse("ee96054b-f6d0-4267-aaaa-724b2afea74c")
+ SystemBType = uuid.MustParse("ee96054b-f6d0-4267-bbbb-724b2afea74c")
+
+ DataType = uuid.MustParse("9eeec464-6885-414a-b278-4305c51f7966")
+)
+
+const (
+ SystemALabel = "METROPOLIS-SYSTEM-A"
+ SystemBLabel = "METROPOLIS-SYSTEM-B"
+ DataLabel = "METROPOLIS-NODE-DATA"
+ ESPLabel = "ESP"
+
+ EFIBootAPath = "EFI/metropolis/boot-a.efi"
+ EFIBootBPath = "EFI/metropolis/boot-b.efi"
+ nodeParamsPath = "metropolis/parameters.pb"
+)
+
+var EFIBootName = map[string]string{
+ "x86_64": "BOOTx64.EFI",
+ "aarch64": "BOOTAA64.EFI",
+}
+
+// EFIBootPath returns the default file path according to the UEFI Specification
+// v2.11 Section 3.5.1.1. This file is booted by any compliant UEFI firmware in
+// absence of another bootable boot entry.
+func EFIBootPath(architecture string) (string, error) {
+ bootName, ok := EFIBootName[architecture]
+ if !ok {
+ return "", fmt.Errorf("unsupported architecture %q", architecture)
+ }
+ return "EFI/BOOT/" + bootName, nil
+}
+
+// PartitionSizeInfo contains parameters used during partition table
+// initialization and, in case of image files, space allocation.
+type PartitionSizeInfo struct {
+ // Size of the EFI System Partition (ESP), in mebibytes. The size must
+ // not be zero.
+ ESP int64
+ // Size of the Metropolis system partition, in mebibytes. The partition
+ // won't be created if the size is zero.
+ System int64
+ // Size of the Metropolis data partition, in mebibytes. The partition
+ // won't be created if the size is zero. If the image is output to a
+ // block device, the partition will be extended to fill the remaining
+ // space.
+ Data int64
+}
+
+// Params contains parameters used by Plan or Write to install Metropolis OS.
+type Params struct {
+ // Output is the block device to which the OS is installed.
+ Output blockdev.BlockDev
+ // Architecture is the CPU architecture of the OS image.
+ Architecture string
+ // ABLoader provides the A/B loader which then loads the EFI loader for the
+ // correct slot.
+ ABLoader structfs.Blob
+ // EFIPayload provides contents of the EFI payload file. It must not be
+ // nil. This gets put into boot slot A.
+ EFIPayload structfs.Blob
+ // SystemImage provides contents of the Metropolis system partition.
+ // If nil, no contents will be copied into the partition.
+ SystemImage structfs.Blob
+ // NodeParameters provides contents of the node parameters file. If nil,
+ // the node parameters file won't be created in the target ESP
+ // filesystem.
+ NodeParameters structfs.Blob
+ // DiskGUID is a unique identifier of the image and a part of Table
+ // header. It's optional and can be left blank if the identifier is
+ // to be randomly generated. Setting it to a predetermined value can
+ // help in implementing reproducible builds.
+ DiskGUID uuid.UUID
+ // PartitionSize specifies a size for the ESP, Metropolis System and
+ // Metropolis data partition.
+ PartitionSize PartitionSizeInfo
+ // BIOSBootCode provides the optional contents for the protective MBR
+ // block which gets executed by legacy BIOS boot.
+ BIOSBootCode []byte
+}
+
+type plan struct {
+ *Params
+ efiBootPath string
+ efiRoot structfs.Tree
+ tbl *gpt.Table
+ efiPartition *gpt.Partition
+ systemPartitionA *gpt.Partition
+ systemPartitionB *gpt.Partition
+ dataPartition *gpt.Partition
+}
+
+// Apply actually writes the planned installation to the blockdevice.
+func (i *plan) Apply() (*efivarfs.LoadOption, error) {
+ // Discard the entire device, we're going to write new data over it.
+ // Ignore errors, this is only advisory.
+ i.Output.Discard(0, i.Output.BlockCount()*i.Output.BlockSize())
+
+ if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.efiRoot, fat32.Options{
+ BlockSize: uint16(i.efiPartition.BlockSize()),
+ BlockCount: uint32(i.efiPartition.BlockCount()),
+ Label: "MNGN_BOOT",
+ }); err != nil {
+ return nil, fmt.Errorf("failed to write FAT32: %w", err)
+ }
+
+ systemImage, err := i.SystemImage.Open()
+ if err != nil {
+ return nil, fmt.Errorf("failed to open system image: %w", err)
+ }
+ if _, err := io.CopyN(blockdev.NewRWS(i.systemPartitionA), systemImage, i.SystemImage.Size()); err != nil {
+ systemImage.Close()
+ return nil, fmt.Errorf("failed to write system partition A: %w", err)
+ }
+ systemImage.Close()
+
+ if err := i.tbl.Write(); err != nil {
+ return nil, fmt.Errorf("failed to write Table: %w", err)
+ }
+
+ // Build an EFI boot entry pointing to the image's ESP.
+ return &efivarfs.LoadOption{
+ Description: "Metropolis",
+ FilePath: efivarfs.DevicePath{
+ &efivarfs.HardDrivePath{
+ PartitionNumber: 1,
+ PartitionStartBlock: i.efiPartition.FirstBlock,
+ PartitionSizeBlocks: i.efiPartition.SizeBlocks(),
+ PartitionMatch: efivarfs.PartitionGPT{
+ PartitionUUID: i.efiPartition.ID,
+ },
+ },
+ efivarfs.FilePath("/" + i.efiBootPath),
+ },
+ }, nil
+}
+
+// Plan allows to prepare an installation without modifying any data on the
+// system. To apply the planned installation, call Apply on the returned plan.
+func Plan(p *Params) (*plan, error) {
+ params := &plan{Params: p}
+
+ var err error
+ params.tbl, err = gpt.New(params.Output)
+ if err != nil {
+ return nil, fmt.Errorf("invalid block device: %w", err)
+ }
+
+ params.tbl.ID = params.DiskGUID
+ params.tbl.BootCode = p.BIOSBootCode
+ params.efiPartition = &gpt.Partition{
+ Type: gpt.PartitionTypeEFISystem,
+ Name: ESPLabel,
+ }
+
+ if err := params.tbl.AddPartition(params.efiPartition, params.PartitionSize.ESP*Mi); err != nil {
+ return nil, fmt.Errorf("failed to allocate ESP: %w", err)
+ }
+
+ if err := params.efiRoot.PlaceFile(EFIBootAPath, params.EFIPayload); err != nil {
+ return nil, err
+ }
+ // Place the A/B loader at the EFI bootloader autodiscovery path.
+ params.efiBootPath, err = EFIBootPath(p.Architecture)
+ if err != nil {
+ return nil, err
+ }
+ if err := params.efiRoot.PlaceFile(params.efiBootPath, params.ABLoader); err != nil {
+ return nil, err
+ }
+ if params.NodeParameters != nil {
+ if err := params.efiRoot.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil {
+ return nil, err
+ }
+ }
+
+ // Try to layout the fat32 partition. If it detects that the disk is too
+ // small, an error will be returned.
+ if _, err := fat32.SizeFS(params.efiRoot, fat32.Options{
+ BlockSize: uint16(params.efiPartition.BlockSize()),
+ BlockCount: uint32(params.efiPartition.BlockCount()),
+ Label: "MNGN_BOOT",
+ }); err != nil {
+ return nil, fmt.Errorf("failed to calculate size of FAT32: %w", err)
+ }
+
+ // Create the system partition only if its size is specified.
+ if params.PartitionSize.System != 0 && params.SystemImage != nil {
+ params.systemPartitionA = &gpt.Partition{
+ Type: SystemAType,
+ Name: SystemALabel,
+ }
+ if err := params.tbl.AddPartition(params.systemPartitionA, params.PartitionSize.System*Mi); err != nil {
+ return nil, fmt.Errorf("failed to allocate system partition A: %w", err)
+ }
+ params.systemPartitionB = &gpt.Partition{
+ Type: SystemBType,
+ Name: SystemBLabel,
+ }
+ if err := params.tbl.AddPartition(params.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")
+ }
+ // Create the data partition only if its size is specified.
+ if params.PartitionSize.Data != 0 {
+ params.dataPartition = &gpt.Partition{
+ Type: DataType,
+ Name: DataLabel,
+ }
+ if err := params.tbl.AddPartition(params.dataPartition, -1); err != nil {
+ return nil, fmt.Errorf("failed to allocate data partition: %w", err)
+ }
+ }
+
+ return params, nil
+}
+
+const Mi = 1024 * 1024
+
+// Write installs Metropolis OS to a block device.
+func Write(params *Params) (*efivarfs.LoadOption, error) {
+ p, err := Plan(params)
+ if err != nil {
+ return nil, err
+ }
+
+ return p.Apply()
+}
diff --git a/metropolis/installer/main.go b/metropolis/installer/main.go
index 20a8240..ac6152f 100644
--- a/metropolis/installer/main.go
+++ b/metropolis/installer/main.go
@@ -18,12 +18,12 @@
"golang.org/x/sys/unix"
+ "source.monogon.dev/metropolis/installer/install"
"source.monogon.dev/osbase/blockdev"
"source.monogon.dev/osbase/bringup"
- "source.monogon.dev/osbase/build/mkimage/osimage"
"source.monogon.dev/osbase/efivarfs"
"source.monogon.dev/osbase/oci"
- ociosimage "source.monogon.dev/osbase/oci/osimage"
+ "source.monogon.dev/osbase/oci/osimage"
"source.monogon.dev/osbase/structfs"
"source.monogon.dev/osbase/supervisor"
"source.monogon.dev/osbase/sysfs"
@@ -159,7 +159,7 @@
if err != nil {
return fmt.Errorf("failed to read OS image from ESP: %w", err)
}
- osImage, err := ociosimage.Read(ociImage)
+ osImage, err := osimage.Read(ociImage)
if err != nil {
return fmt.Errorf("failed to read OS image from ESP: %w", err)
}
@@ -173,16 +173,16 @@
return fmt.Errorf("cannot open system image in OS image: %w", err)
}
- // Build the osimage parameters.
- installParams := osimage.Params{
- PartitionSize: osimage.PartitionSizeInfo{
+ // Build the install parameters.
+ installParams := install.Params{
+ PartitionSize: install.PartitionSizeInfo{
// ESP is the size of the node ESP partition, expressed in mebibytes.
ESP: 384,
// System is the size of the node system partition, expressed in
// mebibytes.
System: 4096,
// Data must be nonzero in order for the data partition to be created.
- // osimage will extend the data partition to fill all the available space
+ // install will extend the data partition to fill all the available space
// whenever it's writing to block devices, such as now.
Data: 128,
},
@@ -207,7 +207,7 @@
}
// Set the first suitable block device found as the installation target.
tgtBlkdevName := blkDevs[0]
- // Update the osimage parameters with a path pointing at the target device.
+ // Update the install parameters with a path pointing at the target device.
tgtBlkdevPath := filepath.Join("/dev", tgtBlkdevName)
tgtBlockDev, err := blockdev.Open(tgtBlkdevPath)
@@ -216,10 +216,10 @@
}
installParams.Output = tgtBlockDev
- // Use osimage to partition the target block device and set up its ESP.
+ // Use install to partition the target block device and set up its ESP.
// Write will return an EFI boot entry on success.
l.Infof("Installing to %s...", tgtBlkdevPath)
- be, err := osimage.Write(&installParams)
+ be, err := install.Write(&installParams)
if err != nil {
return fmt.Errorf("while installing: %w", err)
}
diff --git a/metropolis/installer/test/BUILD.bazel b/metropolis/installer/test/BUILD.bazel
index d68f78a..a5e8973 100644
--- a/metropolis/installer/test/BUILD.bazel
+++ b/metropolis/installer/test/BUILD.bazel
@@ -21,8 +21,8 @@
},
deps = [
"//metropolis/cli/metroctl/core",
+ "//metropolis/installer/install",
"//metropolis/proto/api",
- "//osbase/build/mkimage/osimage",
"//osbase/cmd",
"//osbase/oci",
"//osbase/oci/osimage",
diff --git a/metropolis/installer/test/run_test.go b/metropolis/installer/test/run_test.go
index bba323f..9d9d136 100644
--- a/metropolis/installer/test/run_test.go
+++ b/metropolis/installer/test/run_test.go
@@ -24,10 +24,10 @@
"source.monogon.dev/metropolis/proto/api"
mctl "source.monogon.dev/metropolis/cli/metroctl/core"
- "source.monogon.dev/osbase/build/mkimage/osimage"
+ "source.monogon.dev/metropolis/installer/install"
"source.monogon.dev/osbase/cmd"
"source.monogon.dev/osbase/oci"
- ociosimage "source.monogon.dev/osbase/oci/osimage"
+ "source.monogon.dev/osbase/oci/osimage"
"source.monogon.dev/osbase/structfs"
)
@@ -147,11 +147,11 @@
log.Fatal(err)
}
- osImage, err := ociosimage.Read(image)
+ osImage, err := osimage.Read(image)
if err != nil {
log.Fatal(err)
}
- bootPath, err = osimage.EFIBootPath(osImage.Config.ProductInfo.Architecture())
+ bootPath, err = install.EFIBootPath(osImage.Config.ProductInfo.Architecture())
if err != nil {
log.Fatal(err)
}
@@ -284,22 +284,22 @@
// Check that the first partition is likely to be a valid ESP.
pi := ti.GetPartitions()
esp := (pi[0]).(*gpt.Partition)
- if esp.Name != osimage.ESPLabel || esp.Start == 0 || esp.End == 0 {
+ if esp.Name != install.ESPLabel || esp.Start == 0 || esp.End == 0 {
t.Fatal("The node's ESP GPT entry looks off.")
}
// Verify the system partition's GPT entry.
system := (pi[1]).(*gpt.Partition)
- if system.Name != osimage.SystemALabel || system.Start == 0 || system.End == 0 {
+ if system.Name != install.SystemALabel || system.Start == 0 || system.End == 0 {
t.Fatal("The node's system partition GPT entry looks off.")
}
// Verify the system partition's GPT entry.
systemB := (pi[2]).(*gpt.Partition)
- if systemB.Name != osimage.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
+ if systemB.Name != install.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
t.Fatal("The node's system partition GPT entry looks off.")
}
// Verify the data partition's GPT entry.
data := (pi[3]).(*gpt.Partition)
- if data.Name != osimage.DataLabel || data.Start == 0 || data.End == 0 {
+ if data.Name != install.DataLabel || data.Start == 0 || data.End == 0 {
t.Fatalf("The node's data partition GPT entry looks off: %+v", data)
}
// Verify that there are no more partitions.