From: Daan De Meyer Date: Wed, 14 Jun 2023 08:27:22 +0000 (+0200) Subject: mkfs-util: Hide /proc/self/mounts before running mkfs X-Git-Tag: v254-rc1~204 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=969eb0390f4a94fd95b828ede0588f6c00b293ed;p=thirdparty%2Fsystemd.git mkfs-util: Hide /proc/self/mounts before running mkfs mkfs.btrfs refuses to operate on a block device with mounted partitions, even if doing so is perfectly safe. An example when this happens is when using systemd-repart with it's --image switch to add a root partition to a /usr only image. As a workaround until the issue is fixed, let's hide the information on mounted filesystems from mkfs.btrfs so it doesn't fail and formats the new filesystem as expected. --- diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c index 6ee4307c682..9f6ddda6497 100644 --- a/src/shared/mkfs-util.c +++ b/src/shared/mkfs-util.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include #include #include "dirent-util.h" @@ -8,6 +9,7 @@ #include "fs-util.h" #include "id128-util.h" #include "mkfs-util.h" +#include "mount-util.h" #include "mountpoint-util.h" #include "path-util.h" #include "process-util.h" @@ -514,13 +516,19 @@ int make_filesystem( /*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_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_NEW_MOUNTNS, /*ret_pid=*/ NULL); if (r < 0) return r; if (r == 0) { /* Child */ + /* mkfs.btrfs refuses to operate on block devices with mounted partitions, even if operating + * 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); + execvp(mkfs, argv); log_error_errno(errno, "Failed to execute %s: %m", mkfs);