blob: 71b54995e90e580c7bb09df0c43e81c797b732d2 [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"
Lorenz Brunad131882023-06-28 16:42:20 +020011 "strings"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020012
Mateusz Zalega612a0332021-11-17 20:04:52 +010013 "github.com/google/uuid"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020014
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020015 "source.monogon.dev/osbase/blockdev"
16 "source.monogon.dev/osbase/efivarfs"
17 "source.monogon.dev/osbase/fat32"
18 "source.monogon.dev/osbase/gpt"
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
34 EFIPayloadPath = "/EFI/BOOT/BOOTx64.EFI"
Lorenz Brun35fcf032023-06-29 04:15:58 +020035 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.
63 ABLoader fat32.SizedReader
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.
Lorenz Brunad131882023-06-28 16:42:20 +020066 EFIPayload fat32.SizedReader
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.
69 SystemImage io.Reader
70 // 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.
Lorenz Brunad131882023-06-28 16:42:20 +020073 NodeParameters fat32.SizedReader
74 // 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
89 rootInode fat32.Inode
90 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
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200103 if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.rootInode, fat32.Options{
104 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
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200111 if _, err := io.Copy(blockdev.NewRWS(i.systemPartitionA), i.SystemImage); err != nil {
112 return nil, fmt.Errorf("failed to write system partition A: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200113 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200114
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200115 if err := i.tbl.Write(); err != nil {
Lorenz Brunad131882023-06-28 16:42:20 +0200116 return nil, fmt.Errorf("failed to write Table: %w", err)
Mateusz Zalega612a0332021-11-17 20:04:52 +0100117 }
Lorenz Brunad131882023-06-28 16:42:20 +0200118
119 // Build an EFI boot entry pointing to the image's ESP.
120 return &efivarfs.LoadOption{
Lorenz Brun54a5a052023-10-02 16:40:11 +0200121 Description: "Metropolis",
Lorenz Brunca1cff02023-06-26 17:52:44 +0200122 FilePath: efivarfs.DevicePath{
123 &efivarfs.HardDrivePath{
124 PartitionNumber: 1,
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200125 PartitionStartBlock: i.efiPartition.FirstBlock,
126 PartitionSizeBlocks: i.efiPartition.SizeBlocks(),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200127 PartitionMatch: efivarfs.PartitionGPT{
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200128 PartitionUUID: i.efiPartition.ID,
Lorenz Brunca1cff02023-06-26 17:52:44 +0200129 },
130 },
Lorenz Brun54a5a052023-10-02 16:40:11 +0200131 efivarfs.FilePath(EFIPayloadPath),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200132 },
Lorenz Brunad131882023-06-28 16:42:20 +0200133 }, nil
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200134}
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200135
136// Plan allows to prepare an installation without modifying any data on the
137// system. To apply the planned installation, call Apply on the returned plan.
138func Plan(p *Params) (*plan, error) {
139 params := &plan{Params: p}
140
141 var err error
142 params.tbl, err = gpt.New(params.Output)
143 if err != nil {
144 return nil, fmt.Errorf("invalid block device: %w", err)
145 }
146
147 params.tbl.ID = params.DiskGUID
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +0000148 params.tbl.BootCode = p.BIOSBootCode
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200149 params.efiPartition = &gpt.Partition{
150 Type: gpt.PartitionTypeEFISystem,
151 Name: ESPLabel,
152 }
153
154 if err := params.tbl.AddPartition(params.efiPartition, params.PartitionSize.ESP*Mi); err != nil {
155 return nil, fmt.Errorf("failed to allocate ESP: %w", err)
156 }
157
158 params.rootInode = fat32.Inode{
159 Attrs: fat32.AttrDirectory,
160 }
161 if err := params.rootInode.PlaceFile(strings.TrimPrefix(EFIBootAPath, "/"), params.EFIPayload); err != nil {
162 return nil, err
163 }
164 // Place the A/B loader at the EFI bootloader autodiscovery path.
165 if err := params.rootInode.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), params.ABLoader); err != nil {
166 return nil, err
167 }
168 if params.NodeParameters != nil {
169 if err := params.rootInode.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil {
170 return nil, err
171 }
172 }
173
174 // Try to layout the fat32 partition. If it detects that the disk is too
175 // small, an error will be returned.
176 if _, err := fat32.SizeFS(params.rootInode, fat32.Options{
177 BlockSize: uint16(params.efiPartition.BlockSize()),
178 BlockCount: uint32(params.efiPartition.BlockCount()),
179 Label: "MNGN_BOOT",
180 }); err != nil {
181 return nil, fmt.Errorf("failed to calculate size of FAT32: %w", err)
182 }
183
184 // Create the system partition only if its size is specified.
185 if params.PartitionSize.System != 0 && params.SystemImage != nil {
186 params.systemPartitionA = &gpt.Partition{
187 Type: SystemAType,
188 Name: SystemALabel,
189 }
190 if err := params.tbl.AddPartition(params.systemPartitionA, params.PartitionSize.System*Mi); err != nil {
191 return nil, fmt.Errorf("failed to allocate system partition A: %w", err)
192 }
193 params.systemPartitionB = &gpt.Partition{
194 Type: SystemBType,
195 Name: SystemBLabel,
196 }
197 if err := params.tbl.AddPartition(params.systemPartitionB, params.PartitionSize.System*Mi); err != nil {
198 return nil, fmt.Errorf("failed to allocate system partition B: %w", err)
199 }
200 } else if params.PartitionSize.System == 0 && params.SystemImage != nil {
201 // Safeguard against contradicting parameters.
202 return nil, fmt.Errorf("the system image parameter was passed while the associated partition size is zero")
203 }
204 // Create the data partition only if its size is specified.
205 if params.PartitionSize.Data != 0 {
206 params.dataPartition = &gpt.Partition{
207 Type: DataType,
208 Name: DataLabel,
209 }
210 if err := params.tbl.AddPartition(params.dataPartition, -1); err != nil {
211 return nil, fmt.Errorf("failed to allocate data partition: %w", err)
212 }
213 }
214
215 return params, nil
216}
217
218const Mi = 1024 * 1024
219
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +0200220// Write writes a Metropolis OS image to a block device.
221func Write(params *Params) (*efivarfs.LoadOption, error) {
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200222 p, err := Plan(params)
223 if err != nil {
224 return nil, err
225 }
226
227 return p.Apply()
228}