fix the bug that modify the order of generating image by cpio.
[platform/upstream/mic.git] / mic / imager / baseimager.py
old mode 100644 (file)
new mode 100755 (executable)
index 5ace0e2..e0340a1
@@ -30,13 +30,15 @@ import json
 from datetime import datetime
 
 import rpm
-
+import time
 from mic import kickstart
 from mic import msger, __version__ as VERSION
 from mic.utils.errors import CreatorError, Abort
 from mic.utils import misc, grabber, runner, fs_related as fs
 from mic.chroot import kill_proc_inchroot
 from mic.archive import get_archive_suffixes
+#post script max run time
+MAX_RUN_TIME = 120
 
 class BaseImageCreator(object):
     """Installs a system to a chroot directory.
@@ -152,28 +154,50 @@ class BaseImageCreator(object):
                 if part.fstype and part.fstype == "btrfs":
                     self._dep_checks.append("mkfs.btrfs")
                     break
+                if part.fstype == "cpio":
+                    part.fstype = "ext4"
             if len(self.ks.handler.partition.partitions) > 1:
                 self.multiple_partitions = True
 
-        if self.target_arch and self.target_arch.startswith("arm"):
-            for dep in self._dep_checks:
-                if dep == "extlinux":
-                    self._dep_checks.remove(dep)
-
-            if not os.path.exists("/usr/bin/qemu-arm") or \
-               not misc.is_statically_linked("/usr/bin/qemu-arm"):
-                self._dep_checks.append("qemu-arm-static")
-
-            if os.path.exists("/proc/sys/vm/vdso_enabled"):
-                vdso_fh = open("/proc/sys/vm/vdso_enabled","r")
-                vdso_value = vdso_fh.read().strip()
-                vdso_fh.close()
-                if (int)(vdso_value) == 1:
-                    msger.warning("vdso is enabled on your host, which might "
-                        "cause problems with arm emulations.\n"
-                        "\tYou can disable vdso with following command before "
-                        "starting image build:\n"
-                        "\techo 0 | sudo tee /proc/sys/vm/vdso_enabled")
+        if self.target_arch:
+            if self.target_arch.startswith("arm"):
+                for dep in self._dep_checks:
+                    if dep == "extlinux":
+                        self._dep_checks.remove(dep)
+
+                if not os.path.exists("/usr/bin/qemu-arm") or \
+                   not misc.is_statically_linked("/usr/bin/qemu-arm"):
+                    self._dep_checks.append("qemu-arm-static")
+
+                if os.path.exists("/proc/sys/vm/vdso_enabled"):
+                    vdso_fh = open("/proc/sys/vm/vdso_enabled","r")
+                    vdso_value = vdso_fh.read().strip()
+                    vdso_fh.close()
+                    if (int)(vdso_value) == 1:
+                        msger.warning("vdso is enabled on your host, which might "
+                            "cause problems with arm emulations.\n"
+                            "\tYou can disable vdso with following command before "
+                            "starting image build:\n"
+                            "\techo 0 | sudo tee /proc/sys/vm/vdso_enabled")
+            elif self.target_arch == "mipsel":
+                for dep in self._dep_checks:
+                    if dep == "extlinux":
+                        self._dep_checks.remove(dep)
+
+                if not os.path.exists("/usr/bin/qemu-mipsel") or \
+                   not misc.is_statically_linked("/usr/bin/qemu-mipsel"):
+                    self._dep_checks.append("qemu-mipsel-static")
+
+                if os.path.exists("/proc/sys/vm/vdso_enabled"):
+                    vdso_fh = open("/proc/sys/vm/vdso_enabled","r")
+                    vdso_value = vdso_fh.read().strip()
+                    vdso_fh.close()
+                    if (int)(vdso_value) == 1:
+                        msger.warning("vdso is enabled on your host, which might "
+                            "cause problems with mipsel emulations.\n"
+                            "\tYou can disable vdso with following command before "
+                            "starting image build:\n"
+                            "\techo 0 | sudo tee /proc/sys/vm/vdso_enabled")
 
         # make sure the specified tmpdir and cachedir exist
         if not os.path.exists(self.tmpdir):
@@ -682,7 +706,7 @@ class BaseImageCreator(object):
         return self.cachedir
 
     def __sanity_check(self):
-        """Ensure that the config we've been given is sane."""
+        """Ensure that the config we've been given is same."""
         if not (kickstart.get_packages(self.ks) or
                 kickstart.get_groups(self.ks)):
             raise CreatorError("No packages or groups specified")
