blob: bef75174691937903ea7f4ac3254c3e32d07931a [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Lorenz Brunae0d90d2019-09-05 17:53:56 +02002// SPDX-License-Identifier: Apache-2.0
Lorenz Brunae0d90d2019-09-05 17:53:56 +02003
Tim Windelschmidtc2290c22024-08-15 19:56:00 +02004// mkimage is a tool to generate node disk images.
Mateusz Zalegac71efc92021-09-07 16:46:25 +02005// It can be used both to initialize block devices and to create image
6// files.
7//
Tim Windelschmidt272c8302024-11-05 05:17:44 +01008// 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 Zalegac71efc92021-09-07 16:46:25 +020011// 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 Brunae0d90d2019-09-05 17:53:56 +020014package main
15
16import (
Lorenz Brun54a5a052023-10-02 16:40:11 +020017 _ "embed"
Leopold Schabel65493072019-11-06 13:40:44 +000018 "flag"
Serge Bazanski032ca182020-06-09 20:17:13 +020019 "log"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020020 "os"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020021
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020022 "source.monogon.dev/osbase/blockdev"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020023 "source.monogon.dev/osbase/build/mkimage/osimage"
Jan Schärc1b6df42025-03-20 08:52:18 +000024 "source.monogon.dev/osbase/structfs"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020025)
26
Lorenz Brunae0d90d2019-09-05 17:53:56 +020027func main() {
Mateusz Zalegac71efc92021-09-07 16:46:25 +020028 // Fill in the image parameters based on flags.
29 var (
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +000030 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 Zalegac71efc92021-09-07 16:46:25 +020038 )
39 flag.StringVar(&efiPayload, "efi", "", "Path to the UEFI payload used")
40 flag.StringVar(&systemImage, "system", "", "Path to the system partition image used")
Tim Windelschmidt272c8302024-11-05 05:17:44 +010041 flag.StringVar(&abLoaderPayload, "abloader", "", "Path to the abloader payload used")
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +000042 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 Zalegac71efc92021-09-07 16:46:25 +020043 flag.StringVar(&nodeParams, "node_parameters", "", "Path to Node Parameters to be written to the ESP (default: don't write Node Parameters)")
Lorenz Brunad131882023-06-28 16:42:20 +020044 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 Schabel65493072019-11-06 13:40:44 +000049 flag.Parse()
Serge Bazanski032ca182020-06-09 20:17:13 +020050
Mateusz Zalegac71efc92021-09-07 16:46:25 +020051 // 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.
Jan Schärc1b6df42025-03-20 08:52:18 +000054 var err error
55 cfg.EFIPayload, err = structfs.OSPathBlob(efiPayload)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020056 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010057 log.Fatalf("while opening the EFI payload at %q: %v", efiPayload, err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020058 }
59
Jan Schärc1b6df42025-03-20 08:52:18 +000060 cfg.ABLoader, err = structfs.OSPathBlob(abLoaderPayload)
Tim Windelschmidt272c8302024-11-05 05:17:44 +010061 if err != nil {
62 log.Fatalf("while opening the abloader payload at %q: %v", abLoaderPayload, err)
63 }
Tim Windelschmidt272c8302024-11-05 05:17:44 +010064
Mateusz Zalegac71efc92021-09-07 16:46:25 +020065 // 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 != "" {
Jan Schärc1b6df42025-03-20 08:52:18 +000069 cfg.SystemImage, err = structfs.OSPathBlob(systemImage)
Lorenz Brun3a99c592021-01-26 19:57:21 +010070 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010071 log.Fatalf("while opening the system image at %q: %v", systemImage, err)
Lorenz Brun3a99c592021-01-26 19:57:21 +010072 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020073 }
74
75 // Attempt to open the node parameters file if its path is set.
76 if nodeParams != "" {
Jan Schärc1b6df42025-03-20 08:52:18 +000077 np, err := structfs.OSPathBlob(nodeParams)
Lorenz Brun3a99c592021-01-26 19:57:21 +010078 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010079 log.Fatalf("while opening node parameters at %q: %v", nodeParams, err)
Lorenz Brun3a99c592021-01-26 19:57:21 +010080 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020081 cfg.NodeParameters = np
Lorenz Brun3a99c592021-01-26 19:57:21 +010082 }
83
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +000084 if biosBootCodePayload != "" {
85 bp, err := os.ReadFile(biosBootCodePayload)
86 if err != nil {
87 log.Fatalf("while opening BIOS bootcode at %q: %v", biosBootCodePayload, err)
88 }
89 cfg.BIOSBootCode = bp
90 }
91
Lorenz Brunad131882023-06-28 16:42:20 +020092 // TODO(#254): Build and use dynamically-grown block devices
93 cfg.Output, err = blockdev.CreateFile(outputPath, 512, 10*1024*1024)
94 if err != nil {
95 panic(err)
96 }
97
Mateusz Zalegac71efc92021-09-07 16:46:25 +020098 // Write the parametrized OS image.
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +020099 if _, err := osimage.Write(&cfg); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100100 log.Fatalf("while creating a Metropolis OS image: %v", err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200101 }
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200102}