-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
379 lines (348 loc) · 9.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2022 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.18
// +build go1.18
package main
import (
"flag"
"fmt"
"io/fs"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/hashicorp/go-multierror"
"github.com/u-root/u-root/pkg/cpio"
url "github.com/whilp/git-urls"
)
var (
version = "go1.17.7"
V = log.Printf
arch = runtime.GOARCH
kern = runtime.GOOS
bin string
testrun = true
dest = flag.String("d", "", "Destination directory -- default is os.MkdirTemp")
development = flag.Bool("D", true, "Use development (i.e.) pwd version of installcommand/init, not github version")
outCPIO = flag.String("cpio", "", "output cpio")
)
// Little note here: you'll see we use go/bin/go a lot, instead of kern_arch/bin/go.
// Lessons learned the hard way: go/bin/go seems to do a better job of finding
// GOROOT and other things when run from go/bin. We have not confirmed this from code,
// but from running it: best to run go/bin/go when doing this build.
// Will it work for replication? Remains to be seen.
func clone(tmp, version, repo, dir, base string) error {
V("clone: %q, %q, %q, %q", tmp, version, dir, base)
dest := filepath.Join(tmp, dir)
if err := os.MkdirAll(dest, 0755); err != nil {
return err
}
cmd := []string{"clone", "--depth", "1"}
if len(version) > 0 {
cmd = append(cmd, "-b", version)
}
cmd = append(cmd, repo)
c := exec.Command("git", cmd...)
c.Dir = dest
c.Stdout, c.Stderr = os.Stdout, os.Stderr
if err := c.Run(); err != nil {
return err
}
return nil
}
func tidy(tmp, dir, base string) error {
c := exec.Command(filepath.Join(tmp, "go/bin/go"), "mod", "tidy")
c.Stdout, c.Stderr = os.Stdout, os.Stderr
c.Env = append(c.Env, "GOPATH="+tmp)
c.Env = append(c.Env, "GOARCH="+arch, "GOOS="+kern, "GOROOT_FINAL=/go", "CGO_ENABLED=0")
if a, ok := os.LookupEnv("GOARM"); ok {
c.Env = append(c.Env, a)
}
c.Dir = filepath.Join(tmp, dir, base)
V("Run %v(%q, %q in %q)", c, c.Args, c.Env, c.Dir)
if err := c.Run(); err != nil {
return err
}
return nil
}
func modinit(tmp, host, dir, base string) error {
path := filepath.Join(tmp, dir, base)
V("modinit: check %q for go.mod", path)
if _, err := os.Stat(filepath.Join(path, "go.mod")); err == nil {
V("modinit: it has go.mod")
return nil
}
c := exec.Command(filepath.Join(tmp, "go/bin/go"), "mod", "init", filepath.Join(host, dir, base))
c.Stdout, c.Stderr = os.Stdout, os.Stderr
c.Env = append(c.Env, "GOPATH="+tmp)
c.Dir = path
V("Run %v(%q, %q in %q)", c, c.Args, c.Env, c.Dir)
if err := c.Run(); err != nil {
return err
}
return nil
}
func getgo(d, v string) error {
c := exec.Command("git", "clone", "-b", version, "--depth", "1", "[email protected]:golang/go")
c.Dir = d
c.Stdout, c.Stderr = os.Stdout, os.Stderr
if err := c.Run(); err != nil {
return err
}
// simply sanity check
gover := filepath.Join(d, "go", "VERSION")
dat, err := ioutil.ReadFile(gover)
if err != nil {
return fmt.Errorf("Reading %q: %v", gover, err)
}
if string(dat) != version {
return fmt.Errorf("Version file has %q, but want version %q", string(dat), version)
}
return nil
}
// build builds the code found in filepath.Join(tmp, dir)
// into bin.
func build(tmp, sourcePath, dir, bin string, extra ...string) error {
c := exec.Command(filepath.Join(tmp, "go/bin/go"), "build", "-o", bin)
c.Args = append(c.Args, extra...)
c.Dir = filepath.Join(sourcePath, dir)
c.Stdout, c.Stderr = os.Stdout, os.Stderr
c.Env = os.Environ()
c.Env = append(c.Env, "GOROOT_FINAL=/go", "CGO_ENABLED=0")
if err := c.Run(); err != nil {
return err
}
return nil
}
// buildToolchain builds the needed Go toolchain binaries: go, compile, link,
// asm. We can no longer do this without the script. Damn.
// TODO: figure out what files we can remove.
func buildToolchain(tmp string) error {
c := exec.Command("bash", "make.bash")
c.Dir = filepath.Join(tmp, "go/src")
c.Stdout, c.Stderr = os.Stdout, os.Stderr
c.Env = os.Environ()
c.Env = append(c.Env, "GOROOT_FINAL=/go", "CGO_ENABLED=0")
if err := c.Run(); err != nil {
return err
}
// Need to also build the go command itself.
c = exec.Command(filepath.Join(tmp, "go/bin/go"), "build", "-o", filepath.Join(tmp, bin, "go"))
c.Dir = filepath.Join(tmp, "go/src/cmd/go")
c.Stdout, c.Stderr = os.Stdout, os.Stderr
c.Env = os.Environ()
c.Env = append(c.Env, "GOARCH="+arch, "GOOS="+kern, "GOROOT_FINAL=/go", "CGO_ENABLED=0")
if a, ok := os.LookupEnv("GOARM"); ok {
c.Env = append(c.Env, a)
}
V("Build go toolchain, Args %v, Env %v", c.Args, c.Env)
if err := c.Run(); err != nil {
return err
}
return nil
}
func goName(p string) (string, string, string, error) {
u, err := url.ParseScp(p)
if err != nil {
return "", "", "", err
}
// The `Host` contains both the hostname and the port,
// if present. Use `SplitHostPort` to extract them.
fmt.Println(u.Host)
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
host = u.Host
}
return host, filepath.Dir(u.Path), filepath.Base(u.Path), nil
}
func get(target string, args ...string) error {
var err error
for _, d := range args {
V("Get %q", d)
host, dir, base, err := goName(d)
if err != nil {
V("URL %q: %v", d, err)
err = multierror.Append(err, fmt.Errorf("%q: %v", d, err))
continue
}
dir = filepath.Join(host, dir)
V("goName for %q: %q, %q, %q", d, host, dir, base)
if e := clone(target, "", d, dir, base); err != nil {
err = multierror.Append(err, e)
continue
}
if e := modinit(target, host, dir, base); e != nil {
err = multierror.Append(err, e)
continue
}
if e := tidy(target, dir, base); e != nil {
err = multierror.Append(err, e)
continue
}
}
return err
}
func init() {
if a, ok := os.LookupEnv("GOARCH"); ok {
arch = a
}
if a, ok := os.LookupEnv("GOOS"); ok {
kern = a
}
}
func tree(d string) error {
var err error
for _, n := range []string{"tmp", "dev", "etc"} {
if e := os.MkdirAll(filepath.Join(d, n), 0755); e != nil {
err = multierror.Append(err, e)
}
}
return err
}
func files(tmp, binpath, destdir string) error {
var err error
if err = os.MkdirAll(destdir, 0755); err != nil {
return err
}
include := filepath.Join(tmp, "go/pkg/include")
if err = os.MkdirAll(include, 0755); err != nil {
return err
}
// There are certain common patterns we know are commands.
// Just Do It.
var dirs []string
for _, g := range []string{
"/src/github.com/u-root/u-root/cmds/*/*",
"/src/github.com/u-root/NiChrome/cmds/*",
"/src/github.com/u-root/cpu/cmds/*",
"/src/github.com/nsf/godit",
} {
m, err := filepath.Glob(filepath.Join(tmp, g))
if err != nil {
V("%q: %v", g, err)
continue
}
dirs = append(dirs, m...)
}
for _, n := range dirs {
r, e := filepath.Rel(tmp, n)
if e != nil {
err = multierror.Append(err, e)
}
f := filepath.Join(destdir, filepath.Base(n))
dat := []byte("#!/" + binpath + "/installcommand #!/" + r + "\n")
V("Write %q with %q", f, dat)
if e := ioutil.WriteFile(f, dat, 0755); e != nil {
err = multierror.Append(err, e)
}
}
return err
}
func ramfs(from, out string, filter ...string) error {
to, err := os.Create(out)
if err != nil {
return err
}
log.Printf("Archiving to %v", out)
archiver, err := cpio.Format("newc")
if err != nil {
log.Fatalf("Format %q not supported: %v", "newc", err)
}
rw := archiver.Writer(to)
cr := cpio.NewRecorder()
if err := filepath.WalkDir(from, func(name string, _ fs.DirEntry, err error) error {
if err != nil {
return err
}
n, err := filepath.Rel(from, name)
if err != nil {
return err
}
V("Archive %q", name)
rec, err := cr.GetRecord(name)
rec.Name = n
if err != nil {
return fmt.Errorf("Getting record of %q failed: %v", name, err)
}
if err := rw.WriteRecord(rec); err != nil {
log.Fatalf("Writing record %q failed: %v", name, err)
}
return nil
}); err != nil {
return err
}
if err := cpio.WriteTrailer(rw); err != nil {
return fmt.Errorf("Error writing trailer record: %v", err)
}
return nil
}
func main() {
flag.Parse()
V("Building for %v_%v", arch, kern)
// Build the target directory
// Start with a temporary directory
// copy the toolchain there
// copy the rest of the other directories there
pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
d := *dest
if len(*dest) == 0 {
var err error
d, err = os.MkdirTemp("", "sourcery")
if err != nil {
log.Fatal(err)
}
}
defer fmt.Printf("Tree is %q\n", d)
if err != nil {
log.Fatal(err)
}
if err := tree(d); err != nil {
log.Fatal(err)
}
bin = filepath.Join(fmt.Sprintf("%v_%v", kern, arch), "bin")
if err := os.MkdirAll(filepath.Join(d, bin), 0755); err != nil {
log.Fatal(err)
}
if err := getgo(d, version); err != nil {
log.Printf("getgo errored, %v, keep going", err)
}
if err := buildToolchain(d); err != nil {
log.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(d, "src"), 0755); err != nil {
log.Fatal(err)
}
if err := get(filepath.Join(d, "src"), append(flag.Args(), "[email protected]:u-root/sourcery")...); err != nil {
log.Fatalf("Getting packages: %v", err)
}
if err := files(d, bin, filepath.Join(d, bin)); err != nil {
log.Fatal(err)
}
baseToolPath := filepath.Join(d, bin)
if *development {
baseToolPath = pwd
}
V("Build tools from %q", baseToolPath)
for _, tool := range []string{"installcommand", "init"} {
goBin := filepath.Join(d, bin, tool)
V("Build %q in %q, install to %q", tool, baseToolPath, goBin)
if err := build(d, baseToolPath, tool, goBin); err != nil {
log.Fatalf("Building %q -> %q: %v", goBin, tool, err)
}
}
if *outCPIO != "" {
if err := ramfs(d, *outCPIO); err != nil {
log.Printf("ramfs: %v", err)
}
}
log.Printf("sudo strace -o syscalltrace -f unshare -m chroot %q /%q_%q/bin/init", d, kern, arch)
log.Printf("unshare -m chroot %q /%q_%q/bin/init", d, kern, arch)
log.Printf("rsync -avz --no-owner --no-group -I %q somewhere", d)
}