diff --git a/src/repart/repart.c b/src/repart/repart.c index 271056a06a9..d245cab539f 100644 --- a/src/repart/repart.c +++ b/src/repart/repart.c @@ -1652,23 +1652,14 @@ static bool context_grow_partitions_phase( return !try_again; } -static void context_grow_partition_one(Context *context, FreeArea *a, Partition *p, uint64_t *span) { +static void context_grow_partition_one(Context *context, Partition *p, uint64_t *span) { uint64_t m; assert(context); - assert(a); assert(p); assert(span); - if (*span == 0) - return; - - if (p->allocated_to_area != a) - return; - - if (PARTITION_IS_FOREIGN(p)) - return; - + assert(*span > 0); assert(p->new_size != UINT64_MAX); /* Calculate new size and align. */ @@ -1708,15 +1699,15 @@ static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) { if (context_grow_partitions_phase(context, a, phase, &span, &weight_sum)) phase++; /* go to the next phase */ - /* We still have space left over? Donate to preceding partition if we have one */ - if (span > 0 && a->after) - context_grow_partition_one(context, a, a->after, &span); - /* What? Even still some space left (maybe because there was no preceding partition, or it had a - * size limit), then let's donate it to whoever wants it. */ + /* What? Even still some space left (because one partition had max_size < share + * and another had min_size > share), then let's donate it to whoever wants it. */ if (span > 0) LIST_FOREACH(partitions, p, context->partitions) { - context_grow_partition_one(context, a, p, &span); + if (p->allocated_to_area != a && p->padding_area != a) + continue; + + context_grow_partition_one(context, p, &span); if (span == 0) break; } diff --git a/test/units/TEST-58-REPART.sh b/test/units/TEST-58-REPART.sh index 863023454ea..8593a86c867 100755 --- a/test/units/TEST-58-REPART.sh +++ b/test/units/TEST-58-REPART.sh @@ -2703,6 +2703,92 @@ EOF assert_in "$imgs/gap.img3 : start= 22528, size= 20480, type=$usr_guid, uuid=$usr_uuid, name=\"part-b\"" "$output" } +testcase_distribute_leftover_space() { + local defs imgs output + + defs="$(mktemp --directory "/tmp/test-repart.defs.XXXXXXXXXX")" + imgs="$(mktemp --directory "/var/tmp/test-repart.imgs.XXXXXXXXXX")" + # shellcheck disable=SC2064 + trap "rm -rf '$defs' '$imgs'" RETURN + chmod 0755 "$defs" + + echo "*** Left over space after distributing according to weights should still be assigned ***" + + tee "$defs/01-esp.conf" <