-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
144 lines (122 loc) · 3.39 KB
/
run.go
File metadata and controls
144 lines (122 loc) · 3.39 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"flag"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/creack/pty"
"golang.org/x/term"
)
func exitCode(err error) int {
if err == nil {
return 0
}
if exitErr, ok := err.(*exec.ExitError); ok {
return exitErr.ExitCode()
}
return 1
}
func run(args []string) {
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
rootfsFlag := runCmd.String("rootfs", "", "path to the container root filesystem")
hostnameFlag := runCmd.String("hostname", "", "container hostname")
memoryFlag := runCmd.String("memory", "", "memory limit in bytes for cgroup v2 (e.g. 134217728 for 128MiB)")
if err := runCmd.Parse(args); err != nil {
panic(err)
}
remaining := runCmd.Args()
if len(remaining) < 1 {
fmt.Println("you must provide a command to run inside the container")
fmt.Println()
fmt.Println("Example:")
fmt.Println(" mini-runc run --rootfs=/path/to/rootfs --hostname=demo /bin/sh")
return
}
rootfs := *rootfsFlag
if rootfs == "" {
rootfs = os.Getenv("ROOTFS_PATH")
}
if rootfs == "" {
fmt.Println("no rootfs specified. Use --rootfs flag or set ROOTFS_PATH env")
return
}
hostname := *hostnameFlag
if hostname == "" {
hostname = os.Getenv("CONTAINER_HOSTNAME")
}
if hostname == "" {
hostname = "container"
}
env := os.Environ()
env = append(env, "ROOTFS_PATH="+rootfs)
env = append(env, "CONTAINER_HOSTNAME="+hostname)
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, remaining...)...)
cmd.Env = env
cloneFlags := uintptr(syscall.CLONE_NEWPID |
syscall.CLONE_NEWNS |
syscall.CLONE_NEWUTS |
syscall.CLONE_NEWIPC |
syscall.CLONE_NEWNET)
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: cloneFlags,
}
if os.Getuid() != 0 {
cmd.SysProcAttr.Cloneflags |= syscall.CLONE_NEWUSER
cmd.SysProcAttr.UidMappings = []syscall.SysProcIDMap{
{ContainerID: 0, HostID: os.Getuid(), Size: 1},
}
cmd.SysProcAttr.GidMappings = []syscall.SysProcIDMap{
{ContainerID: 0, HostID: os.Getgid(), Size: 1},
}
}
if term.IsTerminal(int(os.Stdin.Fd())) {
f, err := pty.Start(cmd)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to start with PTY: %v\n", err)
os.Exit(1)
}
defer f.Close()
if *memoryFlag != "" {
if err := setupMemoryCgroup(cmd.Process.Pid, *memoryFlag); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to configure memory cgroup: %v\n", err)
}
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if err := pty.InheritSize(os.Stdin, f); err != nil {
fmt.Fprintf(os.Stderr, "error resizing pty: %s\n", err)
}
}
}()
ch <- syscall.SIGWINCH
defer func() { signal.Stop(ch); close(ch) }()
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
fmt.Fprintf(os.Stderr, "failed to set raw mode: %v\n", err)
os.Exit(1)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
go func() { _, _ = io.Copy(f, os.Stdin) }()
_, _ = io.Copy(os.Stdout, f)
os.Exit(exitCode(cmd.Wait()))
} else {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "failed to start container: %v\n", err)
os.Exit(1)
}
if *memoryFlag != "" {
if err := setupMemoryCgroup(cmd.Process.Pid, *memoryFlag); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to configure memory cgroup: %v\n", err)
}
}
os.Exit(exitCode(cmd.Wait()))
}
}