Skip to content

repart: Keep new partitions after existing same-type partitions - #43470

Merged
bluca merged 1 commit into
systemd:mainfrom
quanyeyang:repart-partition-number
Aug 26, 2026
Merged

bluca merged 1 commit into
systemd:mainfrom
quanyeyang:repart-partition-number

Conversation

@quanyeyang

Copy link
Copy Markdown
Contributor

Fixes #43449

Summary

systemd-repart could fill a low-numbered partition-table gap
with a new partition of the same GPT type as an existing partition.

Because same-type partitions are matched according to definition order, this
could change the mapping on the next invocation and break idempotency.

Fix

  • Keep newly created partitions after existing managed
    partitions of the same GPT type.
  • Preserve the existing first-unused partition-number behavior
    for other types.
  • Preserve deferred/include/exclude workflows that rely on
    filling partition-number gaps.
  • Add a regression test covering deletion, recreation, and
    repeated invocation.

Tests

meson compile -C build systemd-repart
TEST_MATCH_TESTCASE=partition_number_gap ./test/units/TEST-58-
REPART.sh

The regression test verifies:

- the new partition is assigned partition number 3 rather than
  1;

- partition number 2 remains stable;
- a subsequent invocation produces no changes.

@github-actions github-actions Bot added tests repart please-review PR is ready for (re-)review by a maintainer labels Aug 21, 2026
Comment thread test/units/TEST-58-REPART.sh Fixed
Comment thread test/units/TEST-58-REPART.sh Fixed
Comment thread test/units/TEST-58-REPART.sh Fixed
Comment thread test/units/TEST-58-REPART.sh Fixed
Comment thread test/units/TEST-58-REPART.sh Fixed
Comment thread test/units/TEST-58-REPART.sh Fixed
Comment thread test/units/TEST-58-REPART.sh Fixed
@quanyeyang
quanyeyang force-pushed the repart-partition-number branch from 5a430a7 to d7d0e25 Compare August 21, 2026 05:14
@lionheartyu

Copy link
Copy Markdown
Contributor

Looks good, but please squash it into a single commit and drop the Signed-off-by.

@github-actions

github-actions Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Claude review failed (workflow run)

All eight review lenses completed again on the reworked head (e726a3e9): correctness and memory safety, resource lifetimes and concurrency, security and robustness, architecture/API/compatibility, coding style and maintainability, tests and regression coverage, plus two domain lenses (GPT/libfdisk semantics and repart definition-matching idempotency; integration-test shell correctness). No lens failed or timed out. Three validators then re-checked every candidate; one candidate group was dropped as unsubstantiated.

The rework is a clear improvement: find_unused_partno() is now bounded by the GPT entry-array capacity instead of returning an unbounded number, includes foreignized partitions in the floor via PARTITION_EXISTS(), guards p->partno != UINT64_MAX, numbers multiple new same-type partitions in definition order via the new context_assign_partnos() pass, and the testcase now asserts the resulting layout and uses the file's assert_in/assert_not_in helpers with the corrected assert_eq argument order. Seven of the nine previously reported findings are fixed. The seven ShellCheck SC2140 comments are also resolved - shellcheck is clean on the current head.

Two previously reported findings remain open, and one new build-breaking problem was found: moving numbering from free-area order to definition order changes the numbers that the existing testcase_output_order() asserts, and that testcase was not updated.

