-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
104 lines (87 loc) · 2.46 KB
/
container.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
package main
import (
"io/ioutil"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"time"
)
// go run main.go run <cmd> <args>
func main() {
switch os.Args[1] {
case "run":
run()
case "child":
child()
default:
panic("usage: go run container.go run <hostname> <memory>")
}
}
func run() {
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 | syscall.CLONE_NEWNET,
}
cmd.Run()
}
func create_separate_filesystem(container_name string) {
if err := os.Mkdir(container_name, os.ModePerm); err != nil {
log.Fatal(err)
} else {
cmd := exec.Command("/bin/sh", "-c", "sudo tar -xvf ubuntu.tar -C "+container_name)
cmd.Run()
if err != nil {
log.Fatal(err)
}
}
}
func child() {
source := rand.NewSource(time.Now().UnixNano())
new_random := rand.New(source)
uid := strconv.Itoa(new_random.Intn(100000))
container_name := "/container_" + uid
cg(uid)
if len(os.Args) == 4 {
limit_memory_usage(uid)
}
create_separate_filesystem(container_name)
cmd := exec.Command("bash")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
syscall.Sethostname([]byte(os.Args[2]))
syscall.Chroot(container_name)
os.Chdir("/")
syscall.Mount("proc", "proc", "proc", 0, "")
cmd.Run()
syscall.Unmount("proc", 0)
}
func cg(uid string) {
cgroups := "/sys/fs/cgroup/"
pids := filepath.Join(cgroups, "pids")
os.Mkdir(filepath.Join(pids, uid), 0755)
must(ioutil.WriteFile(filepath.Join(pids, uid+"/pids.max"), []byte("20"), 0700))
must(ioutil.WriteFile(filepath.Join(pids, uid+"/notify_on_release"), []byte("1"), 0700))
must(ioutil.WriteFile(filepath.Join(pids, uid+"/cgroup.procs"), []byte(strconv.Itoa(os.Getpid())), 0700))
}
func limit_memory_usage(uid string) {
cgroups := "/sys/fs/cgroup/"
memory := filepath.Join(cgroups, "memory")
os.Mkdir(filepath.Join(memory, uid), 0755)
memory_size, _ := strconv.Atoi(os.Args[3])
must(ioutil.WriteFile(filepath.Join(memory, uid+"/memory.limit_in_bytes"), []byte(strconv.Itoa(memory_size*1000)), 0700))
must(ioutil.WriteFile(filepath.Join(memory, uid+"/tasks"), []byte(strconv.Itoa(os.Getpid())), 0700))
must(ioutil.WriteFile(filepath.Join(memory, uid+"/cgroup.procs"), []byte(strconv.Itoa(os.Getpid())), 0700))
}
func must(err error) {
if err != nil {
panic(err)
}
}