]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-mount.c
Merge pull request #6992 from keszybz/fix-test-copy
[thirdparty/systemd.git] / src / nspawn / nspawn-mount.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2015 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sys/mount.h>
21 #include <linux/magic.h>
22
23 #include "alloc-util.h"
24 #include "escape.h"
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "fs-util.h"
28 #include "label.h"
29 #include "mkdir.h"
30 #include "mount-util.h"
31 #include "nspawn-mount.h"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "rm-rf.h"
35 #include "set.h"
36 #include "stat-util.h"
37 #include "string-util.h"
38 #include "strv.h"
39 #include "user-util.h"
40 #include "util.h"
41
42 CustomMount* custom_mount_add(CustomMount **l, unsigned *n, CustomMountType t) {
43 CustomMount *c, *ret;
44
45 assert(l);
46 assert(n);
47 assert(t >= 0);
48 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
49
50 c = realloc_multiply(*l, (*n + 1), sizeof(CustomMount));
51 if (!c)
52 return NULL;
53
54 *l = c;
55 ret = *l + *n;
56 (*n)++;
57
58 *ret = (CustomMount) { .type = t };
59
60 return ret;
61 }
62
63 void custom_mount_free_all(CustomMount *l, unsigned n) {
64 unsigned i;
65
66 for (i = 0; i < n; i++) {
67 CustomMount *m = l + i;
68
69 free(m->source);
70 free(m->destination);
71 free(m->options);
72
73 if (m->work_dir) {
74 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
75 free(m->work_dir);
76 }
77
78 if (m->rm_rf_tmpdir) {
79 (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL);
80 free(m->rm_rf_tmpdir);
81 }
82
83 strv_free(m->lower);
84 }
85
86 free(l);
87 }
88
89 static int custom_mount_compare(const void *a, const void *b) {
90 const CustomMount *x = a, *y = b;
91 int r;
92
93 r = path_compare(x->destination, y->destination);
94 if (r != 0)
95 return r;
96
97 if (x->type < y->type)
98 return -1;
99 if (x->type > y->type)
100 return 1;
101
102 return 0;
103 }
104
105 static bool source_path_is_valid(const char *p) {
106 assert(p);
107
108 if (*p == '+')
109 p++;
110
111 return path_is_absolute(p);
112 }
113
114 static char *resolve_source_path(const char *dest, const char *source) {
115
116 if (!source)
117 return NULL;
118
119 if (source[0] == '+')
120 return prefix_root(dest, source + 1);
121
122 return strdup(source);
123 }
124
125 int custom_mount_prepare_all(const char *dest, CustomMount *l, unsigned n) {
126 unsigned i;
127 int r;
128
129 /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the
130 * parent process, so that we know the temporary directories to remove on exit before we fork off the
131 * children. */
132
133 assert(l || n == 0);
134
135 /* Order the custom mounts, and make sure we have a working directory */
136 qsort_safe(l, n, sizeof(CustomMount), custom_mount_compare);
137
138 for (i = 0; i < n; i++) {
139 CustomMount *m = l + i;
140
141 if (m->source) {
142 char *s;
143
144 s = resolve_source_path(dest, m->source);
145 if (!s)
146 return log_oom();
147
148 free(m->source);
149 m->source = s;
150 } else {
151 /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */
152
153 m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX");
154 if (!m->rm_rf_tmpdir)
155 return log_oom();
156
157 if (!mkdtemp(m->rm_rf_tmpdir)) {
158 m->rm_rf_tmpdir = mfree(m->rm_rf_tmpdir);
159 return log_error_errno(errno, "Failed to acquire temporary directory: %m");
160 }
161
162 m->source = strjoin(m->rm_rf_tmpdir, "/src");
163 if (!m->source)
164 return log_oom();
165
166 if (mkdir(m->source, 0755) < 0)
167 return log_error_errno(errno, "Failed to create %s: %m", m->source);
168 }
169
170 if (m->type == CUSTOM_MOUNT_OVERLAY) {
171 char **j;
172
173 STRV_FOREACH(j, m->lower) {
174 char *s;
175
176 s = resolve_source_path(dest, *j);
177 if (!s)
178 return log_oom();
179
180 free(*j);
181 *j = s;
182 }
183
184 if (m->work_dir) {
185 char *s;
186
187 s = resolve_source_path(dest, m->work_dir);
188 if (!s)
189 return log_oom();
190
191 free(m->work_dir);
192 m->work_dir = s;
193 } else {
194 assert(m->source);
195
196 r = tempfn_random(m->source, NULL, &m->work_dir);
197 if (r < 0)
198 return log_error_errno(r, "Failed to acquire working directory: %m");
199 }
200
201 (void) mkdir_label(m->work_dir, 0700);
202 }
203 }
204
205 return 0;
206 }
207
208 int bind_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_only) {
209 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL;
210 const char *p = s;
211 CustomMount *m;
212 int r;
213
214 assert(l);
215 assert(n);
216
217 r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
218 if (r < 0)
219 return r;
220 if (r == 0)
221 return -EINVAL;
222 if (r == 1) {
223 destination = strdup(source[0] == '+' ? source+1 : source);
224 if (!destination)
225 return -ENOMEM;
226 }
227 if (r == 2 && !isempty(p)) {
228 opts = strdup(p);
229 if (!opts)
230 return -ENOMEM;
231 }
232
233 if (isempty(source))
234 source = NULL;
235 else if (!source_path_is_valid(source))
236 return -EINVAL;
237
238 if (!path_is_absolute(destination))
239 return -EINVAL;
240
241 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
242 if (!m)
243 return -ENOMEM;
244
245 m->source = source;
246 m->destination = destination;
247 m->read_only = read_only;
248 m->options = opts;
249
250 source = destination = opts = NULL;
251 return 0;
252 }
253
254 int tmpfs_mount_parse(CustomMount **l, unsigned *n, const char *s) {
255 _cleanup_free_ char *path = NULL, *opts = NULL;
256 const char *p = s;
257 CustomMount *m;
258 int r;
259
260 assert(l);
261 assert(n);
262 assert(s);
263
264 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
265 if (r < 0)
266 return r;
267 if (r == 0)
268 return -EINVAL;
269
270 if (isempty(p))
271 opts = strdup("mode=0755");
272 else
273 opts = strdup(p);
274 if (!opts)
275 return -ENOMEM;
276
277 if (!path_is_absolute(path))
278 return -EINVAL;
279
280 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
281 if (!m)
282 return -ENOMEM;
283
284 m->destination = path;
285 m->options = opts;
286
287 path = opts = NULL;
288 return 0;
289 }
290
291 int overlay_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_only) {
292 _cleanup_free_ char *upper = NULL, *destination = NULL;
293 _cleanup_strv_free_ char **lower = NULL;
294 CustomMount *m;
295 int k;
296
297 k = strv_split_extract(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
298 if (k < 0)
299 return k;
300 if (k < 2)
301 return -EADDRNOTAVAIL;
302 if (k == 2) {
303 /* If two parameters are specified, the first one is the lower, the second one the upper directory. And
304 * we'll also define the destination mount point the same as the upper. */
305
306 if (!source_path_is_valid(lower[0]) ||
307 !source_path_is_valid(lower[1]))
308 return -EINVAL;
309
310 upper = lower[1];
311 lower[1] = NULL;
312
313 destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */
314 if (!destination)
315 return -ENOMEM;
316 } else {
317 char **i;
318
319 /* If more than two parameters are specified, the last one is the destination, the second to last one
320 * the "upper", and all before that the "lower" directories. */
321
322 destination = lower[k - 1];
323 upper = lower[k - 2];
324 lower[k - 2] = NULL;
325
326 STRV_FOREACH(i, lower)
327 if (!source_path_is_valid(*i))
328 return -EINVAL;
329
330 /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory
331 * in /var/tmp */
332 if (isempty(upper))
333 upper = NULL;
334 else if (!source_path_is_valid(upper))
335 return -EINVAL;
336
337 if (!path_is_absolute(destination))
338 return -EINVAL;
339 }
340
341 m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY);
342 if (!m)
343 return -ENOMEM;
344
345 m->destination = destination;
346 m->source = upper;
347 m->lower = lower;
348 m->read_only = read_only;
349
350 upper = destination = NULL;
351 lower = NULL;
352
353 return 0;
354 }
355
356 static int tmpfs_patch_options(
357 const char *options,
358 bool userns,
359 uid_t uid_shift, uid_t uid_range,
360 bool patch_ids,
361 const char *selinux_apifs_context,
362 char **ret) {
363
364 char *buf = NULL;
365
366 if ((userns && uid_shift != 0) || patch_ids) {
367 assert(uid_shift != UID_INVALID);
368
369 if (asprintf(&buf, "%s%suid=" UID_FMT ",gid=" UID_FMT,
370 options ?: "", options ? "," : "",
371 uid_shift, uid_shift) < 0)
372 return -ENOMEM;
373
374 options = buf;
375 }
376
377 #if HAVE_SELINUX
378 if (selinux_apifs_context) {
379 char *t;
380
381 t = strjoin(options ?: "", options ? "," : "",
382 "context=\"", selinux_apifs_context, "\"");
383 free(buf);
384 if (!t)
385 return -ENOMEM;
386
387 buf = t;
388 }
389 #endif
390
391 if (!buf && options) {
392 buf = strdup(options);
393 if (!buf)
394 return -ENOMEM;
395 }
396 *ret = buf;
397
398 return !!buf;
399 }
400
401 int mount_sysfs(const char *dest, MountSettingsMask mount_settings) {
402 const char *full, *top, *x;
403 int r;
404 unsigned long extra_flags = 0;
405
406 top = prefix_roota(dest, "/sys");
407 r = path_check_fstype(top, SYSFS_MAGIC);
408 if (r < 0)
409 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
410 /* /sys might already be mounted as sysfs by the outer child in the
411 * !netns case. In this case, it's all good. Don't touch it because we
412 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
413 */
414 if (r > 0)
415 return 0;
416
417 full = prefix_roota(top, "/full");
418
419 (void) mkdir(full, 0755);
420
421 if (mount_settings & MOUNT_APPLY_APIVFS_RO)
422 extra_flags |= MS_RDONLY;
423
424 r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs",
425 MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL);
426 if (r < 0)
427 return r;
428
429 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
430 _cleanup_free_ char *from = NULL, *to = NULL;
431
432 from = prefix_root(full, x);
433 if (!from)
434 return log_oom();
435
436 to = prefix_root(top, x);
437 if (!to)
438 return log_oom();
439
440 (void) mkdir(to, 0755);
441
442 r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL);
443 if (r < 0)
444 return r;
445
446 r = mount_verbose(LOG_ERR, NULL, to, NULL,
447 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
448 if (r < 0)
449 return r;
450 }
451
452 r = umount_verbose(full);
453 if (r < 0)
454 return r;
455
456 if (rmdir(full) < 0)
457 return log_error_errno(errno, "Failed to remove %s: %m", full);
458
459 /* Create mountpoint for cgroups. Otherwise we are not allowed since we
460 * remount /sys read-only.
461 */
462 if (cg_ns_supported()) {
463 x = prefix_roota(top, "/fs/cgroup");
464 (void) mkdir_p(x, 0755);
465 }
466
467 return mount_verbose(LOG_ERR, NULL, top, NULL,
468 MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL);
469 }
470
471 static int mkdir_userns(const char *path, mode_t mode, MountSettingsMask mask, uid_t uid_shift) {
472 int r;
473
474 assert(path);
475
476 r = mkdir(path, mode);
477 if (r < 0 && errno != EEXIST)
478 return -errno;
479
480 if ((mask & MOUNT_USE_USERNS) == 0)
481 return 0;
482
483 if (mask & MOUNT_IN_USERNS)
484 return 0;
485
486 r = lchown(path, uid_shift, uid_shift);
487 if (r < 0)
488 return -errno;
489
490 return 0;
491 }
492
493 static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, MountSettingsMask mask, uid_t uid_shift) {
494 const char *p, *e;
495 int r;
496
497 assert(path);
498
499 if (prefix && !path_startswith(path, prefix))
500 return -ENOTDIR;
501
502 /* create every parent directory in the path, except the last component */
503 p = path + strspn(path, "/");
504 for (;;) {
505 char t[strlen(path) + 1];
506
507 e = p + strcspn(p, "/");
508 p = e + strspn(e, "/");
509
510 /* Is this the last component? If so, then we're done */
511 if (*p == 0)
512 break;
513
514 memcpy(t, path, e - path);
515 t[e-path] = 0;
516
517 if (prefix && path_startswith(prefix, t))
518 continue;
519
520 r = mkdir_userns(t, mode, mask, uid_shift);
521 if (r < 0)
522 return r;
523 }
524
525 return mkdir_userns(path, mode, mask, uid_shift);
526 }
527
528 int mount_all(const char *dest,
529 MountSettingsMask mount_settings,
530 uid_t uid_shift, uid_t uid_range,
531 const char *selinux_apifs_context) {
532
533 typedef struct MountPoint {
534 const char *what;
535 const char *where;
536 const char *type;
537 const char *options;
538 unsigned long flags;
539 MountSettingsMask mount_settings;
540 } MountPoint;
541
542 static const MountPoint mount_table[] = {
543 /* inner child mounts */
544 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_IN_USERNS },
545 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
546 { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */
547 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
548 { "/proc/sysrq-trigger", "/proc/sysrq-trigger", NULL, NULL, MS_BIND, MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */
549 { NULL, "/proc/sysrq-trigger", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */
550
551 /* outer child mounts */
552 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL },
553 { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS },
554 { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO }, /* skipped if above was mounted */
555 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL }, /* skipped if above was mounted */
556
557 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME, MOUNT_FATAL },
558 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL },
559 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL },
560 #if HAVE_SELINUX
561 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, 0 }, /* Bind mount first */
562 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, 0 }, /* Then, make it r/o */
563 #endif
564 };
565
566 unsigned k;
567 int r;
568 bool use_userns = (mount_settings & MOUNT_USE_USERNS);
569 bool netns = (mount_settings & MOUNT_APPLY_APIVFS_NETNS);
570 bool ro = (mount_settings & MOUNT_APPLY_APIVFS_RO);
571 bool in_userns = (mount_settings & MOUNT_IN_USERNS);
572
573 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
574 _cleanup_free_ char *where = NULL, *options = NULL;
575 const char *o;
576 bool fatal = (mount_table[k].mount_settings & MOUNT_FATAL);
577
578 if (in_userns != (bool)(mount_table[k].mount_settings & MOUNT_IN_USERNS))
579 continue;
580
581 if (!netns && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_NETNS))
582 continue;
583
584 if (!ro && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_RO))
585 continue;
586
587 r = chase_symlinks(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where);
588 if (r < 0)
589 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, mount_table[k].where);
590
591 r = path_is_mount_point(where, NULL, 0);
592 if (r < 0 && r != -ENOENT)
593 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
594
595 /* Skip this entry if it is not a remount. */
596 if (mount_table[k].what && r > 0)
597 continue;
598
599 r = mkdir_userns_p(dest, where, 0755, mount_settings, uid_shift);
600 if (r < 0 && r != -EEXIST) {
601 if (fatal)
602 return log_error_errno(r, "Failed to create directory %s: %m", where);
603
604 log_debug_errno(r, "Failed to create directory %s: %m", where);
605 continue;
606 }
607
608 o = mount_table[k].options;
609 if (streq_ptr(mount_table[k].type, "tmpfs")) {
610 if (in_userns)
611 r = tmpfs_patch_options(o, use_userns, 0, uid_range, true, selinux_apifs_context, &options);
612 else
613 r = tmpfs_patch_options(o, use_userns, uid_shift, uid_range, false, selinux_apifs_context, &options);
614 if (r < 0)
615 return log_oom();
616 if (r > 0)
617 o = options;
618 }
619
620 r = mount_verbose(fatal ? LOG_ERR : LOG_DEBUG,
621 mount_table[k].what,
622 where,
623 mount_table[k].type,
624 mount_table[k].flags,
625 o);
626 if (r < 0 && fatal)
627 return r;
628 }
629
630 return 0;
631 }
632
633 static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts) {
634 const char *p = options;
635 unsigned long flags = *mount_flags;
636 char *opts = NULL;
637 int r;
638
639 assert(options);
640
641 for (;;) {
642 _cleanup_free_ char *word = NULL;
643
644 r = extract_first_word(&p, &word, ",", 0);
645 if (r < 0)
646 return log_error_errno(r, "Failed to extract mount option: %m");
647 if (r == 0)
648 break;
649
650 if (streq(word, "rbind"))
651 flags |= MS_REC;
652 else if (streq(word, "norbind"))
653 flags &= ~MS_REC;
654 else {
655 log_error("Invalid bind mount option: %s", word);
656 return -EINVAL;
657 }
658 }
659
660 *mount_flags = flags;
661 /* in the future mount_opts will hold string options for mount(2) */
662 *mount_opts = opts;
663
664 return 0;
665 }
666
667 static int mount_bind(const char *dest, CustomMount *m) {
668
669 _cleanup_free_ char *mount_opts = NULL, *where = NULL;
670 unsigned long mount_flags = MS_BIND | MS_REC;
671 struct stat source_st, dest_st;
672 int r;
673
674 assert(dest);
675 assert(m);
676
677 if (m->options) {
678 r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts);
679 if (r < 0)
680 return r;
681 }
682
683 if (stat(m->source, &source_st) < 0)
684 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
685
686 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
687 if (r < 0)
688 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
689 if (r > 0) { /* Path exists already? */
690
691 if (stat(where, &dest_st) < 0)
692 return log_error_errno(errno, "Failed to stat %s: %m", where);
693
694 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode)) {
695 log_error("Cannot bind mount directory %s on file %s.", m->source, where);
696 return -EINVAL;
697 }
698
699 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode)) {
700 log_error("Cannot bind mount file %s on directory %s.", m->source, where);
701 return -EINVAL;
702 }
703
704 } else { /* Path doesn't exist yet? */
705 r = mkdir_parents_label(where, 0755);
706 if (r < 0)
707 return log_error_errno(r, "Failed to make parents of %s: %m", where);
708
709 /* Create the mount point. Any non-directory file can be
710 * mounted on any non-directory file (regular, fifo, socket,
711 * char, block).
712 */
713 if (S_ISDIR(source_st.st_mode))
714 r = mkdir_label(where, 0755);
715 else
716 r = touch(where);
717 if (r < 0)
718 return log_error_errno(r, "Failed to create mount point %s: %m", where);
719
720 }
721
722 r = mount_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts);
723 if (r < 0)
724 return r;
725
726 if (m->read_only) {
727 r = bind_remount_recursive(where, true, NULL);
728 if (r < 0)
729 return log_error_errno(r, "Read-only bind mount failed: %m");
730 }
731
732 return 0;
733 }
734
735 static int mount_tmpfs(
736 const char *dest,
737 CustomMount *m,
738 bool userns, uid_t uid_shift, uid_t uid_range,
739 const char *selinux_apifs_context) {
740
741 const char *options;
742 _cleanup_free_ char *buf = NULL, *where = NULL;
743 int r;
744
745 assert(dest);
746 assert(m);
747
748 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
749 if (r < 0)
750 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
751 if (r == 0) { /* Doesn't exist yet? */
752 r = mkdir_p_label(where, 0755);
753 if (r < 0)
754 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
755 }
756
757 r = tmpfs_patch_options(m->options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
758 if (r < 0)
759 return log_oom();
760 options = r > 0 ? buf : m->options;
761
762 return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options);
763 }
764
765 static char *joined_and_escaped_lower_dirs(char **lower) {
766 _cleanup_strv_free_ char **sv = NULL;
767
768 sv = strv_copy(lower);
769 if (!sv)
770 return NULL;
771
772 strv_reverse(sv);
773
774 if (!strv_shell_escape(sv, ",:"))
775 return NULL;
776
777 return strv_join(sv, ":");
778 }
779
780 static int mount_overlay(const char *dest, CustomMount *m) {
781
782 _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL;
783 const char *options;
784 int r;
785
786 assert(dest);
787 assert(m);
788
789 r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where);
790 if (r < 0)
791 return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
792 if (r == 0) { /* Doesn't exist yet? */
793 r = mkdir_label(where, 0755);
794 if (r < 0)
795 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
796 }
797
798 (void) mkdir_p_label(m->source, 0755);
799
800 lower = joined_and_escaped_lower_dirs(m->lower);
801 if (!lower)
802 return log_oom();
803
804 escaped_source = shell_escape(m->source, ",:");
805 if (!escaped_source)
806 return log_oom();
807
808 if (m->read_only)
809 options = strjoina("lowerdir=", escaped_source, ":", lower);
810 else {
811 _cleanup_free_ char *escaped_work_dir = NULL;
812
813 escaped_work_dir = shell_escape(m->work_dir, ",:");
814 if (!escaped_work_dir)
815 return log_oom();
816
817 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
818 }
819
820 return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options);
821 }
822
823 int mount_custom(
824 const char *dest,
825 CustomMount *mounts, unsigned n,
826 bool userns, uid_t uid_shift, uid_t uid_range,
827 const char *selinux_apifs_context) {
828
829 unsigned i;
830 int r;
831
832 assert(dest);
833
834 for (i = 0; i < n; i++) {
835 CustomMount *m = mounts + i;
836
837 switch (m->type) {
838
839 case CUSTOM_MOUNT_BIND:
840 r = mount_bind(dest, m);
841 break;
842
843 case CUSTOM_MOUNT_TMPFS:
844 r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context);
845 break;
846
847 case CUSTOM_MOUNT_OVERLAY:
848 r = mount_overlay(dest, m);
849 break;
850
851 default:
852 assert_not_reached("Unknown custom mount type");
853 }
854
855 if (r < 0)
856 return r;
857 }
858
859 return 0;
860 }
861
862 /* Retrieve existing subsystems. This function is called in a new cgroup
863 * namespace.
864 */
865 static int get_controllers(Set *subsystems) {
866 _cleanup_fclose_ FILE *f = NULL;
867 char line[LINE_MAX];
868
869 assert(subsystems);
870
871 f = fopen("/proc/self/cgroup", "re");
872 if (!f)
873 return errno == ENOENT ? -ESRCH : -errno;
874
875 FOREACH_LINE(line, f, return -errno) {
876 int r;
877 char *e, *l, *p;
878
879 l = strchr(line, ':');
880 if (!l)
881 continue;
882
883 l++;
884 e = strchr(l, ':');
885 if (!e)
886 continue;
887
888 *e = 0;
889
890 if (STR_IN_SET(l, "", "name=systemd", "name=unified"))
891 continue;
892
893 p = strdup(l);
894 if (!p)
895 return -ENOMEM;
896
897 r = set_consume(subsystems, p);
898 if (r < 0)
899 return r;
900 }
901
902 return 0;
903 }
904
905 static int mount_legacy_cgroup_hierarchy(
906 const char *dest,
907 const char *controller,
908 const char *hierarchy,
909 bool read_only) {
910
911 const char *to, *fstype, *opts;
912 int r;
913
914 to = strjoina(strempty(dest), "/sys/fs/cgroup/", hierarchy);
915
916 r = path_is_mount_point(to, dest, 0);
917 if (r < 0 && r != -ENOENT)
918 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", to);
919 if (r > 0)
920 return 0;
921
922 mkdir_p(to, 0755);
923
924 /* The superblock mount options of the mount point need to be
925 * identical to the hosts', and hence writable... */
926 if (streq(controller, SYSTEMD_CGROUP_CONTROLLER_HYBRID)) {
927 fstype = "cgroup2";
928 opts = NULL;
929 } else if (streq(controller, SYSTEMD_CGROUP_CONTROLLER_LEGACY)) {
930 fstype = "cgroup";
931 opts = "none,name=systemd,xattr";
932 } else {
933 fstype = "cgroup";
934 opts = controller;
935 }
936
937 r = mount_verbose(LOG_ERR, "cgroup", to, fstype, MS_NOSUID|MS_NOEXEC|MS_NODEV, opts);
938 if (r < 0)
939 return r;
940
941 /* ... hence let's only make the bind mount read-only, not the superblock. */
942 if (read_only) {
943 r = mount_verbose(LOG_ERR, NULL, to, NULL,
944 MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL);
945 if (r < 0)
946 return r;
947 }
948
949 return 1;
950 }
951
952 /* Mount a legacy cgroup hierarchy when cgroup namespaces are supported. */
953 static int mount_legacy_cgns_supported(
954 const char *dest,
955 CGroupUnified unified_requested,
956 bool userns,
957 uid_t uid_shift,
958 uid_t uid_range,
959 const char *selinux_apifs_context) {
960
961 _cleanup_set_free_free_ Set *controllers = NULL;
962 const char *cgroup_root = "/sys/fs/cgroup", *c;
963 int r;
964
965 (void) mkdir_p(cgroup_root, 0755);
966
967 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
968 r = path_is_mount_point(cgroup_root, dest, AT_SYMLINK_FOLLOW);
969 if (r < 0)
970 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
971 if (r == 0) {
972 _cleanup_free_ char *options = NULL;
973
974 /* When cgroup namespaces are enabled and user namespaces are
975 * used then the mount of the cgroupfs is done *inside* the new
976 * user namespace. We're root in the new user namespace and the
977 * kernel will happily translate our uid/gid to the correct
978 * uid/gid as seen from e.g. /proc/1/mountinfo. So we simply
979 * pass uid 0 and not uid_shift to tmpfs_patch_options().
980 */
981 r = tmpfs_patch_options("mode=755", userns, 0, uid_range, true, selinux_apifs_context, &options);
982 if (r < 0)
983 return log_oom();
984
985 r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs",
986 MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options);
987 if (r < 0)
988 return r;
989 }
990
991 r = cg_all_unified();
992 if (r < 0)
993 return r;
994 if (r > 0)
995 goto skip_controllers;
996
997 controllers = set_new(&string_hash_ops);
998 if (!controllers)
999 return log_oom();
1000
1001 r = get_controllers(controllers);
1002 if (r < 0)
1003 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
1004
1005 for (;;) {
1006 _cleanup_free_ const char *controller = NULL;
1007
1008 controller = set_steal_first(controllers);
1009 if (!controller)
1010 break;
1011
1012 r = mount_legacy_cgroup_hierarchy("", controller, controller, !userns);
1013 if (r < 0)
1014 return r;
1015
1016 /* When multiple hierarchies are co-mounted, make their
1017 * constituting individual hierarchies a symlink to the
1018 * co-mount.
1019 */
1020 c = controller;
1021 for (;;) {
1022 _cleanup_free_ char *target = NULL, *tok = NULL;
1023
1024 r = extract_first_word(&c, &tok, ",", 0);
1025 if (r < 0)
1026 return log_error_errno(r, "Failed to extract co-mounted cgroup controller: %m");
1027 if (r == 0)
1028 break;
1029
1030 target = prefix_root("/sys/fs/cgroup", tok);
1031 if (!target)
1032 return log_oom();
1033
1034 if (streq(controller, tok))
1035 break;
1036
1037 r = symlink_idempotent(controller, target);
1038 if (r == -EINVAL)
1039 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
1040 if (r < 0)
1041 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
1042 }
1043 }
1044
1045 skip_controllers:
1046 if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) {
1047 r = mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER_HYBRID, "unified", false);
1048 if (r < 0)
1049 return r;
1050 }
1051
1052 r = mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER_LEGACY, "systemd", false);
1053 if (r < 0)
1054 return r;
1055
1056 if (!userns)
1057 return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL,
1058 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
1059
1060 return 0;
1061 }
1062
1063 /* Mount legacy cgroup hierarchy when cgroup namespaces are unsupported. */
1064 static int mount_legacy_cgns_unsupported(
1065 const char *dest,
1066 CGroupUnified unified_requested,
1067 bool userns,
1068 uid_t uid_shift,
1069 uid_t uid_range,
1070 const char *selinux_apifs_context) {
1071
1072 _cleanup_set_free_free_ Set *controllers = NULL;
1073 const char *cgroup_root;
1074 int r;
1075
1076 cgroup_root = prefix_roota(dest, "/sys/fs/cgroup");
1077
1078 (void) mkdir_p(cgroup_root, 0755);
1079
1080 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
1081 r = path_is_mount_point(cgroup_root, dest, AT_SYMLINK_FOLLOW);
1082 if (r < 0)
1083 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
1084 if (r == 0) {
1085 _cleanup_free_ char *options = NULL;
1086
1087 r = tmpfs_patch_options("mode=755", userns, uid_shift, uid_range, false, selinux_apifs_context, &options);
1088 if (r < 0)
1089 return log_oom();
1090
1091 r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs",
1092 MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options);
1093 if (r < 0)
1094 return r;
1095 }
1096
1097 r = cg_all_unified();
1098 if (r < 0)
1099 return r;
1100 if (r > 0)
1101 goto skip_controllers;
1102
1103 controllers = set_new(&string_hash_ops);
1104 if (!controllers)
1105 return log_oom();
1106
1107 r = cg_kernel_controllers(controllers);
1108 if (r < 0)
1109 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
1110
1111 for (;;) {
1112 _cleanup_free_ char *controller = NULL, *origin = NULL, *combined = NULL;
1113
1114 controller = set_steal_first(controllers);
1115 if (!controller)
1116 break;
1117
1118 origin = prefix_root("/sys/fs/cgroup/", controller);
1119 if (!origin)
1120 return log_oom();
1121
1122 r = readlink_malloc(origin, &combined);
1123 if (r == -EINVAL) {
1124 /* Not a symbolic link, but directly a single cgroup hierarchy */
1125
1126 r = mount_legacy_cgroup_hierarchy(dest, controller, controller, true);
1127 if (r < 0)
1128 return r;
1129
1130 } else if (r < 0)
1131 return log_error_errno(r, "Failed to read link %s: %m", origin);
1132 else {
1133 _cleanup_free_ char *target = NULL;
1134
1135 target = prefix_root(dest, origin);
1136 if (!target)
1137 return log_oom();
1138
1139 /* A symbolic link, a combination of controllers in one hierarchy */
1140
1141 if (!filename_is_valid(combined)) {
1142 log_warning("Ignoring invalid combined hierarchy %s.", combined);
1143 continue;
1144 }
1145
1146 r = mount_legacy_cgroup_hierarchy(dest, combined, combined, true);
1147 if (r < 0)
1148 return r;
1149
1150 r = symlink_idempotent(combined, target);
1151 if (r == -EINVAL)
1152 return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m");
1153 if (r < 0)
1154 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
1155 }
1156 }
1157
1158 skip_controllers:
1159 if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) {
1160 r = mount_legacy_cgroup_hierarchy(dest, SYSTEMD_CGROUP_CONTROLLER_HYBRID, "unified", false);
1161 if (r < 0)
1162 return r;
1163 }
1164
1165 r = mount_legacy_cgroup_hierarchy(dest, SYSTEMD_CGROUP_CONTROLLER_LEGACY, "systemd", false);
1166 if (r < 0)
1167 return r;
1168
1169 return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL,
1170 MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755");
1171 }
1172
1173 static int mount_unified_cgroups(const char *dest) {
1174 const char *p;
1175 int r;
1176
1177 assert(dest);
1178
1179 p = prefix_roota(dest, "/sys/fs/cgroup");
1180
1181 (void) mkdir_p(p, 0755);
1182
1183 r = path_is_mount_point(p, dest, AT_SYMLINK_FOLLOW);
1184 if (r < 0)
1185 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p);
1186 if (r > 0) {
1187 p = prefix_roota(dest, "/sys/fs/cgroup/cgroup.procs");
1188 if (access(p, F_OK) >= 0)
1189 return 0;
1190 if (errno != ENOENT)
1191 return log_error_errno(errno, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p);
1192
1193 log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p);
1194 return -EINVAL;
1195 }
1196
1197 return mount_verbose(LOG_ERR, "cgroup", p, "cgroup2", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL);
1198 }
1199
1200 int mount_cgroups(
1201 const char *dest,
1202 CGroupUnified unified_requested,
1203 bool userns,
1204 uid_t uid_shift,
1205 uid_t uid_range,
1206 const char *selinux_apifs_context,
1207 bool use_cgns) {
1208
1209 if (unified_requested >= CGROUP_UNIFIED_ALL)
1210 return mount_unified_cgroups(dest);
1211 else if (use_cgns)
1212 return mount_legacy_cgns_supported(dest, unified_requested, userns, uid_shift, uid_range, selinux_apifs_context);
1213
1214 return mount_legacy_cgns_unsupported(dest, unified_requested, userns, uid_shift, uid_range, selinux_apifs_context);
1215 }
1216
1217 static int mount_systemd_cgroup_writable_one(const char *systemd_own, const char *systemd_root)
1218 {
1219 int r;
1220
1221 /* Make our own cgroup a (writable) bind mount */
1222 r = mount_verbose(LOG_ERR, systemd_own, systemd_own, NULL, MS_BIND, NULL);
1223 if (r < 0)
1224 return r;
1225
1226 /* And then remount the systemd cgroup root read-only */
1227 return mount_verbose(LOG_ERR, NULL, systemd_root, NULL,
1228 MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL);
1229 }
1230
1231 int mount_systemd_cgroup_writable(
1232 const char *dest,
1233 CGroupUnified unified_requested) {
1234
1235 _cleanup_free_ char *own_cgroup_path = NULL;
1236 int r;
1237
1238 assert(dest);
1239
1240 r = cg_pid_get_path(NULL, 0, &own_cgroup_path);
1241 if (r < 0)
1242 return log_error_errno(r, "Failed to determine our own cgroup path: %m");
1243
1244 /* If we are living in the top-level, then there's nothing to do... */
1245 if (path_equal(own_cgroup_path, "/"))
1246 return 0;
1247
1248 if (unified_requested >= CGROUP_UNIFIED_ALL)
1249 return mount_systemd_cgroup_writable_one(strjoina(dest, "/sys/fs/cgroup", own_cgroup_path),
1250 prefix_roota(dest, "/sys/fs/cgroup"));
1251
1252 if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) {
1253 r = mount_systemd_cgroup_writable_one(strjoina(dest, "/sys/fs/cgroup/unified", own_cgroup_path),
1254 prefix_roota(dest, "/sys/fs/cgroup/unified"));
1255 if (r < 0)
1256 return r;
1257 }
1258
1259 return mount_systemd_cgroup_writable_one(strjoina(dest, "/sys/fs/cgroup/systemd", own_cgroup_path),
1260 prefix_roota(dest, "/sys/fs/cgroup/systemd"));
1261 }
1262
1263 int setup_volatile_state(
1264 const char *directory,
1265 VolatileMode mode,
1266 bool userns, uid_t uid_shift, uid_t uid_range,
1267 const char *selinux_apifs_context) {
1268
1269 _cleanup_free_ char *buf = NULL;
1270 const char *p, *options;
1271 int r;
1272
1273 assert(directory);
1274
1275 if (mode != VOLATILE_STATE)
1276 return 0;
1277
1278 /* --volatile=state means we simply overmount /var
1279 with a tmpfs, and the rest read-only. */
1280
1281 r = bind_remount_recursive(directory, true, NULL);
1282 if (r < 0)
1283 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
1284
1285 p = prefix_roota(directory, "/var");
1286 r = mkdir(p, 0755);
1287 if (r < 0 && errno != EEXIST)
1288 return log_error_errno(errno, "Failed to create %s: %m", directory);
1289
1290 options = "mode=755";
1291 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
1292 if (r < 0)
1293 return log_oom();
1294 if (r > 0)
1295 options = buf;
1296
1297 return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
1298 }
1299
1300 int setup_volatile(
1301 const char *directory,
1302 VolatileMode mode,
1303 bool userns, uid_t uid_shift, uid_t uid_range,
1304 const char *selinux_apifs_context) {
1305
1306 bool tmpfs_mounted = false, bind_mounted = false;
1307 char template[] = "/tmp/nspawn-volatile-XXXXXX";
1308 _cleanup_free_ char *buf = NULL;
1309 const char *f, *t, *options;
1310 int r;
1311
1312 assert(directory);
1313
1314 if (mode != VOLATILE_YES)
1315 return 0;
1316
1317 /* --volatile=yes means we mount a tmpfs to the root dir, and
1318 the original /usr to use inside it, and that read-only. */
1319
1320 if (!mkdtemp(template))
1321 return log_error_errno(errno, "Failed to create temporary directory: %m");
1322
1323 options = "mode=755";
1324 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf);
1325 if (r < 0)
1326 return log_oom();
1327 if (r > 0)
1328 options = buf;
1329
1330 r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
1331 if (r < 0)
1332 goto fail;
1333
1334 tmpfs_mounted = true;
1335
1336 f = prefix_roota(directory, "/usr");
1337 t = prefix_roota(template, "/usr");
1338
1339 r = mkdir(t, 0755);
1340 if (r < 0 && errno != EEXIST) {
1341 r = log_error_errno(errno, "Failed to create %s: %m", t);
1342 goto fail;
1343 }
1344
1345 r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
1346 if (r < 0)
1347 goto fail;
1348
1349 bind_mounted = true;
1350
1351 r = bind_remount_recursive(t, true, NULL);
1352 if (r < 0) {
1353 log_error_errno(r, "Failed to remount %s read-only: %m", t);
1354 goto fail;
1355 }
1356
1357 r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
1358 if (r < 0)
1359 goto fail;
1360
1361 (void) rmdir(template);
1362
1363 return 0;
1364
1365 fail:
1366 if (bind_mounted)
1367 (void) umount_verbose(t);
1368
1369 if (tmpfs_mounted)
1370 (void) umount_verbose(template);
1371 (void) rmdir(template);
1372 return r;
1373 }
1374
1375 /* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
1376 int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
1377 _cleanup_free_ char *root_new = NULL, *root_old = NULL;
1378 const char *p = s;
1379 int r;
1380
1381 assert(pivot_root_new);
1382 assert(pivot_root_old);
1383
1384 r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
1385 if (r < 0)
1386 return r;
1387 if (r == 0)
1388 return -EINVAL;
1389
1390 if (isempty(p))
1391 root_old = NULL;
1392 else {
1393 root_old = strdup(p);
1394 if (!root_old)
1395 return -ENOMEM;
1396 }
1397
1398 if (!path_is_absolute(root_new))
1399 return -EINVAL;
1400 if (root_old && !path_is_absolute(root_old))
1401 return -EINVAL;
1402
1403 free_and_replace(*pivot_root_new, root_new);
1404 free_and_replace(*pivot_root_old, root_old);
1405
1406 return 0;
1407 }
1408
1409 int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
1410 _cleanup_free_ char *directory_pivot_root_new = NULL;
1411 _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
1412 char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX";
1413 bool remove_pivot_tmp = false;
1414 int r;
1415
1416 assert(directory);
1417
1418 if (!pivot_root_new)
1419 return 0;
1420
1421 /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
1422 * If pivot_root_old is NULL, the existing / disappears.
1423 * This requires a temporary directory, pivot_tmp, which is
1424 * not a child of either.
1425 *
1426 * This is typically used for OSTree-style containers, where
1427 * the root partition contains several sysroots which could be
1428 * run. Normally, one would be chosen by the bootloader and
1429 * pivoted to / by initramfs.
1430 *
1431 * For example, for an OSTree deployment, pivot_root_new
1432 * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
1433 * code doesn’t do the /var mount which OSTree expects: use
1434 * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
1435 *
1436 * So in the OSTree case, we’ll end up with something like:
1437 * - directory = /tmp/nspawn-root-123456
1438 * - pivot_root_new = /ostree/deploy/os/deploy/123abc
1439 * - pivot_root_old = /sysroot
1440 * - directory_pivot_root_new =
1441 * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
1442 * - pivot_tmp = /tmp/nspawn-pivot-123456
1443 * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
1444 *
1445 * Requires all file systems at directory and below to be mounted
1446 * MS_PRIVATE or MS_SLAVE so they can be moved.
1447 */
1448 directory_pivot_root_new = prefix_root(directory, pivot_root_new);
1449
1450 /* Remount directory_pivot_root_new to make it movable. */
1451 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
1452 if (r < 0)
1453 goto done;
1454
1455 if (pivot_root_old) {
1456 if (!mkdtemp(pivot_tmp)) {
1457 r = log_error_errno(errno, "Failed to create temporary directory: %m");
1458 goto done;
1459 }
1460
1461 remove_pivot_tmp = true;
1462 pivot_tmp_pivot_root_old = prefix_root(pivot_tmp, pivot_root_old);
1463
1464 r = mount_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
1465 if (r < 0)
1466 goto done;
1467
1468 r = mount_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
1469 if (r < 0)
1470 goto done;
1471
1472 r = mount_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
1473 if (r < 0)
1474 goto done;
1475 } else {
1476 r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
1477 if (r < 0)
1478 goto done;
1479 }
1480
1481 done:
1482 if (remove_pivot_tmp)
1483 (void) rmdir(pivot_tmp);
1484
1485 return r;
1486 }