/
vbox_instance.go
296 lines (241 loc) · 7.02 KB
/
vbox_instance.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//go:build vbox || !onlyprovider
package vbox
import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"strconv"
"strings"
"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/wsl"
"github.com/olekukonko/tablewriter"
"github.com/terra-farm/go-virtualbox"
vbox "github.com/terra-farm/go-virtualbox"
)
var (
virtualBoxDir = path.Join(lepton.GetOpsHome(), "virtualbox")
vmsDir = path.Join(lepton.GetOpsHome(), "virtualbox", "vms")
)
// CreateInstance uses an image to launch a vm in VirtualBox
func (p *Provider) CreateInstance(ctx *lepton.Context) (err error) {
name := ctx.Config().RunConfig.InstanceName
vmPath, err := findOrCreateVmsDir()
if err != nil {
ctx.Logger().Errorf("failed converting vms path")
return
}
vmPath = path.Join(vmPath, name)
err = os.Mkdir(vmPath, 0755)
if err != nil {
ctx.Logger().Errorf("failed creating vm directory")
return
}
imagesDir, err := findOrCreateVdiImagesDir()
if err != nil {
ctx.Logger().Errorf("failed converting vdi images dir")
return
}
imageName := ctx.Config().CloudConfig.ImageName + ".vdi"
imagePath := path.Join(imagesDir, imageName)
vmImagePath := path.Join(vmPath, imageName)
if wsl.IsWSL() {
vmPath, err = wsl.ConvertPathFromWSLtoWindows(vmPath)
if err != nil {
ctx.Logger().Errorf("failed converting vm path")
return
}
imagePath, err = wsl.ConvertPathFromWSLtoWindows(imagePath)
if err != nil {
ctx.Logger().Errorf("failed converting image path %s", imagePath)
return
}
vmImagePath = vmPath + "\\" + imageName
}
err = vbox.CloneHD(imagePath, vmImagePath)
if err != nil {
ctx.Logger().Errorf("failed cloning hdd")
return
}
vm, err := vbox.CreateMachine(name, vmPath)
if err != nil {
ctx.Logger().Errorf("failed creating machine")
return
}
err = vm.AddStorageCtl("IDE", vbox.StorageController{
SysBus: vbox.SysBusIDE,
Ports: 2,
Chipset: vbox.CtrlPIIX3,
HostIOCache: true,
Bootable: true,
})
if err != nil {
ctx.Logger().Errorf("failed adding storage controller")
return
}
err = vm.AttachStorage("IDE", vbox.StorageMedium{
Port: uint(0),
Device: 0,
DriveType: vbox.DriveHDD,
Medium: vmImagePath,
})
if err != nil {
ctx.Logger().Errorf("failed attaching storage")
return
}
modifyVM := exec.Command("VBoxManage", "modifyvm", vm.Name, "--memory", "1024")
err = modifyVM.Run()
if err != nil {
ctx.Logger().Errorf("failed changing vm memory")
return
}
err = virtualbox.SetGuestProperty(vm.Name, "Image", imageName)
if err != nil {
ctx.Logger().Errorf("failed to set image as guest property")
return
}
err = vm.SetNIC(1, vbox.NIC{
Network: vbox.NICNetNAT,
Hardware: vbox.VirtIO,
})
if err != nil {
ctx.Logger().Errorf("failed setting NIC")
return
}
err = vm.Start()
if err != nil {
ctx.Logger().Errorf("failed started vm")
return
}
for _, p := range ctx.Config().RunConfig.Ports {
port, err := strconv.Atoi(p)
if err == nil {
err = vm.AddNATPF(1, fmt.Sprintf("TCP:%d", port), vbox.PFRule{
Proto: vbox.PFTCP,
HostPort: uint16(port),
GuestPort: uint16(port),
})
if err != nil {
ctx.Logger().Errorf("failed creating port forward rule")
return err
}
}
}
// TODO: change go-virtualbox to read forwarding rules on listing instances instead of relying on a guest property
if len(ctx.Config().RunConfig.Ports) != 0 {
err = virtualbox.SetGuestProperty(vm.Name, "Ports", strings.Join(ctx.Config().RunConfig.Ports, ","))
if err != nil {
ctx.Logger().Errorf("failed to set ports as guest property")
return
}
}
return
}
// ListInstances prints vms list managed by VirtualBox in table
func (p *Provider) ListInstances(ctx *lepton.Context) (err error) {
instances, err := p.GetInstances(ctx)
if err != nil {
return
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Name", "Status", "Public Ips", "Ports", "Image"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor})
table.SetRowLine(true)
for _, i := range instances {
var rows []string
rows = append(rows, i.ID)
rows = append(rows, i.Name)
rows = append(rows, i.Status)
rows = append(rows, strings.Join(i.PublicIps, ", "))
rows = append(rows, strings.Join(i.Ports, ", "))
rows = append(rows, i.Image)
table.Append(rows)
}
table.Render()
return
}
// GetInstances returns the list of vms managed by VirtualBox
func (p *Provider) GetInstances(ctx *lepton.Context) (instances []lepton.CloudInstance, err error) {
instances = []lepton.CloudInstance{}
vms, err := vbox.ListMachines()
if err != nil {
return
}
for _, vm := range vms {
image, _ := virtualbox.GetGuestProperty(vm.Name, "Image")
ports, _ := virtualbox.GetGuestProperty(vm.Name, "Ports")
instances = append(instances, lepton.CloudInstance{
ID: vm.UUID,
Name: vm.Name,
PublicIps: []string{"127.0.0.1"},
Status: string(vm.State),
Image: image,
Ports: []string{ports},
})
}
return
}
// DeleteInstance removes a vm
func (p *Provider) DeleteInstance(ctx *lepton.Context, instancename string) (err error) {
vm, err := vbox.GetMachine(instancename)
if err != nil {
return err
}
err = vm.Delete()
return
}
// StopInstance stops vm in VirtualBox
func (p *Provider) StopInstance(ctx *lepton.Context, instancename string) (err error) {
vm, err := vbox.GetMachine(instancename)
if err != nil {
return err
}
stopVMCmd := exec.Command("VBoxManage", "controlvm", vm.Name, "poweroff")
err = stopVMCmd.Run()
return
}
// RebootInstance reboots the instance.
func (p *Provider) RebootInstance(ctx *lepton.Context, instanceName string) error {
return fmt.Errorf("operation not supported")
}
// StartInstance starts a vm in VirtualBox
func (p *Provider) StartInstance(ctx *lepton.Context, instancename string) (err error) {
vm, err := vbox.GetMachine(instancename)
if err != nil {
return err
}
err = vm.Start()
return
}
// GetInstanceByName returns VirtualBox vm with given name
func (p *Provider) GetInstanceByName(ctx *lepton.Context, name string) (instance *lepton.CloudInstance, err error) {
return
}
// GetInstanceLogs is a stub
func (p *Provider) GetInstanceLogs(ctx *lepton.Context, instancename string) (string, error) {
return "", errors.New("Unsupported")
}
// PrintInstanceLogs prints vm log on console
func (p *Provider) PrintInstanceLogs(ctx *lepton.Context, instancename string, watch bool) error {
return errors.New("Unsupported")
}
func findOrCreateVmsDir() (string, error) {
if _, err := os.Stat(virtualBoxDir); os.IsNotExist(err) {
os.MkdirAll(virtualBoxDir, 0755)
} else if err != nil {
return "", err
}
if _, err := os.Stat(vmsDir); os.IsNotExist(err) {
os.MkdirAll(vmsDir, 0755)
} else if err != nil {
return "", err
}
return vmsDir, nil
}