| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
import traceback |
| 10 |
from random import randint |
| 11 |
from ansible.module_utils.common.network import is_mac |
| 12 |
from ansible.module_utils.basic import missing_required_lib |
| 13 |
|
| 14 |
PYVMOMI_IMP_ERR = None |
| 15 |
try: |
| 16 |
from pyVmomi import vim |
| 17 |
HAS_PYVMOMI = True |
| 18 |
except ImportError: |
| 19 |
PYVMOMI_IMP_ERR = traceback.format_exc() |
| 20 |
HAS_PYVMOMI = False |
| 21 |
|
| 22 |
|
| 23 |
class PyVmomiDeviceHelper(object): |
| 24 |
""" This class is a helper to create easily VMware Objects for PyVmomiHelper """ |
| 25 |
|
| 26 |
def __init__(self, module): |
| 27 |
if not HAS_PYVMOMI: |
| 28 |
module.fail_json(msg=missing_required_lib('PyVmomi'), |
| 29 |
exception=PYVMOMI_IMP_ERR) |
| 30 |
|
| 31 |
self.module = module |
| 32 |
# This is not used for the multiple controller with multiple disks scenario, |
| 33 |
# disk unit number can not be None |
| 34 |
# self.next_disk_unit_number = 0 |
| 35 |
self.scsi_device_type = { |
| 36 |
'lsilogic': vim.vm.device.VirtualLsiLogicController, |
| 37 |
'paravirtual': vim.vm.device.ParaVirtualSCSIController, |
| 38 |
'buslogic': vim.vm.device.VirtualBusLogicController, |
| 39 |
'lsilogicsas': vim.vm.device.VirtualLsiLogicSASController |
| 40 |
} |
| 41 |
self.sata_device_type = vim.vm.device.VirtualAHCIController |
| 42 |
self.nvme_device_type = vim.vm.device.VirtualNVMEController |
| 43 |
self.ide_device_type = vim.vm.device.VirtualIDEController |
| 44 |
self.disk_ctl_device_type = self.scsi_device_type.copy() |
| 45 |
self.disk_ctl_device_type.update({ |
| 46 |
'sata': self.sata_device_type, |
| 47 |
'nvme': self.nvme_device_type, |
| 48 |
'ide': self.ide_device_type |
| 49 |
}) |
| 50 |
self.usb_device_type = { |
| 51 |
'usb2': vim.vm.device.VirtualUSBController, |
| 52 |
'usb3': vim.vm.device.VirtualUSBXHCIController |
| 53 |
} |
| 54 |
self.nic_device_type = { |
| 55 |
'pcnet32': vim.vm.device.VirtualPCNet32, |
| 56 |
'vmxnet2': vim.vm.device.VirtualVmxnet2, |
| 57 |
'vmxnet3': vim.vm.device.VirtualVmxnet3, |
| 58 |
'e1000': vim.vm.device.VirtualE1000, |
| 59 |
'e1000e': vim.vm.device.VirtualE1000e, |
| 60 |
'sriov': vim.vm.device.VirtualSriovEthernetCard, |
| 61 |
'pvrdma': vim.vm.device.VirtualVmxnet3Vrdma |
| 62 |
} |
| 63 |
|
| 64 |
def create_scsi_controller(self, scsi_type, bus_number, bus_sharing='noSharing'): |
| 65 |
""" |
| 66 |
Create SCSI Controller with given SCSI Type and SCSI Bus Number |
| 67 |
Args: |
| 68 |
scsi_type: Type of SCSI |
| 69 |
bus_number: SCSI Bus number to be assigned |
| 70 |
bus_sharing: noSharing, virtualSharing, physicalSharing |
| 71 |
|
| 72 |
Returns: Virtual device spec for SCSI Controller |
| 73 |
|
| 74 |
""" |
| 75 |
scsi_ctl = vim.vm.device.VirtualDeviceSpec() |
| 76 |
scsi_ctl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 77 |
scsi_device = self.scsi_device_type.get(scsi_type, vim.vm.device.ParaVirtualSCSIController) |
| 78 |
scsi_ctl.device = scsi_device() |
| 79 |
scsi_ctl.device.deviceInfo = vim.Description() |
| 80 |
scsi_ctl.device.busNumber = bus_number |
| 81 |
# While creating a new SCSI controller, temporary key value |
| 82 |
# should be unique negative integers |
| 83 |
scsi_ctl.device.key = -randint(1000, 9999) |
| 84 |
scsi_ctl.device.hotAddRemove = True |
| 85 |
scsi_ctl.device.sharedBus = bus_sharing |
| 86 |
scsi_ctl.device.scsiCtlrUnitNumber = 7 |
| 87 |
|
| 88 |
return scsi_ctl |
| 89 |
|
| 90 |
def is_scsi_controller(self, device): |
| 91 |
return isinstance(device, tuple(self.scsi_device_type.values())) |
| 92 |
|
| 93 |
@staticmethod |
| 94 |
def create_sata_controller(bus_number): |
| 95 |
sata_ctl = vim.vm.device.VirtualDeviceSpec() |
| 96 |
sata_ctl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 97 |
sata_ctl.device = vim.vm.device.VirtualAHCIController() |
| 98 |
sata_ctl.device.deviceInfo = vim.Description() |
| 99 |
sata_ctl.device.busNumber = bus_number |
| 100 |
sata_ctl.device.key = -randint(15000, 19999) |
| 101 |
|
| 102 |
return sata_ctl |
| 103 |
|
| 104 |
@staticmethod |
| 105 |
def is_sata_controller(device): |
| 106 |
return isinstance(device, vim.vm.device.VirtualAHCIController) |
| 107 |
|
| 108 |
@staticmethod |
| 109 |
def create_nvme_controller(bus_number): |
| 110 |
nvme_ctl = vim.vm.device.VirtualDeviceSpec() |
| 111 |
nvme_ctl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 112 |
nvme_ctl.device = vim.vm.device.VirtualNVMEController() |
| 113 |
nvme_ctl.device.deviceInfo = vim.Description() |
| 114 |
nvme_ctl.device.key = -randint(31000, 39999) |
| 115 |
nvme_ctl.device.busNumber = bus_number |
| 116 |
|
| 117 |
return nvme_ctl |
| 118 |
|
| 119 |
@staticmethod |
| 120 |
def is_nvme_controller(device): |
| 121 |
return isinstance(device, vim.vm.device.VirtualNVMEController) |
| 122 |
|
| 123 |
def create_disk_controller(self, ctl_type, ctl_number, bus_sharing='noSharing'): |
| 124 |
disk_ctl = None |
| 125 |
if ctl_type in self.scsi_device_type.keys(): |
| 126 |
disk_ctl = self.create_scsi_controller(ctl_type, ctl_number, bus_sharing) |
| 127 |
if ctl_type == 'sata': |
| 128 |
disk_ctl = self.create_sata_controller(ctl_number) |
| 129 |
if ctl_type == 'nvme': |
| 130 |
disk_ctl = self.create_nvme_controller(ctl_number) |
| 131 |
|
| 132 |
return disk_ctl |
| 133 |
|
| 134 |
def get_controller_disks(self, vm_obj, ctl_type, ctl_number): |
| 135 |
disk_controller = None |
| 136 |
disk_list = [] |
| 137 |
disk_key_list = [] |
| 138 |
if vm_obj is None: |
| 139 |
return disk_controller, disk_list |
| 140 |
disk_controller_type = self.scsi_device_type.copy() |
| 141 |
disk_controller_type.update({'sata': vim.vm.device.VirtualAHCIController, 'nvme': vim.vm.device.VirtualNVMEController}) |
| 142 |
for device in vm_obj.config.hardware.device: |
| 143 |
if isinstance(device, disk_controller_type[ctl_type]): |
| 144 |
if device.busNumber == ctl_number: |
| 145 |
disk_controller = device |
| 146 |
disk_key_list = device.device |
| 147 |
break |
| 148 |
if len(disk_key_list) != 0: |
| 149 |
for device in vm_obj.config.hardware.device: |
| 150 |
if isinstance(device, vim.vm.device.VirtualDisk): |
| 151 |
if device.key in disk_key_list: |
| 152 |
disk_list.append(device) |
| 153 |
|
| 154 |
return disk_controller, disk_list |
| 155 |
|
| 156 |
@staticmethod |
| 157 |
def create_ide_controller(bus_number=0): |
| 158 |
ide_ctl = vim.vm.device.VirtualDeviceSpec() |
| 159 |
ide_ctl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 160 |
ide_ctl.device = vim.vm.device.VirtualIDEController() |
| 161 |
ide_ctl.device.deviceInfo = vim.Description() |
| 162 |
# While creating a new IDE controller, temporary key value |
| 163 |
# should be unique negative integers |
| 164 |
ide_ctl.device.key = -randint(200, 299) |
| 165 |
ide_ctl.device.busNumber = bus_number |
| 166 |
|
| 167 |
return ide_ctl |
| 168 |
|
| 169 |
@staticmethod |
| 170 |
def create_cdrom(ctl_device, cdrom_type, iso_path=None, unit_number=0): |
| 171 |
cdrom_spec = vim.vm.device.VirtualDeviceSpec() |
| 172 |
cdrom_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 173 |
cdrom_spec.device = vim.vm.device.VirtualCdrom() |
| 174 |
cdrom_spec.device.controllerKey = ctl_device.key |
| 175 |
if isinstance(ctl_device, vim.vm.device.VirtualIDEController): |
| 176 |
cdrom_spec.device.key = -randint(3000, 3999) |
| 177 |
elif isinstance(ctl_device, vim.vm.device.VirtualAHCIController): |
| 178 |
cdrom_spec.device.key = -randint(16000, 16999) |
| 179 |
cdrom_spec.device.unitNumber = unit_number |
| 180 |
cdrom_spec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() |
| 181 |
cdrom_spec.device.connectable.allowGuestControl = True |
| 182 |
cdrom_spec.device.connectable.startConnected = (cdrom_type != "none") |
| 183 |
if cdrom_type in ["none", "client"]: |
| 184 |
cdrom_spec.device.backing = vim.vm.device.VirtualCdrom.RemotePassthroughBackingInfo() |
| 185 |
elif cdrom_type == "iso": |
| 186 |
cdrom_spec.device.backing = vim.vm.device.VirtualCdrom.IsoBackingInfo(fileName=iso_path) |
| 187 |
cdrom_spec.device.connectable.connected = True |
| 188 |
|
| 189 |
return cdrom_spec |
| 190 |
|
| 191 |
@staticmethod |
| 192 |
def is_equal_cdrom(vm_obj, cdrom_device, cdrom_type, iso_path): |
| 193 |
if cdrom_type == "none": |
| 194 |
return ( |
| 195 |
isinstance( |
| 196 |
cdrom_device.backing, |
| 197 |
vim.vm.device.VirtualCdrom.RemotePassthroughBackingInfo, |
| 198 |
) |
| 199 |
and cdrom_device.connectable.allowGuestControl |
| 200 |
and not cdrom_device.connectable.startConnected |
| 201 |
and ( |
| 202 |
vm_obj.runtime.powerState != vim.VirtualMachinePowerState.poweredOn |
| 203 |
or not cdrom_device.connectable.connected |
| 204 |
) |
| 205 |
) |
| 206 |
elif cdrom_type == "client": |
| 207 |
return ( |
| 208 |
isinstance( |
| 209 |
cdrom_device.backing, |
| 210 |
vim.vm.device.VirtualCdrom.RemotePassthroughBackingInfo, |
| 211 |
) |
| 212 |
and cdrom_device.connectable.allowGuestControl |
| 213 |
and cdrom_device.connectable.startConnected |
| 214 |
and ( |
| 215 |
vm_obj.runtime.powerState != vim.VirtualMachinePowerState.poweredOn |
| 216 |
or cdrom_device.connectable.connected |
| 217 |
) |
| 218 |
) |
| 219 |
elif cdrom_type == "iso": |
| 220 |
return ( |
| 221 |
isinstance( |
| 222 |
cdrom_device.backing, vim.vm.device.VirtualCdrom.IsoBackingInfo |
| 223 |
) |
| 224 |
and cdrom_device.backing.fileName == iso_path |
| 225 |
and cdrom_device.connectable.allowGuestControl |
| 226 |
and cdrom_device.connectable.startConnected |
| 227 |
and ( |
| 228 |
vm_obj.runtime.powerState != vim.VirtualMachinePowerState.poweredOn |
| 229 |
or cdrom_device.connectable.connected |
| 230 |
) |
| 231 |
) |
| 232 |
|
| 233 |
@staticmethod |
| 234 |
def update_cdrom_config(vm_obj, cdrom_spec, cdrom_device, iso_path=None): |
| 235 |
# Updating an existing CD-ROM |
| 236 |
if cdrom_spec["type"] in ["client", "none"]: |
| 237 |
cdrom_device.backing = vim.vm.device.VirtualCdrom.RemotePassthroughBackingInfo() |
| 238 |
elif cdrom_spec["type"] == "iso" and iso_path is not None: |
| 239 |
cdrom_device.backing = vim.vm.device.VirtualCdrom.IsoBackingInfo(fileName=iso_path) |
| 240 |
cdrom_device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() |
| 241 |
cdrom_device.connectable.allowGuestControl = True |
| 242 |
cdrom_device.connectable.startConnected = (cdrom_spec["type"] != "none") |
| 243 |
if vm_obj and vm_obj.runtime.powerState == vim.VirtualMachinePowerState.poweredOn: |
| 244 |
cdrom_device.connectable.connected = (cdrom_spec["type"] != "none") |
| 245 |
|
| 246 |
def remove_cdrom(self, cdrom_device): |
| 247 |
cdrom_spec = vim.vm.device.VirtualDeviceSpec() |
| 248 |
cdrom_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.remove |
| 249 |
cdrom_spec.device = cdrom_device |
| 250 |
|
| 251 |
return cdrom_spec |
| 252 |
|
| 253 |
def create_hard_disk(self, disk_ctl, disk_index=None): |
| 254 |
diskspec = vim.vm.device.VirtualDeviceSpec() |
| 255 |
diskspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 256 |
diskspec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create |
| 257 |
diskspec.device = vim.vm.device.VirtualDisk() |
| 258 |
diskspec.device.key = -randint(20000, 24999) |
| 259 |
diskspec.device.backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo() |
| 260 |
diskspec.device.controllerKey = disk_ctl.device.key |
| 261 |
|
| 262 |
if self.is_scsi_controller(disk_ctl.device): |
| 263 |
# one scsi controller attach 0-15 (except 7) disks |
| 264 |
if disk_index is None: |
| 265 |
self.module.fail_json(msg='unitNumber for sata disk is None.') |
| 266 |
else: |
| 267 |
if disk_index == 7 or disk_index > 15: |
| 268 |
self.module.fail_json(msg='Invalid scsi disk unitNumber, valid 0-15(except 7).') |
| 269 |
else: |
| 270 |
diskspec.device.unitNumber = disk_index |
| 271 |
elif self.is_sata_controller(disk_ctl.device): |
| 272 |
# one sata controller attach 0-29 disks |
| 273 |
if disk_index is None: |
| 274 |
self.module.fail_json(msg='unitNumber for sata disk is None.') |
| 275 |
else: |
| 276 |
if disk_index > 29: |
| 277 |
self.module.fail_json(msg='Invalid sata disk unitNumber, valid 0-29.') |
| 278 |
else: |
| 279 |
diskspec.device.unitNumber = disk_index |
| 280 |
elif self.is_nvme_controller(disk_ctl.device): |
| 281 |
# one nvme controller attach 0-14 disks |
| 282 |
if disk_index is None: |
| 283 |
self.module.fail_json(msg='unitNumber for nvme disk is None.') |
| 284 |
else: |
| 285 |
if disk_index > 14: |
| 286 |
self.module.fail_json(msg='Invalid nvme disk unitNumber, valid 0-14.') |
| 287 |
else: |
| 288 |
diskspec.device.unitNumber = disk_index |
| 289 |
|
| 290 |
return diskspec |
| 291 |
|
| 292 |
def create_nic(self, device_type, device_label, device_infos): |
| 293 |
nic = vim.vm.device.VirtualDeviceSpec() |
| 294 |
nic_device = self.nic_device_type.get(device_type) |
| 295 |
nic.device = nic_device() |
| 296 |
nic.device.key = -randint(25000, 29999) |
| 297 |
nic.device.wakeOnLanEnabled = bool(device_infos.get('wake_on_lan', True)) |
| 298 |
nic.device.deviceInfo = vim.Description() |
| 299 |
nic.device.deviceInfo.label = device_label |
| 300 |
nic.device.deviceInfo.summary = device_infos['name'] |
| 301 |
nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() |
| 302 |
nic.device.connectable.startConnected = bool(device_infos.get('start_connected', True)) |
| 303 |
nic.device.connectable.allowGuestControl = bool(device_infos.get('allow_guest_control', True)) |
| 304 |
nic.device.connectable.connected = bool(device_infos.get('connected', True)) |
| 305 |
if device_type == 'sriov': |
| 306 |
pf_backing = device_infos.get('physical_function_backing', None) |
| 307 |
vf_backing = device_infos.get('virtual_function_backing', None) |
| 308 |
|
| 309 |
nic.device.allowGuestOSMtuChange = bool(device_infos.get('allow_guest_os_mtu_change', True)) |
| 310 |
nic.device.sriovBacking = vim.vm.device.VirtualSriovEthernetCard.SriovBackingInfo() |
| 311 |
if pf_backing is not None: |
| 312 |
nic.device.sriovBacking.physicalFunctionBacking = vim.vm.device.VirtualPCIPassthrough.DeviceBackingInfo() |
| 313 |
nic.device.sriovBacking.physicalFunctionBacking.id = pf_backing |
| 314 |
if vf_backing is not None: |
| 315 |
nic.device.sriovBacking.virtualFunctionBacking = vim.vm.device.VirtualPCIPassthrough.DeviceBackingInfo() |
| 316 |
nic.device.sriovBacking.virtualFunctionBacking.id = vf_backing |
| 317 |
if 'mac' in device_infos and is_mac(device_infos['mac']): |
| 318 |
nic.device.addressType = 'manual' |
| 319 |
nic.device.macAddress = device_infos['mac'] |
| 320 |
else: |
| 321 |
nic.device.addressType = 'generated' |
| 322 |
|
| 323 |
return nic |
| 324 |
|
| 325 |
def integer_value(self, input_value, name): |
| 326 |
""" |
| 327 |
Function to return int value for given input, else return error |
| 328 |
Args: |
| 329 |
input_value: Input value to retrieve int value from |
| 330 |
name: Name of the Input value (used to build error message) |
| 331 |
Returns: (int) if integer value can be obtained, otherwise will send a error message. |
| 332 |
""" |
| 333 |
if isinstance(input_value, int): |
| 334 |
return input_value |
| 335 |
elif isinstance(input_value, str) and input_value.isdigit(): |
| 336 |
return int(input_value) |
| 337 |
else: |
| 338 |
self.module.fail_json(msg='"%s" attribute should be an' |
| 339 |
' integer value.' % name) |
| 340 |
|
| 341 |
def create_nvdimm_controller(self): |
| 342 |
nvdimm_ctl = vim.vm.device.VirtualDeviceSpec() |
| 343 |
nvdimm_ctl.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 344 |
nvdimm_ctl.device = vim.vm.device.VirtualNVDIMMController() |
| 345 |
nvdimm_ctl.device.deviceInfo = vim.Description() |
| 346 |
nvdimm_ctl.device.key = -randint(27000, 27999) |
| 347 |
|
| 348 |
return nvdimm_ctl |
| 349 |
|
| 350 |
@staticmethod |
| 351 |
def is_nvdimm_controller(device): |
| 352 |
return isinstance(device, vim.vm.device.VirtualNVDIMMController) |
| 353 |
|
| 354 |
def create_nvdimm_device(self, nvdimm_ctl_dev_key, pmem_profile_id, nvdimm_dev_size_mb=1024): |
| 355 |
nvdimm_dev_spec = vim.vm.device.VirtualDeviceSpec() |
| 356 |
nvdimm_dev_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 357 |
nvdimm_dev_spec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.create |
| 358 |
nvdimm_dev_spec.device = vim.vm.device.VirtualNVDIMM() |
| 359 |
nvdimm_dev_spec.device.controllerKey = nvdimm_ctl_dev_key |
| 360 |
nvdimm_dev_spec.device.key = -randint(28000, 28999) |
| 361 |
nvdimm_dev_spec.device.capacityInMB = nvdimm_dev_size_mb |
| 362 |
nvdimm_dev_spec.device.deviceInfo = vim.Description() |
| 363 |
nvdimm_dev_spec.device.backing = vim.vm.device.VirtualNVDIMM.BackingInfo() |
| 364 |
if pmem_profile_id is not None: |
| 365 |
profile = vim.vm.DefinedProfileSpec() |
| 366 |
profile.profileId = pmem_profile_id |
| 367 |
nvdimm_dev_spec.profile = [profile] |
| 368 |
|
| 369 |
return nvdimm_dev_spec |
| 370 |
|
| 371 |
@staticmethod |
| 372 |
def is_nvdimm_device(device): |
| 373 |
return isinstance(device, vim.vm.device.VirtualNVDIMM) |
| 374 |
|
| 375 |
def find_nvdimm_by_label(self, nvdimm_label, nvdimm_devices): |
| 376 |
nvdimm_dev = None |
| 377 |
for nvdimm in nvdimm_devices: |
| 378 |
if nvdimm.deviceInfo.label == nvdimm_label: |
| 379 |
nvdimm_dev = nvdimm |
| 380 |
|
| 381 |
return nvdimm_dev |
| 382 |
|
| 383 |
def remove_nvdimm(self, nvdimm_device): |
| 384 |
nvdimm_spec = vim.vm.device.VirtualDeviceSpec() |
| 385 |
nvdimm_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.remove |
| 386 |
nvdimm_spec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.destroy |
| 387 |
nvdimm_spec.device = nvdimm_device |
| 388 |
|
| 389 |
return nvdimm_spec |
| 390 |
|
| 391 |
def update_nvdimm_config(self, nvdimm_device, nvdimm_size): |
| 392 |
nvdimm_spec = vim.vm.device.VirtualDeviceSpec() |
| 393 |
nvdimm_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit |
| 394 |
nvdimm_spec.device = nvdimm_device |
| 395 |
nvdimm_device.capacityInMB = nvdimm_size |
| 396 |
|
| 397 |
return nvdimm_spec |
| 398 |
|
| 399 |
def is_tpm_device(self, device): |
| 400 |
return isinstance(device, vim.vm.device.VirtualTPM) |
| 401 |
|
| 402 |
def create_tpm(self): |
| 403 |
vtpm_device_spec = vim.vm.device.VirtualDeviceSpec() |
| 404 |
vtpm_device_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
| 405 |
vtpm_device_spec.device = vim.vm.device.VirtualTPM() |
| 406 |
vtpm_device_spec.device.deviceInfo = vim.Description() |
| 407 |
|
| 408 |
return vtpm_device_spec |
| 409 |
|
| 410 |
def remove_tpm(self, vtpm_device): |
| 411 |
vtpm_device_spec = vim.vm.device.VirtualDeviceSpec() |
| 412 |
vtpm_device_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.remove |
| 413 |
vtpm_device_spec.device = vtpm_device |
| 414 |
|
| 415 |
return vtpm_device_spec |
| 416 |
|
| 417 |
def gather_disk_info(self, vm_obj): |
| 418 |
""" |
| 419 |
Gather information about VM's disks |
| 420 |
Args: |
| 421 |
vm_obj: Managed object of virtual machine |
| 422 |
Returns: A list of dict containing disks information |
| 423 |
""" |
| 424 |
controller_info = dict() |
| 425 |
disks_info = dict() |
| 426 |
if vm_obj is None: |
| 427 |
return disks_info |
| 428 |
|
| 429 |
controller_index = 0 |
| 430 |
for controller in vm_obj.config.hardware.device: |
| 431 |
for name, type in self.disk_ctl_device_type.items(): |
| 432 |
if isinstance(controller, type): |
| 433 |
controller_info[str(controller_index)] = dict( |
| 434 |
key=controller.key, |
| 435 |
controller_type=name, |
| 436 |
bus_number=controller.busNumber, |
| 437 |
devices=controller.device |
| 438 |
) |
| 439 |
controller_index += 1 |
| 440 |
|
| 441 |
disk_index = 0 |
| 442 |
for disk in vm_obj.config.hardware.device: |
| 443 |
if isinstance(disk, vim.vm.device.VirtualDisk): |
| 444 |
if disk.storageIOAllocation is None: |
| 445 |
disk.storageIOAllocation = vim.StorageResourceManager.IOAllocationInfo() |
| 446 |
disk.storageIOAllocation.shares = vim.SharesInfo() |
| 447 |
|
| 448 |
if disk.shares is None: |
| 449 |
disk.shares = vim.SharesInfo() |
| 450 |
|
| 451 |
disks_info[str(disk_index)] = dict( |
| 452 |
key=disk.key, |
| 453 |
label=disk.deviceInfo.label, |
| 454 |
summary=disk.deviceInfo.summary, |
| 455 |
backing_filename=disk.backing.fileName, |
| 456 |
backing_datastore=disk.backing.datastore.name, |
| 457 |
backing_sharing=disk.backing.sharing if hasattr(disk.backing, 'sharing') else None, |
| 458 |
backing_uuid=disk.backing.uuid if hasattr(disk.backing, 'uuid') else None, |
| 459 |
backing_writethrough=disk.backing.writeThrough if hasattr(disk.backing, 'writeThrough') else None, |
| 460 |
backing_diskmode=disk.backing.diskMode if hasattr(disk.backing, 'diskMode') else None, |
| 461 |
backing_disk_mode=disk.backing.diskMode if hasattr(disk.backing, 'diskMode') else None, |
| 462 |
iolimit_limit=disk.storageIOAllocation.limit, |
| 463 |
iolimit_shares_level=disk.storageIOAllocation.shares.level, |
| 464 |
iolimit_shares_limit=disk.storageIOAllocation.shares.shares, |
| 465 |
shares_level=disk.shares.level, |
| 466 |
shares_limit=disk.shares.shares, |
| 467 |
controller_key=disk.controllerKey, |
| 468 |
unit_number=disk.unitNumber, |
| 469 |
capacity_in_kb=disk.capacityInKB, |
| 470 |
capacity_in_bytes=disk.capacityInBytes, |
| 471 |
) |
| 472 |
if isinstance(disk.backing, vim.vm.device.VirtualDisk.FlatVer1BackingInfo): |
| 473 |
disks_info[str(disk_index)]['backing_type'] = 'FlatVer1' |
| 474 |
|
| 475 |
elif isinstance(disk.backing, vim.vm.device.VirtualDisk.FlatVer2BackingInfo): |
| 476 |
disks_info[str(disk_index)]['backing_type'] = 'FlatVer2' |
| 477 |
disks_info[str(disk_index)]['backing_thinprovisioned'] = disk.backing.thinProvisioned |
| 478 |
disks_info[str(disk_index)]['backing_eagerlyscrub'] = disk.backing.eagerlyScrub |
| 479 |
|
| 480 |
elif isinstance(disk.backing, vim.vm.device.VirtualDisk.LocalPMemBackingInfo): |
| 481 |
disks_info[str(disk_index)]['backing_type'] = 'LocalPMem' |
| 482 |
disks_info[str(disk_index)]['backing_volumeuuid'] = disk.backing.volumeUUID |
| 483 |
|
| 484 |
elif isinstance(disk.backing, vim.vm.device.VirtualDisk.PartitionedRawDiskVer2BackingInfo): |
| 485 |
disks_info[str(disk_index)]['backing_type'] = 'PartitionedRawDiskVer2' |
| 486 |
disks_info[str(disk_index)]['backing_descriptorfilename'] = disk.backing.descriptorFileName |
| 487 |
|
| 488 |
elif isinstance(disk.backing, vim.vm.device.VirtualDisk.RawDiskMappingVer1BackingInfo): |
| 489 |
disks_info[str(disk_index)]['backing_type'] = 'RawDiskMappingVer1' |
| 490 |
disks_info[str(disk_index)]['backing_devicename'] = disk.backing.deviceName |
| 491 |
disks_info[str(disk_index)]['backing_lunuuid'] = disk.backing.lunUuid |
| 492 |
disks_info[str(disk_index)]['backing_compatibility_mode'] = disk.backing.compatibilityMode |
| 493 |
|
| 494 |
elif isinstance(disk.backing, vim.vm.device.VirtualDisk.RawDiskVer2BackingInfo): |
| 495 |
disks_info[str(disk_index)]['backing_type'] = 'RawDiskVer2' |
| 496 |
disks_info[str(disk_index)]['backing_descriptorfilename'] = disk.backing.descriptorFileName |
| 497 |
|
| 498 |
elif isinstance(disk.backing, vim.vm.device.VirtualDisk.SeSparseBackingInfo): |
| 499 |
disks_info[str(disk_index)]['backing_type'] = 'SeSparse' |
| 500 |
|
| 501 |
elif isinstance(disk.backing, vim.vm.device.VirtualDisk.SparseVer1BackingInfo): |
| 502 |
disks_info[str(disk_index)]['backing_type'] = 'SparseVer1' |
| 503 |
disks_info[str(disk_index)]['backing_spaceusedinkb'] = disk.backing.spaceUsedInKB |
| 504 |
disks_info[str(disk_index)]['backing_split'] = disk.backing.split |
| 505 |
|
| 506 |
elif isinstance(disk.backing, vim.vm.device.VirtualDisk.SparseVer2BackingInfo): |
| 507 |
disks_info[str(disk_index)]['backing_type'] = 'SparseVer2' |
| 508 |
disks_info[str(disk_index)]['backing_spaceusedinkb'] = disk.backing.spaceUsedInKB |
| 509 |
disks_info[str(disk_index)]['backing_split'] = disk.backing.split |
| 510 |
|
| 511 |
for controller_index in range(len(controller_info)): |
| 512 |
if controller_info[str(controller_index)]['key'] == disks_info[str(disk_index)]['controller_key']: |
| 513 |
disks_info[str(disk_index)]['controller_bus_number'] = controller_info[str(controller_index)]['bus_number'] |
| 514 |
disks_info[str(disk_index)]['controller_type'] = controller_info[str(controller_index)]['controller_type'] |
| 515 |
|
| 516 |
disk_index += 1 |
| 517 |
return disks_info |