From 354694d8ddf4c75d55e133d7b0da4e5e3c082004 Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Fri, 21 Feb 2014 16:25:56 -0600
Subject: [PATCH 03/13] Fix handling of extended partitions across various
 modes of operation.

When flags.installer_mode is True we only create extended partitions as
needed and we remove them each time we allocate partitions. When
flags.installer_mode is False, extended partitions can be created
explicitly, and can only be removed as part of partition allocation if
they were not explicitly requested.
---
 blivet/partitioning.py | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)

diff --git a/blivet/partitioning.py b/blivet/partitioning.py
index e7fe576..b60bf2f 100644
--- a/blivet/partitioning.py
+++ b/blivet/partitioning.py
@@ -651,23 +651,24 @@ def sizeToSectors(size, sectorSize):
     sectors = int(size / sectorSize)
     return sectors
 
-def removeNewPartitions(disks, partitions):
+def removeNewPartitions(disks, remove, all_partitions):
     """ Remove newly added partitions from disks.
 
         Remove all non-existent partitions from the disks in blivet's model.
 
         :param: disks: list of partitioned disks
         :type disks: list of :class:`~.devices.StorageDevice`
-        :param partitions: list of partitions
-        :type partitions: list of :class:`~.devices.PartitionDevice`
+        :param remove: list of partitions to remove
+        :type remove: list of :class:`~.devices.PartitionDevice`
+        :param all_partitions: list of all partitions on the disks
+        :type all_partitions: list of :class:`~.devices.PartitionDevice`
         :returns: None
         :rtype: NoneType
     """
     log.debug("removing all non-preexisting partitions %s from disk(s) %s",
-                ["%s(id %d)" % (p.name, p.id) for p in partitions
-                                                    if not p.exists],
+                ["%s(id %d)" % (p.name, p.id) for p in remove],
                 [d.name for d in disks])
-    for part in partitions:
+    for part in remove:
         if part.partedPartition and part.disk in disks:
             if part.exists:
                 # we're only removing partitions that don't physically exist
@@ -681,13 +682,12 @@ def removeNewPartitions(disks, partitions):
             part.partedPartition = None
             part.disk = None
 
-    if not flags.installer_mode:
-        return
-
     for disk in disks:
         # remove empty extended so it doesn't interfere
         extended = disk.format.extendedPartition
-        if extended and not disk.format.logicalPartitions:
+        if extended and not disk.format.logicalPartitions and \
+           (flags.installer_mode or
+            extended not in (p.partedPartition for p in all_partitions)):
             log.debug("removing empty extended partition from %s", disk.name)
             disk.format.partedDisk.removePartition(extended)
 
@@ -861,13 +861,6 @@ def doPartitioning(storage):
     partitions = storage.partitions[:]
     for part in storage.partitions:
         part.req_bootable = False
-
-        if part.exists:
-            # if the partition is preexisting or part of a complex device
-            # then we shouldn't modify it
-            partitions.remove(part)
-            continue
-
         if not part.exists:
             # start over with flexible-size requests
             part.req_size = part.req_base_size
@@ -878,7 +871,7 @@ def doPartitioning(storage):
         # there's no stage2 device. hopefully it's temporary.
         pass
 
-    removeNewPartitions(disks, partitions)
+    removeNewPartitions(disks, partitions, partitions)
     free = getFreeRegions(disks)
     try:
         allocatePartitions(storage, disks, partitions, free)
@@ -959,7 +952,7 @@ def allocatePartitions(storage, disks, partitions, freespace):
             disklabels[disk.path] = disk.format
             all_disks[disk.path] = disk
 
-    removeNewPartitions(disks, new_partitions)
+    removeNewPartitions(disks, new_partitions, partitions)
 
     for _part in new_partitions:
         if _part.partedPartition and _part.isExtended:
@@ -2039,7 +2032,8 @@ def growPartitions(disks, partitions, free, size_sets=None):
                 new_partitions.append((new_partition, p.device))
 
             # remove all new partitions from this chunk
-            removeNewPartitions([disk], [r.device for r in chunk.requests])
+            removeNewPartitions([disk], [r.device for r in chunk.requests],
+                                partitions)
             log.debug("back from removeNewPartitions")
 
             # adjust the extended partition as needed
-- 
1.9.0

