Reintroduced the compress-disk-image option from mic2.
authorMarko Saukko <marko.saukko@cybercom.com>
Thu, 15 Dec 2011 08:58:31 +0000 (10:58 +0200)
committerMarko Saukko <marko.saukko@cybercom.com>
Thu, 15 Dec 2011 10:08:04 +0000 (12:08 +0200)
Signed-off-by: Marko Saukko <marko.saukko@cybercom.com>
mic/conf.py
mic/creator.py
mic/imager/baseimager.py
mic/imager/fs.py

index d0d82ba..dc241a0 100644 (file)
@@ -40,6 +40,7 @@ class ConfigMgr(object):
                     "release": None,
                     "logfile": None,
                     "record_pkgs": [],
+                    "compress_disk_image": None,
                 },
                 'chroot': {},
                 'convert': {},
index 9b25e86..333bbfe 100644 (file)
@@ -62,6 +62,7 @@ class Creator(cmdln.Cmdln):
                              help='Record the info of installed packages, multiple values can be specified which joined by ",", valid values: "name", "content", "license"')
         optparser.add_option('', '--pkgmgr', type='string', dest='pkgmgr', default=None, help='Specify backend package manager')
         optparser.add_option('', '--local-pkgs-path', type='string', dest='local_pkgs_path', default=None, help='Path for local pkgs(rpms) to be installed')
+        optparser.add_option('', '--compress-disk-image', type='string', dest='compress_disk_image', default=None, help='Sets the disk image compression. NOTE: The available values might depend on the used filesystem type.')
         return optparser
 
     def preoptparse(self, argv):
@@ -147,6 +148,9 @@ class Creator(cmdln.Cmdln):
         if self.options.pkgmgr is not None:
             configmgr.create['pkgmgr'] = self.options.pkgmgr
 
+        if self.options.compress_disk_image is not None:
+            configmgr.create['compress_disk_image'] = self.options.compress_disk_image
+
     def main(self, argv=None):
         if argv is None:
             argv = sys.argv
index e803de1..9f8f2f8 100644 (file)
@@ -63,6 +63,14 @@ class BaseImageCreator(object):
         self.__builddir = None
         self.__bindmounts = []
 
+        # Eeach image type can change these values as they might be image type
+        # specific
+        if not hasattr(self, "_valid_compression_methods"):
+            self._valid_compression_methods = ['bz2']
+
+        # The compression method for disk image.
+        self._img_compression_method = None
+
         if createopts:
             # A pykickstart.KickstartParser instance."""
             self.ks = createopts['ks']
@@ -88,6 +96,7 @@ class BaseImageCreator(object):
             self.cachedir = createopts['cachedir']
             self.target_arch = createopts['arch']
             self._local_pkgs_path = createopts['local_pkgs_path']
+            self._img_compression_method = createopts['compress_disk_image']
 
         else:
             self.ks = None
@@ -103,34 +112,30 @@ class BaseImageCreator(object):
         #FIXME to be obsolete, make it configurable
         self.distro_name = "Tizen"
 
-        # Output image file names"""
+        # Output image file names
         self.outimage = []
 
-        # A flag to generate checksum"""
+        # A flag to generate checksum
         self._genchecksum = False
 
         self._alt_initrd_name = None
 
-        # the disk image after creation, e.g., bz2.
-        # This value is set with compression_method function. """
-        self.__img_compression_method = None
-
         self._recording_pkgs = []
 
         # available size in root fs, init to 0
         self._root_fs_avail = 0
 
-        # Name of the disk image file that is created. """
+        # Name of the disk image file that is created.
         self._img_name = None
 
         self.image_format = None
 
-        # Save qemu emulator file name in order to clean up it finally """
+        # Save qemu emulator file name in order to clean up it finally
         self.qemu_emulator = None
 
-        # No ks provided when called by convertor, so skip the dependency check """
+        # No ks provided when called by convertor, so skip the dependency check
         if self.ks:
-            # If we have btrfs partition we need to check that we have toosl for those """
+            # If we have btrfs partition we need to check that we have toosl for those
             for part in self.ks.handler.partition.partitions:
                 if part.fstype and part.fstype == "btrfs":
                     self._dep_checks.append("mkfs.btrfs")
@@ -159,6 +164,9 @@ class BaseImageCreator(object):
         if not os.path.exists(self.cachedir):
             os.makedirs(self.cachedir)
 
+        if self._img_compression_method != None and self._img_compression_method not in self._valid_compression_methods:
+            raise CreatorError("Given disk image compression method ('%s') is not valid. Valid values are '%s'." % (self._img_compression_method, ' '.join(self._valid_compression_methods)))
+
     def __del__(self):
         self.cleanup()
 
@@ -998,22 +1006,22 @@ class BaseImageCreator(object):
 
         if not os.path.exists(destdir):
             fs.makedirs(destdir)
