2

我正在尝试使用 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 克隆后检查磁盘规格时,硬盘详细信息显示“Thick Provision Lazy Zeroed”。

有谁知道为什么新磁盘没有被精简配置,应该怎么做?

编辑当我第一次写这个问题时,我遗漏了关键的代码行disk_spec.device.backing.thinProvisioned=True,现在包含在上面。这总是包含在我的真实脚本中,所以问题仍然存在。

更新通过将模板迁移到精简配置磁盘,可以为生成的 VM 制作精简配置磁盘。但是,代码的问题仍然存在,似乎它应该可以工作。

我相信可以通过添加类似于以下内容的行来解决问题:

disk_spec.fileOperation="create"

这应该指定也将进行更改device_spec.backing,但在我的试验中,这导致克隆中断。如果有人知道如何完成上述工作,我们将不胜感激。

4

2 回答 2

1

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 并将磁盘类型更改为厚置备急切归零。

于 2018-01-03T10:20:23.697 回答
0

我找到了答案,由jswager1 在 VMWare 论坛上发布:

clonespec.Location.Transform = VirtualMachineRelocateTransformation.sparse; // This location seemed to be the key.

对于精简配置,使用稀疏
的密集配置,使用扁平化

于 2016-03-23T14:45:03.820 回答