From e22aa3a9da0dbf00ac43fa3c5964c9688a1b6cb7 Mon Sep 17 00:00:00 2001
From: Nikos Skalkotos <skalkoto@grnet.gr>
Date: Fri, 20 Apr 2012 11:29:59 +0300
Subject: [PATCH] Seperate snapshoting from get_device in Disk class

---
 image_creator/disk.py | 15 +++++++++------
 image_creator/main.py |  9 +++++++--
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/image_creator/disk.py b/image_creator/disk.py
index 01ff41d..ef53799 100644
--- a/image_creator/disk.py
+++ b/image_creator/disk.py
@@ -93,11 +93,9 @@ class Disk(object):
             job, args = self._cleanup_jobs.pop()
             job(*args)
 
-    def get_device(self):
-        """Returns a newly created DiskDevice instance.
-
-        This instance is a snapshot of the original source media of
-        the Disk instance.
+    def snapshot(self):
+        """Creates a snapshot of the original source media of the Disk
+        instance.
         """
 
         output("Examining source media `%s'..." % self.source, False)
@@ -141,7 +139,12 @@ class Disk(object):
         finally:
             os.unlink(table)
         success('done')
-        new_device = DiskDevice("/dev/mapper/%s" % snapshot)
+        return "/dev/mapper/%s" % snapshot
+
+    def get_device(self, media):
+        """Returns a newly created DiskDevice instance."""
+
+        new_device = DiskDevice(media)
         self._devices.append(new_device)
         new_device.enable()
         return new_device
diff --git a/image_creator/main.py b/image_creator/main.py
index 22fddf0..d073946 100755
--- a/image_creator/main.py
+++ b/image_creator/main.py
@@ -161,7 +161,9 @@ def image_creator():
 
     disk = Disk(options.source)
     try:
-        dev = disk.get_device()
+        snapshot = disk.snapshot()
+
+        dev = disk.get_device(snapshot)
         dev.mount()
 
         osclass = get_os_class(dev.distro, dev.ostype)
@@ -201,10 +203,13 @@ def image_creator():
 
             dev.dump(options.outfile)
 
+        # Destroy the device. We only need the snapshot from now on
+        disk.destroy_device(dev)
+
         if options.upload:
             output("Uploading image to pithos...", False)
             kamaki = Kamaki(options.account, options.token)
-            kamaki.upload(dev.device, size, options.upload)
+            kamaki.upload(snapshot, size, options.upload)
             output("done")
 
     finally:
-- 
GitLab