Add init_expand option to partition
authorDohyung Kim <dohyung2.kim@samsung.com>
Tue, 26 Dec 2017 09:36:57 +0000 (18:36 +0900)
committeryuhuan.yang <yuhuan.yang@samsung.com>
Tue, 30 Jan 2018 03:04:48 +0000 (11:04 +0800)
  - if init_expand is enabled, expand partition size(* 1.5) before mount
  - check partition size after running resize2fs to verify partition size

Change-Id: I60e889f04368877b9d74a581279c9ea4f5d33217
Signed-off-by: Dohyung Kim <dohyung2.kim@samsung.com>
mic/imager/loop.py
mic/kickstart/custom_commands/partition.py
mic/utils/fs_related.py

index 7fb5024..1e44343 100755 (executable)
@@ -170,6 +170,7 @@ class LoopImageCreator(BaseImageCreator):
                     'kspart' : part,
                     'exclude_image' : part.exclude_image or None,
                     'no_shrink': part.no_shrink or False,
+                    'init_expand': part.init_expand or False,
                     })
             self._instloops = allloops
 
@@ -387,7 +388,7 @@ class LoopImageCreator(BaseImageCreator):
             try:
                 msger.verbose('Mounting image "%s" on "%s"' % (imgname, mp))
                 fs.makedirs(mp)
-                loop['loop'].mount()
+                loop['loop'].mount(init_expand=loop['init_expand'])
                 # Make an autogenerated uuid avaialble in _get_post_scripts_env()
                 if loop['kspart'] and loop['kspart'].uuid is None and \
                    loop['loop'].uuid:
index d0385b7..341e8dc 100755 (executable)
@@ -33,6 +33,7 @@ class Mic_PartData(FC4_PartData):
         self.squashfsopts = kwargs.get("squashfsopts", None)
         self.cpioopts = kwargs.get("cpioopts", None)
         self.no_shrink = kwargs.get("no_shrink", False)
+        self.init_expand = kwargs.get("init_expand", False)
 
     def _getArgsAsStr(self):
         retval = FC4_PartData._getArgsAsStr(self)
@@ -55,6 +56,8 @@ class Mic_PartData(FC4_PartData):
             retval += " --cpiooptions=%s" % self.cpioopts
         if self.no_shrink:
             retval += " --no-shrink"
+        if self.init_expand:
+            retval += " --init-expand"
         return retval
 
 class Mic_Partition(FC4_Partition):
@@ -81,4 +84,5 @@ class Mic_Partition(FC4_Partition):
         op.add_option("--cpiooptions", type="string", action="store", dest="cpioopts",
                       default=None)
         op.add_option("--no-shrink", action="store_true", dest="no_shrink", default=False)
+        op.add_option("--init-expand", action="store_true", dest="init_expand", default=False)
         return op
index cee4c8a..0040fa2 100644 (file)
@@ -304,6 +304,17 @@ class LoopbackDisk(Disk):
         rc = runner.show([self.losetupcmd, "-d", self.device])
         self.device = None
 
+    def reread_size(self):
+        if self.device is None:
+            return
+        msger.debug("Reread size %s" % self.device)
+        rc = runner.show([self.losetupcmd, "-c", self.device])
+        # XXX: (WORKAROUND) re-setup loop device when losetup isn't support '-c' option.
+        if rc != 0:
+            msger.debug("Fail to reread size, Try to re-setup loop device to reread the size of the file associated")
+            runner.show([self.losetupcmd, "-d", self.device])
+            runner.show([self.losetupcmd, self.device, self.lofile])
+
 class SparseLoopbackDisk(LoopbackDisk):
     """A Disk backed by a sparse file via the loop module."""
     def __init__(self, lofile, size):
@@ -345,6 +356,9 @@ class SparseLoopbackDisk(LoopbackDisk):
         os.close(fd)
 
     def create(self):
+        if self.device is not None:
+            return
+
         self.expand(create = True)
         LoopbackDisk.create(self)
 
@@ -488,6 +502,9 @@ class ExtDiskMount(DiskMount):
         self.__fsck()
 
         resize2fs(self.disk.lofile, size)
+        if size and size != os.stat(self.disk.lofile)[stat.ST_SIZE]:
+            raise MountError("Failed to resize filesystem %s to " % (self.disk.lofile, size))
+
         return size
 
     def __create(self):
@@ -502,8 +519,13 @@ class ExtDiskMount(DiskMount):
         else:
             self.__format_filesystem()
 
-    def mount(self, options = None):
+    def mount(self, options = None, init_expand = False):
         self.__create()
+        if init_expand:
+            expand_size = long(self.disk.size * 1.5)
+            msger.info("Initial partition size expanded : %ld -> %ld" % (self.disk.size, expand_size))
+            self.__resize_filesystem(expand_size)
+            self.disk.reread_size()
         DiskMount.mount(self, options)
 
     def __fsck(self):
@@ -599,8 +621,13 @@ class VfatDiskMount(DiskMount):
         else:
             self.__format_filesystem()
 
-    def mount(self, options = None):
+    def mount(self, options = None, init_expand = False):
         self.__create()
+        if init_expand:
+            expand_size = long(self.disk.size * 1.5)
+            msger.info("Initial partition size expanded : %ld -> %ld" % (self.disk.size, expand_size))
+            self.__resize_filesystem(expand_size)
+            self.disk.reread_size()
         DiskMount.mount(self, options)
 
     def __fsck(self):
@@ -702,8 +729,13 @@ class BtrfsDiskMount(DiskMount):
         else:
             self.__format_filesystem()
 
-    def mount(self, options = None):
+    def mount(self, options = None, init_expand = False):
         self.__create()
+        if init_expand:
+            expand_size = long(self.disk.size * 1.5)
+            msger.info("Initial partition size expanded : %ld -> %ld" % (self.disk.size, expand_size))
+            self.__resize_filesystem(expand_size)
+            self.disk.reread_size()
         DiskMount.mount(self, options)
 
     def __fsck(self):