-
Notifications
You must be signed in to change notification settings - Fork 5
/
create.go
150 lines (132 loc) · 3.63 KB
/
create.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var createCommand = cli.Command{
Name: "create",
Usage: "create a container",
ArgsUsage: `<container-id>`,
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 {
args := context.Args()
if args.Present() == false {
return fmt.Errorf("Missing container ID")
}
return cmdCreateUkon(context, false)
},
}
func prepareBootCommand(context *cli.Context, container string, childPipe *os.File, volumeMounted bool) (*exec.Cmd, error) {
root := context.GlobalString("root")
const stdioFdCount = 3
// call `runu boot` to create new process
self, err := os.Executable()
if err != nil {
return nil, fmt.Errorf("could not identify who am I: %v", err)
}
args := []string{}
if val := context.GlobalString("log-format"); val != "" {
args = append(args, "-log-format")
args = append(args, context.GlobalString("log-format"))
}
if val := context.GlobalString("log"); val != "" {
args = append(args, "-log")
args = append(args, context.GlobalString("log"))
}
if val := context.GlobalString("root"); val != "" {
args = append(args, "-root")
args = append(args, context.GlobalString("root"))
}
if context.GlobalBool("debug") {
args = append(args, "-debug")
}
args = append(args, "boot")
if val := context.String("bundle"); val != "" {
args = append(args, "-bundle")
args = append(args, context.String("bundle"))
}
if val := context.String("pid-file"); val != "" {
args = append(args, "-pid-file")
args = append(args, context.String("pid-file"))
}
args = append(args, container)
cmd := exec.Command(self, args...)
cmd.ExtraFiles = append(cmd.ExtraFiles, childPipe)
cmd.Env = append(cmd.Env,
fmt.Sprintf("_LIBCONTAINER_INITPIPE=%d", stdioFdCount+len(cmd.ExtraFiles)-1),
)
if volumeMounted {
cmd.Env = append(cmd.Env, "LKL_USE_9PFS=1")
}
cwd, _ := os.Getwd()
logrus.Debugf("Starting command %s, cwd=%s, root=%s",
cmd.Args, cwd, root)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: false,
}
return cmd, nil
}
func cmdCreateUkon(context *cli.Context, attach bool) error {
root := context.GlobalString("root")
bundle := context.String("bundle")
container := context.Args().First()
ocffile := filepath.Join(bundle, specConfig)
spec, err := loadSpec(ocffile)
if err != nil {
return fmt.Errorf("load config failed: %v", err)
}
if container == "" {
return fmt.Errorf("no container id provided")
}
err = createContainer(container, bundle, root, spec)
if err != nil {
return fmt.Errorf("failed to create container: %v", err)
}
volumeMounted, err := doMounts(spec)
if err != nil {
return err
}
// wait for init complete
parentPipe, childPipe, err := os.Pipe()
if err != nil {
return fmt.Errorf("create: pipe failure (%s)", err)
}
cmd, err := prepareBootCommand(context, container, childPipe, volumeMounted)
if err := cmd.Start(); err != nil {
panic(err)
}
go func() {
err = cmd.Wait()
if err != nil {
fmt.Printf("failed to wait a process: %s", cmd.Args)
panic(err)
}
}()
buf := make([]byte, 1)
logrus.Debugf("Waiting for pipe to complete boot %s", cmd.Args)
if _, err := parentPipe.Read(buf); err != nil {
fmt.Printf("pipe read: %s", err)
}
parentPipe.Close()
childPipe.Close()
logrus.Debugf("Waiting pipe done")
return nil
}