-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_from_scratch.go
61 lines (56 loc) · 1.74 KB
/
container_from_scratch.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
package main
import (
"os"
"os/exec"
"fmt"
"syscall"
"path/filepath"
"strconv"
"io/ioutil"
)
func main() {
switch os.Args[1] {
case "run":
run()
case "ns":
ns()
default:
panic("pass me an argument please")
}
}
func run() {
fmt.Printf("Running %v as %d\n" ,os.Args[2:], os.Getpid())
cmd := exec.Command("/proc/self/exe" , append([]string{"ns"}, 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 ns() {
fmt.Printf("Running in new UTS namespace %v as %d\n" ,os.Args[2:], os.Getpid())
cg()
syscall.Sethostname([]byte("inside-container"))
syscall.Chroot("/root/containerFS")
syscall.Chdir("/") // set the working directory inside container
syscall.Mount("proc", "proc", "proc", 0, "")
cmd := exec.Command(os.Args[2], os.Args[3:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
syscall.Unmount("/proc", 0)
}
func cg() {
cgroups := "/sys/fs/cgroup/"
pids := filepath.Join(cgroups, "pids")
os.Mkdir(filepath.Join(pids, "ourContainer"), 0755)
ioutil.WriteFile(filepath.Join(pids, "ourContainer/pids.max"), []byte("10"), 0700)
//up here we limit the number of child processes to 10
ioutil.WriteFile(filepath.Join(pids, "ourContainer/notify_on_release"), []byte("1"), 0700)
ioutil.WriteFile(filepath.Join(pids, "ourContainer/cgroup.procs"), []byte(strconv.Itoa(os.Getpid())), 0700)
// up here we write container PIDs to cgroup.procs
}