Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ var ContainerdCommand = cli.Command{
driver := context.GlobalString("driver")
kernel := context.GlobalString("kernel")
initrd := context.GlobalString("initrd")
bios := context.GlobalString("bios")
cbfs := context.GlobalString("cbfs")
vsock := context.GlobalBool("vsock")
template := context.GlobalString("template")
stateDir := context.String("state-dir")
Expand All @@ -105,14 +107,16 @@ var ContainerdCommand = cli.Command{

if (driver != "" && driver != tconfig.Driver) ||
(kernel != "" && kernel != tconfig.Config.Kernel) ||
(initrd != "" && initrd != tconfig.Config.Initrd) {
glog.Warningf("template config is not match the driver, kernel or initrd argument, disable template")
(initrd != "" && initrd != tconfig.Config.Initrd) ||
(bios != "" && bios != tconfig.Config.Bios) ||
(cbfs != "" && cbfs != tconfig.Config.Cbfs) {
glog.Warningf("template config is not match the driver, kernel, initrd, bios or cbfs argument, disable template")
template = ""
} else if driver == "" {
driver = tconfig.Driver
}
} else if kernel == "" || initrd == "" {
glog.Error("argument kernel and initrd must be set")
} else if (bios == "" || cbfs == "") && (kernel == "" || initrd == "") {
glog.Error("argument kernel+initrd or bios+cbfs must be set")
os.Exit(1)
}

Expand All @@ -130,6 +134,8 @@ var ContainerdCommand = cli.Command{
bootConfig := hypervisor.BootConfig{
Kernel: kernel,
Initrd: initrd,
Bios: bios,
Cbfs: cbfs,
EnableVsock: vsock,
}
f = singlefactory.Dummy(bootConfig)
Expand Down
65 changes: 14 additions & 51 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ import (
netcontext "golang.org/x/net/context"
)

func firstExistingFile(candidates []string) string {
for _, file := range candidates {
if _, err := os.Stat(file); err == nil {
return file
}
}
return ""
}

func getDefaultBundlePath() string {
cwd, err := os.Getwd()
if err != nil {
return ""
}
return cwd
}

var createCommand = cli.Command{
Name: "create",
Usage: "create a container",
Expand Down Expand Up @@ -140,38 +123,6 @@ func runContainer(context *cli.Context, createOnly bool) {
}
}

kernel := context.GlobalString("kernel")
initrd := context.GlobalString("initrd")
// only set the default kernel/initrd when it is the first container(sharedContainer == "")
if kernel == "" && sharedContainer == "" {
kernel = firstExistingFile([]string{
filepath.Join(bundle, spec.Root.Path, "boot/vmlinuz"),
filepath.Join(bundle, "boot/vmlinuz"),
filepath.Join(bundle, "vmlinuz"),
"/var/lib/hyper/kernel",
})
}
if initrd == "" && sharedContainer == "" {
initrd = firstExistingFile([]string{
filepath.Join(bundle, spec.Root.Path, "boot/initrd.img"),
filepath.Join(bundle, "boot/initrd.img"),
filepath.Join(bundle, "initrd.img"),
"/var/lib/hyper/hyper-initrd.img",
})
}

// convert the paths to abs
kernel, err = filepath.Abs(kernel)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot get abs path for kernel: %v\n", err)
os.Exit(-1)
}
initrd, err = filepath.Abs(initrd)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot get abs path for initrd: %v\n", err)
os.Exit(-1)
}

var namespace string
var cmd *exec.Cmd
if sharedContainer != "" {
Expand All @@ -188,18 +139,30 @@ func runContainer(context *cli.Context, createOnly bool) {
os.Exit(-1)
}

kernel, initrd, bios, cbfs, err := getKernelFiles(context, spec.Root.Path)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't find kernel/initrd/bios/cbfs files")
os.Exit(-1)
}

namespace, err = ioutil.TempDir("/run", "runv-namespace-")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create runv namespace path: %v", err)
os.Exit(-1)
}

