From eb6432361ee4946d93867e2d3f0870d892a25c66 Mon Sep 17 00:00:00 2001 From: David Subiros Date: Wed, 28 Sep 2011 15:19:04 +0100 Subject: Enforce snapshot cleanup. Makes sure that the snapshot and the temporary directory is cleaned up, even if qemu-img fails. Fixes bug 861582. Change-Id: I3c7798215c8d0d5dc979379a13e038098b0f31d4 --- nova/virt/libvirt/connection.py | 48 +++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'nova') diff --git a/nova/virt/libvirt/connection.py b/nova/virt/libvirt/connection.py index 064c2688f..6afd41c1b 100644 --- a/nova/virt/libvirt/connection.py +++ b/nova/virt/libvirt/connection.py @@ -445,29 +445,31 @@ class LibvirtConnection(driver.ComputeDriver): # Export the snapshot to a raw image temp_dir = tempfile.mkdtemp() - out_path = os.path.join(temp_dir, snapshot_name) - qemu_img_cmd = ('qemu-img', - 'convert', - '-f', - source_format, - '-O', - image_format, - '-s', - snapshot_name, - disk_path, - out_path) - utils.execute(*qemu_img_cmd) - - # Upload that image to the image service - with open(out_path) as image_file: - image_service.update(context, - image_href, - metadata, - image_file) - - # Clean up - shutil.rmtree(temp_dir) - snapshot_ptr.delete(0) + try: + out_path = os.path.join(temp_dir, snapshot_name) + qemu_img_cmd = ('qemu-img', + 'convert', + '-f', + source_format, + '-O', + image_format, + '-s', + snapshot_name, + disk_path, + out_path) + utils.execute(*qemu_img_cmd) + + # Upload that image to the image service + with open(out_path) as image_file: + image_service.update(context, + image_href, + metadata, + image_file) + + finally: + # Clean up + shutil.rmtree(temp_dir) + snapshot_ptr.delete(0) @exception.wrap_exception() def reboot(self, instance, network_info, reboot_type=None, xml=None): -- cgit