def _add_pci_device_to_vm(self, vm_obj, pci_Passthrough_device_obj, pci_id): changed = False failed = False vm_current_pci_devices = self._get_the_pci_devices_in_the_vm(vm_obj) if self.params['force'] or pci_id not in vm_current_pci_devices: deviceid = hex(pci_Passthrough_device_obj.pciDevice.deviceId % 2**16).lstrip('0x') systemid = pci_Passthrough_device_obj.systemId backing = vim.VirtualPCIPassthroughDeviceBackingInfo(deviceId=deviceid, id=pci_id, systemId=systemid, vendorId=pci_Passthrough_device_obj.pciDevice.vendorId, deviceName=pci_Passthrough_device_obj.pciDevice.deviceName) hba_object = vim.VirtualPCIPassthrough(key=-100, backing=backing) new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object) new_device_config.operation = "add" vmConfigSpec = vim.vm.ConfigSpec() vmConfigSpec.deviceChange = [new_device_config] vmConfigSpec.memoryReservationLockedToMax = True try: task = vm_obj.ReconfigVM_Task(spec=vmConfigSpec) wait_for_task(task) changed = True except Exception as exc: failed = True self.module.fail_json(msg="Failed to add Pci device" " '{}' to vm {}.".format(pci_id, vm_obj.name), detail=exc.msg) else: return changed, failed return changed, failed
def add_pci_device(self, vm, pci_device): """ Attaches PCI device to VM Args: vm (vim.VirtualMachine): VM instance pci_device (vim.vm.PciPassthroughInfo): PCI device to add """ host = vm.runtime.host.name logger.info( f"Adding PCI device with ID:{pci_device.pciDevice.id} on host {host} to {vm.name}" ) deviceId = hex(pci_device.pciDevice.deviceId % 2**16).lstrip("0x") backing = vim.VirtualPCIPassthroughDeviceBackingInfo( deviceId=deviceId, id=pci_device.pciDevice.id, systemId=pci_device.systemId, vendorId=pci_device.pciDevice.vendorId, deviceName=pci_device.pciDevice.deviceName, ) hba_object = vim.VirtualPCIPassthrough(key=-100, backing=backing) new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object) new_device_config.operation = "add" vmConfigSpec = vim.vm.ConfigSpec() vmConfigSpec.memoryReservationLockedToMax = True vmConfigSpec.deviceChange = [new_device_config] WaitForTask(vm.ReconfigVM_Task(spec=vmConfigSpec))
def add_pci(self, pci, host_obj, vm_update, vm_status, mmio_size): """ Add a PCI device for a VM. If a PCI device has large BARs, it requires 64bit MMIO support and large enough MMIO mapping space. This method will add these two configurations by default and check uEFI installation. But haven't evaluated the impacts of adding these configurations for a PCI device which doesn't have large BARs. For more details, check the reference KB article. Args: pci (str): pci ID of the PCI device host_obj (vim.HostSystem): Host obj to locate the PCI device vm_update (ConfigVM): VM update obj vm_status (GetVM): VM status obj mmio_size (int): 64-bit MMIO space in GB Returns: list: a list of Task objects References: https://kb.vmware.com/s/article/2142307 """ self.logger.info("Adding PCI device {0} for {1}".format( pci, self.vm_obj.name)) extra_config_key1 = "pciPassthru.64bitMMIOSizeGB" extra_config_key2 = "pciPassthru.use64bitMMIO" if mmio_size is None: mmio_size = 256 tasks = [] pci_obj = GetHost(host_obj).pci_obj(pci) # Convert decimal to hex for the device ID of PCI device device_id = hex(pci_obj.deviceId % 2**16).lstrip("0x") if not vm_status.uefi(): self.logger.warning("VM {0} is not installed with UEFI. " "If PCI device has large BARs, " "UEFI installation is required.".format( self.vm_obj.name)) else: self.logger.info("Good. VM {0} has UEFI " "installation.".format(self.vm_obj.name)) sys_id = vm_status.pci_id_sys_id_passthru() backing = vim.VirtualPCIPassthroughDeviceBackingInfo( deviceId=device_id, id=pci_obj.id, systemId=sys_id[pci_obj.id], vendorId=pci_obj.vendorId, deviceName=pci_obj.deviceName, ) backing_obj = vim.VirtualPCIPassthrough(backing=backing) dev_config_spec = vim.VirtualDeviceConfigSpec(device=backing_obj) dev_config_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add config_spec = vim.vm.ConfigSpec() config_spec.deviceChange = [dev_config_spec] tasks.append(self.vm_obj.ReconfigVM_Task(spec=config_spec)) tasks.append(vm_update.add_extra(extra_config_key1, str(mmio_size))) tasks.append(vm_update.add_extra(extra_config_key2, "TRUE")) return tasks
def main(): context = None if hasattr(ssl, '_create_unverified_context'): context = ssl._create_unverified_context() si = SmartConnect(host="[VCSA/Host NAME or IP HERE]", user="******", pwd="[PASSWORD HERE]", port=443, sslContext=context) if not si: print("Could not connect to the specified host using specified " "username and password") return -1 atexit.register(Disconnect, si) ################################################### # New Content ################################################### HostContent = si.content vm = None TempVMlist = HostContent.viewManager.CreateContainerView(HostContent.rootFolder,\ [vim.VirtualMachine], True) for managed_VM_ref in TempVMlist.view: #Go thought VM list if managed_VM_ref.name == "Compute000": #find Desired VM print(managed_VM_ref) print(managed_VM_ref.name) vm = managed_VM_ref #Capture VM as an obj to use next if vm != None: #Safety to make sure not added to null object cspec = vim.vm.ConfigSpec() cspec.deviceChange = [vim.VirtualDeviceConfigSpec()] cspec.deviceChange[0].operation = 'add' cspec.deviceChange[0].device = vim.VirtualPCIPassthrough() cspec.deviceChange[0].device.deviceInfo = vim.Description() cspec.deviceChange[ 0].device.deviceInfo.summary = 'NVIDIA GRID vGPU grid_p4-4q' cspec.deviceChange[0].device.deviceInfo.label = 'New PCI device' cspec.deviceChange[0].device.backing = \ vim.VirtualPCIPassthroughVmiopBackingInfo(vgpu='grid_p4-4q') #cspec.deviceChange[0].device.backing.vgpu =str('grid_p4-2q') WaitForTask(vm.Reconfigure(cspec)) ################################################### # End New Content ################################################### return 0
def add_pci_nics(args, vm): pci_id_list = args.pci_nics.rstrip(',') pci_id_list = pci_id_list.split(',') pci_id_list.sort() if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOn: print "VM:%s is powered ON. Cannot do hot pci add now. Shutting it down" % ( args.vm_name) poweroffvm(vm) for pci_id in pci_id_list: device_config_list = [] found = False for device_list in vm.config.hardware.device: if (isinstance(device_list, vim.vm.device.VirtualPCIPassthrough)) == True \ and device_list.backing.id == pci_id: print "pci_device already present! Not adding the pci device." found = True break if found == True: continue pci_passthroughs = vm.environmentBrowser.QueryConfigTarget( host=None).pciPassthrough for pci_entry in pci_passthroughs: if pci_entry.pciDevice.id == pci_id: found = True print "Found the pci device %s in the host" % (pci_id) break if found == False: print "Did not find the pci passthrough device %s on the host" % ( pci_id) exit(1) print "Adding PCI device to Contrail VM: %s" % (args.vm_name) deviceId = hex(pci_entry.pciDevice.deviceId % 2**16).lstrip('0x') backing = vim.VirtualPCIPassthroughDeviceBackingInfo( deviceId=deviceId, id=pci_entry.pciDevice.id, systemId=pci_entry.systemId, vendorId=pci_entry.pciDevice.vendorId, deviceName=pci_entry.pciDevice.deviceName) hba_object = vim.VirtualPCIPassthrough(key=-100, backing=backing) new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object) new_device_config.operation = "add" new_device_config.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo( ) new_device_config.device.connectable.startConnected = True device_config_list.append(new_device_config) vm_spec = vim.vm.ConfigSpec() vm_spec.deviceChange = device_config_list task = vm.ReconfigVM_Task(spec=vm_spec) wait_for_task(task)
def add_vgpu(self, vgpu_profile): """ Add a vGPU profile for a VM Args: vgpu_profile (str): the name of vGPU profile to be added into a VM Returns: Task """ self.logger.info("Adding vGPU {0} for " "VM {1}".format(vgpu_profile, self.vm_obj.name)) backing = vim.VirtualPCIPassthroughVmiopBackingInfo(vgpu=vgpu_profile) backing_obj = vim.VirtualPCIPassthrough(backing=backing) dev_config_spec = vim.VirtualDeviceConfigSpec(device=backing_obj) dev_config_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add config_spec = vim.vm.ConfigSpec() config_spec.deviceChange = [dev_config_spec] return self.vm_obj.ReconfigVM_Task(spec=config_spec)
def _add_vgpu_profile_to_vm(self, vm_obj, vgpu_profile_name, vgpu_prfl): """ Add vGPU profile of virtual machine Args: vm_obj: Managed object of virtual machine vgpu_profile_name: vGPU profile object name from ESXi server list vgpu_prfl: vGPU profile name Returns: Operation results """ changed = False failed = False vm_current_vgpu_profile = self._get_vgpu_profile_in_the_vm(vm_obj) if self.params["force"] or vgpu_prfl not in vm_current_vgpu_profile: vgpu_p = vgpu_profile_name.vgpu backing = vim.VirtualPCIPassthroughVmiopBackingInfo(vgpu=vgpu_p) summary = "NVIDIA GRID vGPU " + vgpu_prfl deviceInfo = vim.Description(summary=summary, label="PCI device 0") hba_object = vim.VirtualPCIPassthrough(backing=backing, deviceInfo=deviceInfo) new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object) new_device_config.operation = "add" vmConfigSpec = vim.vm.ConfigSpec() vmConfigSpec.deviceChange = [new_device_config] vmConfigSpec.memoryReservationLockedToMax = True try: task = vm_obj.ReconfigVM_Task(spec=vmConfigSpec) wait_for_task(task) changed = True except Exception as exc: failed = True self.module.fail_json( msg="Failed to add vGPU Profile" " '%s' to vm %s." % (vgpu_prfl, vm_obj.name), detail=exc.msg, ) else: return changed, failed return changed, failed