dcavalca / rpms / util-linux

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