Save the install root for reuse while creating image.
authorwanchao-xu <wanchao.xu@samsung.com>
Thu, 8 Aug 2024 03:37:22 +0000 (11:37 +0800)
committerwanchao-xu <wanchao.xu@samsung.com>
Mon, 12 Aug 2024 08:05:11 +0000 (16:05 +0800)
Change-Id: I31dd8c0a9fb9a62291f06b0090ae9e4dc295fdfc
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 059e670c20fd52f2640cab50d0d72d5c208c4566..941aadcc6f0aabd43c6bc7d215764d69bc3823a1 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 18f226ca8a65550d74b0d6e1a89066723b97c4c1..dd0c4cddfd7c4242793561e3a5f8b503ffc17210 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 f35ffd81d58330b3da006d7da9da6c216f9b68fb..bb8f134c4498ca6e45ca63105de42d7918284662 100755 (executable)
@@ -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 3c1eb621c7235d6d7121af5e4d0ecfeaad939303..e0f665df7385400fd70b216105090ff27d2bbde5 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')