]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/nspawn/nspawn-mount.c
nspawn: don't hard fail when setting capabilities
[thirdparty/systemd.git] / src / nspawn / nspawn-mount.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
e83bebef 2
4f5dd394 3#include <sys/mount.h>
07630cea 4#include <linux/magic.h>
e83bebef 5
b5efdb8a 6#include "alloc-util.h"
4f5dd394 7#include "escape.h"
0996ef00 8#include "fd-util.h"
ca78ad1d 9#include "format-util.h"
f4f15635 10#include "fs-util.h"
e83bebef 11#include "label.h"
4f5dd394 12#include "mkdir.h"
4349cd7c 13#include "mount-util.h"
049af8ad 14#include "mountpoint-util.h"
6bedfcbb
LP
15#include "nspawn-mount.h"
16#include "parse-util.h"
4f5dd394
LP
17#include "path-util.h"
18#include "rm-rf.h"
e83bebef 19#include "set.h"
760877e9 20#include "sort-util.h"
8fcde012 21#include "stat-util.h"
07630cea 22#include "string-util.h"
4f5dd394 23#include "strv.h"
e4de7287 24#include "tmpfile-util.h"
ee104e11 25#include "user-util.h"
e83bebef 26
88614c8a 27CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) {
e83bebef
LP
28 CustomMount *c, *ret;
29
30 assert(l);
31 assert(n);
32 assert(t >= 0);
33 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
34
aa484f35 35 c = reallocarray(*l, *n + 1, sizeof(CustomMount));
e83bebef
LP
36 if (!c)
37 return NULL;
38
39 *l = c;
40 ret = *l + *n;
41 (*n)++;
42
43 *ret = (CustomMount) { .type = t };
44
45 return ret;
46}
47
88614c8a
LP
48void custom_mount_free_all(CustomMount *l, size_t n) {
49 size_t i;
e83bebef
LP
50
51 for (i = 0; i < n; i++) {
52 CustomMount *m = l + i;
53
54 free(m->source);
55 free(m->destination);
56 free(m->options);
57
58 if (m->work_dir) {
59 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
60 free(m->work_dir);
61 }
62
c7a4890c
LP
63 if (m->rm_rf_tmpdir) {
64 (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
65 free(m->rm_rf_tmpdir);
66 }
67
e83bebef 68 strv_free(m->lower);
de40a303 69 free(m->type_argument);
e83bebef
LP
70 }
71
72 free(l);
73}
74
93bab288 75static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
e83bebef
LP
76 int r;
77
93bab288 78 r = path_compare(a->destination, b->destination);
e83bebef
LP
79 if (r != 0)
80 return r;
81
93bab288 82 return CMP(a->type, b->type);
e83bebef
LP
83}
84
86c0dd4a
LP
85static bool source_path_is_valid(const char *p) {
86 assert(p);
87
88 if (*p == '+')
89 p++;
90
91 return path_is_absolute(p);
92}
93
94static char *resolve_source_path(const char *dest, const char *source) {
95
96 if (!source)
97 return NULL;
98
99 if (source[0] == '+')
100 return prefix_root(dest, source + 1);
101
102 return strdup(source);
103}
104
88614c8a
LP
105int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) {
106 size_t i;
86c0dd4a
LP
107 int r;
108
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
111 * children. */
112
113 assert(l || n == 0);
114
115 /* Order the custom mounts, and make sure we have a working directory */
93bab288 116 typesafe_qsort(l, n, custom_mount_compare);
86c0dd4a
LP
117
118 for (i = 0; i < n; i++) {
119 CustomMount *m = l + i;
120
de40a303
LP
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");
86c0dd4a 126
de40a303
LP
127 if (m->type == CUSTOM_MOUNT_BIND) {
128 if (m->source) {
129 char *s;
86c0dd4a 130
de40a303
LP
131 s = resolve_source_path(dest, m->source);
132 if (!s)
133 return log_oom();
c7a4890c 134
de40a303
LP
135 free_and_replace(m->source, s);
136 } else {
137 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
c7a4890c 138
de40a303
LP
139 m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX");
140 if (!m->rm_rf_tmpdir)
141 return log_oom();
c7a4890c 142
de40a303
LP
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");
146 }
147
148 m->source = strjoin(m->rm_rf_tmpdir, "/src");
149 if (!m->source)
150 return log_oom();
c7a4890c 151
de40a303
LP
152 if (mkdir(m->source, 0755) < 0)
153 return log_error_errno(errno, "Failed to create %s: %m", m->source);
154 }
86c0dd4a
LP
155 }
156
157 if (m->type == CUSTOM_MOUNT_OVERLAY) {
158 char **j;
159
160 STRV_FOREACH(j, m->lower) {
161 char *s;
162
163 s = resolve_source_path(dest, *j);
164 if (!s)
165 return log_oom();
166
10af01a5 167 free_and_replace(*j, s);
86c0dd4a
LP
168 }
169
170 if (m->work_dir) {
171 char *s;
172
173 s = resolve_source_path(dest, m->work_dir);
174 if (!s)
175 return log_oom();
176
10af01a5 177 free_and_replace(m->work_dir, s);
86c0dd4a
LP
178 } else {
179 assert(m->source);
180
181 r = tempfn_random(m->source, NULL, &m->work_dir);
182 if (r < 0)
183 return log_error_errno(r, "Failed to acquire working directory: %m");
184 }
185
186 (void) mkdir_label(m->work_dir, 0700);
187 }
188 }
189
190 return 0;
191}
192
88614c8a 193int bind_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
e83bebef
LP
194 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL;
195 const char *p = s;
196 CustomMount *m;
197 int r;
198
199 assert(l);
200 assert(n);
201
202 r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
203 if (r < 0)
204 return r;
205 if (r == 0)
206 return -EINVAL;
e83bebef 207 if (r == 1) {
86c0dd4a 208 destination = strdup(source[0] == '+' ? source+1 : source);
e83bebef
LP
209 if (!destination)
210 return -ENOMEM;
211 }
e83bebef
LP
212 if (r == 2 && !isempty(p)) {
213 opts = strdup(p);
214 if (!opts)
215 return -ENOMEM;
216 }
217
c7a4890c 218 if (isempty(source))
0e636bf5 219 source = mfree(source);
c7a4890c 220 else if (!source_path_is_valid(source))
e83bebef 221 return -EINVAL;
c7a4890c 222
e83bebef
LP
223 if (!path_is_absolute(destination))
224 return -EINVAL;
0646d3c3
LP
225 if (empty_or_root(destination))
226 return -EINVAL;
e83bebef
LP
227
228 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
229 if (!m)
48cbe5f8 230 return -ENOMEM;
e83bebef 231
0e636bf5
ZJS
232 m->source = TAKE_PTR(source);
233 m->destination = TAKE_PTR(destination);
e83bebef 234 m->read_only = read_only;
0e636bf5 235 m->options = TAKE_PTR(opts);
de40a303 236
e83bebef
LP
237 return 0;
238}
239
88614c8a 240int tmpfs_mount_parse(CustomMount **l, size_t *n, const char *s) {
e83bebef
LP
241 _cleanup_free_ char *path = NULL, *opts = NULL;
242 const char *p = s;
243 CustomMount *m;
244 int r;
245
246 assert(l);
247 assert(n);
248 assert(s);
249
250 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
251 if (r < 0)
252 return r;
253 if (r == 0)
254 return -EINVAL;
255
256 if (isempty(p))
257 opts = strdup("mode=0755");
258 else
259 opts = strdup(p);
260 if (!opts)
261 return -ENOMEM;
262
263 if (!path_is_absolute(path))
264 return -EINVAL;
0646d3c3
LP
265 if (empty_or_root(path))
266 return -EINVAL;
e83bebef
LP
267
268 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
269 if (!m)
270 return -ENOMEM;
271
1cc6c93a
YW
272 m->destination = TAKE_PTR(path);
273 m->options = TAKE_PTR(opts);
e83bebef 274
e83bebef
LP
275 return 0;
276}
277
88614c8a 278int overlay_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) {
ad85779a
LP
279 _cleanup_free_ char *upper = NULL, *destination = NULL;
280 _cleanup_strv_free_ char **lower = NULL;
281 CustomMount *m;
86c0dd4a 282 int k;
ad85779a 283
86c0dd4a
LP
284 k = strv_split_extract(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
285 if (k < 0)
286 return k;
ad85779a
LP
287 if (k < 2)
288 return -EADDRNOTAVAIL;
289 if (k == 2) {
86c0dd4a
LP
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. */
292
293 if (!source_path_is_valid(lower[0]) ||
294 !source_path_is_valid(lower[1]))
295 return -EINVAL;
296
ae2a15bc 297 upper = TAKE_PTR(lower[1]);
ad85779a 298
86c0dd4a 299 destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
ad85779a
LP
300 if (!destination)
301 return -ENOMEM;
ad85779a 302 } else {
c7a4890c 303 char **i;
86c0dd4a
LP
304
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. */
307
ad85779a 308 destination = lower[k - 1];
ae2a15bc 309 upper = TAKE_PTR(lower[k - 2]);
86c0dd4a 310
c7a4890c
LP
311 STRV_FOREACH(i, lower)
312 if (!source_path_is_valid(*i))
313 return -EINVAL;
314
315 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
316 * in /var/tmp */
317 if (isempty(upper))
1d0c1146 318 upper = mfree(upper);
c7a4890c
LP
319 else if (!source_path_is_valid(upper))
320 return -EINVAL;
321
86c0dd4a
LP
322 if (!path_is_absolute(destination))
323 return -EINVAL;
ad85779a
LP
324 }
325
0646d3c3
LP
326 if (empty_or_root(destination))
327 return -EINVAL;
328
ad85779a
LP
329 m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
330 if (!m)
331 return -ENOMEM;
332
1cc6c93a
YW
333 m->destination = TAKE_PTR(destination);
334 m->source = TAKE_PTR(upper);
335 m->lower = TAKE_PTR(lower);
ad85779a
LP
336 m->read_only = read_only;
337
ad85779a
LP
338 return 0;
339}
340
de40a303
LP
341int inaccessible_mount_parse(CustomMount **l, size_t *n, const char *s) {
342 _cleanup_free_ char *path = NULL;
343 CustomMount *m;
344
345 assert(l);
346 assert(n);
347 assert(s);
348
349 if (!path_is_absolute(s))
350 return -EINVAL;
351
352 path = strdup(s);
353 if (!path)
354 return -ENOMEM;
355
356 m = custom_mount_add(l, n, CUSTOM_MOUNT_INACCESSIBLE);
357 if (!m)
358 return -ENOMEM;
359
360 m->destination = TAKE_PTR(path);
361 return 0;
362}
363
04029482 364int tmpfs_patch_options(
e83bebef 365 const char *options,
2fa017f1 366 uid_t uid_shift,
e83bebef
LP
367 const char *selinux_apifs_context,
368 char **ret) {
369
370 char *buf = NULL;
371
2fa017f1 372 if (uid_shift != UID_INVALID) {
9aa2169e 373 if (asprintf(&buf, "%s%suid=" UID_FMT ",gid=" UID_FMT,
87e4e28d 374 strempty(options), options ? "," : "",
9aa2169e 375 uid_shift, uid_shift) < 0)
e83bebef
LP
376 return -ENOMEM;
377
378 options = buf;
379 }
380
349cc4a5 381#if HAVE_SELINUX
e83bebef
LP
382 if (selinux_apifs_context) {
383 char *t;
384
87e4e28d 385 t = strjoin(strempty(options), options ? "," : "",
9aa2169e
ZJS
386 "context=\"", selinux_apifs_context, "\"");
387 free(buf);
388 if (!t)
e83bebef 389 return -ENOMEM;
e83bebef 390
e83bebef
LP
391 buf = t;
392 }
393#endif
394
0996ef00
CB
395 if (!buf && options) {
396 buf = strdup(options);
397 if (!buf)
398 return -ENOMEM;
399 }
e83bebef 400 *ret = buf;
0996ef00 401
e83bebef
LP
402 return !!buf;
403}
404
4f086aab 405int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
d8fc6a00 406 const char *full, *top, *x;
d1678248 407 int r;
4f086aab 408 unsigned long extra_flags = 0;
d8fc6a00
LP
409
410 top = prefix_roota(dest, "/sys");
40fd52f2 411 r = path_is_fs_type(top, SYSFS_MAGIC);
d1678248
ILG
412 if (r < 0)
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.
417 */
418 if (r > 0)
419 return 0;
420
d8fc6a00
LP
421 full = prefix_roota(top, "/full");
422
423 (void) mkdir(full, 0755);
424
4f086aab
SU
425 if (mount_settings & MOUNT_APPLY_APIVFS_RO)
426 extra_flags |= MS_RDONLY;
427
60e76d48 428 r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs",
4f086aab 429 MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
60e76d48
ZJS
430 if (r < 0)
431 return r;
d8fc6a00
LP
432
433 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
434 _cleanup_free_ char *from = NULL, *to = NULL;
435
436 from = prefix_root(full, x);
437 if (!from)
438 return log_oom();
439
440 to = prefix_root(top, x);
441 if (!to)
442 return log_oom();
443
444 (void) mkdir(to, 0755);
445
60e76d48
ZJS
446 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
447 if (r < 0)
448 return r;
d8fc6a00 449
60e76d48 450 r = mount_verbose(LOG_ERR, NULL, to, NULL,
4f086aab 451 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
60e76d48
ZJS
452 if (r < 0)
453 return r;
d8fc6a00
LP
454 }
455
60e76d48
ZJS
456 r = umount_verbose(full);
457 if (r < 0)
458 return r;
d8fc6a00
LP
459
460 if (rmdir(full) < 0)
461 return log_error_errno(errno, "Failed to remove %s: %m", full);
462
0996ef00
CB
463 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
464 * remount /sys read-only.
465 */
677a72cd
LS
466 x = prefix_roota(top, "/fs/cgroup");
467 (void) mkdir_p(x, 0755);
d8fc6a00 468
60e76d48 469 return mount_verbose(LOG_ERR, NULL, top, NULL,
4f086aab 470 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
d8fc6a00
LP
471}
472
9c0fad5f 473static int mkdir_userns(const char *path, mode_t mode, uid_t uid_shift) {
63eae723
EV
474 int r;
475
476 assert(path);
477
dae8b82e
ZJS
478 r = mkdir_errno_wrapper(path, mode);
479 if (r < 0 && r != -EEXIST)
480 return r;
63eae723 481
9c0fad5f 482 if (uid_shift == UID_INVALID)
acbbf69b
LP
483 return 0;
484
dae8b82e 485 if (lchown(path, uid_shift, uid_shift) < 0)
acbbf69b 486 return -errno;
63eae723
EV
487
488 return 0;
489}
490
9c0fad5f 491static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, uid_t uid_shift) {
63eae723
EV
492 const char *p, *e;
493 int r;
494
495 assert(path);
496
497 if (prefix && !path_startswith(path, prefix))
498 return -ENOTDIR;
499
500 /* create every parent directory in the path, except the last component */
501 p = path + strspn(path, "/");
502 for (;;) {
503 char t[strlen(path) + 1];
504
505 e = p + strcspn(p, "/");
506 p = e + strspn(e, "/");
507
508 /* Is this the last component? If so, then we're done */
509 if (*p == 0)
510 break;
511
512 memcpy(t, path, e - path);
513 t[e-path] = 0;
514
515 if (prefix && path_startswith(prefix, t))
516 continue;
517
9c0fad5f 518 r = mkdir_userns(t, mode, uid_shift);
63eae723
EV
519 if (r < 0)
520 return r;
521 }
522
9c0fad5f 523 return mkdir_userns(path, mode, uid_shift);
63eae723
EV
524}
525
e83bebef 526int mount_all(const char *dest,
4f086aab 527 MountSettingsMask mount_settings,
2fa017f1 528 uid_t uid_shift,
e83bebef
LP
529 const char *selinux_apifs_context) {
530
de40a303
LP
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 ... */ \
d4b653c5
LP
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 */
536
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 */
542
e83bebef
LP
543 typedef struct MountPoint {
544 const char *what;
545 const char *where;
546 const char *type;
547 const char *options;
548 unsigned long flags;
4f086aab 549 MountSettingsMask mount_settings;
e83bebef
LP
550 } MountPoint;
551
552 static const MountPoint mount_table[] = {
d4b653c5
LP
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 },
556
557 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND,
558 MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
559
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) */
562
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 */
565
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 */
de40a303
LP
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"),
d4b653c5
LP
573
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"),
583
849b9b85
LP
584 { "mqueue", "/dev/mqueue", "mqueue", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV,
585 MOUNT_IN_USERNS },
586
d4b653c5
LP
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,
1099ceeb 589 MOUNT_FATAL|MOUNT_APPLY_TMPFS_TMP },
03d0f4b5 590 { "tmpfs", "/sys", "tmpfs", "mode=555", MS_NOSUID|MS_NOEXEC|MS_NODEV,
d4b653c5
LP
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,
597 MOUNT_FATAL },
598 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
599 MOUNT_FATAL },
600 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME,
601 MOUNT_FATAL },
602
349cc4a5 603#if HAVE_SELINUX
d4b653c5
LP
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 */
e83bebef
LP
608#endif
609 };
610
4f086aab
SU
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);
1099ceeb 615 bool tmpfs_tmp = (mount_settings & MOUNT_APPLY_TMPFS_TMP);
d4b653c5 616 size_t k;
88614c8a 617 int r;
e83bebef
LP
618
619 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
620 _cleanup_free_ char *where = NULL, *options = NULL;
de40a303 621 const char *o;
4f086aab
SU
622 bool fatal = (mount_table[k].mount_settings & MOUNT_FATAL);
623
624 if (in_userns != (bool)(mount_table[k].mount_settings & MOUNT_IN_USERNS))
625 continue;
e83bebef 626
4f086aab 627 if (!netns && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_NETNS))
d1678248
ILG
628 continue;
629
4f086aab 630 if (!ro && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_RO))
e83bebef
LP
631 continue;
632
1099ceeb
LP
633 if (!tmpfs_tmp && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_TMPFS_TMP))
634 continue;
635
cb638b5e 636 r = chase_symlinks(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where);
8ce48cf0 637 if (r < 0)
ec57bd42 638 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, mount_table[k].where);
e83bebef 639
e83bebef 640 /* Skip this entry if it is not a remount. */
de40a303
LP
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);
645 if (r > 0)
646 continue;
647 }
e83bebef 648
9c0fad5f 649 r = mkdir_userns_p(dest, where, 0755, (use_userns && !in_userns) ? uid_shift : UID_INVALID);
920a7899 650 if (r < 0 && r != -EEXIST) {
4f13e534 651 if (fatal && r != -EROFS)
e83bebef
LP
652 return log_error_errno(r, "Failed to create directory %s: %m", where);
653
201b13c8 654 log_debug_errno(r, "Failed to create directory %s: %m", where);
4f13e534
LT
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 */
658 if (r != -EROFS)
659 continue;
e83bebef
LP
660 }
661
662 o = mount_table[k].options;
663 if (streq_ptr(mount_table[k].type, "tmpfs")) {
2fa017f1 664 r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options);
e83bebef
LP
665 if (r < 0)
666 return log_oom();
667 if (r > 0)
668 o = options;
669 }
670
4f086aab 671 r = mount_verbose(fatal ? LOG_ERR : LOG_DEBUG,
de40a303 672 mount_table[k].what,
60e76d48
ZJS
673 where,
674 mount_table[k].type,
675 mount_table[k].flags,
676 o);
4f086aab 677 if (r < 0 && fatal)
60e76d48 678 return r;
e83bebef
LP
679 }
680
681 return 0;
682}
683
e83bebef 684static int mount_bind(const char *dest, CustomMount *m) {
72d967df 685 _cleanup_free_ char *where = NULL;
68cf43c3 686 struct stat source_st, dest_st;
e83bebef
LP
687 int r;
688
86c0dd4a 689 assert(dest);
e83bebef
LP
690 assert(m);
691
e83bebef
LP
692 if (stat(m->source, &source_st) < 0)
693 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
694
cb638b5e 695 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
68cf43c3 696 if (r < 0)
ec57bd42 697 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
698 if (r > 0) { /* Path exists already? */
699
700 if (stat(where, &dest_st) < 0)
701 return log_error_errno(errno, "Failed to stat %s: %m", where);
e83bebef 702
baaa35ad
ZJS
703 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode))
704 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
705 "Cannot bind mount directory %s on file %s.",
706 m->source, where);
707
708 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode))
709 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
710 "Cannot bind mount file %s on directory %s.",
711 m->source, where);
e83bebef 712
8ce48cf0 713 } else { /* Path doesn't exist yet? */
e83bebef
LP
714 r = mkdir_parents_label(where, 0755);
715 if (r < 0)
716 return log_error_errno(r, "Failed to make parents of %s: %m", where);
b97e83cb
BN
717
718 /* Create the mount point. Any non-directory file can be
719 * mounted on any non-directory file (regular, fifo, socket,
720 * char, block).
721 */
722 if (S_ISDIR(source_st.st_mode))
723 r = mkdir_label(where, 0755);
724 else
725 r = touch(where);
726 if (r < 0)
727 return log_error_errno(r, "Failed to create mount point %s: %m", where);
8ce48cf0 728 }
e83bebef 729
72d967df 730 r = mount_verbose(LOG_ERR, m->source, where, NULL, MS_BIND | MS_REC, m->options);
60e76d48
ZJS
731 if (r < 0)
732 return r;
e83bebef
LP
733
734 if (m->read_only) {
64e82c19 735 r = bind_remount_recursive(where, MS_RDONLY, MS_RDONLY, NULL);
e83bebef
LP
736 if (r < 0)
737 return log_error_errno(r, "Read-only bind mount failed: %m");
738 }
739
740 return 0;
741}
742
743static int mount_tmpfs(
744 const char *dest,
745 CustomMount *m,
746 bool userns, uid_t uid_shift, uid_t uid_range,
747 const char *selinux_apifs_context) {
748
68cf43c3
LP
749 const char *options;
750 _cleanup_free_ char *buf = NULL, *where = NULL;
e83bebef
LP
751 int r;
752
753 assert(dest);
754 assert(m);
755
cb638b5e 756 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
68cf43c3 757 if (r < 0)
ec57bd42 758 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
759 if (r == 0) { /* Doesn't exist yet? */
760 r = mkdir_p_label(where, 0755);
761 if (r < 0)
762 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
763 }
e83bebef 764
2fa017f1 765 r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef
LP
766 if (r < 0)
767 return log_oom();
768 options = r > 0 ? buf : m->options;
769
60e76d48 770 return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
e83bebef
LP
771}
772
86c0dd4a 773static char *joined_and_escaped_lower_dirs(char **lower) {
e83bebef
LP
774 _cleanup_strv_free_ char **sv = NULL;
775
776 sv = strv_copy(lower);
777 if (!sv)
778 return NULL;
779
780 strv_reverse(sv);
781
782 if (!strv_shell_escape(sv, ",:"))
783 return NULL;
784
785 return strv_join(sv, ":");
786}
787
788static int mount_overlay(const char *dest, CustomMount *m) {
86c0dd4a 789 _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
68cf43c3 790 const char *options;
e83bebef
LP
791 int r;
792
793 assert(dest);
794 assert(m);
795
cb638b5e 796 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
68cf43c3 797 if (r < 0)
ec57bd42 798 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
8ce48cf0
LP
799 if (r == 0) { /* Doesn't exist yet? */
800 r = mkdir_label(where, 0755);
801 if (r < 0)
802 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
803 }
e83bebef
LP
804
805 (void) mkdir_p_label(m->source, 0755);
806
807 lower = joined_and_escaped_lower_dirs(m->lower);
808 if (!lower)
809 return log_oom();
810
86c0dd4a
LP
811 escaped_source = shell_escape(m->source, ",:");
812 if (!escaped_source)
813 return log_oom();
e83bebef 814
86c0dd4a 815 if (m->read_only)
e83bebef 816 options = strjoina("lowerdir=", escaped_source, ":", lower);
86c0dd4a
LP
817 else {
818 _cleanup_free_ char *escaped_work_dir = NULL;
e83bebef 819
e83bebef
LP
820 escaped_work_dir = shell_escape(m->work_dir, ",:");
821 if (!escaped_work_dir)
822 return log_oom();
823
824 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
825 }
826
60e76d48 827 return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
e83bebef
LP
828}
829
de40a303
LP
830static int mount_inaccessible(const char *dest, CustomMount *m) {
831 _cleanup_free_ char *where = NULL;
832 const char *source;
833 struct stat st;
834 int r;
835
836 assert(dest);
837 assert(m);
838
839 r = chase_symlinks_and_stat(m->destination, dest, CHASE_PREFIX_ROOT, &where, &st);
840 if (r < 0) {
841 log_full_errno(m->graceful ? LOG_DEBUG : LOG_ERR, r, "Failed to resolve %s/%s: %m", dest, m->destination);
842 return m->graceful ? 0 : r;
843 }
844
845 assert_se(source = mode_to_inaccessible_node(st.st_mode));
846
847 r = mount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, source, where, NULL, MS_BIND, NULL);
848 if (r < 0)
849 return m->graceful ? 0 : r;
850
851 r = mount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, NULL, where, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
2c9b7a7e
LP
852 if (r < 0) {
853 umount_verbose(where);
de40a303 854 return m->graceful ? 0 : r;
2c9b7a7e 855 }
de40a303
LP
856
857 return 0;
858}
859
860static int mount_arbitrary(const char *dest, CustomMount *m) {
861 _cleanup_free_ char *where = NULL;
862 int r;
863
864 assert(dest);
865 assert(m);
866
867 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
868 if (r < 0)
869 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
870 if (r == 0) { /* Doesn't exist yet? */
871 r = mkdir_p_label(where, 0755);
872 if (r < 0)
873 return log_error_errno(r, "Creating mount point for mount %s failed: %m", where);
874 }
875
876 return mount_verbose(LOG_ERR, m->source, where, m->type_argument, 0, m->options);
877}
878
e83bebef
LP
879int mount_custom(
880 const char *dest,
88614c8a 881 CustomMount *mounts, size_t n,
e83bebef 882 bool userns, uid_t uid_shift, uid_t uid_range,
de40a303
LP
883 const char *selinux_apifs_context,
884 bool in_userns) {
e83bebef 885
88614c8a 886 size_t i;
e83bebef
LP
887 int r;
888
889 assert(dest);
890
891 for (i = 0; i < n; i++) {
892 CustomMount *m = mounts + i;
893
de40a303
LP
894 if (m->in_userns != in_userns)
895 continue;
896
e83bebef
LP
897 switch (m->type) {
898
899 case CUSTOM_MOUNT_BIND:
900 r = mount_bind(dest, m);
901 break;
902
903 case CUSTOM_MOUNT_TMPFS:
904 r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context);
905 break;
906
907 case CUSTOM_MOUNT_OVERLAY:
908 r = mount_overlay(dest, m);
909 break;
910
de40a303
LP
911 case CUSTOM_MOUNT_INACCESSIBLE:
912 r = mount_inaccessible(dest, m);
913 break;
914
915 case CUSTOM_MOUNT_ARBITRARY:
916 r = mount_arbitrary(dest, m);
917 break;
918
e83bebef
LP
919 default:
920 assert_not_reached("Unknown custom mount type");
921 }
922
923 if (r < 0)
924 return r;
925 }
926
927 return 0;
928}
929
e5b43a04 930static int setup_volatile_state(
e83bebef 931 const char *directory,
e83bebef
LP
932 bool userns, uid_t uid_shift, uid_t uid_range,
933 const char *selinux_apifs_context) {
934
935 _cleanup_free_ char *buf = NULL;
936 const char *p, *options;
937 int r;
938
939 assert(directory);
940
e5b43a04 941 /* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */
e83bebef 942
64e82c19 943 r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
e83bebef
LP
944 if (r < 0)
945 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
946
947 p = prefix_roota(directory, "/var");
948 r = mkdir(p, 0755);
949 if (r < 0 && errno != EEXIST)
950 return log_error_errno(errno, "Failed to create %s: %m", directory);
951
952 options = "mode=755";
2fa017f1 953 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef
LP
954 if (r < 0)
955 return log_oom();
956 if (r > 0)
957 options = buf;
958
60e76d48 959 return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
e83bebef
LP
960}
961
e5b43a04 962static int setup_volatile_yes(
e83bebef 963 const char *directory,
e83bebef
LP
964 bool userns, uid_t uid_shift, uid_t uid_range,
965 const char *selinux_apifs_context) {
966
967 bool tmpfs_mounted = false, bind_mounted = false;
968 char template[] = "/tmp/nspawn-volatile-XXXXXX";
969 _cleanup_free_ char *buf = NULL;
970 const char *f, *t, *options;
971 int r;
972
973 assert(directory);
974
e5b43a04
LP
975 /* --volatile=yes means we mount a tmpfs to the root dir, and the original /usr to use inside it, and that
976 read-only. */
e83bebef
LP
977
978 if (!mkdtemp(template))
979 return log_error_errno(errno, "Failed to create temporary directory: %m");
980
981 options = "mode=755";
2fa017f1 982 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
e83bebef 983 if (r < 0)
c55d0ae7 984 goto fail;
e83bebef
LP
985 if (r > 0)
986 options = buf;
987
60e76d48
ZJS
988 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
989 if (r < 0)
e83bebef 990 goto fail;
e83bebef
LP
991
992 tmpfs_mounted = true;
993
994 f = prefix_roota(directory, "/usr");
995 t = prefix_roota(template, "/usr");
996
997 r = mkdir(t, 0755);
998 if (r < 0 && errno != EEXIST) {
999 r = log_error_errno(errno, "Failed to create %s: %m", t);
1000 goto fail;
1001 }
1002
60e76d48
ZJS
1003 r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
1004 if (r < 0)
e83bebef 1005 goto fail;
e83bebef
LP
1006
1007 bind_mounted = true;
1008
64e82c19 1009 r = bind_remount_recursive(t, MS_RDONLY, MS_RDONLY, NULL);
e83bebef
LP
1010 if (r < 0) {
1011 log_error_errno(r, "Failed to remount %s read-only: %m", t);
1012 goto fail;
1013 }
1014
60e76d48
ZJS
1015 r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
1016 if (r < 0)
e83bebef 1017 goto fail;
e83bebef
LP
1018
1019 (void) rmdir(template);
1020
1021 return 0;
1022
1023fail:
1024 if (bind_mounted)
60e76d48 1025 (void) umount_verbose(t);
e83bebef
LP
1026
1027 if (tmpfs_mounted)
60e76d48 1028 (void) umount_verbose(template);
e83bebef
LP
1029 (void) rmdir(template);
1030 return r;
1031}
b53ede69 1032
6c610aca
LP
1033static int setup_volatile_overlay(
1034 const char *directory,
1035 bool userns, uid_t uid_shift, uid_t uid_range,
1036 const char *selinux_apifs_context) {
1037
1038 _cleanup_free_ char *buf = NULL, *escaped_directory = NULL, *escaped_upper = NULL, *escaped_work = NULL;
1039 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1040 const char *upper, *work, *options;
1041 bool tmpfs_mounted = false;
1042 int r;
1043
1044 assert(directory);
1045
1046 /* --volatile=overlay means we mount an overlayfs to the root dir. */
1047
1048 if (!mkdtemp(template))
1049 return log_error_errno(errno, "Failed to create temporary directory: %m");
1050
1051 options = "mode=755";
1052 r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
1053 if (r < 0)
1054 goto finish;
1055 if (r > 0)
1056 options = buf;
1057
1058 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1059 if (r < 0)
1060 goto finish;
1061
1062 tmpfs_mounted = true;
1063
1064 upper = strjoina(template, "/upper");
1065 work = strjoina(template, "/work");
1066
1067 if (mkdir(upper, 0755) < 0) {
1068 r = log_error_errno(errno, "Failed to create %s: %m", upper);
1069 goto finish;
1070 }
1071 if (mkdir(work, 0755) < 0) {
1072 r = log_error_errno(errno, "Failed to create %s: %m", work);
1073 goto finish;
1074 }
1075
1076 /* And now, let's overmount the root dir with an overlayfs that uses the root dir as lower dir. It's kinda nice
1077 * that the kernel allows us to do that without going through some mount point rearrangements. */
1078
1079 escaped_directory = shell_escape(directory, ",:");
1080 escaped_upper = shell_escape(upper, ",:");
1081 escaped_work = shell_escape(work, ",:");
1082 if (!escaped_directory || !escaped_upper || !escaped_work) {
1083 r = -ENOMEM;
1084 goto finish;
1085 }
1086
1087 options = strjoina("lowerdir=", escaped_directory, ",upperdir=", escaped_upper, ",workdir=", escaped_work);
1088 r = mount_verbose(LOG_ERR, "overlay", directory, "overlay", 0, options);
1089
1090finish:
1091 if (tmpfs_mounted)
1092 (void) umount_verbose(template);
1093
1094 (void) rmdir(template);
1095 return r;
1096}
1097
e5b43a04
LP
1098int setup_volatile_mode(
1099 const char *directory,
1100 VolatileMode mode,
1101 bool userns, uid_t uid_shift, uid_t uid_range,
1102 const char *selinux_apifs_context) {
1103
1104 switch (mode) {
1105
1106 case VOLATILE_YES:
1107 return setup_volatile_yes(directory, userns, uid_shift, uid_range, selinux_apifs_context);
1108
1109 case VOLATILE_STATE:
1110 return setup_volatile_state(directory, userns, uid_shift, uid_range, selinux_apifs_context);
1111
6c610aca
LP
1112 case VOLATILE_OVERLAY:
1113 return setup_volatile_overlay(directory, userns, uid_shift, uid_range, selinux_apifs_context);
1114
e5b43a04
LP
1115 default:
1116 return 0;
1117 }
1118}
1119
b53ede69
PW
1120/* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1121int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
1122 _cleanup_free_ char *root_new = NULL, *root_old = NULL;
1123 const char *p = s;
1124 int r;
1125
1126 assert(pivot_root_new);
1127 assert(pivot_root_old);
1128
1129 r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1130 if (r < 0)
1131 return r;
1132 if (r == 0)
1133 return -EINVAL;
1134
1135 if (isempty(p))
1136 root_old = NULL;
1137 else {
1138 root_old = strdup(p);
1139 if (!root_old)
1140 return -ENOMEM;
1141 }
1142
1143 if (!path_is_absolute(root_new))
1144 return -EINVAL;
1145 if (root_old && !path_is_absolute(root_old))
1146 return -EINVAL;
1147
1148 free_and_replace(*pivot_root_new, root_new);
1149 free_and_replace(*pivot_root_old, root_old);
1150
1151 return 0;
1152}
1153
1154int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
1155 _cleanup_free_ char *directory_pivot_root_new = NULL;
1156 _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
1157 char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX";
1158 bool remove_pivot_tmp = false;
1159 int r;
1160
1161 assert(directory);
1162
1163 if (!pivot_root_new)
1164 return 0;
1165
1166 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1167 * If pivot_root_old is NULL, the existing / disappears.
1168 * This requires a temporary directory, pivot_tmp, which is
1169 * not a child of either.
1170 *
1171 * This is typically used for OSTree-style containers, where
1172 * the root partition contains several sysroots which could be
1173 * run. Normally, one would be chosen by the bootloader and
1174 * pivoted to / by initramfs.
1175 *
1176 * For example, for an OSTree deployment, pivot_root_new
1177 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1178 * code doesn’t do the /var mount which OSTree expects: use
1179 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1180 *
1181 * So in the OSTree case, we’ll end up with something like:
1182 * - directory = /tmp/nspawn-root-123456
1183 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1184 * - pivot_root_old = /sysroot
1185 * - directory_pivot_root_new =
1186 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1187 * - pivot_tmp = /tmp/nspawn-pivot-123456
1188 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1189 *
1190 * Requires all file systems at directory and below to be mounted
1191 * MS_PRIVATE or MS_SLAVE so they can be moved.
1192 */
1193 directory_pivot_root_new = prefix_root(directory, pivot_root_new);
1194
1195 /* Remount directory_pivot_root_new to make it movable. */
1196 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
1197 if (r < 0)
1198 goto done;
1199
1200 if (pivot_root_old) {
1201 if (!mkdtemp(pivot_tmp)) {
1202 r = log_error_errno(errno, "Failed to create temporary directory: %m");
1203 goto done;
1204 }
1205
1206 remove_pivot_tmp = true;
1207 pivot_tmp_pivot_root_old = prefix_root(pivot_tmp, pivot_root_old);
1208
1209 r = mount_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
1210 if (r < 0)
1211 goto done;
1212
1213 r = mount_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
1214 if (r < 0)
1215 goto done;
1216
1217 r = mount_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
1218 if (r < 0)
1219 goto done;
1220 } else {
1221 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
1222 if (r < 0)
1223 goto done;
1224 }
1225
1226done:
1227 if (remove_pivot_tmp)
1228 (void) rmdir(pivot_tmp);
1229
1230 return r;
1231}