]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
repart: Re-open file descriptor to partition target after mkfs
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 5 Dec 2023 13:56:00 +0000 (14:56 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 8 Dec 2023 01:59:36 +0000 (10:59 +0900)
The mkfs binary might unlink the path we give it and replace it with
a new file so let's make sure that our fd points to any new file rather
than the old deleted file.

Specifically this fixes erofs partition generation.

src/partition/repart.c

index 41eb1d771afcbd5d2c58daa724d32db4dd17c8c7..aa0269e7309e63bf386ceb9ffe295609558ebbd7 100644 (file)
@@ -4849,6 +4849,17 @@ static int context_mkfs(Context *context) {
                 if (r < 0)
                         return r;
 
+                /* The mkfs binary we invoked might have removed our temporary file when we're not operating
+                 * on a loop device, so let's make sure we open the file again to make sure our file
+                 * descriptor points to any potential new file. */
+
+                if (t->fd >= 0 && t->path && !t->loop) {
+                        safe_close(t->fd);
+                        t->fd = open(t->path, O_RDWR|O_CLOEXEC);
+                        if (t->fd < 0)
+                                return log_error_errno(errno, "Failed to reopen temporary file: %m");
+                }
+
                 log_info("Successfully formatted future partition %" PRIu64 ".", p->partno);
 
                 /* If we're writing to a loop device, we can now mount the empty filesystem and populate it. */