blob: 077348e684e68a3cd7ed4cfd877b635785352cb0 [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
Mateusz Zalegac71efc92021-09-07 16:46:25 +020017// 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 Brunae0d90d2019-09-05 17:53:56 +020027package main
28
29import (
Leopold Schabel65493072019-11-06 13:40:44 +000030 "flag"
Serge Bazanski032ca182020-06-09 20:17:13 +020031 "log"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020032 "os"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020033
Mateusz Zalegac71efc92021-09-07 16:46:25 +020034 "source.monogon.dev/metropolis/node/build/mkimage/osimage"
Lorenz Brunad131882023-06-28 16:42:20 +020035 "source.monogon.dev/metropolis/pkg/blkio"
36 "source.monogon.dev/metropolis/pkg/blockdev"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020037)
38
Lorenz Brunae0d90d2019-09-05 17:53:56 +020039func main() {
Mateusz Zalegac71efc92021-09-07 16:46:25 +020040 // Fill in the image parameters based on flags.
41 var (
42 efiPayload string
43 systemImage string
44 nodeParams string
Lorenz Brunad131882023-06-28 16:42:20 +020045 outputPath string
46 diskUUID string
Mateusz Zalegac71efc92021-09-07 16:46:25 +020047 cfg osimage.Params
48 )
49 flag.StringVar(&efiPayload, "efi", "", "Path to the UEFI payload used")
50 flag.StringVar(&systemImage, "system", "", "Path to the system partition image used")
51 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 +020052 flag.StringVar(&outputPath, "out", "", "Path to the resulting disk image or block device")
53 flag.Int64Var(&cfg.PartitionSize.Data, "data_partition_size", 2048, "Override the data partition size (default 2048 MiB). Used only when generating image files.")
54 flag.Int64Var(&cfg.PartitionSize.ESP, "esp_partition_size", 128, "Override the ESP partition size (default: 128MiB)")
55 flag.Int64Var(&cfg.PartitionSize.System, "system_partition_size", 1024, "Override the System partition size (default: 1024MiB)")
56 flag.StringVar(&diskUUID, "GUID", "", "Disk GUID marked in the resulting image's partition table (default: randomly generated)")
Leopold Schabel65493072019-11-06 13:40:44 +000057 flag.Parse()
Serge Bazanski032ca182020-06-09 20:17:13 +020058
Mateusz Zalegac71efc92021-09-07 16:46:25 +020059 // Open the input files for osimage.Create, fill in reader objects and
60 // metadata in osimage.Params.
61 // Start with the EFI Payload the OS will boot from.
Lorenz Brunad131882023-06-28 16:42:20 +020062 p, err := blkio.NewFileReader(efiPayload)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020063 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010064 log.Fatalf("while opening the EFI payload at %q: %v", efiPayload, err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020065 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020066 cfg.EFIPayload = p
Lorenz Brunae0d90d2019-09-05 17:53:56 +020067
Mateusz Zalegac71efc92021-09-07 16:46:25 +020068 // Attempt to open the system image if its path is set. In case the path
69 // isn't set, the system partition will still be created, but no
70 // contents will be written into it.
71 if systemImage != "" {
72 img, err := os.Open(systemImage)
Lorenz Brun3a99c592021-01-26 19:57:21 +010073 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010074 log.Fatalf("while opening the system image at %q: %v", systemImage, err)
Lorenz Brun3a99c592021-01-26 19:57:21 +010075 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020076 defer img.Close()
77 cfg.SystemImage = img
78 }
79
80 // Attempt to open the node parameters file if its path is set.
81 if nodeParams != "" {
Lorenz Brunad131882023-06-28 16:42:20 +020082 np, err := blkio.NewFileReader(nodeParams)
Lorenz Brun3a99c592021-01-26 19:57:21 +010083 if err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010084 log.Fatalf("while opening node parameters at %q: %v", nodeParams, err)
Lorenz Brun3a99c592021-01-26 19:57:21 +010085 }
Mateusz Zalegac71efc92021-09-07 16:46:25 +020086 cfg.NodeParameters = np
Lorenz Brun3a99c592021-01-26 19:57:21 +010087 }
88
Lorenz Brunad131882023-06-28 16:42:20 +020089 // TODO(#254): Build and use dynamically-grown block devices
90 cfg.Output, err = blockdev.CreateFile(outputPath, 512, 10*1024*1024)
91 if err != nil {
92 panic(err)
93 }
94
Mateusz Zalegac71efc92021-09-07 16:46:25 +020095 // Write the parametrized OS image.
96 if _, err := osimage.Create(&cfg); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +010097 log.Fatalf("while creating a Metropolis OS image: %v", err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020098 }
Lorenz Brunae0d90d2019-09-05 17:53:56 +020099}