/
devtools.go
226 lines (199 loc) · 6.14 KB
/
devtools.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
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package esxi
import (
"fmt"
"reflect"
"github.com/vmware/govmomi/vim25/types"
"yunion.io/x/pkg/errors"
)
func NewDiskDev(sizeMb int64, templatePath string, uuid string, index int32, keyBase int32, controlKey int32, key int32) *types.VirtualDisk {
device := types.VirtualDisk{}
var backFile *types.VirtualDiskFlatVer2BackingInfo
if len(templatePath) > 0 {
backFile = &types.VirtualDiskFlatVer2BackingInfo{}
backFile.FileName = templatePath
}
diskFile := types.VirtualDiskFlatVer2BackingInfo{}
diskFile.DiskMode = "persistent"
thinProvisioned := true
diskFile.ThinProvisioned = &thinProvisioned
diskFile.Uuid = uuid
if backFile != nil {
diskFile.Parent = backFile
}
device.Backing = &diskFile
if sizeMb > 0 {
device.CapacityInKB = sizeMb * 1024
}
device.ControllerKey = controlKey
if key != 0 {
device.Key = key
} else {
device.Key = keyBase + index
}
device.UnitNumber = &index
return &device
}
func addDevSpec(device types.BaseVirtualDevice) *types.VirtualDeviceConfigSpec {
spec := types.VirtualDeviceConfigSpec{}
spec.Operation = types.VirtualDeviceConfigSpecOperationAdd
spec.Device = device
return &spec
}
func NewSCSIDev(key, ctlKey int32, driver string) types.BaseVirtualDevice {
desc := types.Description{Label: "SCSI controller 0", Summary: "VMware virtual SCSI"}
if driver == "pvscsi" {
device := types.ParaVirtualSCSIController{}
device.DeviceInfo = &desc
device.Key = key
device.ControllerKey = ctlKey
device.SharedBus = "noSharing"
return &device
}
device := types.VirtualLsiLogicController{}
device.DeviceInfo = &desc
device.Key = key
device.ControllerKey = ctlKey
device.SharedBus = "noSharing"
return &device
}
func NewAHCIDev(key, ctlKey int32) types.BaseVirtualDevice {
device := types.VirtualAHCIController{}
device.DeviceInfo = &types.Description{Label: "SATA controller 0", Summary: "AHCI"}
device.ControllerKey = ctlKey
device.Key = key
return &device
}
func NewSVGADev(key, ctlKey int32) types.BaseVirtualDevice {
device := types.VirtualMachineVideoCard{}
device.DeviceInfo = &types.Description{Label: "Video card", Summary: "Video card"}
device.ControllerKey = ctlKey
device.Key = key
device.VideoRamSizeInKB = 16 * 1024
return &device
}
func NewIDEDev(key, index int32) types.BaseVirtualDevice {
device := types.VirtualIDEController{}
s := fmt.Sprintf("IDE %d", index)
device.DeviceInfo = &types.Description{Label: s, Summary: s}
device.Key = key + index
device.BusNumber = index
return &device
}
func NewCDROMDev(path string, key, ctlKey int32) types.BaseVirtualDevice {
device := types.VirtualCdrom{}
device.DeviceInfo = &types.Description{Label: "CD/DVD drive 1", Summary: "Local ISO Emulated CD-ROM"}
device.ControllerKey = ctlKey
device.Key = key
connectable := types.VirtualDeviceConnectInfo{AllowGuestControl: true, Status: "untried"}
if len(path) != 0 {
device.Backing = &types.VirtualCdromIsoBackingInfo{
VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{
FileName: path,
},
}
connectable.StartConnected = true
} else {
device.Backing = &types.VirtualCdromRemoteAtapiBackingInfo{}
connectable.StartConnected = false
}
device.Connectable = &connectable
return &device
}
func NewUSBController(key *int32) types.BaseVirtualDevice {
device := types.VirtualUSBController{}
device.DeviceInfo = &types.Description{
Label: "USB controller",
}
if key != nil {
device.Key = *key
}
return &device
}
func NewVNICDev(host *SHost, mac, driver string, vlanId int32, key, ctlKey, index int32) (types.BaseVirtualDevice, error) {
desc := types.Description{Label: fmt.Sprintf("Network adapter %d", index+1), Summary: "VM Network"}
inet, err := host.FindNetworkByVlanID(vlanId)
if err != nil {
return nil, errors.Wrap(err, "SHost.FindNetworkByVlanID")
}
if inet == nil || reflect.ValueOf(inet).IsNil() {
return nil, errors.Error(fmt.Sprintf("VLAN %d not found", vlanId))
}
var (
False = false
True = true
)
var backing types.BaseVirtualDeviceBackingInfo
switch inet.(type) {
case *SDistributedVirtualPortgroup:
net := inet.(*SDistributedVirtualPortgroup)
port, err := net.FindPort()
if err != nil {
return nil, errors.Wrap(err, "net.FindPort")
}
if port == nil {
return nil, errors.Error("no valid port on DVS, exhausted")
}
portCon := types.DistributedVirtualSwitchPortConnection{
PortgroupKey: port.PortgroupKey,
SwitchUuid: port.DvsUuid,
PortKey: port.Key,
}
backing = &types.VirtualEthernetCardDistributedVirtualPortBackingInfo{Port: portCon}
case *SNetwork:
monet := inet.(*SNetwork).getMONetwork()
backing = &types.VirtualEthernetCardNetworkBackingInfo{
VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{
DeviceName: monet.Name,
UseAutoDetect: &False,
},
Network: &monet.Self,
}
default:
return nil, errors.Error(fmt.Sprintf("Unsuppport network type %s", reflect.TypeOf(inet).Name()))
}
connectable := types.VirtualDeviceConnectInfo{
StartConnected: true,
AllowGuestControl: true,
Connected: false,
Status: "untried",
}
nic := types.VirtualEthernetCard{
VirtualDevice: types.VirtualDevice{
DeviceInfo: &desc,
Backing: backing,
},
WakeOnLanEnabled: &True,
}
nic.Connectable = &connectable
nic.ControllerKey = ctlKey
nic.Key = key + index
if len(mac) != 0 {
nic.AddressType = "Manual"
nic.MacAddress = mac
} else {
nic.AddressType = "Generated"
}
if driver == "e1000" {
return &types.VirtualE1000{
VirtualEthernetCard: nic,
}, nil
}
return &types.VirtualVmxnet3{
VirtualVmxnet: types.VirtualVmxnet{
VirtualEthernetCard: nic,
},
}, nil
}