1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/magic.h>
6 #include "alloc-util.h"
13 #include "mount-util.h"
14 #include "nspawn-mount.h"
15 #include "parse-util.h"
16 #include "path-util.h"
19 #include "stat-util.h"
20 #include "string-util.h"
22 #include "user-util.h"
25 CustomMount
* custom_mount_add(CustomMount
**l
, size_t *n
, CustomMountType t
) {
31 assert(t
< _CUSTOM_MOUNT_TYPE_MAX
);
33 c
= reallocarray(*l
, *n
+ 1, sizeof(CustomMount
));
41 *ret
= (CustomMount
) { .type
= t
};
46 void custom_mount_free_all(CustomMount
*l
, size_t n
) {
49 for (i
= 0; i
< n
; i
++) {
50 CustomMount
*m
= l
+ i
;
57 (void) rm_rf(m
->work_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
61 if (m
->rm_rf_tmpdir
) {
62 (void) rm_rf(m
->rm_rf_tmpdir
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
63 free(m
->rm_rf_tmpdir
);
72 static int custom_mount_compare(const CustomMount
*a
, const CustomMount
*b
) {
75 r
= path_compare(a
->destination
, b
->destination
);
79 return CMP(a
->type
, b
->type
);
82 static bool source_path_is_valid(const char *p
) {
88 return path_is_absolute(p
);
91 static char *resolve_source_path(const char *dest
, const char *source
) {
97 return prefix_root(dest
, source
+ 1);
99 return strdup(source
);
102 int custom_mount_prepare_all(const char *dest
, CustomMount
*l
, size_t n
) {
106 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
107 * parent process, so that we know the temporary directories to remove on exit before we fork off the
112 /* Order the custom mounts, and make sure we have a working directory */
113 typesafe_qsort(l
, n
, custom_mount_compare
);
115 for (i
= 0; i
< n
; i
++) {
116 CustomMount
*m
= l
+ i
;
121 s
= resolve_source_path(dest
, m
->source
);
125 free_and_replace(m
->source
, s
);
127 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
129 m
->rm_rf_tmpdir
= strdup("/var/tmp/nspawn-temp-XXXXXX");
130 if (!m
->rm_rf_tmpdir
)
133 if (!mkdtemp(m
->rm_rf_tmpdir
)) {
134 m
->rm_rf_tmpdir
= mfree(m
->rm_rf_tmpdir
);
135 return log_error_errno(errno
, "Failed to acquire temporary directory: %m");
138 m
->source
= strjoin(m
->rm_rf_tmpdir
, "/src");
142 if (mkdir(m
->source
, 0755) < 0)
143 return log_error_errno(errno
, "Failed to create %s: %m", m
->source
);
146 if (m
->type
== CUSTOM_MOUNT_OVERLAY
) {
149 STRV_FOREACH(j
, m
->lower
) {
152 s
= resolve_source_path(dest
, *j
);
156 free_and_replace(*j
, s
);
162 s
= resolve_source_path(dest
, m
->work_dir
);
166 free_and_replace(m
->work_dir
, s
);
170 r
= tempfn_random(m
->source
, NULL
, &m
->work_dir
);
172 return log_error_errno(r
, "Failed to acquire working directory: %m");
175 (void) mkdir_label(m
->work_dir
, 0700);
182 int bind_mount_parse(CustomMount
**l
, size_t *n
, const char *s
, bool read_only
) {
183 _cleanup_free_
char *source
= NULL
, *destination
= NULL
, *opts
= NULL
;
191 r
= extract_many_words(&p
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
, &source
, &destination
, NULL
);
197 destination
= strdup(source
[0] == '+' ? source
+1 : source
);
201 if (r
== 2 && !isempty(p
)) {
209 else if (!source_path_is_valid(source
))
212 if (!path_is_absolute(destination
))
215 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_BIND
);
220 m
->destination
= destination
;
221 m
->read_only
= read_only
;
224 source
= destination
= opts
= NULL
;
228 int tmpfs_mount_parse(CustomMount
**l
, size_t *n
, const char *s
) {
229 _cleanup_free_
char *path
= NULL
, *opts
= NULL
;
238 r
= extract_first_word(&p
, &path
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
245 opts
= strdup("mode=0755");
251 if (!path_is_absolute(path
))
254 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_TMPFS
);
258 m
->destination
= TAKE_PTR(path
);
259 m
->options
= TAKE_PTR(opts
);
264 int overlay_mount_parse(CustomMount
**l
, size_t *n
, const char *s
, bool read_only
) {
265 _cleanup_free_
char *upper
= NULL
, *destination
= NULL
;
266 _cleanup_strv_free_
char **lower
= NULL
;
270 k
= strv_split_extract(&lower
, s
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
274 return -EADDRNOTAVAIL
;
276 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
277 * we'll also define the destination mount point the same as the upper. */
279 if (!source_path_is_valid(lower
[0]) ||
280 !source_path_is_valid(lower
[1]))
283 upper
= TAKE_PTR(lower
[1]);
285 destination
= strdup(upper
[0] == '+' ? upper
+1 : upper
); /* take the destination without "+" prefix */
291 /* If more than two parameters are specified, the last one is the destination, the second to last one
292 * the "upper", and all before that the "lower" directories. */
294 destination
= lower
[k
- 1];
295 upper
= TAKE_PTR(lower
[k
- 2]);
297 STRV_FOREACH(i
, lower
)
298 if (!source_path_is_valid(*i
))
301 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
305 else if (!source_path_is_valid(upper
))
308 if (!path_is_absolute(destination
))
312 m
= custom_mount_add(l
, n
, CUSTOM_MOUNT_OVERLAY
);
316 m
->destination
= TAKE_PTR(destination
);
317 m
->source
= TAKE_PTR(upper
);
318 m
->lower
= TAKE_PTR(lower
);
319 m
->read_only
= read_only
;
324 int tmpfs_patch_options(
327 const char *selinux_apifs_context
,
332 if (uid_shift
!= UID_INVALID
) {
333 if (asprintf(&buf
, "%s%suid=" UID_FMT
",gid=" UID_FMT
,
334 strempty(options
), options
? "," : "",
335 uid_shift
, uid_shift
) < 0)
342 if (selinux_apifs_context
) {
345 t
= strjoin(strempty(options
), options
? "," : "",
346 "context=\"", selinux_apifs_context
, "\"");
355 if (!buf
&& options
) {
356 buf
= strdup(options
);
365 int mount_sysfs(const char *dest
, MountSettingsMask mount_settings
) {
366 const char *full
, *top
, *x
;
368 unsigned long extra_flags
= 0;
370 top
= prefix_roota(dest
, "/sys");
371 r
= path_is_fs_type(top
, SYSFS_MAGIC
);
373 return log_error_errno(r
, "Failed to determine filesystem type of %s: %m", top
);
374 /* /sys might already be mounted as sysfs by the outer child in the
375 * !netns case. In this case, it's all good. Don't touch it because we
376 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
381 full
= prefix_roota(top
, "/full");
383 (void) mkdir(full
, 0755);
385 if (mount_settings
& MOUNT_APPLY_APIVFS_RO
)
386 extra_flags
|= MS_RDONLY
;
388 r
= mount_verbose(LOG_ERR
, "sysfs", full
, "sysfs",
389 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|extra_flags
, NULL
);
393 FOREACH_STRING(x
, "block", "bus", "class", "dev", "devices", "kernel") {
394 _cleanup_free_
char *from
= NULL
, *to
= NULL
;
396 from
= prefix_root(full
, x
);
400 to
= prefix_root(top
, x
);
404 (void) mkdir(to
, 0755);
406 r
= mount_verbose(LOG_ERR
, from
, to
, NULL
, MS_BIND
, NULL
);
410 r
= mount_verbose(LOG_ERR
, NULL
, to
, NULL
,
411 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
416 r
= umount_verbose(full
);
421 return log_error_errno(errno
, "Failed to remove %s: %m", full
);
423 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
424 * remount /sys read-only.
426 x
= prefix_roota(top
, "/fs/cgroup");
427 (void) mkdir_p(x
, 0755);
429 return mount_verbose(LOG_ERR
, NULL
, top
, NULL
,
430 MS_BIND
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
|extra_flags
, NULL
);
433 static int mkdir_userns(const char *path
, mode_t mode
, uid_t uid_shift
) {
438 r
= mkdir_errno_wrapper(path
, mode
);
439 if (r
< 0 && r
!= -EEXIST
)
442 if (uid_shift
== UID_INVALID
)
445 if (lchown(path
, uid_shift
, uid_shift
) < 0)
451 static int mkdir_userns_p(const char *prefix
, const char *path
, mode_t mode
, uid_t uid_shift
) {
457 if (prefix
&& !path_startswith(path
, prefix
))
460 /* create every parent directory in the path, except the last component */
461 p
= path
+ strspn(path
, "/");
463 char t
[strlen(path
) + 1];
465 e
= p
+ strcspn(p
, "/");
466 p
= e
+ strspn(e
, "/");
468 /* Is this the last component? If so, then we're done */
472 memcpy(t
, path
, e
- path
);
475 if (prefix
&& path_startswith(prefix
, t
))
478 r
= mkdir_userns(t
, mode
, uid_shift
);
483 return mkdir_userns(path
, mode
, uid_shift
);
486 int mount_all(const char *dest
,
487 MountSettingsMask mount_settings
,
489 const char *selinux_apifs_context
) {
491 #define PROC_INACCESSIBLE(path) \
492 { NULL, (path), NULL, NULL, MS_BIND, \
493 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_INACCESSIBLE_REG }, /* Bind mount first ... */ \
494 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
495 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
497 #define PROC_READ_ONLY(path) \
498 { (path), (path), NULL, NULL, MS_BIND, \
499 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \
500 { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \
501 MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */
503 typedef struct MountPoint
{
509 MountSettingsMask mount_settings
;
512 static const MountPoint mount_table
[] = {
513 /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing) */
514 { "proc", "/proc", "proc", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
515 MOUNT_FATAL
|MOUNT_IN_USERNS
},
517 { "/proc/sys", "/proc/sys", NULL
, NULL
, MS_BIND
,
518 MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* Bind mount first ... */
520 { "/proc/sys/net", "/proc/sys/net", NULL
, NULL
, MS_BIND
,
521 MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
|MOUNT_APPLY_APIVFS_NETNS
}, /* (except for this) */
523 { NULL
, "/proc/sys", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
524 MOUNT_FATAL
|MOUNT_IN_USERNS
|MOUNT_APPLY_APIVFS_RO
}, /* ... then, make it r/o */
526 /* Make these files inaccessible to container payloads: they potentially leak information about kernel
527 * internals or the host's execution environment to the container */
528 PROC_INACCESSIBLE("/proc/kallsyms"),
529 PROC_INACCESSIBLE("/proc/kcore"),
530 PROC_INACCESSIBLE("/proc/keys"),
531 PROC_INACCESSIBLE("/proc/sysrq-trigger"),
532 PROC_INACCESSIBLE("/proc/timer_list"),
534 /* Make these directories read-only to container payloads: they show hardware information, and in some
535 * cases contain tunables the container really shouldn't have access to. */
536 PROC_READ_ONLY("/proc/acpi"),
537 PROC_READ_ONLY("/proc/apm"),
538 PROC_READ_ONLY("/proc/asound"),
539 PROC_READ_ONLY("/proc/bus"),
540 PROC_READ_ONLY("/proc/fs"),
541 PROC_READ_ONLY("/proc/irq"),
542 PROC_READ_ONLY("/proc/scsi"),
544 /* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing) */
545 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
546 MOUNT_FATAL
|MOUNT_APPLY_TMPFS_TMP
},
547 { "tmpfs", "/sys", "tmpfs", "mode=555", MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
548 MOUNT_FATAL
|MOUNT_APPLY_APIVFS_NETNS
},
549 { "sysfs", "/sys", "sysfs", NULL
, MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
550 MOUNT_FATAL
|MOUNT_APPLY_APIVFS_RO
}, /* skipped if above was mounted */
551 { "sysfs", "/sys", "sysfs", NULL
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
,
552 MOUNT_FATAL
}, /* skipped if above was mounted */
553 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID
|MS_STRICTATIME
,
555 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
557 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID
|MS_NODEV
|MS_STRICTATIME
,
561 { "/sys/fs/selinux", "/sys/fs/selinux", NULL
, NULL
, MS_BIND
,
562 0 }, /* Bind mount first */
563 { NULL
, "/sys/fs/selinux", NULL
, NULL
, MS_BIND
|MS_RDONLY
|MS_NOSUID
|MS_NOEXEC
|MS_NODEV
|MS_REMOUNT
,
564 0 }, /* Then, make it r/o */
568 _cleanup_(unlink_and_freep
) char *inaccessible
= NULL
;
569 bool use_userns
= (mount_settings
& MOUNT_USE_USERNS
);
570 bool netns
= (mount_settings
& MOUNT_APPLY_APIVFS_NETNS
);
571 bool ro
= (mount_settings
& MOUNT_APPLY_APIVFS_RO
);
572 bool in_userns
= (mount_settings
& MOUNT_IN_USERNS
);
573 bool tmpfs_tmp
= (mount_settings
& MOUNT_APPLY_TMPFS_TMP
);
577 for (k
= 0; k
< ELEMENTSOF(mount_table
); k
++) {
578 _cleanup_free_
char *where
= NULL
, *options
= NULL
;
579 const char *o
, *what
;
580 bool fatal
= (mount_table
[k
].mount_settings
& MOUNT_FATAL
);
582 if (in_userns
!= (bool)(mount_table
[k
].mount_settings
& MOUNT_IN_USERNS
))
585 if (!netns
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_APIVFS_NETNS
))
588 if (!ro
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_APIVFS_RO
))
591 if (!tmpfs_tmp
&& (bool)(mount_table
[k
].mount_settings
& MOUNT_APPLY_TMPFS_TMP
))
594 r
= chase_symlinks(mount_table
[k
].where
, dest
, CHASE_NONEXISTENT
|CHASE_PREFIX_ROOT
, &where
);
596 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, mount_table
[k
].where
);
598 if (mount_table
[k
].mount_settings
& MOUNT_INACCESSIBLE_REG
) {
601 _cleanup_free_
char *np
= NULL
;
603 r
= tempfn_random_child(NULL
, "inaccessible", &np
);
605 return log_error_errno(r
, "Failed to generate inaccessible file node path: %m");
607 r
= touch_file(np
, false, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, 0000);
609 return log_error_errno(r
, "Failed to create inaccessible file node '%s': %m", np
);
611 inaccessible
= TAKE_PTR(np
);
616 what
= mount_table
[k
].what
;
618 r
= path_is_mount_point(where
, NULL
, 0);
619 if (r
< 0 && r
!= -ENOENT
)
620 return log_error_errno(r
, "Failed to detect whether %s is a mount point: %m", where
);
622 /* Skip this entry if it is not a remount. */
626 r
= mkdir_userns_p(dest
, where
, 0755, (use_userns
&& !in_userns
) ? uid_shift
: UID_INVALID
);
627 if (r
< 0 && r
!= -EEXIST
) {
628 if (fatal
&& r
!= -EROFS
)
629 return log_error_errno(r
, "Failed to create directory %s: %m", where
);
631 log_debug_errno(r
, "Failed to create directory %s: %m", where
);
632 /* If we failed mkdir() or chown() due to the root
633 * directory being read only, attempt to mount this fs
634 * anyway and let mount_verbose log any errors */
639 o
= mount_table
[k
].options
;
640 if (streq_ptr(mount_table
[k
].type
, "tmpfs")) {
641 r
= tmpfs_patch_options(o
, in_userns
? 0 : uid_shift
, selinux_apifs_context
, &options
);
648 r
= mount_verbose(fatal
? LOG_ERR
: LOG_DEBUG
,
652 mount_table
[k
].flags
,
661 static int mount_bind(const char *dest
, CustomMount
*m
) {
663 _cleanup_free_
char *where
= NULL
;
664 struct stat source_st
, dest_st
;
670 if (stat(m
->source
, &source_st
) < 0)
671 return log_error_errno(errno
, "Failed to stat %s: %m", m
->source
);
673 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
);
675 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
676 if (r
> 0) { /* Path exists already? */
678 if (stat(where
, &dest_st
) < 0)
679 return log_error_errno(errno
, "Failed to stat %s: %m", where
);
681 if (S_ISDIR(source_st
.st_mode
) && !S_ISDIR(dest_st
.st_mode
)) {
682 log_error("Cannot bind mount directory %s on file %s.", m
->source
, where
);
686 if (!S_ISDIR(source_st
.st_mode
) && S_ISDIR(dest_st
.st_mode
)) {
687 log_error("Cannot bind mount file %s on directory %s.", m
->source
, where
);
691 } else { /* Path doesn't exist yet? */
692 r
= mkdir_parents_label(where
, 0755);
694 return log_error_errno(r
, "Failed to make parents of %s: %m", where
);
696 /* Create the mount point. Any non-directory file can be
697 * mounted on any non-directory file (regular, fifo, socket,
700 if (S_ISDIR(source_st
.st_mode
))
701 r
= mkdir_label(where
, 0755);
705 return log_error_errno(r
, "Failed to create mount point %s: %m", where
);
709 r
= mount_verbose(LOG_ERR
, m
->source
, where
, NULL
, MS_BIND
| MS_REC
, m
->options
);
714 r
= bind_remount_recursive(where
, true, NULL
);
716 return log_error_errno(r
, "Read-only bind mount failed: %m");
722 static int mount_tmpfs(
725 bool userns
, uid_t uid_shift
, uid_t uid_range
,
726 const char *selinux_apifs_context
) {
729 _cleanup_free_
char *buf
= NULL
, *where
= NULL
;
735 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
);
737 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
738 if (r
== 0) { /* Doesn't exist yet? */
739 r
= mkdir_p_label(where
, 0755);
741 return log_error_errno(r
, "Creating mount point for tmpfs %s failed: %m", where
);
744 r
= tmpfs_patch_options(m
->options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
747 options
= r
> 0 ? buf
: m
->options
;
749 return mount_verbose(LOG_ERR
, "tmpfs", where
, "tmpfs", MS_NODEV
|MS_STRICTATIME
, options
);
752 static char *joined_and_escaped_lower_dirs(char **lower
) {
753 _cleanup_strv_free_
char **sv
= NULL
;
755 sv
= strv_copy(lower
);
761 if (!strv_shell_escape(sv
, ",:"))
764 return strv_join(sv
, ":");
767 static int mount_overlay(const char *dest
, CustomMount
*m
) {
769 _cleanup_free_
char *lower
= NULL
, *where
= NULL
, *escaped_source
= NULL
;
776 r
= chase_symlinks(m
->destination
, dest
, CHASE_PREFIX_ROOT
|CHASE_NONEXISTENT
, &where
);
778 return log_error_errno(r
, "Failed to resolve %s/%s: %m", dest
, m
->destination
);
779 if (r
== 0) { /* Doesn't exist yet? */
780 r
= mkdir_label(where
, 0755);
782 return log_error_errno(r
, "Creating mount point for overlay %s failed: %m", where
);
785 (void) mkdir_p_label(m
->source
, 0755);
787 lower
= joined_and_escaped_lower_dirs(m
->lower
);
791 escaped_source
= shell_escape(m
->source
, ",:");
796 options
= strjoina("lowerdir=", escaped_source
, ":", lower
);
798 _cleanup_free_
char *escaped_work_dir
= NULL
;
800 escaped_work_dir
= shell_escape(m
->work_dir
, ",:");
801 if (!escaped_work_dir
)
804 options
= strjoina("lowerdir=", lower
, ",upperdir=", escaped_source
, ",workdir=", escaped_work_dir
);
807 return mount_verbose(LOG_ERR
, "overlay", where
, "overlay", m
->read_only
? MS_RDONLY
: 0, options
);
812 CustomMount
*mounts
, size_t n
,
813 bool userns
, uid_t uid_shift
, uid_t uid_range
,
814 const char *selinux_apifs_context
) {
821 for (i
= 0; i
< n
; i
++) {
822 CustomMount
*m
= mounts
+ i
;
826 case CUSTOM_MOUNT_BIND
:
827 r
= mount_bind(dest
, m
);
830 case CUSTOM_MOUNT_TMPFS
:
831 r
= mount_tmpfs(dest
, m
, userns
, uid_shift
, uid_range
, selinux_apifs_context
);
834 case CUSTOM_MOUNT_OVERLAY
:
835 r
= mount_overlay(dest
, m
);
839 assert_not_reached("Unknown custom mount type");
849 int setup_volatile_state(
850 const char *directory
,
852 bool userns
, uid_t uid_shift
, uid_t uid_range
,
853 const char *selinux_apifs_context
) {
855 _cleanup_free_
char *buf
= NULL
;
856 const char *p
, *options
;
861 if (mode
!= VOLATILE_STATE
)
864 /* --volatile=state means we simply overmount /var
865 with a tmpfs, and the rest read-only. */
867 r
= bind_remount_recursive(directory
, true, NULL
);
869 return log_error_errno(r
, "Failed to remount %s read-only: %m", directory
);
871 p
= prefix_roota(directory
, "/var");
873 if (r
< 0 && errno
!= EEXIST
)
874 return log_error_errno(errno
, "Failed to create %s: %m", directory
);
876 options
= "mode=755";
877 r
= tmpfs_patch_options(options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
883 return mount_verbose(LOG_ERR
, "tmpfs", p
, "tmpfs", MS_STRICTATIME
, options
);
887 const char *directory
,
889 bool userns
, uid_t uid_shift
, uid_t uid_range
,
890 const char *selinux_apifs_context
) {
892 bool tmpfs_mounted
= false, bind_mounted
= false;
893 char template[] = "/tmp/nspawn-volatile-XXXXXX";
894 _cleanup_free_
char *buf
= NULL
;
895 const char *f
, *t
, *options
;
900 if (mode
!= VOLATILE_YES
)
903 /* --volatile=yes means we mount a tmpfs to the root dir, and
904 the original /usr to use inside it, and that read-only. */
906 if (!mkdtemp(template))
907 return log_error_errno(errno
, "Failed to create temporary directory: %m");
909 options
= "mode=755";
910 r
= tmpfs_patch_options(options
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, selinux_apifs_context
, &buf
);
916 r
= mount_verbose(LOG_ERR
, "tmpfs", template, "tmpfs", MS_STRICTATIME
, options
);
920 tmpfs_mounted
= true;
922 f
= prefix_roota(directory
, "/usr");
923 t
= prefix_roota(template, "/usr");
926 if (r
< 0 && errno
!= EEXIST
) {
927 r
= log_error_errno(errno
, "Failed to create %s: %m", t
);
931 r
= mount_verbose(LOG_ERR
, f
, t
, NULL
, MS_BIND
|MS_REC
, NULL
);
937 r
= bind_remount_recursive(t
, true, NULL
);
939 log_error_errno(r
, "Failed to remount %s read-only: %m", t
);
943 r
= mount_verbose(LOG_ERR
, template, directory
, NULL
, MS_MOVE
, NULL
);
947 (void) rmdir(template);
953 (void) umount_verbose(t
);
956 (void) umount_verbose(template);
957 (void) rmdir(template);
961 /* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
962 int pivot_root_parse(char **pivot_root_new
, char **pivot_root_old
, const char *s
) {
963 _cleanup_free_
char *root_new
= NULL
, *root_old
= NULL
;
967 assert(pivot_root_new
);
968 assert(pivot_root_old
);
970 r
= extract_first_word(&p
, &root_new
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
979 root_old
= strdup(p
);
984 if (!path_is_absolute(root_new
))
986 if (root_old
&& !path_is_absolute(root_old
))
989 free_and_replace(*pivot_root_new
, root_new
);
990 free_and_replace(*pivot_root_old
, root_old
);
995 int setup_pivot_root(const char *directory
, const char *pivot_root_new
, const char *pivot_root_old
) {
996 _cleanup_free_
char *directory_pivot_root_new
= NULL
;
997 _cleanup_free_
char *pivot_tmp_pivot_root_old
= NULL
;
998 char pivot_tmp
[] = "/tmp/nspawn-pivot-XXXXXX";
999 bool remove_pivot_tmp
= false;
1004 if (!pivot_root_new
)
1007 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1008 * If pivot_root_old is NULL, the existing / disappears.
1009 * This requires a temporary directory, pivot_tmp, which is
1010 * not a child of either.
1012 * This is typically used for OSTree-style containers, where
1013 * the root partition contains several sysroots which could be
1014 * run. Normally, one would be chosen by the bootloader and
1015 * pivoted to / by initramfs.
1017 * For example, for an OSTree deployment, pivot_root_new
1018 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1019 * code doesn’t do the /var mount which OSTree expects: use
1020 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1022 * So in the OSTree case, we’ll end up with something like:
1023 * - directory = /tmp/nspawn-root-123456
1024 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1025 * - pivot_root_old = /sysroot
1026 * - directory_pivot_root_new =
1027 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1028 * - pivot_tmp = /tmp/nspawn-pivot-123456
1029 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1031 * Requires all file systems at directory and below to be mounted
1032 * MS_PRIVATE or MS_SLAVE so they can be moved.
1034 directory_pivot_root_new
= prefix_root(directory
, pivot_root_new
);
1036 /* Remount directory_pivot_root_new to make it movable. */
1037 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, directory_pivot_root_new
, NULL
, MS_BIND
, NULL
);
1041 if (pivot_root_old
) {
1042 if (!mkdtemp(pivot_tmp
)) {
1043 r
= log_error_errno(errno
, "Failed to create temporary directory: %m");
1047 remove_pivot_tmp
= true;
1048 pivot_tmp_pivot_root_old
= prefix_root(pivot_tmp
, pivot_root_old
);
1050 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, pivot_tmp
, NULL
, MS_MOVE
, NULL
);
1054 r
= mount_verbose(LOG_ERR
, directory
, pivot_tmp_pivot_root_old
, NULL
, MS_MOVE
, NULL
);
1058 r
= mount_verbose(LOG_ERR
, pivot_tmp
, directory
, NULL
, MS_MOVE
, NULL
);
1062 r
= mount_verbose(LOG_ERR
, directory_pivot_root_new
, directory
, NULL
, MS_MOVE
, NULL
);
1068 if (remove_pivot_tmp
)
1069 (void) rmdir(pivot_tmp
);