blob: a09f5d10f3d137eeab774688c4fe957b2e11122c [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
Lorenz Brunad131882023-06-28 16:42:20 +020028 "source.monogon.dev/metropolis/pkg/blockdev"
Mateusz Zalegac71efc92021-09-07 16:46:25 +020029 "source.monogon.dev/metropolis/pkg/efivarfs"
Lorenz Brunad131882023-06-28 16:42:20 +020030 "source.monogon.dev/metropolis/pkg/fat32"
31 "source.monogon.dev/metropolis/pkg/gpt"
32)
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
Lorenz Brunad131882023-06-28 16:42:20 +020097const Mi = 1024 * 1024
98
99// Create writes a Metropolis OS image to a block device.
Lorenz Brunca1cff02023-06-26 17:52:44 +0200100func Create(params *Params) (*efivarfs.LoadOption, error) {
Lorenz Brunad131882023-06-28 16:42:20 +0200101 // Discard the entire device, we're going to write new data over it.
102 // Ignore errors, this is only advisory.
103 params.Output.Discard(0, params.Output.BlockCount())
104
105 tbl, err := gpt.New(params.Output)
106 if err != nil {
107 return nil, fmt.Errorf("invalid block device: %w", err)
108 }
109 tbl.ID = params.DiskGUID
110 esp := gpt.Partition{
111 Type: gpt.PartitionTypeEFISystem,
112 Name: ESPLabel,
113 }
114 if err := tbl.AddPartition(&esp, params.PartitionSize.ESP*Mi); err != nil {
115 return nil, fmt.Errorf("failed to allocate ESP: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200116 }
117
Lorenz Brunad131882023-06-28 16:42:20 +0200118 rootInode := fat32.Inode{
119 Attrs: fat32.AttrDirectory,
120 }
Lorenz Brun54a5a052023-10-02 16:40:11 +0200121 if err := rootInode.PlaceFile(strings.TrimPrefix(EFIBootAPath, "/"), params.EFIPayload); err != nil {
Lorenz Brun35fcf032023-06-29 04:15:58 +0200122 return nil, err
123 }
Lorenz Brun54a5a052023-10-02 16:40:11 +0200124 // Place the A/B loader at the EFI bootloader autodiscovery path.
125 if err := rootInode.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), params.ABLoader); err != nil {
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200126 return nil, err
127 }
Lorenz Brunad131882023-06-28 16:42:20 +0200128 if params.NodeParameters != nil {
129 if err := rootInode.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil {
130 return nil, err
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200131 }
132 }
Lorenz Brunad131882023-06-28 16:42:20 +0200133 if err := fat32.WriteFS(blockdev.NewRWS(esp), rootInode, fat32.Options{
134 BlockSize: uint16(esp.BlockSize()),
135 BlockCount: uint32(esp.BlockCount()),
136 Label: "MNGN_BOOT",
137 }); err != nil {
138 return nil, fmt.Errorf("failed to write FAT32: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200139 }
Lorenz Brunad131882023-06-28 16:42:20 +0200140
141 // Create the system partition only if its size is specified.
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200142 if params.PartitionSize.System != 0 && params.SystemImage != nil {
Lorenz Brunad131882023-06-28 16:42:20 +0200143 systemPartitionA := gpt.Partition{
144 Type: SystemAType,
Lorenz Brun35fcf032023-06-29 04:15:58 +0200145 Name: SystemALabel,
Lorenz Brunad131882023-06-28 16:42:20 +0200146 }
147 if err := tbl.AddPartition(&systemPartitionA, params.PartitionSize.System*Mi); err != nil {
148 return nil, fmt.Errorf("failed to allocate system partition A: %w", err)
149 }
150 if _, err := io.Copy(blockdev.NewRWS(systemPartitionA), params.SystemImage); err != nil {
151 return nil, fmt.Errorf("failed to write system partition A: %w", err)
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200152 }
Lorenz Brun35fcf032023-06-29 04:15:58 +0200153 systemPartitionB := gpt.Partition{
154 Type: SystemBType,
155 Name: SystemBLabel,
156 }
157 if err := tbl.AddPartition(&systemPartitionB, params.PartitionSize.System*Mi); err != nil {
158 return nil, fmt.Errorf("failed to allocate system partition B: %w", err)
159 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200160 } else if params.PartitionSize.System == 0 && params.SystemImage != nil {
161 // Safeguard against contradicting parameters.
162 return nil, fmt.Errorf("the system image parameter was passed while the associated partition size is zero")
163 }
Lorenz Brunad131882023-06-28 16:42:20 +0200164 // Create the data partition only if its size is specified.
165 if params.PartitionSize.Data != 0 {
166 dataPartition := gpt.Partition{
167 Type: DataType,
168 Name: DataLabel,
169 }
170 if err := tbl.AddPartition(&dataPartition, -1); err != nil {
171 return nil, fmt.Errorf("failed to allocate data partition: %w", err)
172 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200173 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200174
Lorenz Brunad131882023-06-28 16:42:20 +0200175 if err := tbl.Write(); err != nil {
176 return nil, fmt.Errorf("failed to write Table: %w", err)
Mateusz Zalega612a0332021-11-17 20:04:52 +0100177 }
Lorenz Brunad131882023-06-28 16:42:20 +0200178
179 // Build an EFI boot entry pointing to the image's ESP.
180 return &efivarfs.LoadOption{
Lorenz Brun54a5a052023-10-02 16:40:11 +0200181 Description: "Metropolis",
Lorenz Brunca1cff02023-06-26 17:52:44 +0200182 FilePath: efivarfs.DevicePath{
183 &efivarfs.HardDrivePath{
184 PartitionNumber: 1,
Lorenz Brunad131882023-06-28 16:42:20 +0200185 PartitionStartBlock: esp.FirstBlock,
186 PartitionSizeBlocks: esp.SizeBlocks(),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200187 PartitionMatch: efivarfs.PartitionGPT{
Lorenz Brunad131882023-06-28 16:42:20 +0200188 PartitionUUID: esp.ID,
Lorenz Brunca1cff02023-06-26 17:52:44 +0200189 },
190 },
Lorenz Brun54a5a052023-10-02 16:40:11 +0200191 efivarfs.FilePath(EFIPayloadPath),
Lorenz Brunca1cff02023-06-26 17:52:44 +0200192 },
Lorenz Brunad131882023-06-28 16:42:20 +0200193 }, nil
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200194}