forked from florianl/bluebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
66 lines (55 loc) · 1.98 KB
/
init.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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"syscall"
"text/template"
exec "golang.org/x/sys/execabs"
)
// createInit writes a Go program and compiles it so it can be used as init.
func createInit(dir string, execs []string, args [][]string) error {
f, err := os.OpenFile(filepath.Join(dir, "init.go"), os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
config := Bluebox{
Arguments: args,
Environment: []Environment{
Mount{source: "devtmpfs", target: "/dev", fstype: "devtmpfs", flags: 0, data: "", targetPerm: 0o777, targetCreate: true},
Mount{source: "tmpfs", target: "/tmp", fstype: "tmpfs", flags: 0, data: "", targetPerm: 0o777, targetCreate: true},
Mount{source: "proc", target: "/proc", fstype: "proc", flags: 0, data: "", targetPerm: 0o555, targetCreate: true},
Nod{path: "/dev/tty", mode: syscall.S_IFCHR | 0o666, dev: 0x0500},
Nod{path: "/dev/urandom", mode: syscall.S_IFCHR | 0o444, dev: 0x0109},
Mount{source: "sysfs", target: "/sys", fstype: "sysfs", flags: 0, data: "", targetPerm: 0o555, targetCreate: true},
Mount{source: "securityfs", target: "/sys/kernel/security", fstype: "securityfs", flags: 0, data: ""},
Mount{source: "debugfs", target: "/sys/kernel/debug", fstype: "debugfs", flags: 0, data: ""},
Mount{source: "bpffs", target: "/sys/fs/bpf", fstype: "bpf", flags: 0, data: ""},
},
}
config.Executables = make([]string, len(execs))
// Strip the path from the executable before adding it.
for i, exe := range execs {
config.Executables[i] = filepath.Base(exe)
}
tmpl, err := template.New("").Parse(initTemplate)
if err != nil {
return err
}
if err := tmpl.Execute(f, config); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
cmd := exec.CommandContext(context.TODO(), "go", "build", "-o", filepath.Join(dir, "init"),
f.Name())
if arch != "" {
cmd.Env = append(os.Environ(), fmt.Sprintf("GOARCH=%s", arch))
}
if err := cmd.Run(); err != nil {
return err
}
return nil
}