From 1116884cf7de88727845c0ee5c2ee7cb440e44d7 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 6 Sep 2019 13:09:30 +0200 Subject: [PATCH] libfdisk: use grain as small as possible The current implementation does not allow to move partition for example in +/-1 sector range, because free space analyze is by default based on regular grain used for partitioning (=1MiB). Signed-off-by: Karel Zak --- libfdisk/src/partition.c | 11 +++++++++-- libfdisk/src/table.c | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/libfdisk/src/partition.c b/libfdisk/src/partition.c index 80a43ffde5..7cfca3fa40 100644 --- a/libfdisk/src/partition.c +++ b/libfdisk/src/partition.c @@ -1160,8 +1160,15 @@ static int recount_resize( FDISK_INIT_UNDEF(size); rc = fdisk_get_partitions(cxt, &tb); - if (!rc) + if (!rc) { + /* For resize we do not follow grain to detect free-space, but + * we allow to resize with very small granulation. */ + unsigned long org = cxt->grain; + + cxt->grain = cxt->sector_size; rc = fdisk_get_freespaces(cxt, &tb); + cxt->grain = org; + } if (rc) { fdisk_unref_table(tb); return rc; @@ -1258,7 +1265,7 @@ static int recount_resize( goto erange; } - if (!FDISK_IS_UNDEF(size)) { + if (FDISK_IS_UNDEF(size)) { DBG(PART, ul_debugobj(tpl, "resize: size unchanged (undefined)")); } diff --git a/libfdisk/src/table.c b/libfdisk/src/table.c index 8a3a935af9..e78ecc4372 100644 --- a/libfdisk/src/table.c +++ b/libfdisk/src/table.c @@ -646,7 +646,7 @@ int fdisk_get_freespaces(struct fdisk_context *cxt, struct fdisk_table **tb) /* add free-space behind last partition to the end of the table (so * don't use table_add_freespace()) */ - if (rc == 0 && last + grain < cxt->total_sectors - 1) { + if (rc == 0 && last + grain < cxt->last_lba - 1) { DBG(CXT, ul_debugobj(cxt, "freespace behind last partition detected")); rc = new_freespace(cxt, last + (last > cxt->first_lba || nparts ? 1 : 0), -- 2.39.2