forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bb.go
71 lines (60 loc) · 1.95 KB
/
bb.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
// Copyright 2015-2017 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.
package builder
import (
"fmt"
"path"
"path/filepath"
"github.com/u-root/u-root/pkg/bb"
"github.com/u-root/u-root/pkg/cpio"
"github.com/u-root/u-root/pkg/uroot/initramfs"
)
// Commands to skip building in bb mode.
var skip = map[string]struct{}{
"bb": {},
}
// BBBuilder is an implementation of Builder that compiles many Go commands
// into one busybox-style binary.
//
// BBBuilder will also include symlinks for each command to the busybox binary.
//
// BBBuilder does all this by rewriting the source files of the packages given
// to create one busybox-like binary containing all commands.
//
// The compiled binary uses argv[0] to decide which Go command to run.
//
// See bb/README.md for a detailed explanation of the implementation of busybox
// mode.
type BBBuilder struct{}
// DefaultBinaryDir implements Builder.DefaultBinaryDir.
//
// The default initramfs binary dir is bbin for busybox binaries.
func (BBBuilder) DefaultBinaryDir() string {
return "bbin"
}
// Build is an implementation of Builder.Build for a busybox-like initramfs.
func (BBBuilder) Build(af *initramfs.Files, opts Opts) error {
// Build the busybox binary.
bbPath := filepath.Join(opts.TempDir, "bb")
if err := bb.BuildBusybox(opts.Env, opts.Packages, bbPath); err != nil {
return err
}
if len(opts.BinaryDir) == 0 {
return fmt.Errorf("must specify binary directory")
}
if err := af.AddFile(bbPath, path.Join(opts.BinaryDir, "bb")); err != nil {
return err
}
// Add symlinks for included commands to initramfs.
for _, pkg := range opts.Packages {
if _, ok := skip[path.Base(pkg)]; ok {
continue
}
// Add a symlink /bbin/{cmd} -> /bbin/bb to our initramfs.
if err := af.AddRecord(cpio.Symlink(filepath.Join(opts.BinaryDir, path.Base(pkg)), "bb")); err != nil {
return err
}
}
return nil
}