-
Notifications
You must be signed in to change notification settings - Fork 14
/
vm.go
98 lines (83 loc) · 2.11 KB
/
vm.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
package pkg
import (
"fmt"
"net"
)
//go:generate zbusc -module vmd -version 0.0.1 -name manager -package stubs github.com/threefoldtech/zos/pkg+VMModule stubs/vmd_stub.go
// VMNetworkInfo structure
type VMNetworkInfo struct {
// Tap device name
Tap string
// Mac address of the device
MAC string
// Address of the device in the form of cidr
AddressCIDR net.IPNet
// Gateway gateway address
GatewayIP net.IP
// Nameservers dns servers
Nameservers []net.IP
}
// VMDisk specifies vm disk params
type VMDisk struct {
// Size is disk size in Mib
Path string
ReadOnly bool
Root bool
}
// VM config structure
type VM struct {
// virtual machine name, or ID
Name string
// CPU is number of cores assigned to the VM
CPU uint8
// Memory size in Mib
Memory int64
// Network is network info
Network VMNetworkInfo
// KernelImage path to uncompressed linux kernel ELF
KernelImage string
// InitrdImage (optiona) path to initrd disk
InitrdImage string
// KernelArgs to override the default kernel arguments. (default: "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules")
KernelArgs string
// Disks are a list of disks that are going to
// be auto allocated on the provided storage path
Disks []VMDisk
}
// Validate vm data
func (vm *VM) Validate() error {
missing := func(s string) bool {
return len(s) == 0
}
if missing(vm.Name) {
return fmt.Errorf("name is required")
}
if missing(vm.KernelImage) {
return fmt.Errorf("kernel-image is required")
}
if vm.Memory < 512 {
return fmt.Errorf("invalid memory must not be less than 512M")
}
if vm.CPU == 0 || vm.CPU > 32 {
return fmt.Errorf("invalid cpu must be between 1 and 32")
}
return nil
}
// VMInfo returned by the inspect method
type VMInfo struct {
// Flag for enabling/disabling Hyperthreading
// Required: true
HtEnabled bool
// Memory size of VM
// Required: true
Memory int64
// Number of vCPUs (either 1 or an even number)
CPU int64
}
// VMModule defines the virtual machine module interface
type VMModule interface {
Run(vm VM) error
Inspect(name string) (VMInfo, error)
Delete(name string) error
Exists(id string) bool
}