-
Notifications
You must be signed in to change notification settings - Fork 8
/
vmx.go
108 lines (93 loc) · 2.8 KB
/
vmx.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package vmx
import (
"bytes"
"fmt"
"strings"
)
// Maximum values for a virtual machine
// From http://www.vmware.com/pdf/vsphere5/r55/vsphere-55-configuration-maximums.pdf
const (
MAX_VCPUS = 64
// Megabytes
MAX_MEMORY = 1000000 // 1tib
MAX_IDE_ADAPTERS = 2
MAX_IDE_DEVICES_PER_ADAPTER = 2
MAX_SATA_ADAPTERS = 4
MAX_SATA_DEVICES_PER_ADAPTER = 30
MAX_SCSI_ADAPTERS = 4
MAX_SCSI_DEVICES_PER_ADAPTER = 15
MAX_VDISKS = 60
// Megabytes
MAX_VDISK_SIZE = 6200000 // 62tib
MAX_FLOPPY_ADAPTERS = 1
MAX_FLOPPY_DEVICES = 2
MAX_VNICS = 10
MAX_USB_ADAPTERS = 1
MAX_USB_DEVICES = 20
MAX_PARALLEL_PORTS = 3
MAX_SERIAL_PORTS = 4
MAX_REMOTE_CONSOLE_CONNECTIONS = 40
// Megabytes
MAX_VIDEO_MEMORY = 512
)
func init() {
//log.SetFlags(log.Lshortfile)
}
// Marshal traverses the value v recursively.
// If an encountered value implements the Marshaler interface
// and is not a nil pointer, Marshal calls its MarshalVMX method
// to produce VMX. The nil pointer exception is not strictly necessary
// but mimics a similar, necessary exception in the behavior of
// UnmarshalVMX.
func Marshal(v interface{}) ([]byte, error) {
var b bytes.Buffer
if err := NewEncoder(&b).Encode(v); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// Takes VMX data and binds it to the Go value pointed by v
func Unmarshal(data []byte, v interface{}) error {
return NewDecoder(bytes.NewReader(data), false).Decode(v)
}
// Parses struct tag
func parseTag(tag string) (string, bool, bool, error) {
if tag == "" {
return "", false, false, nil
}
omitempty := false
omit := false
// Takes out first colon found
parts := strings.Split(tag, ":")
if len(parts) < 2 || parts[1] == "" {
return "", omitempty, omit, fmt.Errorf("Invalid tag: %s", tag)
}
if parts[1] == `""` {
return "", omitempty, omit, fmt.Errorf("Tag name is missing: %s", tag)
}
// Takes out double quotes
parts2 := strings.Split(parts[1], `"`)
if len(parts2) < 2 {
return "", omitempty, omit, fmt.Errorf("Tag name has to be enclosed in double quotes: %s", tag)
}
values := strings.Split(parts2[1], ",")
key := strings.TrimSpace(values[0])
if len(values) > 1 {
options := values[1:]
for _, option := range options {
option = strings.TrimSpace(option)
switch option {
case "omitempty":
omitempty = true
case "omit":
omit = true
default:
return key, omitempty, omit, fmt.Errorf("Unknown option: %s", option)
}
}
}
return key, omitempty, omit, nil
}