-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
209 lines (175 loc) · 3.86 KB
/
config.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path"
"strconv"
)
type Config struct {
VM map[string]VmConfig
LogPath string
Pidfile PidConfig
}
type PidConfig struct {
Path string
Prefix string
Suffix string
}
func (pid PidConfig) getPath(name string) string {
return path.Join(pid.Path, pid.Prefix+name+pid.Suffix)
}
func NewConfig() *Config {
return &Config{
Pidfile: PidConfig{
Path: "/tmp",
Prefix: "vm.",
Suffix: ".pid",
},
}
}
func (config Config) getConfigArgs(name string) []string {
vmconfig, ok := config.VM[name]
if !ok {
L.ERR("config not exist!")
os.Exit(-1)
}
args := vmconfig.makeArgs()
args = append(args, "-pidfile", config.Pidfile.getPath(name))
return args
}
type VmConfig struct {
CPU CPUConfig
Memory string
Network NetworkConfig
LogFile string
Disks []DiskConfig
CdRoms []CDRomConfig
VGA string
VNC string
Others []string
}
func (vm VmConfig) makeArgs() []string {
var args []string
args = append(args, "-daemonize")
args = append(args, vm.CPU.makeArgs()...)
if vm.Memory == "" {
L.ERR("Memory must be exist")
os.Exit(-2)
}
args = append(args, "-m", vm.Memory)
for _, disk := range vm.Disks {
args = append(args, disk.makeArgs()...)
}
for _, cdrom := range vm.CdRoms {
args = append(args, cdrom.makeArgs()...)
}
if vm.Network.Ifname != "" || vm.Network.MAC != "" {
args = append(args, vm.Network.makeArgs()...)
} else {
L.WARN("No network configuration. you connot use network in VM")
}
if vm.VGA != "" {
args = append(args, "-vga", vm.VGA)
}
if vm.VNC != "" {
args = append(args, "-vnc", vm.VNC)
}
args = append(args, vm.Others...)
return args
}
type CPUConfig struct {
Type string
Core int
Sockets int
VirtualCore int
Threads int
}
func (cc CPUConfig) makeArgs() (args []string) {
L.DEBUG("make CPU arguments")
if cc.Type != "" {
args = append(args, "-cpu", cc.Type)
}
if cc.Core <= 0 {
L.ERR("CPU must be >= 1")
os.Exit(-2)
}
var smp string
smp += strconv.Itoa(cc.Core)
if cc.Sockets != 0 {
smp += ",sockets=" + strconv.Itoa(cc.Sockets)
}
if cc.VirtualCore != 0 {
smp += ",cores=" + strconv.Itoa(cc.VirtualCore)
}
if cc.Threads != 0 {
smp += ",threads=" + strconv.Itoa(cc.Threads)
}
args = append(args, "-smp", smp)
L.DEBUG("CPU options", args)
return
}
type NetworkConfig struct {
MAC string
Ifname string
}
func (nc NetworkConfig) makeArgs() (args []string) {
L.DEBUG("make Network arguments")
mac := "nic,macaddr=" + nc.MAC
ifname := "tap,ifname=" + nc.Ifname
ifname += ",script=/etc/kvm-ifup,downscript=/etc/kvm-ifdown"
args = append(args, "-net", mac, "-net", ifname)
L.DEBUG("Network options", args)
return
}
type DiskConfig struct {
Path string
Interface string
Index int
}
func (dc DiskConfig) makeArgs() (args []string) {
L.DEBUG("make Disk arguments")
var drive string
if dc.Path == "" {
L.WARN("Disk must have path - skip disk")
return
}
drive += "file=" + dc.Path
if dc.Interface != "" {
drive += ",if=" + dc.Interface
}
if dc.Index != 0 {
drive += ",index=" + strconv.Itoa(dc.Index)
}
drive += ",media=disk"
args = append(args, "-drive", drive)
L.DEBUG("Disk options", args)
return
}
type CDRomConfig string
func (cdrom CDRomConfig) makeArgs() (args []string) {
L.DEBUG("make CDRom arguments")
var drive string
drive += "file=" + string(cdrom)
drive += ",media=cdrom"
drive += ",if=ide"
args = append(args, "-drive", drive)
L.DEBUG("CDRom options", args)
return
}
func readConfig(filename string) *Config {
L.DEBUG("read file :", filename)
buf, err := ioutil.ReadFile(filename)
if err != nil {
L.ERR("Connot read config file :", filename, err)
os.Exit(-100)
}
config := NewConfig()
L.DEBUG("try to parse yaml")
err = yaml.Unmarshal(buf, config)
if err != nil {
L.ERR("Connot parse config file :", err)
os.Exit(-100)
}
return config
}