Merge "VMware: Use virtual disk size instead of image size" into stable/kilo

This commit is contained in:
Jenkins 2016-05-04 02:47:42 +00:00 committed by Gerrit Code Review
commit cd81512f87
4 changed files with 40 additions and 0 deletions

View File

@ -1396,6 +1396,7 @@ class FakeVim(object):
for file in matched_files:
matched = DataObject()
matched.path = file
matched.fileSize = 1024
result.file.append(matched)
task_mdo = create_task(method, "success", result=result)
else:

View File

@ -1803,7 +1803,9 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
@mock.patch.object(ds_util, 'file_move')
@mock.patch.object(vm_util, 'copy_virtual_disk')
@mock.patch.object(vmops.VMwareVMOps, '_delete_datastore_file')
@mock.patch.object(vmops.VMwareVMOps, '_update_image_size')
def test_cache_sparse_image(self,
mock_update_image_size,
mock_delete_datastore_file,
mock_copy_virtual_disk,
mock_file_move):
@ -1822,6 +1824,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
self._session, self._dc_info.ref,
sparse_disk_path,
DsPathMatcher(target_disk_path))
mock_update_image_size.assert_called_once_with(vi)
def test_get_storage_policy_none(self):
flavor = objects.Flavor(name='m1.small',

View File

@ -364,6 +364,11 @@ def search_datastore_spec(client_factory, file_name):
"""Builds the datastore search spec."""
search_spec = client_factory.create('ns0:HostDatastoreBrowserSearchSpec')
search_spec.matchPattern = [file_name]
search_spec.details = client_factory.create('ns0:FileQueryFlags')
search_spec.details.fileOwner = False
search_spec.details.fileSize = True
search_spec.details.fileType = False
search_spec.details.modification = False
return search_spec
@ -386,6 +391,20 @@ def file_exists(session, ds_browser, ds_path, file_name):
return file_exists
def file_size(session, ds_browser, ds_path, file_name):
"""Returns the size of the specified file."""
client_factory = session.vim.client.factory
search_spec = search_datastore_spec(client_factory, file_name)
search_task = session._call_method(session.vim,
"SearchDatastore_Task",
ds_browser,
datastorePath=str(ds_path),
searchSpec=search_spec)
task_info = session._wait_for_task(search_task)
if hasattr(task_info.result, 'file'):
return task_info.result.file[0].fileSize
def mkdir(session, ds_path, dc_ref):
"""Creates a directory at the path specified. If it is just "NAME",
then a directory with this name is created at the topmost level of the

View File

@ -419,6 +419,9 @@ class VMwareVMOps(object):
self._move_to_cache(vi.dc_info.ref,
tmp_image_ds_loc.parent,
vi.cache_image_folder)
# The size of the image is different from the size of the virtual
# disk. We want to use the latter.
self._update_image_size(vi)
def _cache_flat_image(self, vi, tmp_image_ds_loc):
self._move_to_cache(vi.dc_info.ref,
@ -552,6 +555,20 @@ class VMwareVMOps(object):
dc_info, size,
adapter_type, path)
def _update_image_size(self, vi):
"""Updates the file size of the specified image."""
# The size of the Glance image is different from the deployed VMDK
# size for sparse, streamOptimized and OVA images. We need to retrieve
# the size of the flat VMDK and update the file_size property of the
# image. This ensures that further operations involving size checks
# and disk resizing will work as expected.
ds_browser = self._get_ds_browser(vi.datastore.ref)
flat_file = "%s-flat.vmdk" % vi.ii.image_id
new_size = ds_util.file_size(self._session, ds_browser,
vi.cache_image_folder, flat_file)
if new_size is not None:
vi.ii.file_size = new_size
def spawn(self, context, instance, image_meta, injected_files,
admin_password, network_info, block_device_info=None,
power_on=True):