| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 3 | |
| Tim Windelschmidt | c2290c2 | 2024-08-15 19:56:00 +0200 | [diff] [blame] | 4 | // mkimage is a tool to generate node disk images. |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 5 | // It can be used both to initialize block devices and to create image |
| 6 | // files. |
| 7 | // |
| Tim Windelschmidt | 272c830 | 2024-11-05 05:17:44 +0100 | [diff] [blame] | 8 | // The tool takes a path to an EFI payload (--efi), a path to a abloader |
| 9 | // payload (--abloader) and a path to a system image (--system) as its only |
| 10 | // required inputs. In addition, an output path must be supplied (--out). |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 11 | // Node parameters file path (--node_parameters) may also be supplied, in |
| 12 | // which case the file will be copied to the EFI system partition. |
| 13 | // Partition sizes are fixed and may be overridden by command line flags. |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| Lorenz Brun | 54a5a05 | 2023-10-02 16:40:11 +0200 | [diff] [blame] | 17 | _ "embed" |
| Leopold Schabel | 6549307 | 2019-11-06 13:40:44 +0000 | [diff] [blame] | 18 | "flag" |
| Serge Bazanski | 032ca18 | 2020-06-09 20:17:13 +0200 | [diff] [blame] | 19 | "log" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 20 | "os" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 21 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 22 | "source.monogon.dev/osbase/blkio" |
| 23 | "source.monogon.dev/osbase/blockdev" |
| Tim Windelschmidt | c2290c2 | 2024-08-15 19:56:00 +0200 | [diff] [blame] | 24 | "source.monogon.dev/osbase/build/mkimage/osimage" |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 25 | ) |
| 26 | |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 27 | func main() { |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 28 | // Fill in the image parameters based on flags. |
| 29 | var ( |
| Tim Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 30 | efiPayload string |
| 31 | systemImage string |
| 32 | abLoaderPayload string |
| 33 | biosBootCodePayload string |
| 34 | nodeParams string |
| 35 | outputPath string |
| 36 | diskUUID string |
| 37 | cfg osimage.Params |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 38 | ) |
| 39 | flag.StringVar(&efiPayload, "efi", "", "Path to the UEFI payload used") |
| 40 | flag.StringVar(&systemImage, "system", "", "Path to the system partition image used") |
| Tim Windelschmidt | 272c830 | 2024-11-05 05:17:44 +0100 | [diff] [blame] | 41 | flag.StringVar(&abLoaderPayload, "abloader", "", "Path to the abloader payload used") |
| Tim Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 42 | flag.StringVar(&biosBootCodePayload, "bios_bootcode", "", "Optional path to the BIOS bootcode which gets placed at the start of the first block of the image. Limited to 440 bytes, padding is not required. It is only used by legacy BIOS boot.") |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 43 | flag.StringVar(&nodeParams, "node_parameters", "", "Path to Node Parameters to be written to the ESP (default: don't write Node Parameters)") |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 44 | flag.StringVar(&outputPath, "out", "", "Path to the resulting disk image or block device") |
| 45 | flag.Int64Var(&cfg.PartitionSize.Data, "data_partition_size", 2048, "Override the data partition size (default 2048 MiB). Used only when generating image files.") |
| 46 | flag.Int64Var(&cfg.PartitionSize.ESP, "esp_partition_size", 128, "Override the ESP partition size (default: 128MiB)") |
| 47 | flag.Int64Var(&cfg.PartitionSize.System, "system_partition_size", 1024, "Override the System partition size (default: 1024MiB)") |
| 48 | flag.StringVar(&diskUUID, "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] | 49 | flag.Parse() |
| Serge Bazanski | 032ca18 | 2020-06-09 20:17:13 +0200 | [diff] [blame] | 50 | |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 51 | // Open the input files for osimage.Create, fill in reader objects and |
| 52 | // metadata in osimage.Params. |
| 53 | // Start with the EFI Payload the OS will boot from. |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 54 | p, err := blkio.NewFileReader(efiPayload) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 55 | if err != nil { |
| Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame] | 56 | log.Fatalf("while opening the EFI payload at %q: %v", efiPayload, err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 57 | } |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 58 | cfg.EFIPayload = p |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 59 | |
| Tim Windelschmidt | 272c830 | 2024-11-05 05:17:44 +0100 | [diff] [blame] | 60 | ab, err := blkio.NewFileReader(abLoaderPayload) |
| 61 | if err != nil { |
| 62 | log.Fatalf("while opening the abloader payload at %q: %v", abLoaderPayload, err) |
| 63 | } |
| 64 | cfg.ABLoader = ab |
| 65 | |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 66 | // Attempt to open the system image if its path is set. In case the path |
| 67 | // isn't set, the system partition will still be created, but no |
| 68 | // contents will be written into it. |
| 69 | if systemImage != "" { |
| 70 | img, err := os.Open(systemImage) |
| Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 71 | if err != nil { |
| Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame] | 72 | log.Fatalf("while opening the system image at %q: %v", systemImage, err) |
| Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 73 | } |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 74 | defer img.Close() |
| 75 | cfg.SystemImage = img |
| 76 | } |
| 77 | |
| 78 | // Attempt to open the node parameters file if its path is set. |
| 79 | if nodeParams != "" { |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 80 | np, err := blkio.NewFileReader(nodeParams) |
| Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 81 | if err != nil { |
| Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame] | 82 | log.Fatalf("while opening node parameters at %q: %v", nodeParams, err) |
| Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 83 | } |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 84 | cfg.NodeParameters = np |
| Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| Tim Windelschmidt | 8e19fa4 | 2024-11-12 13:39:43 +0000 | [diff] [blame] | 87 | if biosBootCodePayload != "" { |
| 88 | bp, err := os.ReadFile(biosBootCodePayload) |
| 89 | if err != nil { |
| 90 | log.Fatalf("while opening BIOS bootcode at %q: %v", biosBootCodePayload, err) |
| 91 | } |
| 92 | cfg.BIOSBootCode = bp |
| 93 | } |
| 94 | |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 95 | // TODO(#254): Build and use dynamically-grown block devices |
| 96 | cfg.Output, err = blockdev.CreateFile(outputPath, 512, 10*1024*1024) |
| 97 | if err != nil { |
| 98 | panic(err) |
| 99 | } |
| 100 | |
| Mateusz Zalega | c71efc9 | 2021-09-07 16:46:25 +0200 | [diff] [blame] | 101 | // Write the parametrized OS image. |
| Tim Windelschmidt | cc27faa | 2024-08-01 02:18:35 +0200 | [diff] [blame] | 102 | if _, err := osimage.Write(&cfg); err != nil { |
| Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame] | 103 | log.Fatalf("while creating a Metropolis OS image: %v", err) |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 104 | } |
| Lorenz Brun | ae0d90d | 2019-09-05 17:53:56 +0200 | [diff] [blame] | 105 | } |