-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathu-bmc.go
194 lines (180 loc) · 3.59 KB
/
u-bmc.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
// Copyright 2015-2021 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 main
import (
"flag"
"log"
"os"
"path/filepath"
"github.com/u-root/gobusybox/src/pkg/bb"
"github.com/u-root/gobusybox/src/pkg/golang"
)
// Flags for the gobusybox builder
var (
// Using this explicit list we can easier restrict which commands
// end up in our busybox binary
urootWhitelist = []string{
"backoff",
"basename",
"bind",
"blkid",
"cat",
"chmod",
"cmp",
"comm",
"cp",
"date",
"dd",
"df",
"dirname",
"dmesg",
"echo",
"elvish", //TODO(MDr164) maybe use upstream instead?
"false",
"find",
"free",
"grep",
"hexdump",
"hostname",
"hwclock",
"id",
"init", //TODO(MDr164) write own slimmed down u-bmc init
"io",
"ip",
"kill",
"ln",
"losetup",
"ls",
"lsmod",
"man",
"md5sum", //TODO(MDr164) should be obsolete
"mkdir",
"mkfifo",
"mknod",
"mktemp",
"more",
"mount",
"mv",
"netcat",
"pci",
"printenv",
"ps",
"pwd",
"readlink",
"rm",
"scp",
"seq",
"shasum",
"shutdown",
"sleep",
"sort",
"sshd", //TODO(MDr164) replace with in-process sshd
"strace",
"strings",
"stty",
"sync",
"tail",
"tar",
"tee",
"time",
"tr",
"true",
"truncate",
"ts",
"umount",
"unshare", //TODO(MDr164) probably not needed
"uptime",
"watchdog",
"watchdogd",
"wc",
"wget",
"which",
"yes",
}
)
func main() {
flag.Parse()
// Main is in a separate functions so defers run on return
if err := Main(); err != nil {
log.Fatal(err)
}
log.Print("Successfully created root directory")
}
// Main is a separate function so defers are run on return, which they wouldn't
// on exit
func Main() error {
// Make sure to disable CGO as it's not supported
env := golang.Default()
if env.CgoEnabled {
env.CgoEnabled = false
}
// Resolve paths to all individual go commands for the busybox
urootcmds := urootAbsEach(urootWhitelist)
bmccmds, err := filepath.Glob("../cmd/*")
if err != nil {
return err
}
var extracmds []string
for _, arg := range flag.Args() {
path, err := filepath.Glob(arg)
if err != nil {
continue
}
extracmds = append(extracmds, path...)
}
var commands []string
commands = append(commands, bmccmds...)
commands = append(commands, urootcmds...)
commands = append(commands, extracmds...)
pwd, err := os.Getwd()
if err != nil {
return err
}
// Define some options for building the busybox
opts := bb.Opts{
Env: env,
CommandPaths: commands,
BinaryPath: pwd + "/rootfs/bin/bb",
GoBuildOpts: &golang.BuildOpts{
NoStrip: false,
NoTrimPath: false,
EnableInlining: false,
},
AllowMixedMode: true,
}
// Build the bb binary
err = bb.BuildBusybox(&opts)
if err != nil {
return err
}
// Create symlinks for the bb binary and all commands
for _, cmd := range commands {
os.RemoveAll("rootfs/bin/" + filepath.Base(cmd))
err := os.Symlink("bb", "rootfs/bin/"+filepath.Base(cmd))
if err != nil {
return err
}
}
os.RemoveAll("rootfs/bin/sh")
err = os.Symlink("elvish", "rootfs/bin/sh")
if err != nil {
return err
}
// Create default directory structure
directories := []string{"dev", "proc", "sys", "usr/lib", "var/log", "tmp", "etc", "boot", "config"}
for _, dir := range directories {
err := os.MkdirAll("rootfs/"+dir, 0755)
if err != nil {
return err
}
}
return nil
}
func urootAbsEach(in []string) []string {
out := make([]string, len(in))
for i, s := range in {
out[i], _ = filepath.Abs("../../u-root/cmds/core/" + s)
}
return out
}