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

Commit 89ca0b5

Browse files
committed
use hypervisor.BootConfig for factories' arguements
make it easier to pass pc-lite, dax, tempalte, vsocks configurations to the hypervisor driver(qemu/libvirt...). Signed-off-by: Lai Jiangshan <jiangshanlai@gmail.com>
1 parent 567bbc5 commit 89ca0b5

File tree

8 files changed

+64
-70
lines changed

8 files changed

+64
-70
lines changed

containerd/containerd.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ var ContainerdCommand = cli.Command{
104104
f.Close()
105105

106106
if (driver != "" && driver != tconfig.Driver) ||
107-
(kernel != "" && kernel != tconfig.Kernel) ||
108-
(initrd != "" && initrd != tconfig.Initrd) {
107+
(kernel != "" && kernel != tconfig.Config.Kernel) ||
108+
(initrd != "" && initrd != tconfig.Config.Initrd) {
109109
glog.Warningf("template config is not match the driver, kernel or initrd argument, disable template")
110110
template = ""
111111
} else if driver == "" {
@@ -127,7 +127,12 @@ var ContainerdCommand = cli.Command{
127127
if template != "" {
128128
f = singlefactory.New(templatefactory.NewFromExisted(tconfig))
129129
} else {
130-
f = factory.NewFromConfigs(kernel, initrd, vsock, nil)
130+
bootConfig := hypervisor.BootConfig{
131+
Kernel: kernel,
132+
Initrd: initrd,
133+
EnableVsock: vsock,
134+
}
135+
f = singlefactory.Dummy(bootConfig)
131136
}
132137
sv, err := supervisor.New(stateDir, containerdDir, f,
133138
context.GlobalInt("default_cpus"), context.GlobalInt("default_memory"))

factory/direct/direct.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@ type directFactory struct {
1010
config hypervisor.BootConfig
1111
}
1212

13-
func New(cpu, mem int, kernel, initrd string, vsock bool) base.Factory {
14-
b := hypervisor.BootConfig{
15-
CPU: cpu,
16-
Memory: mem,
17-
EnableVsock: vsock,
18-
Kernel: kernel,
19-
Initrd: initrd,
20-
}
13+
func New(b hypervisor.BootConfig) base.Factory {
2114
return &directFactory{config: b}
2215
}
2316

factory/factory.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,23 @@ type FactoryConfig struct {
3030
Memory int `json:"memory"`
3131
}
3232

33-
func NewFromConfigs(kernel, initrd string, vsock bool, configs []FactoryConfig) Factory {
33+
func NewFromConfigs(bootConfig hypervisor.BootConfig, configs []FactoryConfig) Factory {
3434
bases := make([]base.Factory, len(configs))
3535
for i, c := range configs {
3636
var b base.Factory
37+
boot := bootConfig
38+
boot.CPU = c.Cpu
39+
boot.Memory = c.Memory
3740
if c.Template {
38-
b = template.New(filepath.Join(hypervisor.BaseDir, "template"), c.Cpu, c.Memory, kernel, initrd, vsock)
41+
b = template.New(filepath.Join(hypervisor.BaseDir, "template"), boot)
3942
} else {
40-
b = direct.New(c.Cpu, c.Memory, kernel, initrd, vsock)
43+
b = direct.New(boot)
4144
}
4245
bases[i] = cache.New(c.Cache, b)
4346
}
4447

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

5557
// vmFactoryPolicy = [FactoryConfig,]*FactoryConfig
5658
// FactoryConfig = {["cache":NUMBER,]["template":true|false,]"cpu":NUMBER,"memory":NUMBER}
57-
func NewFromPolicy(kernel, initrd string, vsock bool, policy string) Factory {
59+
func NewFromPolicy(bootConfig hypervisor.BootConfig, policy string) Factory {
5860
var configs []FactoryConfig
5961
jsonString := "[" + policy + "]"
6062
err := json.Unmarshal([]byte(jsonString), &configs)
6163
if err != nil && policy != "none" {
6264
glog.Errorf("Incorrect policy: %s", policy)
6365
}
64-
return NewFromConfigs(kernel, initrd, vsock, configs)
66+
return NewFromConfigs(bootConfig, configs)
6567
}

factory/multi/multi.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ func (f Factory) GetVm(cpu, mem int) (*hypervisor.Vm, error) {
1717
return single.New(b).GetVm(cpu, mem)
1818
}
1919
}
20-
return single.New(f[0]).GetVm(cpu, mem)
20+
boot := *f[0].Config()
21+
return single.Dummy(boot).GetVm(cpu, mem)
2122
}
2223

2324
func (f Factory) CloseFactory() {

factory/single/single.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,8 @@ func (f Factory) GetVm(cpu, mem int) (*hypervisor.Vm, error) {
1616
// check if match the base
1717
config := f.Config()
1818
if config.CPU > cpu || config.Memory > mem {
19-
// also strip unrelated option from @config
20-
boot := &hypervisor.BootConfig{
21-
CPU: cpu,
22-
Memory: mem,
23-
Kernel: config.Kernel,
24-
Initrd: config.Initrd,
25-
EnableVsock: config.EnableVsock,
26-
}
27-
return hypervisor.GetVm("", boot, false)
19+
boot := *config
20+
return Dummy(boot).GetVm(cpu, mem)
2821
}
2922

3023
vm, err := f.GetBaseVm()
@@ -59,3 +52,13 @@ func (f Factory) GetVm(cpu, mem int) (*hypervisor.Vm, error) {
5952
}
6053
return vm, err
6154
}
55+
56+
type Dummy hypervisor.BootConfig
57+
58+
func (f Dummy) GetVm(cpu, mem int) (*hypervisor.Vm, error) {
59+
config := hypervisor.BootConfig(f)
60+
config.CPU = cpu
61+
config.Memory = mem
62+
return hypervisor.GetVm("", &config, false)
63+
}
64+
func (f Dummy) CloseFactory() {}

factory/template/template.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type templateFactory struct {
1717
s *template.TemplateVmConfig
1818
}
1919

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

2323
for {
@@ -26,11 +26,11 @@ func New(templateRoot string, cpu, mem int, kernel, initrd string, vsock bool) b
2626
break
2727
}
2828
}
29-
s, err := template.CreateTemplateVM(filepath.Join(templateRoot, vmName), vmName, cpu, mem, kernel, initrd, vsock)
29+
s, err := template.CreateTemplateVM(filepath.Join(templateRoot, vmName), vmName, b)
3030
if err != nil {
3131
glog.Errorf("failed to create template factory: %v", err)
3232
glog.V(3).Infof("use direct factory instead")
33-
return direct.New(cpu, mem, kernel, initrd, vsock)
33+
return direct.New(b)
3434
}
3535
return &templateFactory{s: s}
3636
}

manage.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ var createTemplateCommand = cli.Command{
8080
os.Exit(-1)
8181
}
8282

83-
if _, err := templatecore.CreateTemplateVM(template, "", context.Int("cpu"), context.Int("mem"), kernel, initrd, context.GlobalBool("vsock")); err != nil {
83+
boot := hypervisor.BootConfig{
84+
CPU: context.Int("cpu"),
85+
Memory: context.Int("mem"),
86+
Kernel: kernel,
87+
Initrd: initrd,
88+
EnableVsock: context.GlobalBool("vsock"),
89+
}
90+
if _, err := templatecore.CreateTemplateVM(template, "", boot); err != nil {
8491
fmt.Printf("Failed to create the template: %v\n", err)
8592
os.Exit(-1)
8693
}

template/template.go

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,26 @@ import (
2626
type TemplateVmConfig struct {
2727
StatePath string `json:"statepath"`
2828
Driver string `json:"driver"`
29-
Cpu int `json:"cpu"`
30-
Memory int `json:"memory"`
31-
Kernel string `json:"kernel"`
32-
Initrd string `json:"initrd"`
29+
Config hypervisor.BootConfig
3330
}
3431

35-
func CreateTemplateVM(statePath, vmName string, cpu, mem int, kernel, initrd string, vsock bool) (t *TemplateVmConfig, err error) {
32+
func CreateTemplateVM(statePath, vmName string, b hypervisor.BootConfig) (t *TemplateVmConfig, err error) {
33+
if b.BootToBeTemplate || b.BootFromTemplate || b.MemoryPath != "" || b.DevicesStatePath != "" {
34+
return nil, fmt.Errorf("Error boot config for template")
35+
}
36+
b.MemoryPath = statePath + "/memory"
37+
b.DevicesStatePath = statePath + "/state"
38+
39+
config := &TemplateVmConfig{
40+
StatePath: statePath,
41+
Driver: hypervisor.HDriver.Name(),
42+
Config: b,
43+
}
44+
config.Config.BootFromTemplate = true
45+
3646
defer func() {
3747
if err != nil {
38-
(&TemplateVmConfig{StatePath: statePath}).Destroy()
48+
config.Destroy()
3949
}
4050
}()
4151

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

6070
// launch vm
61-
b := &hypervisor.BootConfig{
62-
CPU: cpu,
63-
Memory: mem,
64-
BootToBeTemplate: true,
65-
BootFromTemplate: false,
66-
EnableVsock: vsock,
67-
MemoryPath: statePath + "/memory",
68-
DevicesStatePath: statePath + "/state",
69-
Kernel: kernel,
70-
Initrd: initrd,
71-
}
72-
vm, err := hypervisor.GetVm(vmName, b, true)
71+
b.BootToBeTemplate = true
72+
vm, err := hypervisor.GetVm(vmName, &b, true)
7373
if err != nil {
7474
return nil, err
7575
}
@@ -89,15 +89,6 @@ func CreateTemplateVM(statePath, vmName string, cpu, mem int, kernel, initrd str
8989
// so we wait here. We should fix it in the qemu driver side.
9090
time.Sleep(1 * time.Second)
9191

92-
config := &TemplateVmConfig{
93-
StatePath: statePath,
94-
Driver: hypervisor.HDriver.Name(),
95-
Cpu: cpu,
96-
Memory: mem,
97-
Kernel: kernel,
98-
Initrd: initrd,
99-
}
100-
10192
configData, err := json.MarshalIndent(config, "", "\t")
10293
if err != nil {
10394
glog.V(1).Infof("%s\n", err.Error())
@@ -114,16 +105,8 @@ func CreateTemplateVM(statePath, vmName string, cpu, mem int, kernel, initrd str
114105
}
115106

116107
func (t *TemplateVmConfig) BootConfigFromTemplate() *hypervisor.BootConfig {
117-
return &hypervisor.BootConfig{
118-
CPU: t.Cpu,
119-
Memory: t.Memory,
120-
BootToBeTemplate: false,
121-
BootFromTemplate: true,
122-
MemoryPath: t.StatePath + "/memory",
123-
DevicesStatePath: t.StatePath + "/state",
124-
Kernel: t.Kernel,
125-
Initrd: t.Initrd,
126-
}
108+
b := t.Config
109+
return &b
127110
}
128111

129112
// boot vm from template, the returned vm is paused

0 commit comments

Comments
 (0)