Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 1 | // 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. |
| 19 | package osimage |
| 20 | |
| 21 | import ( |
| 22 | "fmt" |
| 23 | "io" |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 24 | "strings" |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 25 | |
Mateusz Zalega | 612a033 | 2021-11-17 20:04:52 +0100 | [diff] [blame] | 26 | "github.com/google/uuid" |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 27 | |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 28 | "source.monogon.dev/metropolis/pkg/blockdev" |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 29 | "source.monogon.dev/metropolis/pkg/efivarfs" |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 30 | "source.monogon.dev/metropolis/pkg/fat32" |
| 31 | "source.monogon.dev/metropolis/pkg/gpt" |
| 32 | ) |
| 33 | |
| 34 | var ( |
| 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 Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | const ( |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 42 | SystemLabel = "METROPOLIS-SYSTEM" |
| 43 | DataLabel = "METROPOLIS-NODE-DATA" |
| 44 | ESPLabel = "ESP" |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 45 | |
| 46 | EFIPayloadPath = "/EFI/BOOT/BOOTx64.EFI" |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 47 | nodeParamsPath = "metropolis/parameters.pb" |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 48 | ) |
| 49 | |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 50 | // PartitionSizeInfo contains parameters used during partition table |
| 51 | // initialization and, in case of image files, space allocation. |
| 52 | type PartitionSizeInfo struct { |
| 53 | // Size of the EFI System Partition (ESP), in mebibytes. The size must |
| 54 | // not be zero. |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 55 | ESP int64 |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 56 | // Size of the Metropolis system partition, in mebibytes. The partition |
| 57 | // won't be created if the size is zero. |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 58 | System int64 |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 59 | // Size of the Metropolis data partition, in mebibytes. The partition |
| 60 | // won't be created if the size is zero. If the image is output to a |
| 61 | // block device, the partition will be extended to fill the remaining |
| 62 | // space. |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 63 | Data int64 |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Params contains parameters used by Create to build a Metropolis OS |
| 67 | // image. |
| 68 | type Params struct { |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 69 | // Output is the block device to which the OS image is written. |
| 70 | Output blockdev.BlockDev |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 71 | // EFIPayload provides contents of the EFI payload file. It must not be |
| 72 | // nil. |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 73 | EFIPayload fat32.SizedReader |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 74 | // SystemImage provides contents of the Metropolis system partition. |
| 75 | // If nil, no contents will be copied into the partition. |
| 76 | SystemImage io.Reader |
| 77 | // NodeParameters provides contents of the node parameters file. If nil, |
| 78 | // the node parameters file won't be created in the target ESP |
| 79 | // filesystem. |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 80 | NodeParameters fat32.SizedReader |
| 81 | // 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] | 82 | // header. It's optional and can be left blank if the identifier is |
| 83 | // to be randomly generated. Setting it to a predetermined value can |
| 84 | // help in implementing reproducible builds. |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 85 | DiskGUID uuid.UUID |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 86 | // PartitionSize specifies a size for the ESP, Metropolis System and |
| 87 | // Metropolis data partition. |
| 88 | PartitionSize PartitionSizeInfo |
| 89 | } |
| 90 | |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 91 | const Mi = 1024 * 1024 |
| 92 | |
| 93 | // Create writes a Metropolis OS image to a block device. |
Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 94 | func Create(params *Params) (*efivarfs.LoadOption, error) { |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 95 | // Discard the entire device, we're going to write new data over it. |
| 96 | // Ignore errors, this is only advisory. |
| 97 | params.Output.Discard(0, params.Output.BlockCount()) |
| 98 | |
| 99 | tbl, err := gpt.New(params.Output) |
| 100 | if err != nil { |
| 101 | return nil, fmt.Errorf("invalid block device: %w", err) |
| 102 | } |
| 103 | tbl.ID = params.DiskGUID |
| 104 | esp := gpt.Partition{ |
| 105 | Type: gpt.PartitionTypeEFISystem, |
| 106 | Name: ESPLabel, |
| 107 | } |
| 108 | if err := tbl.AddPartition(&esp, params.PartitionSize.ESP*Mi); err != nil { |
| 109 | return nil, fmt.Errorf("failed to allocate ESP: %w", err) |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 112 | rootInode := fat32.Inode{ |
| 113 | Attrs: fat32.AttrDirectory, |
| 114 | } |
| 115 | if err := rootInode.PlaceFile(strings.TrimPrefix(EFIPayloadPath, "/"), params.EFIPayload); err != nil { |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 116 | return nil, err |
| 117 | } |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 118 | if params.NodeParameters != nil { |
| 119 | if err := rootInode.PlaceFile(nodeParamsPath, params.NodeParameters); err != nil { |
| 120 | return nil, err |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 123 | if err := fat32.WriteFS(blockdev.NewRWS(esp), rootInode, fat32.Options{ |
| 124 | BlockSize: uint16(esp.BlockSize()), |
| 125 | BlockCount: uint32(esp.BlockCount()), |
| 126 | Label: "MNGN_BOOT", |
| 127 | }); err != nil { |
| 128 | return nil, fmt.Errorf("failed to write FAT32: %w", err) |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 129 | } |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 130 | |
| 131 | // Create the system partition only if its size is specified. |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 132 | if params.PartitionSize.System != 0 && params.SystemImage != nil { |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 133 | systemPartitionA := gpt.Partition{ |
| 134 | Type: SystemAType, |
| 135 | Name: SystemLabel, |
| 136 | } |
| 137 | if err := tbl.AddPartition(&systemPartitionA, params.PartitionSize.System*Mi); err != nil { |
| 138 | return nil, fmt.Errorf("failed to allocate system partition A: %w", err) |
| 139 | } |
| 140 | if _, err := io.Copy(blockdev.NewRWS(systemPartitionA), params.SystemImage); err != nil { |
| 141 | return nil, fmt.Errorf("failed to write system partition A: %w", err) |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 142 | } |
| 143 | } else if params.PartitionSize.System == 0 && params.SystemImage != nil { |
| 144 | // Safeguard against contradicting parameters. |
| 145 | return nil, fmt.Errorf("the system image parameter was passed while the associated partition size is zero") |
| 146 | } |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 147 | // Create the data partition only if its size is specified. |
| 148 | if params.PartitionSize.Data != 0 { |
| 149 | dataPartition := gpt.Partition{ |
| 150 | Type: DataType, |
| 151 | Name: DataLabel, |
| 152 | } |
| 153 | if err := tbl.AddPartition(&dataPartition, -1); err != nil { |
| 154 | return nil, fmt.Errorf("failed to allocate data partition: %w", err) |
| 155 | } |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 156 | } |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 157 | |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 158 | if err := tbl.Write(); err != nil { |
| 159 | return nil, fmt.Errorf("failed to write Table: %w", err) |
Mateusz Zalega | 612a033 | 2021-11-17 20:04:52 +0100 | [diff] [blame] | 160 | } |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 161 | |
| 162 | // Build an EFI boot entry pointing to the image's ESP. |
| 163 | return &efivarfs.LoadOption{ |
| 164 | Description: "Metropolis Slot A", |
Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 165 | FilePath: efivarfs.DevicePath{ |
| 166 | &efivarfs.HardDrivePath{ |
| 167 | PartitionNumber: 1, |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 168 | PartitionStartBlock: esp.FirstBlock, |
| 169 | PartitionSizeBlocks: esp.SizeBlocks(), |
Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 170 | PartitionMatch: efivarfs.PartitionGPT{ |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 171 | PartitionUUID: esp.ID, |
Lorenz Brun | ca1cff0 | 2023-06-26 17:52:44 +0200 | [diff] [blame] | 172 | }, |
| 173 | }, |
| 174 | efivarfs.FilePath(EFIPayloadPath), |
| 175 | }, |
Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame^] | 176 | }, nil |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 177 | } |