diff -uNr disk.orig/api.py disk/api.py --- disk.orig/api.py 2013-11-20 09:59:23.000000000 +1300 +++ disk/api.py 2013-11-25 15:06:49.696375793 +1300 @@ -172,16 +172,21 @@ # Check the image is unpartitioned if use_cow: + + fs = vfs.VFS.instance_for_image(image, 'qcow2', None) try: - fs = vfs.VFS.instance_for_image(image, 'qcow2', None) fs.setup() - fs.teardown() except exception.NovaException as e: LOG.debug(_('Unable to mount image %(image)s with ' 'error %(error)s. Cannot resize.'), {'image': image, 'error': e}) + return False + + finally: + fs.teardown() + else: # For raw, we can directly inspect the file system try: @@ -318,9 +323,13 @@ fmt = "raw" if use_cow: fmt = "qcow2" - try: fs = vfs.VFS.instance_for_image(image, fmt, partition) + try: fs.setup() + fs.teardown() + return inject_data_into_fs(fs, key, net, metadata, + admin_password, files, mandatory) + except Exception as e: # If a mandatory item is passed to this function, # then reraise the exception to indicate the error. @@ -332,12 +341,6 @@ '(%(e)s)'), {'e': e}) return False - try: - return inject_data_into_fs(fs, key, net, metadata, - admin_password, files, mandatory) - finally: - fs.teardown() - def setup_container(image, container_dir, use_cow=False): """Setup the LXC container. diff -uNr disk.orig/vfs/guestfs.py disk/vfs/guestfs.py --- disk.orig/vfs/guestfs.py 2013-11-20 10:01:27.000000000 +1300 +++ disk/vfs/guestfs.py 2013-11-25 14:48:53.628129923 +1300 @@ -123,17 +123,17 @@ self.handle.aug_init("/", 0) except RuntimeError as e: # dereference object and implicitly close() - self.handle = None + #self.handle = None raise exception.NovaException( _("Error mounting %(imgfile)s with libguestfs (%(e)s)") % {'imgfile': self.imgfile, 'e': e}) except Exception: - self.handle = None raise def teardown(self): LOG.debug(_("Tearing down appliance")) - + if self.handle is None: + return try: try: self.handle.aug_close()