From f6f293ea44c73c59e651817a9315778ab5aa0e5e Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Thu, 26 Jul 2012 11:23:13 -0400 Subject: Don't import libvirt_utils in disk api. Updates the virt/disk/api so that we no longer import libvirt_utils. This fixes issues when using Nova compute with compute drivers other than libvirt. (xenapi, etc.) Fixes LP Bug #1029501. Change-Id: I46ece309480ce0a0941a96371a51d77712c41eb6 --- nova/tests/fake_libvirt_utils.py | 4 ---- nova/tests/test_imagebackend.py | 8 ++++---- nova/tests/test_libvirt.py | 21 +++++++++++++-------- nova/virt/disk/api.py | 18 +++++++++++++++--- nova/virt/libvirt/driver.py | 2 +- nova/virt/libvirt/imagebackend.py | 2 +- 6 files changed, 34 insertions(+), 21 deletions(-) diff --git a/nova/tests/fake_libvirt_utils.py b/nova/tests/fake_libvirt_utils.py index 1467a26de..1196e8576 100644 --- a/nova/tests/fake_libvirt_utils.py +++ b/nova/tests/fake_libvirt_utils.py @@ -35,10 +35,6 @@ def create_cow_image(backing_file, path): pass -def get_disk_size(path): - return disk_sizes.get(path, 1024 * 1024 * 20) - - def get_disk_backing_file(path): return disk_backing_files.get(path, None) diff --git a/nova/tests/test_imagebackend.py b/nova/tests/test_imagebackend.py index f2f48d73c..3f9ea38ce 100644 --- a/nova/tests/test_imagebackend.py +++ b/nova/tests/test_imagebackend.py @@ -238,7 +238,7 @@ class LvmTestCase(_ImageTestCase): fn = self.mox.CreateMockAnything() self.mox.StubOutWithMock(self.disk, 'resize2fs') self.mox.StubOutWithMock(self.libvirt_utils, 'create_lvm_image') - self.mox.StubOutWithMock(self.libvirt_utils, 'get_disk_size') + self.mox.StubOutWithMock(self.disk, 'get_disk_size') self.mox.StubOutWithMock(self.utils, 'execute') return fn @@ -249,7 +249,7 @@ class LvmTestCase(_ImageTestCase): self.LV, self.TEMPLATE_SIZE, sparse=sparse) - self.libvirt_utils.get_disk_size(self.TEMPLATE_PATH + self.disk.get_disk_size(self.TEMPLATE_PATH ).AndReturn(self.TEMPLATE_SIZE) cmd = ('dd', 'if=%s' % self.TEMPLATE_PATH, 'of=%s' % self.PATH, 'bs=4M') @@ -279,7 +279,7 @@ class LvmTestCase(_ImageTestCase): fn(target=self.TEMPLATE_PATH) self.libvirt_utils.create_lvm_image(self.VG, self.LV, self.SIZE, sparse=sparse) - self.libvirt_utils.get_disk_size(self.TEMPLATE_PATH + self.disk.get_disk_size(self.TEMPLATE_PATH ).AndReturn(self.TEMPLATE_SIZE) cmd = ('dd', 'if=%s' % self.TEMPLATE_PATH, 'of=%s' % self.PATH, 'bs=4M') @@ -321,7 +321,7 @@ class LvmTestCase(_ImageTestCase): self.SIZE, sparse=False ).AndRaise(RuntimeError()) - self.libvirt_utils.get_disk_size(self.TEMPLATE_PATH + self.disk.get_disk_size(self.TEMPLATE_PATH ).AndReturn(self.TEMPLATE_SIZE) self.mox.StubOutWithMock(self.libvirt_utils, 'remove_logical_volumes') self.libvirt_utils.remove_logical_volumes(self.PATH) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 322cd080a..ba0242a05 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -46,6 +46,7 @@ from nova.tests import fake_libvirt_utils from nova.tests import fake_network import nova.tests.image.fake from nova import utils +from nova.virt.disk import api as disk from nova.virt import driver from nova.virt import firewall as base_firewall from nova.virt import images @@ -1803,13 +1804,6 @@ class LibvirtConnTestCase(test.TestCase): "" "") - ret = ("image: /test/disk\n" - "file format: raw\n" - "virtual size: 20G (21474836480 bytes)\n" - "disk size: 3.1G\n" - "cluster_size: 2097152\n" - "backing file: /test/dummy (actual path: /backing/file)\n") - # Preparing mocks vdmock = self.mox.CreateMock(libvirt.virDomain) self.mox.StubOutWithMock(vdmock, "XMLDesc") @@ -1829,6 +1823,17 @@ class LibvirtConnTestCase(test.TestCase): os.path.getsize('/test/disk').AndReturn((10737418240)) os.path.getsize('/test/disk.local').AndReturn((21474836480)) + ret = ("image: /test/disk\n" + "file format: raw\n" + "virtual size: 20G (21474836480 bytes)\n" + "disk size: 3.1G\n" + "cluster_size: 2097152\n" + "backing file: /test/dummy (actual path: /backing/file)\n") + + self.mox.StubOutWithMock(utils, "execute") + utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info', + '/test/disk.local').AndReturn((ret, '')) + self.mox.ReplayAll() conn = libvirt_driver.LibvirtDriver(False) info = conn.get_instance_disk_info(instance_ref.name) @@ -3207,7 +3212,7 @@ disk size: 4.4M''', '')) # Start test self.mox.ReplayAll() - self.assertEquals(libvirt_utils.get_disk_size('/some/path'), 4592640) + self.assertEquals(disk.get_disk_size('/some/path'), 4592640) def test_copy_image(self): dst_fd, dst_path = tempfile.mkstemp() diff --git a/nova/virt/disk/api.py b/nova/virt/disk/api.py index 68d83c386..e55b54fa1 100644 --- a/nova/virt/disk/api.py +++ b/nova/virt/disk/api.py @@ -39,7 +39,7 @@ from nova import utils from nova.virt.disk import guestfs from nova.virt.disk import loop from nova.virt.disk import nbd -from nova.virt.libvirt import utils as libvirt_utils +from nova.virt import images LOG = logging.getLogger(__name__) @@ -103,9 +103,21 @@ def resize2fs(image, check_exit_code=False): utils.execute('resize2fs', image, check_exit_code=check_exit_code) +def get_disk_size(path): + """Get the (virtual) size of a disk image + + :param path: Path to the disk image + :returns: Size (in bytes) of the given disk image as it would be seen + by a virtual machine. + """ + size = images.qemu_img_info(path)['virtual size'] + size = size.split('(')[1].split()[0] + return int(size) + + def extend(image, size): """Increase image to size""" - virt_size = libvirt_utils.get_disk_size(image) + virt_size = get_disk_size(image) if virt_size >= size: return utils.execute('qemu-img', 'resize', image, size) @@ -117,7 +129,7 @@ def can_resize_fs(image, size, use_cow=False): """Check whether we can resize contained file system.""" # Check that we're increasing the size - virt_size = libvirt_utils.get_disk_size(image) + virt_size = get_disk_size(image) if virt_size >= size: return False diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 761ca3e45..71454d38e 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -2667,7 +2667,7 @@ class LibvirtDriver(driver.ComputeDriver): disk_type = driver_nodes[cnt].get('type') if disk_type == "qcow2": backing_file = libvirt_utils.get_disk_backing_file(path) - virt_size = libvirt_utils.get_disk_size(path) + virt_size = disk.get_disk_size(path) else: backing_file = "" virt_size = 0 diff --git a/nova/virt/libvirt/imagebackend.py b/nova/virt/libvirt/imagebackend.py index d66dbaf2c..001bf58a3 100644 --- a/nova/virt/libvirt/imagebackend.py +++ b/nova/virt/libvirt/imagebackend.py @@ -191,7 +191,7 @@ class Lvm(Image): def create_image(self, prepare_template, base, size, *args, **kwargs): @utils.synchronized(base) def create_lvm_image(base, size): - base_size = libvirt_utils.get_disk_size(base) + base_size = disk.get_disk_size(base) resize = size > base_size size = size if resize else base_size libvirt_utils.create_lvm_image(self.vg, self.lv, -- cgit