-
Notifications
You must be signed in to change notification settings - Fork 6
/
boot.go
102 lines (89 loc) · 2.56 KB
/
boot.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
goruntime "runtime"
"strconv"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/sys/unix"
)
var bootCommand = cli.Command{
Name: "boot",
Usage: "(internal use only) boot a container",
Flags: []cli.Flag{
cli.StringFlag{
Name: "bundle, b",
Value: "",
Usage: `path to the root of the bundle directory, defaults to the current directory`,
},
cli.StringFlag{
Name: "pid-file",
Usage: "specify the file to write the process id to",
},
},
Action: func(context *cli.Context) error {
return bootContainer(context, false)
},
}
func bootContainer(context *cli.Context, attach bool) error {
container := context.Args().First()
cmd, err := prepareUkontainer(context)
if err != nil {
return fmt.Errorf("failed to prepare container: %v", err)
}
// write pid file for containerd-shim
if pidf := context.String("pid-file"); pidf != "" {
// 0) pid file for containerd
f, err := os.OpenFile(pidf,
os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
if err != nil {
return fmt.Errorf("pid-file: %s\n", err)
}
//
// XXX:
// linux should tell containerd with a child process (subreaper)
// while darwin should with a grandchild process (ReapMore)
//
if goruntime.GOOS == "linux" {
_, _ = fmt.Fprintf(f, "%d", os.Getpid())
} else if goruntime.GOOS == "darwin" {
_, _ = fmt.Fprintf(f, "%d", cmd.Process.Pid)
}
f.Close()
}
saveState(specs.StateCreated, container, context)
envInitPipe := os.Getenv("_LIBCONTAINER_INITPIPE")
pipefd, err := strconv.Atoi(envInitPipe)
if err != nil {
return fmt.Errorf("unable to convert _LIBCONTAINER_INITPIPE=%s to int: %s", envInitPipe, err)
}
// notify to `runu create`
pipe := os.NewFile(uintptr(pipefd), "pipe")
logrus.Debugf("writing pipe %s _LIBCONTAINER_INITPIPE=%s", pipe.Name(), envInitPipe)
pipe.Write([]byte("1"))
pipe.Close()
// catch child errors if possible
err = cmd.Wait()
if err != nil {
logrus.Warnf("wait error %s", err)
}
// stop 9pfs server
err9 := killFromPidFile(context, pidFile9p, 15)
if err9 != nil {
logrus.Warnf("killing 9pfs error %s", err9)
}
// signal to containerd-shim to request exit
bundle := context.String("bundle")
file := filepath.Join(bundle, "shim.pid")
pid, _ := ioutil.ReadFile(file)
pidI, _ := strconv.Atoi(string(pid))
unix.Kill(pidI, unix.SIGCHLD)
logrus.Debugf("sending SIGCHLD to parent %d", pidI)
saveState(specs.StateStopped, container, context)
logrus.Debugf("process stopped %s", cmd.Args)
return err
}