[PATCH 2/2] Update partitions' numbers and names when adding new partition (#1166598)

Vratislav Podzimek vpodzime at redhat.com
Wed Nov 26 13:37:32 UTC 2014


Adding a new partition may change partitions' order (e.g. adding sda1 after
sda2) so we need to handle that correctly.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 blivet/devicetree.py        |  9 +++++++++
 blivet/formats/disklabel.py | 26 ++++++++++++++++++++++++--
 python-blivet.spec          |  2 +-
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/blivet/devicetree.py b/blivet/devicetree.py
index f6cf600..98b1251 100644
--- a/blivet/devicetree.py
+++ b/blivet/devicetree.py
@@ -26,6 +26,7 @@ import re
 import shutil
 import pprint
 import copy
+import itertools
 
 from .errors import CryptoError, DeviceError, DeviceTreeError, DiskLabelCommitError, DMError, FSError, InvalidDiskLabelError, LUKSError, MDRaidError, StorageError
 from .devices import BTRFSDevice, BTRFSSubVolumeDevice, BTRFSVolumeDevice, BTRFSSnapShotDevice
@@ -402,6 +403,14 @@ class DeviceTree(object):
         newdev.addHook(new=new)
         self._devices.append(newdev)
 
+        # adding a partition may change partedPartition's paths which are used
+        # to construct names which thus should be updated
+        if isinstance(newdev, PartitionDevice):
+            sibling_parts_lists = [self.getChildren(parent) for parent in newdev.parents]
+            sibling_parts = itertools.chain(*sibling_parts_lists)
+            for part in sibling_parts:
+                part.updateName()
+
         # don't include "req%d" partition names
         if ((newdev.type != "partition" or
              not newdev.name.startswith("req")) and
diff --git a/blivet/formats/disklabel.py b/blivet/formats/disklabel.py
index c69aa04..590b58c 100644
--- a/blivet/formats/disklabel.py
+++ b/blivet/formats/disklabel.py
@@ -311,8 +311,30 @@ class DiskLabel(DeviceFormat):
                                          geometry=geometry)
 
         constraint = parted.Constraint(exactGeom=geometry)
-        self.partedDisk.addPartition(partition=new_partition,
-                                     constraint=constraint)
+
+        # adding a partition the start of which is before some already added
+        # partition requires change of partitions' order because parted just
+        # increments the number while partitions are added
+        if any(part.geometry.start > start for part in self.partedDisk.partitions):
+            parts = self.partedDisk.partitions[:]
+
+            # first remove all partitions
+            for part in parts:
+                self.partedDisk.removePartition(part)
+
+            # then add them back sorted together with the new one
+            parts.append(new_partition)
+            parts.sort(key=lambda p: p.geometry.start)
+            for part in parts:
+                # reset the partition's number so that it gets a new one when
+                # added to partedDisk
+                part.resetNumber()
+                constr = parted.Constraint(exactGeom=part.geometry)
+                self.partedDisk.addPartition(partition=part,
+                                             constraint=constr)
+        else:
+            self.partedDisk.addPartition(partition=new_partition,
+                                         constraint=constraint)
 
     def removePartition(self, partition):
         """ Remove a partition from the disklabel.
diff --git a/python-blivet.spec b/python-blivet.spec
index 3a81c4a..08f5887 100644
--- a/python-blivet.spec
+++ b/python-blivet.spec
@@ -14,7 +14,7 @@ Source0: http://github.com/dwlehman/blivet/archive/%{realname}-%{version}.tar.gz
 %define dmver 1.02.17-6
 %define pykickstartver 1.99.22
 %define partedver 1.8.1
-%define pypartedver 2.5-2
+%define pypartedver 3.10.2
 %define pythonpyblockver 0.45
 %define e2fsver 1.41.0
 %define pythoncryptsetupver 0.1.1
-- 
1.9.3



More information about the anaconda-patches mailing list