1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/mount.h>
32 #include "mount-setup.h"
42 typedef struct MountPoint {
51 static const MountPoint mount_table[] = {
52 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
53 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
54 { "devtmpfs", "/dev", "devtmpfs", "mode=755", MS_NOSUID, true },
55 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV, true },
56 { "devpts", "/dev/pts", "devpts", "mode=620,gid=" STRINGIFY(TTY_GID), MS_NOSUID|MS_NOEXEC, false },
57 { "tmpfs", "/dev/.run", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
58 { "tmpfs", "/sys/fs/cgroup", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
59 { "cgroup", "/sys/fs/cgroup/systemd", "cgroup", "none,name=systemd", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
62 /* These are API file systems that might be mounted by other software,
63 * we just list them here so that we know that we should ignore them */
65 static const char * const ignore_paths[] = {
68 "/var/lib/nfs/rpc_pipefs",
72 bool mount_point_is_api(const char *path) {
75 /* Checks if this mount point is considered "API", and hence
76 * should be ignored */
78 for (i = 0; i < ELEMENTSOF(mount_table); i ++)
79 if (path_equal(path, mount_table[i].where))
82 return path_startswith(path, "/sys/fs/cgroup/");
85 bool mount_point_ignore(const char *path) {
88 for (i = 0; i < ELEMENTSOF(ignore_paths); i++)
89 if (path_equal(path, ignore_paths[i]))
95 static int mount_one(const MountPoint *p) {
100 if ((r = path_is_mount_point(p->where)) < 0)
106 /* The access mode here doesn't really matter too much, since
107 * the mounted file system will take precedence anyway. */
108 mkdir_p(p->where, 0755);
110 log_debug("Mounting %s to %s of type %s with options %s.",
121 log_error("Failed to mount %s: %s", p->where, strerror(errno));
122 return p->fatal ? -errno : 0;
125 label_fix(p->where, false);
130 static int mount_cgroup_controllers(void) {
135 /* Mount all available cgroup controllers that are built into the kernel. */
137 if (!(f = fopen("/proc/cgroups", "re")))
140 /* Ignore the header line */
141 (void) fgets(buf, sizeof(buf), f);
145 char *controller, *where;
148 if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) {
153 log_error("Failed to parse /proc/cgroups.");
163 if (asprintf(&where, "/sys/fs/cgroup/%s", controller) < 0) {
173 p.options = controller;
174 p.flags = MS_NOSUID|MS_NOEXEC|MS_NODEV;
193 static int symlink_and_label(const char *old_path, const char *new_path) {
199 if ((r = label_symlinkfile_set(new_path)) < 0)
202 if (symlink(old_path, new_path) < 0)
212 const struct stat *sb,
214 struct FTW *ftwbuf) {
216 /* No need to label /dev twice in a row... */
217 if (ftwbuf->level == 0)
220 label_fix(fpath, true);
224 int mount_setup(void) {
226 const char symlinks[] =
227 "/proc/kcore\0" "/dev/core\0"
228 "/proc/self/fd\0" "/dev/fd\0"
229 "/proc/self/fd/0\0" "/dev/stdin\0"
230 "/proc/self/fd/1\0" "/dev/stdout\0"
231 "/proc/self/fd/2\0" "/dev/stderr\0";
237 for (i = 0; i < ELEMENTSOF(mount_table); i ++)
238 if ((r = mount_one(mount_table+i)) < 0)
241 /* Nodes in devtmpfs need to be manually updated for the
242 * appropriate labels, after mounting. The other virtual API
243 * file systems do not need. */
245 if (unlink("/dev/.systemd-relabel-devtmpfs") >= 0)
246 nftw("/dev", nftw_cb, 64, FTW_MOUNT|FTW_PHYS);
248 /* Create a few default symlinks, which are normally created
249 * bei udevd, but some scripts might need them before we start
252 NULSTR_FOREACH_PAIR(j, k, symlinks)
253 symlink_and_label(j, k);
255 /* Create a few directories we always want around */
256 mkdir("/dev/.run/systemd", 0755);
257 mkdir("/dev/.run/systemd/ask-password", 0755);
259 return mount_cgroup_controllers();