@@ -1004,7 +1028,14 @@ class BaseImageCreator(object):
                         continue
              
             return False
-            
+
+        def showErrorInfo(filepath):
+            if os.path.isfile(filepath):
+                for line in open(filepath):
+                    msger.info("The error install package info: %s" % line)
+            else:
+                msger.info("%s is not found." % filepath)
+
         def get_ssl_verify(ssl_verify=None):
             if ssl_verify is not None:
                 return not ssl_verify.lower().strip() == 'no'
@@ -1086,6 +1117,7 @@ class BaseImageCreator(object):
             pkg_manager.close()
 
         if checkScriptletError(self._instroot + "/tmp/.postscript/error/", "_error"):
+            showErrorInfo(self._instroot + "/tmp/.preload_install_error")
             raise CreatorError('scriptlet errors occurred')
             
         # hook post install
@@ -1128,6 +1160,7 @@ class BaseImageCreator(object):
                 preexec = self._chroot
                 script = "/tmp/" + os.path.basename(path)
 
+            start_time = time.time()
             try:
                 try:
                     p = subprocess.Popen([s.interp, script],
@@ -1135,8 +1168,11 @@ class BaseImageCreator(object):
                                        env = env,
                                        stdout = subprocess.PIPE,
                                        stderr = subprocess.STDOUT)
-                    for entry in p.communicate()[0].splitlines():
-                        msger.info(entry)
+                    while p.poll() == None:
+                       msger.info(p.stdout.readline().strip())
+                        end_time = time.time()
+                        if (end_time - start_time)/60 > MAX_RUN_TIME:
+                            raise CreatorError("Your post script is executed more than "+MAX_RUN_TIME+"mins, please check it!")
                 except OSError, (err, msg):
                     raise CreatorError("Failed to execute %%post script "
                                        "with '%s' : %s" % (s.interp, msg))
@@ -1221,6 +1257,38 @@ class BaseImageCreator(object):
                         pass
                 self._instloops.remove(item)
 
+    def create_cpio_image(self):
+        for item in self._instloops:
+            if item['cpioopts']:
+                msger.info("Create image by cpio.")
+                tmp_cpio = self.__builddir + "/tmp-cpio"
+                if not os.path.exists(tmp_cpio):
+                    os.mkdir(tmp_cpio)
+                tmp_cpio_imgfile = os.path.join(tmp_cpio, item['name'])
+                try:
+                    cpiocmd = fs.find_binary_path('cpio')
+                    if cpiocmd:
+                        oldoutdir = os.getcwd()
+                        os.chdir(os.path.join(self._instroot, item['mountpoint'].lstrip('/')))
+                        # find . | cpio --create --'format=newc' | gzip > ../ramdisk.img
+                        runner.show('find . | cpio --create %s | gzip > %s' % (item['cpioopts'], tmp_cpio_imgfile))
+                        os.chdir(oldoutdir)
+                except OSError, (errno, msg):
+                    raise errors.CreatorError("Create image by cpio error: %s" % msg)
+
+    def copy_cpio_image(self):
+        for item in self._instloops:
+            if item['cpioopts']:
+                tmp_cpio = self.__builddir + "/tmp-cpio"
+                msger.info("Copy cpio image from %s to %s." %(tmp_cpio, self._imgdir))
+                try:
+                    shutil.copyfile(os.path.join(tmp_cpio, item['name']),os.path.join(self._imgdir, item['name']))
+                except IOError:
+                    raise errors.CreatorError("Copy cpio image error")
+                os.remove(os.path.join(tmp_cpio, item['name']))
+                if not os.listdir(tmp_cpio):
+                    shutil.rmtree(tmp_cpio, ignore_errors=True)
+
     def package(self, destdir = "."):
         """Prepares the created image for final delivery.