Add {enable, disable}_guestfs methods in image cls
authorNikos Skalkotos <skalkoto@grnet.gr>
Thu, 1 Aug 2013 09:08:46 +0000 (12:08 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Thu, 1 Aug 2013 09:26:03 +0000 (12:26 +0300)
image_creator/image.py
image_creator/os_type/__init__.py
image_creator/os_type/linux.py
image_creator/os_type/windows.py

index 2ec52a6..282789d 100644 (file)
@@ -60,6 +60,39 @@ class Image(object):
         self.size = 0
 
         self.g = guestfs.GuestFS()
+        self.guestfs_enabled = False
+
+    def enable(self):
+        """Enable a newly created Image instance"""
+
+        self.enable_guestfs()
+
+        self.out.output('Inspecting Operating System ...', False)
+        roots = self.g.inspect_os()
+        if len(roots) == 0:
+            raise FatalError("No operating system found")
+        if len(roots) > 1:
+            raise FatalError("Multiple operating systems found."
+                             "We only support images with one OS.")
+        self.root = roots[0]
+        self.guestfs_device = self.g.part_to_dev(self.root)
+        self.size = self.g.blockdev_getsize64(self.guestfs_device)
+        self.meta['PARTITION_TABLE'] = \
+            self.g.part_get_parttype(self.guestfs_device)
+
+        self.ostype = self.g.inspect_get_type(self.root)
+        self.distro = self.g.inspect_get_distro(self.root)
+        self.out.success(
+            'found a(n) %s system' %
+            self.ostype if self.distro == "unknown" else self.distro)
+
+    def enable_guestfs(self):
+        """Enable the guestfs handler"""
+
+        if self.guestfs_enabled:
+            self.out.warn("Guestfs is already enabled")
+            return
+
         self.g.add_drive_opts(self.device, readonly=0, format="raw")
 
         # Before version 1.17.14 the recovery process, which is a fork of the
@@ -76,11 +109,6 @@ class Image(object):
         #self.g.set_trace(1)
         #self.g.set_verbose(1)
 
-        self.guestfs_enabled = False
-
-    def enable(self):
-        """Enable a newly created Image instance"""
-
         self.out.output('Launching helper VM (may take a while) ...', False)
         # self.progressbar = self.out.Progress(100, "Launching helper VM",
         #                                     "percent")
@@ -93,24 +121,24 @@ class Image(object):
         # self.progressbar = None
         self.out.success('done')
 
-        self.out.output('Inspecting Operating System ...', False)
-        roots = self.g.inspect_os()
-        if len(roots) == 0:
-            raise FatalError("No operating system found")
-        if len(roots) > 1:
-            raise FatalError("Multiple operating systems found."
-                             "We only support images with one OS.")
-        self.root = roots[0]
-        self.guestfs_device = self.g.part_to_dev(self.root)
-        self.size = self.g.blockdev_getsize64(self.guestfs_device)
-        self.meta['PARTITION_TABLE'] = \
-            self.g.part_get_parttype(self.guestfs_device)
+    def disable_guestfs(self):
+        """Disable the guestfs handler"""
 
-        self.ostype = self.g.inspect_get_type(self.root)
-        self.distro = self.g.inspect_get_distro(self.root)
-        self.out.success(
-            'found a(n) %s system' %
-            self.ostype if self.distro == "unknown" else self.distro)
+        if not self.guestfs_enabled:
+            self.out.warn("Guestfs is already disabled")
+            return
+
+        self.out.output("Shutting down helper VM ...", False)
+        self.g.sync()
+        # guestfs_shutdown which is the prefered way to shutdown the backend
+        # process was introduced in version 1.19.16
+        if check_guestfs_version(self.g, 1, 19, 16) >= 0:
+            self.g.shutdown()
+        else:
+            self.g.kill_subprocess()
+
+        self.guestfs_enabled = False
+        self.out.success('done')
 
     def _get_os(self):
         """Return an OS class instance for this image"""
index 7f8c786..19e63fc 100644 (file)
@@ -355,7 +355,8 @@ class OSBase(object):
         self.meta['OS'] = self.image.g.inspect_get_distro(self.root)
         if self.meta['OS'] == "unknown":
             self.meta['OS'] = self.meta['OSFAMILY']
-        self.meta['DESCRIPTION'] = self.image.g.inspect_get_product_name(self.root)
+        self.meta['DESCRIPTION'] = \
+            self.image.g.inspect_get_product_name(self.root)
 
     def _do_mount(self, readonly):
         """helper method for mount"""
index ada6ab4..31b660f 100644 (file)
@@ -155,9 +155,9 @@ class Linux(Unix):
                         self.out.warn("Acpid action file: %s does not exist" %
                                       action)
                         return
-                    self.image.g.copy_file_to_file(action,
-                                             "%s.orig.snf-image-creator-%d" %
-                                             (action, time.time()))
+                    self.image.g.copy_file_to_file(
+                        action, "%s.orig.snf-image-creator-%d" %
+                        (action, time.time()))
                     self.image.g.write(action, powerbtn_action)
                     return
                 else:
index 994a7b0..a5ef78a 100644 (file)
@@ -122,10 +122,10 @@ class Windows(OSBase):
         self.last_drive = None
         self.system_drive = None
 
-        for drive, partition in self.image.g.inspect_get_drive_mappings(self.root):
-            if partition == "%s%d" % (device, self.last_part_num):
+        for drive, part in self.image.g.inspect_get_drive_mappings(self.root):
+            if part == "%s%d" % (device, self.last_part_num):
                 self.last_drive = drive
-            if partition == self.root:
+            if part == self.root:
                 self.system_drive = drive
 
         assert self.system_drive
@@ -301,16 +301,7 @@ class Windows(OSBase):
         finally:
             self.umount()
 
-        self.out.output("Shutting down helper VM ...", False)
-        self.image.g.sync()
-        # guestfs_shutdown which is the prefered way to shutdown the backend
-        # process was introduced in version 1.19.16
-        if check_guestfs_version(self.g, 1, 19, 16) >= 0:
-            self.image.g.shutdown()
-        else:
-            self.image.g.kill_subprocess()
-
-        self.out.success('done')
+        self.image.disable_guestfs()
 
         vm = None
         monitor = None
@@ -381,10 +372,7 @@ class Windows(OSBase):
                     vm.destroy()
                     self.out.success("done")
             finally:
-                self.out.output("Relaunching helper VM (may take a while) ...",
-                                False)
-                self.image.g.launch()
-                self.out.success('done')
+                self.image.enable_guestfs()
 
                 self.mount(readonly=False)
                 try: