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
11 changes: 8 additions & 3 deletions containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ var ContainerdCommand = cli.Command{
f.Close()

if (driver != "" && driver != tconfig.Driver) ||
(kernel != "" && kernel != tconfig.Kernel) ||
(initrd != "" && initrd != tconfig.Initrd) {
(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")
template = ""
} else if driver == "" {
Expand All @@ -127,7 +127,12 @@ var ContainerdCommand = cli.Command{
if template != "" {
f = singlefactory.New(templatefactory.NewFromExisted(tconfig))
} else {
f = factory.NewFromConfigs(kernel, initrd, vsock, nil)
bootConfig := hypervisor.BootConfig{
Kernel: kernel,
Initrd: initrd,
EnableVsock: vsock,
}
f = singlefactory.Dummy(bootConfig)
}
sv, err := supervisor.New(stateDir, containerdDir, f,
context.GlobalInt("default_cpus"), context.GlobalInt("default_memory"))
Expand Down
9 changes: 1 addition & 8 deletions factory/direct/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ type directFactory struct {
config hypervisor.BootConfig
}

func New(cpu, mem int, kernel, initrd string, vsock bool) base.Factory {
b := hypervisor.BootConfig{
CPU: cpu,
Memory: mem,
EnableVsock: vsock,
Kernel: kernel,
Initrd: initrd,
}
func New(b hypervisor.BootConfig) base.Factory {
return &directFactory{config: b}
}

Expand Down
16 changes: 9 additions & 7 deletions factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ type FactoryConfig struct {
Memory int `json:"memory"`
}

func NewFromConfigs(kernel, initrd string, vsock bool, configs []FactoryConfig) Factory {
func NewFromConfigs(bootConfig hypervisor.BootConfig, configs []FactoryConfig) Factory {
bases := make([]base.Factory, len(configs))
for i, c := range configs {
var b base.Factory
boot := bootConfig
boot.CPU = c.Cpu
boot.Memory = c.Memory
if c.Template {
b = template.New(filepath.Join(hypervisor.BaseDir, "template"), c.Cpu, c.Memory, kernel, initrd, vsock)
b = template.New(filepath.Join(hypervisor.BaseDir, "template"), boot)
} else {
b = direct.New(c.Cpu, c.Memory, kernel, initrd, vsock)
b = direct.New(boot)
}
bases[i] = cache.New(c.Cache, b)
}

if len(bases) == 0 {
// skip GetVm from the base factory
return single.New(direct.New(1000000, 1000000, kernel, initrd, vsock))
return single.Dummy(bootConfig)
} else if len(bases) == 1 {
return single.New(bases[0])
} else {
Expand All @@ -54,12 +56,12 @@ func NewFromConfigs(kernel, initrd string, vsock bool, configs []FactoryConfig)

// vmFactoryPolicy = [FactoryConfig,]*FactoryConfig
// FactoryConfig = {["cache":NUMBER,]["template":true|false,]"cpu":NUMBER,"memory":NUMBER}
func NewFromPolicy(kernel, initrd string, vsock bool, policy string) Factory {
func NewFromPolicy(bootConfig hypervisor.BootConfig, policy string) Factory {
var configs []FactoryConfig
jsonString := "[" + policy + "]"
err := json.Unmarshal([]byte(jsonString), &configs)
if err != nil && policy != "none" {
glog.Errorf("Incorrect policy: %s", policy)
}
return NewFromConfigs(kernel, initrd, vsock, configs)
return NewFromConfigs(bootConfig, configs)
}
3 changes: 2 additions & 1 deletion factory/multi/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func (f Factory) GetVm(cpu, mem int) (*hypervisor.Vm, error) {
return single.New(b).GetVm(cpu, mem)
}
}
return single.New(f[0]).GetVm(cpu, mem)
boot := *f[0].Config()
return single.Dummy(boot).GetVm(cpu, mem)
}

func (f Factory) CloseFactory() {
Expand Down
21 changes: 12 additions & 9 deletions factory/single/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@ func (f Factory) GetVm(cpu, mem int) (*hypervisor.Vm, error) {
// check if match the base
config := f.Config()
if config.CPU > cpu || config.Memory > mem {
// also strip unrelated option from @config
boot := &hypervisor.BootConfig{
CPU: cpu,
Memory: mem,
Kernel: config.Kernel,
Initrd: config.Initrd,
EnableVsock: config.EnableVsock,
}
return hypervisor.GetVm("", boot, false)
boot := *config
return Dummy(boot).GetVm(cpu, mem)
}

vm, err := f.GetBaseVm()
Expand Down Expand Up @@ -59,3 +52,13 @@ func (f Factory) GetVm(cpu, mem int) (*hypervisor.Vm, error) {
}
return vm, err
}

type Dummy hypervisor.BootConfig

func (f Dummy) GetVm(cpu, mem int) (*hypervisor.Vm, error) {
config := hypervisor.BootConfig(f)
config.CPU = cpu
config.Memory = mem
return hypervisor.GetVm("", &config, false)
}
func (f Dummy) CloseFactory() {}
6 changes: 3 additions & 3 deletions factory/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type templateFactory struct {
s *template.TemplateVmConfig
}

func New(templateRoot string, cpu, mem int, kernel, initrd string, vsock bool) base.Factory {
func New(templateRoot string, b hypervisor.BootConfig) base.Factory {
var vmName string

for {
Expand All @@ -26,11 +26,11 @@ func New(templateRoot string, cpu, mem int, kernel, initrd string, vsock bool) b
break
}
}
s, err := template.CreateTemplateVM(filepath.Join(templateRoot, vmName), vmName, cpu, mem, kernel, initrd, vsock)
s, err := template.CreateTemplateVM(filepath.Join(templateRoot, vmName), vmName, b)
if err != nil {
glog.Errorf("failed to create template factory: %v", err)
glog.V(3).Infof("use direct factory instead")
return direct.New(cpu, mem, kernel, initrd, vsock)
return direct.New(b)
}
return &templateFactory{s: s}
}
Expand Down
9 changes: 8 additions & 1 deletion manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ var createTemplateCommand = cli.Command{
os.Exit(-1)
}

if _, err := templatecore.CreateTemplateVM(template, "", context.Int("cpu"), context.Int("mem"), kernel, initrd, context.GlobalBool("vsock")); err != nil {
boot := hypervisor.BootConfig{
CPU: context.Int("cpu"),
Memory: context.Int("mem"),
Kernel: kernel,
Initrd: initrd,
EnableVsock: context.GlobalBool("vsock"),
}
if _, err := templatecore.CreateTemplateVM(template, "", boot); err != nil {
fmt.Printf("Failed to create the template: %v\n", err)
os.Exit(-1)
}
Expand Down
59 changes: 21 additions & 38 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,26 @@ import (
type TemplateVmConfig struct {
StatePath string `json:"statepath"`
Driver string `json:"driver"`
Cpu int `json:"cpu"`
Memory int `json:"memory"`
Kernel string `json:"kernel"`
Initrd string `json:"initrd"`
Config hypervisor.BootConfig
}

func CreateTemplateVM(statePath, vmName string, cpu, mem int, kernel, initrd string, vsock bool) (t *TemplateVmConfig, err error) {
func CreateTemplateVM(statePath, vmName string, b hypervisor.BootConfig) (t *TemplateVmConfig, err error) {
if b.BootToBeTemplate || b.BootFromTemplate || b.MemoryPath != "" || b.DevicesStatePath != "" {
return nil, fmt.Errorf("Error boot config for template")
}
b.MemoryPath = statePath + "/memory"
b.DevicesStatePath = statePath + "/state"

config := &TemplateVmConfig{
StatePath: statePath,
Driver: hypervisor.HDriver.Name(),
Config: b,
}
config.Config.BootFromTemplate = true

defer func() {
if err != nil {
(&TemplateVmConfig{StatePath: statePath}).Destroy()
config.Destroy()
}
}()

Expand All @@ -45,7 +55,7 @@ func CreateTemplateVM(statePath, vmName string, cpu, mem int, kernel, initrd str
return nil, err
}
flags := uintptr(syscall.MS_NOSUID | syscall.MS_NODEV)
opts := fmt.Sprintf("size=%dM", mem+8)
opts := fmt.Sprintf("size=%dM", b.Memory+8)
if err = syscall.Mount("tmpfs", statePath, "tmpfs", flags, opts); err != nil {
glog.Infof("mount template state path failed: %v", err)
return nil, err
Expand All @@ -58,18 +68,8 @@ func CreateTemplateVM(statePath, vmName string, cpu, mem int, kernel, initrd str
}

// launch vm
b := &hypervisor.BootConfig{
CPU: cpu,
Memory: mem,
BootToBeTemplate: true,
BootFromTemplate: false,
EnableVsock: vsock,
MemoryPath: statePath + "/memory",
DevicesStatePath: statePath + "/state",
Kernel: kernel,
Initrd: initrd,
}
vm, err := hypervisor.GetVm(vmName, b, true)
b.BootToBeTemplate = true
vm, err := hypervisor.GetVm(vmName, &b, true)
if err != nil {
return nil, err
}
Expand All @@ -89,15 +89,6 @@ func CreateTemplateVM(statePath, vmName string, cpu, mem int, kernel, initrd str
// so we wait here. We should fix it in the qemu driver side.
time.Sleep(1 * time.Second)

config := &TemplateVmConfig{
StatePath: statePath,
Driver: hypervisor.HDriver.Name(),
Cpu: cpu,
Memory: mem,
Kernel: kernel,
Initrd: initrd,
}

configData, err := json.MarshalIndent(config, "", "\t")
if err != nil {
glog.V(1).Infof("%s\n", err.Error())
Expand All @@ -114,16 +105,8 @@ func CreateTemplateVM(statePath, vmName string, cpu, mem int, kernel, initrd str
}

func (t *TemplateVmConfig) BootConfigFromTemplate() *hypervisor.BootConfig {
return &hypervisor.BootConfig{
CPU: t.Cpu,
Memory: t.Memory,
BootToBeTemplate: false,
BootFromTemplate: true,
MemoryPath: t.StatePath + "/memory",
DevicesStatePath: t.StatePath + "/state",
Kernel: t.Kernel,
Initrd: t.Initrd,
}
b := t.Config
return &b
}

// boot vm from template, the returned vm is paused
Expand Down