Blame SOURCES/0030-libparted-handle-logical-partitions-starting-immedia.patch

9e7f5d
From 535f60c9ef91cc662d5ccaecb2f7048a3c2241d9 Mon Sep 17 00:00:00 2001
9e7f5d
From: Phillip Susi <psusi@ubuntu.com>
9e7f5d
Date: Thu, 12 Jan 2012 14:53:56 -0500
9e7f5d
Subject: [PATCH] libparted: handle logical partitions starting immediately
9e7f5d
 after the EBR
9e7f5d
9e7f5d
_blkpg_add_partition() set the length of the extended partition
9e7f5d
to 2 sectors to allow LILO to be installed there, beacuse the
9e7f5d
linux kernel does this.  If a logical partition used that second
9e7f5d
sector, adding it would fail beacuse of the overlap.  Now
9e7f5d
_blkpg_add_partition() will limit the length to only the first
9e7f5d
sector if the second is used by a logical partition.
9e7f5d
9e7f5d
Previously parted did create the partition table, and after a
9e7f5d
reboot, the kernel would recognize the table, and happily create
9e7f5d
the extended partition as 2 sectors long, thus overlapping the
9e7f5d
logical partition, but when parted tried to recreate the same
9e7f5d
table with BLKPG, the kernel rightly rejected it.
9e7f5d
9e7f5d
(cherry picked from commit f503870153eda7659b09e52e4adeda3bebf06471)
9e7f5d
---
9e7f5d
 NEWS                                            |  8 ++++++++
9e7f5d
 libparted/arch/linux.c                          | 17 +++++++++++++++--
9e7f5d
 tests/t2310-dos-extended-2-sector-min-offset.sh | 16 ++++------------
9e7f5d
 3 files changed, 27 insertions(+), 14 deletions(-)
9e7f5d
9e7f5d
diff --git a/NEWS b/NEWS
9e7f5d
index 4d30b1b..d1ab2a6 100644
9e7f5d
--- a/NEWS
9e7f5d
+++ b/NEWS
9e7f5d
@@ -11,6 +11,14 @@ GNU parted NEWS                                    -*- outline -*-
9e7f5d
 
9e7f5d
 ** Bug Fixes
9e7f5d
 
9e7f5d
+  libparted: handle logical partitions starting immediately after
9e7f5d
+  the EBR.  Creating a logical partition one sector after the EBR
9e7f5d
+  used to cause parted to complain that it could not inform the
9e7f5d
+  kernel of the changes, but after a reboot, everything was fine.
9e7f5d
+  Parted will now correctly inform the kernel of the changes, but
9e7f5d
+  only set the length of the extended partition to 1 sector instead
9e7f5d
+  of two, which would cause it to overlap the logical partition.
9e7f5d
+
9e7f5d
   If a drive was 100 times an even multiple of two, sizes specified as
9e7f5d
   a percentage would trigger the exact placement rule and refuse to round
9e7f5d
   to the nearest half percent.
9e7f5d
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
9e7f5d
index 4daa881..788cdc3 100644
9e7f5d
--- a/libparted/arch/linux.c
9e7f5d
+++ b/libparted/arch/linux.c
9e7f5d
@@ -2371,8 +2371,21 @@ _blkpg_add_partition (PedDisk* disk, const PedPartition *part)
9e7f5d
         memset (&linux_part, 0, sizeof (linux_part));
9e7f5d
         linux_part.start = part->geom.start * disk->dev->sector_size;
9e7f5d
         /* see fs/partitions/msdos.c:msdos_partition(): "leave room for LILO" */
9e7f5d
-        if (part->type & PED_PARTITION_EXTENDED)
9e7f5d
-                linux_part.length = part->geom.length == 1 ? 512 : 1024;
9e7f5d
+        if (part->type & PED_PARTITION_EXTENDED) {
9e7f5d
+                linux_part.length = 1;
9e7f5d
+                if (disk->dev->sector_size == 512) {
9e7f5d
+                        if (linux_part.length == 1)
9e7f5d
+                                linux_part.length = 2;
9e7f5d
+                        PedPartition *walk;
9e7f5d
+                        /* if the second sector is claimed by a logical partition,
9e7f5d
+                           then there's just no room for lilo, so don't try to use it */
9e7f5d
+                        for (walk = part->part_list; walk; walk = walk->next) {
9e7f5d
+                                if (walk->geom.start == part->geom.start+1)
9e7f5d
+                                        linux_part.length = 1;
9e7f5d
+                        }
9e7f5d
+                }
9e7f5d
+                linux_part.length *= disk->dev->sector_size;
9e7f5d
+        }
9e7f5d
         else
9e7f5d
                 linux_part.length = part->geom.length * disk->dev->sector_size;
9e7f5d
         linux_part.pno = part->num;
9e7f5d
diff --git a/tests/t2310-dos-extended-2-sector-min-offset.sh b/tests/t2310-dos-extended-2-sector-min-offset.sh
9e7f5d
index 89453ae..bd7defb 100644
9e7f5d
--- a/tests/t2310-dos-extended-2-sector-min-offset.sh
9e7f5d
+++ b/tests/t2310-dos-extended-2-sector-min-offset.sh
9e7f5d
@@ -1,8 +1,6 @@
9e7f5d
 #!/bin/sh
9e7f5d
-# Ensure that parted leaves at least 2 sectors between the beginning
9e7f5d
+# Ensure that parted allows a single sector between the beginning
9e7f5d
 # of an extended partition and the first logical partition.
9e7f5d
-# Before parted-2.3, it could be made to leave just one, and that
9e7f5d
-# would cause trouble with the Linux kernel.
9e7f5d
 
9e7f5d
 # Copyright (C) 2010-2012 Free Software Foundation, Inc.
9e7f5d
 
9e7f5d
@@ -35,7 +33,7 @@ cat <<EOF > exp || framework_failure
9e7f5d
 BYT;
9e7f5d
 $scsi_dev:2048s:scsi:512:512:msdos:Linux scsi_debug:;
9e7f5d
 1:64s:128s:65s:::lba;
9e7f5d
-5:66s:128s:63s:::;
9e7f5d
+5:65s:128s:64s:::;
9e7f5d
 EOF
9e7f5d
 
9e7f5d
 cat <<EOF > err.exp || framework_failure
9e7f5d
@@ -49,15 +47,9 @@ parted --align=min -s $scsi_dev mkpart extended 64s 128s> out 2>&1 || fail=1
9e7f5d
 parted -m -s $scsi_dev u s print
9e7f5d
 compare /dev/null out || fail=1
9e7f5d
 
9e7f5d
-# Provoke a failure by trying to create a partition that starts just
9e7f5d
+# Trying to create a partition that starts just
9e7f5d
 # one sector after the start of the extended partition.
9e7f5d
-parted --align=min -s $scsi_dev mkpart logical 65s 128s > err 2>&1 && fail=1
9e7f5d
-compare err.exp err || fail=1
9e7f5d
-
9e7f5d
-# The above failed, but created the partition nonetheless.  Remove it.
9e7f5d
-parted -s $scsi_dev rm 5 || fail=1
9e7f5d
-
9e7f5d
-parted --align=min -s $scsi_dev mkpart logical 66s 128s > out 2>&1 || fail=1
9e7f5d
+parted --align=min -s $scsi_dev mkpart logical 65s 128s > out 2>&1 || fail=1
9e7f5d
 compare /dev/null out || fail=1
9e7f5d
 
9e7f5d
 parted -m -s $scsi_dev u s print > out 2>&1
9e7f5d
-- 
9e7f5d
2.1.0
9e7f5d