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
This commit is contained in:
Dan Prince
2012-07-26 11:23:13 -04:00
parent ee4e63dbf4
commit f6f293ea44
6 changed files with 34 additions and 21 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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):
"<target dev='vdb' bus='virtio'/></disk>"
"</devices></domain>")
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()

View File

@@ -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

View File

@@ -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

View File

@@ -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,