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.
This commit is contained in:
Daan De Meyer
2023-06-14 19:53:49 +02:00
parent 43b49c0f14
commit 969eb0390f
+9 -1
View 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);