2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/mount.h>
21 #include <linux/magic.h>
23 #include "alloc-util.h"
30 #include "mount-util.h"
31 #include "nspawn-mount.h"
32 #include "parse-util.h"
33 #include "path-util.h"
36 #include "stat-util.h"
37 #include "string-util.h"
39 #include "user-util.h"
42 CustomMount
* custom_mount_add(CustomMount
**l
, unsigned *n
, CustomMountType t
) {
48 assert(t
< _CUSTOM_MOUNT_TYPE_MAX
);
50 c
= realloc(*l
, (*n
+ 1) * sizeof(CustomMount
));
58 *ret
= (CustomMount
) { .type
= t
};
63 void custom_mount_free_all(CustomMount
*l
, unsigned n
) {
66 for (i
= 0; i
< n
; i
++) {
67 CustomMount
*m
= l
+ i
;
74 (void) rm_rf(m
->work_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
84 int custom_mount_compare(const void *a
, const void *b
) {
85 const CustomMount
*x
= a
, *y
= b
;
88 r
= path_compare(x
->destination
, y
->destination
);
92 if (x
->type
< y
->type
)
94 if (x
->type
> y
->type
)
100 int bind_mount_parse(CustomMount
**l
, unsigned *n
, const char *s
, bool read_only
) {
101 _cleanup_free_
char *source
= NULL
, *destination
= NULL
, *opts
= NULL
;
109 r
= extract_many_words(&p
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
, &source
, &destination
, NULL
);
116 destination
= strdup(source
);
121 if (r
== 2 && !isempty(p
)) {
127 if (!path_is_absolute(source
))
130 if (!path_is_absolute(destination
))
133 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_BIND
);
138 m
->destination
= destination
;
139 m
->read_only
= read_only
;
142 source
= destination
= opts
= NULL
;
146 int tmpfs_mount_parse(CustomMount
**l
, unsigned *n
, const char *s
) {
147 _cleanup_free_
char *path
= NULL
, *opts
= NULL
;
156 r
= extract_first_word(&p
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
163 opts
= strdup("mode=0755");
169 if (!path_is_absolute(path
))
172 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_TMPFS
);
176 m
->destination
= path
;
183 static int tmpfs_patch_options(
186 uid_t uid_shift
, uid_t uid_range
,
188 const char *selinux_apifs_context
,
193 if ((userns
&& uid_shift
!= 0) || patch_ids
) {
194 assert(uid_shift
!= UID_INVALID
);
197 (void) asprintf(&buf
, "%s,uid=" UID_FMT
",gid=" UID_FMT
, options
, uid_shift
, uid_shift
);
199 (void) asprintf(&buf
, "uid=" UID_FMT
",gid=" UID_FMT
, uid_shift
, uid_shift
);
207 if (selinux_apifs_context
) {
211 t
= strjoin(options
, ",context=\"", selinux_apifs_context
, "\"", NULL
);
213 t
= strjoin("context=\"", selinux_apifs_context
, "\"", NULL
);
224 if (!buf
&& options
) {
225 buf
= strdup(options
);
234 int mount_sysfs(const char *dest
) {
235 const char *full
, *top
, *x
;
238 top
= prefix_roota(dest
, "/sys");
239 r
= path_check_fstype(top
, SYSFS_MAGIC
);
241 return log_error_errno(r
, "Failed to determine filesystem type of %s: %m", top
);
242 /* /sys might already be mounted as sysfs by the outer child in the
243 * !netns case. In this case, it's all good. Don't touch it because we
244 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
249 full
= prefix_roota(top
, "/full");
251 (void) mkdir(full
, 0755);
253 if (mount("sysfs", full
, "sysfs", MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
) < 0)
254 return log_error_errno(errno
, "Failed to mount sysfs to %s: %m", full
);
256 FOREACH_STRING(x
, "block", "bus", "class", "dev", "devices", "kernel") {
257 _cleanup_free_
char *from
= NULL
, *to
= NULL
;
259 from
= prefix_root(full
, x
);
263 to
= prefix_root(top
, x
);
267 (void) mkdir(to
, 0755);
269 if (mount(from
, to
, NULL
, MS_BIND
, NULL
) < 0)
270 return log_error_errno(errno
, "Failed to mount /sys/%s into place: %m", x
);
272 if (mount(NULL
, to
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
) < 0)
273 return log_error_errno(errno
, "Failed to mount /sys/%s read-only: %m", x
);
276 if (umount(full
) < 0)
277 return log_error_errno(errno
, "Failed to unmount %s: %m", full
);
280 return log_error_errno(errno
, "Failed to remove %s: %m", full
);
282 x
= prefix_roota(top
, "/fs/kdbus");
283 (void) mkdir_p(x
, 0755);
285 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
286 * remount /sys read-only.
288 if (cg_ns_supported()) {
289 x
= prefix_roota(top
, "/fs/cgroup");
290 (void) mkdir_p(x
, 0755);
293 if (mount(NULL
, top
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, NULL
) < 0)
294 return log_error_errno(errno
, "Failed to make %s read-only: %m", top
);
299 int mount_all(const char *dest
,
300 bool use_userns
, bool in_userns
,
302 uid_t uid_shift
, uid_t uid_range
,
303 const char *selinux_apifs_context
) {
305 typedef struct MountPoint
{
316 static const MountPoint mount_table
[] = {
317 { "proc", "/proc", "proc", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, true, true, false },
318 { "/proc/sys", "/proc/sys", NULL
, NULL
, MS_BIND
, true, true, false }, /* Bind mount first ...*/
319 { "/proc/sys/net", "/proc/sys/net", NULL
, NULL
, MS_BIND
, true, true, true }, /* (except for this) */
320 { NULL
, "/proc/sys", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, true, true, false }, /* ... then, make it r/o */
321 { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, true, false, true },
322 { "sysfs", "/sys", "sysfs", NULL
, MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, true, false, false },
323 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID
|MS_STRICTATIME
, true, false, false },
324 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, true, false, false },
325 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, true, false, false },
326 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_STRICTATIME
, true, false, false },
328 { "/sys/fs/selinux", "/sys/fs/selinux", NULL
, NULL
, MS_BIND
, false, false, false }, /* Bind mount first */
329 { NULL
, "/sys/fs/selinux", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, false, false, false }, /* Then, make it r/o */
336 for (k
= 0; k
< ELEMENTSOF(mount_table
); k
++) {
337 _cleanup_free_
char *where
= NULL
, *options
= NULL
;
340 if (in_userns
!= mount_table
[k
].in_userns
)
343 if (!use_netns
&& mount_table
[k
].use_netns
)
346 where
= prefix_root(dest
, mount_table
[k
].where
);
350 r
= path_is_mount_point(where
, AT_SYMLINK_FOLLOW
);
351 if (r
< 0 && r
!= -ENOENT
)
352 return log_error_errno(r
, "Failed to detect whether %s is a mount point: %m", where
);
354 /* Skip this entry if it is not a remount. */
355 if (mount_table
[k
].what
&& r
> 0)
358 r
= mkdir_p(where
, 0755);
360 if (mount_table
[k
].fatal
)
361 return log_error_errno(r
, "Failed to create directory %s: %m", where
);
363 log_debug_errno(r
, "Failed to create directory %s: %m", where
);
367 o
= mount_table
[k
].options
;
368 if (streq_ptr(mount_table
[k
].type
, "tmpfs")) {
369 r
= tmpfs_patch_options(o
, use_userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &options
);
376 if (mount(mount_table
[k
].what
,
379 mount_table
[k
].flags
,
382 if (mount_table
[k
].fatal
)
383 return log_error_errno(errno
, "mount(%s) failed: %m", where
);
385 log_warning_errno(errno
, "mount(%s) failed, ignoring: %m", where
);
392 static int parse_mount_bind_options(const char *options
, unsigned long *mount_flags
, char **mount_opts
) {
393 const char *p
= options
;
394 unsigned long flags
= *mount_flags
;
400 _cleanup_free_
char *word
= NULL
;
401 int r
= extract_first_word(&p
, &word
, ",", 0);
403 return log_error_errno(r
, "Failed to extract mount option: %m");
407 if (streq(word
, "rbind"))
409 else if (streq(word
, "norbind"))
412 log_error("Invalid bind mount option: %s", word
);
417 *mount_flags
= flags
;
418 /* in the future mount_opts will hold string options for mount(2) */
424 static int mount_bind(const char *dest
, CustomMount
*m
) {
425 struct stat source_st
, dest_st
;
427 unsigned long mount_flags
= MS_BIND
| MS_REC
;
428 _cleanup_free_
char *mount_opts
= NULL
;
434 r
= parse_mount_bind_options(m
->options
, &mount_flags
, &mount_opts
);
439 if (stat(m
->source
, &source_st
) < 0)
440 return log_error_errno(errno
, "Failed to stat %s: %m", m
->source
);
442 where
= prefix_roota(dest
, m
->destination
);
444 if (stat(where
, &dest_st
) >= 0) {
445 if (S_ISDIR(source_st
.st_mode
) && !S_ISDIR(dest_st
.st_mode
)) {
446 log_error("Cannot bind mount directory %s on file %s.", m
->source
, where
);
450 if (!S_ISDIR(source_st
.st_mode
) && S_ISDIR(dest_st
.st_mode
)) {
451 log_error("Cannot bind mount file %s on directory %s.", m
->source
, where
);
455 } else if (errno
== ENOENT
) {
456 r
= mkdir_parents_label(where
, 0755);
458 return log_error_errno(r
, "Failed to make parents of %s: %m", where
);
460 /* Create the mount point. Any non-directory file can be
461 * mounted on any non-directory file (regular, fifo, socket,
464 if (S_ISDIR(source_st
.st_mode
))
465 r
= mkdir_label(where
, 0755);
469 return log_error_errno(r
, "Failed to create mount point %s: %m", where
);
472 return log_error_errno(errno
, "Failed to stat %s: %m", where
);
475 if (mount(m
->source
, where
, NULL
, mount_flags
, mount_opts
) < 0)
476 return log_error_errno(errno
, "mount(%s) failed: %m", where
);
479 r
= bind_remount_recursive(where
, true);
481 return log_error_errno(r
, "Read-only bind mount failed: %m");
487 static int mount_tmpfs(
490 bool userns
, uid_t uid_shift
, uid_t uid_range
,
491 const char *selinux_apifs_context
) {
493 const char *where
, *options
;
494 _cleanup_free_
char *buf
= NULL
;
500 where
= prefix_roota(dest
, m
->destination
);
502 r
= mkdir_p_label(where
, 0755);
503 if (r
< 0 && r
!= -EEXIST
)
504 return log_error_errno(r
, "Creating mount point for tmpfs %s failed: %m", where
);
506 r
= tmpfs_patch_options(m
->options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
509 options
= r
> 0 ? buf
: m
->options
;
511 if (mount("tmpfs", where
, "tmpfs", MS_NODEV
|MS_STRICTATIME
, options
) < 0)
512 return log_error_errno(errno
, "tmpfs mount to %s failed: %m", where
);
517 static char *joined_and_escaped_lower_dirs(char * const *lower
) {
518 _cleanup_strv_free_
char **sv
= NULL
;
520 sv
= strv_copy(lower
);
526 if (!strv_shell_escape(sv
, ",:"))
529 return strv_join(sv
, ":");
532 static int mount_overlay(const char *dest
, CustomMount
*m
) {
533 _cleanup_free_
char *lower
= NULL
;
534 const char *where
, *options
;
540 where
= prefix_roota(dest
, m
->destination
);
542 r
= mkdir_label(where
, 0755);
543 if (r
< 0 && r
!= -EEXIST
)
544 return log_error_errno(r
, "Creating mount point for overlay %s failed: %m", where
);
546 (void) mkdir_p_label(m
->source
, 0755);
548 lower
= joined_and_escaped_lower_dirs(m
->lower
);
553 _cleanup_free_
char *escaped_source
= NULL
;
555 escaped_source
= shell_escape(m
->source
, ",:");
559 options
= strjoina("lowerdir=", escaped_source
, ":", lower
);
561 _cleanup_free_
char *escaped_source
= NULL
, *escaped_work_dir
= NULL
;
564 (void) mkdir_label(m
->work_dir
, 0700);
566 escaped_source
= shell_escape(m
->source
, ",:");
569 escaped_work_dir
= shell_escape(m
->work_dir
, ",:");
570 if (!escaped_work_dir
)
573 options
= strjoina("lowerdir=", lower
, ",upperdir=", escaped_source
, ",workdir=", escaped_work_dir
);
576 if (mount("overlay", where
, "overlay", m
->read_only
? MS_RDONLY
: 0, options
) < 0)
577 return log_error_errno(errno
, "overlay mount to %s failed: %m", where
);
584 CustomMount
*mounts
, unsigned n
,
585 bool userns
, uid_t uid_shift
, uid_t uid_range
,
586 const char *selinux_apifs_context
) {
593 for (i
= 0; i
< n
; i
++) {
594 CustomMount
*m
= mounts
+ i
;
598 case CUSTOM_MOUNT_BIND
:
599 r
= mount_bind(dest
, m
);
602 case CUSTOM_MOUNT_TMPFS
:
603 r
= mount_tmpfs(dest
, m
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
606 case CUSTOM_MOUNT_OVERLAY
:
607 r
= mount_overlay(dest
, m
);
611 assert_not_reached("Unknown custom mount type");
621 /* Retrieve existing subsystems. This function is called in a new cgroup
624 static int get_controllers(Set
*subsystems
) {
625 _cleanup_fclose_
FILE *f
= NULL
;
630 f
= fopen("/proc/self/cgroup", "re");
632 return errno
== ENOENT
? -ESRCH
: -errno
;
634 FOREACH_LINE(line
, f
, return -errno
) {
640 l
= strchr(line
, ':');
651 if (streq(l
, "") || streq(l
, "name=systemd"))
655 r
= set_consume(subsystems
, p
);
663 static int mount_legacy_cgroup_hierarchy(const char *dest
, const char *controller
, const char *hierarchy
,
664 CGroupUnified unified_requested
, bool read_only
) {
668 to
= strjoina(strempty(dest
), "/sys/fs/cgroup/", hierarchy
);
670 r
= path_is_mount_point(to
, 0);
671 if (r
< 0 && r
!= -ENOENT
)
672 return log_error_errno(r
, "Failed to determine if %s is mounted already: %m", to
);
678 /* The superblock mount options of the mount point need to be
679 * identical to the hosts', and hence writable... */
680 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
681 if (unified_requested
>= CGROUP_UNIFIED_SYSTEMD
)
682 r
= mount("cgroup", to
, "cgroup2", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
684 r
= mount("cgroup", to
, "cgroup", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, "none,name=systemd,xattr");
686 r
= mount("cgroup", to
, "cgroup", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, controller
);
689 return log_error_errno(errno
, "Failed to mount to %s: %m", to
);
691 /* ... hence let's only make the bind mount read-only, not the
694 if (mount(NULL
, to
, NULL
, MS_BIND
|MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_RDONLY
, NULL
) < 0)
695 return log_error_errno(errno
, "Failed to remount %s read-only: %m", to
);
700 /* Mount a legacy cgroup hierarchy when cgroup namespaces are supported. */
701 static int mount_legacy_cgns_supported(
702 CGroupUnified unified_requested
, bool userns
, uid_t uid_shift
,
703 uid_t uid_range
, const char *selinux_apifs_context
) {
704 _cleanup_set_free_free_ Set
*controllers
= NULL
;
705 const char *cgroup_root
= "/sys/fs/cgroup", *c
;
708 (void) mkdir_p(cgroup_root
, 0755);
710 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
711 r
= path_is_mount_point(cgroup_root
, AT_SYMLINK_FOLLOW
);
713 return log_error_errno(r
, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
715 _cleanup_free_
char *options
= NULL
;
717 /* When cgroup namespaces are enabled and user namespaces are
718 * used then the mount of the cgroupfs is done *inside* the new
719 * user namespace. We're root in the new user namespace and the
720 * kernel will happily translate our uid/gid to the correct
721 * uid/gid as seen from e.g. /proc/1/mountinfo. So we simply
722 * pass uid 0 and not uid_shift to tmpfs_patch_options().
724 r
= tmpfs_patch_options("mode=755", userns
, 0, uid_range
, true, selinux_apifs_context
, &options
);
728 if (mount("tmpfs", cgroup_root
, "tmpfs", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
, options
) < 0)
729 return log_error_errno(errno
, "Failed to mount /sys/fs/cgroup: %m");
732 if (cg_all_unified() > 0)
733 goto skip_controllers
;
735 controllers
= set_new(&string_hash_ops
);
739 r
= get_controllers(controllers
);
741 return log_error_errno(r
, "Failed to determine cgroup controllers: %m");
744 _cleanup_free_
const char *controller
= NULL
;
746 controller
= set_steal_first(controllers
);
750 r
= mount_legacy_cgroup_hierarchy("", controller
, controller
, unified_requested
, !userns
);
754 /* When multiple hierarchies are co-mounted, make their
755 * constituting individual hierarchies a symlink to the
760 _cleanup_free_
char *target
= NULL
, *tok
= NULL
;
762 r
= extract_first_word(&c
, &tok
, ",", 0);
764 return log_error_errno(r
, "Failed to extract co-mounted cgroup controller: %m");
768 target
= prefix_root("/sys/fs/cgroup", tok
);
772 if (streq(controller
, tok
))
775 r
= symlink_idempotent(controller
, target
);
777 return log_error_errno(r
, "Invalid existing symlink for combined hierarchy: %m");
779 return log_error_errno(r
, "Failed to create symlink for combined hierarchy: %m");
784 r
= mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER
, "systemd", unified_requested
, false);
789 if (mount(NULL
, cgroup_root
, NULL
, MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
|MS_RDONLY
, "mode=755") < 0)
790 return log_error_errno(errno
, "Failed to remount %s read-only: %m", cgroup_root
);
796 /* Mount legacy cgroup hierarchy when cgroup namespaces are unsupported. */
797 static int mount_legacy_cgns_unsupported(
799 CGroupUnified unified_requested
, bool userns
, uid_t uid_shift
, uid_t uid_range
,
800 const char *selinux_apifs_context
) {
801 _cleanup_set_free_free_ Set
*controllers
= NULL
;
802 const char *cgroup_root
;
805 cgroup_root
= prefix_roota(dest
, "/sys/fs/cgroup");
807 (void) mkdir_p(cgroup_root
, 0755);
809 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
810 r
= path_is_mount_point(cgroup_root
, AT_SYMLINK_FOLLOW
);
812 return log_error_errno(r
, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
814 _cleanup_free_
char *options
= NULL
;
816 r
= tmpfs_patch_options("mode=755", userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &options
);
820 if (mount("tmpfs", cgroup_root
, "tmpfs", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
, options
) < 0)
821 return log_error_errno(errno
, "Failed to mount /sys/fs/cgroup: %m");
824 if (cg_all_unified() > 0)
825 goto skip_controllers
;
827 controllers
= set_new(&string_hash_ops
);
831 r
= cg_kernel_controllers(controllers
);
833 return log_error_errno(r
, "Failed to determine cgroup controllers: %m");
836 _cleanup_free_
char *controller
= NULL
, *origin
= NULL
, *combined
= NULL
;
838 controller
= set_steal_first(controllers
);
842 origin
= prefix_root("/sys/fs/cgroup/", controller
);
846 r
= readlink_malloc(origin
, &combined
);
848 /* Not a symbolic link, but directly a single cgroup hierarchy */
850 r
= mount_legacy_cgroup_hierarchy(dest
, controller
, controller
, unified_requested
, true);
855 return log_error_errno(r
, "Failed to read link %s: %m", origin
);
857 _cleanup_free_
char *target
= NULL
;
859 target
= prefix_root(dest
, origin
);
863 /* A symbolic link, a combination of controllers in one hierarchy */
865 if (!filename_is_valid(combined
)) {
866 log_warning("Ignoring invalid combined hierarchy %s.", combined
);
870 r
= mount_legacy_cgroup_hierarchy(dest
, combined
, combined
, unified_requested
, true);
874 r
= symlink_idempotent(combined
, target
);
876 return log_error_errno(r
, "Invalid existing symlink for combined hierarchy: %m");
878 return log_error_errno(r
, "Failed to create symlink for combined hierarchy: %m");
883 r
= mount_legacy_cgroup_hierarchy(dest
, SYSTEMD_CGROUP_CONTROLLER
, "systemd", unified_requested
, false);
887 if (mount(NULL
, cgroup_root
, NULL
, MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
|MS_RDONLY
, "mode=755") < 0)
888 return log_error_errno(errno
, "Failed to remount %s read-only: %m", cgroup_root
);
893 static int mount_unified_cgroups(const char *dest
) {
899 p
= prefix_roota(dest
, "/sys/fs/cgroup");
901 (void) mkdir_p(p
, 0755);
903 r
= path_is_mount_point(p
, AT_SYMLINK_FOLLOW
);
905 return log_error_errno(r
, "Failed to determine if %s is mounted already: %m", p
);
907 p
= prefix_roota(dest
, "/sys/fs/cgroup/cgroup.procs");
908 if (access(p
, F_OK
) >= 0)
911 return log_error_errno(errno
, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p
);
913 log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p
);
917 if (mount("cgroup", p
, "cgroup2", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
) < 0)
918 return log_error_errno(errno
, "Failed to mount unified cgroup hierarchy to %s: %m", p
);
925 CGroupUnified unified_requested
,
926 bool userns
, uid_t uid_shift
, uid_t uid_range
,
927 const char *selinux_apifs_context
,
930 if (unified_requested
>= CGROUP_UNIFIED_ALL
)
931 return mount_unified_cgroups(dest
);
932 else if (use_cgns
&& cg_ns_supported())
933 return mount_legacy_cgns_supported(unified_requested
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
935 return mount_legacy_cgns_unsupported(dest
, unified_requested
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
938 int mount_systemd_cgroup_writable(
940 CGroupUnified unified_requested
) {
942 _cleanup_free_
char *own_cgroup_path
= NULL
;
943 const char *systemd_root
, *systemd_own
;
948 r
= cg_pid_get_path(NULL
, 0, &own_cgroup_path
);
950 return log_error_errno(r
, "Failed to determine our own cgroup path: %m");
952 /* If we are living in the top-level, then there's nothing to do... */
953 if (path_equal(own_cgroup_path
, "/"))
956 if (unified_requested
>= CGROUP_UNIFIED_ALL
) {
957 systemd_own
= strjoina(dest
, "/sys/fs/cgroup", own_cgroup_path
);
958 systemd_root
= prefix_roota(dest
, "/sys/fs/cgroup");
960 systemd_own
= strjoina(dest
, "/sys/fs/cgroup/systemd", own_cgroup_path
);
961 systemd_root
= prefix_roota(dest
, "/sys/fs/cgroup/systemd");
964 /* Make our own cgroup a (writable) bind mount */
965 if (mount(systemd_own
, systemd_own
, NULL
, MS_BIND
, NULL
) < 0)
966 return log_error_errno(errno
, "Failed to turn %s into a bind mount: %m", own_cgroup_path
);
968 /* And then remount the systemd cgroup root read-only */
969 if (mount(NULL
, systemd_root
, NULL
, MS_BIND
|MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_RDONLY
, NULL
) < 0)
970 return log_error_errno(errno
, "Failed to mount cgroup root read-only: %m");
975 int setup_volatile_state(
976 const char *directory
,
978 bool userns
, uid_t uid_shift
, uid_t uid_range
,
979 const char *selinux_apifs_context
) {
981 _cleanup_free_
char *buf
= NULL
;
982 const char *p
, *options
;
987 if (mode
!= VOLATILE_STATE
)
990 /* --volatile=state means we simply overmount /var
991 with a tmpfs, and the rest read-only. */
993 r
= bind_remount_recursive(directory
, true);
995 return log_error_errno(r
, "Failed to remount %s read-only: %m", directory
);
997 p
= prefix_roota(directory
, "/var");
999 if (r
< 0 && errno
!= EEXIST
)
1000 return log_error_errno(errno
, "Failed to create %s: %m", directory
);
1002 options
= "mode=755";
1003 r
= tmpfs_patch_options(options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
1009 if (mount("tmpfs", p
, "tmpfs", MS_STRICTATIME
, options
) < 0)
1010 return log_error_errno(errno
, "Failed to mount tmpfs to /var: %m");
1016 const char *directory
,
1018 bool userns
, uid_t uid_shift
, uid_t uid_range
,
1019 const char *selinux_apifs_context
) {
1021 bool tmpfs_mounted
= false, bind_mounted
= false;
1022 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1023 _cleanup_free_
char *buf
= NULL
;
1024 const char *f
, *t
, *options
;
1029 if (mode
!= VOLATILE_YES
)
1032 /* --volatile=yes means we mount a tmpfs to the root dir, and
1033 the original /usr to use inside it, and that read-only. */
1035 if (!mkdtemp(template))
1036 return log_error_errno(errno
, "Failed to create temporary directory: %m");
1038 options
= "mode=755";
1039 r
= tmpfs_patch_options(options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
1045 if (mount("tmpfs", template, "tmpfs", MS_STRICTATIME
, options
) < 0) {
1046 r
= log_error_errno(errno
, "Failed to mount tmpfs for root directory: %m");
1050 tmpfs_mounted
= true;
1052 f
= prefix_roota(directory
, "/usr");
1053 t
= prefix_roota(template, "/usr");
1056 if (r
< 0 && errno
!= EEXIST
) {
1057 r
= log_error_errno(errno
, "Failed to create %s: %m", t
);
1061 if (mount(f
, t
, NULL
, MS_BIND
|MS_REC
, NULL
) < 0) {
1062 r
= log_error_errno(errno
, "Failed to create /usr bind mount: %m");
1066 bind_mounted
= true;
1068 r
= bind_remount_recursive(t
, true);
1070 log_error_errno(r
, "Failed to remount %s read-only: %m", t
);
1074 if (mount(template, directory
, NULL
, MS_MOVE
, NULL
) < 0) {
1075 r
= log_error_errno(errno
, "Failed to move root mount: %m");
1079 (void) rmdir(template);
1088 (void) umount(template);
1089 (void) rmdir(template);
1093 VolatileMode
volatile_mode_from_string(const char *s
) {
1097 return _VOLATILE_MODE_INVALID
;
1099 b
= parse_boolean(s
);
1101 return VOLATILE_YES
;
1105 if (streq(s
, "state"))
1106 return VOLATILE_STATE
;
1108 return _VOLATILE_MODE_INVALID
;