Updated chroot's cleanup function
authorShuangquan Zhou <shuangquan.zhou@intel.com>
Mon, 15 Aug 2011 06:42:46 +0000 (14:42 +0800)
committerShuangquan Zhou <shuangquan.zhou@intel.com>
Mon, 15 Aug 2011 06:42:46 +0000 (14:42 +0800)
plugins/imager/livecd_plugin.py
plugins/imager/liveusb_plugin.py
plugins/imager/loop_plugin.py
plugins/imager/raw_plugin.py

index f621bb8..c98a0cc 100644 (file)
@@ -126,10 +126,11 @@ class LiveCDPlugin(ImagerPlugin):
         try:
             chroot.chroot(extmnt, None,  "/bin/env HOME=/root /bin/bash")
         except:
-            print >> sys.stderr, "Failed to chroot to %s." % img
+            raise CreatorError("Failed to chroot to %s." %img)
         finally:
-            chroot.cleanup_after_chroot("img",extloop,None,None)
-            return 1
+            imgloop.cleanup()
+            shutil.rmtree(imgmnt, ignore_errors = True) 
+            chroot.cleanup_after_chroot("img",extloop,tmpoutdir,extmnt)
         
     @classmethod
     def do_pack(cls, base_on):
index c54c5dc..33af1a6 100644 (file)
@@ -128,9 +128,11 @@ class LiveUSBPlugin(ImagerPlugin):
         try:
             chroot.chroot(extmnt, None,  "/bin/env HOME=/root /bin/bash")
         except:
-            chroot.cleanup_after_chroot("img", extloop, None, None)
-            print >> sys.stderr, "Failed to chroot to %s." % img
-            return 1
+            raise CreatorError("Failed to chroot to %s." %img)
+        finally:
+            imgloop.cleanup()
+            shutil.rmtree(imgmnt, ignore_errors = True) 
+            chroot.cleanup_after_chroot("img", extloop, tmpoutdir, extmnt)
     
     @classmethod
     def do_pack(cls, base_on):
index ba02686..f743b6b 100644 (file)
@@ -82,9 +82,9 @@ class LoopPlugin(ImagerPlugin):
         try:
             chroot.chroot(extmnt, None,  "/bin/env HOME=/root /bin/bash")
         except:
-            chroot.cleanup_after_chroot("img", extloop, None, None)
-            print >> sys.stderr, "Failed to chroot to %s." % extloop
-            return 1
+            raise CreatorError("Failed to chroot to %s." %img)  
+        finally:
+            chroot.cleanup_after_chroot("img", extloop, None, extmnt)
 
     @classmethod
     def do_unpack(cls, srcimg):
index 8c7c140..bdb9076 100644 (file)
@@ -71,11 +71,9 @@ class RawPlugin(ImagerPlugin):
         imgsize = misc.get_file_size(img) * 1024L * 1024L
         partedcmd = fs_related.find_binary_path("parted")
         disk = fs_related.SparseLoopbackDisk(img, imgsize)
-        extmnt = misc.mkdtemp()
-        tmpoutdir = misc.mkdtemp()
-        imgloop = PartitionedMount({'/dev/sdb':disk}, extmnt, skipformat = True)
+        imgmnt = misc.mkdtemp()
+        imgloop = PartitionedMount({'/dev/sdb':disk}, imgmnt, skipformat = True)
         img_fstype = "ext3"
-        extloop = None
         
         # Check the partitions from raw disk.
         p1 = subprocess.Popen([partedcmd,"-s",img,"unit","B","print"],
@@ -143,18 +141,17 @@ class RawPlugin(ImagerPlugin):
         
         try:
             imgloop.mount()
-            os_image = img
         except MountError, e:
             imgloop.cleanup()
             raise CreatorError("Failed to loopback mount '%s' : %s" %
                                (img, e))
 
         try:
-            chroot.chroot(extmnt, None,  "/bin/env HOME=/root /bin/bash")
+            chroot.chroot(imgmnt, None,  "/bin/env HOME=/root /bin/bash")
         except:
-            chroot.cleanup_after_chroot("img", imgloop, None, None)
-            print >> sys.stderr, "Failed to chroot to %s." % img
-            return 1
+            raise CreatorError("Failed to chroot to %s." %img)  
+        finally:
+            chroot.cleanup_after_chroot("img", imgloop, None, imgmnt)
             
     @classmethod
     def do_unpack(cls, srcimg):