-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
72 lines (58 loc) · 1.8 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
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
"mydocker/cgroups"
"mydocker/dockerfileParser"
)
func main() {
switch os.Args[1] {
case "run":
run()
case "child":
child()
default:
panic("unknown command")
}
}
func run() {
fmt.Printf("Running in parent %v as %d\n", os.Args[2:], os.Getpid())
// create a new command that will execute the current program in a new process.
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// NEWUTS -> unix timesharing system -> is the host name inside the container
// NEWNS -> new mount namespace -> isolates the mount points
// mount is recursively shared between the parent and child
cmd.SysProcAttr = &syscall.SysProcAttr {
Cloneflags: syscall.CLONE_NEWPID | syscall.CLONE_NEWUTS | syscall.CLONE_NEWNS,
Unshareflags: syscall.CLONE_NEWNS, // un-share the mount namespace, host won't see the child's mounts
}
if err := cmd.Start(); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
// Wait for the child process to finish
if err := cmd.Wait(); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}
// should already be in a new namespace
func child() {
fmt.Printf("Running in child %v as %d\n", os.Args[2:], os.Getpid())
cgroups.ConfigureCgroup()
syscall.Sethostname([]byte("container"))
syscall.Chroot("/docker-fs")
syscall.Chdir("/")
syscall.Mount("proc", "proc", "proc", 0, "")
instructions := dockerfileParser.ParseDockerfile("./express-app/Dockerfile")
for _, instruction := range instructions {
fmt.Println("Executing instruction: ", instruction)
dockerfileParser.ExecuteInstruction(instruction)
}
syscall.Unmount("/proc", 0)
}