【问题标题】:pyvmomi no thin provision when cloningpyvmomi 克隆时没有瘦配置
【发布时间】:2016-05-27 18:28:22
【问题描述】:

我正在尝试使用 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,现在包含在上面。这总是包含在我的真实脚本中,所以问题仍然存在。

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

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

disk_spec.fileOperation="create"

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

【问题讨论】:

    标签: vmware pyvmomi


    【解决方案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)
    

    上面的示例克隆虚拟机并将磁盘类型更改为厚置备急切归零。

    【讨论】:

      【解决方案2】:

      我找到了答案,由jswager1 on the VMWare forums发布:

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

      对于精简配置,请使用稀疏
      对于厚配置,请使用 flat

      【讨论】:

      • 太棒了,无法测试它,因为我们将所有模板都移到了薄,但看起来很完美。这里还有更多explanation of sparse vs flat
      猜你喜欢
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多