Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add PCI passthrough device to a VM using pyvmomi
def add_pci_to_vm(host_object, vm_object, host_pci_dev):
"""
host_pci_dev must be one of the devices from the host_object.hardware.pciDevice list
which is configured as a PCI passthrough device
"""
pci_passthroughs = vm_object.environmentBrowser.QueryConfigTarget(host=None).pciPassthrough
systemid_by_pciid = {item.pciDevice.id: item.systemId for item in pci_passthroughs}
if host_pci_dev.id not in systemid_by_pciid:
raise Exception("Not a passthrough device")
# According to the documentation, the deviceId is a signed short but the server expects the PCI device ID
# to be unsigned. TODO the modulu here is because of this, but I think it's not needed
deviceId = hex(host_pci_dev.deviceId % 2**16).lstrip('0x')
backing = vim.VirtualPCIPassthroughDeviceBackingInfo(deviceId=deviceId,
id=host_pci_dev.id,
systemId=systemid_by_pciid[host_pci_dev.id],
vendorId=host_pci_dev.vendorId,
deviceName=host_pci_dev.deviceName)
hba_object = vim.VirtualPCIPassthrough(key=-100, backing=backing)
new_device_config = vim.VirtualDeviceConfigSpec(device=hba_object)
new_device_config.operation = "add"
vm_object.ReconfigVM_Task(spec=new_device_config)
@Jain123

This comment has been minimized.

Copy link

@Jain123 Jain123 commented Feb 27, 2015

I tried the same(without changing anything) and getting TypeError.
TypeError: For "spec" expected type vim.vm.ConfigSpec, but got vim.vm.device.VirtualDeviceSpec

(With Changes)
I have created an object of Virtual Machine Config Spec and got different TypeError.

vm_spec=vim.vm.ConfigSpec()
vm_spec.deviceChange=new_device_config

TypeError: 'vim.vm.device.VirtualDeviceSpec' object is not iterable

please help me to sort it out.
Thanks

@Jain123

This comment has been minimized.

Copy link

@Jain123 Jain123 commented Mar 3, 2015

Got the solution for TypeError:'vim.vm.device.VirtualDeviceSpec' object is not iterable.

I had to pass config_spec file as a list of specs not as a single spec file.

Thanks

@yangyangyang001

This comment has been minimized.

Copy link

@yangyangyang001 yangyangyang001 commented Aug 3, 2016

@Jain123 Hi, I try the same and get error, and i pass config_spec file as a list of specs too, and get error too,can you show me how to add the pci dev?

@vikhyath

This comment has been minimized.

Copy link

@vikhyath vikhyath commented Oct 6, 2016

Its been a while, hope it helps someone out there :)

`
...
...
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]
vm_object.ReconfigVM_Task(spec=new_device_config)`

@matvesio

This comment has been minimized.

Copy link

@matvesio matvesio commented Jan 5, 2017

vmConfigSpec = vim.vm.ConfigSpec()
vmConfigSpec.deviceChange = [new_device_config]
vm_object.ReconfigVM_Task(spec=new_device_config) -->> vm_object.ReconfigVM_Task(spec=vmConfigSpec)

@matvesio

This comment has been minimized.

Copy link

@matvesio matvesio commented Jan 5, 2017

And Don't forget about memory reservation, the vm would not start without it.

@talvezu

This comment has been minimized.

Copy link

@talvezu talvezu commented Nov 20, 2017

Hi Guys,
I am wondering How I can remove PCI card in a similar way you are discussing here,
according to https://github.com/vmware/pyvmomi/blob/master/docs/vim/vm/device/VirtualDeviceSpec/Operation.rst
it seem strait forward that the only modification needed is "remove" instead of the "add"
but I get the notorious, Invalid configuration for device '0',
What can I be missing?
regards.

EDIT


One way to remove vm pci's card is as follows:

spec = vim.vm.ConfigSpec()

spec.deviceChange = []
for dev in vm.config.hardware.device:

    if isinstance(dev, vim.vm.device.VirtualPCIPassthrough):
        vdspec = vim.vm.device.VirtualDeviceSpec()
        vdspec.operation='remove'
        vdspec.device = dev
        spec.deviceChange.append(vdspec)
the_task = vm.ReconfigVM_Task(spec=spec)
@anand0716

This comment has been minimized.

Copy link

@anand0716 anand0716 commented Jul 10, 2019

Hello I'm not able to configure how to set memory reservation through the code so that my vm can start automatically through code.
Any help would be awesome.
regards.
EDIT
I figured it out how to set reserved memory.
For anyone wants to know add
vmConfigSpec.memoryReservationLockedToMax = True above vm_object.ReconfigVM_Task(spec=vmConfigSpec)
Cheers!

@kiranmohite6004

This comment has been minimized.

Copy link

@kiranmohite6004 kiranmohite6004 commented Apr 2, 2020

Basic question but How to set values of host_object, vm_object, host_pci_dev

@wiggin15

This comment has been minimized.

Copy link
Owner Author

@wiggin15 wiggin15 commented Apr 3, 2020

To get vm_object you can take a look at the pyvmomi community samples, or infi.pyvmomi_wrapper. Make sure it is not None. host_object can be the VM's runtime host - vm.summary.runtime.host. For host_pci_dev see the docstring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.