-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
501 lines (433 loc) · 12.5 KB
/
utils.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
goruntime "runtime"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
exactArgs = iota
minArgs
maxArgs
)
var (
fdInfoConfigJson = "__RUMP_FDINFO_CONFIGJSON"
fdInfoEnvPrefixNet = "__RUMP_FDINFO_NET_"
fdInfoEnvPrefixDisk = "__RUMP_FDINFO_DISK_"
fdInfoEnvPrefixRoot = "__RUMP_FDINFO_ROOT"
)
func checkArgs(context *cli.Context, expected, checkType int) error {
var err error
cmdName := context.Command.Name
switch checkType {
case exactArgs:
if context.NArg() != expected {
err = fmt.Errorf(
"%s: %q requires exactly %d argument(s)",
os.Args[0], cmdName, expected)
}
case minArgs:
if context.NArg() < expected {
err = fmt.Errorf(
"%s: %q requires a minimum of %d argument(s)",
os.Args[0], cmdName, expected)
}
case maxArgs:
if context.NArg() > expected {
err = fmt.Errorf(
"%s: %q requires a maximum of %d argument(s)",
os.Args[0], cmdName, expected)
}
}
if err != nil {
fmt.Printf("Incorrect Usage.\n\n")
cli.ShowCommandHelp(context, cmdName)
return err
}
return nil
}
func readPidFile(context *cli.Context, pidFile string) (int, error) {
root := context.GlobalString("root")
container := context.Args().Get(0)
file := filepath.Join(root, container, pidFile)
pid, err := ioutil.ReadFile(file)
if err != nil {
return 0, err
}
pidI, err := strconv.Atoi(string(pid))
if err != nil {
return 0, err
}
return pidI, nil
}
func copyFile(src, dst string, mode os.FileMode) error {
b, err := ioutil.ReadFile(src)
if err != nil {
return err
}
err = ioutil.WriteFile(dst, b, mode)
if err != nil {
return err
}
return nil
}
func isAlpineImage(rootfs string) bool {
osRelease := rootfs + "/etc/os-release"
f, err := os.Open(osRelease)
if err != nil {
return false
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
matched, _ := regexp.MatchString("Alpine Linux",
scanner.Text())
if matched {
return true
}
}
return false
}
func changeLdso(spec *specs.Spec, rootfs string) error {
for _, env := range spec.Process.Env {
if strings.HasPrefix(env, "RUNU_AUX_DIR=") {
runuAuxFileDir = strings.TrimLeft(env, "RUNU_AUX_DIR=")
}
}
// XXX: only for alpine
// install frankenlibc-ed libc.so to the system one
if goruntime.GOARCH == "amd64" {
if err := copyFile(runuAuxFileDir+"/libc.so",
rootfs+"/lib/ld-musl-x86_64.so.1", 0755); err != nil {
return err
}
} else if goruntime.GOARCH == "arm" {
if err := copyFile(runuAuxFileDir+"/libc.so",
rootfs+"/lib/ld-musl-armhf.so.1", 0755); err != nil {
return err
}
} else if goruntime.GOARCH == "arm64" {
if err := copyFile(runuAuxFileDir+"/libc.so",
rootfs+"/lib/ld-musl-aarch64.so.1", 0755); err != nil {
return err
}
}
// install frankenlibc-ed libc.so to the system one
if err := copyFile(runuAuxFileDir+"/lkick",
rootfs+"/bin/lkick", 0755); err != nil {
return err
}
return nil
}
type lklInterface struct {
MacAddr string `json:"mac,omitempty"`
V4Addr string `json:"ip"`
V4MaskLen string `json:"masklen"`
V6Addr string `json:"ipv6,omitempty"`
V6MaskLen string `json:"masklen6,omitempty"`
Name string `json:"name"`
Iftype string `json:"type"`
Offload string `json:"offload,omitempty"`
}
type lklRoute struct {
Destination string `json:"destination"`
Nexthop string `json:"nexthop"`
}
type lklConfig struct {
V4Gateway string `json:"gateway,omitempty"`
Interfaces []lklInterface `json:"interfaces,omitempty"`
Debug string `json:"debug,omitempty"`
DelayMain string `json:"delay_main,omitempty"`
SingleCpu string `json:"singlecpu,omitempty"`
Sysctl string `json:"sysctl,omitempty"`
Routes string `json:"route,omitempty"`
}
type lklIfInfo struct {
ifAddrs []net.IPNet
ifName string
v4Gw net.IP
}
func generateLklJsonFile(lklJson string, lklJsonOut *string, spec *specs.Spec) (*lklConfig, error) {
config := new(lklConfig)
// IPv4 address
ifInfo, routeInfo, err := setupNetwork(spec)
if err != nil {
return nil, fmt.Errorf("failed to parse ipv4/ipv6 address: (%s)", err)
}
if ifInfo == nil {
logrus.Warnf("no interface detected")
*lklJsonOut = lklJson
return config, nil
}
logrus.Debugf(ifInfo.ifAddrs[0].String())
logrus.Debugf("interface=>%+v", ifInfo)
logrus.Debugf("route=>%+v", routeInfo)
v4masklen, _ := ifInfo.ifAddrs[0].Mask.Size()
v4addr := ifInfo.ifAddrs[0].IP
v4addr = v4addr.To4()
v4gw := ifInfo.v4Gw.To4()
// read user-specified file
if lklJson != "" {
bytes, err := ioutil.ReadFile(lklJson)
if err != nil {
logrus.Errorf("failed to read JSON file: %s (%s)",
lklJson, err)
panic(err)
}
// decode json
if err := json.Unmarshal(bytes, &config); err != nil {
logrus.Errorf("failed to decode JSON file: %s (%s)",
lklJson, err)
panic(err)
}
// only replace IPv4 address when the address is written
// with "AUTO": otherwise use user-specified address
if len(config.Interfaces) > 0 {
if config.Interfaces[0].V4Addr == "AUTO" {
config.Interfaces[0].V4Addr = v4addr.String()
config.Interfaces[0].V4MaskLen = strconv.Itoa(v4masklen)
config.V4Gateway = v4gw.String()
}
}
} else {
config.Debug = "1"
config.Interfaces = append(config.Interfaces,
lklInterface{
V4Addr: v4addr.String(),
V4MaskLen: strconv.Itoa(v4masklen),
Name: ifInfo.ifName,
Iftype: "rumpfd",
})
config.V4Gateway = v4gw.String()
for _, route := range routeInfo {
config.Routes += route.Destination
config.Routes += "="
config.Routes += route.Nexthop
config.Routes += ";"
}
}
outJson, err := json.MarshalIndent(config, "", " ")
if err != nil {
logrus.Errorf("failed to encode JSON file: %s (%s)",
lklJson, err)
panic(err)
}
ioutil.WriteFile(*lklJsonOut, outJson, os.ModePerm)
return config, nil
}
func parseEnvs(spec *specs.Spec, context *cli.Context, rootfs string) ([]string, map[*os.File]bool) {
specEnv := []string{}
fds := map[*os.File]bool{}
fdNum := 3
hasRootFs := false
lklJson := ""
// check if -v is specified
_, use9pFs := os.LookupEnv("LKL_USE_9PFS")
for _, env := range spec.Process.Env {
// look for LKL_ROOTFS env for .img/.iso files
if strings.HasPrefix(env, "LKL_ROOTFS=") {
lklRootfs := strings.TrimLeft(env, "LKL_ROOTFS=")
// if a file exists in local/host, copy and use it
if _, err := os.Stat(lklRootfs); err == nil {
copyFile(lklRootfs,
rootfs+"/"+filepath.Base(lklRootfs),
0644)
lklRootfs = "/" + filepath.Base(lklRootfs)
}
fd, nonblock := openRootfsFd(rootfs + "/" + lklRootfs)
fds[fd] = nonblock
specEnv = append(specEnv, fdInfoEnvPrefixRoot+"="+strconv.Itoa(fdNum))
fdNum++
hasRootFs = true
continue
}
// look for LKL_NET env for tap/macvtap devices
if strings.HasPrefix(env, "LKL_NET=") {
lklNet := strings.TrimLeft(env, "LKL_NET=")
fd, nonblock := openNetFd(lklNet, spec.Process.Env)
fds[fd] = nonblock
specEnv = append(specEnv, fdInfoEnvPrefixNet+lklNet+"="+strconv.Itoa(fdNum))
fdNum++
continue
}
// look for LKL_CONFIG env for json file
if strings.HasPrefix(env, "LKL_CONFIG=") {
lklJson = strings.TrimLeft(env, "LKL_CONFIG=")
copyFile(lklJson, rootfs+"/"+filepath.Base(lklJson), 0644)
lklJson = rootfs + "/" + filepath.Base(lklJson)
continue
}
// lookf for LKL_USE_9PFS
if strings.HasPrefix(env, "LKL_USE_9PFS=") {
use9pFs = true
}
// XXX: should exclude duplicated PATH variable in spec.Env since
// it eliminates following values
if !strings.HasPrefix(env, "PATH=") {
specEnv = append(specEnv, env)
}
}
// Set IPv4 addr/route from CNI info
// and configure as lkl-$(container-id)-out.json file
container := context.Args().First()
clen := 10
if len(container) < 10 {
clen = len(container)
}
lklJsonOut := rootfs + "/" + "lkl-" + container[:clen] + "-out.json"
jsonObj, err := generateLklJsonFile(lklJson, &lklJsonOut, spec)
if err != nil {
panic(err)
}
// XXX: eth0 should be somewhere else
if len(jsonObj.Interfaces) > 0 {
lklNet := "eth0"
fd, nonblock := openNetFd(lklNet, spec.Process.Env)
fds[fd] = nonblock
specEnv = append(specEnv, fdInfoEnvPrefixNet+lklNet+"="+strconv.Itoa(fdNum))
fdNum++
}
if lklJsonOut != "" {
fd, nonblock := openJsonFd(lklJsonOut)
fds[fd] = nonblock
specEnv = append(specEnv, fdInfoConfigJson+"="+strconv.Itoa(fdNum))
fdNum++
}
// start 9pfs server as a child process
// if there is no rootfs disk image
if !hasRootFs && use9pFs {
childArgs := []string{"--9ps=" + rootfs + "/"}
cmd := exec.Command(os.Args[0], childArgs[0:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Start(); err != nil {
panic(err)
}
// pid file for 9pfs server
root := context.GlobalString("root")
name := context.Args().Get(0)
pidf := filepath.Join(root, name, pidFile9p)
f, _ := os.OpenFile(pidf,
os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
_, _ = fmt.Fprintf(f, "%d", cmd.Process.Pid)
f.Close()
logrus.Debugf("Starting command %s, Env=%s, rootfs=%s\n",
cmd.Args, cmd.Env, rootfs)
time.Sleep(100 * time.Millisecond)
fd, nonblock := connect9pfs()
fds[fd] = nonblock
specEnv = append(specEnv, "9PFS_FD="+strconv.Itoa(fdNum))
specEnv = append(specEnv, "9PFS_MNT=/")
}
return specEnv, fds
}
func prepareUkontainer(context *cli.Context) (*exec.Cmd, error) {
container := context.Args().Get(0)
spec, err := setupSpec(context)
if err != nil {
logrus.Warnf("setupSepc err %s\n", err)
return nil, err
}
rootfs, _ := filepath.Abs(spec.Root.Path)
// open fds to pass to main programs later
specEnv, fds := parseEnvs(spec, context, rootfs)
// fixup ldso to a pulled image
err = changeLdso(spec, rootfs)
if err != nil {
logrus.Warnf("ldso fixup error. skipping (%s)", err)
}
for _, node := range DefaultDevices {
createDeviceNode(rootfs, node)
}
// call rexec
os.Setenv("PATH", rootfs+":"+rootfs+
"/sbin:"+rootfs+"/bin:/bin:/sbin:")
cmd := exec.Command(spec.Process.Args[0], spec.Process.Args[1:]...)
// XXX: need a better way to detect
if isAlpineImage(rootfs) && goruntime.GOOS == "darwin" {
logrus.Debugf("This is alpine linux image")
cmd = exec.Command("lkick", spec.Process.Args[0:]...)
}
// do chroot(2) in rexec-ed processes
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: false,
}
cmd.Dir = "/"
// on Linux, libc.so is replaced in a chrooted directory
if goruntime.GOOS == "linux" {
cmd.SysProcAttr.Chroot = rootfs
binpath, err := exec.LookPath(spec.Process.Args[0])
if err != nil {
logrus.Errorf("cmd %s not found %s",
spec.Process.Args[0], err)
os.Setenv("PATH", "/bin:/sbin:/usr/bin:"+rootfs+":"+rootfs+
"/sbin:"+rootfs+"/bin")
}
if strings.HasPrefix(binpath, rootfs) {
cmd.Path = strings.Split(binpath, rootfs)[1]
logrus.Debugf("cmd %s found at %s=>%s",
spec.Process.Args[0], binpath, cmd.Path)
}
}
cmd.Env = append(specEnv, "PATH=/bin:/sbin:"+os.Getenv("PATH"))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// XXX: need to sort cmd.Extrafiles to sync with frankenlibc
fdkeys := make([]*os.File, 0)
for k := range fds {
fdkeys = append(fdkeys, k)
}
sort.SliceStable(fdkeys, func(i, j int) bool {
return fdkeys[i].Fd() < fdkeys[j].Fd()
})
for k := range fdkeys {
cmd.ExtraFiles = append(cmd.ExtraFiles, fdkeys[k])
}
cwd, _ := os.Getwd()
logrus.Debugf("Starting command %s, Env=%s, cwd=%s, chroot=%s",
cmd.Args, cmd.Env, cwd, rootfs)
if err := cmd.Start(); err != nil {
logrus.Errorf("cmd error %s (cmd=%s)", err, cmd.Args)
panic(err)
}
// pid file for forked process from `runu create`
// it'll be used later by `runu start`
root := context.GlobalString("root")
pidf := filepath.Join(root, container, pidFilePriv)
f, _ := os.OpenFile(pidf,
os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
_, _ = fmt.Fprintf(f, "%d", cmd.Process.Pid)
f.Close()
logrus.Debugf("PID=%d to pid file %s",
cmd.Process.Pid, pidf)
proc, err := os.FindProcess(cmd.Process.Pid)
if err != nil {
return nil, fmt.Errorf("couldn't find pid %d", cmd.Process.Pid)
}
proc.Signal(syscall.Signal(syscall.SIGSTOP))
// XXX:
// os/exec.Start() close and open extrafiles thus strip O_NONBLOCK flag
// thus re-enable it here
for fd, nbFlag := range fds {
if err := syscall.SetNonblock(int(fd.Fd()), nbFlag); err != nil {
logrus.Errorf("setNonBlock %d error: %s\n", int(fd.Fd()), err)
panic(err)
}
}
return cmd, nil
}