blob: 1238c0489da7a2e4f3e5a5c0796795b4ff88fcf6 [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
19import (
Leopold Schabel65493072019-11-06 13:40:44 +000020 "flag"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020021 "fmt"
22 "io/ioutil"
23 "os"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020024
Hendrik Hofstadt8efe51e2020-02-28 12:53:41 +010025 diskfs "github.com/diskfs/go-diskfs"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020026 "github.com/diskfs/go-diskfs/disk"
27 "github.com/diskfs/go-diskfs/filesystem"
28 "github.com/diskfs/go-diskfs/partition/gpt"
Lorenz Brunae0d90d2019-09-05 17:53:56 +020029)
30
31var SmalltownDataPartition gpt.Type = gpt.Type("9eeec464-6885-414a-b278-4305c51f7966")
32
Leopold Schabel65493072019-11-06 13:40:44 +000033var (
Lorenz Brunaa6b7342019-12-12 02:55:02 +010034 efiPayloadPath = flag.String("efi", "", "UEFI payload")
35 outputPath = flag.String("out", "", "Output disk image")
36 initramfsPath = flag.String("initramfs", "", "External initramfs [optional]")
37 enrolmentCredentialsPath = flag.String("enrolment-credentials", "", "Enrolment credentials [optional]")
Lorenz Brun878f5f92020-05-12 16:15:39 +020038 dataPartitionSizeMiB = flag.Uint64("data-partition-size", 2048, "Override the data partition size (default 2048 MiB)")
Leopold Schabel65493072019-11-06 13:40:44 +000039)
40
Lorenz Brunae0d90d2019-09-05 17:53:56 +020041func mibToSectors(size uint64) uint64 {
42 return (size * 1024 * 1024) / 512
43}
44
Lorenz Brunae0d90d2019-09-05 17:53:56 +020045func main() {
Leopold Schabel65493072019-11-06 13:40:44 +000046 flag.Parse()
47 if *efiPayloadPath == "" || *outputPath == "" {
48 flag.PrintDefaults()
Lorenz Brunf95909d2019-09-11 19:48:26 +020049 os.Exit(2)
50 }
Leopold Schabel65493072019-11-06 13:40:44 +000051
52 _ = os.Remove(*outputPath)
53 diskImg, err := diskfs.Create(*outputPath, 3*1024*1024*1024, diskfs.Raw)
Lorenz Brunae0d90d2019-09-05 17:53:56 +020054 if err != nil {
55 fmt.Printf("Failed to create disk: %v", err)
56 os.Exit(1)
57 }
58
59 table := &gpt.Table{
60 // This is appropriate at least for virtio disks. Might need to be adjusted for real ones.
61 LogicalSectorSize: 512,
62 PhysicalSectorSize: 512,
63 ProtectiveMBR: true,
64 Partitions: []*gpt.Partition{
65 {
66 Type: gpt.EFISystemPartition,
67 Name: "ESP",
68 Start: mibToSectors(1),
Lorenz Brun0bcaaee2019-11-06 12:42:39 +010069 End: mibToSectors(256) - 1,
Lorenz Brunae0d90d2019-09-05 17:53:56 +020070 },
71 {
72 Type: SmalltownDataPartition,
73 Name: "SIGNOS-DATA",
Lorenz Brun0bcaaee2019-11-06 12:42:39 +010074 Start: mibToSectors(256),
Lorenz Brun878f5f92020-05-12 16:15:39 +020075 End: mibToSectors(*dataPartitionSizeMiB+256) - 1,
Lorenz Brunae0d90d2019-09-05 17:53:56 +020076 },
77 },
78 }
79 if err := diskImg.Partition(table); err != nil {
80 fmt.Printf("Failed to apply partition table: %v", err)
81 os.Exit(1)
82 }
83
84 fs, err := diskImg.CreateFilesystem(disk.FilesystemSpec{Partition: 1, FSType: filesystem.TypeFat32, VolumeLabel: "ESP"})
85 if err != nil {
86 fmt.Printf("Failed to create filesystem: %v", err)
87 os.Exit(1)
88 }
89 if err := fs.Mkdir("/EFI"); err != nil {
90 panic(err)
91 }
92 if err := fs.Mkdir("/EFI/BOOT"); err != nil {
93 panic(err)
94 }
95 if err := fs.Mkdir("/EFI/smalltown"); err != nil {
96 panic(err)
97 }
98 efiPayload, err := fs.OpenFile("/EFI/BOOT/BOOTX64.EFI", os.O_CREATE|os.O_RDWR)
99 if err != nil {
100 fmt.Printf("Failed to open EFI payload for writing: %v", err)
101 os.Exit(1)
102 }
Leopold Schabel65493072019-11-06 13:40:44 +0000103 efiPayloadSrc, err := os.Open(*efiPayloadPath)
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200104 if err != nil {
105 fmt.Printf("Failed to open EFI payload for reading: %v", err)
106 os.Exit(1)
107 }
108 defer efiPayloadSrc.Close()
109 // If this is streamed (e.g. using io.Copy) it exposes a bug in diskfs, so do it in one go.
110 efiPayloadFull, err := ioutil.ReadAll(efiPayloadSrc)
111 if err != nil {
112 panic(err)
113 }
114 if _, err := efiPayload.Write(efiPayloadFull); err != nil {
115 fmt.Printf("Failed to write EFI payload: %v", err)
116 os.Exit(1)
117 }
Lorenz Brun0bcaaee2019-11-06 12:42:39 +0100118 initramfs, err := fs.OpenFile("/EFI/smalltown/initramfs.cpio.lz4", os.O_CREATE|os.O_RDWR)
119 if err != nil {
120 fmt.Printf("Failed to open initramfs for writing: %v", err)
121 os.Exit(1)
122 }
123 // If we have more than two arguments, the second one is the initramfs
Leopold Schabel65493072019-11-06 13:40:44 +0000124 if *initramfsPath != "" {
125 initramfsSrc, err := os.Open(*initramfsPath)
Lorenz Brun0bcaaee2019-11-06 12:42:39 +0100126 if err != nil {
127 fmt.Printf("Failed to open initramfs for reading: %v", err)
128 os.Exit(1)
129 }
130 initramfsFull, err := ioutil.ReadAll(initramfsSrc)
131 if err != nil {
132 fmt.Printf("Failed to read initramfs: %v", err)
133 os.Exit(1)
134 }
135 if _, err := initramfs.Write(initramfsFull); err != nil {
136 fmt.Printf("Failed to write initramfs: %v", err)
137 os.Exit(1)
138 }
139 }
Lorenz Brunaa6b7342019-12-12 02:55:02 +0100140 if *enrolmentCredentialsPath != "" {
141 enrolmentCredentials, err := fs.OpenFile("/EFI/smalltown/enrolment.pb", os.O_CREATE|os.O_RDWR)
142 enrolmentCredentialsSrc, err := os.Open(*enrolmentCredentialsPath)
143 if err != nil {
144 fmt.Printf("Failed to open enrolment credentials for reading: %v", err)
145 os.Exit(1)
146 }
147 enrolmentCredentialsFull, err := ioutil.ReadAll(enrolmentCredentialsSrc)
148 if err != nil {
149 fmt.Printf("Failed to read enrolment credentials: %v", err)
150 os.Exit(1)
151 }
152 if _, err := enrolmentCredentials.Write(enrolmentCredentialsFull); err != nil {
153 fmt.Printf("Failed to write enrolment credentials")
154 os.Exit(1)
155 }
156 }
Lorenz Brunae0d90d2019-09-05 17:53:56 +0200157 if err := diskImg.File.Close(); err != nil {
158 fmt.Printf("Failed to write image: %v", err)
159 os.Exit(1)
160 }
161 fmt.Println("Success! You can now boot smalltown.img")
162}