]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
ibfdisk: (gpt) allow to maximize partition
authorKarel Zak <kzak@redhat.com>
Thu, 20 Nov 2014 12:28:41 +0000 (13:28 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 20 Nov 2014 12:28:41 +0000 (13:28 +0100)
enlarge second partition:

# echo ',+' | ./sfdisk -N2 /dev/sdb
...
Disk /dev/sdb: 100 MiB, 104857600 bytes, 204800 sectors

Old situation:
Device     Start   End Sectors Size Type
/dev/sdb1   2048 22527   20480  10M Linux filesystem
/dev/sdb2  22528 43007   20480  10M Linux filesystem

New situation:
Device     Start    End Sectors Size Type
/dev/sdb1   2048  22527   20480  10M Linux filesystem
/dev/sdb2  22528 204766  182239  89M Linux filesystem

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/gpt.c

index ecf62d2d4f90ed198549a7f66cbc7001873d82a1..d3bc4ae00ef6b8d9124f42e2875abf29733cf673 100644 (file)
@@ -1499,6 +1499,7 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n,
        struct fdisk_gpt_label *gpt;
        struct gpt_entry *e;
        int rc = 0;
+       uint64_t start, end;
 
        assert(cxt);
        assert(cxt->label);
@@ -1509,6 +1510,9 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n,
        if ((uint32_t) n >= le32_to_cpu(gpt->pheader->npartition_entries))
                return -EINVAL;
 
+       FDISK_INIT_UNDEF(start);
+       FDISK_INIT_UNDEF(end);
+
        gpt = self_label(cxt);
        e = &gpt->ents[n];
 
@@ -1550,9 +1554,23 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n,
        }
 
        if (fdisk_partition_has_start(pa))
-               e->lba_start = cpu_to_le64(pa->start);
+               start = pa->start;
        if (fdisk_partition_has_size(pa))
-               e->lba_end = cpu_to_le64(gpt_partition_start(e) + pa->size - 1ULL);
+               end = gpt_partition_start(e) + pa->size - 1ULL;
+
+       if (pa->end_follow_default) {
+               /* enlarge */
+               if (!FDISK_IS_UNDEF(start))
+                       start = gpt_partition_start(e);
+               end = find_last_free(gpt->bheader, gpt->ents, start);
+               if (!end)
+                       FDISK_INIT_UNDEF(end);
+       }
+
+       if (!FDISK_IS_UNDEF(start))
+               e->lba_start = cpu_to_le64(start);
+       if (!FDISK_IS_UNDEF(end))
+               e->lba_end = cpu_to_le64(end);
 
        gpt_recompute_crc(gpt->pheader, gpt->ents);
        gpt_recompute_crc(gpt->bheader, gpt->ents);