dcavalca / rpms / util-linux

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