]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: Use first unused partition number for new partitions
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 14 Oct 2022 10:40:28 +0000 (12:40 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 15 Nov 2022 12:27:59 +0000 (13:27 +0100)
If we skip some partition types in a first run of systemd-repart,
we don't want their partition numbers to be different than usual,
so let's change the allocation of partition numbers to account for
that.

src/partition/repart.c

index 20f764ee1ca10ec927ea00906ac007c90a8485fb..c2051de65964695b4734499b06a581b18cd6f5c8 100644 (file)
@@ -1025,21 +1025,30 @@ static int context_grow_partitions(Context *context) {
         return 0;
 }
 
-static void context_place_partitions(Context *context) {
+static uint64_t find_first_unused_partno(Context *context) {
         uint64_t partno = 0;
 
         assert(context);
 
-        /* Determine next partition number to assign */
-        LIST_FOREACH(partitions, p, context->partitions) {
-                if (!PARTITION_EXISTS(p))
-                        continue;
+        for (bool changed = true; changed;) {
+                changed = false;
 
-                assert(p->partno != UINT64_MAX);
-                if (p->partno >= partno)
-                        partno = p->partno + 1;
+                LIST_FOREACH(partitions, p, context->partitions) {
+                        if (p->partno != UINT64_MAX && p->partno == partno) {
+                                partno++;
+                                changed = true;
+                                break;
+                        }
+                }
         }
 
+        return partno;
+}
+
+static void context_place_partitions(Context *context) {
+
+        assert(context);
+
         for (size_t i = 0; i < context->n_free_areas; i++) {
                 FreeArea *a = context->free_areas[i];
                 _unused_ uint64_t left;
@@ -1062,7 +1071,7 @@ static void context_place_partitions(Context *context) {
                                 continue;
 
                         p->offset = start;
-                        p->partno = partno++;
+                        p->partno = find_first_unused_partno(context);
 
                         assert(left >= p->new_size);
                         start += p->new_size;