-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
93 lines (82 loc) · 2.67 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"syscall"
)
// docker run image <cmd> <params>
// go run main.go run {some command} <cmd> <params>
func main() {
switch os.Args[1] {
case "run":
run()
case "child":
child()
case "command":
command()
default:
panic("invalid command")
}
}
func run() {
fmt.Printf("Running %v as %d\n", os.Args[2:], os.Getpid())
// proc dir is a directory where all processes metadata is there
// our temporary binary will also be present here
// below line executes child function inside the newly created container
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...)
// attatching os-std process to our cmd-std process
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// setting some system process attributes
// below line of code is responsible for creating a new isolated process
cmd.SysProcAttr = &syscall.SysProcAttr{
// Cloning is what creates the process(container) in which we would be running our command.
// CLONE_NEWUTS will allow to have our own hostname inside our container by creating a new unix timesharing system.
// CLONE_NEWPID assigns pids to only process inside the new namspace.
// CLONE_NEWNS new namespace for mount.
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS,
// Mounts in systemd gets recursively shared property.
// Unshare the recursively shared property for new mount namespace.
// It prevents sharing of new namespace with the host.
Unshareflags: syscall.CLONE_NEWNS,
}
// running the command and catching error
if err := cmd.Run(); err != nil {
log.Fatal("Error: ", err)
}
}
func child() {
fmt.Printf("Running %v as %d\n", os.Args[2:], os.Getpid())
// below are some system calls that set some container properties
// sets hostname for newly created namespace
must(syscall.Sethostname([]byte("maverick")))
// setting root director for the container
must(syscall.Chroot("/"))
// making "/" as default dir
must(syscall.Chdir("/"))
// mounting proc dir to see the process running inside container
must(syscall.Mount("proc", "proc", "proc", 0, ""))
// below line finally executes the user-command inside our own created container!
cmd := exec.Command(os.Args[2], os.Args[3:]...)
// attatching os-std process to our cmd-std process
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// running the command and catching error
if err := cmd.Run(); err != nil {
log.Fatal("Error: ", err)
}
// unmount the proc after command is finished
syscall.Unmount("/proc", 0)
}
func command() {
os.Create("my.txt")
}
func must(err error) {
if err != nil {
panic(err)
}
}