blob: 4edb077e4df508bb5038bfee4059ed1d14f094a3 [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
4// This package provides self-contained implementation used to generate
5// Metropolis disk images.
6package osimage
7
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 EFIPayloadPath = "EFI/BOOT/BOOTx64.EFI"
35 EFIBootAPath = "EFI/metropolis/boot-a.efi"
36 EFIBootBPath = "EFI/metropolis/boot-b.efi"
Lorenz Brunad131882023-06-28 16:42:20 +020037 nodeParamsPath = "metropolis/parameters.pb"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020038)
39
Mateusz Zalegac71efc92021-09-07 16:46:25 +020040// PartitionSizeInfo contains parameters used during partition table
41// initialization and, in case of image files, space allocation.
42type PartitionSizeInfo struct {
43 // Size of the EFI System Partition (ESP), in mebibytes. The size must
44 // not be zero.
Lorenz Brunad131882023-06-28 16:42:20 +020045 ESP int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020046 // Size of the Metropolis system partition, in mebibytes. The partition
47 // won't be created if the size is zero.
Lorenz Brunad131882023-06-28 16:42:20 +020048 System int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020049 // Size of the Metropolis data partition, in mebibytes. The partition
50 // won't be created if the size is zero. If the image is output to a
51 // block device, the partition will be extended to fill the remaining
52 // space.
Lorenz Brunad131882023-06-28 16:42:20 +020053 Data int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020054}
55
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +020056// Params contains parameters used by Plan or Write to build a Metropolis OS
Mateusz Zalegac71efc92021-09-07 16:46:25 +020057// image.
58type Params struct {
Lorenz Brunad131882023-06-28 16:42:20 +020059 // Output is the block device to which the OS image is written.
60 Output blockdev.BlockDev
Lorenz Brun54a5a052023-10-02 16:40:11 +020061 // ABLoader provides the A/B loader which then loads the EFI loader for the
62 // correct slot.
Jan Schärc1b6df42025-03-20 08:52:18 +000063 ABLoader structfs.Blob
Mateusz Zalegac71efc92021-09-07 16:46:25 +020064 // EFIPayload provides contents of the EFI payload file. It must not be
Lorenz Brun54a5a052023-10-02 16:40:11 +020065 // nil. This gets put into boot slot A.
Jan Schärc1b6df42025-03-20 08:52:18 +000066 EFIPayload structfs.Blob
Mateusz Zalegac71efc92021-09-07 16:46:25 +020067 // SystemImage provides contents of the Metropolis system partition.
68 // If nil, no contents will be copied into the partition.
Jan Schärc1b6df42025-03-20 08:52:18 +000069 SystemImage structfs.Blob
Mateusz Zalegac71efc92021-09-07 16:46:25 +020070 // NodeParameters provides contents of the node parameters file. If nil,
71 // the node parameters file won't be created in the target ESP
72 // filesystem.
Jan Schärc1b6df42025-03-20 08:52:18 +000073 NodeParameters structfs.Blob
Lorenz Brunad131882023-06-28 16:42:20 +020074 // DiskGUID is a unique identifier of the image and a part of Table
Mateusz Zalegac71efc92021-09-07 16:46:25 +020075 // header. It's optional and can be left blank if the identifier is
76 // to be randomly generated. Setting it to a predetermined value can
77 // help in implementing reproducible builds.
Lorenz Brunad131882023-06-28 16:42:20 +020078 DiskGUID uuid.UUID
Mateusz Zalegac71efc92021-09-07 16:46:25 +020079 // PartitionSize specifies a size for the ESP, Metropolis System and
80 // Metropolis data partition.
81 PartitionSize PartitionSizeInfo
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +000082 // BIOSBootCode provides the optional contents for the protective MBR
83 // block which gets executed by legacy BIOS boot.
84 BIOSBootCode []byte
Mateusz Zalegac71efc92021-09-07 16:46:25 +020085}
86
Tim Windelschmidtbceb1602024-07-10 18:17:32 +020087type plan struct {
88 *Params
Jan Schärc1b6df42025-03-20 08:52:18 +000089 efiRoot structfs.Tree
Tim Windelschmidtbceb1602024-07-10 18:17:32 +020090 tbl *gpt.Table
91 efiPartition *gpt.Partition
92 systemPartitionA *gpt.Partition
93 systemPartitionB *gpt.Partition
94 dataPartition *gpt.Partition
95}
Lorenz Brunad131882023-06-28 16:42:20 +020096
Tim Windelschmidtbceb1602024-07-10 18:17:32 +020097// Apply actually writes the planned installation to the blockdevice.
98func (i *plan) Apply() (*efivarfs.LoadOption, error) {
Lorenz Brunad131882023-06-28 16:42:20 +020099 // Discard the entire device, we're going to write new data over it.
100 // Ignore errors, this is only advisory.
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200101 i.Output.Discard(0, i.Output.BlockCount()*i.Output.BlockSize())
Lorenz Brunad131882023-06-28 16:42:20 +0200102
Jan Schärc1b6df42025-03-20 08:52:18 +0000103 if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.efiRoot, fat32.Options{
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200104 BlockSize: uint16(i.efiPartition.BlockSize()),
105 BlockCount: uint32(i.efiPartition.BlockCount()),
Lorenz Brunad131882023-06-28 16:42:20 +0200106 Label: "MNGN_BOOT",
107 }); err != nil {
108 return nil, fmt.Errorf("failed to write FAT32: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200109 }
Lorenz Brunad131882023-06-28 16:42:20 +0200110
Jan Schärc1b6df42025-03-20 08:52:18 +0000111 systemImage, err := i.SystemImage.Open()
112 if err != nil {
113 return nil, fmt.Errorf("failed to open system image: %w", err)
114 }
115 if _, err := io.Copy(blockdev.NewRWS(i.systemPartitionA), systemImage); err != nil {
116 systemImage.Close()
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200117 return nil, fmt.Errorf("failed to write system partition A: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200118 }
Jan Schärc1b6df42025-03-20 08:52:18 +0000119 systemImage.Close()
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200120
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200121 if err := i.tbl.Write(); err != nil {
Lorenz Brunad131882023-06-28 16:42:20 +0200122 return nil, fmt.Errorf("failed to write Table: %w", err)
Mateusz Zalega612a0332021-11-17 20:04:52 +0100123 }
Lorenz Brunad131882023-06-28 16:42:20 +0200124
125 // Build an EFI boot entry pointing to the image's ESP.
126 return &efivarfs.LoadOption{
Lorenz Brun54a5a052023-10-02 16:40:11 +0200127 Description: "Metropolis",
Lorenz Brunca1cff02023-06-26 17:52:44 +0200128 FilePath: efivarfs.DevicePath{
129 &efivarfs.HardDrivePath{
130 PartitionNumber: 1,
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200131 PartitionStartBlock: i.efiPartition.FirstBlock,
132 PartitionSizeBlocks: i.efiPartition.SizeBlocks(),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200133 PartitionMatch: efivarfs.PartitionGPT{
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200134 PartitionUUID: i.efiPartition.ID,
Lorenz Brunca1cff02023-06-26 17:52:44 +0200135 },
136 },
Jan Schär091b8a62025-05-13 09:07:50 +0000137 efivarfs.FilePath("/" + EFIPayloadPath),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200138 },
Lorenz Brunad131882023-06-28 16:42:20 +0200139 }, nil
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200140}
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200141
142// Plan allows to prepare an installation without modifying any data on the
143// system. To apply the planned installation, call Apply on the returned plan.
144func Plan(p *Params) (*plan, error) {
145 params := &plan{Params: p}
146
147 var err error
148 params.tbl, err = gpt.New(params.Output)
149 if err != nil {
150 return nil, fmt.Errorf("invalid block device: %w", err)
151 }
152
153 params.tbl.ID = params.DiskGUID
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +0000154 params.tbl.BootCode = p.BIOSBootCode
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200155 params.efiPartition = &gpt.Partition{
156 Type: gpt.PartitionTypeEFISystem,
157 Name: ESPLabel,
158 }
159
160 if err := params.tbl.AddPartition(params.efiPartition, params.PartitionSize.ESP*Mi); err != nil {
161 return nil, fmt.Errorf("failed to allocate ESP: %w", err)
162 }
163
Jan Schär091b8a62025-05-13 09:07:50 +0000164 if err := params.efiRoot.PlaceFile(EFIBootAPath, params.EFIPayload); err != nil {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200165 return nil, err
166 }
167 // Place the A/B loader at the EFI bootloader autodiscovery path.
Jan Schär091b8a62025-05-13 09:07:50 +0000168 if err := params.efiRoot.PlaceFile(EFIPayloadPath, params.ABLoader); err != nil {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200169 return nil, err
170 }
171 if params.NodeParameters != nil {
Jan Schärc1b6df42025-03-20 08:52:18 +0000172 if err := params.efiRoot.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200173 return nil, err
174 }
175 }
176
177 // Try to layout the fat32 partition. If it detects that the disk is too
178 // small, an error will be returned.
Jan Schärc1b6df42025-03-20 08:52:18 +0000179 if _, err := fat32.SizeFS(params.efiRoot, fat32.Options{
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200180 BlockSize: uint16(params.efiPartition.BlockSize()),
181 BlockCount: uint32(params.efiPartition.BlockCount()),
182 Label: "MNGN_BOOT",
183 }); err != nil {
184 return nil, fmt.Errorf("failed to calculate size of FAT32: %w", err)
185 }
186
187 // Create the system partition only if its size is specified.
188 if params.PartitionSize.System != 0 && params.SystemImage != nil {
189 params.systemPartitionA = &gpt.Partition{
190 Type: SystemAType,
191 Name: SystemALabel,
192 }
193 if err := params.tbl.AddPartition(params.systemPartitionA, params.PartitionSize.System*Mi); err != nil {
194 return nil, fmt.Errorf("failed to allocate system partition A: %w", err)
195 }
196 params.systemPartitionB = &gpt.Partition{
197 Type: SystemBType,
198 Name: SystemBLabel,
199 }
200 if err := params.tbl.AddPartition(params.systemPartitionB, params.PartitionSize.System*Mi); err != nil {
201 return nil, fmt.Errorf("failed to allocate system partition B: %w", err)
202 }
203 } else if params.PartitionSize.System == 0 && params.SystemImage != nil {
204 // Safeguard against contradicting parameters.
205 return nil, fmt.Errorf("the system image parameter was passed while the associated partition size is zero")
206 }
207 // Create the data partition only if its size is specified.
208 if params.PartitionSize.Data != 0 {
209 params.dataPartition = &gpt.Partition{
210 Type: DataType,
211 Name: DataLabel,
212 }
213 if err := params.tbl.AddPartition(params.dataPartition, -1); err != nil {
214 return nil, fmt.Errorf("failed to allocate data partition: %w", err)
215 }
216 }
217
218 return params, nil
219}
220
221const Mi = 1024 * 1024
222
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +0200223// Write writes a Metropolis OS image to a block device.
224func Write(params *Params) (*efivarfs.LoadOption, error) {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200225 p, err := Plan(params)
226 if err != nil {
227 return nil, err
228 }
229
230 return p.Apply()
231}