forked from povik/envdeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontain.go
274 lines (229 loc) · 5.3 KB
/
contain.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"fmt"
"net"
"os"
"os/exec"
"runtime"
// "syscall"
"bytes"
"errors"
"strings"
// "net/http"
"github.com/vishvananda/netns"
)
var (
errConnClosed = errors.New("the persistent connection closed")
)
func mustOpen(name string) *os.File {
f, err := os.Open(name)
if err != nil {
panic(err)
}
return f
}
var (
devNull = mustOpen(os.DevNull)
)
type dialResp struct {
net.Conn
error
}
type dialReq struct {
network string
address string
resch chan<- dialResp
}
type pinnedDialer struct {
/* read in the loop, written in Dial */
reqch chan dialReq
/* waited on anywhere, closed in loop/Quit */
readych chan interface{}
quitch chan interface{}
}
func CreatePinnedDialer() *pinnedDialer {
return &pinnedDialer{
make(chan dialReq),
make(chan interface{}),
make(chan interface{}),
}
}
func (d *pinnedDialer) loop() {
var req dialReq
var resp dialResp
close(d.readych)
for {
select {
case <-d.quitch:
return
case req = <-d.reqch:
}
resp.Conn, resp.error = net.Dial(req.network, req.address)
req.resch <- resp
}
}
func (d *pinnedDialer) WaitReady() <-chan interface{} {
ret := make(chan interface{})
go func() {
select {
case <-d.quitch:
case <-d.readych:
}
close(ret)
}()
return ret
}
func (d *pinnedDialer) Quit() {
close(d.quitch)
}
func (d *pinnedDialer) Dial(network, address string) (net.Conn, error) {
resch := make(chan dialResp)
req := dialReq{
network: network,
address: address,
resch: resch,
}
select {
case <-d.quitch:
return nil, errConnClosed
case d.reqch <- req:
}
res, ok := <-resch
if !ok {
return nil, errConnClosed
}
return res.Conn, res.error
}
var hostIfCounter int = 0
func allocHostIfName() (name string) {
name = fmt.Sprintf("ve-envdeploy%d", hostIfCounter)
hostIfCounter += 1
return
}
func runContainedWithDialerThread(id string, cmd string, env []string, dir string, cgroupPath string, stderr *os.File, pd *pinnedDialer, donech chan<- interface{}) {
var proc *os.Process
var state *os.ProcessState
var err error
var path string
var argv []string
bgNetns, _ := netns.Get()
donechClosed := false
done := func() {
if donechClosed {
return
}
donechClosed = true
close(donech)
}
defer done()
argv = strings.Split(cmd, " ")
if len(argv) == 0 {
fmt.Fprintf(stderr, "envdeploy: no command to run\n")
return
}
path, err = exec.LookPath(argv[0])
if err != nil {
fmt.Fprintf(stderr, "envdeploy: %s\n", err)
return
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
newns, err := netns.New()
if err != nil {
fmt.Fprintf(stderr, "envdeploy: network namespace creation failed: %s\n", err)
return
}
defer func() {
newns.Close()
netns.Set(bgNetns)
}()
hostIf := allocHostIfName()
runInBackgroundNetns(func() error {
cmd := exec.Command("ip", "link", "delete", hostIf)
cmd.Stdout = devNull
cmd.Stderr = devNull
cmd.Run()
return nil
})
err = createRouteFromCurrentNetns(hostIf)
if err != nil {
fmt.Fprintf(stderr, "envdeploy: failed to create veth interface pair: %s\n", err)
}
defer func() {
runInBackgroundNetns(func() error {
cmd := exec.Command("ip", "link", "delete", hostIf)
cmd.Stdout = stderr
cmd.Stderr = stderr
cmd.Run()
return nil
})
}()
hostIp := fmt.Sprintf("10.0.%d.%d", (hostIfCounter/127)%256, (hostIfCounter%127)*2+0)
guestIp := fmt.Sprintf("10.0.%d.%d", (hostIfCounter/127)%256, (hostIfCounter%127)*2+1)
err = runInBackgroundNetns(func() error {
var cmd *exec.Cmd
var err error
cmd = exec.Command("ip", "link", "set", "dev", hostIf, "up")
cmd.Stdout = stderr
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
return err
}
cmd = exec.Command("ip", "addr", "add", hostIp+"/31", "dev", hostIf)
cmd.Stdout = stderr
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
return err
}
return nil
})
if err != nil {
fmt.Fprintf(stderr, "envdeploy: could not set up host veth interface: %s\n", err)
return
}
guestCmds := [][]string{
{"ip", "link", "set", "dev", "lo", "up"},
{"ip", "link", "set", "dev", "eth1", "up"},
{"ip", "addr", "add", guestIp + "/31", "dev", "eth1"},
{"ip", "route", "add", hostIp, "dev", "eth1"},
{"ip", "route", "add", "default", "via", hostIp},
}
for _, cmds := range guestCmds {
err = exec.Command(cmds[0], cmds[1:]...).Run()
if err != nil {
fmt.Fprintf(stderr, "envdeploy: error executing '%s': %s\n", strings.Join(cmds, " "), err)
return
}
}
proc, err = startProcessInCgroup(path, argv, env, dir, cgroupPath, stderr)
if err != nil {
fmt.Fprintf(stderr, "envdeploy: starting process failed: %s\n", err)
return
}
state, err = proc.Wait()
if err != nil {
fmt.Fprintf(stderr, "envdeploy: wait on entry process: %s\n", err)
return
}
fmt.Fprintf(stderr, "envdeploy: entry process exited: %s\n", state)
done()
pd.loop()
}
func RunContainedWithDialer(id string, cmd string, env []string, dir string, cgroupDir string, stderr *os.File, pd *pinnedDialer) {
donech := make(chan interface{})
go runContainedWithDialerThread(id, cmd, env, dir, cgroupDir, stderr, pd, donech)
<-donech
}
func Sh(shCmd string) string {
cmd := exec.Command("/bin/sh", "-c", shCmd)
cmd.Stdin = devNull
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return fmt.Sprintf("running '%s' failed: %v", shCmd, err.Error())
}
return out.String()
}