partitionedfs: separate out partitions placing
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 4 Jan 2013 10:29:16 +0000 (12:29 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 10 Jan 2013 12:13:17 +0000 (14:13 +0200)
Create a new method which lays out (or places) partitions to the disks. It will
make it possible to find out disk size before creating a disk. Indeed, this
function places all partitions, so the end of the last partition is the size of
the disk. So the user will have to be able to do this:

1. Add all partitions
2. Layout partitions
3. Get the disk size
4. Create the disk
5. Add the disk and format/mount it

And no more buggy heuristics for disk size are needed.

Change-Id: Ib6edf5e57d3d03f4a84d3f46eefad7d25bc793ec
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mic/utils/partitionedfs.py

index a8545eb..d8b1d1c 100644 (file)
@@ -50,6 +50,7 @@ class PartitionedMount(Mount):
         self.snapshot_created = self.skipformat
         # Size of a sector used in calculations
         self.sector_size = SECTOR_SIZE
+        self._partitions_layed_out = False
 
     def __add_disk(self, disk_name):
         """ Add a disk 'disk_name' to the internal list of disks. Note,
@@ -60,6 +61,8 @@ class PartitionedMount(Mount):
             # We already have this disk
             return
 
+        assert not self._partitions_layed_out
+
         self.disks[disk_name] = \
                 { 'disk': None,     # Disk object
                   'mapped': False,  # True if kpartx mapping exists
@@ -81,6 +84,8 @@ class PartitionedMount(Mount):
         """ This is a helper function for 'add_partition()' which adds a
         partition to the internal list of partitions. """
 
+        assert not self._partitions_layed_out
+
         self.partitions.append(part)
         self.__add_disk(part['disk'])
 
@@ -153,9 +158,16 @@ class PartitionedMount(Mount):
             msger.debug('"parted" output: %s' % out)
         return rc
 
-    def __format_disks(self):
+    def layout_partitions(self):
+        """ Layout the partitions, meaning calculate the position of every
+        partition on the disk. """
+
         msger.debug("Assigning partitions to disks")
 
+        if self._partitions_layed_out:
+            return
+
+        self._partitions_layed_out = True
         mbr_sector_skipped = False
 
         # Go through partitions in the order they are added in .ks file
@@ -227,6 +239,9 @@ class PartitionedMount(Mount):
                                          p['start'], p['size'],
                                          p['size'] * self.sector_size))
 
+    def __format_disks(self):
+        self.layout_partitions()
+
         if self.skipformat:
             msger.debug("Skipping disk format, because skipformat flag is set.")
             return