def move_disk(self, source_datastore, source_id, dest_datastore, dest_id): source = vmdk_path(source_datastore, source_id) dest = vmdk_path(dest_datastore, dest_id) self._vmdk_mkdir(dest_datastore, dest_id) self._manage_disk(vim.VirtualDiskManager.MoveVirtualDisk_Task, sourceName=source, destName=dest) self._vmdk_rmdir(source_datastore, source_id)
def move_disk(self, source_datastore, source_id, dest_datastore, dest_id): source = vmdk_path(source_datastore, source_id) dest = vmdk_path(dest_datastore, dest_id) vmdk_mkdir(dest_datastore, dest_id, self._logger) self._manage_disk(vim.VirtualDiskManager.MoveVirtualDisk_Task, sourceName=source, destName=dest) self._vmdk_rmdir(source_datastore, source_id)
def test_find_virtual_disk(self): spec = vim.vm.ConfigSpec() vm_config = self.vm_config devices = self.dummy_devices() for device in devices: vm_config.add_device(spec, device) cfg_info = FakeConfigInfo() device_type = vim.vm.device.VirtualDisk datastore = "ds1" filename = "folder/foo" path = vmdk_path(datastore, filename) find_disk = vm_config.disk_matcher(datastore, filename) disk = vm_config.find_device(devices, device_type, matcher=find_disk) assert_that(disk, equal_to(None)) vm_config.add_scsi_disk(cfg_info, spec, datastore, "nope") self.assertRaises(DeviceNotFoundException, vm_config.get_device, devices, device_type, matcher=find_disk) vm_config.add_scsi_disk(cfg_info, spec, datastore, filename) device_changes = spec.deviceChange device_list = [] for device_change in device_changes: device_list.append(device_change.device) disk = vm_config.find_device(device_list, device_type, matcher=find_disk) assert_that(disk.backing.fileName, equal_to(path))
def copy_disk(self, source_datastore, source_id, dest_datastore, dest_id): """Copy a virtual disk. This method is used to create a "full clone" of a vmdk. Underneath, this call boils down to doing a DiskLib_Clone() Command line equivalent: $ vmkfstools -i source dest """ source = vmdk_path(source_datastore, source_id, IMAGE_FOLDER_NAME_PREFIX) dest = vmdk_path(dest_datastore, dest_id) self._vmdk_mkdir(dest_datastore, dest_id) self._manage_disk(vim.VirtualDiskManager.CopyVirtualDisk_Task, sourceName=source, destName=dest) self._manage_disk(vim.VirtualDiskManager.SetVirtualDiskUuid, name=dest, uuid=uuid_to_vmdk_uuid(dest_id))
def create_disk(self, datastore, disk_id, size): spec = self._create_spec(size) name = vmdk_path(datastore, disk_id) self._vmdk_mkdir(datastore, disk_id) self._manage_disk(vim.VirtualDiskManager.CreateVirtualDisk_Task, name=name, spec=spec) self._manage_disk(vim.VirtualDiskManager.SetVirtualDiskUuid, name=name, uuid=uuid_to_vmdk_uuid(disk_id))
def create_disk(self, datastore, disk_id, size): spec = self._create_spec(size) name = vmdk_path(datastore, disk_id) vmdk_mkdir(datastore, disk_id, self._logger) self._manage_disk(vim.VirtualDiskManager.CreateVirtualDisk_Task, name=name, spec=spec) self._manage_disk(vim.VirtualDiskManager.SetVirtualDiskUuid, name=name, uuid=uuid_to_vmdk_uuid(disk_id))
def copy_disk(self, source_datastore, source_id, dest_datastore, dest_id): """Copy a virtual disk. This method is used to create a "full clone" of a vmdk. Underneath, this call boils down to doing a DiskLib_Clone() Command line equivalent: $ vmkfstools -i source dest """ source = vmdk_path(source_datastore, source_id, IMAGE_FOLDER_NAME) dest = vmdk_path(dest_datastore, dest_id) vmdk_mkdir(dest_datastore, dest_id, self._logger) self._manage_disk(vim.VirtualDiskManager.CopyVirtualDisk_Task, sourceName=source, destName=dest) self._manage_disk(vim.VirtualDiskManager.SetVirtualDiskUuid, name=dest, uuid=uuid_to_vmdk_uuid(dest_id))
def _create_tmp_image(self, source_datastore, source_id, dest_datastore, dest_id): """ Copy an image into a temp location. 1. Lock a tmp image destination file with an exclusive lock. This is to prevent the GC thread from garbage collecting directories that are actively being used. The temp directory name contains a random UUID to prevent collisions with concurrent copies 2. Create the temp directory. 3. Copy the metadata file over. 4. Copy the vmdk over. @return the tmp image directory on success. """ source = vmdk_path(source_datastore, source_id, IMAGE_FOLDER_NAME) temp_dest = tmp_image_path(dest_datastore, dest_id) ds_type = self._get_datastore_type(dest_datastore) tmp_image_dir_path = os.path.dirname(datastore_to_os_path(temp_dest)) # Try grabbing the lock on the temp directory if it fails # (very unlikely) someone else is copying an image just retry # later. with FileBackedLock(tmp_image_dir_path, ds_type): source_meta = os_metadata_path(source_datastore, source_id, IMAGE_FOLDER_NAME) # Create the temp directory mkdir_p(tmp_image_dir_path) # Copy the metadata file if it exists. if os.path.exists(source_meta): try: dest_meta = os.path.join(tmp_image_dir_path, metadata_filename(dest_id)) shutil.copy(source_meta, dest_meta) except: self._logger.exception("Failed to copy metadata file %s", source_meta) raise # Copy the manifest file if it exists source_manifest = os_image_manifest_path(source_datastore, source_id) if os.path.exists(source_manifest): try: dest_manifest = os.path.join(tmp_image_dir_path, manifest_filename(dest_id)) shutil.copy(source_manifest, dest_manifest) except: # Swallow it. Not critical. pass # Create the timestamp file self._create_image_timestamp_file(tmp_image_dir_path) _vd_spec = self._prepare_virtual_disk_spec( vim.VirtualDiskManager.VirtualDiskType.thin, vim.VirtualDiskManager.VirtualDiskAdapterType.lsiLogic ) self._manage_disk( vim.VirtualDiskManager.CopyVirtualDisk_Task, sourceName=source, destName=temp_dest, destSpec=_vd_spec ) return tmp_image_dir_path
def _create_tmp_image(self, source_datastore, source_id, dest_datastore, dest_id): """ Copy an image into a temp location. 1. Lock a tmp image destination file with an exclusive lock. This is to prevent the GC thread from garbage collecting directories that are actively being used. The temp directory name contains a random UUID to prevent collisions with concurrent copies 2. Create the temp directory. 3. Copy the metadata file over. 4. Copy the vmdk over. @return the tmp image directory on success. """ source = vmdk_path(source_datastore, source_id, IMAGE_FOLDER_NAME) temp_dest = tmp_image_path(dest_datastore, dest_id) ds_type = self._get_datastore_type(dest_datastore) tmp_image_dir_path = os.path.dirname(datastore_to_os_path(temp_dest)) # Try grabbing the lock on the temp directory if it fails # (very unlikely) someone else is copying an image just retry # later. with FileBackedLock(tmp_image_dir_path, ds_type): source_meta = os_metadata_path(source_datastore, source_id, IMAGE_FOLDER_NAME) # Create the temp directory mkdir_p(tmp_image_dir_path) # Copy the metadata file if it exists. if os.path.exists(source_meta): try: shutil.copy(source_meta, tmp_image_dir_path) except: self._logger.exception("Failed to copy metadata file %s", source_meta) raise # Create the timestamp file self._create_image_timestamp_file(tmp_image_dir_path) _vd_spec = self._prepare_virtual_disk_spec( vim.VirtualDiskManager.VirtualDiskType.thin, vim.VirtualDiskManager.VirtualDiskAdapterType.lsiLogic) self._manage_disk(vim.VirtualDiskManager.CopyVirtualDisk_Task, sourceName=source, destName=temp_dest, destSpec=_vd_spec) return tmp_image_dir_path
def _copy_to_tmp_image(self, source_datastore, source_id, dest_datastore, dest_id): """ Copy an image into a temp location. 1. Lock a tmp image destination file with an exclusive lock. This is to prevent the GC thread from garbage collecting directories that are actively being used. The temp directory name contains a random UUID to prevent collisions with concurrent copies 2. Create the temp directory. 3. Copy the metadata file over. 4. Copy the vmdk over. @return the tmp image directory on success. """ ds_type = self._get_datastore_type(dest_datastore) if ds_type == DatastoreType.VSAN: tmp_image_dir = os_datastore_path(dest_datastore, compond_path_join(IMAGE_FOLDER_NAME_PREFIX, dest_id), compond_path_join(TMP_IMAGE_FOLDER_NAME_PREFIX, str(uuid.uuid4()))) else: tmp_image_dir = os_datastore_path(dest_datastore, compond_path_join(TMP_IMAGE_FOLDER_NAME_PREFIX, str(uuid.uuid4()))) # Create the temp directory self._vim_client.make_directory(tmp_image_dir) # Copy the metadata file if it exists. source_meta = os_metadata_path(source_datastore, source_id, IMAGE_FOLDER_NAME_PREFIX) if os.path.exists(source_meta): try: dest_meta = os.path.join(tmp_image_dir, metadata_filename(dest_id)) shutil.copy(source_meta, dest_meta) except: self._logger.exception("Failed to copy metadata file %s", source_meta) raise # Create the timestamp file self._create_image_timestamp_file(tmp_image_dir) _vd_spec = self._prepare_virtual_disk_spec( vim.VirtualDiskManager.VirtualDiskType.thin, vim.VirtualDiskManager.VirtualDiskAdapterType.lsiLogic) self._manage_disk(vim.VirtualDiskManager.CopyVirtualDisk_Task, sourceName=vmdk_path(source_datastore, source_id, IMAGE_FOLDER_NAME_PREFIX), destName=os_to_datastore_path(os.path.join(tmp_image_dir, "%s.vmdk" % dest_id)), destSpec=_vd_spec) return tmp_image_dir
def delete_disk(self, datastore, disk_id): name = vmdk_path(datastore, disk_id) self._manage_disk(vim.VirtualDiskManager.DeleteVirtualDisk_Task, name=name) self._vmdk_rmdir(datastore, disk_id)
def _query_uuid(self, datastore, disk_id): name = vmdk_path(datastore, disk_id) return self._manager.QueryVirtualDiskUuid(name=name)