-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (73 loc) · 2.08 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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
)
func must(err error) {
if err != nil {
panic(err)
}
}
func main() {
switch os.Args[1] {
case "run":
run()
case "child":
child()
default:
panic("help")
}
}
func run() {
fmt.Printf("Running %v as %d\n", os.Args[2:], os.Getegid())
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_NEWUSER,
Credential: &syscall.Credential{Uid: 0, Gid: 0},
UidMappings: []syscall.SysProcIDMap{
{ContainerID: 0, HostID: os.Getuid(), Size: 1},
},
GidMappings: []syscall.SysProcIDMap{
{ContainerID: 0, HostID: os.Getuid(), Size: 1},
},
}
must(cmd.Run())
}
func child() {
fmt.Printf("Running %v as %d\n", os.Args[2:], os.Getegid())
cmd := exec.Command(os.Args[2], os.Args[3:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
must(syscall.Sethostname([]byte("container")))
must(syscall.Chroot("./ubuntufs"))
must(os.Chdir("/"))
must(syscall.Mount("proc", "proc", "proc", 0, ""))
if _, err := os.Stat("mytemp"); os.IsNotExist(err) {
must(os.Mkdir("mytemp", 0700))
}
// fmt.Println(os.Getwd())
must(syscall.Mount("something", "mytemp", "tmpfs", 0, ""))
must(cmd.Run())
must(syscall.Unmount("proc", 0))
must(syscall.Unmount("mytemp", 0))
}
func cg() {
cgroups := "/sys/fs/cgroup"
mem := filepath.Join(cgroups, "memory")
fmt.Println(mem)
os.Mkdir(filepath.Join(mem, "alexandre"), 0755)
must(ioutil.WriteFile(filepath.Join(mem, "alexandre/memory.limit_in_bytes"), []byte("999424"), 0700))
must(ioutil.WriteFile(filepath.Join(mem, "alexandre/memory.memsw.limit_in_bytes"), []byte("999424"), 0700))
must(ioutil.WriteFile(filepath.Join(mem, "alexandre/memory.on_release"), []byte("1"), 0700))
pid := strconv.Itoa(os.Getpid())
must(ioutil.WriteFile(filepath.Join(mem, "alexandre/cgroup.procs"), []byte(pid), 0700))
}