/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#include <sys/mount.h>
#include <unistd.h>
#include "dirent-util.h"
#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"
/*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);