Must fix

  • src/repart/repart.c:1781 - excluding PARTITION_IS_FOREIGN(p) from the floor computation defeats the invariant the patch establishes: foreignness is purely in-memory state for the current run, and context_load_partition_table() matches existing partitions to definitions by type UUID in table order with no notion of foreignness, so a partition foreignized by partition_drop_or_foreignize() still consumes a definition slot on the next run. (fixed: the head revision keys the floor on PARTITION_EXISTS(p) alone)
  • src/repart/repart.c:1785 - the raised starting partno is unbounded and no longer falls back to a free lower slot, so numbers for a type drift upward on every delete/re-run cycle and are never reclaimed; once the value reaches the GPT entry-array capacity, fdisk_partition_set_partno()/fdisk_add_partition() fail late in context_mangle_partitions() on images that previously worked. (fixed: the search is now bounded by sym_fdisk_get_npartitions() and fails before any changes are applied)
  • src/repart/repart.c:1775 - the change makes the outcome idempotent but locks in a wrong definition-to-partition mapping: in the scenario the new test exercises, 10-a.conf adopts the surviving partition that was created for 20-b.conf, grows it to 20M, keeps its name="b" (labels are never changed once set), and the newly created partition is labelled b as well. The mis-association that underlies the reported bug is untouched.
  • src/repart/repart.c:1822 - assigning partition numbers in definition-list order changes the numbering asserted by the existing testcase_output_order(), which was not updated, so the test fails. home and swap land in different free areas, so the old smallest-free-area-first walk gave swap=3/home=4; definition order now gives home=3/swap=4, flipping both partno/node pairs in the expected --json=pretty output and both trailing sfdisk --dump assertions.
  • src/repart/repart.c:1789 - the new floor never falls back to a free lower slot, so an image whose only same-type partition sits high in the GPT entry array now fails the whole run even though lower slots are free; systemd-repart.service does not treat that exit status as success, so the root filesystem is not grown at boot. The floor is also monotone, so create/delete churn walks a type's maximum upward until every run fails.

Suggestions

  • src/repart/repart.c:1825 - the invariant is not maintained between several new partitions of the same type in one run: context_place_partitions() walks context->free_areas in free_area_compare() order (smallest available first), so partition numbers can be assigned out of definition order and the next run rematches definitions again. (fixed by the new context_assign_partnos() pass, which numbers in definition order before context_sort_partitions())
  • src/repart/repart.c:1769 - find_first_unused_partno() no longer finds the first unused partno; consider renaming, and taking sd_id128_t type_uuid rather than a whole const Partition * since that is all the helper reads. The man page's documented numbering rule should also be reconciled with whichever rule is implemented. (fixed: renamed to find_unused_partno(), now takes sd_id128_t type_uuid, and the man page was rewritten)
  • test/units/TEST-58-REPART.sh:3223 - the assertions only check which partition numbers exist, so they pass regardless of type, size, label or which drop-in each partition ended up bound to; neighbouring testcases assert the full sfdisk -d record. Uncovered boundaries: a different-type partition still filling the low gap, a same-type foreign partition, and two new same-type partitions in one run. (still open on the reworked test: the assertions now pin start=/size= but stop at type= and never pin name=, and the numbers asserted encode the re-adoption of partition 2 by 10-a.conf; multi-new-same-type ordering remains uncovered because the first invocation uses --empty=create and so has a single free area)
  • test/units/TEST-58-REPART.sh:3201 - use the file's assert_in/assert_not_in helpers instead of assert_rc N grep -Fq ... <<<"$output"; grep -q in a pipeline is disallowed for these integration tests, and assert_rc 1 grep conflates "no match" with "grep error". (fixed: the testcase now uses assert_in/assert_not_in throughout)
  • src/repart/repart.c:1802 - reporting GPT-slot exhaustion as -ENOSPC collides with the disk-space meaning both context_ponder() callers attach to that errno: run() answers it with determine_auto_size(..., LOG_INFO, ...) and the Varlink handler translates it into io.systemd.Repart.DiskTooSmall/InsufficientFreeSpace, so a client is told to grow the image and retry, which can never free an entry slot.
  • man/systemd-repart.xml:101 - "so that multiple partitions of the same type keep matching their configuration files" overstates the guarantee, since matching stays positional on type UUID in partition-number order; the new hard failure when no slot is available is also undocumented.

Nits

  • src/repart/repart.c:1783 - p->partno + 1 is evaluated without the p->partno != UINT64_MAX guard that the scan immediately below uses. (fixed: the condition now includes p->partno != UINT64_MAX)
  • test/units/TEST-58-REPART.sh:3236 - assert_eq prints expected: '$2' actual: '$1', so the arguments are reversed here. (fixed: the head revision calls assert_eq "$after" "$before")
  • src/repart/repart.c:1784 - the in-loop p->partno >= nents - 1 bail-out never changes behaviour, only the error message, because the floor only rises via MAX() and the post-loop partno >= nents check already returns the same error.
  • src/repart/repart.c:1786 - lines 1786 (130 columns) and 1783 (112 columns) exceed the project's 109-column limit.
  • src/repart/repart.c:1769 - commit e726a3e9's subject and every body line carry two leading spaces, which mangles git log output.

The seven ShellCheck SC2140 comments on this pull request were filed against the earlier "$image"1" :" spelling and are resolved by the rework; shellcheck is clean on the current head.

