From: Daan De Meyer Date: Tue, 5 Dec 2023 13:56:00 +0000 (+0100) Subject: repart: Re-open file descriptor to partition target after mkfs X-Git-Tag: v256-rc1~1552 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0122c7d060c03c116bd5931c53580f000b2b920e;p=thirdparty%2Fsystemd.git repart: Re-open file descriptor to partition target after mkfs 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. --- diff --git a/src/partition/repart.c b/src/partition/repart.c index 41eb1d771af..aa0269e7309 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -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. */