1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

mkfs-util: Only unshare mount namespace if needed

We only need a separate mount namespace if we're operating on a
btrfs block device so let's make sure we only unshare the mount
namespace if that's the case.

Replaces #29214
This commit is contained in:
Daan De Meyer 2023-09-19 11:44:54 +02:00
parent df4535a5c0
commit e078490175

View File

@ -334,6 +334,8 @@ int make_filesystem(
_cleanup_(unlink_and_freep) char *protofile = NULL;
char vol_id[CONST_MAX(SD_ID128_UUID_STRING_MAX, 8U + 1U)] = {};
int stdio_fds[3] = { -EBADF, STDERR_FILENO, STDERR_FILENO};
ForkFlags flags = FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|
FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_REOPEN_LOG;
int r;
assert(node);
@ -590,6 +592,16 @@ int make_filesystem(
if (extra_mkfs_args && strv_extend_strv(&argv, extra_mkfs_args, false) < 0)
return log_oom();
if (streq(fstype, "btrfs")) {
struct stat st;
if (stat(node, &st) < 0)
return log_error_errno(r, "Failed to stat '%s': %m", node);
if (S_ISBLK(st.st_mode))
flags |= FORK_NEW_MOUNTNS;
}
if (DEBUG_LOGGING) {
_cleanup_free_ char *j = NULL;
@ -602,8 +614,7 @@ int make_filesystem(
stdio_fds,
/*except_fds=*/ NULL,
/*n_except_fds=*/ 0,
FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT|
FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_NEW_MOUNTNS|FORK_REOPEN_LOG,
flags,
/*ret_pid=*/ NULL);
if (r < 0)
return r;
@ -620,7 +631,8 @@ int make_filesystem(
* on unformatted free space, so let's trick it and other mkfs tools into thinking no
* partitions are mounted. See https://github.com/kdave/btrfs-progs/issues/640 for more
° information. */
(void) mount_nofollow_verbose(LOG_DEBUG, "/dev/null", "/proc/self/mounts", NULL, MS_BIND, NULL);
if (flags & FORK_NEW_MOUNTNS)
(void) mount_nofollow_verbose(LOG_DEBUG, "/dev/null", "/proc/self/mounts", NULL, MS_BIND, NULL);
execvp(mkfs, argv);