libfdisk: fix alignment logic for tiny partitions

Addresses: https://github.com/karelzak/util-linux/issues/1018 (second case)
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2020-04-29 11:10:54 +02:00
parent b80fb30a5b
commit 89c0297bcc
2 changed files with 10 additions and 5 deletions

View File

@ -140,12 +140,14 @@ fdisk_sector_t fdisk_align_lba_in_range(struct fdisk_context *cxt,
{
fdisk_sector_t res;
start = fdisk_align_lba(cxt, start, FDISK_ALIGN_UP);
stop = fdisk_align_lba(cxt, stop, FDISK_ALIGN_DOWN);
DBG(CXT, ul_debugobj(cxt, "LBA: align in range <%ju..%ju>", (uintmax_t) start, (uintmax_t) stop));
if (lba > start && lba < stop
&& (lba - start) < (cxt->grain / cxt->sector_size)) {
if (start + (cxt->grain / cxt->sector_size) <= stop) {
start = fdisk_align_lba(cxt, start, FDISK_ALIGN_UP);
stop = fdisk_align_lba(cxt, stop, FDISK_ALIGN_DOWN);
}
if (start + (cxt->grain / cxt->sector_size) > stop) {
DBG(CXT, ul_debugobj(cxt, "LBA: area smaller than grain, don't align"));
res = lba;
goto done;

View File

@ -1236,7 +1236,11 @@ static int add_partition(struct fdisk_context *cxt, size_t n,
struct pte *pe = self_pte(cxt, n);
assert(pe);
assert(start >= cxt->first_lba);
pe->offset = start - cxt->first_lba;
DBG(LABEL, ul_debug("DOS: setting EBR offset to %ju [start=%ju]", pe->offset, start));
if (pe->offset == l->ext_offset) { /* must be corrected */
pe->offset++;
if (cxt->first_lba == 1)
@ -1337,7 +1341,6 @@ static int add_partition(struct fdisk_context *cxt, size_t n,
set_partition(cxt, n, 0, start, stop, sys, fdisk_partition_is_bootable(pa));
if (n > 4) {
struct pte *pe = self_pte(cxt, n);
assert(pe);
set_partition(cxt, n - 1, 1, pe->offset, stop,
MBR_DOS_EXTENDED_PARTITION, 0);