-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (121 loc) · 3.2 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
package main
import (
"io/ioutil"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
)
func main() {
if len(os.Args) < 3 {
help()
}
switch os.Args[1] {
case "run":
if len(os.Args) < 3 {
help()
} else {
run(os.Args[2], os.Args[3:])
}
case "child":
child(os.Args[2], os.Args[3:])
}
}
func run(image string, args []string) {
cmd := exec.Command("/proc/self/exe", append([]string{"child", image}, args[0:]...)...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS,
Unshareflags: syscall.CLONE_NEWNS,
}
cmd.Run()
}
func child(image string, args []string) {
log.Println("Running ", args, " on image ", image)
homeDir, _ := os.UserHomeDir()
gockerDir := filepath.Join(homeDir, ".gocker")
// Create the gocker directory if it doesn't exist
if _, err := os.Stat(gockerDir); err != nil {
os.Mkdir(gockerDir, 0755)
}
fsDir, manifestConfig := DownloadImage(image, gockerDir)
containerID := generateContainerID()
setCGroup(containerID)
err := syscall.Sethostname([]byte(containerID))
if err != nil {
log.Fatal("Could not set hostname")
}
err = syscall.Chroot(fsDir)
if err != nil {
log.Fatal("Could not chroot to image filesystem")
}
err = os.Chdir("/")
if err != nil {
log.Fatal("Could not chdir to the root filesystem")
}
err = syscall.Mount("proc", "proc", "proc", 0, "")
if err != nil {
log.Fatal("Could not mount the proc filesystem")
}
// If the image manifest defines a working directory,
// chdir to it
if len(manifestConfig.WorkingDir) > 0 {
err = os.Chdir(manifestConfig.WorkingDir)
if err != nil {
log.Fatal("Could not chdir to workingDir")
}
}
for _, c := range manifestConfig.Env {
sEnv := strings.SplitN(c, "=", 2)
os.Setenv(sEnv[0], sEnv[1])
}
var cmdOrEntrypoint []string
if len(args) > 0 {
cmdOrEntrypoint = args
} else if len(manifestConfig.Cmd) > 0 {
cmdOrEntrypoint = manifestConfig.Cmd
} else if len(manifestConfig.Entrypoint) > 0 {
cmdOrEntrypoint = manifestConfig.Entrypoint
} else {
log.Fatal("No command provided")
}
var cmd *exec.Cmd
if len(cmdOrEntrypoint) >= 2 {
cmd = exec.Command(cmdOrEntrypoint[0], cmdOrEntrypoint[1:]...)
} else {
cmd = exec.Command(cmdOrEntrypoint[0])
}
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
log.Println("Starting container", containerID)
cmd.Run()
syscall.Unmount("proc", 0)
}
func setCGroup(id string) {
cgroups := "/sys/fs/cgroup/"
pids := filepath.Join(cgroups, "pids")
os.Mkdir(pids, 0755) // Make the pids dir if it doesn't exist
os.Mkdir(filepath.Join(pids, id), 0755)
ioutil.WriteFile(filepath.Join(pids, id, "notify_on_release"), []byte("1"), 0700)
ioutil.WriteFile(filepath.Join(pids, id, "cgroup.procs"), []byte(strconv.Itoa(os.Getpid())), 0700)
}
func generateContainerID() string {
rand.Seed(time.Now().UnixNano())
vals := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
id := make([]byte, 32)
for i := range id {
id[i] = vals[rand.Intn(len(vals))]
}
return string(id)
}
func help() {
log.Fatal("Invalid command or missing parameters")
}