-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (81 loc) · 2.28 KB
/
Copy pathmain.go
File metadata and controls
109 lines (81 loc) · 2.28 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"time"
"docker/utils"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: sudo ./mycontainer run <command>")
os.Exit(1)
}
switch os.Args[1] {
case "run":
run()
case "child":
child()
default:
fmt.Println("Unknown command")
}
}
func run() {
fmt.Printf("Running [Parent] - PID: %d\n", os.Getpid())
basePath, _ := os.Getwd()
lowerDir := filepath.Join(basePath, "alpine-rootfs")
utils.CheckAndSetupRootFS(lowerDir)
containerID := fmt.Sprintf("container-%d", time.Now().UnixNano())
defer utils.CleanupContainer(containerID)
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...)
cmd.Env = append(os.Environ(), fmt.Sprintf("CONTAINER_ID=%s", containerID))
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,
}
if err := cmd.Run(); err != nil {
fmt.Printf("Error running child process: %v\n", err)
os.Exit(1)
}
}
func child() {
fmt.Printf("Running [Child] - PID: %d\n", os.Getpid())
containerID := os.Getenv("CONTAINER_ID")
if containerID == "" {
fmt.Println("Container ID not found in environment")
os.Exit(1)
}
basePath, _ := os.Getwd()
lowerDir := filepath.Join(basePath, "alpine-rootfs")
upperDir := filepath.Join(basePath, "containers", containerID, "upper")
workDir := filepath.Join(basePath, "containers", containerID, "work")
mergedDir := filepath.Join(basePath, "containers", containerID, "merged")
utils.CreateContainerDirs(upperDir, workDir, mergedDir)
utils.MountOverlayFS(lowerDir, upperDir, workDir, mergedDir)
utils.IsolateAndPivotRoot(mergedDir)
command := os.Args[2]
args := os.Args[2:]
cmd := exec.Command(command, args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = []string{
"PATH=/bin:/sbin:/usr/bin:/usr/sbin",
"TERM=xterm-256color",
}
if err := cmd.Run(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
fmt.Printf("\n👋 Container shell exited with status: %d\n", exitErr.ExitCode())
} else {
fmt.Printf("Error executing command inside container: %v\n", err)
os.Exit(1)
}
}
}