summaryrefslogtreecommitdiffstats
path: root/disk-utils/partx.c
diff options
context:
space:
mode:
authorScott Moser2014-01-13 21:32:49 +0100
committerKarel Zak2014-01-14 09:25:49 +0100
commit84ac311212c06963d616b1b9a40644842f9adabd (patch)
tree0aaf0a8dff61581822aeff9762f5a48bfc521171 /disk-utils/partx.c
parentmount: update man page ext3/4 mount options (diff)
downloadkernel-qcow2-util-linux-84ac311212c06963d616b1b9a40644842f9adabd.tar.gz
kernel-qcow2-util-linux-84ac311212c06963d616b1b9a40644842f9adabd.tar.xz
kernel-qcow2-util-linux-84ac311212c06963d616b1b9a40644842f9adabd.zip
partx: fix --update ranges and out of order tables
partx --update DEVICE NUMBER was broken in 2 cases: * if NUMBER != 1 * if the partition table was "out of order". Ie, where sda2 came after sda3. References: https://bugs.launchpad.net/ubuntu/+source/cloud-utils/+bug/1244662 Signed-off-by: Scott Moser <smoser@ubuntu.com> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'disk-utils/partx.c')
-rw-r--r--disk-utils/partx.c75
1 files changed, 50 insertions, 25 deletions
diff --git a/disk-utils/partx.c b/disk-utils/partx.c
index 880d77962..df03e591e 100644
--- a/disk-utils/partx.c
+++ b/disk-utils/partx.c
@@ -412,10 +412,41 @@ static void upd_parts_warnx(const char *device, int first, int last)
device, first, last);
}
+/**
+ * get_partition_by_partno:
+ * @ls: partitions list
+ * @n: the partition number (e.g. 'N' from sda'N')
+ *
+ * This does not assume any order of the input blkid_partlist.
+ * And correctly handles "out of order" partition tables.
+ * partition N is located after partition N+1 on the disk.
+ *
+ * Returns: partition object or NULL in case or error.
+ */
+blkid_partition get_partition_by_partno(blkid_partlist ls, int n)
+{
+ int i, nparts;
+ blkid_partition par;
+ if (!ls)
+ return NULL;
+
+ nparts = blkid_partlist_numof_partitions(ls);
+ if (nparts < 0)
+ return NULL;
+
+ for (i = 0; i < nparts; i++) {
+ par = blkid_partlist_get_partition(ls, i);
+ if (n == blkid_partition_get_partno(par)) {
+ return par;
+ }
+ }
+ return NULL;
+}
+
static int upd_parts(int fd, const char *device, dev_t devno,
blkid_partlist ls, int lower, int upper)
{
- int i, n, an, nparts, rc = 0, errfirst = 0, errlast = 0, err;
+ int n, nparts, rc = 0, errfirst = 0, errlast = 0, err;
blkid_partition par;
uintmax_t start, size;
@@ -441,18 +472,16 @@ static int upd_parts(int fd, const char *device, dev_t devno,
return -1;
}
- for (i = 0, n = lower; n <= upper; n++) {
- par = blkid_partlist_get_partition(ls, i);
- an = blkid_partition_get_partno(par);
-
- if (lower && n < lower)
- continue;
- if (upper && n > upper)
+ for (n = lower; n <= upper; n++) {
+ par = get_partition_by_partno(ls, n);
+ if (!par) {
+ if (verbose)
+ warn(_("%s: no partition #%d"), device, n);
continue;
+ }
start = blkid_partition_get_start(par);
size = blkid_partition_get_size(par);
-
if (blkid_partition_is_extended(par))
/*
* Let's follow the Linux kernel and reduce
@@ -463,25 +492,21 @@ static int upd_parts(int fd, const char *device, dev_t devno,
err = partx_del_partition(fd, n);
if (err == -1 && errno == ENXIO)
err = 0; /* good, it already doesn't exist */
- if (an == n)
+ if (err == -1 && errno == EBUSY)
{
- if (i < nparts)
- i++;
- if (err == -1 && errno == EBUSY)
- {
- /* try to resize */
- err = partx_resize_partition(fd, n, start, size);
- if (verbose)
- printf(_("%s: partition #%d resized\n"), device, n);
- if (err == 0)
- continue;
- }
- if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
- if (verbose)
- printf(_("%s: partition #%d added\n"), device, n);
+ /* try to resize */
+ err = partx_resize_partition(fd, n, start, size);
+ if (verbose)
+ printf(_("%s: partition #%d resized\n"), device, n);
+ if (err == 0)
continue;
- }
}
+ if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
+ if (verbose)
+ printf(_("%s: partition #%d added\n"), device, n);
+ continue;
+ }
+
if (err == 0)
continue;
rc = -1;