blob: 79e2920fdf5536b23a84e99ccdc8c0c4498b9745 [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/blkio"
23 "source.monogon.dev/osbase/blockdev"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020024 "source.monogon.dev/osbase/build/mkimage/osimage"
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.
Lorenz Brunad131882023-06-28 16:42:20 +020054 p, err := blkio.NewFileReader(efiPayload)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020055 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010056 log.Fatalf("while opening the EFI payload at %q: %v", efiPayload, err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020057 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020058 cfg.EFIPayload = p
Lorenz Brunae0d90d2019-09-05 17:53:56 +020059
Tim Windelschmidt272c8302024-11-05 05:17:44 +010060 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 Zalegac71efc92021-09-07 16:46:25 +020066 // 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 Brun3a99c592021-01-26 19:57:21 +010071 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010072 log.Fatalf("while opening the system image at %q: %v", systemImage, err)
Lorenz Brun3a99c592021-01-26 19:57:21 +010073 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020074 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 Brunad131882023-06-28 16:42:20 +020080 np, err := blkio.NewFileReader(nodeParams)
Lorenz Brun3a99c592021-01-26 19:57:21 +010081 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010082 log.Fatalf("while opening node parameters at %q: %v", nodeParams, err)
Lorenz Brun3a99c592021-01-26 19:57:21 +010083 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020084 cfg.NodeParameters = np
Lorenz Brun3a99c592021-01-26 19:57:21 +010085 }
86
Tim Windelschmidt8e19fa42024-11-12 13:39:43 +000087 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 Brunad131882023-06-28 16:42:20 +020095 // 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 Zalegac71efc92021-09-07 16:46:25 +0200101 // Write the parametrized OS image.
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +0200102 if _, err := osimage.Write(&cfg); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100103 log.Fatalf("while creating a Metropolis OS image: %v", err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200104 }
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200105}