'Consolidate volume backing VMDK files if needed.
The volume\'s VMDK file attached to an instance can be moved by SDRS
if enabled on the cluster.
By this the VMDK files can get copied onto another datastore and the
copy on this new location will be the latest version of the VMDK file.
So at the time of detach, we need to consolidate the current backing
VMDK file with the VMDK file in the new location.
We need to ensure that the VMDK chain (snapshots) remains intact during
the consolidation. SDRS retains the chain when it copies VMDK files
over, so for consolidation we relocate the backing with move option
as moveAllDiskBackingsAndAllowSharing and then delete the older version
of the VMDK file attaching the new version VMDK file.
In the case of a volume boot the we need to ensure that the volume
is on the datastore of the instance.'

def _consolidate_vmdk_volume(self, instance, vm_ref, device, volume_ref, adapter_type=None, disk_type=None):

    original_device = self._get_vmdk_base_volume_device(volume_ref)
    original_device_path = original_device.backing.fileName
    current_device_path = device.backing.fileName
    if (original_device_path == current_device_path):
        LOG.debug('The    volume    has    not    been    displaced    from    its    original    location:    %s.    No    consolidation    needed.', current_device_path)
        return
    LOG.info("The    volume's    backing    has    been    relocated    to    %s.    Need    to    consolidate    backing    disk    file.", current_device_path)
    host = self._get_host_of_vm(vm_ref)
    res_pool = self._get_res_pool_of_host(host)
    datastore = device.backing.datastore
    detached = False
    LOG.debug("Relocating    volume's    backing:    %(backing)s    to    resource    pool:    %(rp)s,    datastore:    %(ds)s,    host:    %(host)s.", {'backing': volume_ref, 'rp': res_pool, 'ds': datastore, 'host': host})
    try:
        self._relocate_vmdk_volume(volume_ref, res_pool, datastore, host)
    except oslo_vmw_exceptions.FileNotFoundException:
        LOG.warning("Virtual    disk:    %s    of    volume's    backing    not    found.", original_device_path, exc_info=True)
        LOG.debug("Removing    disk    device    of    volume's    backing    and    reattempting    relocate.")
        self.detach_disk_from_vm(volume_ref, instance, original_device)
        detached = True
        self._relocate_vmdk_volume(volume_ref, res_pool, datastore, host)
    if (not detached):
        self.detach_disk_from_vm(volume_ref, instance, original_device, destroy_disk=True)
    self.attach_disk_to_vm(volume_ref, instance, adapter_type, disk_type, vmdk_path=current_device_path)
