3df8c3
From 5cc378e4cdeb957b405e0264a09295eda7d75ff7 Mon Sep 17 00:00:00 2001
3df8c3
From: Scott Moser <smoser@ubuntu.com>
3df8c3
Date: Mon, 13 Jan 2014 15:32:49 -0500
3df8c3
Subject: [PATCH] partx: fix --update ranges and out of order tables
3df8c3
3df8c3
partx --update DEVICE NUMBER
3df8c3
was broken in 2 cases:
3df8c3
 * if NUMBER != 1
3df8c3
 * if the partition table was "out of order".
3df8c3
   Ie, where sda2 came after sda3.
3df8c3
3df8c3
References: https://bugs.launchpad.net/ubuntu/+source/cloud-utils/+bug/1244662
3df8c3
Signed-off-by: Scott Moser <smoser@ubuntu.com>
3df8c3
Signed-off-by: Karel Zak <kzak@redhat.com>
3df8c3
---
3df8c3
 disk-utils/partx.c | 75 ++++++++++++++++++++++++++++++++++++------------------
3df8c3
 1 file changed, 50 insertions(+), 25 deletions(-)
3df8c3
3df8c3
diff --git a/disk-utils/partx.c b/disk-utils/partx.c
3df8c3
index 880d779..df03e59 100644
3df8c3
--- a/disk-utils/partx.c
3df8c3
+++ b/disk-utils/partx.c
3df8c3
@@ -412,10 +412,41 @@ static void upd_parts_warnx(const char *device, int first, int last)
3df8c3
 				device, first, last);
3df8c3
 }
3df8c3
 
3df8c3
+/**
3df8c3
+ * get_partition_by_partno:
3df8c3
+ * @ls: partitions list
3df8c3
+ * @n: the partition number (e.g. 'N' from sda'N')
3df8c3
+ *
3df8c3
+ * This does not assume any order of the input blkid_partlist.
3df8c3
+ * And correctly handles "out of order" partition tables.
3df8c3
+ * partition N is located after partition N+1 on the disk.
3df8c3
+ *
3df8c3
+ * Returns: partition object or NULL in case or error.
3df8c3
+ */
3df8c3
+blkid_partition get_partition_by_partno(blkid_partlist ls, int n)
3df8c3
+{
3df8c3
+	int i, nparts;
3df8c3
+	blkid_partition par;
3df8c3
+	if (!ls)
3df8c3
+		return NULL;
3df8c3
+
3df8c3
+	nparts = blkid_partlist_numof_partitions(ls);
3df8c3
+	if (nparts < 0)
3df8c3
+		return NULL;
3df8c3
+
3df8c3
+	for (i = 0; i < nparts; i++) {
3df8c3
+		par = blkid_partlist_get_partition(ls, i);
3df8c3
+		if (n == blkid_partition_get_partno(par)) {
3df8c3
+			return par;
3df8c3
+		}
3df8c3
+	}
3df8c3
+	return NULL;
3df8c3
+}
3df8c3
+
3df8c3
 static int upd_parts(int fd, const char *device, dev_t devno,
3df8c3
 		     blkid_partlist ls, int lower, int upper)
3df8c3
 {
3df8c3
-	int i, n, an, nparts, rc = 0, errfirst = 0, errlast = 0, err;
3df8c3
+	int n, nparts, rc = 0, errfirst = 0, errlast = 0, err;
3df8c3
 	blkid_partition par;
3df8c3
 	uintmax_t start, size;
3df8c3
 
3df8c3
@@ -441,18 +472,16 @@ static int upd_parts(int fd, const char *device, dev_t devno,
3df8c3
 		return -1;
3df8c3
 	}
3df8c3
 
3df8c3
-	for (i = 0, n = lower; n <= upper; n++) {
3df8c3
-		par = blkid_partlist_get_partition(ls, i);
3df8c3
-		an = blkid_partition_get_partno(par);
3df8c3
-
3df8c3
-		if (lower && n < lower)
3df8c3
-			continue;
3df8c3
-		if (upper && n > upper)
3df8c3
+	for (n = lower; n <= upper; n++) {
3df8c3
+		par = get_partition_by_partno(ls, n);
3df8c3
+		if (!par) {
3df8c3
+			if (verbose)
3df8c3
+				warn(_("%s: no partition #%d"), device, n);
3df8c3
 			continue;
3df8c3
+		}
3df8c3
 
3df8c3
 		start = blkid_partition_get_start(par);
3df8c3
 		size =  blkid_partition_get_size(par);
3df8c3
-
3df8c3
 		if (blkid_partition_is_extended(par))
3df8c3
 			/*
3df8c3
 			 * Let's follow the Linux kernel and reduce
3df8c3
@@ -463,25 +492,21 @@ static int upd_parts(int fd, const char *device, dev_t devno,
3df8c3
 		err = partx_del_partition(fd, n);
3df8c3
 		if (err == -1 && errno == ENXIO)
3df8c3
 			err = 0; /* good, it already doesn't exist */
3df8c3
-		if (an == n)
3df8c3
+		if (err == -1 && errno == EBUSY)
3df8c3
 		{
3df8c3
-			if (i < nparts)
3df8c3
-				i++;
3df8c3
-			if (err == -1 && errno == EBUSY)
3df8c3
-			{
3df8c3
-				/* try to resize */
3df8c3
-				err = partx_resize_partition(fd, n, start, size);
3df8c3
-				if (verbose)
3df8c3
-					printf(_("%s: partition #%d resized\n"), device, n);
3df8c3
-				if (err == 0)
3df8c3
-					continue;
3df8c3
-			}
3df8c3
-			if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
3df8c3
-				if (verbose)
3df8c3
-					printf(_("%s: partition #%d added\n"), device, n);
3df8c3
+			/* try to resize */
3df8c3
+			err = partx_resize_partition(fd, n, start, size);
3df8c3
+			if (verbose)
3df8c3
+				printf(_("%s: partition #%d resized\n"), device, n);
3df8c3
+			if (err == 0)
3df8c3
 				continue;
3df8c3
-			}
3df8c3
 		}
3df8c3
+		if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
3df8c3
+			if (verbose)
3df8c3
+				printf(_("%s: partition #%d added\n"), device, n);
3df8c3
+			continue;
3df8c3
+		}
3df8c3
+
3df8c3
 		if (err == 0)
3df8c3
 			continue;
3df8c3
 		rc = -1;
3df8c3
-- 
3df8c3
1.9.3
3df8c3