-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (86 loc) · 2.15 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
package main
import (
"errors"
"log"
"os"
"os/exec"
"syscall"
"github.com/docker/docker/pkg/reexec"
)
func init() {
reexec.Register("nsInitialisation", nsInitialisation)
if reexec.Init() {
os.Exit(0)
}
}
func nsInitialisation() {
newroot := os.Args[1]
if err := mountProc(newroot); err != nil {
log.Fatalf("error mounting /proc - %s", err)
}
if err := mountSys(newroot); err != nil {
log.Fatalf("error mounting /sys - %s", err)
}
if err := pivot_root(newroot); err != nil {
log.Fatalf("error running pivot_root - %s", err)
}
if err := syscall.Sethostname([]byte("sandbox")); err != nil {
log.Fatalf("error setting hostname - %s", err)
}
nsRun()
}
func nsRun() {
cmd := exec.Command("/bin/sh")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = []string{`PS1=\w \$ `}
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
func main() {
var rootfsPath = "/tmp/ns-process/rootfs"
if len(os.Args) > 3 {
log.Fatalln(errors.New("invalid args: arguments exceeded expected amount"))
}
if len(os.Args) >= 2 {
rootfsPath = os.Args[1]
}
cmd := reexec.Command("nsInitialisation", rootfsPath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
// flags for the clone command
Cloneflags: syscall.CLONE_NEWNS |
syscall.CLONE_NEWUTS |
syscall.CLONE_NEWIPC |
syscall.CLONE_NEWPID |
syscall.CLONE_NEWNET |
syscall.CLONE_NEWUSER,
// this is to break the mountns' link to the initial ns.
// this prevents the sandbox from populating the initial ns' /proc/mount file
Unshareflags: syscall.CLONE_NEWNS,
// mapping uid in the new ns to it's uid in the initial namespace. Unmapped users
// are assigned the overflow uid (65534) so we're mapping the container id 0 (root) to
// the corresponding uid in the initial namespace.
UidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: os.Getuid(),
Size: 1,
},
},
GidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: os.Getgid(),
Size: 1,
},
},
}
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}