blob: 94b2ecb040cf922fc74edf69c005a360dfb92bec [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
17package main
18
Serge Bazanski662b5b32020-12-21 13:49:00 +010019// mkimage is a tool to generate a Metropolis node disk image containing the
20// given EFI payload, and optionally, a given external initramfs image and
21// enrolment credentials.
Serge Bazanski032ca182020-06-09 20:17:13 +020022
Lorenz Brunae0d90d2019-09-05 17:53:56 +020023import (
Leopold Schabel65493072019-11-06 13:40:44 +000024 "flag"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020025 "fmt"
26 "io/ioutil"
Serge Bazanski032ca182020-06-09 20:17:13 +020027 "log"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020028 "os"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020029
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010030 diskfs "github.com/diskfs/go-diskfs"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020031 "github.com/diskfs/go-diskfs/disk"
32 "github.com/diskfs/go-diskfs/filesystem"
33 "github.com/diskfs/go-diskfs/partition/gpt"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020034)
35
Serge Bazanski662b5b32020-12-21 13:49:00 +010036var NodeDataPartition gpt.Type = gpt.Type("9eeec464-6885-414a-b278-4305c51f7966")
Lorenz Brunae0d90d2019-09-05 17:53:56 +020037
Leopold Schabel65493072019-11-06 13:40:44 +000038var (
Serge Bazanski032ca182020-06-09 20:17:13 +020039 flagEFI string
40 flagOut string
41 flagInitramfs string
42 flagEnrolmentCredentials string
43 flagDataPartitionSize uint64
44 flagESPPartitionSize uint64
Leopold Schabel65493072019-11-06 13:40:44 +000045)
46
Lorenz Brunae0d90d2019-09-05 17:53:56 +020047func mibToSectors(size uint64) uint64 {
48 return (size * 1024 * 1024) / 512
49}
50
Lorenz Brunae0d90d2019-09-05 17:53:56 +020051func main() {
Serge Bazanski032ca182020-06-09 20:17:13 +020052 flag.StringVar(&flagEFI, "efi", "", "UEFI payload")
53 flag.StringVar(&flagOut, "out", "", "Output disk image")
54 flag.StringVar(&flagInitramfs, "initramfs", "", "External initramfs [optional]")
55 flag.StringVar(&flagEnrolmentCredentials, "enrolment_credentials", "", "Enrolment credentials [optional]")
56 flag.Uint64Var(&flagDataPartitionSize, "data_partition_size", 2048, "Override the data partition size (default 2048 MiB)")
57 flag.Uint64Var(&flagESPPartitionSize, "esp_partition_size", 512, "Override the ESP partition size (default: 512MiB)")
Leopold Schabel65493072019-11-06 13:40:44 +000058 flag.Parse()
Serge Bazanski032ca182020-06-09 20:17:13 +020059
60 if flagEFI == "" || flagOut == "" {
61 log.Fatalf("efi and initramfs must be set")
Lorenz Brunf95909d2019-09-11 19:48:26 +020062 }
Leopold Schabel65493072019-11-06 13:40:44 +000063
Serge Bazanski032ca182020-06-09 20:17:13 +020064 _ = os.Remove(flagOut)
65 diskImg, err := diskfs.Create(flagOut, 3*1024*1024*1024, diskfs.Raw)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020066 if err != nil {
Serge Bazanski032ca182020-06-09 20:17:13 +020067 log.Fatalf("diskfs.Create(%q): %v", flagOut, err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020068 }
69
70 table := &gpt.Table{
71 // This is appropriate at least for virtio disks. Might need to be adjusted for real ones.
72 LogicalSectorSize: 512,
73 PhysicalSectorSize: 512,
74 ProtectiveMBR: true,
75 Partitions: []*gpt.Partition{
76 {
77 Type: gpt.EFISystemPartition,
78 Name: "ESP",
79 Start: mibToSectors(1),
Serge Bazanski032ca182020-06-09 20:17:13 +020080 End: mibToSectors(flagESPPartitionSize) - 1,
Lorenz Brunae0d90d2019-09-05 17:53:56 +020081 },
82 {
Serge Bazanski662b5b32020-12-21 13:49:00 +010083 Type: NodeDataPartition,
84 Name: "METROPOLIS-NODE-DATA",
Serge Bazanski032ca182020-06-09 20:17:13 +020085 Start: mibToSectors(flagESPPartitionSize),
86 End: mibToSectors(flagESPPartitionSize+flagDataPartitionSize) - 1,
Lorenz Brunae0d90d2019-09-05 17:53:56 +020087 },
88 },
89 }
90 if err := diskImg.Partition(table); err != nil {
Serge Bazanski032ca182020-06-09 20:17:13 +020091 log.Fatalf("Failed to apply partition table: %v", err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020092 }
93
94 fs, err := diskImg.CreateFilesystem(disk.FilesystemSpec{Partition: 1, FSType: filesystem.TypeFat32, VolumeLabel: "ESP"})
95 if err != nil {
Serge Bazanski032ca182020-06-09 20:17:13 +020096 log.Fatalf("Failed to create filesystem: %v", err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020097 }
Serge Bazanski032ca182020-06-09 20:17:13 +020098
99 // Create EFI partition structure.
Serge Bazanski662b5b32020-12-21 13:49:00 +0100100 for _, dir := range []string{"/EFI", "/EFI/BOOT", "/EFI/metropolis"} {
Serge Bazanski032ca182020-06-09 20:17:13 +0200101 if err := fs.Mkdir(dir); err != nil {
102 log.Fatalf("Mkdir(%q): %v", dir, err)
Lorenz Brun0bcaaee2019-11-06 12:42:39 +0100103 }
104 }
Serge Bazanski032ca182020-06-09 20:17:13 +0200105
106 put(fs, flagEFI, "/EFI/BOOT/BOOTX64.EFI")
107
108 if flagInitramfs != "" {
Serge Bazanski662b5b32020-12-21 13:49:00 +0100109 put(fs, flagInitramfs, "/EFI/metropolis/initramfs.cpio.lz4")
Lorenz Brunaa6b7342019-12-12 02:55:02 +0100110 }
Serge Bazanski032ca182020-06-09 20:17:13 +0200111
112 if flagEnrolmentCredentials != "" {
Serge Bazanski662b5b32020-12-21 13:49:00 +0100113 put(fs, flagEnrolmentCredentials, "/EFI/metropolis/enrolment.pb")
Serge Bazanski032ca182020-06-09 20:17:13 +0200114 }
115
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200116 if err := diskImg.File.Close(); err != nil {
Serge Bazanski032ca182020-06-09 20:17:13 +0200117 log.Fatalf("Failed to finalize image: %v", err)
118 }
119 log.Printf("Success! You can now boot %v", flagOut)
120}
121
122// put copies a file from the host filesystem into the target image.
123func put(fs filesystem.FileSystem, src, dst string) {
124 target, err := fs.OpenFile(dst, os.O_CREATE|os.O_RDWR)
125 if err != nil {
126 log.Fatalf("fs.OpenFile(%q): %v", dst, err)
127 }
128 source, err := os.Open(src)
129 if err != nil {
130 log.Fatalf("os.Open(%q): %v", src, err)
131 }
132 defer source.Close()
133 // If this is streamed (e.g. using io.Copy) it exposes a bug in diskfs, so do it in one go.
134 data, err := ioutil.ReadAll(source)
135 if err != nil {
136 log.Fatalf("Reading %q: %v", src, err)
137 }
138 if _, err := target.Write(data); err != nil {
139 fmt.Printf("writing file %q: %v", dst, err)
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200140 os.Exit(1)
141 }
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200142}