forked from u-root/webboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webboot.go
268 lines (243 loc) · 7.66 KB
/
webboot.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
// Copyright 2019-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
// This program depends on the present of the u-root and NiChrome projects.
// First time use requires that you run
// go get -u github.com/u-root/u-root
// go get -u github.com/u-root/NiChrome/...
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
)
type cmd struct {
args []string
dir string
}
var (
debug = func(string, ...interface{}) {}
verbose = flag.Bool("v", true, "verbose debugging output")
uroot = flag.String("u", "", "options for u-root")
cmds = flag.String("c", "all", "u-root commands to build into the image")
ncmds = flag.String("n", "github.com/u-root/NiChrome/cmds/wifi", "NiChrome commands to build into the image")
bzImage = flag.String("bzImage", "", "Optional bzImage to embed in the initramfs")
iso = flag.String("iso", "", "Optional iso (e.g. tinycore.iso) to embed in the initramfs")
wifi = flag.Bool("wifi", true, "include wifi tools")
wpaVersion = flag.String("wpa-version", "system", "if set, download and build the wpa_supplicant (ex: 2.9)")
)
func init() {
flag.Parse()
if *verbose {
debug = log.Printf
}
}
// This function is a bit nasty but we'll need it until we can extend
// u-root a bit. Consider it a hack to get us ready for OSFC.
// the Must means it has to succeed or we die.
func extraBinMust(n string) string {
p, err := exec.LookPath(n)
if err != nil {
log.Fatalf("extraMustBin(%q): %v", n, err)
}
debug("Using %q from %q", n, p)
return p
}
// buildWPASupplicant downloads and builds the wpa_supplicant (and other tools)
// statically. The path containing these tools is returned.
func buildWPASupplicant(version string) (string, error) {
// Download and extract the tar release.
url := fmt.Sprintf("https://w1.fi/releases/wpa_supplicant-%s.tar.gz", version)
file := fmt.Sprintf("wpa_supplicant-%s.tar.gz", version)
extractDir := fmt.Sprintf("wpa_supplicant-%s", version)
workDir := filepath.Join(extractDir, "wpa_supplicant")
if _, err := os.Stat(extractDir); os.IsNotExist(err) {
// Download file.
if err := downloadFile(url, file); err != nil {
return "", err
}
// Extract the tar file.
debug("Extracting %q to %q...", file, extractDir)
tarArgs := []string{"-x", "-f", file}
if *verbose {
tarArgs = append(tarArgs, "-v")
}
cmd := exec.Command("tar", tarArgs...)
if *verbose {
cmd.Stdout = os.Stdout
}
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("Failed to extract %q: %v", file, err)
}
} else if err == nil {
log.Printf("Directory %q already exists, skipping download", extractDir)
} else {
return "", fmt.Errorf("error with stat on %q: %v", extractDir, err)
}
wantFiles := []string{
filepath.Join(workDir, "wpa_supplicant"),
filepath.Join(workDir, "wpa_cli"),
filepath.Join(workDir, "wpa_passphrase"),
}
if err := checkFilesExist(wantFiles); err == nil {
log.Printf("Files %v already exist, skipping build", wantFiles)
} else {
debug("Building wpa_supplicant...")
// Use the defconfig. Everything related to DBUS is stripped out
// because DBUS breaks the static build.
origin := filepath.Join(workDir, "defconfig")
destination := filepath.Join(workDir, ".config")
if err := filterFile(origin, destination, regexp.MustCompile("DBUS")); err != nil {
return "", fmt.Errorf("error creating .config: %v", err)
}
// Build with the following options:
// -Os: Optimize for size
// -flto: Link time optimization (reduces size)
// -static: No dynamic dependencies
// -pthread: Use gcc's version of pthreads to be static
// -s: Strip symbols
cmd := exec.Command("make", fmt.Sprintf("-j%d", runtime.NumCPU()), "EXTRA_CFLAGS=-Os -flto", "LDFLAGS=-static -pthread -Os -flto -s")
cmd.Dir = workDir
debug("cd %q && %s", workDir, cmd)
if *verbose {
cmd.Stdout = os.Stdout
}
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to compile wpa_supplicant: %v", err)
}
if err := checkFilesExist(wantFiles); err != nil {
return "", fmt.Errorf("failed to build files %v, they do not exist: %v", wantFiles, err)
}
}
return workDir, nil
}
// downloadFile download from the given url to the given file.
func downloadFile(url, file string) error {
debug("Downloading %q to %q...", url, file)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error downloading %q: %s", url, resp.Status)
}
f, err := os.Create(file)
if err != nil {
return err
}
if _, err = io.Copy(f, resp.Body); err != nil {
f.Close()
return err
}
return f.Close()
}
// checkFilesExist if each file exist.
func checkFilesExist(files []string) error {
for _, f := range files {
if _, err := os.Stat(f); os.IsNotExist(err) {
return fmt.Errorf("%q does not exist", f)
} else if err != nil {
return fmt.Errorf("error with stat on %q: %v", f, err)
}
}
return nil
}
// filterFile copies a file from origin to destination while deleting matching lines.
func filterFile(origin, destination string, filterOut *regexp.Regexp) error {
// Open the files.
originF, err := os.Open(origin)
if err != nil {
return err
}
defer originF.Close()
destF, err := os.Create(destination)
if err != nil {
return err
}
// Copy the lines.
s := bufio.NewScanner(originF)
for s.Scan() {
line := s.Text()
if !filterOut.MatchString(line) {
if _, err := destF.WriteString(line + "\n"); err != nil {
destF.Close()
return err
}
}
}
if err := s.Err(); err != nil {
destF.Close()
return err
}
return destF.Close()
}
func main() {
currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("error getting current directory %v", err)
}
// Use the system wpa_supplicant or download them.
if *wpaVersion != "system" {
wpaSupplicantPath, err := buildWPASupplicant(*wpaVersion)
if err != nil {
log.Fatalf("Error building wpa_supplicant: %v", err)
}
// Add to front of PATH to be picked up later.
if err := os.Setenv("PATH", fmt.Sprintf("%s:%s", wpaSupplicantPath, os.Getenv("PATH"))); err != nil {
log.Fatalf("Error setting PATH env variable: %v", err)
}
}
var args = []string{
"go", "run", "github.com/u-root/u-root/.",
"-files", "/etc/ssl/certs",
}
// Try to find the system kexec. We can not use LookPath as people
// building this might have the u-root kexec in their path.
if _, err := os.Stat("/sbin/kexec"); err == nil {
args = append(args, "-files=/sbin/kexec")
}
if *wifi {
args = append(args,
"-files", extraBinMust("iwconfig"),
"-files", extraBinMust("iwlist"),
"-files", extraBinMust("wpa_supplicant")+":/bin/wpa_supplicant",
"-files", extraBinMust("wpa_cli")+":/bin/wpa_cli",
"-files", extraBinMust("wpa_passphrase")+":/bin/wpa_passphrase",
"-files", filepath.Join(currentDir, "cmds", "webboot", "webboot")+":bbin/webboot",
"-files", extraBinMust("strace"),
"-files", "cmds/webboot/distros.json:distros.json",
)
}
if *bzImage != "" {
args = append(args, "-files", *bzImage+":bzImage")
}
if *iso != "" {
args = append(args, "-files", *iso+":iso")
}
var commands = []cmd{
{args: []string{"go", "build"}, dir: filepath.Join(currentDir, "cmds", "webboot")},
{args: append(append(args, strings.Fields(*uroot)...), *cmds, *ncmds)},
}
for _, cmd := range commands {
debug("Run %v", cmd)
c := exec.Command(cmd.args[0], cmd.args[1:]...)
c.Stdout, c.Stderr = os.Stdout, os.Stderr
c.Dir = cmd.dir
if err := c.Run(); err != nil {
log.Fatalf("%s failed: %v", cmd, err)
}
}
debug("done")
}