我正在尝试使用pyVmomi在VMWare中克隆VM。
但是,我无法为磁盘进行精简配置。
以下是我认为与复制问题相关的代码部分。
def find_disk(vm,index):
"""Return the disk of the given index in the vm"""
i=0
for device in vm.config.hardware.device:
if hasattr(device.backing,"fileName"):
if i==index:
return device
else:
i +=1
def disk_controller(vm):
"""Return the first disk controller for the given vm"""
for device in vm.config.hardware.device:
if isinstance(device,vim.vm.device.VirtualSCSIController):
return device
# retrieve the template
template=retrieve_template_vm()
disk=find_disk(template,0)
controller=disk_controller(template)
## Create the the clonespec
clonespec=vim.vm.CloneSpec()
# Define the disk-change specification for changing the template's hard disk
disk_spec=vim.vm.device.VirtualDeviceSpec()
disk_spec.operation=vim.vm.device.VirtualDeviceSpec.Operation.edit
disk_spec.device=disk
disk_spec.device.controllerKey=controller.key
disk_spec.device.capacityInKB=60*1024*1024
disk_spec.device.backing.thinProvisioned=True
clonespec.config=vim.vm.ConfigSpec(numCPUs=2,memoryMB=4096,deviceChange=[disk_spec])
...
# Make other changes to the clone spec, such as setting datastore and cluster
...
template.Clone(folder=FOLDER,name=NEW_VM_NAME,spec=clonespec)
除了磁盘的精简配置之外,一切似乎都在使用克隆。当我在克隆之前打印出clonespec时,我看到以下内容(请注意thinProvisioned = true
部分):
(vim.vm.device.VirtualDeviceSpec) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
operation = 'edit',
fileOperation = <unset>,
device = (vim.vm.device.VirtualDisk) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
key = 2000,
deviceInfo = (vim.Description) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
label = 'Hard disk 1',
summary = '52,428,800 KB'
},
backing = (vim.vm.device.VirtualDisk.FlatVer2BackingInfo) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
fileName = '[SOME_IDENTIFIER] TEMPLATE_NAME/template_vm_name.vmdk',
datastore = 'vim.Datastore:datastore-38',
backingObjectId = '26-2000-0',
diskMode = 'persistent',
split = false,
writeThrough = false,
thinProvisioned = true,
eagerlyScrub = true,
uuid = '6000C293-7bc0-d8b5-9258-661c0368f67d',
contentId = '2a9b1883eb64e83b2a02d32225cead08',
changeId = <unset>,
parent = <unset>,
deltaDiskFormat = <unset>,
digestEnabled = false,
deltaGrainSize = <unset>
},
connectable = <unset>,
slotInfo = <unset>,
controllerKey = 1000,
unitNumber = 0,
capacityInKB = 62914560,
capacityInBytes = 53687091200,
shares = (vim.SharesInfo) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
shares = 1000,
level = 'normal'
},
storageIOAllocation = (vim.StorageResourceManager.IOAllocationInfo) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
limit = -1,
shares = (vim.SharesInfo) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
shares = 1000,
level = 'normal'
},
reservation = 0
},
diskObjectId = '26-2000',
vFlashCacheConfigInfo = <unset>
},
profile = (vim.vm.ProfileSpec) []
当我通过GUI克隆后检查磁盘规格时,硬盘详细信息说&#34;厚供应懒惰归零&#34;。
有人知道为什么新磁盘没有进行精简配置,应该做些什么呢?
编辑当我第一次写这个问题时,我遗漏了关键的代码行disk_spec.device.backing.thinProvisioned=True
,现在包含在上面。这已经包含在我的真实脚本中了,所以问题仍然存在。
更新通过将模板迁移到精简置备磁盘,可以为生成的VM创建精简置备磁盘。但是,代码的问题仍然存在,因为它似乎应该工作。
我相信问题可能会解决,添加类似于:
的行disk_spec.fileOperation="create"
这应该指定对device_spec.backing
的更改,但在我的试验中,这导致克隆中断。如果有人知道如何进行上述工作,我们将不胜感激。
答案 0 :(得分:0)
我找到了答案,由fire a simple event发布:
clonespec.Location.Transform = VirtualMachineRelocateTransformation.sparse; // This location seemed to be the key.
对于精简配置,请使用稀疏
对于厚配置,请使用平面
答案 1 :(得分:0)
VirtualMachineRelocateTransformation已被删除。请改用vim.vm.RelocateSpec.DiskLocator:
disk_locator = vim.vm.RelocateSpec.DiskLocator()
disk_locator.diskBackingInfo = vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
disk_locator.diskBackingInfo.eagerlyScrub = True
disk_locator.diskBackingInfo.thinProvisioned = False
for device in template_vm.config.hardware.device:
if hasattr(device.backing, 'fileName'):
disk_locator.diskId = device.key
break
disk_locator.datastore = datastore_object
relospec.disk.append(disk_locator)
上面的示例克隆VM并将磁盘类型更改为密集配置的急切归零。