| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 3 | |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame^] | 4 | // Package install allows planning and executing the installation of Metropolis |
| 5 | // to a block device. |
| 6 | package install |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 7 | |
| 8 | import ( |
| 9 | "fmt" |
| 10 | "io" |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 11 | |
| Mateusz Zalega | 612a033 | 2021-11-17 20:04:52 +0100 | [diff] [blame] | 12 | "github.com/google/uuid" |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 13 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 14 | "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är | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 18 | "source.monogon.dev/osbase/structfs" |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | var ( |
| 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 Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | const ( |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 29 | SystemALabel = "METROPOLIS-SYSTEM-A" |
| 30 | SystemBLabel = "METROPOLIS-SYSTEM-B" |
| 31 | DataLabel = "METROPOLIS-NODE-DATA" |
| 32 | ESPLabel = "ESP" |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 33 | |
| Jan Schär | 091b8a6 | 2025-05-13 09:07:50 +0000 | [diff] [blame] | 34 | EFIBootAPath = "EFI/metropolis/boot-a.efi" |
| 35 | EFIBootBPath = "EFI/metropolis/boot-b.efi" |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 36 | nodeParamsPath = "metropolis/parameters.pb" |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 37 | ) |
| 38 | |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 39 | var 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. |
| 47 | func 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 Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 55 | // PartitionSizeInfo contains parameters used during partition table |
| 56 | // initialization and, in case of image files, space allocation. |
| 57 | type PartitionSizeInfo struct { |
| 58 | // Size of the EFI System Partition (ESP), in mebibytes. The size must |
| 59 | // not be zero. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 60 | ESP int64 |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 61 | // Size of the Metropolis system partition, in mebibytes. The partition |
| 62 | // won't be created if the size is zero. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 63 | System int64 |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 64 | // 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 Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 68 | Data int64 |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame^] | 71 | // Params contains parameters used by Plan or Write to install Metropolis OS. |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 72 | type Params struct { |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame^] | 73 | // Output is the block device to which the OS is installed. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 74 | Output blockdev.BlockDev |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 75 | // Architecture is the CPU architecture of the OS image. |
| 76 | Architecture string |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 77 | // ABLoader provides the A/B loader which then loads the EFI loader for the |
| 78 | // correct slot. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 79 | ABLoader structfs.Blob |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 80 | // EFIPayload provides contents of the EFI payload file. It must not be |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 81 | // nil. This gets put into boot slot A. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 82 | EFIPayload structfs.Blob |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 83 | // SystemImage provides contents of the Metropolis system partition. |
| 84 | // If nil, no contents will be copied into the partition. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 85 | SystemImage structfs.Blob |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 86 | // 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är | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 89 | NodeParameters structfs.Blob |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 90 | // DiskGUID is a unique identifier of the image and a part of Table |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 91 | // 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 Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 94 | DiskGUID uuid.UUID |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 95 | // PartitionSize specifies a size for the ESP, Metropolis System and |
| 96 | // Metropolis data partition. |
| 97 | PartitionSize PartitionSizeInfo |
| Tim Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 98 | // BIOSBootCode provides the optional contents for the protective MBR |
| 99 | // block which gets executed by legacy BIOS boot. |
| 100 | BIOSBootCode []byte |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 103 | type plan struct { |
| 104 | *Params |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 105 | efiBootPath string |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 106 | efiRoot structfs.Tree |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 107 | tbl *gpt.Table |
| 108 | efiPartition *gpt.Partition |
| 109 | systemPartitionA *gpt.Partition |
| 110 | systemPartitionB *gpt.Partition |
| 111 | dataPartition *gpt.Partition |
| 112 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 113 | |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 114 | // Apply actually writes the planned installation to the blockdevice. |
| 115 | func (i *plan) Apply() (*efivarfs.LoadOption, error) { |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 116 | // Discard the entire device, we're going to write new data over it. |
| 117 | // Ignore errors, this is only advisory. |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 118 | i.Output.Discard(0, i.Output.BlockCount()*i.Output.BlockSize()) |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 119 | |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 120 | if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.efiRoot, fat32.Options{ |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 121 | BlockSize: uint16(i.efiPartition.BlockSize()), |
| 122 | BlockCount: uint32(i.efiPartition.BlockCount()), |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 123 | Label: "MNGN_BOOT", |
| 124 | }); err != nil { |
| 125 | return nil, fmt.Errorf("failed to write FAT32: %w", err) |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 126 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 127 | |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 128 | systemImage, err := i.SystemImage.Open() |
| 129 | if err != nil { |
| 130 | return nil, fmt.Errorf("failed to open system image: %w", err) |
| 131 | } |
| Jan Schär | e0db72c | 2025-06-18 18:14:07 +0000 | [diff] [blame] | 132 | if _, err := io.CopyN(blockdev.NewRWS(i.systemPartitionA), systemImage, i.SystemImage.Size()); err != nil { |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 133 | systemImage.Close() |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 134 | return nil, fmt.Errorf("failed to write system partition A: %w", err) |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 135 | } |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 136 | systemImage.Close() |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 137 | |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 138 | if err := i.tbl.Write(); err != nil { |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 139 | return nil, fmt.Errorf("failed to write Table: %w", err) |
| Mateusz Zalega | 612a033 | 2021-11-17 20:04:52 +0100 | [diff] [blame] | 140 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 141 | |
| 142 | // Build an EFI boot entry pointing to the image's ESP. |
| 143 | return &efivarfs.LoadOption{ |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 144 | Description: "Metropolis", |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 145 | FilePath: efivarfs.DevicePath{ |
| 146 | &efivarfs.HardDrivePath{ |
| 147 | PartitionNumber: 1, |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 148 | PartitionStartBlock: i.efiPartition.FirstBlock, |
| 149 | PartitionSizeBlocks: i.efiPartition.SizeBlocks(), |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 150 | PartitionMatch: efivarfs.PartitionGPT{ |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 151 | PartitionUUID: i.efiPartition.ID, |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 152 | }, |
| 153 | }, |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 154 | efivarfs.FilePath("/" + i.efiBootPath), |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 155 | }, |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 156 | }, nil |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 157 | } |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 158 | |
| 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. |
| 161 | func 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 Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 171 | params.tbl.BootCode = p.BIOSBootCode |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 172 | 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är | 091b8a6 | 2025-05-13 09:07:50 +0000 | [diff] [blame] | 181 | if err := params.efiRoot.PlaceFile(EFIBootAPath, params.EFIPayload); err != nil { |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 182 | return nil, err |
| 183 | } |
| 184 | // Place the A/B loader at the EFI bootloader autodiscovery path. |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 185 | 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 Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 190 | return nil, err |
| 191 | } |
| 192 | if params.NodeParameters != nil { |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 193 | if err := params.efiRoot.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil { |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 194 | 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är | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 200 | if _, err := fat32.SizeFS(params.efiRoot, fat32.Options{ |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 201 | 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 | |
| 242 | const Mi = 1024 * 1024 |
| 243 | |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame^] | 244 | // Write installs Metropolis OS to a block device. |
| Tim Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 245 | func Write(params *Params) (*efivarfs.LoadOption, error) { |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 246 | p, err := Plan(params) |
| 247 | if err != nil { |
| 248 | return nil, err |
| 249 | } |
| 250 | |
| 251 | return p.Apply() |
| 252 | } |