partitionedfs: introduce add_disks() method
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 3 Jan 2013 11:15:22 +0000 (13:15 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 10 Jan 2013 12:07:46 +0000 (14:07 +0200)
The 'PartitionedMount()' class has an 'add_partition()' method for adding a
partition. Introduce a simialr 'add_disks()' method of adding disks.

Currently the disks are added by the constructor and this is very inflexible.
Indeed, to add a disk, wee need know first create it. To create it - we need to
know its size. To find out its size, we need to sum up all the partition sizes,
take into account the alignment, the MBR size and so on. IOW, do complex
calculations.

This is a preparation for further changes where the disk size calculation will
be done by 'PartitionedMount()' instead. It will tell us the disk size, and
we'll add the disk after adding partitions.

The final goal is to fix the --align bug - when --align is used, the generated
raw disk size is larger than needed - it has unused space at the end.

In this patch we also amend all the users of PartitionedMount to comply to
the new interface.

Change-Id: I0cacfb64410c7b2da634642fbf57d5b4be28acad
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mic/imager/liveusb.py
mic/imager/raw.py
mic/utils/partitionedfs.py
plugins/imager/liveusb_plugin.py
plugins/imager/raw_plugin.py

index d45ac79..fbfb5ab 100644 (file)
@@ -70,7 +70,8 @@ class LiveUSBImageCreator(LiveCDImageCreator):
                                                  % (self._outdir, self.name),
                                              usbimgsize)
         usbmnt = self._mkdtemp("usb-mnt")
-        usbloop = PartitionedMount({'/dev/sdb':disk}, usbmnt)
+        usbloop = PartitionedMount(usbmnt)
+        usbloop.add_disks({'/dev/sdb':disk})
 
         usbloop.add_partition(usbimgsize/1024/1024,
                               "/dev/sdb",
index a1942a7..b3e9256 100644 (file)
@@ -206,7 +206,8 @@ class RawImageCreator(BaseImageCreator):
             disk = fs_related.SparseLoopbackDisk(full_path, item['size'])
             self.__disks[item['name']] = disk
 
-        self.__instloop = PartitionedMount(self.__disks, self._instroot)
+        self.__instloop = PartitionedMount(self._instroot)
+        self.__instloop.add_disks(self.__disks)
 
         for p in parts:
             self.__instloop.add_partition(int(p.size),
index d6f2813..c725ecd 100644 (file)
@@ -32,20 +32,9 @@ MBR_SECTOR_LEN = 1
 SECTOR_SIZE = 512
 
 class PartitionedMount(Mount):
-    def __init__(self, disks, mountdir, skipformat = False):
+    def __init__(self, mountdir, skipformat = False):
         Mount.__init__(self, mountdir)
         self.disks = {}
-        for name in disks.keys():
-            self.disks[name] = { 'disk': disks[name],  # Disk object
-                                 'mapped': False, # True if kpartx mapping exists
-                                 'numpart': 0, # Number of allocate partitions
-                                 'partitions': [], # indexes to self.partitions
-                                 # Partitions with part num higher than 3 will
-                                 # be put inside extended partition.
-                                 'extended': 0, # Size of extended partition
-                                 # Offset of next partition (in sectors)
-                                 'offset': 0 }
-
         self.partitions = []
         self.subvolumes = []
         self.mapped = False
@@ -62,6 +51,21 @@ class PartitionedMount(Mount):
         # Size of a sector used in calculations
         self.sector_size = SECTOR_SIZE
 
+    def add_disks(self, disks):
+        """ Add the disks which have to be partitioned. """
+
+        for name in disks.keys():
+            self.disks[name] = { 'disk': disks[name],  # Disk object
+                                 'mapped': False, # True if kpartx mapping exists
+                                 'numpart': 0, # Number of allocate partitions
+                                 'partitions': [], # indexes to self.partitions
+                                 # Partitions with part num higher than 3 will
+                                 # be put inside extended partition.
+                                 'extended': 0, # Size of extended partition
+                                 # Offset of next partition (in sectors)
+                                 'offset': 0 }
+
+
     def add_partition(self, size, disk, mountpoint, fstype = None, label=None, fsopts = None, boot = False, align = None):
         # Converting MB to sectors for parted
         size = size * 1024 * 1024 / self.sector_size
@@ -358,13 +362,14 @@ class PartitionedMount(Mount):
 
     def cleanup(self):
         Mount.cleanup(self)
-        self.__unmap_partitions()
-        for dev in self.disks.keys():
-            d = self.disks[dev]
-            try:
-                d['disk'].cleanup()
-            except:
-                pass
+        if self.disks:
+            self.__unmap_partitions()
+            for dev in self.disks.keys():
+                d = self.disks[dev]
+                try:
+                    d['disk'].cleanup()
+                except:
+                    pass
 
     def unmount(self):
         self.__unmount_subvolumes()
index 6a12834..91d281c 100644 (file)
@@ -215,7 +215,8 @@ class LiveUSBPlugin(ImagerPlugin):
         imgsize = misc.get_file_size(img) * 1024L * 1024L
         imgmnt = misc.mkdtemp()
         disk = fs_related.SparseLoopbackDisk(img, imgsize)
-        imgloop = PartitionedMount({'/dev/sdb':disk}, imgmnt, skipformat = True)
+        imgloop = PartitionedMount(imgmnt, skipformat = True)
+        imgloop.add_disks({'/dev/sdb':disk})
         imgloop.add_partition(imgsize/1024/1024, "/dev/sdb", "/", "vfat", boot=False)
         try:
             imgloop.mount()
index bcc3f55..3bd76d9 100644 (file)
@@ -135,7 +135,8 @@ class RawPlugin(ImagerPlugin):
         partedcmd = fs_related.find_binary_path("parted")
         disk = fs_related.SparseLoopbackDisk(img, imgsize)
         imgmnt = misc.mkdtemp()
-        imgloop = PartitionedMount({'/dev/sdb':disk}, imgmnt, skipformat = True)
+        imgloop = PartitionedMount(imgmnt, skipformat = True)
+        imgloop.add_disks({'/dev/sdb':disk})
         img_fstype = "ext3"
 
         msger.info("Partition Table:")
@@ -243,8 +244,9 @@ class RawPlugin(ImagerPlugin):
         srcimgsize = (misc.get_file_size(srcimg)) * 1024L * 1024L
         srcmnt = misc.mkdtemp("srcmnt")
         disk = fs_related.SparseLoopbackDisk(srcimg, srcimgsize)
-        srcloop = PartitionedMount({'/dev/sdb':disk}, srcmnt, skipformat = True)
+        srcloop = PartitionedMount(srcmnt, skipformat = True)
 
+        srcloop.add_disks({'/dev/sdb':disk})
         srcloop.add_partition(srcimgsize/1024/1024, "/dev/sdb", "/", "ext3", boot=False)
         try:
             srcloop.mount()