]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mkfs-util: Hide /proc/self/mounts before running mkfs
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 14 Jun 2023 08:27:22 +0000 (10:27 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 14 Jun 2023 17:53:49 +0000 (19:53 +0200)
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.

src/shared/mkfs-util.c

index 6ee4307c68269e0afec690d7a65125a60e09240b..9f6ddda6497870e1d22eca7691873460e3d7efae 100644 (file)
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include <sys/mount.h>
 #include <unistd.h>
 
 #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);