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_multiply(*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 int overlay_mount_parse(CustomMount
**l
, unsigned *n
, const char *s
, bool read_only
) {
184 _cleanup_free_
char *upper
= NULL
, *destination
= NULL
;
185 _cleanup_strv_free_
char **lower
= NULL
;
191 r
= strv_split_extract(&lower
, s
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
195 STRV_FOREACH(i
, lower
) {
196 if (!path_is_absolute(*i
))
203 return -EADDRNOTAVAIL
;
205 /* If two parameters are specified,
206 * the first one is the lower, the
207 * second one the upper directory. And
208 * we'll also define the destination
209 * mount point the same as the upper. */
213 destination
= strdup(upper
);
218 upper
= lower
[k
- 2];
219 destination
= lower
[k
- 1];
223 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_OVERLAY
);
227 m
->destination
= destination
;
230 m
->read_only
= read_only
;
232 upper
= destination
= NULL
;
238 static int tmpfs_patch_options(
241 uid_t uid_shift
, uid_t uid_range
,
243 const char *selinux_apifs_context
,
248 if ((userns
&& uid_shift
!= 0) || patch_ids
) {
249 assert(uid_shift
!= UID_INVALID
);
251 if (asprintf(&buf
, "%s%suid=" UID_FMT
",gid=" UID_FMT
,
252 options
?: "", options
? "," : "",
253 uid_shift
, uid_shift
) < 0)
260 if (selinux_apifs_context
) {
263 t
= strjoin(options
?: "", options
? "," : "",
264 "context=\"", selinux_apifs_context
, "\"");
273 if (!buf
&& options
) {
274 buf
= strdup(options
);
283 int mount_sysfs(const char *dest
, MountSettingsMask mount_settings
) {
284 const char *full
, *top
, *x
;
286 unsigned long extra_flags
= 0;
288 top
= prefix_roota(dest
, "/sys");
289 r
= path_check_fstype(top
, SYSFS_MAGIC
);
291 return log_error_errno(r
, "Failed to determine filesystem type of %s: %m", top
);
292 /* /sys might already be mounted as sysfs by the outer child in the
293 * !netns case. In this case, it's all good. Don't touch it because we
294 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
299 full
= prefix_roota(top
, "/full");
301 (void) mkdir(full
, 0755);
303 if (mount_settings
& MOUNT_APPLY_APIVFS_RO
)
304 extra_flags
|= MS_RDONLY
;
306 r
= mount_verbose(LOG_ERR
, "sysfs", full
, "sysfs",
307 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|extra_flags
, NULL
);
311 FOREACH_STRING(x
, "block", "bus", "class", "dev", "devices", "kernel") {
312 _cleanup_free_
char *from
= NULL
, *to
= NULL
;
314 from
= prefix_root(full
, x
);
318 to
= prefix_root(top
, x
);
322 (void) mkdir(to
, 0755);
324 r
= mount_verbose(LOG_ERR
, from
, to
, NULL
, MS_BIND
, NULL
);
328 r
= mount_verbose(LOG_ERR
, NULL
, to
, NULL
,
329 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
334 r
= umount_verbose(full
);
339 return log_error_errno(errno
, "Failed to remove %s: %m", full
);
341 x
= prefix_roota(top
, "/fs/kdbus");
342 (void) mkdir_p(x
, 0755);
344 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
345 * remount /sys read-only.
347 if (cg_ns_supported()) {
348 x
= prefix_roota(top
, "/fs/cgroup");
349 (void) mkdir_p(x
, 0755);
352 return mount_verbose(LOG_ERR
, NULL
, top
, NULL
,
353 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
356 static int mkdir_userns(const char *path
, mode_t mode
, MountSettingsMask mask
, uid_t uid_shift
) {
361 r
= mkdir(path
, mode
);
362 if (r
< 0 && errno
!= EEXIST
)
365 if ((mask
& MOUNT_USE_USERNS
) == 0)
368 if (mask
& MOUNT_IN_USERNS
)
371 r
= lchown(path
, uid_shift
, uid_shift
);
378 static int mkdir_userns_p(const char *prefix
, const char *path
, mode_t mode
, MountSettingsMask mask
, uid_t uid_shift
) {
384 if (prefix
&& !path_startswith(path
, prefix
))
387 /* create every parent directory in the path, except the last component */
388 p
= path
+ strspn(path
, "/");
390 char t
[strlen(path
) + 1];
392 e
= p
+ strcspn(p
, "/");
393 p
= e
+ strspn(e
, "/");
395 /* Is this the last component? If so, then we're done */
399 memcpy(t
, path
, e
- path
);
402 if (prefix
&& path_startswith(prefix
, t
))
405 r
= mkdir_userns(t
, mode
, mask
, uid_shift
);
410 return mkdir_userns(path
, mode
, mask
, uid_shift
);
413 int mount_all(const char *dest
,
414 MountSettingsMask mount_settings
,
415 uid_t uid_shift
, uid_t uid_range
,
416 const char *selinux_apifs_context
) {
418 typedef struct MountPoint
{
424 MountSettingsMask mount_settings
;
427 static const MountPoint mount_table
[] = {
428 /* inner child mounts */
429 { "proc", "/proc", "proc", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, MOUNT_FATAL
|MOUNT_IN_USERNS
},
430 { "/proc/sys", "/proc/sys", NULL
, NULL
, MS_BIND
, MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* Bind mount first ...*/
431 { "/proc/sys/net", "/proc/sys/net", NULL
, NULL
, MS_BIND
, MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
|MOUNT_APPLY_APIVFS_NETNS
}, /* (except for this) */
432 { NULL
, "/proc/sys", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* ... then, make it r/o */
433 { "/proc/sysrq-trigger", "/proc/sysrq-trigger", NULL
, NULL
, MS_BIND
, MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* Bind mount first ...*/
434 { NULL
, "/proc/sysrq-trigger", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* ... then, make it r/o */
435 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_STRICTATIME
, MOUNT_FATAL
|MOUNT_IN_USERNS
},
437 /* outer child mounts */
438 { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, MOUNT_FATAL
|MOUNT_APPLY_APIVFS_NETNS
},
439 { "sysfs", "/sys", "sysfs", NULL
, MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, MOUNT_FATAL
|MOUNT_APPLY_APIVFS_RO
}, /* skipped if above was mounted */
440 { "sysfs", "/sys", "sysfs", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, MOUNT_FATAL
}, /* skipped if above was mounted */
442 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID
|MS_STRICTATIME
, MOUNT_FATAL
},
443 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, MOUNT_FATAL
},
444 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, MOUNT_FATAL
},
446 { "/sys/fs/selinux", "/sys/fs/selinux", NULL
, NULL
, MS_BIND
, 0 }, /* Bind mount first */
447 { NULL
, "/sys/fs/selinux", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, 0 }, /* Then, make it r/o */
453 bool use_userns
= (mount_settings
& MOUNT_USE_USERNS
);
454 bool netns
= (mount_settings
& MOUNT_APPLY_APIVFS_NETNS
);
455 bool ro
= (mount_settings
& MOUNT_APPLY_APIVFS_RO
);
456 bool in_userns
= (mount_settings
& MOUNT_IN_USERNS
);
458 for (k
= 0; k
< ELEMENTSOF(mount_table
); k
++) {
459 _cleanup_free_
char *where
= NULL
, *options
= NULL
;
461 bool fatal
= (mount_table
[k
].mount_settings
& MOUNT_FATAL
);
463 if (in_userns
!= (bool)(mount_table
[k
].mount_settings
& MOUNT_IN_USERNS
))
466 if (!netns
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_APIVFS_NETNS
))
469 if (!ro
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_APIVFS_RO
))
472 r
= chase_symlinks(mount_table
[k
].where
, dest
, CHASE_NON_EXISTING
|CHASE_PREFIX_ROOT
, &where
);
474 return log_error_errno(r
, "Failed to resolve %s: %m", mount_table
[k
].where
);
476 r
= path_is_mount_point(where
, NULL
, 0);
477 if (r
< 0 && r
!= -ENOENT
)
478 return log_error_errno(r
, "Failed to detect whether %s is a mount point: %m", where
);
480 /* Skip this entry if it is not a remount. */
481 if (mount_table
[k
].what
&& r
> 0)
484 r
= mkdir_userns_p(dest
, where
, 0755, mount_settings
, uid_shift
);
485 if (r
< 0 && r
!= -EEXIST
) {
487 return log_error_errno(r
, "Failed to create directory %s: %m", where
);
489 log_debug_errno(r
, "Failed to create directory %s: %m", where
);
493 o
= mount_table
[k
].options
;
494 if (streq_ptr(mount_table
[k
].type
, "tmpfs")) {
496 r
= tmpfs_patch_options(o
, use_userns
, 0, uid_range
, true, selinux_apifs_context
, &options
);
498 r
= tmpfs_patch_options(o
, use_userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &options
);
505 r
= mount_verbose(fatal
? LOG_ERR
: LOG_DEBUG
,
509 mount_table
[k
].flags
,
518 static int parse_mount_bind_options(const char *options
, unsigned long *mount_flags
, char **mount_opts
) {
519 const char *p
= options
;
520 unsigned long flags
= *mount_flags
;
527 _cleanup_free_
char *word
= NULL
;
529 r
= extract_first_word(&p
, &word
, ",", 0);
531 return log_error_errno(r
, "Failed to extract mount option: %m");
535 if (streq(word
, "rbind"))
537 else if (streq(word
, "norbind"))
540 log_error("Invalid bind mount option: %s", word
);
545 *mount_flags
= flags
;
546 /* in the future mount_opts will hold string options for mount(2) */
552 static int mount_bind(const char *dest
, CustomMount
*m
) {
554 _cleanup_free_
char *mount_opts
= NULL
, *where
= NULL
;
555 unsigned long mount_flags
= MS_BIND
| MS_REC
;
556 struct stat source_st
, dest_st
;
562 r
= parse_mount_bind_options(m
->options
, &mount_flags
, &mount_opts
);
567 if (stat(m
->source
, &source_st
) < 0)
568 return log_error_errno(errno
, "Failed to stat %s: %m", m
->source
);
570 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NON_EXISTING
, &where
);
572 return log_error_errno(r
, "Failed to resolve %s: %m", m
->destination
);
573 if (r
> 0) { /* Path exists already? */
575 if (stat(where
, &dest_st
) < 0)
576 return log_error_errno(errno
, "Failed to stat %s: %m", where
);
578 if (S_ISDIR(source_st
.st_mode
) && !S_ISDIR(dest_st
.st_mode
)) {
579 log_error("Cannot bind mount directory %s on file %s.", m
->source
, where
);
583 if (!S_ISDIR(source_st
.st_mode
) && S_ISDIR(dest_st
.st_mode
)) {
584 log_error("Cannot bind mount file %s on directory %s.", m
->source
, where
);
588 } else { /* Path doesn't exist yet? */
589 r
= mkdir_parents_label(where
, 0755);
591 return log_error_errno(r
, "Failed to make parents of %s: %m", where
);
593 /* Create the mount point. Any non-directory file can be
594 * mounted on any non-directory file (regular, fifo, socket,
597 if (S_ISDIR(source_st
.st_mode
))
598 r
= mkdir_label(where
, 0755);
602 return log_error_errno(r
, "Failed to create mount point %s: %m", where
);
606 r
= mount_verbose(LOG_ERR
, m
->source
, where
, NULL
, mount_flags
, mount_opts
);
611 r
= bind_remount_recursive(where
, true, NULL
);
613 return log_error_errno(r
, "Read-only bind mount failed: %m");
619 static int mount_tmpfs(
622 bool userns
, uid_t uid_shift
, uid_t uid_range
,
623 const char *selinux_apifs_context
) {
626 _cleanup_free_
char *buf
= NULL
, *where
= NULL
;
632 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NON_EXISTING
, &where
);
634 return log_error_errno(r
, "Failed to resolve %s: %m", m
->destination
);
635 if (r
== 0) { /* Doesn't exist yet? */
636 r
= mkdir_p_label(where
, 0755);
638 return log_error_errno(r
, "Creating mount point for tmpfs %s failed: %m", where
);
641 r
= tmpfs_patch_options(m
->options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
644 options
= r
> 0 ? buf
: m
->options
;
646 return mount_verbose(LOG_ERR
, "tmpfs", where
, "tmpfs", MS_NODEV
|MS_STRICTATIME
, options
);
649 static char *joined_and_escaped_lower_dirs(char * const *lower
) {
650 _cleanup_strv_free_
char **sv
= NULL
;
652 sv
= strv_copy(lower
);
658 if (!strv_shell_escape(sv
, ",:"))
661 return strv_join(sv
, ":");
664 static int mount_overlay(const char *dest
, CustomMount
*m
) {
666 _cleanup_free_
char *lower
= NULL
, *where
= NULL
;
673 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NON_EXISTING
, &where
);
675 return log_error_errno(r
, "Failed to resolve %s: %m", m
->destination
);
676 if (r
== 0) { /* Doesn't exist yet? */
677 r
= mkdir_label(where
, 0755);
679 return log_error_errno(r
, "Creating mount point for overlay %s failed: %m", where
);
682 (void) mkdir_p_label(m
->source
, 0755);
684 lower
= joined_and_escaped_lower_dirs(m
->lower
);
689 _cleanup_free_
char *escaped_source
= NULL
;
691 escaped_source
= shell_escape(m
->source
, ",:");
695 options
= strjoina("lowerdir=", escaped_source
, ":", lower
);
697 _cleanup_free_
char *escaped_source
= NULL
, *escaped_work_dir
= NULL
;
700 (void) mkdir_label(m
->work_dir
, 0700);
702 escaped_source
= shell_escape(m
->source
, ",:");
705 escaped_work_dir
= shell_escape(m
->work_dir
, ",:");
706 if (!escaped_work_dir
)
709 options
= strjoina("lowerdir=", lower
, ",upperdir=", escaped_source
, ",workdir=", escaped_work_dir
);
712 return mount_verbose(LOG_ERR
, "overlay", where
, "overlay", m
->read_only
? MS_RDONLY
: 0, options
);
717 CustomMount
*mounts
, unsigned n
,
718 bool userns
, uid_t uid_shift
, uid_t uid_range
,
719 const char *selinux_apifs_context
) {
726 for (i
= 0; i
< n
; i
++) {
727 CustomMount
*m
= mounts
+ i
;
731 case CUSTOM_MOUNT_BIND
:
732 r
= mount_bind(dest
, m
);
735 case CUSTOM_MOUNT_TMPFS
:
736 r
= mount_tmpfs(dest
, m
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
739 case CUSTOM_MOUNT_OVERLAY
:
740 r
= mount_overlay(dest
, m
);
744 assert_not_reached("Unknown custom mount type");
754 /* Retrieve existing subsystems. This function is called in a new cgroup
757 static int get_controllers(Set
*subsystems
) {
758 _cleanup_fclose_
FILE *f
= NULL
;
763 f
= fopen("/proc/self/cgroup", "re");
765 return errno
== ENOENT
? -ESRCH
: -errno
;
767 FOREACH_LINE(line
, f
, return -errno
) {
771 l
= strchr(line
, ':');
782 if (STR_IN_SET(l
, "", "name=systemd"))
789 r
= set_consume(subsystems
, p
);
797 static int mount_legacy_cgroup_hierarchy(
799 const char *controller
,
800 const char *hierarchy
,
801 CGroupUnified unified_requested
,
804 const char *to
, *fstype
, *opts
;
807 to
= strjoina(strempty(dest
), "/sys/fs/cgroup/", hierarchy
);
809 r
= path_is_mount_point(to
, dest
, 0);
810 if (r
< 0 && r
!= -ENOENT
)
811 return log_error_errno(r
, "Failed to determine if %s is mounted already: %m", to
);
817 /* The superblock mount options of the mount point need to be
818 * identical to the hosts', and hence writable... */
819 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
820 if (unified_requested
>= CGROUP_UNIFIED_SYSTEMD
) {
825 opts
= "none,name=systemd,xattr";
832 r
= mount_verbose(LOG_ERR
, "cgroup", to
, fstype
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, opts
);
836 /* ... hence let's only make the bind mount read-only, not the superblock. */
838 r
= mount_verbose(LOG_ERR
, NULL
, to
, NULL
,
839 MS_BIND
|MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_RDONLY
, NULL
);
847 /* Mount a legacy cgroup hierarchy when cgroup namespaces are supported. */
848 static int mount_legacy_cgns_supported(
850 CGroupUnified unified_requested
,
854 const char *selinux_apifs_context
) {
856 _cleanup_set_free_free_ Set
*controllers
= NULL
;
857 const char *cgroup_root
= "/sys/fs/cgroup", *c
;
860 (void) mkdir_p(cgroup_root
, 0755);
862 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
863 r
= path_is_mount_point(cgroup_root
, dest
, AT_SYMLINK_FOLLOW
);
865 return log_error_errno(r
, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
867 _cleanup_free_
char *options
= NULL
;
869 /* When cgroup namespaces are enabled and user namespaces are
870 * used then the mount of the cgroupfs is done *inside* the new
871 * user namespace. We're root in the new user namespace and the
872 * kernel will happily translate our uid/gid to the correct
873 * uid/gid as seen from e.g. /proc/1/mountinfo. So we simply
874 * pass uid 0 and not uid_shift to tmpfs_patch_options().
876 r
= tmpfs_patch_options("mode=755", userns
, 0, uid_range
, true, selinux_apifs_context
, &options
);
880 r
= mount_verbose(LOG_ERR
, "tmpfs", cgroup_root
, "tmpfs",
881 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
, options
);
886 if (cg_all_unified() > 0)
887 goto skip_controllers
;
889 controllers
= set_new(&string_hash_ops
);
893 r
= get_controllers(controllers
);
895 return log_error_errno(r
, "Failed to determine cgroup controllers: %m");
898 _cleanup_free_
const char *controller
= NULL
;
900 controller
= set_steal_first(controllers
);
904 r
= mount_legacy_cgroup_hierarchy("", controller
, controller
, unified_requested
, !userns
);
908 /* When multiple hierarchies are co-mounted, make their
909 * constituting individual hierarchies a symlink to the
914 _cleanup_free_
char *target
= NULL
, *tok
= NULL
;
916 r
= extract_first_word(&c
, &tok
, ",", 0);
918 return log_error_errno(r
, "Failed to extract co-mounted cgroup controller: %m");
922 target
= prefix_root("/sys/fs/cgroup", tok
);
926 if (streq(controller
, tok
))
929 r
= symlink_idempotent(controller
, target
);
931 return log_error_errno(r
, "Invalid existing symlink for combined hierarchy: %m");
933 return log_error_errno(r
, "Failed to create symlink for combined hierarchy: %m");
938 r
= mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER
, "systemd", unified_requested
, false);
943 return mount_verbose(LOG_ERR
, NULL
, cgroup_root
, NULL
,
944 MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
|MS_RDONLY
, "mode=755");
949 /* Mount legacy cgroup hierarchy when cgroup namespaces are unsupported. */
950 static int mount_legacy_cgns_unsupported(
952 CGroupUnified unified_requested
,
956 const char *selinux_apifs_context
) {
958 _cleanup_set_free_free_ Set
*controllers
= NULL
;
959 const char *cgroup_root
;
962 cgroup_root
= prefix_roota(dest
, "/sys/fs/cgroup");
964 (void) mkdir_p(cgroup_root
, 0755);
966 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
967 r
= path_is_mount_point(cgroup_root
, dest
, AT_SYMLINK_FOLLOW
);
969 return log_error_errno(r
, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
971 _cleanup_free_
char *options
= NULL
;
973 r
= tmpfs_patch_options("mode=755", userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &options
);
977 r
= mount_verbose(LOG_ERR
, "tmpfs", cgroup_root
, "tmpfs",
978 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
, options
);
983 if (cg_all_unified() > 0)
984 goto skip_controllers
;
986 controllers
= set_new(&string_hash_ops
);
990 r
= cg_kernel_controllers(controllers
);
992 return log_error_errno(r
, "Failed to determine cgroup controllers: %m");
995 _cleanup_free_
char *controller
= NULL
, *origin
= NULL
, *combined
= NULL
;
997 controller
= set_steal_first(controllers
);
1001 origin
= prefix_root("/sys/fs/cgroup/", controller
);
1005 r
= readlink_malloc(origin
, &combined
);
1007 /* Not a symbolic link, but directly a single cgroup hierarchy */
1009 r
= mount_legacy_cgroup_hierarchy(dest
, controller
, controller
, unified_requested
, true);
1014 return log_error_errno(r
, "Failed to read link %s: %m", origin
);
1016 _cleanup_free_
char *target
= NULL
;
1018 target
= prefix_root(dest
, origin
);
1022 /* A symbolic link, a combination of controllers in one hierarchy */
1024 if (!filename_is_valid(combined
)) {
1025 log_warning("Ignoring invalid combined hierarchy %s.", combined
);
1029 r
= mount_legacy_cgroup_hierarchy(dest
, combined
, combined
, unified_requested
, true);
1033 r
= symlink_idempotent(combined
, target
);
1035 return log_error_errno(r
, "Invalid existing symlink for combined hierarchy: %m");
1037 return log_error_errno(r
, "Failed to create symlink for combined hierarchy: %m");
1042 r
= mount_legacy_cgroup_hierarchy(dest
, SYSTEMD_CGROUP_CONTROLLER
, "systemd", unified_requested
, false);
1046 return mount_verbose(LOG_ERR
, NULL
, cgroup_root
, NULL
,
1047 MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
|MS_RDONLY
, "mode=755");
1050 static int mount_unified_cgroups(const char *dest
) {
1056 p
= prefix_roota(dest
, "/sys/fs/cgroup");
1058 (void) mkdir_p(p
, 0755);
1060 r
= path_is_mount_point(p
, dest
, AT_SYMLINK_FOLLOW
);
1062 return log_error_errno(r
, "Failed to determine if %s is mounted already: %m", p
);
1064 p
= prefix_roota(dest
, "/sys/fs/cgroup/cgroup.procs");
1065 if (access(p
, F_OK
) >= 0)
1067 if (errno
!= ENOENT
)
1068 return log_error_errno(errno
, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p
);
1070 log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p
);
1074 return mount_verbose(LOG_ERR
, "cgroup", p
, "cgroup2", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
1079 CGroupUnified unified_requested
,
1083 const char *selinux_apifs_context
,
1086 if (unified_requested
>= CGROUP_UNIFIED_ALL
)
1087 return mount_unified_cgroups(dest
);
1089 return mount_legacy_cgns_supported(dest
, unified_requested
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
1091 return mount_legacy_cgns_unsupported(dest
, unified_requested
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
1094 int mount_systemd_cgroup_writable(
1096 CGroupUnified unified_requested
) {
1098 _cleanup_free_
char *own_cgroup_path
= NULL
;
1099 const char *systemd_root
, *systemd_own
;
1104 r
= cg_pid_get_path(NULL
, 0, &own_cgroup_path
);
1106 return log_error_errno(r
, "Failed to determine our own cgroup path: %m");
1108 /* If we are living in the top-level, then there's nothing to do... */
1109 if (path_equal(own_cgroup_path
, "/"))
1112 if (unified_requested
>= CGROUP_UNIFIED_ALL
) {
1113 systemd_own
= strjoina(dest
, "/sys/fs/cgroup", own_cgroup_path
);
1114 systemd_root
= prefix_roota(dest
, "/sys/fs/cgroup");
1116 systemd_own
= strjoina(dest
, "/sys/fs/cgroup/systemd", own_cgroup_path
);
1117 systemd_root
= prefix_roota(dest
, "/sys/fs/cgroup/systemd");
1120 /* Make our own cgroup a (writable) bind mount */
1121 r
= mount_verbose(LOG_ERR
, systemd_own
, systemd_own
, NULL
, MS_BIND
, NULL
);
1125 /* And then remount the systemd cgroup root read-only */
1126 return mount_verbose(LOG_ERR
, NULL
, systemd_root
, NULL
,
1127 MS_BIND
|MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_RDONLY
, NULL
);
1130 int setup_volatile_state(
1131 const char *directory
,
1133 bool userns
, uid_t uid_shift
, uid_t uid_range
,
1134 const char *selinux_apifs_context
) {
1136 _cleanup_free_
char *buf
= NULL
;
1137 const char *p
, *options
;
1142 if (mode
!= VOLATILE_STATE
)
1145 /* --volatile=state means we simply overmount /var
1146 with a tmpfs, and the rest read-only. */
1148 r
= bind_remount_recursive(directory
, true, NULL
);
1150 return log_error_errno(r
, "Failed to remount %s read-only: %m", directory
);
1152 p
= prefix_roota(directory
, "/var");
1154 if (r
< 0 && errno
!= EEXIST
)
1155 return log_error_errno(errno
, "Failed to create %s: %m", directory
);
1157 options
= "mode=755";
1158 r
= tmpfs_patch_options(options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
1164 return mount_verbose(LOG_ERR
, "tmpfs", p
, "tmpfs", MS_STRICTATIME
, options
);
1168 const char *directory
,
1170 bool userns
, uid_t uid_shift
, uid_t uid_range
,
1171 const char *selinux_apifs_context
) {
1173 bool tmpfs_mounted
= false, bind_mounted
= false;
1174 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1175 _cleanup_free_
char *buf
= NULL
;
1176 const char *f
, *t
, *options
;
1181 if (mode
!= VOLATILE_YES
)
1184 /* --volatile=yes means we mount a tmpfs to the root dir, and
1185 the original /usr to use inside it, and that read-only. */
1187 if (!mkdtemp(template))
1188 return log_error_errno(errno
, "Failed to create temporary directory: %m");
1190 options
= "mode=755";
1191 r
= tmpfs_patch_options(options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
1197 r
= mount_verbose(LOG_ERR
, "tmpfs", template, "tmpfs", MS_STRICTATIME
, options
);
1201 tmpfs_mounted
= true;
1203 f
= prefix_roota(directory
, "/usr");
1204 t
= prefix_roota(template, "/usr");
1207 if (r
< 0 && errno
!= EEXIST
) {
1208 r
= log_error_errno(errno
, "Failed to create %s: %m", t
);
1212 r
= mount_verbose(LOG_ERR
, f
, t
, NULL
, MS_BIND
|MS_REC
, NULL
);
1216 bind_mounted
= true;
1218 r
= bind_remount_recursive(t
, true, NULL
);
1220 log_error_errno(r
, "Failed to remount %s read-only: %m", t
);
1224 r
= mount_verbose(LOG_ERR
, template, directory
, NULL
, MS_MOVE
, NULL
);
1228 (void) rmdir(template);
1234 (void) umount_verbose(t
);
1237 (void) umount_verbose(template);
1238 (void) rmdir(template);
1242 VolatileMode
volatile_mode_from_string(const char *s
) {
1246 return _VOLATILE_MODE_INVALID
;
1248 b
= parse_boolean(s
);
1250 return VOLATILE_YES
;
1254 if (streq(s
, "state"))
1255 return VOLATILE_STATE
;
1257 return _VOLATILE_MODE_INVALID
;