1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/magic.h>
6 #include "alloc-util.h"
9 #include "format-util.h"
13 #include "mount-util.h"
14 #include "mountpoint-util.h"
15 #include "nspawn-mount.h"
16 #include "parse-util.h"
17 #include "path-util.h"
20 #include "sort-util.h"
21 #include "stat-util.h"
22 #include "string-util.h"
24 #include "tmpfile-util.h"
25 #include "user-util.h"
27 CustomMount
* custom_mount_add(CustomMount
**l
, size_t *n
, CustomMountType t
) {
33 assert(t
< _CUSTOM_MOUNT_TYPE_MAX
);
35 c
= reallocarray(*l
, *n
+ 1, sizeof(CustomMount
));
43 *ret
= (CustomMount
) { .type
= t
};
48 void custom_mount_free_all(CustomMount
*l
, size_t n
) {
51 for (i
= 0; i
< n
; i
++) {
52 CustomMount
*m
= l
+ i
;
59 (void) rm_rf(m
->work_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
63 if (m
->rm_rf_tmpdir
) {
64 (void) rm_rf(m
->rm_rf_tmpdir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
65 free(m
->rm_rf_tmpdir
);
69 free(m
->type_argument
);
75 static int custom_mount_compare(const CustomMount
*a
, const CustomMount
*b
) {
78 r
= path_compare(a
->destination
, b
->destination
);
82 return CMP(a
->type
, b
->type
);
85 static bool source_path_is_valid(const char *p
) {
91 return path_is_absolute(p
);
94 static char *resolve_source_path(const char *dest
, const char *source
) {
100 return path_join(dest
, source
+ 1);
102 return strdup(source
);
105 int custom_mount_prepare_all(const char *dest
, CustomMount
*l
, size_t n
) {
109 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
110 * parent process, so that we know the temporary directories to remove on exit before we fork off the
115 /* Order the custom mounts, and make sure we have a working directory */
116 typesafe_qsort(l
, n
, custom_mount_compare
);
118 for (i
= 0; i
< n
; i
++) {
119 CustomMount
*m
= l
+ i
;
121 /* /proc we mount in the inner child, i.e. when we acquired CLONE_NEWPID. All other mounts we mount
122 * already in the outer child, so that the mounts are already established before CLONE_NEWPID and in
123 * particular CLONE_NEWUSER. This also means any custom mounts below /proc also need to be mounted in
124 * the inner child, not the outer one. Determine this here. */
125 m
->in_userns
= path_startswith(m
->destination
, "/proc");
127 if (m
->type
== CUSTOM_MOUNT_BIND
) {
131 s
= resolve_source_path(dest
, m
->source
);
135 free_and_replace(m
->source
, s
);
137 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
139 m
->rm_rf_tmpdir
= strdup("/var/tmp/nspawn-temp-XXXXXX");
140 if (!m
->rm_rf_tmpdir
)
143 if (!mkdtemp(m
->rm_rf_tmpdir
)) {
144 m
->rm_rf_tmpdir
= mfree(m
->rm_rf_tmpdir
);
145 return log_error_errno(errno
, "Failed to acquire temporary directory: %m");
148 m
->source
= path_join(m
->rm_rf_tmpdir
, "src");
152 if (mkdir(m
->source
, 0755) < 0)
153 return log_error_errno(errno
, "Failed to create %s: %m", m
->source
);
157 if (m
->type
== CUSTOM_MOUNT_OVERLAY
) {
160 STRV_FOREACH(j
, m
->lower
) {
163 s
= resolve_source_path(dest
, *j
);
167 free_and_replace(*j
, s
);
173 s
= resolve_source_path(dest
, m
->work_dir
);
177 free_and_replace(m
->work_dir
, s
);
181 r
= tempfn_random(m
->source
, NULL
, &m
->work_dir
);
183 return log_error_errno(r
, "Failed to acquire working directory: %m");
186 (void) mkdir_label(m
->work_dir
, 0700);
193 int bind_mount_parse(CustomMount
**l
, size_t *n
, const char *s
, bool read_only
) {
194 _cleanup_free_
char *source
= NULL
, *destination
= NULL
, *opts
= NULL
;
202 r
= extract_many_words(&p
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
, &source
, &destination
, NULL
);
208 destination
= strdup(source
[0] == '+' ? source
+1 : source
);
212 if (r
== 2 && !isempty(p
)) {
219 source
= mfree(source
);
220 else if (!source_path_is_valid(source
))
223 if (!path_is_absolute(destination
))
225 if (empty_or_root(destination
))
228 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_BIND
);
232 m
->source
= TAKE_PTR(source
);
233 m
->destination
= TAKE_PTR(destination
);
234 m
->read_only
= read_only
;
235 m
->options
= TAKE_PTR(opts
);
240 int tmpfs_mount_parse(CustomMount
**l
, size_t *n
, const char *s
) {
241 _cleanup_free_
char *path
= NULL
, *opts
= NULL
;
250 r
= extract_first_word(&p
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
257 opts
= strdup("mode=0755");
263 if (!path_is_absolute(path
))
265 if (empty_or_root(path
))
268 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_TMPFS
);
272 m
->destination
= TAKE_PTR(path
);
273 m
->options
= TAKE_PTR(opts
);
278 int overlay_mount_parse(CustomMount
**l
, size_t *n
, const char *s
, bool read_only
) {
279 _cleanup_free_
char *upper
= NULL
, *destination
= NULL
;
280 _cleanup_strv_free_
char **lower
= NULL
;
284 k
= strv_split_extract(&lower
, s
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
288 return -EADDRNOTAVAIL
;
290 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
291 * we'll also define the destination mount point the same as the upper. */
293 if (!source_path_is_valid(lower
[0]) ||
294 !source_path_is_valid(lower
[1]))
297 upper
= TAKE_PTR(lower
[1]);
299 destination
= strdup(upper
[0] == '+' ? upper
+1 : upper
); /* take the destination without "+" prefix */
305 /* If more than two parameters are specified, the last one is the destination, the second to last one
306 * the "upper", and all before that the "lower" directories. */
308 destination
= lower
[k
- 1];
309 upper
= TAKE_PTR(lower
[k
- 2]);
311 STRV_FOREACH(i
, lower
)
312 if (!source_path_is_valid(*i
))
315 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
318 upper
= mfree(upper
);
319 else if (!source_path_is_valid(upper
))
322 if (!path_is_absolute(destination
))
326 if (empty_or_root(destination
))
329 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_OVERLAY
);
333 m
->destination
= TAKE_PTR(destination
);
334 m
->source
= TAKE_PTR(upper
);
335 m
->lower
= TAKE_PTR(lower
);
336 m
->read_only
= read_only
;
341 int inaccessible_mount_parse(CustomMount
**l
, size_t *n
, const char *s
) {
342 _cleanup_free_
char *path
= NULL
;
349 if (!path_is_absolute(s
))
356 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_INACCESSIBLE
);
360 m
->destination
= TAKE_PTR(path
);
364 int tmpfs_patch_options(
367 const char *selinux_apifs_context
,
372 if (uid_shift
!= UID_INVALID
) {
373 if (asprintf(&buf
, "%s%suid=" UID_FMT
",gid=" UID_FMT
,
374 strempty(options
), options
? "," : "",
375 uid_shift
, uid_shift
) < 0)
382 if (selinux_apifs_context
) {
385 t
= strjoin(strempty(options
), options
? "," : "",
386 "context=\"", selinux_apifs_context
, "\"");
395 if (!buf
&& options
) {
396 buf
= strdup(options
);
405 int mount_sysfs(const char *dest
, MountSettingsMask mount_settings
) {
406 const char *full
, *top
, *x
;
408 unsigned long extra_flags
= 0;
410 top
= prefix_roota(dest
, "/sys");
411 r
= path_is_fs_type(top
, SYSFS_MAGIC
);
413 return log_error_errno(r
, "Failed to determine filesystem type of %s: %m", top
);
414 /* /sys might already be mounted as sysfs by the outer child in the
415 * !netns case. In this case, it's all good. Don't touch it because we
416 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
421 full
= prefix_roota(top
, "/full");
423 (void) mkdir(full
, 0755);
425 if (mount_settings
& MOUNT_APPLY_APIVFS_RO
)
426 extra_flags
|= MS_RDONLY
;
428 r
= mount_verbose(LOG_ERR
, "sysfs", full
, "sysfs",
429 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|extra_flags
, NULL
);
433 FOREACH_STRING(x
, "block", "bus", "class", "dev", "devices", "kernel") {
434 _cleanup_free_
char *from
= NULL
, *to
= NULL
;
436 from
= path_join(full
, x
);
440 to
= path_join(top
, x
);
444 (void) mkdir(to
, 0755);
446 r
= mount_verbose(LOG_ERR
, from
, to
, NULL
, MS_BIND
, NULL
);
450 r
= mount_verbose(LOG_ERR
, NULL
, to
, NULL
,
451 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
456 r
= umount_verbose(full
);
461 return log_error_errno(errno
, "Failed to remove %s: %m", full
);
463 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
464 * remount /sys read-only.
466 x
= prefix_roota(top
, "/fs/cgroup");
467 (void) mkdir_p(x
, 0755);
469 return mount_verbose(LOG_ERR
, NULL
, top
, NULL
,
470 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
473 static int mkdir_userns(const char *path
, mode_t mode
, uid_t uid_shift
) {
478 r
= mkdir_errno_wrapper(path
, mode
);
479 if (r
< 0 && r
!= -EEXIST
)
482 if (uid_shift
== UID_INVALID
)
485 if (lchown(path
, uid_shift
, uid_shift
) < 0)
491 static int mkdir_userns_p(const char *prefix
, const char *path
, mode_t mode
, uid_t uid_shift
) {
497 if (prefix
&& !path_startswith(path
, prefix
))
500 /* create every parent directory in the path, except the last component */
501 p
= path
+ strspn(path
, "/");
503 char t
[strlen(path
) + 1];
505 e
= p
+ strcspn(p
, "/");
506 p
= e
+ strspn(e
, "/");
508 /* Is this the last component? If so, then we're done */
512 memcpy(t
, path
, e
- path
);
515 if (prefix
&& path_startswith(prefix
, t
))
518 r
= mkdir_userns(t
, mode
, uid_shift
);
523 return mkdir_userns(path
, mode
, uid_shift
);
526 int mount_all(const char *dest
,
527 MountSettingsMask mount_settings
,
529 const char *selinux_apifs_context
) {
531 #define PROC_INACCESSIBLE_REG(path) \
532 { "/run/systemd/inaccessible/reg", (path), NULL, NULL, MS_BIND, \
533 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
534 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
535 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
537 #define PROC_READ_ONLY(path) \
538 { (path), (path), NULL, NULL, MS_BIND, \
539 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
540 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
541 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
543 typedef struct MountPoint
{
549 MountSettingsMask mount_settings
;
552 static const MountPoint mount_table
[] = {
553 /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing) */
554 { "proc", "/proc", "proc", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
555 MOUNT_FATAL
|MOUNT_IN_USERNS
},
557 { "/proc/sys", "/proc/sys", NULL
, NULL
, MS_BIND
,
558 MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* Bind mount first ... */
560 { "/proc/sys/net", "/proc/sys/net", NULL
, NULL
, MS_BIND
,
561 MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
|MOUNT_APPLY_APIVFS_NETNS
}, /* (except for this) */
563 { NULL
, "/proc/sys", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
564 MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* ... then, make it r/o */
566 /* Make these files inaccessible to container payloads: they potentially leak information about kernel
567 * internals or the host's execution environment to the container */
568 PROC_INACCESSIBLE_REG("/proc/kallsyms"),
569 PROC_INACCESSIBLE_REG("/proc/kcore"),
570 PROC_INACCESSIBLE_REG("/proc/keys"),
571 PROC_INACCESSIBLE_REG("/proc/sysrq-trigger"),
572 PROC_INACCESSIBLE_REG("/proc/timer_list"),
574 /* Make these directories read-only to container payloads: they show hardware information, and in some
575 * cases contain tunables the container really shouldn't have access to. */
576 PROC_READ_ONLY("/proc/acpi"),
577 PROC_READ_ONLY("/proc/apm"),
578 PROC_READ_ONLY("/proc/asound"),
579 PROC_READ_ONLY("/proc/bus"),
580 PROC_READ_ONLY("/proc/fs"),
581 PROC_READ_ONLY("/proc/irq"),
582 PROC_READ_ONLY("/proc/scsi"),
584 { "mqueue", "/dev/mqueue", "mqueue", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
587 /* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing) */
588 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
589 MOUNT_FATAL
|MOUNT_APPLY_TMPFS_TMP
},
590 { "tmpfs", "/sys", "tmpfs", "mode=555", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
591 MOUNT_FATAL
|MOUNT_APPLY_APIVFS_NETNS
},
592 { "sysfs", "/sys", "sysfs", NULL
, MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
593 MOUNT_FATAL
|MOUNT_APPLY_APIVFS_RO
}, /* skipped if above was mounted */
594 { "sysfs", "/sys", "sysfs", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
595 MOUNT_FATAL
}, /* skipped if above was mounted */
596 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID
|MS_STRICTATIME
,
598 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
600 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
604 { "/sys/fs/selinux", "/sys/fs/selinux", NULL
, NULL
, MS_BIND
,
605 0 }, /* Bind mount first */
606 { NULL
, "/sys/fs/selinux", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
607 0 }, /* Then, make it r/o */
611 bool use_userns
= (mount_settings
& MOUNT_USE_USERNS
);
612 bool netns
= (mount_settings
& MOUNT_APPLY_APIVFS_NETNS
);
613 bool ro
= (mount_settings
& MOUNT_APPLY_APIVFS_RO
);
614 bool in_userns
= (mount_settings
& MOUNT_IN_USERNS
);
615 bool tmpfs_tmp
= (mount_settings
& MOUNT_APPLY_TMPFS_TMP
);
619 for (k
= 0; k
< ELEMENTSOF(mount_table
); k
++) {
620 _cleanup_free_
char *where
= NULL
, *options
= NULL
;
622 bool fatal
= (mount_table
[k
].mount_settings
& MOUNT_FATAL
);
624 if (in_userns
!= (bool)(mount_table
[k
].mount_settings
& MOUNT_IN_USERNS
))
627 if (!netns
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_APIVFS_NETNS
))
630 if (!ro
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_APIVFS_RO
))
633 if (!tmpfs_tmp
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_TMPFS_TMP
))
636 r
= chase_symlinks(mount_table
[k
].where
, dest
, CHASE_NONEXISTENT
|CHASE_PREFIX_ROOT
, &where
, NULL
);
638 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, mount_table
[k
].where
);
640 /* Skip this entry if it is not a remount. */
641 if (mount_table
[k
].what
) {
642 r
= path_is_mount_point(where
, NULL
, 0);
643 if (r
< 0 && r
!= -ENOENT
)
644 return log_error_errno(r
, "Failed to detect whether %s is a mount point: %m", where
);
649 r
= mkdir_userns_p(dest
, where
, 0755, (use_userns
&& !in_userns
) ? uid_shift
: UID_INVALID
);
650 if (r
< 0 && r
!= -EEXIST
) {
651 if (fatal
&& r
!= -EROFS
)
652 return log_error_errno(r
, "Failed to create directory %s: %m", where
);
654 log_debug_errno(r
, "Failed to create directory %s: %m", where
);
655 /* If we failed mkdir() or chown() due to the root
656 * directory being read only, attempt to mount this fs
657 * anyway and let mount_verbose log any errors */
662 o
= mount_table
[k
].options
;
663 if (streq_ptr(mount_table
[k
].type
, "tmpfs")) {
664 r
= tmpfs_patch_options(o
, in_userns
? 0 : uid_shift
, selinux_apifs_context
, &options
);
671 r
= mount_verbose(fatal
? LOG_ERR
: LOG_DEBUG
,
675 mount_table
[k
].flags
,
684 static int parse_mount_bind_options(const char *options
, unsigned long *mount_flags
, char **mount_opts
) {
685 const char *p
= options
;
686 unsigned long flags
= *mount_flags
;
693 _cleanup_free_
char *word
= NULL
;
695 r
= extract_first_word(&p
, &word
, ",", 0);
697 return log_error_errno(r
, "Failed to extract mount option: %m");
701 if (streq(word
, "rbind"))
703 else if (streq(word
, "norbind"))
706 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
707 "Invalid bind mount option: %s",
712 *mount_flags
= flags
;
713 /* in the future mount_opts will hold string options for mount(2) */
719 static int mount_bind(const char *dest
, CustomMount
*m
) {
720 _cleanup_free_
char *mount_opts
= NULL
, *where
= NULL
;
721 unsigned long mount_flags
= MS_BIND
| MS_REC
;
722 struct stat source_st
, dest_st
;
729 r
= parse_mount_bind_options(m
->options
, &mount_flags
, &mount_opts
);
734 if (stat(m
->source
, &source_st
) < 0)
735 return log_error_errno(errno
, "Failed to stat %s: %m", m
->source
);
737 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
, NULL
);
739 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
740 if (r
> 0) { /* Path exists already? */
742 if (stat(where
, &dest_st
) < 0)
743 return log_error_errno(errno
, "Failed to stat %s: %m", where
);
745 if (S_ISDIR(source_st
.st_mode
) && !S_ISDIR(dest_st
.st_mode
))
746 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
747 "Cannot bind mount directory %s on file %s.",
750 if (!S_ISDIR(source_st
.st_mode
) && S_ISDIR(dest_st
.st_mode
))
751 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
752 "Cannot bind mount file %s on directory %s.",
755 } else { /* Path doesn't exist yet? */
756 r
= mkdir_parents_label(where
, 0755);
758 return log_error_errno(r
, "Failed to make parents of %s: %m", where
);
760 /* Create the mount point. Any non-directory file can be
761 * mounted on any non-directory file (regular, fifo, socket,
764 if (S_ISDIR(source_st
.st_mode
))
765 r
= mkdir_label(where
, 0755);
769 return log_error_errno(r
, "Failed to create mount point %s: %m", where
);
772 r
= mount_verbose(LOG_ERR
, m
->source
, where
, NULL
, mount_flags
, mount_opts
);
777 r
= bind_remount_recursive(where
, MS_RDONLY
, MS_RDONLY
, NULL
);
779 return log_error_errno(r
, "Read-only bind mount failed: %m");
785 static int mount_tmpfs(
788 bool userns
, uid_t uid_shift
, uid_t uid_range
,
789 const char *selinux_apifs_context
) {
792 _cleanup_free_
char *buf
= NULL
, *where
= NULL
;
798 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
, NULL
);
800 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
801 if (r
== 0) { /* Doesn't exist yet? */
802 r
= mkdir_p_label(where
, 0755);
804 return log_error_errno(r
, "Creating mount point for tmpfs %s failed: %m", where
);
807 r
= tmpfs_patch_options(m
->options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
810 options
= r
> 0 ? buf
: m
->options
;
812 return mount_verbose(LOG_ERR
, "tmpfs", where
, "tmpfs", MS_NODEV
|MS_STRICTATIME
, options
);
815 static char *joined_and_escaped_lower_dirs(char **lower
) {
816 _cleanup_strv_free_
char **sv
= NULL
;
818 sv
= strv_copy(lower
);
824 if (!strv_shell_escape(sv
, ",:"))
827 return strv_join(sv
, ":");
830 static int mount_overlay(const char *dest
, CustomMount
*m
) {
831 _cleanup_free_
char *lower
= NULL
, *where
= NULL
, *escaped_source
= NULL
;
838 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
, NULL
);
840 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
841 if (r
== 0) { /* Doesn't exist yet? */
842 r
= mkdir_label(where
, 0755);
844 return log_error_errno(r
, "Creating mount point for overlay %s failed: %m", where
);
847 (void) mkdir_p_label(m
->source
, 0755);
849 lower
= joined_and_escaped_lower_dirs(m
->lower
);
853 escaped_source
= shell_escape(m
->source
, ",:");
858 options
= strjoina("lowerdir=", escaped_source
, ":", lower
);
860 _cleanup_free_
char *escaped_work_dir
= NULL
;
862 escaped_work_dir
= shell_escape(m
->work_dir
, ",:");
863 if (!escaped_work_dir
)
866 options
= strjoina("lowerdir=", lower
, ",upperdir=", escaped_source
, ",workdir=", escaped_work_dir
);
869 return mount_verbose(LOG_ERR
, "overlay", where
, "overlay", m
->read_only
? MS_RDONLY
: 0, options
);
872 static int mount_inaccessible(const char *dest
, CustomMount
*m
) {
873 _cleanup_free_
char *where
= NULL
;
881 r
= chase_symlinks_and_stat(m
->destination
, dest
, CHASE_PREFIX_ROOT
, &where
, &st
, NULL
);
883 log_full_errno(m
->graceful
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
884 return m
->graceful
? 0 : r
;
887 assert_se(source
= mode_to_inaccessible_node(st
.st_mode
));
889 r
= mount_verbose(m
->graceful
? LOG_DEBUG
: LOG_ERR
, source
, where
, NULL
, MS_BIND
, NULL
);
891 return m
->graceful
? 0 : r
;
893 r
= mount_verbose(m
->graceful
? LOG_DEBUG
: LOG_ERR
, NULL
, where
, NULL
, MS_BIND
|MS_RDONLY
|MS_REMOUNT
, NULL
);
895 umount_verbose(where
);
896 return m
->graceful
? 0 : r
;
902 static int mount_arbitrary(const char *dest
, CustomMount
*m
) {
903 _cleanup_free_
char *where
= NULL
;
909 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
, NULL
);
911 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
912 if (r
== 0) { /* Doesn't exist yet? */
913 r
= mkdir_p_label(where
, 0755);
915 return log_error_errno(r
, "Creating mount point for mount %s failed: %m", where
);
918 return mount_verbose(LOG_ERR
, m
->source
, where
, m
->type_argument
, 0, m
->options
);
923 CustomMount
*mounts
, size_t n
,
924 bool userns
, uid_t uid_shift
, uid_t uid_range
,
925 const char *selinux_apifs_context
,
933 for (i
= 0; i
< n
; i
++) {
934 CustomMount
*m
= mounts
+ i
;
936 if (m
->in_userns
!= in_userns
)
941 case CUSTOM_MOUNT_BIND
:
942 r
= mount_bind(dest
, m
);
945 case CUSTOM_MOUNT_TMPFS
:
946 r
= mount_tmpfs(dest
, m
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
949 case CUSTOM_MOUNT_OVERLAY
:
950 r
= mount_overlay(dest
, m
);
953 case CUSTOM_MOUNT_INACCESSIBLE
:
954 r
= mount_inaccessible(dest
, m
);
957 case CUSTOM_MOUNT_ARBITRARY
:
958 r
= mount_arbitrary(dest
, m
);
962 assert_not_reached("Unknown custom mount type");
972 static int setup_volatile_state(
973 const char *directory
,
974 bool userns
, uid_t uid_shift
, uid_t uid_range
,
975 const char *selinux_apifs_context
) {
977 _cleanup_free_
char *buf
= NULL
;
978 const char *p
, *options
;
983 /* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */
985 r
= bind_remount_recursive(directory
, MS_RDONLY
, MS_RDONLY
, NULL
);
987 return log_error_errno(r
, "Failed to remount %s read-only: %m", directory
);
989 p
= prefix_roota(directory
, "/var");
991 if (r
< 0 && errno
!= EEXIST
)
992 return log_error_errno(errno
, "Failed to create %s: %m", directory
);
994 options
= "mode=755";
995 r
= tmpfs_patch_options(options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
1001 return mount_verbose(LOG_ERR
, "tmpfs", p
, "tmpfs", MS_STRICTATIME
, options
);
1004 static int setup_volatile_yes(
1005 const char *directory
,
1006 bool userns
, uid_t uid_shift
, uid_t uid_range
,
1007 const char *selinux_apifs_context
) {
1009 bool tmpfs_mounted
= false, bind_mounted
= false;
1010 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1011 _cleanup_free_
char *buf
= NULL
, *bindir
= NULL
;
1012 const char *f
, *t
, *options
;
1018 /* --volatile=yes means we mount a tmpfs to the root dir, and the original /usr to use inside it, and
1019 * that read-only. Before we start setting this up let's validate if the image has the /usr merge
1020 * implemented, and let's output a friendly log message if it hasn't. */
1022 bindir
= path_join(directory
, "/bin");
1025 if (lstat(bindir
, &st
) < 0) {
1026 if (errno
!= ENOENT
)
1027 return log_error_errno(errno
, "Failed to stat /bin directory below image: %m");
1029 /* ENOENT is fine, just means the image is probably just a naked /usr and we can create the
1031 } else if (S_ISDIR(st
.st_mode
))
1032 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
1033 "Sorry, --volatile=yes mode is not supported with OS images that have not merged /bin/, /sbin/, /lib/, /lib64/ into /usr/. "
1034 "Please work with your distribution and help them adopt the merged /usr scheme.");
1035 else if (!S_ISLNK(st
.st_mode
))
1036 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1037 "Error starting image: if --volatile=yes is used /bin must be a symlink (for merged /usr support) or non-existent (in which case a symlink is created automatically).");
1039 if (!mkdtemp(template))
1040 return log_error_errno(errno
, "Failed to create temporary directory: %m");
1042 options
= "mode=755";
1043 r
= tmpfs_patch_options(options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
1049 r
= mount_verbose(LOG_ERR
, "tmpfs", template, "tmpfs", MS_STRICTATIME
, options
);
1053 tmpfs_mounted
= true;
1055 f
= prefix_roota(directory
, "/usr");
1056 t
= prefix_roota(template, "/usr");
1059 if (r
< 0 && errno
!= EEXIST
) {
1060 r
= log_error_errno(errno
, "Failed to create %s: %m", t
);
1064 r
= mount_verbose(LOG_ERR
, f
, t
, NULL
, MS_BIND
|MS_REC
, NULL
);
1068 bind_mounted
= true;
1070 r
= bind_remount_recursive(t
, MS_RDONLY
, MS_RDONLY
, NULL
);
1072 log_error_errno(r
, "Failed to remount %s read-only: %m", t
);
1076 r
= mount_verbose(LOG_ERR
, template, directory
, NULL
, MS_MOVE
, NULL
);
1080 (void) rmdir(template);
1086 (void) umount_verbose(t
);
1089 (void) umount_verbose(template);
1090 (void) rmdir(template);
1094 static int setup_volatile_overlay(
1095 const char *directory
,
1096 bool userns
, uid_t uid_shift
, uid_t uid_range
,
1097 const char *selinux_apifs_context
) {
1099 _cleanup_free_
char *buf
= NULL
, *escaped_directory
= NULL
, *escaped_upper
= NULL
, *escaped_work
= NULL
;
1100 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1101 const char *upper
, *work
, *options
;
1102 bool tmpfs_mounted
= false;
1107 /* --volatile=overlay means we mount an overlayfs to the root dir. */
1109 if (!mkdtemp(template))
1110 return log_error_errno(errno
, "Failed to create temporary directory: %m");
1112 options
= "mode=755";
1113 r
= tmpfs_patch_options(options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
1119 r
= mount_verbose(LOG_ERR
, "tmpfs", template, "tmpfs", MS_STRICTATIME
, options
);
1123 tmpfs_mounted
= true;
1125 upper
= strjoina(template, "/upper");
1126 work
= strjoina(template, "/work");
1128 if (mkdir(upper
, 0755) < 0) {
1129 r
= log_error_errno(errno
, "Failed to create %s: %m", upper
);
1132 if (mkdir(work
, 0755) < 0) {
1133 r
= log_error_errno(errno
, "Failed to create %s: %m", work
);
1137 /* And now, let's overmount the root dir with an overlayfs that uses the root dir as lower dir. It's kinda nice
1138 * that the kernel allows us to do that without going through some mount point rearrangements. */
1140 escaped_directory
= shell_escape(directory
, ",:");
1141 escaped_upper
= shell_escape(upper
, ",:");
1142 escaped_work
= shell_escape(work
, ",:");
1143 if (!escaped_directory
|| !escaped_upper
|| !escaped_work
) {
1148 options
= strjoina("lowerdir=", escaped_directory
, ",upperdir=", escaped_upper
, ",workdir=", escaped_work
);
1149 r
= mount_verbose(LOG_ERR
, "overlay", directory
, "overlay", 0, options
);
1153 (void) umount_verbose(template);
1155 (void) rmdir(template);
1159 int setup_volatile_mode(
1160 const char *directory
,
1162 bool userns
, uid_t uid_shift
, uid_t uid_range
,
1163 const char *selinux_apifs_context
) {
1168 return setup_volatile_yes(directory
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
1170 case VOLATILE_STATE
:
1171 return setup_volatile_state(directory
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
1173 case VOLATILE_OVERLAY
:
1174 return setup_volatile_overlay(directory
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
1181 /* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1182 int pivot_root_parse(char **pivot_root_new
, char **pivot_root_old
, const char *s
) {
1183 _cleanup_free_
char *root_new
= NULL
, *root_old
= NULL
;
1187 assert(pivot_root_new
);
1188 assert(pivot_root_old
);
1190 r
= extract_first_word(&p
, &root_new
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
1199 root_old
= strdup(p
);
1204 if (!path_is_absolute(root_new
))
1206 if (root_old
&& !path_is_absolute(root_old
))
1209 free_and_replace(*pivot_root_new
, root_new
);
1210 free_and_replace(*pivot_root_old
, root_old
);
1215 int setup_pivot_root(const char *directory
, const char *pivot_root_new
, const char *pivot_root_old
) {
1216 _cleanup_free_
char *directory_pivot_root_new
= NULL
;
1217 _cleanup_free_
char *pivot_tmp_pivot_root_old
= NULL
;
1218 char pivot_tmp
[] = "/tmp/nspawn-pivot-XXXXXX";
1219 bool remove_pivot_tmp
= false;
1224 if (!pivot_root_new
)
1227 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1228 * If pivot_root_old is NULL, the existing / disappears.
1229 * This requires a temporary directory, pivot_tmp, which is
1230 * not a child of either.
1232 * This is typically used for OSTree-style containers, where
1233 * the root partition contains several sysroots which could be
1234 * run. Normally, one would be chosen by the bootloader and
1235 * pivoted to / by initramfs.
1237 * For example, for an OSTree deployment, pivot_root_new
1238 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1239 * code doesn’t do the /var mount which OSTree expects: use
1240 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1242 * So in the OSTree case, we’ll end up with something like:
1243 * - directory = /tmp/nspawn-root-123456
1244 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1245 * - pivot_root_old = /sysroot
1246 * - directory_pivot_root_new =
1247 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1248 * - pivot_tmp = /tmp/nspawn-pivot-123456
1249 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1251 * Requires all file systems at directory and below to be mounted
1252 * MS_PRIVATE or MS_SLAVE so they can be moved.
1254 directory_pivot_root_new
= path_join(directory
, pivot_root_new
);
1255 if (!directory_pivot_root_new
)
1258 /* Remount directory_pivot_root_new to make it movable. */
1259 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, directory_pivot_root_new
, NULL
, MS_BIND
, NULL
);
1263 if (pivot_root_old
) {
1264 if (!mkdtemp(pivot_tmp
)) {
1265 r
= log_error_errno(errno
, "Failed to create temporary directory: %m");
1269 remove_pivot_tmp
= true;
1270 pivot_tmp_pivot_root_old
= path_join(pivot_tmp
, pivot_root_old
);
1271 if (!pivot_tmp_pivot_root_old
) {
1276 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, pivot_tmp
, NULL
, MS_MOVE
, NULL
);
1280 r
= mount_verbose(LOG_ERR
, directory
, pivot_tmp_pivot_root_old
, NULL
, MS_MOVE
, NULL
);
1284 r
= mount_verbose(LOG_ERR
, pivot_tmp
, directory
, NULL
, MS_MOVE
, NULL
);
1288 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, directory
, NULL
, MS_MOVE
, NULL
);
1294 if (remove_pivot_tmp
)
1295 (void) rmdir(pivot_tmp
);