-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcontainer.go
203 lines (187 loc) · 4.63 KB
/
container.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"syscall"
"github.com/sirupsen/logrus"
)
const ipTmpl = "10.100.42.%d/24"
type Container struct {
Args []string
Uid int
Gid int
}
var defaultEnvironment = []string{
"HOME=/root",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"TERM=xterm",
}
func (c *Container) Start() error {
cmd := &exec.Cmd{
Path: os.Args[0],
Args: append([]string{"unc-fork"}, c.Args...),
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = defaultEnvironment
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUSER |
syscall.CLONE_NEWPID |
syscall.CLONE_NEWUTS |
syscall.CLONE_NEWNS |
syscall.CLONE_NEWNET,
UidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: c.Uid,
Size: 1,
},
},
GidMappings: []syscall.SysProcIDMap{
{
ContainerID: 0,
HostID: c.Gid,
Size: 1,
},
},
}
if err := cmd.Start(); err != nil {
return err
}
logrus.Debugf("container PID: %d", cmd.Process.Pid)
if err := putIface(cmd.Process.Pid); err != nil {
return err
}
return cmd.Wait()
}
type Mount struct {
Source string
Target string
Fs string
Flags int
Data string
}
type Cfg struct {
Path string
Args []string
Hostname string
Mounts []Mount
Rootfs string
IP string
}
var defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
var defaultCfg = Cfg{
Hostname: "unc",
Mounts: []Mount{
{
Source: "proc",
Target: "/proc",
Fs: "proc",
Flags: defaultMountFlags,
},
{
Source: "tmpfs",
Target: "/dev",
Fs: "tmpfs",
Flags: syscall.MS_NOSUID | syscall.MS_STRICTATIME,
Data: "mode=755",
},
},
Rootfs: "/home/moroz/project/busybox",
}
func pivotRoot(root string) error {
// we need this to satisfy restriction:
// "new_root and put_old must not be on the same filesystem as the current root"
if err := syscall.Mount(root, root, "bind", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
return fmt.Errorf("Mount rootfs to itself error: %v", err)
}
// create rootfs/.pivot_root as path for old_root
pivotDir := filepath.Join(root, ".pivot_root")
if err := os.Mkdir(pivotDir, 0777); err != nil {
return err
}
logrus.Debugf("Pivot root dir: %s", pivotDir)
logrus.Debugf("Pivot root to %s", root)
// pivot_root to rootfs, now old_root is mounted in rootfs/.pivot_root
// mounts from it still can be seen in `mount`
if err := syscall.PivotRoot(root, pivotDir); err != nil {
return fmt.Errorf("pivot_root %v", err)
}
// change working directory to /
// it is recommendation from man-page
if err := syscall.Chdir("/"); err != nil {
return fmt.Errorf("chdir / %v", err)
}
// path to pivot root now changed, update
pivotDir = filepath.Join("/", ".pivot_root")
// umount rootfs/.pivot_root(which is now /.pivot_root) with all submounts
// now we have only mounts that we mounted ourself in `mount`
if err := syscall.Unmount(pivotDir, syscall.MNT_DETACH); err != nil {
return fmt.Errorf("unmount pivot_root dir %v", err)
}
// remove temporary directory
return os.Remove(pivotDir)
}
func mount(cfg Cfg) error {
for _, m := range cfg.Mounts {
target := filepath.Join(cfg.Rootfs, m.Target)
logrus.Debugf("Mount %s to %s", m.Source, target)
if err := syscall.Mount(m.Source, target, m.Fs, uintptr(m.Flags), m.Data); err != nil {
return fmt.Errorf("failed to mount %s to %s: %v", m.Source, target, err)
}
}
return nil
}
func setup(cfg Cfg) error {
if err := mount(cfg); err != nil {
return err
}
if err := pivotRoot(cfg.Rootfs); err != nil {
return fmt.Errorf("Pivot root error: %v", err)
}
if err := syscall.Sethostname([]byte(cfg.Hostname)); err != nil {
return fmt.Errorf("Sethostname: %v", err)
}
return nil
}
func execProc(cfg Cfg) error {
logrus.Debugf("Execute %s", append([]string{cfg.Path}, cfg.Args[1:]...))
return syscall.Exec(cfg.Path, cfg.Args, os.Environ())
}
func fillCfg() error {
name, err := exec.LookPath(os.Args[1])
if err != nil {
return fmt.Errorf("LookPath: %v", err)
}
defaultCfg.Path = name
defaultCfg.Args = os.Args[1:]
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("Error get working dir: %v", err)
}
defaultCfg.Rootfs = wd
// choose ip
defaultCfg.IP = fmt.Sprintf(ipTmpl, rand.Intn(253)+2)
return nil
}
func fork() error {
logrus.Debug("Start fork")
if err := fillCfg(); err != nil {
return err
}
if err := setup(defaultCfg); err != nil {
return err
}
lnk, err := waitForIface()
if err != nil {
return err
}
if err := setupIface(lnk, defaultCfg); err != nil {
return err
}
return execProc(defaultCfg)
}