Merge "libvirt: Report the allocated size of preallocated file based disks"

This commit is contained in:
Zuul 2018-04-17 21:16:41 +00:00 committed by Gerrit Code Review
commit f106094e96
3 changed files with 39 additions and 14 deletions

View File

@ -4439,8 +4439,9 @@ class LibvirtConnTestCase(test.NoDBTestCase,
@mock.patch.object(dmcrypt, 'delete_volume') @mock.patch.object(dmcrypt, 'delete_volume')
@mock.patch.object(conn._host, '_get_domain', return_value=dom) @mock.patch.object(conn._host, '_get_domain', return_value=dom)
def detach_encrypted_volumes(block_device_info, mock_get_domain, @mock.patch.object(libvirt_driver.disk_api, 'get_allocated_disk_size')
mock_delete_volume): def detach_encrypted_volumes(block_device_info, mock_get_alloc_size,
mock_get_domain, mock_delete_volume):
conn._detach_encrypted_volumes(instance, block_device_info) conn._detach_encrypted_volumes(instance, block_device_info)
mock_get_domain.assert_called_once_with(instance) mock_get_domain.assert_called_once_with(instance)
@ -8968,7 +8969,8 @@ class LibvirtConnTestCase(test.NoDBTestCase,
data = objects.LibvirtLiveMigrateData(is_volume_backed=True, data = objects.LibvirtLiveMigrateData(is_volume_backed=True,
is_shared_instance_path=False) is_shared_instance_path=False)
with test.nested( with test.nested(
mock.patch.object(os.path, 'getsize', mock_getsize), mock.patch.object(libvirt_driver.disk_api,
'get_allocated_disk_size', mock_getsize),
mock.patch.object(host.Host, '_get_domain', mock_lookup)): mock.patch.object(host.Host, '_get_domain', mock_lookup)):
self.assertFalse(drvr._is_shared_block_storage( self.assertFalse(drvr._is_shared_block_storage(
instance, data, instance, data,
@ -11667,9 +11669,14 @@ class LibvirtConnTestCase(test.NoDBTestCase,
fake_libvirt_utils.disk_sizes['/test/disk.local'] = 20 * units.Gi fake_libvirt_utils.disk_sizes['/test/disk.local'] = 20 * units.Gi
fake_libvirt_utils.disk_backing_files['/test/disk.local'] = 'file' fake_libvirt_utils.disk_backing_files['/test/disk.local'] = 'file'
self.mox.StubOutWithMock(os.path, "getsize") self.mox.StubOutWithMock(libvirt_driver.disk_api,
os.path.getsize('/test/disk').AndReturn((10737418240)) 'get_allocated_disk_size')
os.path.getsize('/test/disk.local').AndReturn((3328599655)) path = '/test/disk'
size = 10737418240
libvirt_driver.disk_api.get_allocated_disk_size(path).AndReturn((size))
path = '/test/disk.local'
size = 3328599655
libvirt_driver.disk_api.get_allocated_disk_size(path).AndReturn((size))
ret = ("image: /test/disk.local\n" ret = ("image: /test/disk.local\n"
"file format: qcow2\n" "file format: qcow2\n"
@ -11819,9 +11826,14 @@ class LibvirtConnTestCase(test.NoDBTestCase,
fake_libvirt_utils.disk_sizes['/test/disk.local'] = 20 * units.Gi fake_libvirt_utils.disk_sizes['/test/disk.local'] = 20 * units.Gi
fake_libvirt_utils.disk_backing_files['/test/disk.local'] = 'file' fake_libvirt_utils.disk_backing_files['/test/disk.local'] = 'file'
self.mox.StubOutWithMock(os.path, "getsize") self.mox.StubOutWithMock(libvirt_driver.disk_api,
os.path.getsize('/test/disk').AndReturn((10737418240)) 'get_allocated_disk_size')
os.path.getsize('/test/disk.local').AndReturn((3328599655)) path = '/test/disk'
size = 10737418240
libvirt_driver.disk_api.get_allocated_disk_size(path).AndReturn((size))
path = '/test/disk.local'
size = 3328599655
libvirt_driver.disk_api.get_allocated_disk_size(path).AndReturn((size))
ret = ("image: /test/disk.local\n" ret = ("image: /test/disk.local\n"
"file format: qcow2\n" "file format: qcow2\n"
@ -11885,8 +11897,11 @@ class LibvirtConnTestCase(test.NoDBTestCase,
fake_libvirt_utils.disk_sizes['/test/disk'] = 10 * units.Gi fake_libvirt_utils.disk_sizes['/test/disk'] = 10 * units.Gi
self.mox.StubOutWithMock(os.path, "getsize") self.mox.StubOutWithMock(libvirt_driver.disk_api,
os.path.getsize('/test/disk').AndReturn((10737418240)) "get_allocated_disk_size")
path = '/test/disk'
size = 10737418240
libvirt_driver.disk_api.get_allocated_disk_size(path).AndReturn((size))
self.mox.ReplayAll() self.mox.ReplayAll()
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
@ -11895,8 +11910,8 @@ class LibvirtConnTestCase(test.NoDBTestCase,
info = jsonutils.loads(info) info = jsonutils.loads(info)
self.assertEqual(1, len(info)) self.assertEqual(1, len(info))
self.assertEqual(info[0]['type'], 'raw') self.assertEqual(info[0]['type'], 'raw')
self.assertEqual(info[0]['path'], '/test/disk') self.assertEqual(info[0]['path'], path)
self.assertEqual(info[0]['disk_size'], 10737418240) self.assertEqual(info[0]['disk_size'], size)
self.assertEqual(info[0]['backing_file'], "") self.assertEqual(info[0]['backing_file'], "")
self.assertEqual(info[0]['over_committed_disk_size'], 0) self.assertEqual(info[0]['over_committed_disk_size'], 0)

View File

@ -99,6 +99,16 @@ def get_disk_size(path):
return images.qemu_img_info(path).virtual_size return images.qemu_img_info(path).virtual_size
def get_allocated_disk_size(path):
"""Get the allocated size of a disk image
:param path: Path to the disk image
:returns: Size (in bytes) of the given disk image as allocated on the
filesystem
"""
return images.qemu_img_info(path).disk_size
def extend(image, size): def extend(image, size):
"""Increase image to size. """Increase image to size.

View File

@ -7962,7 +7962,7 @@ class LibvirtDriver(driver.ComputeDriver):
fp = os.path.join(dirpath, f) fp = os.path.join(dirpath, f)
dk_size += os.path.getsize(fp) dk_size += os.path.getsize(fp)
else: else:
dk_size = int(os.path.getsize(path)) dk_size = disk_api.get_allocated_disk_size(path)
elif disk_type == 'block' and block_device_info: elif disk_type == 'block' and block_device_info:
dk_size = lvm.get_volume_size(path) dk_size = lvm.get_volume_size(path)
else: else: