Save the install root for reuse while creating image.
authorwanchao-xu <wanchao.xu@samsung.com>
Thu, 8 Aug 2024 03:30:43 +0000 (11:30 +0800)
committerwanchao-xu <wanchao.xu@samsung.com>
Tue, 13 Aug 2024 01:35:05 +0000 (09:35 +0800)
Change-Id: I1a467461d15d03eb1575784847b72135add82929
Signed-off-by: wanchao-xu <wanchao.xu@samsung.com>
mic/imager/baseimager.py
mic/imager/loop.py
plugins/imager/loop_plugin.py
tools/mic

index 2602c60a1a66b14de6b49dc0b027a128b4e4d866..e3669ed2e279e25945bc97c9b2a18104ec828c26 100644 (file)
@@ -533,7 +533,8 @@ class BaseImageCreator(object):
         if not in_chroot:
             env["INSTALL_ROOT"] = self._instroot
             env["IMG_NAME"] = self._name
-            env['ATTACHMENT_PATHS'] = ' '.join(self._attachment)
+            if hasattr(self, '_attachment') and self._attachment:
+                env['ATTACHMENT_PATHS'] = ' '.join(self._attachment)
             if self._imgdir:
                 env['IMG_DIR_PATH'] = str(self._imgdir)
 
index ed0dcedb415d59eced078b001ddf43f08298b528..f732e8bb2d40f84e613d65ee53da301b610d2625 100644 (file)
@@ -105,7 +105,8 @@ class LoopImageCreator(BaseImageCreator):
 
     def __init__(self, creatoropts=None, pkgmgr=None,
                  compress_image=None,
-                 shrink_image=False):
+                 shrink_image=False,
+                 reuse_environment=False):
         """Initialize a LoopImageCreator instance.
 
         This method takes the same arguments as ImageCreator.__init__()
@@ -118,6 +119,7 @@ class LoopImageCreator(BaseImageCreator):
 
         self.compress_image = compress_image
         self.shrink_image = shrink_image
+        self.reuse_environment = reuse_environment
 
         self.__fslabel = None
         self.fslabel = self.name
@@ -339,6 +341,37 @@ class LoopImageCreator(BaseImageCreator):
         if self._imgdir is None:
             self._imgdir = self._mkdtemp()
 
+    def _get_instroot_cache(self):
+        return os.path.join(self.get_cachedir(), "instroot")
+
+    def _check_instroot_cache(self):
+        cachedir = self._get_instroot_cache()
+        if not os.path.exists(cachedir):
+            return False
+
+        for loop in self._instloops:
+            image = os.path.join(cachedir, loop['name'])
+            if not os.path.exists(image):
+                return False
+
+        return True
+
+    def save_install_root(self):
+        if self._imgdir is None:
+            raise CreatorError("image dir is invalid while save install root")
+
+        cachedir = self._get_instroot_cache()
+        if os.path.exists(cachedir):
+            if os.path.isdir(cachedir):
+                shutil.rmtree(cachedir)
+            else:
+                os.unlink(cachedir)
+
+        try:
+            shutil.copytree(self._imgdir, cachedir)
+        except (OSError, IOError) as e:
+            shutil.rmtree(cachedir, ignore_errors=True)
+            raise CreatorError("Cannot save install root : %s" % str(e))
 
     #
     # Actual implementation
@@ -367,6 +400,13 @@ class LoopImageCreator(BaseImageCreator):
 
         self._check_imgdir()
 
+        if self.reuse_environment and self._check_instroot_cache():
+            cachedir = self._get_instroot_cache()
+            for file in os.listdir(cachedir):
+                shutil.move(os.path.join(cachedir, file), self._imgdir)
+        else:
+            self.reuse_environment = False
+
         for loop in self._instloops:
             fstype = loop['fstype']
             fsopt = loop['fsopts']
index dc0bfd95503180dfb5a50f139fcd4e9811fe124e..57e8ae48042e80f6636cd2a46fc32262e7920d69 100644 (file)
@@ -39,7 +39,8 @@ class LoopPlugin(ImagerPlugin):
         creator = LoopImageCreator(creatoropts,
                                    pkgmgr,
                                    args.compress_image,
-                                   args.shrink)
+                                   args.shrink,
+                                   args.reuse_environment)
 
         if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
@@ -54,8 +55,12 @@ class LoopPlugin(ImagerPlugin):
         try:
             creator.check_depend_tools()
             creator.mount(None, creatoropts["cachedir"])
-            creator.install()
-            creator.tpkinstall()
+            if creator.reuse_environment:
+                msger.info("Re-install the packages which are modified")
+            else:
+                creator.install()
+                creator.tpkinstall()
+            creator.save_install_root()
             creator.configure(creatoropts["repomd"])
             creator.copy_kernel()
             creator.create_cpio_image()
index df8fe53c98e90562a3f5d3a69d716c0ae56b4c13..96caf89d905172b0f03e1416d5e0214c348218f5 100755 (executable)
--- a/tools/mic
+++ b/tools/mic
@@ -167,6 +167,12 @@ def create_parser(parser):
                              help="Compress all loop images with 'gz' or 'bz2'")
     loop_parser.add_argument("--shrink", action='store_true', default=False,
                   help="Whether to shrink loop images to minimal size")
+    loop_parser.add_argument('--reuse-environment', action='store_true', default=False,
+                             help='Reuse the image creator environment of mic')
+    group = loop_parser.add_argument_group('only for reuse create environment option')
+    group.add_argument('--reinsrall-pkgs', action='store', dest='reinstall_pkgs', default=[],
+                       help='Reinstall the given packages to re-create image, '
+                            'packages should be separated by comma')
                   
     qcow_parser = subparsers.add_parser('qcow', parents=[parent_parser], help='create qcow image')