Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit dac6bdc

Browse files
authored
Merge pull request #453 from WeiZhang555/add-cbfs-support
Add bios+cbfs support for runv
2 parents 68a7478 + c2bd4b8 commit dac6bdc

File tree

4 files changed

+118
-56
lines changed

4 files changed

+118
-56
lines changed

containerd/containerd.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ var ContainerdCommand = cli.Command{
8080
driver := context.GlobalString("driver")
8181
kernel := context.GlobalString("kernel")
8282
initrd := context.GlobalString("initrd")
83+
bios := context.GlobalString("bios")
84+
cbfs := context.GlobalString("cbfs")
8385
vsock := context.GlobalBool("vsock")
8486
template := context.GlobalString("template")
8587
stateDir := context.String("state-dir")
@@ -105,14 +107,16 @@ var ContainerdCommand = cli.Command{
105107

106108
if (driver != "" && driver != tconfig.Driver) ||
107109
(kernel != "" && kernel != tconfig.Config.Kernel) ||
108-
(initrd != "" && initrd != tconfig.Config.Initrd) {
109-
glog.Warningf("template config is not match the driver, kernel or initrd argument, disable template")
110+
(initrd != "" && initrd != tconfig.Config.Initrd) ||
111+
(bios != "" && bios != tconfig.Config.Bios) ||
112+
(cbfs != "" && cbfs != tconfig.Config.Cbfs) {
113+
glog.Warningf("template config is not match the driver, kernel, initrd, bios or cbfs argument, disable template")
110114
template = ""
111115
} else if driver == "" {
112116
driver = tconfig.Driver
113117
}
114-
} else if kernel == "" || initrd == "" {
115-
glog.Error("argument kernel and initrd must be set")
118+
} else if (bios == "" || cbfs == "") && (kernel == "" || initrd == "") {
119+
glog.Error("argument kernel+initrd or bios+cbfs must be set")
116120
os.Exit(1)
117121
}
118122

@@ -130,6 +134,8 @@ var ContainerdCommand = cli.Command{
130134
bootConfig := hypervisor.BootConfig{
131135
Kernel: kernel,
132136
Initrd: initrd,
137+
Bios: bios,
138+
Cbfs: cbfs,
133139
EnableVsock: vsock,
134140
}
135141
f = singlefactory.Dummy(bootConfig)

create.go

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ import (
1818
netcontext "golang.org/x/net/context"
1919
)
2020

21-
func firstExistingFile(candidates []string) string {
22-
for _, file := range candidates {
23-
if _, err := os.Stat(file); err == nil {
24-
return file
25-
}
26-
}
27-
return ""
28-
}
29-
30-
func getDefaultBundlePath() string {
31-
cwd, err := os.Getwd()
32-
if err != nil {
33-
return ""
34-
}
35-
return cwd
36-
}
37-
3821
var createCommand = cli.Command{
3922
Name: "create",
4023
Usage: "create a container",
@@ -140,38 +123,6 @@ func runContainer(context *cli.Context, createOnly bool) {
140123
}
141124
}
142125

143-
kernel := context.GlobalString("kernel")
144-
initrd := context.GlobalString("initrd")
145-
// only set the default kernel/initrd when it is the first container(sharedContainer == "")
146-
if kernel == "" && sharedContainer == "" {
147-
kernel = firstExistingFile([]string{
148-
filepath.Join(bundle, spec.Root.Path, "boot/vmlinuz"),
149-
filepath.Join(bundle, "boot/vmlinuz"),
150-
filepath.Join(bundle, "vmlinuz"),
151-
"/var/lib/hyper/kernel",
152-
})
153-
}
154-
if initrd == "" && sharedContainer == "" {
155-
initrd = firstExistingFile([]string{
156-
filepath.Join(bundle, spec.Root.Path, "boot/initrd.img"),
157-
filepath.Join(bundle, "boot/initrd.img"),
158-
filepath.Join(bundle, "initrd.img"),
159-
"/var/lib/hyper/hyper-initrd.img",
160-
})
161-
}
162-
163-
// convert the paths to abs
164-
kernel, err = filepath.Abs(kernel)
165-
if err != nil {
166-
fmt.Fprintf(os.Stderr, "Cannot get abs path for kernel: %v\n", err)
167-
os.Exit(-1)
168-
}
169-
initrd, err = filepath.Abs(initrd)
170-
if err != nil {
171-
fmt.Fprintf(os.Stderr, "Cannot get abs path for initrd: %v\n", err)
172-
os.Exit(-1)
173-
}
174-
175126
var namespace string
176127
var cmd *exec.Cmd
177128
if sharedContainer != "" {
@@ -188,18 +139,30 @@ func runContainer(context *cli.Context, createOnly bool) {
188139
os.Exit(-1)
189140
}
190141

142+
kernel, initrd, bios, cbfs, err := getKernelFiles(context, spec.Root.Path)
143+
if err != nil {
144+
fmt.Fprintf(os.Stderr, "Can't find kernel/initrd/bios/cbfs files")
145+
os.Exit(-1)
146+
}
147+
191148
namespace, err = ioutil.TempDir("/run", "runv-namespace-")
192149
if err != nil {
193150
fmt.Fprintf(os.Stderr, "Failed to create runv namespace path: %v", err)
194151
os.Exit(-1)
195152
}
196153

197154
args := []string{
198-
"--kernel", kernel,
199-
"--initrd", initrd,
200155
"--default_cpus", fmt.Sprintf("%d", context.GlobalInt("default_cpus")),
201156
"--default_memory", fmt.Sprintf("%d", context.GlobalInt("default_memory")),
202157
}
158+
159+
// if user set bios+cbfs, then use bios+cbfs first
160+
if context.GlobalString("bios") != "" && context.GlobalString("cbfs") != "" {
161+
args = append(args, "--bios", bios, "--cbfs", cbfs)
162+
} else {
163+
args = append(args, "--kernel", kernel, "--initrd", initrd)
164+
}
165+
203166
if context.GlobalBool("debug") {
204167
args = append(args, "--debug")
205168
}

main.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/docker/docker/pkg/reexec"
13+
"github.com/golang/glog"
1314
"github.com/golang/protobuf/ptypes"
1415
"github.com/hyperhq/runv/containerd"
1516
"github.com/hyperhq/runv/containerd/api/grpc/types"
@@ -109,6 +110,14 @@ func main() {
109110
Name: "initrd",
110111
Usage: "runv-compatible initrd for the container",
111112
},
113+
cli.StringFlag{
114+
Name: "bios",
115+
Usage: "bios for the container",
116+
},
117+
cli.StringFlag{
118+
Name: "cbfs",
119+
Usage: "cbfs for the container",
120+
},
112121
cli.StringFlag{
113122
Name: "template",
114123
Usage: "path to the template vm state directory",
@@ -132,6 +141,11 @@ func main() {
132141
}
133142
return nil
134143
}
144+
app.After = func(context *cli.Context) error {
145+
// make sure glog flush all the messages to file
146+
glog.Flush()
147+
return nil
148+
}
135149

136150
app.Commands = []cli.Command{
137151
createCommand,
@@ -147,7 +161,7 @@ func main() {
147161
containerd.ContainerdCommand,
148162
}
149163
if err := app.Run(os.Args); err != nil {
150-
fmt.Printf("%s\n", err.Error())
164+
fmt.Fprintf(os.Stderr, "%v", err)
151165
}
152166
}
153167

utils.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/urfave/cli"
9+
)
10+
11+
const (
12+
defaultKernelInstallDir string = "/var/lib/hyper"
13+
)
14+
15+
func firstExistingFile(candidates []string) string {
16+
for _, file := range candidates {
17+
if _, err := os.Stat(file); err == nil {
18+
return file
19+
}
20+
}
21+
return ""
22+
}
23+
24+
func getDefaultBundlePath() string {
25+
cwd, err := os.Getwd()
26+
if err != nil {
27+
return ""
28+
}
29+
return cwd
30+
}
31+
32+
// getKernelFiles chooses kernel/initrd/bios/cbfs files based on user specified ones
33+
func getKernelFiles(context *cli.Context, rootPath string) (string, string, string, string, error) {
34+
kernel := context.GlobalString("kernel")
35+
initrd := context.GlobalString("initrd")
36+
bios := context.GlobalString("bios")
37+
cbfs := context.GlobalString("cbfs")
38+
bundle := context.String("bundle")
39+
40+
for k, v := range map[*string][]string{
41+
&kernel: {
42+
filepath.Join(bundle, rootPath, "boot/vmlinuz"),
43+
filepath.Join(bundle, "boot/vmlinuz"),
44+
filepath.Join(bundle, "vmlinuz"),
45+
filepath.Join(defaultKernelInstallDir, "kernel"),
46+
},
47+
&initrd: {
48+
filepath.Join(bundle, rootPath, "boot/initrd.img"),
49+
filepath.Join(bundle, "boot/initrd.img"),
50+
filepath.Join(bundle, "initrd.img"),
51+
filepath.Join(defaultKernelInstallDir, "hyper-initrd.img"),
52+
},
53+
&bios: {
54+
filepath.Join(bundle, rootPath, "boot/bios.bin"),
55+
filepath.Join(bundle, "boot/bios.bin"),
56+
filepath.Join(bundle, "bios.bin"),
57+
filepath.Join(defaultKernelInstallDir, "bios.bin"),
58+
},
59+
&cbfs: {
60+
filepath.Join(bundle, rootPath, "boot/cbfs.rom"),
61+
filepath.Join(bundle, "boot/cbfs.rom"),
62+
filepath.Join(bundle, "cbfs.rom"),
63+
filepath.Join(defaultKernelInstallDir, "cbfs.rom"),
64+
},
65+
} {
66+
if *k == "" {
67+
*k = firstExistingFile(v)
68+
}
69+
if *k != "" {
70+
var err error
71+
*k, err = filepath.Abs(*k)
72+
if err != nil {
73+
return "", "", "", "", fmt.Errorf("cannot get abs path for kernel files: %v", err)
74+
}
75+
}
76+
}
77+
78+
return kernel, initrd, bios, cbfs, nil
79+
}

0 commit comments

Comments
 (0)