Wrap all mount(2) calls so we at least get an error message

If any of these mount() calls fail, not counting EBUSY because that
would mean sth is already mounted, there really isn't much we can do.
So the least we should do is warn on console if it happens.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2021-06-14 11:20:08 +02:00
1 parent 2f34315634
commit 47fbe49861
1 file changed
+22 -15
+22 -15
View File
@@ -156,6 +156,18 @@ static int fsck_all(void)
return rc; return rc;
} }
/* Wrapper for mount(2), logs any errors to stderr */
static void fs_mount(const char *src, const char *tgt, const char *fstype,
unsigned long flags, const void *data)
{
const char *msg = !fstype ? "MS_MOVE" : "mounting";
int rc;
rc = mount(src, tgt, fstype, flags, data);
if (rc && errno != EBUSY)
_pe("Failed %s %s on %s", msg, src, tgt);
}
#ifndef SYSROOT #ifndef SYSROOT
/* If / is not listed in fstab, or listed as 'ro', leave it alone */ /* If / is not listed in fstab, or listed as 'ro', leave it alone */
static int fs_readonly_root(struct fstab *fs) static int fs_readonly_root(struct fstab *fs)
@@ -207,8 +219,7 @@ static void fs_remount_root(int fsckerr)
* XXX: Untested, in the initramfs age we should * XXX: Untested, in the initramfs age we should
* probably use switch_root instead. * probably use switch_root instead.
*/ */
if (mount(SYSROOT, "/", NULL, MS_MOVE, NULL)) fs_mount(SYSROOT, "/", NULL, MS_MOVE, NULL);
_pe("Failed %s / MS_MOVE");
} }
#endif /* SYSROOT */ #endif /* SYSROOT */
@@ -232,7 +243,7 @@ static void fs_finalize(void)
*/ */
if (!fismnt("/dev/shm")) { if (!fismnt("/dev/shm")) {
makedir("/dev/shm", 0777); makedir("/dev/shm", 0777);
mount("shm", "/dev/shm", "tmpfs", 0, "mode=0777"); fs_mount("shm", "/dev/shm", "tmpfs", 0, "mode=0777");
} }
/* Modern systems use /dev/pts */ /* Modern systems use /dev/pts */
@@ -250,7 +261,7 @@ static void fs_finalize(void)
snprintf(opts, sizeof(opts), "gid=%d,mode=%d,ptmxmode=0666", gid, mode); snprintf(opts, sizeof(opts), "gid=%d,mode=%d,ptmxmode=0666", gid, mode);
makedir("/dev/pts", 0755); makedir("/dev/pts", 0755);
mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, opts); fs_mount("devpts", "/dev/pts", "devpts", MS_NOSUID | MS_NOEXEC, opts);
} }
/* /*
@@ -263,19 +274,19 @@ static void fs_finalize(void)
* for /run (and optionally /run/lock). * for /run (and optionally /run/lock).
*/ */
if (fisdir("/run") && !fismnt("/run")) { if (fisdir("/run") && !fismnt("/run")) {
mount("tmpfs", "/run", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, "mode=0755,size=10%"); fs_mount("tmpfs", "/run", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, "mode=0755,size=10%");
/* This prevents user DoS of /run by filling /run/lock at the expense of another tmpfs, max 5MiB */ /* This prevents user DoS of /run by filling /run/lock at the expense of another tmpfs, max 5MiB */
makedir("/run/lock", 1777); makedir("/run/lock", 1777);
mount("tmpfs", "/run/lock", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, "mode=0777,size=5252880"); fs_mount("tmpfs", "/run/lock", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_RELATIME, "mode=0777,size=5252880");
} }
/* Modern systems use tmpfs for /tmp */ /* Modern systems use tmpfs for /tmp */
if (!fismnt("/tmp")) if (!fismnt("/tmp"))
mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV, "mode=1777"); fs_mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV, "mode=1777");
} }
static void fs_mount(void) static void fs_mount_all(void)
{ {
if (!rescue) if (!rescue)
fs_remount_root(fsck_all()); fs_remount_root(fsck_all());
@@ -299,7 +310,7 @@ static void fs_mount(void)
* We need /proc for rs_remount_root() and conf_parse_cmdline(), /dev * We need /proc for rs_remount_root() and conf_parse_cmdline(), /dev
* for early multi-console, and /sys for the cgroups support. Any * for early multi-console, and /sys for the cgroups support. Any
* occurrence of these file systems in /etc/fstab will replace these * occurrence of these file systems in /etc/fstab will replace these
* mounts later in fs_mount() * mounts later in fs_mount_all()
* *
* Ignore any mount errors with EBUSY, kernel likely alread mounted * Ignore any mount errors with EBUSY, kernel likely alread mounted
* the filesystem for us automatically, e.g., CONFIG_DEVTMPFS_MOUNT. * the filesystem for us automatically, e.g., CONFIG_DEVTMPFS_MOUNT.
@@ -319,8 +330,6 @@ static void fs_init(void)
umask(022); umask(022);
for (i = 0; i < NELEMS(fs); i++) { for (i = 0; i < NELEMS(fs); i++) {
int rc;
/* /*
* Check if already mounted, we may be running in a * Check if already mounted, we may be running in a
* container, or an initramfs ran before us. The * container, or an initramfs ran before us. The
@@ -330,9 +339,7 @@ static void fs_init(void)
if (fismnt(fs[i].file)) if (fismnt(fs[i].file))
continue; continue;
rc = mount(fs[i].spec, fs[i].file, fs[i].type, 0, NULL); fs_mount(fs[i].spec, fs[i].file, fs[i].type, 0, NULL);
if (rc && errno != EBUSY)
_pe("Failed mounting %s on %s", fs[i].spec, fs[i].file);
} }
} }
@@ -608,7 +615,7 @@ int main(int argc, char *argv[])
cgroup_init(&loop); cgroup_init(&loop);
/* Check and mount filesystems. */ /* Check and mount filesystems. */
fs_mount(); fs_mount_all();
/* Bootstrap conditions, needed for hooks */ /* Bootstrap conditions, needed for hooks */
cond_init(); cond_init();