2
votes

I am trying to clone a VM in VMWare using pyVmomi.

However, I am having trouble getting thin provisioning for the disk.

Here are the portions of the code I believe are relevant for reproducing the issue.

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)    

Everything seems to be working with the cloning EXCEPT the thin provisioning of the disk. When I print out the clonespec right before cloning, I see the following (note the thinProvisioned = true portion):

     (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) []

When I check the disk specs after cloning via the GUI, The hard disk details say "Thick Provision Lazy Zeroed".

Does anybody know why the new disks are not being thin-provisioned, and what should be done instead?

Edit When I first wrote this question, I left out the crucial line of code disk_spec.device.backing.thinProvisioned=True, which is now included above. This was always included in my real script already, so the problem still remains.

UPDATE By migrating the template to a thin-provisioned disk, a thin provisioned disk can be made for the resulting VM. However, the issue with the code still remains as it seems as though it should work.

I believe the problem may be solved adding line(s) similar to:

disk_spec.fileOperation="create"

This should specify a change to device_spec.backing will be made also, but in my trials this caused the cloning to break. If anybody knows how to make the above work it would still be greatly appreciated.

2

2 Answers

1
votes

The VirtualMachineRelocateTransformation is depricated. Please use vim.vm.RelocateSpec.DiskLocator instead:

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)

Example above clones VM and changes disk type to thick provisioned eager zeroed.

0
votes

I found the answer, posted by jswager1 on the VMWare forums:

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

for thin provisioning , use sparse
for thick provisioning , use flat