Adding Devices to Virtual
MachinesLast Updated December 16, 2024
You can add devices to a virtual machine
during creation using the
VirtualMachineConfigSpec.deviceChange
property,
which is a
VirtualDeviceSpec
. You specify the
host device that the virtual device should map to by using a backing object. A
backing object represents the host device associated with a virtual device.
- Backing option objects – You can find out which devices the host supports by extracting the relevant backing option object.
- Backing information object – The backing information object allows you to supply data for virtual device configuration. You access aVirtualDeviceBackinInfoobject as follows:
To add a device to a virtual machine, you must
first find out which devices are supported on the corresponding ESXi host, and
then specify a
VirtualDevice
object. Perform
these tasks to add a device to a virtual machine:
- Find out which devices your ESXi system supports by calling theQueryConfigOptionmethod, which you can access through theVirtualMachine.environmentBrowserproperty. The method returns aVirtualMachineConfigOptiondata object that specifies what the ESXi supports. For example,VirtualMachineConfigOption.hardwareOptionsincludes information about supported CPU and memory and an array ofVirtualDeviceOptiondata objects.You cannot use theQueryConfigOptionmethod to create another instance of a default device. If you attempt to add a default device, such as an IDE controller, the server ignores the operation.
- Specify the backing information object for the device. The actual process for defining the object differs for different objects. For example, for a CD-ROM passthrough device, you use aVirtualCdromPassthroughBackingInfodevice. TheVirtualDevice.backingproperty is aVirtualDeviceBackingInfoobject which is extended by devices.The following code fragment adds a CD-ROM passthrough device:
- Specify connection information for the device.TheVirtualDevice.connectableproperty is aVirtualDeviceConnectInfodata object. This object provides information about restrictions on removing the device while a virtual machine is running. This property isnullif the device is not removable.
- Define the controller key, the virtual device key, and the unit number.You define these items with the integer properties:controllerKey,key, andunitNumber. See theVirtualDevicedata object in theAPI Reference.
- Specify device Information.ThedeviceInfoproperty is aDescriptiondata object that has anameproperty and asummaryproperty. You can supply a string value for each, describing the device.
- Specify the virtual device as thedeviceproperty of aVirtualDeviceConfigSpec.
- Specify theVirtualDeviceConfigSpecas thedeviceChangeproperty to theVirtualMachineConfigSpecthat you pass in to aFolder.CreateVM_TaskorVirtualMachine.ReconfigVM_Taskmethod.
Here’s the complete code fragment for a CD-ROM
passthrough device: