forked from hyperhq/runv
/
runv.go
623 lines (538 loc) · 14.5 KB
/
runv.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package main
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"github.com/hyperhq/runv/driverloader"
"github.com/hyperhq/runv/hypervisor"
"github.com/hyperhq/runv/hypervisor/pod"
"github.com/hyperhq/runv/hypervisor/types"
"github.com/hyperhq/runv/lib/utils"
"github.com/opencontainers/specs"
)
const (
_ = iota
RUNV_INITCONTAINER
RUNV_STARTCONTAINER
RUNV_ACK
RUNV_EXECCMD
RUNV_EXITSTATUS
RUNV_WINSIZE
RUNV_KILLCONTAINER
)
const shortLen = 12
func TruncateID(id string) string {
trimTo := shortLen
if len(id) < shortLen {
trimTo = len(id)
}
return id[:trimTo]
}
// GenerateRandomID returns an unique id
func GenerateRandomID() string {
for {
id := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, id); err != nil {
panic(err) // This shouldn't happen
}
value := hex.EncodeToString(id)
// if we try to parse the truncated for as an int and we don't have
// an error then the value is all numberic and causes issues when
// used as a hostname. ref #3869
if _, err := strconv.ParseInt(TruncateID(value), 10, 64); err == nil {
continue
}
return value
}
}
func execHook(hook specs.Hook, state *specs.State) error {
b, err := json.Marshal(state)
if err != nil {
return err
}
cmd := exec.Cmd{
Path: hook.Path,
Args: hook.Args,
Env: hook.Env,
Stdin: bytes.NewReader(b),
}
return cmd.Run()
}
func execPrestartHooks(rt *specs.RuntimeSpec, state *specs.State) error {
for _, hook := range rt.Hooks.Prestart {
err := execHook(hook, state)
if err != nil {
return err
}
}
return nil
}
func execPoststartHooks(rt *specs.RuntimeSpec, state *specs.State) error {
for _, hook := range rt.Hooks.Poststart {
err := execHook(hook, state)
if err != nil {
fmt.Printf("exec Poststart hook %s failed %s", hook.Path, err.Error())
}
}
return nil
}
func execPoststopHooks(rt *specs.RuntimeSpec, state *specs.State) error {
for _, hook := range rt.Hooks.Poststop {
err := execHook(hook, state)
if err != nil {
fmt.Printf("exec Poststop hook %s failed %s", hook.Path, err.Error())
}
}
return nil
}
func prepareInfo(config *startConfig, c *pod.UserContainer, vmId string) (*hypervisor.ContainerInfo, error) {
var root string
var err error
containerId := GenerateRandomID()
sharedDir := filepath.Join(hypervisor.BaseDir, vmId, hypervisor.ShareDirTag)
rootDir := filepath.Join(sharedDir, containerId)
os.MkdirAll(rootDir, 0755)
rootDir = filepath.Join(rootDir, "rootfs")
if !filepath.IsAbs(c.Image) {
root = filepath.Join(config.BundlePath, c.Image)
} else {
root = c.Image
}
err = utils.Mount(root, rootDir)
if err != nil {
fmt.Printf("mount %s to %s failed: %s\n", root, rootDir, err.Error())
return nil, err
}
containerInfo := &hypervisor.ContainerInfo{
Id: containerId,
Rootfs: "rootfs",
Image: containerId,
Fstype: "dir",
}
return containerInfo, nil
}
func startVm(config *startConfig, userPod *pod.UserPod, vmId string) (*hypervisor.Vm, error) {
var (
err error
cpu = 1
mem = 128
)
vbox := config.Vbox
if _, err = os.Stat(vbox); err == nil {
vbox, err = filepath.Abs(vbox)
if err != nil {
fmt.Printf("Cannot get abs path for vbox: %s\n", err.Error())
return nil, err
}
}
if config.Driver == "vbox" {
if _, err = os.Stat(config.Vbox); err != nil {
return nil, err
}
} else {
if _, err = os.Stat(config.Kernel); err != nil {
return nil, err
}
if _, err = os.Stat(config.Initrd); err != nil {
return nil, err
}
}
if userPod.Resource.Vcpu > 0 {
cpu = userPod.Resource.Vcpu
}
if userPod.Resource.Memory > 0 {
mem = userPod.Resource.Memory
}
b := &hypervisor.BootConfig{
Kernel: config.Kernel,
Initrd: config.Initrd,
Bios: "",
Cbfs: "",
Vbox: config.Vbox,
CPU: cpu,
Memory: mem,
}
vm := hypervisor.NewVm(vmId, cpu, mem, false, types.VM_KEEP_NONE)
err = vm.Launch(b)
if err != nil {
fmt.Printf("%s\n", err.Error())
return nil, err
}
return vm, nil
}
func startRunvPod(context *nsContext, config *startConfig) (err error) {
context.lock.Lock()
defer context.lock.Unlock()
if context.firstConfig == nil {
context.firstConfig = config
} else {
// check stopped
if len(context.actives) == 0 {
return fmt.Errorf("The namespace service was stopped")
}
// check match
if config.Root != "" && config.Root != context.firstConfig.Root {
return fmt.Errorf("The root is not match")
}
if config.Driver != "" && config.Driver != context.firstConfig.Driver {
return fmt.Errorf("The driver is not match")
}
if config.Kernel != "" && config.Kernel != context.firstConfig.Kernel {
return fmt.Errorf("The kernel is not match")
}
if config.Initrd != "" && config.Initrd != context.firstConfig.Initrd {
return fmt.Errorf("The initrd is not match")
}
if config.Vbox != "" && config.Vbox != context.firstConfig.Vbox {
return fmt.Errorf("The vbox is not match")
}
// check shared namespace
for _, ns := range config.LinuxRuntimeSpec.Linux.Namespaces {
if ns.Path == "" {
continue
}
_, ok := context.actives[ns.Path]
if !ok {
return fmt.Errorf("Cann't share namespace with: %s", ns.Path)
}
}
// OK, the pod has been started, add this config and return
context.actives[config.Name] = config
return nil
}
hypervisor.InterfaceCount = 0
driver := config.Driver
if hypervisor.HDriver, err = driverloader.Probe(driver); err != nil {
fmt.Printf("%s\n", err.Error())
return err
}
context.podId = fmt.Sprintf("pod-%s", pod.RandStr(10, "alpha"))
context.vmId = fmt.Sprintf("vm-%s", pod.RandStr(10, "alpha"))
context.userPod = pod.ConvertOCF2PureUserPod(&config.LinuxSpec, &config.LinuxRuntimeSpec)
context.podStatus = hypervisor.NewPod(context.podId, context.userPod)
context.vm, err = startVm(config, context.userPod, context.vmId)
if err != nil {
fmt.Printf("%s\n", err.Error())
return err
}
Response := context.vm.StartPod(context.podStatus, context.userPod, nil, nil)
if Response.Data == nil {
fmt.Printf("StartPod fail: QEMU response data is nil\n")
return fmt.Errorf("StartPod fail")
}
fmt.Printf("result: code %d %s\n", Response.Code, Response.Cause)
context.actives[config.Name] = config
return nil
}
func cleanupRunvPod(context *nsContext, name string) {
context.lock.Lock()
defer context.lock.Unlock()
delete(context.actives, name)
if len(context.actives) > 0 {
return
}
Response := context.vm.StopPod(context.podStatus, "yes")
if Response.Data == nil {
fmt.Printf("StopPod fail: QEMU response data is nil\n")
return
}
fmt.Printf("result: code %d %s\n", Response.Code, Response.Cause)
os.RemoveAll(filepath.Join(hypervisor.BaseDir, context.vmId))
}
func createVSocket(context *nsContext, root, container string) error {
// create stateDir
stateDir := filepath.Join(root, container)
_, err := os.Stat(stateDir)
if err == nil {
fmt.Printf("Container %s exists\n", container)
return err
}
err = os.MkdirAll(stateDir, 0644)
if err != nil {
fmt.Printf("%s\n", err.Error())
return err
}
defer func() {
if err != nil {
os.RemoveAll(stateDir)
}
}()
// create connection sock
sock, err := net.Listen("unix", filepath.Join(stateDir, "runv.sock"))
if err != nil {
fmt.Printf("%s\n", err.Error())
return err
}
context.sockets[container] = sock
return nil
}
func cleanupVSocket(context *nsContext, root, container string) {
stateDir := filepath.Join(root, container)
if sock, ok := context.sockets[container]; ok {
sock.Close()
delete(context.sockets, container)
}
os.RemoveAll(stateDir)
}
func startVContainer(context *nsContext, root, container string) {
stateDir := filepath.Join(root, container)
err := createVSocket(context, root, container)
if err != nil {
fmt.Printf("create runv Socket err: %v\n", err)
return
}
sock, ok := context.sockets[container]
if !ok {
fmt.Printf("can not find runv Socket, container %v\n", container)
return
}
conn, err := sock.Accept()
if err != nil {
fmt.Printf("accept on runv Socket err: %v\n", err)
return
}
// get config from sock
msg, err := hypervisor.ReadVmMessage(conn.(*net.UnixConn))
if err != nil || msg.Code != RUNV_STARTCONTAINER {
fmt.Printf("read runv client data failed: %v\n", err)
return
}
config := &startConfig{}
err = json.Unmarshal(msg.Message, config)
if err != nil || config.Root != root || config.Name != container {
fmt.Printf("parse runv start config failed: %v\n", err)
return
}
// start pure pod
err = startRunvPod(context, config)
if err != nil {
fmt.Printf("Start Pod failed: %s\n", err.Error())
return
}
defer cleanupRunvPod(context, container)
// save the state
state := &specs.State{
Version: config.LinuxSpec.Spec.Version,
ID: container,
Pid: -1,
BundlePath: config.BundlePath,
}
stateData, err := json.MarshalIndent(state, "", "\t")
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
stateFile := filepath.Join(stateDir, "state.json")
err = ioutil.WriteFile(stateFile, stateData, 0644)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
userContainer := pod.ConvertOCF2UserContainer(&config.LinuxSpec, &config.LinuxRuntimeSpec)
info, err := prepareInfo(config, userContainer, context.vmId)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
defer func() {
rootDir := filepath.Join(hypervisor.BaseDir, context.vmId, hypervisor.ShareDirTag, info.Id, "rootfs")
utils.Umount(rootDir)
os.RemoveAll(filepath.Join(hypervisor.BaseDir, context.vmId, hypervisor.ShareDirTag, info.Id))
}()
tag, _ := runvAllocAndRespondTag(conn)
tty := &hypervisor.TtyIO{
ClientTag: tag,
Stdin: conn,
Stdout: conn,
Callback: make(chan *types.VmResponse, 1),
}
context.Lock()
context.ttyList[tag] = tty
context.Unlock()
err = context.vm.Attach(tty, info.Id, nil)
if err != nil {
fmt.Printf("StartPod fail: fail to set up tty connection.\n")
return
}
err = execPrestartHooks(&config.LinuxRuntimeSpec.RuntimeSpec, state)
if err != nil {
fmt.Printf("execute Prestart hooks failed, %s\n", err.Error())
return
}
context.podStatus.AddContainer(info.Id, context.podId, "", []string{}, types.S_POD_CREATED)
context.vm.NewContainer(userContainer, info)
ListenAndHandleRunvRequests(context, info, sock)
err = execPoststartHooks(&config.LinuxRuntimeSpec.RuntimeSpec, state)
if err != nil {
fmt.Printf("execute Poststart hooks failed %s\n", err.Error())
}
err = tty.WaitForFinish()
if err != nil {
fmt.Printf("get exit code failed %s\n", err.Error())
}
err = execPoststopHooks(&config.LinuxRuntimeSpec.RuntimeSpec, state)
if err != nil {
fmt.Printf("execute Poststop hooks failed %s\n", err.Error())
return
}
}
func runvRequest(root, name string, code uint32, msg interface{}) (net.Conn, error) {
conn, err := utils.UnixSocketConnect(filepath.Join(root, name, "runv.sock"))
if err != nil {
return nil, err
}
cmd, err := json.Marshal(msg)
if err != nil {
conn.Close()
return nil, err
}
m := &hypervisor.DecodedMessage{
Code: code,
Message: cmd,
}
data := hypervisor.NewVmMessage(m)
w, err := conn.Write(data[:])
if w != len(data) {
err = fmt.Errorf("Not full write") // TODO
}
if err != nil {
conn.Close()
return nil, err
}
return conn, nil
}
func runvAllocAndRespondTag(conn net.Conn) (tag string, err error) {
tag = pod.RandStr(8, "alphanum")
m := &hypervisor.DecodedMessage{
Code: RUNV_ACK,
Message: []byte(tag),
}
data := hypervisor.NewVmMessage(m)
conn.Write(data)
return tag, nil
}
func runvGetTag(conn net.Conn) (tag string, err error) {
msg, err := hypervisor.ReadVmMessage(conn.(*net.UnixConn))
if err != nil {
fmt.Printf("read runv server data failed: %v\n", err)
return "", err
}
if msg.Code != RUNV_ACK {
return "", fmt.Errorf("unexpected respond code")
}
return string(msg.Message), nil
}
type ttyTagCmd struct {
Root string
Container string
Tag string
}
func HandleRunvRequest(context *nsContext, info *hypervisor.ContainerInfo, conn net.Conn) {
defer context.wg.Done()
defer conn.Close()
msg, err := hypervisor.ReadVmMessage(conn.(*net.UnixConn))
if err != nil {
fmt.Printf("read runv client data failed: %v\n", err)
return
}
switch msg.Code {
case RUNV_INITCONTAINER:
{
initCmd := &initContainerCmd{}
err = json.Unmarshal(msg.Message, initCmd)
if err != nil {
fmt.Printf("parse runv init container command failed: %v\n", err)
return
}
startVContainer(context, initCmd.Root, initCmd.Name)
}
case RUNV_EXECCMD:
{
tag, _ := runvAllocAndRespondTag(conn)
fmt.Printf("client exec cmd request %s\n", msg.Message[:])
tty := &hypervisor.TtyIO{
ClientTag: tag,
Stdin: conn,
Stdout: conn,
Callback: make(chan *types.VmResponse, 1),
}
context.Lock()
context.ttyList[tag] = tty
context.Unlock()
err = context.vm.Exec(tty, info.Id, string(msg.Message[:]))
if err != nil {
fmt.Printf("read runv client data failed: %v\n", err)
}
}
case RUNV_EXITSTATUS:
{
var code uint8 = 255
tagCmd := &ttyTagCmd{}
err = json.Unmarshal(msg.Message, &tagCmd)
if err != nil {
fmt.Printf("parse exit status failed: %v\n", err)
return
}
fmt.Printf("client get exit status: tag %v\n", tagCmd)
context.Lock()
if tty, ok := context.ttyList[tagCmd.Tag]; ok {
code = uint8(tty.ExitCode)
delete(context.ttyList, tagCmd.Tag)
}
context.Unlock()
m := &hypervisor.DecodedMessage{
Code: RUNV_EXITSTATUS,
Message: []byte{code},
}
data := hypervisor.NewVmMessage(m)
conn.Write(data)
/* Get exit code of Container, it's time to let container go */
if tagCmd.Container != "" {
cleanupVSocket(context, tagCmd.Root, tagCmd.Container)
}
}
case RUNV_WINSIZE:
{
var winSize ttyWinSize
json.Unmarshal(msg.Message, &winSize)
//fmt.Printf("client exec winsize request %v\n", winSize)
context.vm.Tty(winSize.Tag, winSize.Height, winSize.Width)
}
case RUNV_KILLCONTAINER:
{
killCmd := &killContainerCmd{}
err = json.Unmarshal(msg.Message, killCmd)
if err != nil {
fmt.Printf("parse runv kill container command failed: %v\n", err)
return
}
context.vm.KillContainer(info.Id, killCmd.Signal)
}
default:
fmt.Printf("unknown cient request\n")
}
}
func ListenAndHandleRunvRequests(context *nsContext, info *hypervisor.ContainerInfo, sock net.Listener) {
context.wg.Add(1)
go func() {
defer context.wg.Done()
for {
conn, err := sock.Accept()
if err != nil {
fmt.Printf("accept on runv Socket err: %v\n", err)
break
}
context.wg.Add(1)
go HandleRunvRequest(context, info, conn)
}
}()
}