summaryrefslogtreecommitdiffstats
path: root/nova/virt
diff options
context:
space:
mode:
authorJosh Kearney <josh@jk0.org>2011-08-12 16:18:48 -0500
committerJosh Kearney <josh@jk0.org>2011-08-12 16:18:48 -0500
commitb60fa0d09d02066863736a3e98f07094c4db05a6 (patch)
tree0f2b7f52820a3820eb826c533da73287c3b50c22 /nova/virt
parent665d6130d0eb95ba1464e0ef7c09c4fa8f7f61b8 (diff)
downloadnova-b60fa0d09d02066863736a3e98f07094c4db05a6.tar.gz
nova-b60fa0d09d02066863736a3e98f07094c4db05a6.tar.xz
nova-b60fa0d09d02066863736a3e98f07094c4db05a6.zip
Check uncompressed VHD size.
Diffstat (limited to 'nova/virt')
-rw-r--r--nova/virt/xenapi/fake.py1
-rw-r--r--nova/virt/xenapi/vm_utils.py74
-rw-r--r--nova/virt/xenapi/vmops.py6
3 files changed, 70 insertions, 11 deletions
diff --git a/nova/virt/xenapi/fake.py b/nova/virt/xenapi/fake.py
index d5ac39473..eecd60d58 100644
--- a/nova/virt/xenapi/fake.py
+++ b/nova/virt/xenapi/fake.py
@@ -140,6 +140,7 @@ def create_vdi(name_label, read_only, sr_ref, sharable):
'location': '',
'xenstore_data': '',
'sm_config': {},
+ 'physical_utilisation': '123',
'VBDs': {}})
diff --git a/nova/virt/xenapi/vm_utils.py b/nova/virt/xenapi/vm_utils.py
index 6c44d53d4..e86f72347 100644
--- a/nova/virt/xenapi/vm_utils.py
+++ b/nova/virt/xenapi/vm_utils.py
@@ -31,6 +31,7 @@ import uuid
from xml.dom import minidom
import glance.client
+from nova import db
from nova import exception
from nova import flags
import nova.image
@@ -368,7 +369,7 @@ class VMHelper(HelperBase):
session.wait_for_task(task, instance.id)
@classmethod
- def fetch_image(cls, context, session, instance_id, image, user_id,
+ def fetch_image(cls, context, session, instance, image, user_id,
project_id, image_type):
"""Fetch image from glance based on image type.
@@ -377,18 +378,19 @@ class VMHelper(HelperBase):
"""
if image_type == ImageType.DISK_VHD:
return cls._fetch_image_glance_vhd(context,
- session, instance_id, image, image_type)
+ session, instance, image, image_type)
else:
return cls._fetch_image_glance_disk(context,
- session, instance_id, image, image_type)
+ session, instance, image, image_type)
@classmethod
- def _fetch_image_glance_vhd(cls, context, session, instance_id, image,
+ def _fetch_image_glance_vhd(cls, context, session, instance, image,
image_type):
"""Tell glance to download an image and put the VHDs into the SR
Returns: A list of dictionaries that describe VDIs
"""
+ instance_id = instance.id
LOG.debug(_("Asking xapi to fetch vhd image %(image)s")
% locals())
sr_ref = safe_find_sr(session)
@@ -422,17 +424,57 @@ class VMHelper(HelperBase):
cls.scan_sr(session, instance_id, sr_ref)
- # Pull out the UUID of the first VDI
- vdi_uuid = vdis[0]['vdi_uuid']
+ # Pull out the UUID of the first VDI (which is the os VDI)
+ os_vdi_uuid = vdis[0]['vdi_uuid']
+
# Set the name-label to ease debugging
- vdi_ref = session.get_xenapi().VDI.get_by_uuid(vdi_uuid)
+ vdi_ref = session.get_xenapi().VDI.get_by_uuid(os_vdi_uuid)
primary_name_label = get_name_label_for_image(image)
session.get_xenapi().VDI.set_name_label(vdi_ref, primary_name_label)
+ cls._check_vdi_size(context, session, instance, os_vdi_uuid)
return vdis
@classmethod
- def _fetch_image_glance_disk(cls, context, session, instance_id, image,
+ def _get_vdi_chain_size(cls, context, session, vdi_uuid):
+ """Compute the total size of a VDI chain, starting with the specified
+ VDI UUID.
+
+ This will walk the VDI chain to the root, add the size of each VDI into
+ the total.
+ """
+ size_bytes = 0
+ for vdi_rec in walk_vdi_chain(session, vdi_uuid):
+ vdi_size_bytes = int(vdi_rec['physical_utilisation'])
+ LOG.debug(_('vdi_uuid=%(vdi_uuid)s vdi_size_bytes='
+ '%(vdi_size_bytes)d' % locals()))
+ size_bytes += vdi_size_bytes
+ return size_bytes
+
+ @classmethod
+ def _check_vdi_size(cls, context, session, instance, vdi_uuid):
+ size_bytes = cls._get_vdi_chain_size(context, session, vdi_uuid)
+
+ # FIXME(sirp): this was copied directly from compute.manager.py, let's
+ # refactor this to a common area
+ instance_type_id = instance['instance_type_id']
+ instance_type = db.instance_type_get(context,
+ instance_type_id)
+ allowed_size_gb = instance_type['local_gb']
+ allowed_size_bytes = allowed_size_gb * 1024 * 1024 * 1024
+
+ LOG.debug(_("image_size_bytes=%(size_bytes)d, allowed_size_bytes="
+ "%(allowed_size_bytes)d") % locals())
+
+ if size_bytes > allowed_size_bytes:
+ LOG.info(_("Image size %(size_bytes)d exceeded"
+ " instance_type allowed size "
+ "%(allowed_size_bytes)d")
+ % locals())
+ raise exception.ImageTooLarge()
+
+ @classmethod
+ def _fetch_image_glance_disk(cls, context, session, instance, image,
image_type):
"""Fetch the image from Glance
@@ -444,6 +486,7 @@ class VMHelper(HelperBase):
Returns: A single filename if image_type is KERNEL_RAMDISK
A list of dictionaries that describe VDIs, otherwise
"""
+ instance_id = instance.id
# FIXME(sirp): Since the Glance plugin seems to be required for the
# VHD disk, it may be worth using the plugin for both VHD and RAW and
# DISK restores
@@ -750,6 +793,21 @@ def get_vhd_parent_uuid(session, vdi_ref):
return None
+def walk_vdi_chain(session, vdi_uuid):
+ """Yield vdi_recs for each element in a VDI chain"""
+ # TODO: perhaps make get_vhd_parent use this
+ while True:
+ vdi_ref = session.get_xenapi().VDI.get_by_uuid(vdi_uuid)
+ vdi_rec = session.get_xenapi().VDI.get_record(vdi_ref)
+ yield vdi_rec
+
+ parent_uuid = vdi_rec['sm_config'].get('vhd-parent')
+ if parent_uuid:
+ vdi_uuid = parent_uuid
+ else:
+ break
+
+
def wait_for_vhd_coalesce(session, instance_id, sr_ref, vdi_ref,
original_parent_uuid):
""" Spin until the parent VHD is coalesced into its parent VHD
diff --git a/nova/virt/xenapi/vmops.py b/nova/virt/xenapi/vmops.py
index b1522729a..7586ade7b 100644
--- a/nova/virt/xenapi/vmops.py
+++ b/nova/virt/xenapi/vmops.py
@@ -137,7 +137,7 @@ class VMOps(object):
def _create_disks(self, context, instance):
disk_image_type = VMHelper.determine_disk_image_type(instance)
vdis = VMHelper.fetch_image(context, self._session,
- instance.id, instance.image_ref,
+ instance, instance.image_ref,
instance.user_id, instance.project_id,
disk_image_type)
return vdis
@@ -182,11 +182,11 @@ class VMOps(object):
try:
if instance.kernel_id:
kernel = VMHelper.fetch_image(context, self._session,
- instance.id, instance.kernel_id, instance.user_id,
+ instance, instance.kernel_id, instance.user_id,
instance.project_id, ImageType.KERNEL)[0]
if instance.ramdisk_id:
ramdisk = VMHelper.fetch_image(context, self._session,
- instance.id, instance.ramdisk_id, instance.user_id,
+ instance, instance.ramdisk_id, instance.user_id,
instance.project_id, ImageType.RAMDISK)[0]
# Create the VM ref and attach the first disk
first_vdi_ref = self._session.call_xenapi('VDI.get_by_uuid',