-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathspec.go
More file actions
305 lines (244 loc) · 9.11 KB
/
Copy pathspec.go
File metadata and controls
305 lines (244 loc) · 9.11 KB
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
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 spec
import (
"context"
"net/url"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/lib/config/executor"
"github.com/vmware/vic/lib/constants"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/extraconfig/vmomi"
"github.com/vmware/vic/pkg/vsphere/session"
)
// VirtualMachineConfigSpecConfig holds the config values
type VirtualMachineConfigSpecConfig struct {
// ID of the VM
ID string
BiosUUID string
VMFullName string
// ParentImageID of the VM
ParentImageID string
// Name of the VM
Name string
// Number of CPUs
NumCPUs int32
// Memory - in MB
MemoryMB int64
// VMFork enabled
VMForkEnabled bool
// datastore path of the media file we boot from
BootMediaPath string
// datastore path of the VM
VMPathName string
// Name of the image store
ImageStoreName string
// url path to image store
ImageStorePath *url.URL
// Temporary
Metadata *executor.ExecutorConfig
}
// VirtualMachineConfigSpec type
type VirtualMachineConfigSpec struct {
*session.Session
*types.VirtualMachineConfigSpec
config *VirtualMachineConfigSpecConfig
// internal value to keep track of next ID
key int32
}
// NewVirtualMachineConfigSpec returns a VirtualMachineConfigSpec
func NewVirtualMachineConfigSpec(ctx context.Context, session *session.Session, config *VirtualMachineConfigSpecConfig) (*VirtualMachineConfigSpec, error) {
defer trace.End(trace.Begin(config.ID))
s := &types.VirtualMachineConfigSpec{
Name: config.VMFullName,
Uuid: config.BiosUUID,
Files: &types.VirtualMachineFileInfo{
VmPathName: config.VMPathName,
},
NumCPUs: config.NumCPUs,
CpuHotAddEnabled: &config.VMForkEnabled, // this disables vNUMA when true
MemoryMB: config.MemoryMB,
MemoryHotAddEnabled: &config.VMForkEnabled,
ExtraConfig: []types.BaseOptionValue{
// lets us see the UUID for the containerfs disk (hidden from daemon)
&types.OptionValue{Key: "disk.EnableUUID", Value: "true"},
// needed to avoid the questions that occur when attaching multiple disks with the same uuid (bugzilla 1362918)
&types.OptionValue{Key: "answer.msg.disk.duplicateUUID", Value: "Yes"},
// needed to avoid the question that occur when opening a file backed serial port
&types.OptionValue{Key: "answer.msg.serial.file.open", Value: "Append"},
&types.OptionValue{Key: "sched.mem.lpage.maxSharedPages", Value: "256"},
// seems to be needed to avoid children hanging shortly after fork
&types.OptionValue{Key: "vmotion.checkpointSVGAPrimarySize", Value: "4194304"},
// trying this out - if it works then we need to determine if we can rely on serial0 being the correct index.
&types.OptionValue{Key: "serial0.hardwareFlowControl", Value: "TRUE"},
// https://enatai-jira.eng.vmware.com/browse/BON-257
// Hotadd memory above 3 GB not working
&types.OptionValue{Key: "memory.noHotAddOver4GB", Value: "FALSE"},
&types.OptionValue{Key: "memory.maxGrow", Value: "512"},
// http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2030189
&types.OptionValue{Key: "tools.remindInstall", Value: "FALSE"},
&types.OptionValue{Key: "tools.upgrade.policy", Value: "manual"},
},
}
// encode the config as optionvalues
cfg := map[string]string{}
extraconfig.Encode(extraconfig.MapSink(cfg), config.Metadata)
metaCfg := vmomi.OptionValueFromMap(cfg, true)
// merge it with the sec
s.ExtraConfig = append(s.ExtraConfig, metaCfg...)
vmcs := &VirtualMachineConfigSpec{
Session: session,
VirtualMachineConfigSpec: s,
config: config,
}
return vmcs, nil
}
// AddVirtualDevice appends an Add operation to the DeviceChange list
func (s *VirtualMachineConfigSpec) AddVirtualDevice(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
s.DeviceChange = append(s.DeviceChange,
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationAdd,
Device: device,
},
)
return s
}
// AddAndCreateVirtualDevice appends an Add operation to the DeviceChange list
func (s *VirtualMachineConfigSpec) AddAndCreateVirtualDevice(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
s.DeviceChange = append(s.DeviceChange,
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationAdd,
FileOperation: types.VirtualDeviceConfigSpecFileOperationCreate,
Device: device,
},
)
return s
}
// RemoveVirtualDevice appends a Remove operation to the DeviceChange list
func (s *VirtualMachineConfigSpec) RemoveVirtualDevice(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
s.DeviceChange = append(s.DeviceChange,
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationRemove,
Device: device,
},
)
return s
}
// RemoveAndDestroyVirtualDevice appends a Remove operation to the DeviceChange list
func (s *VirtualMachineConfigSpec) RemoveAndDestroyVirtualDevice(device types.BaseVirtualDevice) *VirtualMachineConfigSpec {
s.DeviceChange = append(s.DeviceChange,
&types.VirtualDeviceConfigSpec{
Operation: types.VirtualDeviceConfigSpecOperationRemove,
FileOperation: types.VirtualDeviceConfigSpecFileOperationDestroy,
Device: device,
},
)
return s
}
// Name returns the name of the VM
func (s *VirtualMachineConfigSpec) Name() string {
defer trace.End(trace.Begin(s.config.Name))
return s.config.Name
}
// ID returns the ID of the VM
func (s *VirtualMachineConfigSpec) ID() string {
defer trace.End(trace.Begin(s.config.ID))
return s.config.ID
}
// ParentImageID returns the ID of the image that VM is based on
func (s *VirtualMachineConfigSpec) ParentImageID() string {
defer trace.End(trace.Begin(s.config.ParentImageID))
return s.config.ParentImageID
}
// BootMediaPath returns the image path
func (s *VirtualMachineConfigSpec) BootMediaPath() string {
defer trace.End(trace.Begin(s.config.ID))
return s.config.BootMediaPath
}
// VMPathName returns the VM folder path
func (s *VirtualMachineConfigSpec) VMPathName() string {
defer trace.End(trace.Begin(s.config.ID))
return s.config.VMPathName
}
// ImageStoreName returns the image store name
func (s *VirtualMachineConfigSpec) ImageStoreName() string {
defer trace.End(trace.Begin(s.config.ID))
return s.config.ImageStoreName
}
// ImageStorePath returns the image store url
func (s *VirtualMachineConfigSpec) ImageStorePath() *url.URL {
defer trace.End(trace.Begin(s.config.ID))
return s.config.ImageStorePath
}
func (s *VirtualMachineConfigSpec) generateNextKey() int32 {
s.key -= 10
return s.key
}
// Spec returns the base types.VirtualMachineConfigSpec object
func (s *VirtualMachineConfigSpec) Spec() *types.VirtualMachineConfigSpec {
return s.VirtualMachineConfigSpec
}
// VirtualDeviceSlotNumber returns the PCI slot number of a device
func VirtualDeviceSlotNumber(d types.BaseVirtualDevice) int32 {
s := d.GetVirtualDevice().SlotInfo
if s == nil {
return constants.NilSlot
}
if i, ok := s.(*types.VirtualDevicePciBusSlotInfo); ok {
return i.PciSlotNumber
}
return constants.NilSlot
}
func findSlotNumber(slots map[int32]bool) int32 {
// see https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2047927
slot := constants.PCISlotNumberBegin
for _, ok := slots[slot]; ok && slot != constants.PCISlotNumberEnd; {
slot += constants.PCISlotNumberInc
_, ok = slots[slot]
}
if slot == constants.PCISlotNumberEnd {
return constants.NilSlot
}
return slot
}
// AssignSlotNumber assigns a specific PCI slot number to the specified device. This ensures that
// the slot is valid and not in use by anything else in the spec
func (s *VirtualMachineConfigSpec) AssignSlotNumber(dev types.BaseVirtualDevice, known map[int32]bool) int32 {
slot := VirtualDeviceSlotNumber(dev)
if slot != constants.NilSlot {
return slot
}
// build the slots in use from the spec
slots := s.CollectSlotNumbers(known)
slot = findSlotNumber(slots)
if slot != constants.NilSlot {
dev.GetVirtualDevice().SlotInfo = &types.VirtualDevicePciBusSlotInfo{PciSlotNumber: slot}
}
return slot
}
// CollectSlotNumbers returns a collection of all the PCI slot numbers for devices in the spec
// Can take a nil map as argument
func (s *VirtualMachineConfigSpec) CollectSlotNumbers(known map[int32]bool) map[int32]bool {
if known == nil {
known = make(map[int32]bool)
}
// collect all the already assigned slot numbers
for _, c := range s.DeviceChange {
if s := VirtualDeviceSlotNumber(c.GetVirtualDeviceConfigSpec().Device); s != constants.NilSlot {
known[s] = true
}
}
return known
}