blob: a05e440e842ca194dd042629b1c17a3b1f9bd7b5 [file] [log] [blame]
Lorenz Brun6b13bf12021-01-26 19:54:24 +01001// 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
17// mkerofs takes a specification in the form of a prototext file (see fsspec next to this) and assembles an
18// EROFS filesystem according to it. The output is fully reproducible.
19package main
20
21import (
22 "flag"
23 "io"
24 "io/ioutil"
25 "log"
26 "os"
27 "path"
28 "sort"
29 "strings"
30
31 "github.com/golang/protobuf/proto"
32
33 "source.monogon.dev/metropolis/node/build/mkerofs/fsspec"
34 "source.monogon.dev/metropolis/pkg/erofs"
35)
36
37func (spec *entrySpec) writeRecursive(w *erofs.Writer, pathname string) {
38 switch inode := spec.data.Type.(type) {
39 case *fsspec.Inode_Directory:
40 // Sort children for reproducibility
41 var sortedChildren []string
42 for name := range spec.children {
43 sortedChildren = append(sortedChildren, name)
44 }
45 sort.Strings(sortedChildren)
46
47 err := w.Create(pathname, &erofs.Directory{
48 Base: erofs.Base{
49 Permissions: uint16(inode.Directory.Mode),
50 UID: uint16(inode.Directory.Uid),
51 GID: uint16(inode.Directory.Gid),
52 },
53 Children: sortedChildren,
54 })
55 if err != nil {
56 log.Fatalf("failed to write directory: %s", err)
57 }
58 for _, name := range sortedChildren {
59 spec.children[name].writeRecursive(w, path.Join(pathname, name))
60 }
61 case *fsspec.Inode_File:
62 iw := w.CreateFile(pathname, &erofs.FileMeta{
63 Base: erofs.Base{
64 Permissions: uint16(inode.File.Mode),
65 UID: uint16(inode.File.Uid),
66 GID: uint16(inode.File.Gid),
67 },
68 })
69
70 sourceFile, err := os.Open(inode.File.SourcePath)
71 if err != nil {
72 log.Fatalf("failed to open source file %s: %s", inode.File.SourcePath, err)
73 }
74
75 _, err = io.Copy(iw, sourceFile)
76 if err != nil {
77 log.Fatalf("failed to copy file into filesystem: %s", err)
78 }
79 sourceFile.Close()
80 if err := iw.Close(); err != nil {
81 log.Fatalf("failed to close target file: %s", err)
82 }
83 case *fsspec.Inode_SymbolicLink:
84 err := w.Create(pathname, &erofs.SymbolicLink{
85 Base: erofs.Base{
86 Permissions: 0777, // Nominal, Linux forces that mode anyways, see symlink(7)
87 },
88 Target: inode.SymbolicLink.TargetPath,
89 })
90 if err != nil {
91 log.Fatalf("failed to create symbolic link: %s", err)
92 }
93 }
94}
95
96// entrySpec is a recursive structure representing the filesystem tree
97type entrySpec struct {
98 data fsspec.Inode
99 children map[string]*entrySpec
100}
101
102// pathRef gets the entrySpec at the leaf of the given path, inferring directories if necessary
103func (s *entrySpec) pathRef(p string) *entrySpec {
104 // This block gets a path array starting at the root of the filesystem. The root folder is the zero-length array.
105 pathParts := strings.Split(path.Clean("./"+p), "/")
106 if pathParts[0] == "." {
107 pathParts = pathParts[1:]
108 }
109
110 entryRef := s
111 for _, part := range pathParts {
112 childRef, ok := entryRef.children[part]
113 if !ok {
114 childRef = &entrySpec{
115 data: fsspec.Inode{Type: &fsspec.Inode_Directory{Directory: &fsspec.Directory{Mode: 0555}}},
116 children: make(map[string]*entrySpec),
117 }
118 entryRef.children[part] = childRef
119 }
120 entryRef = childRef
121 }
122 return entryRef
123}
124
125var (
126 specPath = flag.String("spec", "", "Path to the filesystem specification (spec.FSSpec)")
127 outPath = flag.String("out", "", "Output file path")
128)
129
130func main() {
131 flag.Parse()
132 specRaw, err := ioutil.ReadFile(*specPath)
133 if err != nil {
134 log.Fatalf("failed to open spec: %v", err)
135 }
136
137 var spec fsspec.FSSpec
138 if err := proto.UnmarshalText(string(specRaw), &spec); err != nil {
139 log.Fatalf("failed to parse spec: %v", err)
140 }
141
142 var fsRoot = &entrySpec{
143 data: fsspec.Inode{Type: &fsspec.Inode_Directory{Directory: &fsspec.Directory{Mode: 0555}}},
144 children: make(map[string]*entrySpec),
145 }
146
147 for _, dir := range spec.Directory {
148 entryRef := fsRoot.pathRef(dir.Path)
149 entryRef.data.Type = &fsspec.Inode_Directory{Directory: dir}
150 }
151
152 for _, file := range spec.File {
153 entryRef := fsRoot.pathRef(file.Path)
154 entryRef.data.Type = &fsspec.Inode_File{File: file}
155 }
156
157 for _, symlink := range spec.SymbolicLink {
158 entryRef := fsRoot.pathRef(symlink.Path)
159 entryRef.data.Type = &fsspec.Inode_SymbolicLink{SymbolicLink: symlink}
160 }
161
162 fs, err := os.Create(*outPath)
163 if err != nil {
164 log.Fatalf("failed to open output file: %v", err)
165 }
166 writer, err := erofs.NewWriter(fs)
167 if err != nil {
168 log.Fatalf("failed to initialize EROFS writer: %v", err)
169 }
170
171 fsRoot.writeRecursive(writer, ".")
172
173 if err := writer.Close(); err != nil {
174 panic(err)
175 }
176 if err := fs.Close(); err != nil {
177 panic(err)
178 }
179}