/
vm.go
846 lines (679 loc) · 18.8 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
// Copyright (2012) Sandia Corporation.
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
package main
import (
"bridge"
"encoding/gob"
"errors"
"fmt"
"io"
"io/ioutil"
log "minilog"
"os"
"path/filepath"
"ron"
"strconv"
"strings"
"sync"
"time"
"vlans"
)
var vmID *Counter // channel of new VM IDs, shared across namespaces
type VMType int
const (
_ VMType = iota
KVM
CONTAINER
)
type VM interface {
GetID() int // GetID returns the VM's per-host unique ID
GetPID() int // GetPID returns the VM's PID
GetName() string // GetName returns the VM's per-host unique name
GetNamespace() string // GetNamespace returns the VM's namespace name
GetHost() string // GetHost returns the hostname that the VM is running on
GetState() VMState
GetLaunchTime() time.Time // GetLaunchTime returns the time when the VM was launched
GetType() VMType
GetInstancePath() string
GetUUID() string
GetCPUs() uint64
GetMem() uint64
GetCoschedule() int
GetNetwork(i int) (NetConfig, error) // GetNetwork returns the ith NetConfigs associated with the vm.
GetNetworks() []NetConfig // GetNetworks returns an ordered, deep copy of the NetConfigs associated with the vm.
// Lifecycle functions
Launch() error
Kill() error
Start() error
Stop() error
Flush() error
String() string
Info(string) (string, error)
Screenshot(int) ([]byte, error)
Tag(string) string // Tag gets the value of the given tag
SetTag(string, string) // SetTag updates the given tag
GetTags() map[string]string // GetTags returns a copy of the tags
ClearTag(string) // ClearTag deletes one or all tags
// Conflicts checks whether the VMs have conflicting configs. Called
// when we create a VM but before adding it to the list of VMs.
Conflicts(VM) error
SetCCActive(bool)
HasCC() bool
Connect(*ron.Server, bool) error
Disconnect(*ron.Server) error
UpdateNetworks()
// NetworkConnect updates the VM's config to reflect that it has been
// connected to the specified VLAN and Bridge.
NetworkConnect(int, int, string) error
// NetworkDisconnect updates the VM's config to reflect that the specified
// tap has been disconnected.
NetworkDisconnect(int) error
// Qos functions
GetQos() [][]bridge.QosOption
UpdateQos(uint, bridge.QosOption) error
ClearQos(uint) error
ClearAllQos() error
ProcStats() (map[int]*ProcStats, error)
// WriteConfig writes the VM's config to the provided writer.
WriteConfig(io.Writer) error
// Make a deep copy that shouldn't be used for anything but reads
Copy() VM
}
// BaseVM provides the bare-bones for base VM functionality. It implements
// several functions from the VM interface that are relatively common. All
// newly created VM types will most likely embed this struct to reuse the base
// functionality.
type BaseVM struct {
BaseConfig // embed
ID int
Name string
Namespace string
Host string // hostname where this VM is running
State VMState
LaunchTime time.Time
Type VMType
ActiveCC bool // set when CC is active
Pid int
lock sync.Mutex // synchronizes changes to this VM
cond *sync.Cond
kill chan bool // channel to signal the vm to shut down
instancePath string
}
// Valid names for output masks for `vm info`, in preferred output order
var vmInfo = []string{
// generic fields
"id", "name", "state", "uptime", "type", "uuid", "cc_active", "pid",
// network fields
"vlan", "bridge", "tap", "mac", "ip", "ip6", "qos",
// more generic fields but want next to vcpus
"memory",
// kvm fields
"vcpus", "disks", "snapshot", "initrd", "kernel", "cdrom", "migrate",
"append", "serial-ports", "virtio-ports", "vnc_port",
// container fields
"filesystem", "hostname", "init", "preinit", "fifo", "volume",
"console_port",
// more generic fields (tags can be huge so throw it at the end)
"tags",
}
// Valid names for output masks for `vm summary`, in preferred output order
var vmInfoLite = []string{
// generic fields
"id", "name", "state", "type", "uuid", "cc_active",
// network fields
"vlan",
}
func init() {
vmID = NewCounter()
// for serializing VMs
gob.Register([]VM{})
gob.Register(&KvmVM{})
gob.Register(&ContainerVM{})
}
func NewVM(name, namespace string, vmType VMType, config VMConfig) (VM, error) {
switch vmType {
case KVM:
return NewKVM(name, namespace, config)
case CONTAINER:
return NewContainer(name, namespace, config)
}
return nil, errors.New("unknown VM type")
}
// NewBaseVM creates a new VM, copying the specified configs. After a VM is
// created, it can be Launched.
func NewBaseVM(name, namespace string, config VMConfig) *BaseVM {
vm := new(BaseVM)
vm.BaseConfig = config.BaseConfig.Copy() // deep-copy configured fields
vm.ID = vmID.Next()
if name == "" {
vm.Name = fmt.Sprintf("vm-%d", vm.ID)
} else {
vm.Name = name
}
vm.Namespace = namespace
vm.Host = hostname
// generate a UUID if we don't have one
if vm.UUID == "" {
vm.UUID = generateUUID()
}
// Initialize tags, if not already
if vm.Tags == nil {
vm.Tags = map[string]string{}
}
// generate MAC addresses if any are unassigned. Don't bother checking
// for collisions -- based on the birthday paradox, there's only a
// 0.016% chance of collisions when running 10K VMs (one interface/VM).
for i := range vm.Networks {
if vm.Networks[i].MAC == "" {
vm.Networks[i].MAC = randomMac()
}
}
vm.kill = make(chan bool)
vm.instancePath = filepath.Join(*f_base, strconv.Itoa(vm.ID))
vm.State = VM_BUILDING
vm.LaunchTime = time.Now()
vm.cond = &sync.Cond{L: &vm.lock}
// New VMs are returned pre-locked. This ensures that the first operation
// called on a new VM is Launch.
vm.lock.Lock()
return vm
}
// copy a BaseVM... assume that lock is held.
func (vm *BaseVM) copy() *BaseVM {
vm2 := new(BaseVM)
// Make copies of all fields except lock/kill
vm2.BaseConfig = vm.BaseConfig.Copy()
vm2.ID = vm.ID
vm2.Name = vm.Name
vm2.Namespace = vm.Namespace
vm2.Host = vm.Host
vm2.State = vm.State
vm2.LaunchTime = vm.LaunchTime
vm2.Type = vm.Type
vm2.ActiveCC = vm.ActiveCC
vm2.instancePath = vm.instancePath
vm2.Pid = vm.Pid
return vm2
}
func (s VMType) String() string {
switch s {
case KVM:
return "kvm"
case CONTAINER:
return "container"
default:
return "???"
}
}
func ParseVMType(s string) (VMType, error) {
switch s {
case "kvm":
return KVM, nil
case "container":
return CONTAINER, nil
default:
return 0, errors.New("invalid VMType")
}
}
// findVMType tries to find a key that parses to a valid VMType. Useful for
// hunting through a command's BoolArgs.
func findVMType(args map[string]bool) (VMType, error) {
for k := range args {
if res, err := ParseVMType(k); err == nil {
return res, nil
}
}
return 0, errors.New("invalid VMType")
}
func (vm *BaseVM) GetID() int {
return vm.ID
}
func (vm *BaseVM) GetName() string {
return vm.Name
}
func (vm *BaseVM) GetNamespace() string {
return vm.Namespace
}
func (vm *BaseVM) GetNetwork(i int) (NetConfig, error) {
vm.lock.Lock()
defer vm.lock.Unlock()
if len(vm.Networks) <= i {
return NetConfig{}, fmt.Errorf("no such interface %v for %v", i, vm.Name)
}
return vm.Networks[i], nil
}
func (vm *BaseVM) GetNetworks() []NetConfig {
vm.lock.Lock()
defer vm.lock.Unlock()
// Make a deep copy of the NetConfigs
n := make([]NetConfig, len(vm.Networks))
copy(n, vm.Networks)
return n
}
func (vm *BaseVM) GetHost() string {
return vm.Host
}
func (vm *BaseVM) GetUUID() string {
return vm.UUID
}
func (vm *BaseVM) GetState() VMState {
vm.lock.Lock()
defer vm.lock.Unlock()
return vm.State
}
func (vm *BaseVM) GetLaunchTime() time.Time {
vm.lock.Lock()
defer vm.lock.Unlock()
return vm.LaunchTime
}
func (vm *BaseVM) GetType() VMType {
return vm.Type
}
func (vm *BaseVM) GetInstancePath() string {
return vm.instancePath
}
func (vm *BaseVM) GetCPUs() uint64 {
return vm.VCPUs
}
func (vm *BaseVM) GetMem() uint64 {
return vm.Memory
}
func (vm *BaseVM) GetCoschedule() int {
return int(vm.Coschedule)
}
func (vm *BaseVM) GetPID() int {
return vm.Pid
}
// Kill a VM. Blocks until the VM process has terminated.
func (vm *BaseVM) Kill() error {
vm.lock.Lock()
defer vm.lock.Unlock()
if vm.State&VM_KILLABLE == 0 {
return fmt.Errorf("invalid VM state to kill: %d %v", vm.ID, vm.State)
}
// Close the channel to signal to all dependent goroutines that they should
// stop. Anyone blocking on the channel will unblock immediately.
// http://golang.org/ref/spec#Receive_operator
close(vm.kill)
// wait until the VM is in an unkillable state (it must have been killed)
for vm.State&VM_KILLABLE != 0 {
vm.cond.Wait()
}
return nil
}
func (vm *BaseVM) Flush() error {
namespacesDir := filepath.Join(*f_base, "namespaces")
namespaceAliasDir := filepath.Join(namespacesDir, vm.Namespace)
vmAlias := filepath.Join(namespaceAliasDir, vm.UUID)
// remove just the symlink to the instance path
if err := os.Remove(vmAlias); err != nil {
return err
}
// try removing the <namespace> directory, but let it fail if not empty
os.Remove(namespaceAliasDir)
// try removing the namespaces/ directory, but let it fail if not empty
os.Remove(namespacesDir)
// remove the actual instance path
if err := os.RemoveAll(vm.instancePath); err != nil {
return err
}
return nil
}
func (vm *BaseVM) Tag(t string) string {
vm.lock.Lock()
defer vm.lock.Unlock()
return vm.Tags[t]
}
func (vm *BaseVM) SetTag(t, v string) {
vm.lock.Lock()
defer vm.lock.Unlock()
vm.Tags[t] = v
}
func (vm *BaseVM) GetTags() map[string]string {
vm.lock.Lock()
defer vm.lock.Unlock()
res := map[string]string{}
for k, v := range vm.Tags {
res[k] = v
}
return res
}
func (vm *BaseVM) ClearTag(t string) {
vm.lock.Lock()
defer vm.lock.Unlock()
if t == Wildcard {
vm.Tags = make(map[string]string)
} else {
delete(vm.Tags, t)
}
}
func (vm *BaseVM) UpdateNetworks() {
vm.lock.Lock()
defer vm.lock.Unlock()
for i := range vm.Networks {
n := &vm.Networks[i]
tap, err := bridges.FindTap(n.Tap)
if err != nil {
// weird...
n.RxRate, n.TxRate = 0, 0
continue
}
n.RxRate, n.TxRate = tap.BandwidthStats()
n.IP4 = tap.IP4
n.IP6 = tap.IP6
}
}
func (vm *BaseVM) UpdateQos(tap uint, op bridge.QosOption) error {
vm.lock.Lock()
defer vm.lock.Unlock()
if tap >= uint(len(vm.Networks)) {
return fmt.Errorf("invalid tap index specified: %d", tap)
}
bName := vm.Networks[tap].Bridge
tapName := vm.Networks[tap].Tap
br, err := getBridge(bName)
if err != nil {
return err
}
return br.UpdateQos(tapName, op)
}
func (vm *BaseVM) ClearAllQos() error {
vm.lock.Lock()
defer vm.lock.Unlock()
for _, nc := range vm.Networks {
b, err := getBridge(nc.Bridge)
if err != nil {
log.Error("failed to get bridge %s for vm %s", nc.Bridge, vm.GetName())
return err
}
err = b.RemoveQos(nc.Tap)
if err != nil {
log.Error("failed to remove qos from vm %s", vm.GetName())
return err
}
}
return nil
}
func (vm *BaseVM) ClearQos(tap uint) error {
vm.lock.Lock()
defer vm.lock.Unlock()
if tap >= uint(len(vm.Networks)) {
return fmt.Errorf("invalid tap index specified: %d", tap)
}
nc := vm.Networks[tap]
b, err := getBridge(nc.Bridge)
if err != nil {
return err
}
return b.RemoveQos(nc.Tap)
}
func (vm *BaseVM) GetQos() [][]bridge.QosOption {
vm.lock.Lock()
defer vm.lock.Unlock()
var res [][]bridge.QosOption
for _, nc := range vm.Networks {
b, err := getBridge(nc.Bridge)
if err != nil {
log.Error("failed to get bridge %s for vm %s", nc.Bridge, vm.GetName())
continue
}
q := b.GetQos(nc.Tap)
if q != nil {
res = append(res, q)
}
}
return res
}
func (vm *BaseVM) SetCCActive(active bool) {
vm.lock.Lock()
defer vm.lock.Unlock()
vm.ActiveCC = active
}
func (vm *BaseVM) HasCC() bool {
vm.lock.Lock()
defer vm.lock.Unlock()
return vm.ActiveCC
}
func (vm *BaseVM) NetworkConnect(pos, vlan int, bridge string) error {
vm.lock.Lock()
defer vm.lock.Unlock()
return vm.networkConnect(pos, vlan, bridge)
}
// networkConnect assumes that the VM lock is held.
func (vm *BaseVM) networkConnect(pos, vlan int, bridge string) error {
if len(vm.Networks) <= pos {
return fmt.Errorf("no network %v, VM only has %v networks", pos, len(vm.Networks))
}
nic := &vm.Networks[pos]
// special case -- if bridge is not specified, reconnect tap to the same
// bridge if it is already on a bridge.
if bridge == "" {
bridge = nic.Bridge
}
// fallback -- connect to the default bridge.
if bridge == "" {
bridge = DefaultBridge
}
log.Info("moving network connection: %v %v %v:%v -> %v:%v", vm.ID, pos, nic.Bridge, nic.VLAN, bridge, vlan)
// Do this before disconnecting from the old bridge in case the new one was
// mistyped or invalid.
dst, err := getBridge(bridge)
if err != nil {
return err
}
// Disconnect from the old bridge, if we were connected
if nic.VLAN != DisconnectedVLAN {
src, err := getBridge(nic.Bridge)
if err != nil {
return err
}
if err := src.RemoveTap(nic.Tap); err != nil {
return err
}
}
// Connect to the new bridge
if err := dst.AddTap(nic.Tap, nic.MAC, vlan, false); err != nil {
return err
}
// Record updates to the VM config
nic.Alias = ""
if alias, err := vlans.GetAlias(vlan); err == nil {
if alias.Namespace != vm.Namespace {
nic.Alias = alias.String()
} else {
nic.Alias = alias.Value
}
}
nic.VLAN = vlan
nic.Bridge = bridge
// TODO: what to do with nic.Raw?
return nil
}
func (vm *BaseVM) NetworkDisconnect(pos int) error {
vm.lock.Lock()
defer vm.lock.Unlock()
return vm.networkDisconnect(pos)
}
// networkDisconnect assumes that the VM lock is held.
func (vm *BaseVM) networkDisconnect(pos int) error {
if len(vm.Networks) <= pos {
return fmt.Errorf("no network %v, VM only has %v networks", pos, len(vm.Networks))
}
nic := &vm.Networks[pos]
// Don't try to diconnect an interface that is already disconnected...
if nic.VLAN == DisconnectedVLAN {
return nil
}
log.Debug("disconnect network connection: %v %v %v", vm.ID, pos, nic)
br, err := getBridge(nic.Bridge)
if err != nil {
return err
}
if err := br.RemoveTap(nic.Tap); err != nil {
return err
}
nic.Alias = ""
nic.Bridge = ""
nic.VLAN = DisconnectedVLAN
// TODO: what to do with nic.Raw?
return nil
}
// info returns information about the VM for the provided field.
func (vm *BaseVM) Info(field string) (string, error) {
vm.lock.Lock()
defer vm.lock.Unlock()
var vals []string
switch field {
case "id":
return strconv.Itoa(vm.ID), nil
case "pid":
return strconv.Itoa(vm.Pid), nil
case "name":
return vm.Name, nil
case "state":
return vm.State.String(), nil
case "uptime":
return time.Since(vm.LaunchTime).String(), nil
case "type":
return vm.Type.String(), nil
case "vlan":
for _, net := range vm.Networks {
if net.VLAN == DisconnectedVLAN {
vals = append(vals, "disconnected")
} else {
vals = append(vals, printVLAN(vm.Namespace, net.VLAN))
}
}
case "bridge":
for _, v := range vm.Networks {
vals = append(vals, v.Bridge)
}
case "tap":
for _, v := range vm.Networks {
vals = append(vals, v.Tap)
}
case "mac":
for _, v := range vm.Networks {
vals = append(vals, v.MAC)
}
case "ip":
for _, v := range vm.Networks {
vals = append(vals, v.IP4)
}
case "ip6":
for _, v := range vm.Networks {
vals = append(vals, v.IP6)
}
case "qos":
for idx, v := range vm.Networks {
s := vm.QosString(v.Bridge, v.Tap, strconv.Itoa(idx))
if s != "" {
vals = append(vals, s)
}
}
case "tags":
return marshal(vm.Tags), nil
case "cc_active":
return strconv.FormatBool(vm.ActiveCC), nil
default:
// at this point, hopefully field is part of BaseConfig
return vm.BaseConfig.Info(field)
}
return "[" + strings.Join(vals, ", ") + "]", nil
}
// setState updates the vm state, and write the state to file. Assumes that the
// caller has locked the vm.
func (vm *BaseVM) setState(s VMState) {
log.Debug("updating vm %v state: %v -> %v", vm.ID, vm.State, s)
vm.State = s
mustWrite(vm.path("state"), s.String())
}
// setErrorf logs the error, updates the vm state, and records the error in the
// vm's tags. Assumes that the caller has locked the vm. Returns the final
// error.
func (vm *BaseVM) setErrorf(format string, arg ...interface{}) error {
// create the error
err := fmt.Errorf(format, arg...)
log.Error("vm %v: %v", vm.ID, err)
vm.Tags["error"] = err.Error()
vm.setState(VM_ERROR)
return err
}
// writeTaps writes the vm's taps to disk in the vm's instance path.
func (vm *BaseVM) writeTaps() error {
taps := []string{}
for _, net := range vm.Networks {
taps = append(taps, net.Tap)
}
f := vm.path("taps")
if err := ioutil.WriteFile(f, []byte(strings.Join(taps, "\n")), 0666); err != nil {
return fmt.Errorf("write instance taps file: %v", err)
}
return nil
}
func (vm *BaseVM) conflicts(vm2 *BaseVM) error {
// Return error if two VMs have same name or UUID
if vm.Name == vm2.Name {
return fmt.Errorf("duplicate VM name: %s", vm.Name)
}
if vm.UUID == vm2.UUID {
return fmt.Errorf("duplicate VM UUID: %s", vm.UUID)
}
// Warn if we see two VMs that share a MAC on the same VLAN
for _, n := range vm.Networks {
for _, n2 := range vm2.Networks {
if n.MAC == n2.MAC && n.VLAN == n2.VLAN {
log.Warn("duplicate MAC/VLAN: %v/%v for %v and %v", n.MAC, n.VLAN, vm.ID, vm2.ID)
}
}
}
return nil
}
// path joins instancePath with provided path
func (vm *BaseVM) path(s string) string {
return filepath.Join(vm.instancePath, s)
}
func (vm *BaseVM) createInstancePathAlias() error {
// create the namespaces/<namespace> directory
namespaceAliasDir := filepath.Join(*f_base, "namespaces", vm.GetNamespace())
if err := os.MkdirAll(namespaceAliasDir, os.FileMode(0700)); err != nil {
return fmt.Errorf("unable to create namespace dir: %v", err)
}
// create a symlink under namespaces/<namespace> to the instance path
// only if it does not already exist, otherwise error
vmAlias := filepath.Join(namespaceAliasDir, vm.GetUUID())
if _, err := os.Stat(vmAlias); err == nil {
// symlink already exists
return fmt.Errorf("unable to create VM dir symlink: %v already exists", vmAlias)
}
if err := os.Symlink(vm.GetInstancePath(), vmAlias); err != nil {
return fmt.Errorf("unable to create VM dir symlink: %v", err)
}
return nil
}
func writeVMConfig(vm VM) error {
log.Info("writing vm config")
name := filepath.Join(vm.GetInstancePath(), "config")
f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer f.Close()
return vm.WriteConfig(f)
}
func vmNotFound(name string) error {
return fmt.Errorf("vm not found: %v", name)
}
func vmNotRunning(name string) error {
return fmt.Errorf("vm not running: %v", name)
}
func vmNotKVM(name string) error {
return fmt.Errorf("vm not KVM: %v", name)
}
func vmNotContainer(name string) error {
return fmt.Errorf("vm not container: %v", name)
}
func isVMNotFound(err string) bool {
return strings.HasPrefix(err, "vm not found: ")
}