blob: 8f7ab6fb4908e11859cffcc9442c785128b5b6c6 [file] [log] [blame]
Mateusz Zalegac71efc92021-09-07 16:46:25 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17// This package provides self-contained implementation used to generate
18// Metropolis disk images.
19package osimage
20
21import (
22 "fmt"
23 "io"
Lorenz Brunad131882023-06-28 16:42:20 +020024 "strings"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020025
Mateusz Zalega612a0332021-11-17 20:04:52 +010026 "github.com/google/uuid"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020027
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020028 "source.monogon.dev/osbase/blockdev"
29 "source.monogon.dev/osbase/efivarfs"
30 "source.monogon.dev/osbase/fat32"
31 "source.monogon.dev/osbase/gpt"
Lorenz Brunad131882023-06-28 16:42:20 +020032)
33
34var (
35 SystemAType = uuid.MustParse("ee96054b-f6d0-4267-aaaa-724b2afea74c")
36 SystemBType = uuid.MustParse("ee96054b-f6d0-4267-bbbb-724b2afea74c")
37
38 DataType = uuid.MustParse("9eeec464-6885-414a-b278-4305c51f7966")
Mateusz Zalegac71efc92021-09-07 16:46:25 +020039)
40
41const (
Lorenz Brun35fcf032023-06-29 04:15:58 +020042 SystemALabel = "METROPOLIS-SYSTEM-A"
43 SystemBLabel = "METROPOLIS-SYSTEM-B"
44 DataLabel = "METROPOLIS-NODE-DATA"
45 ESPLabel = "ESP"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020046
47 EFIPayloadPath = "/EFI/BOOT/BOOTx64.EFI"
Lorenz Brun35fcf032023-06-29 04:15:58 +020048 EFIBootAPath = "/EFI/metropolis/boot-a.efi"
49 EFIBootBPath = "/EFI/metropolis/boot-b.efi"
Lorenz Brunad131882023-06-28 16:42:20 +020050 nodeParamsPath = "metropolis/parameters.pb"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020051)
52
Mateusz Zalegac71efc92021-09-07 16:46:25 +020053// PartitionSizeInfo contains parameters used during partition table
54// initialization and, in case of image files, space allocation.
55type PartitionSizeInfo struct {
56 // Size of the EFI System Partition (ESP), in mebibytes. The size must
57 // not be zero.
Lorenz Brunad131882023-06-28 16:42:20 +020058 ESP int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020059 // Size of the Metropolis system partition, in mebibytes. The partition
60 // won't be created if the size is zero.
Lorenz Brunad131882023-06-28 16:42:20 +020061 System int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020062 // Size of the Metropolis data partition, in mebibytes. The partition
63 // won't be created if the size is zero. If the image is output to a
64 // block device, the partition will be extended to fill the remaining
65 // space.
Lorenz Brunad131882023-06-28 16:42:20 +020066 Data int64
Mateusz Zalegac71efc92021-09-07 16:46:25 +020067}
68
69// Params contains parameters used by Create to build a Metropolis OS
70// image.
71type Params struct {
Lorenz Brunad131882023-06-28 16:42:20 +020072 // Output is the block device to which the OS image is written.
73 Output blockdev.BlockDev
Lorenz Brun54a5a052023-10-02 16:40:11 +020074 // ABLoader provides the A/B loader which then loads the EFI loader for the
75 // correct slot.
76 ABLoader fat32.SizedReader
Mateusz Zalegac71efc92021-09-07 16:46:25 +020077 // EFIPayload provides contents of the EFI payload file. It must not be
Lorenz Brun54a5a052023-10-02 16:40:11 +020078 // nil. This gets put into boot slot A.
Lorenz Brunad131882023-06-28 16:42:20 +020079 EFIPayload fat32.SizedReader
Mateusz Zalegac71efc92021-09-07 16:46:25 +020080 // SystemImage provides contents of the Metropolis system partition.
81 // If nil, no contents will be copied into the partition.
82 SystemImage io.Reader
83 // NodeParameters provides contents of the node parameters file. If nil,
84 // the node parameters file won't be created in the target ESP
85 // filesystem.
Lorenz Brunad131882023-06-28 16:42:20 +020086 NodeParameters fat32.SizedReader
87 // DiskGUID is a unique identifier of the image and a part of Table
Mateusz Zalegac71efc92021-09-07 16:46:25 +020088 // header. It's optional and can be left blank if the identifier is
89 // to be randomly generated. Setting it to a predetermined value can
90 // help in implementing reproducible builds.
Lorenz Brunad131882023-06-28 16:42:20 +020091 DiskGUID uuid.UUID
Mateusz Zalegac71efc92021-09-07 16:46:25 +020092 // PartitionSize specifies a size for the ESP, Metropolis System and
93 // Metropolis data partition.
94 PartitionSize PartitionSizeInfo
95}
96
Tim Windelschmidtbceb1602024-07-10 18:17:32 +020097type plan struct {
98 *Params
99 rootInode fat32.Inode
100 tbl *gpt.Table
101 efiPartition *gpt.Partition
102 systemPartitionA *gpt.Partition
103 systemPartitionB *gpt.Partition
104 dataPartition *gpt.Partition
105}
Lorenz Brunad131882023-06-28 16:42:20 +0200106
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200107// Apply actually writes the planned installation to the blockdevice.
108func (i *plan) Apply() (*efivarfs.LoadOption, error) {
Lorenz Brunad131882023-06-28 16:42:20 +0200109 // Discard the entire device, we're going to write new data over it.
110 // Ignore errors, this is only advisory.
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200111 i.Output.Discard(0, i.Output.BlockCount()*i.Output.BlockSize())
Lorenz Brunad131882023-06-28 16:42:20 +0200112
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200113 if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.rootInode, fat32.Options{
114 BlockSize: uint16(i.efiPartition.BlockSize()),
115 BlockCount: uint32(i.efiPartition.BlockCount()),
Lorenz Brunad131882023-06-28 16:42:20 +0200116 Label: "MNGN_BOOT",
117 }); err != nil {
118 return nil, fmt.Errorf("failed to write FAT32: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200119 }
Lorenz Brunad131882023-06-28 16:42:20 +0200120
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200121 if _, err := io.Copy(blockdev.NewRWS(i.systemPartitionA), i.SystemImage); err != nil {
122 return nil, fmt.Errorf("failed to write system partition A: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200123 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200124
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200125 if err := i.tbl.Write(); err != nil {
Lorenz Brunad131882023-06-28 16:42:20 +0200126 return nil, fmt.Errorf("failed to write Table: %w", err)
Mateusz Zalega612a0332021-11-17 20:04:52 +0100127 }
Lorenz Brunad131882023-06-28 16:42:20 +0200128
129 // Build an EFI boot entry pointing to the image's ESP.
130 return &efivarfs.LoadOption{
Lorenz Brun54a5a052023-10-02 16:40:11 +0200131 Description: "Metropolis",
Lorenz Brunca1cff02023-06-26 17:52:44 +0200132 FilePath: efivarfs.DevicePath{
133 &efivarfs.HardDrivePath{
134 PartitionNumber: 1,
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200135 PartitionStartBlock: i.efiPartition.FirstBlock,
136 PartitionSizeBlocks: i.efiPartition.SizeBlocks(),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200137 PartitionMatch: efivarfs.PartitionGPT{
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200138 PartitionUUID: i.efiPartition.ID,
Lorenz Brunca1cff02023-06-26 17:52:44 +0200139 },
140 },
Lorenz Brun54a5a052023-10-02 16:40:11 +0200141 efivarfs.FilePath(EFIPayloadPath),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200142 },
Lorenz Brunad131882023-06-28 16:42:20 +0200143 }, nil
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200144}
Tim Windelschmidtbceb1602024-07-10 18:17:32 +0200145
146// Plan allows to prepare an installation without modifying any data on the
147// system. To apply the planned installation, call Apply on the returned plan.
148func Plan(p *Params) (*plan, error) {
149 params := &plan{Params: p}
150
151 var err error
152 params.tbl, err = gpt.New(params.Output)
153 if err != nil {
154 return nil, fmt.Errorf("invalid block device: %w", err)
155 }
156
157 params.tbl.ID = params.DiskGUID
158 params.efiPartition = &gpt.Partition{
159 Type: gpt.PartitionTypeEFISystem,
160 Name: ESPLabel,
161 }
162
163 if err := params.tbl.AddPartition(params.efiPartition, params.PartitionSize.ESP*Mi); err != nil {
164 return nil, fmt.Errorf("failed to allocate ESP: %w", err)
165 }
166
167 params.rootInode = fat32.Inode{
168 Attrs: fat32.AttrDirectory,
169 }
170 if err := params.rootInode.PlaceFile(strings.TrimPrefix(EFIBootAPath, "/"), params.EFIPayload); err != nil {
171 return nil, err
172 }
173 // Place the A/B loader at the EFI bootloader autodiscovery path.
174 if err := params.rootInode.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), params.ABLoader); err != nil {
175 return nil, err
176 }
177 if params.NodeParameters != nil {
178 if err := params.rootInode.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil {
179 return nil, err
180 }
181 }
182
183 // Try to layout the fat32 partition. If it detects that the disk is too
184 // small, an error will be returned.
185 if _, err := fat32.SizeFS(params.rootInode, fat32.Options{
186 BlockSize: uint16(params.efiPartition.BlockSize()),
187 BlockCount: uint32(params.efiPartition.BlockCount()),
188 Label: "MNGN_BOOT",
189 }); err != nil {
190 return nil, fmt.Errorf("failed to calculate size of FAT32: %w", err)
191 }
192
193 // Create the system partition only if its size is specified.
194 if params.PartitionSize.System != 0 && params.SystemImage != nil {
195 params.systemPartitionA = &gpt.Partition{
196 Type: SystemAType,
197 Name: SystemALabel,
198 }
199 if err := params.tbl.AddPartition(params.systemPartitionA, params.PartitionSize.System*Mi); err != nil {
200 return nil, fmt.Errorf("failed to allocate system partition A: %w", err)
201 }
202 params.systemPartitionB = &gpt.Partition{
203 Type: SystemBType,
204 Name: SystemBLabel,
205 }
206 if err := params.tbl.AddPartition(params.systemPartitionB, params.PartitionSize.System*Mi); err != nil {
207 return nil, fmt.Errorf("failed to allocate system partition B: %w", err)
208 }
209 } else if params.PartitionSize.System == 0 && params.SystemImage != nil {
210 // Safeguard against contradicting parameters.
211 return nil, fmt.Errorf("the system image parameter was passed while the associated partition size is zero")
212 }
213 // Create the data partition only if its size is specified.
214 if params.PartitionSize.Data != 0 {
215 params.dataPartition = &gpt.Partition{
216 Type: DataType,
217 Name: DataLabel,
218 }
219 if err := params.tbl.AddPartition(params.dataPartition, -1); err != nil {
220 return nil, fmt.Errorf("failed to allocate data partition: %w", err)
221 }
222 }
223
224 return params, nil
225}
226
227const Mi = 1024 * 1024
228
229// Create writes a Metropolis OS image to a block device.
230func Create(params *Params) (*efivarfs.LoadOption, error) {
231 p, err := Plan(params)
232 if err != nil {
233 return nil, err
234 }
235
236 return p.Apply()
237}