Comment thread src/repart/repart.c Resolved
Comment thread src/repart/repart.c Resolved
Comment thread src/repart/repart.c Resolved
Comment thread src/repart/repart.c Resolved
Comment thread src/repart/repart.c Resolved
Comment thread src/repart/repart.c
LIST_FOREACH(partitions, p, context->partitions)
if (PARTITION_EXISTS(p) && !PARTITION_IS_FOREIGN(p) &&
sd_id128_equal(p->type.uuid, partition->type.uuid))
partno = MAX(partno, p->partno + 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude: nit: The scan just below deliberately guards with p->partno != UINT64_MAX before touching p->partno, but this loop evaluates p->partno + 1 unguarded. It is safe today because context_unload_partition_table() resets partno to UINT64_MAX and clears current_partition together (lines 4218/4222), so PARTITION_EXISTS(p) implies a valid number. But if that pairing ever breaks, the sentinel wraps to 0 and silently disables the whole adjustment rather than failing. Either mirror the existing style with p->partno != UINT64_MAX && in the condition, or add assert(p->partno != UINT64_MAX); to document the dependency.

Comment thread test/units/TEST-58-REPART.sh Resolved
Comment thread test/units/TEST-58-REPART.sh Resolved
Comment thread test/units/TEST-58-REPART.sh Resolved
Comment thread src/repart/repart.c Resolved
Comment thread src/repart/repart.c
"No free partition number available after existing partitions of type %s.",
SD_ID128_TO_UUID_STRING(type_uuid));

partno = MAX(partno, p->partno + 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude: must-fix: The new floor never falls back to a free lower slot, so a table that previously worked can now fail the whole run permanently. partno only ever rises (MAX(partno, p->partno + 1)) and freed low slots are never reclaimed, so if any existing partition of the requested type sits high in the entry array, every lower hole is skipped and find_unused_partno() fails even though dozens of slots are free. Before this change find_first_unused_partno() returned the first hole and the run succeeded.

Concrete case: a GPT whose only linux-generic partition is at partno 127 of 128, plus a definition that adds another linux-generic. context_assign_partnos() fails, so context_ponder() fails, so systemd-repart --dry-run=no refuses the disk. That is the ExecStart= of units/systemd-repart.service, which is not covered by the unit's SuccessExitStatus=76 77, so the unit fails at boot and the root filesystem is never grown. The entry count comes from the on-disk GPT header, so for --image= on an untrusted or merely unusual image this is externally influenced input.

The condition is also self-inflicted and monotone: repeated create/delete churn of one type walks that type's maximum upward, after which every subsequent invocation fails with no recovery short of manual repartitioning. Consider falling back to a free lower slot (with a warning) when no slot is available above the floor, or confining the failure to the affected partition (drop/defer it) rather than aborting the whole run.

Comment thread src/repart/repart.c Resolved
Comment thread man/systemd-repart.xml Resolved
Comment thread src/repart/repart.c Resolved
Comment thread src/repart/repart.c Resolved
Comment thread src/repart/repart.c Resolved
@quanyeyang
quanyeyang force-pushed the repart-partition-number branch from e726a3e to aedfbe5 Compare August 26, 2026 12:45
When a partition is deleted from a table containing multiple partitions
of the same type, filling the resulting partition-number gap can place
a new partition before an existing partition matched to an earlier
definition. This changes definition matching on the next invocation and
breaks idempotency.

Keep new partitions after existing partitions of the same type. If no
partition table slot is available after them, fall back to the first
unused slot.

Which existing partition is matched to which definition is still
determined by partition type and partition number order, as documented,
and is unchanged by this.

Fixes systemd#43449
@quanyeyang
quanyeyang force-pushed the repart-partition-number branch from aedfbe5 to 0a14ef7 Compare August 26, 2026 12:57
@poettering poettering removed the please-review PR is ready for (re-)review by a maintainer label Aug 26, 2026
@poettering poettering added the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Aug 26, 2026
@bluca
bluca merged commit a9ff124 into systemd:main Aug 26, 2026
63 of 72 checks passed
@github-actions github-actions Bot removed the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Aug 26, 2026
@quanyeyang
quanyeyang deleted the repart-partition-number branch August 27, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

systemd-repart: added partition number not being the highest one available

5 participants