我使用某些example code将新磁盘附加到虚拟机,因为我使用pyVmomi创建它。现在我将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
答案 0 :(得分:1)
短篇小说:有关详细信息,请参阅a,b或c。一般的建议是坚持使用a)或controllerKeys
的硬编码列表,用于特定的vmware硬件模型,因为它比c)更快(大尺寸响应),而不是实现的喧嚣。
a)从现有设备中找到controllerkey
我们的代码库往往只添加我们的vms中已经存在的设备类型,因此以下代码段对我们来说已经足够了,可能也适合您:
从已有的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)hw版本的有效控制器密钥列表
查看/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>