From: Karel Zak Date: Thu, 20 Nov 2014 12:28:41 +0000 (+0100) Subject: ibfdisk: (gpt) allow to maximize partition X-Git-Tag: v2.26-rc1~189 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9146a00823925f318fb7b71f0db0feb0d71d06ba;p=thirdparty%2Futil-linux.git ibfdisk: (gpt) allow to maximize partition 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 --- diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index ecf62d2d4f..d3bc4ae00e 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -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);