blob: 37691a951e04dc9ea3ea391b4d14d0f67a754059 [file] [log] [blame]
Lorenz Brunae0d90d2019-09-05 17:53:56 +02001// 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
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020017// mkimage is a tool to generate node disk images.
Mateusz Zalegac71efc92021-09-07 16:46:25 +020018// It can be used both to initialize block devices and to create image
19// files.
20//
Tim Windelschmidt272c8302024-11-05 05:17:44 +010021// The tool takes a path to an EFI payload (--efi), a path to a abloader
22// payload (--abloader) and a path to a system image (--system) as its only
23// required inputs. In addition, an output path must be supplied (--out).
Mateusz Zalegac71efc92021-09-07 16:46:25 +020024// 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 Brunae0d90d2019-09-05 17:53:56 +020027package main
28
29import (
Lorenz Brun54a5a052023-10-02 16:40:11 +020030 _ "embed"
Leopold Schabel65493072019-11-06 13:40:44 +000031 "flag"
Serge Bazanski032ca182020-06-09 20:17:13 +020032 "log"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020033 "os"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020034
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020035 "source.monogon.dev/osbase/blkio"
36 "source.monogon.dev/osbase/blockdev"
Tim Windelschmidtc2290c22024-08-15 19:56:00 +020037 "source.monogon.dev/osbase/build/mkimage/osimage"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020038)
39
Lorenz Brunae0d90d2019-09-05 17:53:56 +020040func main() {
Mateusz Zalegac71efc92021-09-07 16:46:25 +020041 // Fill in the image parameters based on flags.
42 var (
Tim Windelschmidt272c8302024-11-05 05:17:44 +010043 efiPayload string
44 systemImage string
45 abLoaderPayload string
46 nodeParams string
47 outputPath string
48 diskUUID string
49 cfg osimage.Params
Mateusz Zalegac71efc92021-09-07 16:46:25 +020050 )
51 flag.StringVar(&efiPayload, "efi", "", "Path to the UEFI payload used")
52 flag.StringVar(&systemImage, "system", "", "Path to the system partition image used")
Tim Windelschmidt272c8302024-11-05 05:17:44 +010053 flag.StringVar(&abLoaderPayload, "abloader", "", "Path to the abloader payload used")
Mateusz Zalegac71efc92021-09-07 16:46:25 +020054 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 +020055 flag.StringVar(&outputPath, "out", "", "Path to the resulting disk image or block device")
56 flag.Int64Var(&cfg.PartitionSize.Data, "data_partition_size", 2048, "Override the data partition size (default 2048 MiB). Used only when generating image files.")
57 flag.Int64Var(&cfg.PartitionSize.ESP, "esp_partition_size", 128, "Override the ESP partition size (default: 128MiB)")
58 flag.Int64Var(&cfg.PartitionSize.System, "system_partition_size", 1024, "Override the System partition size (default: 1024MiB)")
59 flag.StringVar(&diskUUID, "GUID", "", "Disk GUID marked in the resulting image's partition table (default: randomly generated)")
Leopold Schabel65493072019-11-06 13:40:44 +000060 flag.Parse()
Serge Bazanski032ca182020-06-09 20:17:13 +020061
Mateusz Zalegac71efc92021-09-07 16:46:25 +020062 // Open the input files for osimage.Create, fill in reader objects and
63 // metadata in osimage.Params.
64 // Start with the EFI Payload the OS will boot from.
Lorenz Brunad131882023-06-28 16:42:20 +020065 p, err := blkio.NewFileReader(efiPayload)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020066 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010067 log.Fatalf("while opening the EFI payload at %q: %v", efiPayload, err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020068 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020069 cfg.EFIPayload = p
Lorenz Brunae0d90d2019-09-05 17:53:56 +020070
Tim Windelschmidt272c8302024-11-05 05:17:44 +010071 ab, err := blkio.NewFileReader(abLoaderPayload)
72 if err != nil {
73 log.Fatalf("while opening the abloader payload at %q: %v", abLoaderPayload, err)
74 }
75 cfg.ABLoader = ab
76
Mateusz Zalegac71efc92021-09-07 16:46:25 +020077 // Attempt to open the system image if its path is set. In case the path
78 // isn't set, the system partition will still be created, but no
79 // contents will be written into it.
80 if systemImage != "" {
81 img, err := os.Open(systemImage)
Lorenz Brun3a99c592021-01-26 19:57:21 +010082 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010083 log.Fatalf("while opening the system image at %q: %v", systemImage, err)
Lorenz Brun3a99c592021-01-26 19:57:21 +010084 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020085 defer img.Close()
86 cfg.SystemImage = img
87 }
88
89 // Attempt to open the node parameters file if its path is set.
90 if nodeParams != "" {
Lorenz Brunad131882023-06-28 16:42:20 +020091 np, err := blkio.NewFileReader(nodeParams)
Lorenz Brun3a99c592021-01-26 19:57:21 +010092 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010093 log.Fatalf("while opening node parameters at %q: %v", nodeParams, err)
Lorenz Brun3a99c592021-01-26 19:57:21 +010094 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020095 cfg.NodeParameters = np
Lorenz Brun3a99c592021-01-26 19:57:21 +010096 }
97
Lorenz Brunad131882023-06-28 16:42:20 +020098 // TODO(#254): Build and use dynamically-grown block devices
99 cfg.Output, err = blockdev.CreateFile(outputPath, 512, 10*1024*1024)
100 if err != nil {
101 panic(err)
102 }
103
Mateusz Zalegac71efc92021-09-07 16:46:25 +0200104 // Write the parametrized OS image.
Tim Windelschmidtcc27faa2024-08-01 02:18:35 +0200105 if _, err := osimage.Write(&cfg); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100106 log.Fatalf("while creating a Metropolis OS image: %v", err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200107 }
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200108}