-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (64 loc) · 1.6 KB
/
Copy pathmain.go
File metadata and controls
80 lines (64 loc) · 1.6 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
)
func main() {
switch os.Args[1] {
case "run":
run()
case "child":
child()
default:
panic("invalid command")
}
}
func run() {
fmt.Printf("Running %v as %v \n", os.Args[2:], os.Getpid())
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
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() {
fmt.Printf("running %v as %v \n", os.Args[2:], os.Getpid())
cgroup()
syscall.Sethostname([]byte("dockerlito"))
syscall.Chroot("/mp/ubuntu-fs")
syscall.Chdir("/")
syscall.Mount("proc", "proc", "proc", 0, "")
cmd := exec.Command(os.Args[2], os.Args[3:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Run()
syscall.Unmount("/proc", 0)
}
// docker run image <cmd> <params>
// go run main.go run <cmd> <params>
// limit what a process can use
func cgroup() {
cgroups := "/sys/fs/cgroup"
cgroupPath := filepath.Join(cgroups, "chris")
err := os.Mkdir(cgroupPath, 0755)
if err != nil && !os.IsExist(err) {
panic(err)
}
must(os.WriteFile(filepath.Join(cgroups, "cgroup.subtree_control"), []byte("+pids"), 0700))
must(os.WriteFile(filepath.Join(cgroupPath, "pids.max"), []byte("20"), 0700))
must(os.WriteFile(filepath.Join(cgroupPath, "cgroup.procs"), []byte(strconv.Itoa(os.Getpid())), 0700))
}
func must(err error) {
if err != nil {
panic(err)
}
}