Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +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 | |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 17 | // mkimage is a tool to generate Metropolis node disk images. |
| 18 | // It can be used both to initialize block devices and to create image |
| 19 | // files. |
| 20 | // |
| 21 | // The tool takes a path to an EFI payload (--efi), and a path to a |
| 22 | // Metropolis system image (--system) as its only required inputs. In |
| 23 | // addition, an output path must be supplied (--out). |
| 24 | // Node parameters file path (--node_parameters) may also be supplied, in |
| 25 | // which case the file will be copied to the EFI system partition. |
| 26 | // Partition sizes are fixed and may be overridden by command line flags. |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 27 | package main |
| 28 | |
| 29 | import ( |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 30 | "flag" |
Serge Bazanski | 032ca18 | 2020-06-09 20:17:13 +0200 | [diff] [blame] | 31 | "log" |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 32 | "os" |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 33 | |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 34 | "source.monogon.dev/metropolis/node/build/mkimage/osimage" |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 35 | ) |
| 36 | |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 37 | func main() { |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 38 | // Fill in the image parameters based on flags. |
| 39 | var ( |
| 40 | efiPayload string |
| 41 | systemImage string |
| 42 | nodeParams string |
| 43 | cfg osimage.Params |
| 44 | ) |
| 45 | flag.StringVar(&efiPayload, "efi", "", "Path to the UEFI payload used") |
| 46 | flag.StringVar(&systemImage, "system", "", "Path to the system partition image used") |
| 47 | flag.StringVar(&nodeParams, "node_parameters", "", "Path to Node Parameters to be written to the ESP (default: don't write Node Parameters)") |
| 48 | flag.StringVar(&cfg.OutputPath, "out", "", "Path to the resulting disk image or block device") |
| 49 | flag.Uint64Var(&cfg.PartitionSize.Data, "data_partition_size", 2048, "Override the data partition size (default 2048 MiB). Used only when generating image files.") |
| 50 | flag.Uint64Var(&cfg.PartitionSize.ESP, "esp_partition_size", 128, "Override the ESP partition size (default: 128MiB)") |
| 51 | flag.Uint64Var(&cfg.PartitionSize.System, "system_partition_size", 1024, "Override the System partition size (default: 1024MiB)") |
| 52 | flag.StringVar(&cfg.DiskGUID, "GUID", "", "Disk GUID marked in the resulting image's partition table (default: randomly generated)") |
Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 53 | flag.Parse() |
Serge Bazanski | 032ca18 | 2020-06-09 20:17:13 +0200 | [diff] [blame] | 54 | |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 55 | // Open the input files for osimage.Create, fill in reader objects and |
| 56 | // metadata in osimage.Params. |
| 57 | // Start with the EFI Payload the OS will boot from. |
| 58 | p, err := os.Open(efiPayload) |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 59 | if err != nil { |
Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame^] | 60 | log.Fatalf("while opening the EFI payload at %q: %v", efiPayload, err) |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 61 | } |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 62 | defer p.Close() |
| 63 | cfg.EFIPayload = p |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 64 | |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 65 | // Attempt to open the system image if its path is set. In case the path |
| 66 | // isn't set, the system partition will still be created, but no |
| 67 | // contents will be written into it. |
| 68 | if systemImage != "" { |
| 69 | img, err := os.Open(systemImage) |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 70 | if err != nil { |
Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame^] | 71 | log.Fatalf("while opening the system image at %q: %v", systemImage, err) |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 72 | } |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 73 | defer img.Close() |
| 74 | cfg.SystemImage = img |
| 75 | } |
| 76 | |
| 77 | // Attempt to open the node parameters file if its path is set. |
| 78 | if nodeParams != "" { |
| 79 | np, err := os.Open(nodeParams) |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 80 | if err != nil { |
Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame^] | 81 | log.Fatalf("while opening node parameters at %q: %v", nodeParams, err) |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 82 | } |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 83 | defer np.Close() |
| 84 | cfg.NodeParameters = np |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 87 | // Write the parametrized OS image. |
| 88 | if _, err := osimage.Create(&cfg); err != nil { |
Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame^] | 89 | log.Fatalf("while creating a Metropolis OS image: %v", err) |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 90 | } |
Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 91 | } |