blob: a2b9ff892ebe5362fd79b04ad4fbf64b1ec34413 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Mateusz Zalegac71efc92021-09-07 16:46:25 +02002// SPDX-License-Identifier: Apache-2.0
Mateusz Zalegac71efc92021-09-07 16:46:25 +02003
Jan Schäre19d2792025-06-23 12:37:58 +00004// Package install allows planning and executing the installation of Metropolis
5// to a block device.
6package install
Mateusz Zalegac71efc92021-09-07 16:46:25 +02007
8import (
9 "fmt"
10 "io"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020011
Mateusz Zalega612a0332021-11-17 20:04:52 +010012 "github.com/google/uuid"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020013
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020014 "source.monogon.dev/osbase/blockdev"
15 "source.monogon.dev/osbase/efivarfs"
16 "source.monogon.dev/osbase/fat32"
17 "source.monogon.dev/osbase/gpt"
Jan Schärc1b6df42025-03-20 08:52:18 +000018 "source.monogon.dev/osbase/structfs"
Lorenz Brunad131882023-06-28 16:42:20 +020019)
20
21var (
22 SystemAType = uuid.MustParse("ee96054b-f6d0-4267-aaaa-724b2afea74c")
23 SystemBType = uuid.MustParse("ee96054b-f6d0-4267-bbbb-724b2afea74c")
24
25 DataType = uuid.MustParse("9eeec464-6885-414a-b278-4305c51f7966")
Mateusz Zalegac71efc92021-09-07 16:46:25 +020026)
27
28const (
Lorenz Brun35fcf032023-06-29 04:15:58 +020029 SystemALabel = "METROPOLIS-SYSTEM-A"
30 SystemBLabel = "METROPOLIS-SYSTEM-B"
31 DataLabel = "METROPOLIS-NODE-DATA"
32 ESPLabel = "ESP"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020033
Jan Schär091b8a62025-05-13 09:07:50 +000034 EFIBootAPath = "EFI/metropolis/boot-a.efi"
35 EFIBootBPath = "EFI/metropolis/boot-b.efi"
Lorenz Brunad131882023-06-28 16:42:20 +020036 nodeParamsPath = "metropolis/parameters.pb"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020037)
38
Jan Schär4b888262025-05-13 09:12:03 +000039var EFIBootName = map[string]string{
40 "x86_64": "BOOTx64.EFI",
41 "aarch64": "BOOTAA64.EFI",
42}
43
44// EFIBootPath returns the default file path according to the UEFI Specification
45// v2.11 Section 3.5.1.1. This file is booted by any compliant UEFI firmware in
46// absence of another bootable boot entry.
47func EFIBootPath(architecture string) (string, error) {
48 bootName, ok := EFIBootName[architecture]
49 if !ok {
50 return "", fmt.Errorf("unsupported architecture %q", architecture)
51 }
52 return "EFI/BOOT/" + bootName, nil
53}
54
Mateusz Zalegac71efc92021-09-07 16:46:25 +020055// PartitionSizeInfo contains parameters used during partition table
56// initialization and, in case of image files, space allocation.
57type PartitionSizeInfo struct {
58 // Size of the EFI System Partition (ESP), in mebibytes. The size must
59 // not be zero.
Lorenz Brunad131882023-06-28 16:42:20 +020060 ESP int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020061 // Size of the Metropolis system partition, in mebibytes. The partition
62 // won't be created if the size is zero.
Lorenz Brunad131882023-06-28 16:42:20 +020063 System int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020064 // Size of the Metropolis data partition, in mebibytes. The partition
65 // won't be created if the size is zero. If the image is output to a
66 // block device, the partition will be extended to fill the remaining
67 // space.
Lorenz Brunad131882023-06-28 16:42:20 +020068 Data int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020069}
70
Jan Schäre19d2792025-06-23 12:37:58 +000071// Params contains parameters used by Plan or Write to install Metropolis OS.
Mateusz Zalegac71efc92021-09-07 16:46:25 +020072type Params struct {
Jan Schäre19d2792025-06-23 12:37:58 +000073 // Output is the block device to which the OS is installed.
Lorenz Brunad131882023-06-28 16:42:20 +020074 Output blockdev.BlockDev
Jan Schär4b888262025-05-13 09:12:03 +000075 // Architecture is the CPU architecture of the OS image.
76 Architecture string
Lorenz Brun54a5a052023-10-02 16:40:11 +020077 // ABLoader provides the A/B loader which then loads the EFI loader for the
78 // correct slot.
Jan Schärc1b6df42025-03-20 08:52:18 +000079 ABLoader structfs.Blob
Mateusz Zalegac71efc92021-09-07 16:46:25 +020080 // EFIPayload provides contents of the EFI payload file. It must not be
Lorenz Brun54a5a052023-10-02 16:40:11 +020081 // nil. This gets put into boot slot A.
Jan Schärc1b6df42025-03-20 08:52:18 +000082 EFIPayload structfs.Blob
Mateusz Zalegac71efc92021-09-07 16:46:25 +020083 // SystemImage provides contents of the Metropolis system partition.
84 // If nil, no contents will be copied into the partition.
Jan Schärc1b6df42025-03-20 08:52:18 +000085 SystemImage structfs.Blob
Mateusz Zalegac71efc92021-09-07 16:46:25 +020086 // NodeParameters provides contents of the node parameters file. If nil,
87 // the node parameters file won't be created in the target ESP
88 // filesystem.
Jan Schärc1b6df42025-03-20 08:52:18 +000089 NodeParameters structfs.Blob
Lorenz Brunad131882023-06-28 16:42:20 +020090 // DiskGUID is a unique identifier of the image and a part of Table
Mateusz Zalegac71efc92021-09-07 16:46:25 +020091 // header. It's optional and can be left blank if the identifier is
92 // to be randomly generated. Setting it to a predetermined value can
93 // help in implementing reproducible builds.
Lorenz Brunad131882023-06-28 16:42:20 +020094 DiskGUID uuid.UUID
Mateusz Zalegac71efc92021-09-07 16:46:25 +020095 // PartitionSize specifies a size for the ESP, Metropolis System and
96 // Metropolis data partition.
97 PartitionSize PartitionSizeInfo
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +000098 // BIOSBootCode provides the optional contents for the protective MBR
99 // block which gets executed by legacy BIOS boot.
100 BIOSBootCode []byte
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200101}
102
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200103type plan struct {
104 *Params
Jan Schär4b888262025-05-13 09:12:03 +0000105 efiBootPath string
Jan Schärc1b6df42025-03-20 08:52:18 +0000106 efiRoot structfs.Tree
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200107 tbl *gpt.Table
108 efiPartition *gpt.Partition
109 systemPartitionA *gpt.Partition
110 systemPartitionB *gpt.Partition
111 dataPartition *gpt.Partition
112}
Lorenz Brunad131882023-06-28 16:42:20 +0200113
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200114// Apply actually writes the planned installation to the blockdevice.
115func (i *plan) Apply() (*efivarfs.LoadOption, error) {
Lorenz Brunad131882023-06-28 16:42:20 +0200116 // Discard the entire device, we're going to write new data over it.
117 // Ignore errors, this is only advisory.
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200118 i.Output.Discard(0, i.Output.BlockCount()*i.Output.BlockSize())
Lorenz Brunad131882023-06-28 16:42:20 +0200119
Jan Schärc1b6df42025-03-20 08:52:18 +0000120 if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.efiRoot, fat32.Options{
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200121 BlockSize: uint16(i.efiPartition.BlockSize()),
122 BlockCount: uint32(i.efiPartition.BlockCount()),
Lorenz Brunad131882023-06-28 16:42:20 +0200123 Label: "MNGN_BOOT",
124 }); err != nil {
125 return nil, fmt.Errorf("failed to write FAT32: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200126 }
Lorenz Brunad131882023-06-28 16:42:20 +0200127
Jan Schärc1b6df42025-03-20 08:52:18 +0000128 systemImage, err := i.SystemImage.Open()
129 if err != nil {
130 return nil, fmt.Errorf("failed to open system image: %w", err)
131 }
Jan Schäre0db72c2025-06-18 18:14:07 +0000132 if _, err := io.CopyN(blockdev.NewRWS(i.systemPartitionA), systemImage, i.SystemImage.Size()); err != nil {
Jan Schärc1b6df42025-03-20 08:52:18 +0000133 systemImage.Close()
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200134 return nil, fmt.Errorf("failed to write system partition A: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200135 }
Jan Schärc1b6df42025-03-20 08:52:18 +0000136 systemImage.Close()
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200137
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200138 if err := i.tbl.Write(); err != nil {
Lorenz Brunad131882023-06-28 16:42:20 +0200139 return nil, fmt.Errorf("failed to write Table: %w", err)
Mateusz Zalega612a0332021-11-17 20:04:52 +0100140 }
Lorenz Brunad131882023-06-28 16:42:20 +0200141
142 // Build an EFI boot entry pointing to the image's ESP.
143 return &efivarfs.LoadOption{
Lorenz Brun54a5a052023-10-02 16:40:11 +0200144 Description: "Metropolis",
Lorenz Brunca1cff02023-06-26 17:52:44 +0200145 FilePath: efivarfs.DevicePath{
146 &efivarfs.HardDrivePath{
147 PartitionNumber: 1,
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200148 PartitionStartBlock: i.efiPartition.FirstBlock,
149 PartitionSizeBlocks: i.efiPartition.SizeBlocks(),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200150 PartitionMatch: efivarfs.PartitionGPT{
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200151 PartitionUUID: i.efiPartition.ID,
Lorenz Brunca1cff02023-06-26 17:52:44 +0200152 },
153 },
Jan Schär4b888262025-05-13 09:12:03 +0000154 efivarfs.FilePath("/" + i.efiBootPath),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200155 },
Lorenz Brunad131882023-06-28 16:42:20 +0200156 }, nil
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200157}
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200158
159// Plan allows to prepare an installation without modifying any data on the
160// system. To apply the planned installation, call Apply on the returned plan.
161func Plan(p *Params) (*plan, error) {
162 params := &plan{Params: p}
163
164 var err error
165 params.tbl, err = gpt.New(params.Output)
166 if err != nil {
167 return nil, fmt.Errorf("invalid block device: %w", err)
168 }
169
170 params.tbl.ID = params.DiskGUID
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +0000171 params.tbl.BootCode = p.BIOSBootCode
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200172 params.efiPartition = &gpt.Partition{
173 Type: gpt.PartitionTypeEFISystem,
174 Name: ESPLabel,
175 }
176
177 if err := params.tbl.AddPartition(params.efiPartition, params.PartitionSize.ESP*Mi); err != nil {
178 return nil, fmt.Errorf("failed to allocate ESP: %w", err)
179 }
180
Jan Schär091b8a62025-05-13 09:07:50 +0000181 if err := params.efiRoot.PlaceFile(EFIBootAPath, params.EFIPayload); err != nil {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200182 return nil, err
183 }
184 // Place the A/B loader at the EFI bootloader autodiscovery path.
Jan Schär4b888262025-05-13 09:12:03 +0000185 params.efiBootPath, err = EFIBootPath(p.Architecture)
186 if err != nil {
187 return nil, err
188 }
189 if err := params.efiRoot.PlaceFile(params.efiBootPath, params.ABLoader); err != nil {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200190 return nil, err
191 }
192 if params.NodeParameters != nil {
Jan Schärc1b6df42025-03-20 08:52:18 +0000193 if err := params.efiRoot.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200194 return nil, err
195 }
196 }
197
198 // Try to layout the fat32 partition. If it detects that the disk is too
199 // small, an error will be returned.
Jan Schärc1b6df42025-03-20 08:52:18 +0000200 if _, err := fat32.SizeFS(params.efiRoot, fat32.Options{
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200201 BlockSize: uint16(params.efiPartition.BlockSize()),
202 BlockCount: uint32(params.efiPartition.BlockCount()),
203 Label: "MNGN_BOOT",
204 }); err != nil {
205 return nil, fmt.Errorf("failed to calculate size of FAT32: %w", err)
206 }
207
208 // Create the system partition only if its size is specified.
209 if params.PartitionSize.System != 0 && params.SystemImage != nil {
210 params.systemPartitionA = &gpt.Partition{
211 Type: SystemAType,
212 Name: SystemALabel,
213 }
214 if err := params.tbl.AddPartition(params.systemPartitionA, params.PartitionSize.System*Mi); err != nil {
215 return nil, fmt.Errorf("failed to allocate system partition A: %w", err)
216 }
217 params.systemPartitionB = &gpt.Partition{
218 Type: SystemBType,
219 Name: SystemBLabel,
220 }
221 if err := params.tbl.AddPartition(params.systemPartitionB, params.PartitionSize.System*Mi); err != nil {
222 return nil, fmt.Errorf("failed to allocate system partition B: %w", err)
223 }
224 } else if params.PartitionSize.System == 0 && params.SystemImage != nil {
225 // Safeguard against contradicting parameters.
226 return nil, fmt.Errorf("the system image parameter was passed while the associated partition size is zero")
227 }
228 // Create the data partition only if its size is specified.
229 if params.PartitionSize.Data != 0 {
230 params.dataPartition = &gpt.Partition{
231 Type: DataType,
232 Name: DataLabel,
233 }
234 if err := params.tbl.AddPartition(params.dataPartition, -1); err != nil {
235 return nil, fmt.Errorf("failed to allocate data partition: %w", err)
236 }
237 }
238
239 return params, nil
240}
241
242const Mi = 1024 * 1024
243
Jan Schäre19d2792025-06-23 12:37:58 +0000244// Write installs Metropolis OS to a block device.
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +0200245func Write(params *Params) (*efivarfs.LoadOption, error) {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200246 p, err := Plan(params)
247 if err != nil {
248 return nil, err
249 }
250
251 return p.Apply()
252}