使用 pyVmomi 列出有效的 controllerKey 值
Listing valid controllerKey values with pyVmomi
我正在使用一些 example code to attach a new disk to a virtual machine as I create it using pyVmomi. Right now I have the controllerKey 硬编码为 200(在我的设置中恰好是一个 IDE 控制器——我不知道这个值在安装中的一致性如何)。
我想删除硬编码的控制器密钥,让用户至少可以在 IDE 和 SCSI 控制器之间进行选择。
有谁知道如何获取有效 VirtualController 密钥的列表?
为了后代,我使用的示例代码如下:
def create_virtual_disk(capacity, controller_key, unit_number, in_bytes=False):
"""
:param capacity: Capacity of new disk in Bytes
:param unit_number: device unit
:return:
"""
virtual_disk = vim.vm.device.VirtualDisk()
if in_bytes:
virtual_disk.capacityInBytes = capacity
else:
virtual_disk.capacityInKB = capacity
# Verify this.
virtual_disk.unitNumber = unit_number
virtual_disk.controllerKey = controller_key
# backing info
virtual_disk_backing_info = vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
virtual_disk_backing_info.diskMode = "persistent"
virtual_disk_backing_info.thinProvisioned = True
# assigning backing info to virtual disk device backing
virtual_disk.backing = virtual_disk_backing_info
# creating virtualdevice spec and assigning recently created virtual disk
virtual_disk_spec = vim.vm.device.VirtualDeviceSpec()
virtual_disk_spec.device = virtual_disk
virtual_disk_spec.fileOperation = "create"
virtual_disk_spec.operation = "add"
return virtual_disk_spec
短篇小说:有关详细信息,请参阅 a、b 或 c。一般建议坚持使用 a) 或针对特定 vmware 硬件模型的 controllerKeys
硬编码列表,因为它比 c) 更快(大尺寸响应)并且实施起来更轻松。
a) 从现有设备规格中找到控制器密钥
我们的代码库往往只添加已经存在于我们的虚拟机中的设备类型,因此以下代码片段对我们来说已经足够了,可能对您也一样:
从 type==ctrl_type
的现有控制器中获取密钥
def _find_controller(self, ctrl_type, max_devs=None):
for dev in self._machine._vm.config.hardware.device:
if isinstance(dev, ctrl_type):
if not max_devs or len(dev.device) < max_devs:
return dev
raise ControllerNotFoundException(ctrl_type)
一个具体的用例是添加另一个 IDE 类型的 CDRom(这仅添加到引用的规范中并在以后一次提交):
def _add_cdrom(self, spec, path):
spec.device.controllerKey = self._find_controller(vim.vm.device.VirtualIDEController, max_devs=2).key
dev = VSphereMediaDevice(spec.device, self._machine)
dev._spec.device.backing = vim.vm.device.VirtualCdrom.IsoBackingInfo()
dev.mount(path)
spec.device.backing = dev._spec.device.backing
b) 适用于您的硬件版本的有效控制器密钥列表
查看 /etc/vmware/hostd/env/vmconfigoption-esx-hw8.xml
(esxi,硬件版本 8 vms 的默认值),其中包含特定 vmware 硬件版本的所有可用 controllerKeys
。 controllerKeys
对于硬件版本来说非常一致,但在未来的版本中它总是不太可能发生变化。
<deviceInfo>
<_type>vim.Description</_type>
<label>VirtualIDEController 0</label>
<summary>VirtualIDEController 0</summary>
</deviceInfo>
<key>200</key>
c) 通过 SOAP 列出 vmconfigoptions
每当通过 vsphere 客户端创建新虚拟机时,都会执行 following SOAP 请求。它的响应包含大多数设备的 controllerKeys
硬件配置文件(win8、win7、winlonghorn 等)。但我想如果由于反应强烈而不得不多次执行此操作,这可能有点矫枉过正。
POST /sdk HTTP/1.1
User-Agent: VMware VI Client/xxx
Content-Type: text/xml; charset="utf-8"
SOAPAction: "urn:internalvim25/5.0"
Host: xxx
Cookie: vmware_soap_session="xxxx"
Content-Length: 526
...<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<operationID>xxx</operationID>
</soap:Header>
<soap:Body>
<QueryConfigOption xmlns="urn:internalvim25">
<_this xsi:type="ManagedObjectReference" type="EnvironmentBrowser" serverGuid="">ha-env-browser-vmx-08</_this>
<key>vmx-08</key>
</QueryConfigOption>
</soap:Body>
</soap:Envelope>
响应(截断):
HTTP/1.1 200 OK
Date: xxx
Cache-Control: no-cache
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8
Transfer-Encoding: chunked
7ED8
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<QueryConfigOptionResponse xmlns="urn:internalvim25"><returnval><version>vmx-08</version><description>Hardware version 8 virtual machine</description><guestOSDescriptor><id>windows8Server64Guest
....
<defaultDevice xsi:type="VirtualIDEController"><key>200</key><deviceInfo><label>IDE 0</label><summary>IDE 0</summary></deviceInfo><busNumber>0</busNumber></defaultDevice><defaultDevice xsi:type="VirtualIDEController"><key>201</key><deviceInfo><label>IDE 1</label><summary>IDE 1</summary></deviceInfo><busNumber>1</busNumber></defaultDevice><defaultDevice xsi:type="VirtualPS2Controller"><key>300</key><deviceInfo><label>PS2 controller 0</label><summary>PS2 controller 0</summary></deviceInfo><busNumber>0</busNumber></defaultDevice><defaultDevice xsi:type="VirtualPCIController"><key>100</key><deviceInfo><label>PCI controller 0</label><summary>PCI controller 0</summary></deviceInfo><busNumber>0</busNumber></defaultDevice><defaultDevice xsi:type="VirtualSIOController"><key>400</key><deviceInfo><label>SIO controller 0</label><summary>SIO controller 0</summary></deviceInfo><busNumber>0</busNumber></defaultDevice><defaultDevice xsi:type="VirtualKeyboard"><key>600</key><deviceInfo><label>Keyboard </label><summary>Keyboard</summary></deviceInfo><controllerKey>300</controllerKey><unitNumber>0</unitNumber></defaultDevice><defaultDevice xsi:type="VirtualPointingDevice"><key>700</key><deviceInfo><label>Pointing device</label><summary>Pointing device; Device</summary></deviceInfo><backing xsi:type="VirtualPointingDeviceDeviceBackingInfo"><deviceName></deviceName><useAutoDetect>false</useAutoDetect><hostPointingDevice>autodetect</hostPointingDevice></backing><controllerKey>300</controllerKey><unitNumber>1</unitNumber></defaultDevice><defaultDevice xsi:type="VirtualMachineVideoCard"><key>500</key><deviceInfo><label>Video card </label><summary>Video card</summary></deviceInfo>
我正在使用一些 example code to attach a new disk to a virtual machine as I create it using pyVmomi. Right now I have the controllerKey 硬编码为 200(在我的设置中恰好是一个 IDE 控制器——我不知道这个值在安装中的一致性如何)。
我想删除硬编码的控制器密钥,让用户至少可以在 IDE 和 SCSI 控制器之间进行选择。
有谁知道如何获取有效 VirtualController 密钥的列表?
为了后代,我使用的示例代码如下:
def create_virtual_disk(capacity, controller_key, unit_number, in_bytes=False):
"""
:param capacity: Capacity of new disk in Bytes
:param unit_number: device unit
:return:
"""
virtual_disk = vim.vm.device.VirtualDisk()
if in_bytes:
virtual_disk.capacityInBytes = capacity
else:
virtual_disk.capacityInKB = capacity
# Verify this.
virtual_disk.unitNumber = unit_number
virtual_disk.controllerKey = controller_key
# backing info
virtual_disk_backing_info = vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
virtual_disk_backing_info.diskMode = "persistent"
virtual_disk_backing_info.thinProvisioned = True
# assigning backing info to virtual disk device backing
virtual_disk.backing = virtual_disk_backing_info
# creating virtualdevice spec and assigning recently created virtual disk
virtual_disk_spec = vim.vm.device.VirtualDeviceSpec()
virtual_disk_spec.device = virtual_disk
virtual_disk_spec.fileOperation = "create"
virtual_disk_spec.operation = "add"
return virtual_disk_spec
短篇小说:有关详细信息,请参阅 a、b 或 c。一般建议坚持使用 a) 或针对特定 vmware 硬件模型的 controllerKeys
硬编码列表,因为它比 c) 更快(大尺寸响应)并且实施起来更轻松。
a) 从现有设备规格中找到控制器密钥
我们的代码库往往只添加已经存在于我们的虚拟机中的设备类型,因此以下代码片段对我们来说已经足够了,可能对您也一样:
从 type==ctrl_type
def _find_controller(self, ctrl_type, max_devs=None):
for dev in self._machine._vm.config.hardware.device:
if isinstance(dev, ctrl_type):
if not max_devs or len(dev.device) < max_devs:
return dev
raise ControllerNotFoundException(ctrl_type)
一个具体的用例是添加另一个 IDE 类型的 CDRom(这仅添加到引用的规范中并在以后一次提交):
def _add_cdrom(self, spec, path):
spec.device.controllerKey = self._find_controller(vim.vm.device.VirtualIDEController, max_devs=2).key
dev = VSphereMediaDevice(spec.device, self._machine)
dev._spec.device.backing = vim.vm.device.VirtualCdrom.IsoBackingInfo()
dev.mount(path)
spec.device.backing = dev._spec.device.backing
b) 适用于您的硬件版本的有效控制器密钥列表
查看 /etc/vmware/hostd/env/vmconfigoption-esx-hw8.xml
(esxi,硬件版本 8 vms 的默认值),其中包含特定 vmware 硬件版本的所有可用 controllerKeys
。 controllerKeys
对于硬件版本来说非常一致,但在未来的版本中它总是不太可能发生变化。
<deviceInfo>
<_type>vim.Description</_type>
<label>VirtualIDEController 0</label>
<summary>VirtualIDEController 0</summary>
</deviceInfo>
<key>200</key>
c) 通过 SOAP 列出 vmconfigoptions
每当通过 vsphere 客户端创建新虚拟机时,都会执行 following SOAP 请求。它的响应包含大多数设备的 controllerKeys
硬件配置文件(win8、win7、winlonghorn 等)。但我想如果由于反应强烈而不得不多次执行此操作,这可能有点矫枉过正。
POST /sdk HTTP/1.1
User-Agent: VMware VI Client/xxx
Content-Type: text/xml; charset="utf-8"
SOAPAction: "urn:internalvim25/5.0"
Host: xxx
Cookie: vmware_soap_session="xxxx"
Content-Length: 526
...<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<operationID>xxx</operationID>
</soap:Header>
<soap:Body>
<QueryConfigOption xmlns="urn:internalvim25">
<_this xsi:type="ManagedObjectReference" type="EnvironmentBrowser" serverGuid="">ha-env-browser-vmx-08</_this>
<key>vmx-08</key>
</QueryConfigOption>
</soap:Body>
</soap:Envelope>
响应(截断):
HTTP/1.1 200 OK
Date: xxx
Cache-Control: no-cache
Connection: Keep-Alive
Content-Type: text/xml; charset=utf-8
Transfer-Encoding: chunked
7ED8
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<QueryConfigOptionResponse xmlns="urn:internalvim25"><returnval><version>vmx-08</version><description>Hardware version 8 virtual machine</description><guestOSDescriptor><id>windows8Server64Guest
....
<defaultDevice xsi:type="VirtualIDEController"><key>200</key><deviceInfo><label>IDE 0</label><summary>IDE 0</summary></deviceInfo><busNumber>0</busNumber></defaultDevice><defaultDevice xsi:type="VirtualIDEController"><key>201</key><deviceInfo><label>IDE 1</label><summary>IDE 1</summary></deviceInfo><busNumber>1</busNumber></defaultDevice><defaultDevice xsi:type="VirtualPS2Controller"><key>300</key><deviceInfo><label>PS2 controller 0</label><summary>PS2 controller 0</summary></deviceInfo><busNumber>0</busNumber></defaultDevice><defaultDevice xsi:type="VirtualPCIController"><key>100</key><deviceInfo><label>PCI controller 0</label><summary>PCI controller 0</summary></deviceInfo><busNumber>0</busNumber></defaultDevice><defaultDevice xsi:type="VirtualSIOController"><key>400</key><deviceInfo><label>SIO controller 0</label><summary>SIO controller 0</summary></deviceInfo><busNumber>0</busNumber></defaultDevice><defaultDevice xsi:type="VirtualKeyboard"><key>600</key><deviceInfo><label>Keyboard </label><summary>Keyboard</summary></deviceInfo><controllerKey>300</controllerKey><unitNumber>0</unitNumber></defaultDevice><defaultDevice xsi:type="VirtualPointingDevice"><key>700</key><deviceInfo><label>Pointing device</label><summary>Pointing device; Device</summary></deviceInfo><backing xsi:type="VirtualPointingDeviceDeviceBackingInfo"><deviceName></deviceName><useAutoDetect>false</useAutoDetect><hostPointingDevice>autodetect</hostPointingDevice></backing><controllerKey>300</controllerKey><unitNumber>1</unitNumber></defaultDevice><defaultDevice xsi:type="VirtualMachineVideoCard"><key>500</key><deviceInfo><label>Video card </label><summary>Video card</summary></deviceInfo>