| 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 | |
| 4 | // This package provides self-contained implementation used to generate |
| 5 | // Metropolis disk images. |
| 6 | package osimage |
| 7 | |
| 8 | import ( |
| 9 | "fmt" |
| 10 | "io" |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 11 | "strings" |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 12 | |
| Mateusz Zalega | 612a033 | 2021-11-17 20:04:52 +0100 | [diff] [blame] | 13 | "github.com/google/uuid" |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 14 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 15 | "source.monogon.dev/osbase/blockdev" |
| 16 | "source.monogon.dev/osbase/efivarfs" |
| 17 | "source.monogon.dev/osbase/fat32" |
| 18 | "source.monogon.dev/osbase/gpt" |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 19 | "source.monogon.dev/osbase/structfs" |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | var ( |
| 23 | SystemAType = uuid.MustParse("ee96054b-f6d0-4267-aaaa-724b2afea74c") |
| 24 | SystemBType = uuid.MustParse("ee96054b-f6d0-4267-bbbb-724b2afea74c") |
| 25 | |
| 26 | DataType = uuid.MustParse("9eeec464-6885-414a-b278-4305c51f7966") |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | const ( |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 30 | SystemALabel = "METROPOLIS-SYSTEM-A" |
| 31 | SystemBLabel = "METROPOLIS-SYSTEM-B" |
| 32 | DataLabel = "METROPOLIS-NODE-DATA" |
| 33 | ESPLabel = "ESP" |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 34 | |
| 35 | EFIPayloadPath = "/EFI/BOOT/BOOTx64.EFI" |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 36 | EFIBootAPath = "/EFI/metropolis/boot-a.efi" |
| 37 | EFIBootBPath = "/EFI/metropolis/boot-b.efi" |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 38 | nodeParamsPath = "metropolis/parameters.pb" |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 39 | ) |
| 40 | |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 41 | // PartitionSizeInfo contains parameters used during partition table |
| 42 | // initialization and, in case of image files, space allocation. |
| 43 | type PartitionSizeInfo struct { |
| 44 | // Size of the EFI System Partition (ESP), in mebibytes. The size must |
| 45 | // not be zero. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 46 | ESP int64 |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 47 | // Size of the Metropolis system partition, in mebibytes. The partition |
| 48 | // won't be created if the size is zero. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 49 | System int64 |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 50 | // Size of the Metropolis data partition, in mebibytes. The partition |
| 51 | // won't be created if the size is zero. If the image is output to a |
| 52 | // block device, the partition will be extended to fill the remaining |
| 53 | // space. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 54 | Data int64 |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| Tim Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 57 | // Params contains parameters used by Plan or Write to build a Metropolis OS |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 58 | // image. |
| 59 | type Params struct { |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 60 | // Output is the block device to which the OS image is written. |
| 61 | Output blockdev.BlockDev |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 62 | // ABLoader provides the A/B loader which then loads the EFI loader for the |
| 63 | // correct slot. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 64 | ABLoader structfs.Blob |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 65 | // EFIPayload provides contents of the EFI payload file. It must not be |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 66 | // nil. This gets put into boot slot A. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 67 | EFIPayload structfs.Blob |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 68 | // SystemImage provides contents of the Metropolis system partition. |
| 69 | // If nil, no contents will be copied into the partition. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 70 | SystemImage structfs.Blob |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 71 | // NodeParameters provides contents of the node parameters file. If nil, |
| 72 | // the node parameters file won't be created in the target ESP |
| 73 | // filesystem. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 74 | NodeParameters structfs.Blob |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 75 | // 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] | 76 | // header. It's optional and can be left blank if the identifier is |
| 77 | // to be randomly generated. Setting it to a predetermined value can |
| 78 | // help in implementing reproducible builds. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 79 | DiskGUID uuid.UUID |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 80 | // PartitionSize specifies a size for the ESP, Metropolis System and |
| 81 | // Metropolis data partition. |
| 82 | PartitionSize PartitionSizeInfo |
| Tim Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 83 | // BIOSBootCode provides the optional contents for the protective MBR |
| 84 | // block which gets executed by legacy BIOS boot. |
| 85 | BIOSBootCode []byte |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 88 | type plan struct { |
| 89 | *Params |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 90 | efiRoot structfs.Tree |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 91 | tbl *gpt.Table |
| 92 | efiPartition *gpt.Partition |
| 93 | systemPartitionA *gpt.Partition |
| 94 | systemPartitionB *gpt.Partition |
| 95 | dataPartition *gpt.Partition |
| 96 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 97 | |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 98 | // Apply actually writes the planned installation to the blockdevice. |
| 99 | func (i *plan) Apply() (*efivarfs.LoadOption, error) { |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 100 | // Discard the entire device, we're going to write new data over it. |
| 101 | // Ignore errors, this is only advisory. |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 102 | i.Output.Discard(0, i.Output.BlockCount()*i.Output.BlockSize()) |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 103 | |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 104 | if err := fat32.WriteFS(blockdev.NewRWS(i.efiPartition), i.efiRoot, fat32.Options{ |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 105 | BlockSize: uint16(i.efiPartition.BlockSize()), |
| 106 | BlockCount: uint32(i.efiPartition.BlockCount()), |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 107 | Label: "MNGN_BOOT", |
| 108 | }); err != nil { |
| 109 | return nil, fmt.Errorf("failed to write FAT32: %w", err) |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 110 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 111 | |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 112 | systemImage, err := i.SystemImage.Open() |
| 113 | if err != nil { |
| 114 | return nil, fmt.Errorf("failed to open system image: %w", err) |
| 115 | } |
| 116 | if _, err := io.Copy(blockdev.NewRWS(i.systemPartitionA), systemImage); err != nil { |
| 117 | systemImage.Close() |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 118 | return nil, fmt.Errorf("failed to write system partition A: %w", err) |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 119 | } |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 120 | systemImage.Close() |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 121 | |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 122 | if err := i.tbl.Write(); err != nil { |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 123 | return nil, fmt.Errorf("failed to write Table: %w", err) |
| Mateusz Zalega | 612a033 | 2021-11-17 20:04:52 +0100 | [diff] [blame] | 124 | } |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 125 | |
| 126 | // Build an EFI boot entry pointing to the image's ESP. |
| 127 | return &efivarfs.LoadOption{ |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 128 | Description: "Metropolis", |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 129 | FilePath: efivarfs.DevicePath{ |
| 130 | &efivarfs.HardDrivePath{ |
| 131 | PartitionNumber: 1, |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 132 | PartitionStartBlock: i.efiPartition.FirstBlock, |
| 133 | PartitionSizeBlocks: i.efiPartition.SizeBlocks(), |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 134 | PartitionMatch: efivarfs.PartitionGPT{ |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 135 | PartitionUUID: i.efiPartition.ID, |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 136 | }, |
| 137 | }, |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 138 | efivarfs.FilePath(EFIPayloadPath), |
| Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 139 | }, |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 140 | }, nil |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 141 | } |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 142 | |
| 143 | // Plan allows to prepare an installation without modifying any data on the |
| 144 | // system. To apply the planned installation, call Apply on the returned plan. |
| 145 | func Plan(p *Params) (*plan, error) { |
| 146 | params := &plan{Params: p} |
| 147 | |
| 148 | var err error |
| 149 | params.tbl, err = gpt.New(params.Output) |
| 150 | if err != nil { |
| 151 | return nil, fmt.Errorf("invalid block device: %w", err) |
| 152 | } |
| 153 | |
| 154 | params.tbl.ID = params.DiskGUID |
| Tim Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 155 | params.tbl.BootCode = p.BIOSBootCode |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 156 | params.efiPartition = &gpt.Partition{ |
| 157 | Type: gpt.PartitionTypeEFISystem, |
| 158 | Name: ESPLabel, |
| 159 | } |
| 160 | |
| 161 | if err := params.tbl.AddPartition(params.efiPartition, params.PartitionSize.ESP*Mi); err != nil { |
| 162 | return nil, fmt.Errorf("failed to allocate ESP: %w", err) |
| 163 | } |
| 164 | |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 165 | if err := params.efiRoot.PlaceFile(strings.TrimPrefix(EFIBootAPath, "/"), params.EFIPayload); err != nil { |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 166 | return nil, err |
| 167 | } |
| 168 | // Place the A/B loader at the EFI bootloader autodiscovery path. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 169 | if err := params.efiRoot.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), params.ABLoader); err != nil { |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 170 | return nil, err |
| 171 | } |
| 172 | if params.NodeParameters != nil { |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 173 | if err := params.efiRoot.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil { |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 174 | return nil, err |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Try to layout the fat32 partition. If it detects that the disk is too |
| 179 | // small, an error will be returned. |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 180 | if _, err := fat32.SizeFS(params.efiRoot, fat32.Options{ |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 181 | BlockSize: uint16(params.efiPartition.BlockSize()), |
| 182 | BlockCount: uint32(params.efiPartition.BlockCount()), |
| 183 | Label: "MNGN_BOOT", |
| 184 | }); err != nil { |
| 185 | return nil, fmt.Errorf("failed to calculate size of FAT32: %w", err) |
| 186 | } |
| 187 | |
| 188 | // Create the system partition only if its size is specified. |
| 189 | if params.PartitionSize.System != 0 && params.SystemImage != nil { |
| 190 | params.systemPartitionA = &gpt.Partition{ |
| 191 | Type: SystemAType, |
| 192 | Name: SystemALabel, |
| 193 | } |
| 194 | if err := params.tbl.AddPartition(params.systemPartitionA, params.PartitionSize.System*Mi); err != nil { |
| 195 | return nil, fmt.Errorf("failed to allocate system partition A: %w", err) |
| 196 | } |
| 197 | params.systemPartitionB = &gpt.Partition{ |
| 198 | Type: SystemBType, |
| 199 | Name: SystemBLabel, |
| 200 | } |
| 201 | if err := params.tbl.AddPartition(params.systemPartitionB, params.PartitionSize.System*Mi); err != nil { |
| 202 | return nil, fmt.Errorf("failed to allocate system partition B: %w", err) |
| 203 | } |
| 204 | } else if params.PartitionSize.System == 0 && params.SystemImage != nil { |
| 205 | // Safeguard against contradicting parameters. |
| 206 | return nil, fmt.Errorf("the system image parameter was passed while the associated partition size is zero") |
| 207 | } |
| 208 | // Create the data partition only if its size is specified. |
| 209 | if params.PartitionSize.Data != 0 { |
| 210 | params.dataPartition = &gpt.Partition{ |
| 211 | Type: DataType, |
| 212 | Name: DataLabel, |
| 213 | } |
| 214 | if err := params.tbl.AddPartition(params.dataPartition, -1); err != nil { |
| 215 | return nil, fmt.Errorf("failed to allocate data partition: %w", err) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return params, nil |
| 220 | } |
| 221 | |
| 222 | const Mi = 1024 * 1024 |
| 223 | |
| Tim Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 224 | // Write writes a Metropolis OS image to a block device. |
| 225 | func Write(params *Params) (*efivarfs.LoadOption, error) { |
| Tim Windelschmidt | bceb160 | 2024-07-10 18:17:32 +0200 | [diff] [blame] | 226 | p, err := Plan(params) |
| 227 | if err != nil { |
| 228 | return nil, err |
| 229 | } |
| 230 | |
| 231 | return p.Apply() |
| 232 | } |