-        if self.__img_compression_method:
+        if self._img_compression_method:
             if not self._img_name:
                 raise CreatorError("Image name not set.")
             rc = None
             img_location = os.path.join(self._outdir,self._img_name)
-            if self.__img_compression_method == "bz2":
+            if self._img_compression_method == "bz2":
                 bzip2 = fs.find_binary_path('bzip2')
                 msger.info("Compressing %s with bzip2. Please wait..." % img_location)
                 rc = runner.show([bzip2, "-f", img_location])
                 if rc:
-                    raise CreatorError("Failed to compress image %s with %s." % (img_location, self.__img_compression_method))
+                    raise CreatorError("Failed to compress image %s with %s." % (img_location, self._img_compression_method))
                 for bootimg in glob.glob(os.path.dirname(img_location) + "/*-boot.bin"):
                     msger.info("Compressing %s with bzip2. Please wait..." % bootimg)
                     rc = runner.show([bzip2, "-f", bootimg])
                     if rc:
-                        raise CreatorError("Failed to compress image %s with %s." % (bootimg, self.__img_compression_method))
+                        raise CreatorError("Failed to compress image %s with %s." % (bootimg, self._img_compression_method))
 
         if self._recording_pkgs:
             self._save_recording_pkgs(destdir)
@@ -1137,16 +1145,5 @@ class BaseImageCreator(object):
             shutil.copy(kernel, kernelfilename)
             self.outimage.append(kernelfilename)
 
-    def compress_disk_image(self, compression_method):
-        """
-        With this you can set the method that is used to compress the disk
-        image after it is created.
-        """
-
-        if compression_method not in ('bz2'):
-            raise CreatorError("Given disk image compression method ('%s') is not valid." % (compression_method))
-
-        self.__img_compression_method = compression_method
-
     def get_pkg_manager(self):
         return self.pkgmgr(creator = self)
index 1934469..66348e0 100644 (file)
@@ -20,27 +20,58 @@ import os, sys
 from baseimager import BaseImageCreator
 from mic import msger
 from mic.utils import runner
+from mic.utils.fs_related import *
+from subprocess import call
 
 class FsImageCreator(BaseImageCreator):
     def __init__(self, cfgmgr = None, pkgmgr = None):
+        self._valid_compression_methods = ["tar.bz2"]
         BaseImageCreator.__init__(self, cfgmgr, pkgmgr)
         self._fstype = None
         self._fsopts = None
         self._include_src = False
 
     def package(self, destdir = "."):
+        
+        ignores = ["/dev/fd", "/dev/stdin", "/dev/stdout", "/dev/stderr", "/etc/mtab"]
+
         if not os.path.exists(destdir):
             os.makedirs(destdir)
-        fsdir = os.path.join(destdir, self.name)
-
+        
         if self._recording_pkgs:
             self._save_recording_pkgs(destdir)
 
-        msger.info("Copying %s to %s ..." % (self._instroot, fsdir))
-        runner.show(['cp', "-af", self._instroot, fsdir])
+        if self._img_compression_method == None:
+            fsdir = os.path.join(destdir, self.name)
+
+            msger.info("Copying %s to %s ..." % (self._instroot, fsdir))
+            runner.show(['cp', "-af", self._instroot, fsdir])
+
+            for exclude in ignores:
+                if os.path.exists(fsdir + exclude):
+                    os.unlink(fsdir + exclude)
+
+            self.outimage.append(fsdir)
+        
+        elif self._img_compression_method == "tar.bz2":
+            dst = "%s/%s.tar.bz2" % (destdir, self.name)
+            msger.info("Creating %s (compressing %s with %s). Please wait..." % (dst, self._instroot, self._img_compression_method))
+
+            tar = find_binary_path('tar')
+            tar_cmdline = [tar, "--numeric-owner", "--preserve-permissions", "--preserve-order", "--one-file-system", "--directory", self._instroot]
+            for ignore_entry in ignores:
+                if ignore_entry.startswith('/'):
+                    ignore_entry = ignore_entry[1:]
+                
+                tar_cmdline.append("--exclude=%s" % (ignore_entry))
+            
+            tar_cmdline.extend(["-cjf", dst, "."])
+            
+            rc = call(tar_cmdline)
+            if rc:
+                raise CreatorError("Failed compress image with tar.bz2. Cmdline: %s" % (" ".join(tar_cmdline)))
 
-        for exclude in ["/dev/fd", "/dev/stdin", "/dev/stdout", "/dev/stderr", "/etc/mtab"]:
-            if os.path.exists(fsdir + exclude):
-                os.unlink(fsdir + exclude)
+            self.outimage.append(dst)
 
-        self.outimage.append(fsdir)
+        else:
+            raise CreatorError("Compression method '%s' not supported for 'fs' image format." % (self._img_compression_method))