dcavalca / rpms / util-linux

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