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
);
78 if (m
->rm_rf_tmpdir
) {
79 (void) rm_rf(m
->rm_rf_tmpdir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
80 free(m
->rm_rf_tmpdir
);
89 static int custom_mount_compare(const void *a
, const void *b
) {
90 const CustomMount
*x
= a
, *y
= b
;
93 r
= path_compare(x
->destination
, y
->destination
);
97 if (x
->type
< y
->type
)
99 if (x
->type
> y
->type
)
105 static bool source_path_is_valid(const char *p
) {
111 return path_is_absolute(p
);
114 static char *resolve_source_path(const char *dest
, const char *source
) {
119 if (source
[0] == '+')
120 return prefix_root(dest
, source
+ 1);
122 return strdup(source
);
125 int custom_mount_prepare_all(const char *dest
, CustomMount
*l
, unsigned n
) {
129 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
130 * parent process, so that we know the temporary directories to remove on exit before we fork off the
135 /* Order the custom mounts, and make sure we have a working directory */
136 qsort_safe(l
, n
, sizeof(CustomMount
), custom_mount_compare
);
138 for (i
= 0; i
< n
; i
++) {
139 CustomMount
*m
= l
+ i
;
144 s
= resolve_source_path(dest
, m
->source
);
151 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
153 m
->rm_rf_tmpdir
= strdup("/var/tmp/nspawn-temp-XXXXXX");
154 if (!m
->rm_rf_tmpdir
)
157 if (!mkdtemp(m
->rm_rf_tmpdir
)) {
158 m
->rm_rf_tmpdir
= mfree(m
->rm_rf_tmpdir
);
159 return log_error_errno(errno
, "Failed to acquire temporary directory: %m");
162 m
->source
= strjoin(m
->rm_rf_tmpdir
, "/src");
166 if (mkdir(m
->source
, 0755) < 0)
167 return log_error_errno(errno
, "Failed to create %s: %m", m
->source
);
170 if (m
->type
== CUSTOM_MOUNT_OVERLAY
) {
173 STRV_FOREACH(j
, m
->lower
) {
176 s
= resolve_source_path(dest
, *j
);
187 s
= resolve_source_path(dest
, m
->work_dir
);
196 r
= tempfn_random(m
->source
, NULL
, &m
->work_dir
);
198 return log_error_errno(r
, "Failed to acquire working directory: %m");
201 (void) mkdir_label(m
->work_dir
, 0700);
208 int bind_mount_parse(CustomMount
**l
, unsigned *n
, const char *s
, bool read_only
) {
209 _cleanup_free_
char *source
= NULL
, *destination
= NULL
, *opts
= NULL
;
217 r
= extract_many_words(&p
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
, &source
, &destination
, NULL
);
223 destination
= strdup(source
[0] == '+' ? source
+1 : source
);
227 if (r
== 2 && !isempty(p
)) {
235 else if (!source_path_is_valid(source
))
238 if (!path_is_absolute(destination
))
241 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_BIND
);
246 m
->destination
= destination
;
247 m
->read_only
= read_only
;
250 source
= destination
= opts
= NULL
;
254 int tmpfs_mount_parse(CustomMount
**l
, unsigned *n
, const char *s
) {
255 _cleanup_free_
char *path
= NULL
, *opts
= NULL
;
264 r
= extract_first_word(&p
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
271 opts
= strdup("mode=0755");
277 if (!path_is_absolute(path
))
280 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_TMPFS
);
284 m
->destination
= path
;
291 int overlay_mount_parse(CustomMount
**l
, unsigned *n
, const char *s
, bool read_only
) {
292 _cleanup_free_
char *upper
= NULL
, *destination
= NULL
;
293 _cleanup_strv_free_
char **lower
= NULL
;
297 k
= strv_split_extract(&lower
, s
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
301 return -EADDRNOTAVAIL
;
303 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
304 * we'll also define the destination mount point the same as the upper. */
306 if (!source_path_is_valid(lower
[0]) ||
307 !source_path_is_valid(lower
[1]))
313 destination
= strdup(upper
[0] == '+' ? upper
+1 : upper
); /* take the destination without "+" prefix */
319 /* If more than two parameters are specified, the last one is the destination, the second to last one
320 * the "upper", and all before that the "lower" directories. */
322 destination
= lower
[k
- 1];
323 upper
= lower
[k
- 2];
326 STRV_FOREACH(i
, lower
)
327 if (!source_path_is_valid(*i
))
330 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
334 else if (!source_path_is_valid(upper
))
337 if (!path_is_absolute(destination
))
341 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_OVERLAY
);
345 m
->destination
= destination
;
348 m
->read_only
= read_only
;
350 upper
= destination
= NULL
;
356 static int tmpfs_patch_options(
359 uid_t uid_shift
, uid_t uid_range
,
361 const char *selinux_apifs_context
,
366 if ((userns
&& uid_shift
!= 0) || patch_ids
) {
367 assert(uid_shift
!= UID_INVALID
);
369 if (asprintf(&buf
, "%s%suid=" UID_FMT
",gid=" UID_FMT
,
370 options
?: "", options
? "," : "",
371 uid_shift
, uid_shift
) < 0)
378 if (selinux_apifs_context
) {
381 t
= strjoin(options
?: "", options
? "," : "",
382 "context=\"", selinux_apifs_context
, "\"");
391 if (!buf
&& options
) {
392 buf
= strdup(options
);
401 int mount_sysfs(const char *dest
, MountSettingsMask mount_settings
) {
402 const char *full
, *top
, *x
;
404 unsigned long extra_flags
= 0;
406 top
= prefix_roota(dest
, "/sys");
407 r
= path_check_fstype(top
, SYSFS_MAGIC
);
409 return log_error_errno(r
, "Failed to determine filesystem type of %s: %m", top
);
410 /* /sys might already be mounted as sysfs by the outer child in the
411 * !netns case. In this case, it's all good. Don't touch it because we
412 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
417 full
= prefix_roota(top
, "/full");
419 (void) mkdir(full
, 0755);
421 if (mount_settings
& MOUNT_APPLY_APIVFS_RO
)
422 extra_flags
|= MS_RDONLY
;
424 r
= mount_verbose(LOG_ERR
, "sysfs", full
, "sysfs",
425 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|extra_flags
, NULL
);
429 FOREACH_STRING(x
, "block", "bus", "class", "dev", "devices", "kernel") {
430 _cleanup_free_
char *from
= NULL
, *to
= NULL
;
432 from
= prefix_root(full
, x
);
436 to
= prefix_root(top
, x
);
440 (void) mkdir(to
, 0755);
442 r
= mount_verbose(LOG_ERR
, from
, to
, NULL
, MS_BIND
, NULL
);
446 r
= mount_verbose(LOG_ERR
, NULL
, to
, NULL
,
447 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
452 r
= umount_verbose(full
);
457 return log_error_errno(errno
, "Failed to remove %s: %m", full
);
459 x
= prefix_roota(top
, "/fs/kdbus");
460 (void) mkdir_p(x
, 0755);
462 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
463 * remount /sys read-only.
465 if (cg_ns_supported()) {
466 x
= prefix_roota(top
, "/fs/cgroup");
467 (void) mkdir_p(x
, 0755);
470 return mount_verbose(LOG_ERR
, NULL
, top
, NULL
,
471 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
474 static int mkdir_userns(const char *path
, mode_t mode
, MountSettingsMask mask
, uid_t uid_shift
) {
479 r
= mkdir(path
, mode
);
480 if (r
< 0 && errno
!= EEXIST
)
483 if ((mask
& MOUNT_USE_USERNS
) == 0)
486 if (mask
& MOUNT_IN_USERNS
)
489 r
= lchown(path
, uid_shift
, uid_shift
);
496 static int mkdir_userns_p(const char *prefix
, const char *path
, mode_t mode
, MountSettingsMask mask
, uid_t uid_shift
) {
502 if (prefix
&& !path_startswith(path
, prefix
))
505 /* create every parent directory in the path, except the last component */
506 p
= path
+ strspn(path
, "/");
508 char t
[strlen(path
) + 1];
510 e
= p
+ strcspn(p
, "/");
511 p
= e
+ strspn(e
, "/");
513 /* Is this the last component? If so, then we're done */
517 memcpy(t
, path
, e
- path
);
520 if (prefix
&& path_startswith(prefix
, t
))
523 r
= mkdir_userns(t
, mode
, mask
, uid_shift
);
528 return mkdir_userns(path
, mode
, mask
, uid_shift
);
531 int mount_all(const char *dest
,
532 MountSettingsMask mount_settings
,
533 uid_t uid_shift
, uid_t uid_range
,
534 const char *selinux_apifs_context
) {
536 typedef struct MountPoint
{
542 MountSettingsMask mount_settings
;
545 static const MountPoint mount_table
[] = {
546 /* inner child mounts */
547 { "proc", "/proc", "proc", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, MOUNT_FATAL
|MOUNT_IN_USERNS
},
548 { "/proc/sys", "/proc/sys", NULL
, NULL
, MS_BIND
, MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* Bind mount first ... */
549 { "/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) */
550 { 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 */
551 { "/proc/sysrq-trigger", "/proc/sysrq-trigger", NULL
, NULL
, MS_BIND
, MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* Bind mount first ... */
552 { 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 */
554 /* outer child mounts */
555 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, MOUNT_FATAL
},
556 { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, MOUNT_FATAL
|MOUNT_APPLY_APIVFS_NETNS
},
557 { "sysfs", "/sys", "sysfs", NULL
, MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, MOUNT_FATAL
|MOUNT_APPLY_APIVFS_RO
}, /* skipped if above was mounted */
558 { "sysfs", "/sys", "sysfs", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, MOUNT_FATAL
}, /* skipped if above was mounted */
560 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID
|MS_STRICTATIME
, MOUNT_FATAL
},
561 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, MOUNT_FATAL
},
562 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
, MOUNT_FATAL
},
564 { "/sys/fs/selinux", "/sys/fs/selinux", NULL
, NULL
, MS_BIND
, 0 }, /* Bind mount first */
565 { NULL
, "/sys/fs/selinux", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
, 0 }, /* Then, make it r/o */
571 bool use_userns
= (mount_settings
& MOUNT_USE_USERNS
);
572 bool netns
= (mount_settings
& MOUNT_APPLY_APIVFS_NETNS
);
573 bool ro
= (mount_settings
& MOUNT_APPLY_APIVFS_RO
);
574 bool in_userns
= (mount_settings
& MOUNT_IN_USERNS
);
576 for (k
= 0; k
< ELEMENTSOF(mount_table
); k
++) {
577 _cleanup_free_
char *where
= NULL
, *options
= NULL
;
579 bool fatal
= (mount_table
[k
].mount_settings
& MOUNT_FATAL
);
581 if (in_userns
!= (bool)(mount_table
[k
].mount_settings
& MOUNT_IN_USERNS
))
584 if (!netns
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_APIVFS_NETNS
))
587 if (!ro
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_APIVFS_RO
))
590 r
= chase_symlinks(mount_table
[k
].where
, dest
, CHASE_NONEXISTENT
|CHASE_PREFIX_ROOT
, &where
);
592 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, mount_table
[k
].where
);
594 r
= path_is_mount_point(where
, NULL
, 0);
595 if (r
< 0 && r
!= -ENOENT
)
596 return log_error_errno(r
, "Failed to detect whether %s is a mount point: %m", where
);
598 /* Skip this entry if it is not a remount. */
599 if (mount_table
[k
].what
&& r
> 0)
602 r
= mkdir_userns_p(dest
, where
, 0755, mount_settings
, uid_shift
);
603 if (r
< 0 && r
!= -EEXIST
) {
605 return log_error_errno(r
, "Failed to create directory %s: %m", where
);
607 log_debug_errno(r
, "Failed to create directory %s: %m", where
);
611 o
= mount_table
[k
].options
;
612 if (streq_ptr(mount_table
[k
].type
, "tmpfs")) {
614 r
= tmpfs_patch_options(o
, use_userns
, 0, uid_range
, true, selinux_apifs_context
, &options
);
616 r
= tmpfs_patch_options(o
, use_userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &options
);
623 r
= mount_verbose(fatal
? LOG_ERR
: LOG_DEBUG
,
627 mount_table
[k
].flags
,
636 static int parse_mount_bind_options(const char *options
, unsigned long *mount_flags
, char **mount_opts
) {
637 const char *p
= options
;
638 unsigned long flags
= *mount_flags
;
645 _cleanup_free_
char *word
= NULL
;
647 r
= extract_first_word(&p
, &word
, ",", 0);
649 return log_error_errno(r
, "Failed to extract mount option: %m");
653 if (streq(word
, "rbind"))
655 else if (streq(word
, "norbind"))
658 log_error("Invalid bind mount option: %s", word
);
663 *mount_flags
= flags
;
664 /* in the future mount_opts will hold string options for mount(2) */
670 static int mount_bind(const char *dest
, CustomMount
*m
) {
672 _cleanup_free_
char *mount_opts
= NULL
, *where
= NULL
;
673 unsigned long mount_flags
= MS_BIND
| MS_REC
;
674 struct stat source_st
, dest_st
;
681 r
= parse_mount_bind_options(m
->options
, &mount_flags
, &mount_opts
);
686 if (stat(m
->source
, &source_st
) < 0)
687 return log_error_errno(errno
, "Failed to stat %s: %m", m
->source
);
689 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
);
691 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
692 if (r
> 0) { /* Path exists already? */
694 if (stat(where
, &dest_st
) < 0)
695 return log_error_errno(errno
, "Failed to stat %s: %m", where
);
697 if (S_ISDIR(source_st
.st_mode
) && !S_ISDIR(dest_st
.st_mode
)) {
698 log_error("Cannot bind mount directory %s on file %s.", m
->source
, where
);
702 if (!S_ISDIR(source_st
.st_mode
) && S_ISDIR(dest_st
.st_mode
)) {
703 log_error("Cannot bind mount file %s on directory %s.", m
->source
, where
);
707 } else { /* Path doesn't exist yet? */
708 r
= mkdir_parents_label(where
, 0755);
710 return log_error_errno(r
, "Failed to make parents of %s: %m", where
);
712 /* Create the mount point. Any non-directory file can be
713 * mounted on any non-directory file (regular, fifo, socket,
716 if (S_ISDIR(source_st
.st_mode
))
717 r
= mkdir_label(where
, 0755);
721 return log_error_errno(r
, "Failed to create mount point %s: %m", where
);
725 r
= mount_verbose(LOG_ERR
, m
->source
, where
, NULL
, mount_flags
, mount_opts
);
730 r
= bind_remount_recursive(where
, true, NULL
);
732 return log_error_errno(r
, "Read-only bind mount failed: %m");
738 static int mount_tmpfs(
741 bool userns
, uid_t uid_shift
, uid_t uid_range
,
742 const char *selinux_apifs_context
) {
745 _cleanup_free_
char *buf
= NULL
, *where
= NULL
;
751 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
);
753 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
754 if (r
== 0) { /* Doesn't exist yet? */
755 r
= mkdir_p_label(where
, 0755);
757 return log_error_errno(r
, "Creating mount point for tmpfs %s failed: %m", where
);
760 r
= tmpfs_patch_options(m
->options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
763 options
= r
> 0 ? buf
: m
->options
;
765 return mount_verbose(LOG_ERR
, "tmpfs", where
, "tmpfs", MS_NODEV
|MS_STRICTATIME
, options
);
768 static char *joined_and_escaped_lower_dirs(char **lower
) {
769 _cleanup_strv_free_
char **sv
= NULL
;
771 sv
= strv_copy(lower
);
777 if (!strv_shell_escape(sv
, ",:"))
780 return strv_join(sv
, ":");
783 static int mount_overlay(const char *dest
, CustomMount
*m
) {
785 _cleanup_free_
char *lower
= NULL
, *where
= NULL
, *escaped_source
= NULL
;
792 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
);
794 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
795 if (r
== 0) { /* Doesn't exist yet? */
796 r
= mkdir_label(where
, 0755);
798 return log_error_errno(r
, "Creating mount point for overlay %s failed: %m", where
);
801 (void) mkdir_p_label(m
->source
, 0755);
803 lower
= joined_and_escaped_lower_dirs(m
->lower
);
807 escaped_source
= shell_escape(m
->source
, ",:");
812 options
= strjoina("lowerdir=", escaped_source
, ":", lower
);
814 _cleanup_free_
char *escaped_work_dir
= NULL
;
816 escaped_work_dir
= shell_escape(m
->work_dir
, ",:");
817 if (!escaped_work_dir
)
820 options
= strjoina("lowerdir=", lower
, ",upperdir=", escaped_source
, ",workdir=", escaped_work_dir
);
823 return mount_verbose(LOG_ERR
, "overlay", where
, "overlay", m
->read_only
? MS_RDONLY
: 0, options
);
828 CustomMount
*mounts
, unsigned n
,
829 bool userns
, uid_t uid_shift
, uid_t uid_range
,
830 const char *selinux_apifs_context
) {
837 for (i
= 0; i
< n
; i
++) {
838 CustomMount
*m
= mounts
+ i
;
842 case CUSTOM_MOUNT_BIND
:
843 r
= mount_bind(dest
, m
);
846 case CUSTOM_MOUNT_TMPFS
:
847 r
= mount_tmpfs(dest
, m
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
850 case CUSTOM_MOUNT_OVERLAY
:
851 r
= mount_overlay(dest
, m
);
855 assert_not_reached("Unknown custom mount type");
865 /* Retrieve existing subsystems. This function is called in a new cgroup
868 static int get_controllers(Set
*subsystems
) {
869 _cleanup_fclose_
FILE *f
= NULL
;
874 f
= fopen("/proc/self/cgroup", "re");
876 return errno
== ENOENT
? -ESRCH
: -errno
;
878 FOREACH_LINE(line
, f
, return -errno
) {
882 l
= strchr(line
, ':');
893 if (STR_IN_SET(l
, "", "name=systemd", "name=unified"))
900 r
= set_consume(subsystems
, p
);
908 static int mount_legacy_cgroup_hierarchy(
910 const char *controller
,
911 const char *hierarchy
,
914 const char *to
, *fstype
, *opts
;
917 to
= strjoina(strempty(dest
), "/sys/fs/cgroup/", hierarchy
);
919 r
= path_is_mount_point(to
, dest
, 0);
920 if (r
< 0 && r
!= -ENOENT
)
921 return log_error_errno(r
, "Failed to determine if %s is mounted already: %m", to
);
927 /* The superblock mount options of the mount point need to be
928 * identical to the hosts', and hence writable... */
929 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER_HYBRID
)) {
932 } else if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER_LEGACY
)) {
934 opts
= "none,name=systemd,xattr";
940 r
= mount_verbose(LOG_ERR
, "cgroup", to
, fstype
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, opts
);
944 /* ... hence let's only make the bind mount read-only, not the superblock. */
946 r
= mount_verbose(LOG_ERR
, NULL
, to
, NULL
,
947 MS_BIND
|MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_RDONLY
, NULL
);
955 /* Mount a legacy cgroup hierarchy when cgroup namespaces are supported. */
956 static int mount_legacy_cgns_supported(
958 CGroupUnified unified_requested
,
962 const char *selinux_apifs_context
) {
964 _cleanup_set_free_free_ Set
*controllers
= NULL
;
965 const char *cgroup_root
= "/sys/fs/cgroup", *c
;
968 (void) mkdir_p(cgroup_root
, 0755);
970 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
971 r
= path_is_mount_point(cgroup_root
, dest
, AT_SYMLINK_FOLLOW
);
973 return log_error_errno(r
, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
975 _cleanup_free_
char *options
= NULL
;
977 /* When cgroup namespaces are enabled and user namespaces are
978 * used then the mount of the cgroupfs is done *inside* the new
979 * user namespace. We're root in the new user namespace and the
980 * kernel will happily translate our uid/gid to the correct
981 * uid/gid as seen from e.g. /proc/1/mountinfo. So we simply
982 * pass uid 0 and not uid_shift to tmpfs_patch_options().
984 r
= tmpfs_patch_options("mode=755", userns
, 0, uid_range
, true, selinux_apifs_context
, &options
);
988 r
= mount_verbose(LOG_ERR
, "tmpfs", cgroup_root
, "tmpfs",
989 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
, options
);
994 r
= cg_all_unified();
998 goto skip_controllers
;
1000 controllers
= set_new(&string_hash_ops
);
1004 r
= get_controllers(controllers
);
1006 return log_error_errno(r
, "Failed to determine cgroup controllers: %m");
1009 _cleanup_free_
const char *controller
= NULL
;
1011 controller
= set_steal_first(controllers
);
1015 r
= mount_legacy_cgroup_hierarchy("", controller
, controller
, !userns
);
1019 /* When multiple hierarchies are co-mounted, make their
1020 * constituting individual hierarchies a symlink to the
1025 _cleanup_free_
char *target
= NULL
, *tok
= NULL
;
1027 r
= extract_first_word(&c
, &tok
, ",", 0);
1029 return log_error_errno(r
, "Failed to extract co-mounted cgroup controller: %m");
1033 target
= prefix_root("/sys/fs/cgroup", tok
);
1037 if (streq(controller
, tok
))
1040 r
= symlink_idempotent(controller
, target
);
1042 return log_error_errno(r
, "Invalid existing symlink for combined hierarchy: %m");
1044 return log_error_errno(r
, "Failed to create symlink for combined hierarchy: %m");
1049 if (unified_requested
>= CGROUP_UNIFIED_SYSTEMD
) {
1050 r
= mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER_HYBRID
, "unified", false);
1055 r
= mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER_LEGACY
, "systemd", false);
1060 return mount_verbose(LOG_ERR
, NULL
, cgroup_root
, NULL
,
1061 MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
|MS_RDONLY
, "mode=755");
1066 /* Mount legacy cgroup hierarchy when cgroup namespaces are unsupported. */
1067 static int mount_legacy_cgns_unsupported(
1069 CGroupUnified unified_requested
,
1073 const char *selinux_apifs_context
) {
1075 _cleanup_set_free_free_ Set
*controllers
= NULL
;
1076 const char *cgroup_root
;
1079 cgroup_root
= prefix_roota(dest
, "/sys/fs/cgroup");
1081 (void) mkdir_p(cgroup_root
, 0755);
1083 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
1084 r
= path_is_mount_point(cgroup_root
, dest
, AT_SYMLINK_FOLLOW
);
1086 return log_error_errno(r
, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
1088 _cleanup_free_
char *options
= NULL
;
1090 r
= tmpfs_patch_options("mode=755", userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &options
);
1094 r
= mount_verbose(LOG_ERR
, "tmpfs", cgroup_root
, "tmpfs",
1095 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
, options
);
1100 r
= cg_all_unified();
1104 goto skip_controllers
;
1106 controllers
= set_new(&string_hash_ops
);
1110 r
= cg_kernel_controllers(controllers
);
1112 return log_error_errno(r
, "Failed to determine cgroup controllers: %m");
1115 _cleanup_free_
char *controller
= NULL
, *origin
= NULL
, *combined
= NULL
;
1117 controller
= set_steal_first(controllers
);
1121 origin
= prefix_root("/sys/fs/cgroup/", controller
);
1125 r
= readlink_malloc(origin
, &combined
);
1127 /* Not a symbolic link, but directly a single cgroup hierarchy */
1129 r
= mount_legacy_cgroup_hierarchy(dest
, controller
, controller
, true);
1134 return log_error_errno(r
, "Failed to read link %s: %m", origin
);
1136 _cleanup_free_
char *target
= NULL
;
1138 target
= prefix_root(dest
, origin
);
1142 /* A symbolic link, a combination of controllers in one hierarchy */
1144 if (!filename_is_valid(combined
)) {
1145 log_warning("Ignoring invalid combined hierarchy %s.", combined
);
1149 r
= mount_legacy_cgroup_hierarchy(dest
, combined
, combined
, true);
1153 r
= symlink_idempotent(combined
, target
);
1155 return log_error_errno(r
, "Invalid existing symlink for combined hierarchy: %m");
1157 return log_error_errno(r
, "Failed to create symlink for combined hierarchy: %m");
1162 if (unified_requested
>= CGROUP_UNIFIED_SYSTEMD
) {
1163 r
= mount_legacy_cgroup_hierarchy(dest
, SYSTEMD_CGROUP_CONTROLLER_HYBRID
, "unified", false);
1168 r
= mount_legacy_cgroup_hierarchy(dest
, SYSTEMD_CGROUP_CONTROLLER_LEGACY
, "systemd", false);
1172 return mount_verbose(LOG_ERR
, NULL
, cgroup_root
, NULL
,
1173 MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_STRICTATIME
|MS_RDONLY
, "mode=755");
1176 static int mount_unified_cgroups(const char *dest
) {
1182 p
= prefix_roota(dest
, "/sys/fs/cgroup");
1184 (void) mkdir_p(p
, 0755);
1186 r
= path_is_mount_point(p
, dest
, AT_SYMLINK_FOLLOW
);
1188 return log_error_errno(r
, "Failed to determine if %s is mounted already: %m", p
);
1190 p
= prefix_roota(dest
, "/sys/fs/cgroup/cgroup.procs");
1191 if (access(p
, F_OK
) >= 0)
1193 if (errno
!= ENOENT
)
1194 return log_error_errno(errno
, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p
);
1196 log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p
);
1200 return mount_verbose(LOG_ERR
, "cgroup", p
, "cgroup2", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
, NULL
);
1205 CGroupUnified unified_requested
,
1209 const char *selinux_apifs_context
,
1212 if (unified_requested
>= CGROUP_UNIFIED_ALL
)
1213 return mount_unified_cgroups(dest
);
1215 return mount_legacy_cgns_supported(dest
, unified_requested
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
1217 return mount_legacy_cgns_unsupported(dest
, unified_requested
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
1220 static int mount_systemd_cgroup_writable_one(const char *systemd_own
, const char *systemd_root
)
1224 /* Make our own cgroup a (writable) bind mount */
1225 r
= mount_verbose(LOG_ERR
, systemd_own
, systemd_own
, NULL
, MS_BIND
, NULL
);
1229 /* And then remount the systemd cgroup root read-only */
1230 return mount_verbose(LOG_ERR
, NULL
, systemd_root
, NULL
,
1231 MS_BIND
|MS_REMOUNT
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_RDONLY
, NULL
);
1234 int mount_systemd_cgroup_writable(
1236 CGroupUnified unified_requested
) {
1238 _cleanup_free_
char *own_cgroup_path
= NULL
;
1243 r
= cg_pid_get_path(NULL
, 0, &own_cgroup_path
);
1245 return log_error_errno(r
, "Failed to determine our own cgroup path: %m");
1247 /* If we are living in the top-level, then there's nothing to do... */
1248 if (path_equal(own_cgroup_path
, "/"))
1251 if (unified_requested
>= CGROUP_UNIFIED_ALL
)
1252 return mount_systemd_cgroup_writable_one(strjoina(dest
, "/sys/fs/cgroup", own_cgroup_path
),
1253 prefix_roota(dest
, "/sys/fs/cgroup"));
1255 if (unified_requested
>= CGROUP_UNIFIED_SYSTEMD
) {
1256 r
= mount_systemd_cgroup_writable_one(strjoina(dest
, "/sys/fs/cgroup/unified", own_cgroup_path
),
1257 prefix_roota(dest
, "/sys/fs/cgroup/unified"));
1262 return mount_systemd_cgroup_writable_one(strjoina(dest
, "/sys/fs/cgroup/systemd", own_cgroup_path
),
1263 prefix_roota(dest
, "/sys/fs/cgroup/systemd"));
1266 int setup_volatile_state(
1267 const char *directory
,
1269 bool userns
, uid_t uid_shift
, uid_t uid_range
,
1270 const char *selinux_apifs_context
) {
1272 _cleanup_free_
char *buf
= NULL
;
1273 const char *p
, *options
;
1278 if (mode
!= VOLATILE_STATE
)
1281 /* --volatile=state means we simply overmount /var
1282 with a tmpfs, and the rest read-only. */
1284 r
= bind_remount_recursive(directory
, true, NULL
);
1286 return log_error_errno(r
, "Failed to remount %s read-only: %m", directory
);
1288 p
= prefix_roota(directory
, "/var");
1290 if (r
< 0 && errno
!= EEXIST
)
1291 return log_error_errno(errno
, "Failed to create %s: %m", directory
);
1293 options
= "mode=755";
1294 r
= tmpfs_patch_options(options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
1300 return mount_verbose(LOG_ERR
, "tmpfs", p
, "tmpfs", MS_STRICTATIME
, options
);
1304 const char *directory
,
1306 bool userns
, uid_t uid_shift
, uid_t uid_range
,
1307 const char *selinux_apifs_context
) {
1309 bool tmpfs_mounted
= false, bind_mounted
= false;
1310 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1311 _cleanup_free_
char *buf
= NULL
;
1312 const char *f
, *t
, *options
;
1317 if (mode
!= VOLATILE_YES
)
1320 /* --volatile=yes means we mount a tmpfs to the root dir, and
1321 the original /usr to use inside it, and that read-only. */
1323 if (!mkdtemp(template))
1324 return log_error_errno(errno
, "Failed to create temporary directory: %m");
1326 options
= "mode=755";
1327 r
= tmpfs_patch_options(options
, userns
, uid_shift
, uid_range
, false, selinux_apifs_context
, &buf
);
1333 r
= mount_verbose(LOG_ERR
, "tmpfs", template, "tmpfs", MS_STRICTATIME
, options
);
1337 tmpfs_mounted
= true;
1339 f
= prefix_roota(directory
, "/usr");
1340 t
= prefix_roota(template, "/usr");
1343 if (r
< 0 && errno
!= EEXIST
) {
1344 r
= log_error_errno(errno
, "Failed to create %s: %m", t
);
1348 r
= mount_verbose(LOG_ERR
, f
, t
, NULL
, MS_BIND
|MS_REC
, NULL
);
1352 bind_mounted
= true;
1354 r
= bind_remount_recursive(t
, true, NULL
);
1356 log_error_errno(r
, "Failed to remount %s read-only: %m", t
);
1360 r
= mount_verbose(LOG_ERR
, template, directory
, NULL
, MS_MOVE
, NULL
);
1364 (void) rmdir(template);
1370 (void) umount_verbose(t
);
1373 (void) umount_verbose(template);
1374 (void) rmdir(template);
1378 /* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1379 int pivot_root_parse(char **pivot_root_new
, char **pivot_root_old
, const char *s
) {
1380 _cleanup_free_
char *root_new
= NULL
, *root_old
= NULL
;
1384 assert(pivot_root_new
);
1385 assert(pivot_root_old
);
1387 r
= extract_first_word(&p
, &root_new
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
1396 root_old
= strdup(p
);
1401 if (!path_is_absolute(root_new
))
1403 if (root_old
&& !path_is_absolute(root_old
))
1406 free_and_replace(*pivot_root_new
, root_new
);
1407 free_and_replace(*pivot_root_old
, root_old
);
1412 int setup_pivot_root(const char *directory
, const char *pivot_root_new
, const char *pivot_root_old
) {
1413 _cleanup_free_
char *directory_pivot_root_new
= NULL
;
1414 _cleanup_free_
char *pivot_tmp_pivot_root_old
= NULL
;
1415 char pivot_tmp
[] = "/tmp/nspawn-pivot-XXXXXX";
1416 bool remove_pivot_tmp
= false;
1421 if (!pivot_root_new
)
1424 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1425 * If pivot_root_old is NULL, the existing / disappears.
1426 * This requires a temporary directory, pivot_tmp, which is
1427 * not a child of either.
1429 * This is typically used for OSTree-style containers, where
1430 * the root partition contains several sysroots which could be
1431 * run. Normally, one would be chosen by the bootloader and
1432 * pivoted to / by initramfs.
1434 * For example, for an OSTree deployment, pivot_root_new
1435 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1436 * code doesn’t do the /var mount which OSTree expects: use
1437 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1439 * So in the OSTree case, we’ll end up with something like:
1440 * - directory = /tmp/nspawn-root-123456
1441 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1442 * - pivot_root_old = /sysroot
1443 * - directory_pivot_root_new =
1444 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1445 * - pivot_tmp = /tmp/nspawn-pivot-123456
1446 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1448 * Requires all file systems at directory and below to be mounted
1449 * MS_PRIVATE or MS_SLAVE so they can be moved.
1451 directory_pivot_root_new
= prefix_root(directory
, pivot_root_new
);
1453 /* Remount directory_pivot_root_new to make it movable. */
1454 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, directory_pivot_root_new
, NULL
, MS_BIND
, NULL
);
1458 if (pivot_root_old
) {
1459 if (!mkdtemp(pivot_tmp
)) {
1460 r
= log_error_errno(errno
, "Failed to create temporary directory: %m");
1464 remove_pivot_tmp
= true;
1465 pivot_tmp_pivot_root_old
= prefix_root(pivot_tmp
, pivot_root_old
);
1467 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, pivot_tmp
, NULL
, MS_MOVE
, NULL
);
1471 r
= mount_verbose(LOG_ERR
, directory
, pivot_tmp_pivot_root_old
, NULL
, MS_MOVE
, NULL
);
1475 r
= mount_verbose(LOG_ERR
, pivot_tmp
, directory
, NULL
, MS_MOVE
, NULL
);
1479 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, directory
, NULL
, MS_MOVE
, NULL
);
1485 if (remove_pivot_tmp
)
1486 (void) rmdir(pivot_tmp
);