]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: Calculate filesystem UUID earlier
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 13 Oct 2022 11:26:07 +0000 (13:26 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 11 Nov 2022 12:48:21 +0000 (13:48 +0100)
src/partition/repart.c

index e18a82960fc5c953129c8747e1183b8851c297d3..e2b6ab18e92cc11f84e74341560b97cdb3487792 100644 (file)
@@ -168,6 +168,7 @@ struct Partition {
         sd_id128_t current_uuid, new_uuid;
         bool new_uuid_is_set;
         char *current_label, *new_label;
+        sd_id128_t fs_uuid;
 
         bool dropped;
         bool factory_reset;
@@ -344,7 +345,6 @@ static void partition_foreignize(Partition *p) {
 
         /* Reset several parameters set through definition file to make the partition foreign. */
 
-        p->new_label = mfree(p->new_label);
         p->definition_path = mfree(p->definition_path);
         p->drop_in_files = strv_free(p->drop_in_files);
 
@@ -356,8 +356,6 @@ static void partition_foreignize(Partition *p) {
         p->make_directories = strv_free(p->make_directories);
         p->verity_match_key = mfree(p->verity_match_key);
 
-        p->new_uuid = SD_ID128_NULL;
-        p->new_uuid_is_set = false;
         p->priority = 0;
         p->weight = 1000;
         p->padding_weight = 0;
@@ -3509,7 +3507,6 @@ static int context_mkfs(Context *context) {
                 _cleanup_free_ char *encrypted = NULL, *root = NULL;
                 _cleanup_close_ int encrypted_dev_fd = -1;
                 const char *fsdev;
-                sd_id128_t fs_uuid;
 
                 if (p->dropped)
                         continue;
@@ -3547,12 +3544,6 @@ static int context_mkfs(Context *context) {
 
                 log_info("Formatting future partition %" PRIu64 ".", p->partno);
 
-                /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
-                 * keyed off the partition UUID. */
-                r = derive_uuid(p->new_uuid, "file-system-uuid", &fs_uuid);
-                if (r < 0)
-                        return r;
-
                 /* Ideally, we populate filesystems using our own code after creating the filesystem to
                  * ensure consistent handling of chattrs, xattrs and other similar things. However, when
                  * using read-only filesystems such as squashfs, we can't populate after creating the
@@ -3562,7 +3553,7 @@ static int context_mkfs(Context *context) {
                 if (r < 0)
                         return r;
 
-                r = make_filesystem(fsdev, p->format, strempty(p->new_label), root ?: tmp_root, fs_uuid, arg_discard);
+                r = make_filesystem(fsdev, p->format, strempty(p->new_label), root ?: tmp_root, p->fs_uuid, arg_discard);
                 if (r < 0) {
                         encrypted_dev_fd = safe_close(encrypted_dev_fd);
                         (void) deactivate_luks(cd, encrypted);
@@ -4062,6 +4053,12 @@ static int context_acquire_partition_uuids_and_labels(Context *context) {
                         p->new_uuid_is_set = true;
                 }
 
+                /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
+                 * keyed off the partition UUID. */
+                r = derive_uuid(p->new_uuid, "file-system-uuid", &p->fs_uuid);
+                if (r < 0)
+                        return r;
+
                 if (!isempty(p->current_label)) {
                         /* never change initialized labels */
                         r = free_and_strdup_warn(&p->new_label, p->current_label);
@@ -5951,6 +5948,11 @@ static int run(int argc, char *argv[]) {
         if (r < 0)
                 return r;
 
+        /* Make sure each partition has a unique UUID and unique label */
+        r = context_acquire_partition_uuids_and_labels(context);
+        if (r < 0)
+                return r;
+
         /* Open all files to copy blocks from now, since we want to take their size into consideration */
         r = context_open_copy_block_paths(
                         context,
@@ -6007,11 +6009,6 @@ static int run(int argc, char *argv[]) {
         /* Now calculate where each new partition gets placed */
         context_place_partitions(context);
 
-        /* Make sure each partition has a unique UUID and unique label */
-        r = context_acquire_partition_uuids_and_labels(context);
-        if (r < 0)
-                return r;
-
         (void) context_dump(context, node, /*late=*/ false);
 
         r = context_write_partition_table(context, node, from_scratch);