args := []string{
"--kernel", kernel,
"--initrd", initrd,
"--default_cpus", fmt.Sprintf("%d", context.GlobalInt("default_cpus")),
"--default_memory", fmt.Sprintf("%d", context.GlobalInt("default_memory")),
}

// if user set bios+cbfs, then use bios+cbfs first
if context.GlobalString("bios") != "" && context.GlobalString("cbfs") != "" {
args = append(args, "--bios", bios, "--cbfs", cbfs)
} else {
args = append(args, "--kernel", kernel, "--initrd", initrd)
}

if context.GlobalBool("debug") {
args = append(args, "--debug")
}
Expand Down
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/docker/docker/pkg/reexec"
"github.com/golang/glog"
"github.com/golang/protobuf/ptypes"
"github.com/hyperhq/runv/containerd"
"github.com/hyperhq/runv/containerd/api/grpc/types"
Expand Down Expand Up @@ -109,6 +110,14 @@ func main() {
Name: "initrd",
Usage: "runv-compatible initrd for the container",
},
cli.StringFlag{
Name: "bios",
Usage: "bios for the container",
},
cli.StringFlag{
Name: "cbfs",
Usage: "cbfs for the container",
},
cli.StringFlag{
Name: "template",
Usage: "path to the template vm state directory",
Expand All @@ -132,6 +141,11 @@ func main() {
}
return nil
}
app.After = func(context *cli.Context) error {
// make sure glog flush all the messages to file
glog.Flush()
return nil
}

app.Commands = []cli.Command{
createCommand,
Expand All @@ -147,7 +161,7 @@ func main() {
containerd.ContainerdCommand,
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("%s\n", err.Error())
fmt.Fprintf(os.Stderr, "%v", err)
}
}

Expand Down
79 changes: 79 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"
"os"
"path/filepath"

"github.com/urfave/cli"
)

const (
defaultKernelInstallDir string = "/var/lib/hyper"
)

func firstExistingFile(candidates []string) string {
for _, file := range candidates {
if _, err := os.Stat(file); err == nil {
return file
}
}
return ""
}

func getDefaultBundlePath() string {
cwd, err := os.Getwd()
if err != nil {
return ""
}
return cwd
}

// getKernelFiles chooses kernel/initrd/bios/cbfs files based on user specified ones
func getKernelFiles(context *cli.Context, rootPath string) (string, string, string, string, error) {
kernel := context.GlobalString("kernel")
initrd := context.GlobalString("initrd")
bios := context.GlobalString("bios")
cbfs := context.GlobalString("cbfs")
bundle := context.String("bundle")

for k, v := range map[*string][]string{
&kernel: {
filepath.Join(bundle, rootPath, "boot/vmlinuz"),
filepath.Join(bundle, "boot/vmlinuz"),
filepath.Join(bundle, "vmlinuz"),
filepath.Join(defaultKernelInstallDir, "kernel"),
},
&initrd: {
filepath.Join(bundle, rootPath, "boot/initrd.img"),
filepath.Join(bundle, "boot/initrd.img"),
filepath.Join(bundle, "initrd.img"),
filepath.Join(defaultKernelInstallDir, "hyper-initrd.img"),
},
&bios: {
filepath.Join(bundle, rootPath, "boot/bios.bin"),
filepath.Join(bundle, "boot/bios.bin"),
filepath.Join(bundle, "bios.bin"),
filepath.Join(defaultKernelInstallDir, "bios.bin"),
},
&cbfs: {
filepath.Join(bundle, rootPath, "boot/cbfs.rom"),
filepath.Join(bundle, "boot/cbfs.rom"),
filepath.Join(bundle, "cbfs.rom"),
filepath.Join(defaultKernelInstallDir, "cbfs.rom"),
},
} {
if *k == "" {
*k = firstExistingFile(v)
}
if *k != "" {
var err error
*k, err = filepath.Abs(*k)
if err != nil {
return "", "", "", "", fmt.Errorf("cannot get abs path for kernel files: %v", err)
}
}
}

return kernel, initrd, bios, cbfs, nil
}