]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/nspawn/nspawn-mount.c
Merge pull request #2944 from keszybz/man-coredump-sysctl
[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 "cgroup-util.h"
25 #include "escape.h"
26 #include "fs-util.h"
27 #include "label.h"
28 #include "mkdir.h"
29 #include "mount-util.h"
30 #include "nspawn-mount.h"
31 #include "parse-util.h"
32 #include "path-util.h"
33 #include "rm-rf.h"
34 #include "set.h"
35 #include "stat-util.h"
36 #include "string-util.h"
37 #include "strv.h"
38 #include "user-util.h"
39 #include "util.h"
40
41 CustomMount* custom_mount_add(CustomMount **l, unsigned *n, CustomMountType t) {
42 CustomMount *c, *ret;
43
44 assert(l);
45 assert(n);
46 assert(t >= 0);
47 assert(t < _CUSTOM_MOUNT_TYPE_MAX);
48
49 c = realloc(*l, (*n + 1) * sizeof(CustomMount));
50 if (!c)
51 return NULL;
52
53 *l = c;
54 ret = *l + *n;
55 (*n)++;
56
57 *ret = (CustomMount) { .type = t };
58
59 return ret;
60 }
61
62 void custom_mount_free_all(CustomMount *l, unsigned n) {
63 unsigned i;
64
65 for (i = 0; i < n; i++) {
66 CustomMount *m = l + i;
67
68 free(m->source);
69 free(m->destination);
70 free(m->options);
71
72 if (m->work_dir) {
73 (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL);
74 free(m->work_dir);
75 }
76
77 strv_free(m->lower);
78 }
79
80 free(l);
81 }
82
83 int custom_mount_compare(const void *a, const void *b) {
84 const CustomMount *x = a, *y = b;
85 int r;
86
87 r = path_compare(x->destination, y->destination);
88 if (r != 0)
89 return r;
90
91 if (x->type < y->type)
92 return -1;
93 if (x->type > y->type)
94 return 1;
95
96 return 0;
97 }
98
99 int bind_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_only) {
100 _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL;
101 const char *p = s;
102 CustomMount *m;
103 int r;
104
105 assert(l);
106 assert(n);
107
108 r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL);
109 if (r < 0)
110 return r;
111 if (r == 0)
112 return -EINVAL;
113
114 if (r == 1) {
115 destination = strdup(source);
116 if (!destination)
117 return -ENOMEM;
118 }
119
120 if (r == 2 && !isempty(p)) {
121 opts = strdup(p);
122 if (!opts)
123 return -ENOMEM;
124 }
125
126 if (!path_is_absolute(source))
127 return -EINVAL;
128
129 if (!path_is_absolute(destination))
130 return -EINVAL;
131
132 m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND);
133 if (!m)
134 return log_oom();
135
136 m->source = source;
137 m->destination = destination;
138 m->read_only = read_only;
139 m->options = opts;
140
141 source = destination = opts = NULL;
142 return 0;
143 }
144
145 int tmpfs_mount_parse(CustomMount **l, unsigned *n, const char *s) {
146 _cleanup_free_ char *path = NULL, *opts = NULL;
147 const char *p = s;
148 CustomMount *m;
149 int r;
150
151 assert(l);
152 assert(n);
153 assert(s);
154
155 r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
156 if (r < 0)
157 return r;
158 if (r == 0)
159 return -EINVAL;
160
161 if (isempty(p))
162 opts = strdup("mode=0755");
163 else
164 opts = strdup(p);
165 if (!opts)
166 return -ENOMEM;
167
168 if (!path_is_absolute(path))
169 return -EINVAL;
170
171 m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS);
172 if (!m)
173 return -ENOMEM;
174
175 m->destination = path;
176 m->options = opts;
177
178 path = opts = NULL;
179 return 0;
180 }
181
182 static int tmpfs_patch_options(
183 const char *options,
184 bool userns, uid_t uid_shift, uid_t uid_range,
185 const char *selinux_apifs_context,
186 char **ret) {
187
188 char *buf = NULL;
189
190 if (userns && uid_shift != 0) {
191 assert(uid_shift != UID_INVALID);
192
193 if (options)
194 (void) asprintf(&buf, "%s,uid=" UID_FMT ",gid=" UID_FMT, options, uid_shift, uid_shift);
195 else
196 (void) asprintf(&buf, "uid=" UID_FMT ",gid=" UID_FMT, uid_shift, uid_shift);
197 if (!buf)
198 return -ENOMEM;
199
200 options = buf;
201 }
202
203 #ifdef HAVE_SELINUX
204 if (selinux_apifs_context) {
205 char *t;
206
207 if (options)
208 t = strjoin(options, ",context=\"", selinux_apifs_context, "\"", NULL);
209 else
210 t = strjoin("context=\"", selinux_apifs_context, "\"", NULL);
211 if (!t) {
212 free(buf);
213 return -ENOMEM;
214 }
215
216 free(buf);
217 buf = t;
218 }
219 #endif
220
221 *ret = buf;
222 return !!buf;
223 }
224
225 int mount_sysfs(const char *dest) {
226 const char *full, *top, *x;
227 int r;
228
229 top = prefix_roota(dest, "/sys");
230 r = path_check_fstype(top, SYSFS_MAGIC);
231 if (r < 0)
232 return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top);
233 /* /sys might already be mounted as sysfs by the outer child in the
234 * !netns case. In this case, it's all good. Don't touch it because we
235 * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555.
236 */
237 if (r > 0)
238 return 0;
239
240 full = prefix_roota(top, "/full");
241
242 (void) mkdir(full, 0755);
243
244 if (mount("sysfs", full, "sysfs", MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) < 0)
245 return log_error_errno(errno, "Failed to mount sysfs to %s: %m", full);
246
247 FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
248 _cleanup_free_ char *from = NULL, *to = NULL;
249
250 from = prefix_root(full, x);
251 if (!from)
252 return log_oom();
253
254 to = prefix_root(top, x);
255 if (!to)
256 return log_oom();
257
258 (void) mkdir(to, 0755);
259
260 if (mount(from, to, NULL, MS_BIND, NULL) < 0)
261 return log_error_errno(errno, "Failed to mount /sys/%s into place: %m", x);
262
263 if (mount(NULL, to, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL) < 0)
264 return log_error_errno(errno, "Failed to mount /sys/%s read-only: %m", x);
265 }
266
267 if (umount(full) < 0)
268 return log_error_errno(errno, "Failed to unmount %s: %m", full);
269
270 if (rmdir(full) < 0)
271 return log_error_errno(errno, "Failed to remove %s: %m", full);
272
273 x = prefix_roota(top, "/fs/kdbus");
274 (void) mkdir(x, 0755);
275
276 if (mount(NULL, top, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, NULL) < 0)
277 return log_error_errno(errno, "Failed to make %s read-only: %m", top);
278
279 return 0;
280 }
281
282 int mount_all(const char *dest,
283 bool use_userns, bool in_userns,
284 bool use_netns,
285 uid_t uid_shift, uid_t uid_range,
286 const char *selinux_apifs_context) {
287
288 typedef struct MountPoint {
289 const char *what;
290 const char *where;
291 const char *type;
292 const char *options;
293 unsigned long flags;
294 bool fatal;
295 bool in_userns;
296 bool use_netns;
297 } MountPoint;
298
299 static const MountPoint mount_table[] = {
300 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true, true, false },
301 { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND, true, true, false }, /* Bind mount first */
302 { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, true, true, false }, /* Then, make it r/o */
303 { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, true, false, true },
304 { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, true, false, false },
305 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME, true, false, false },
306 { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, true, false, false },
307 { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME, true, false, false },
308 { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_STRICTATIME, true, false, false },
309 #ifdef HAVE_SELINUX
310 { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, false, false, false }, /* Bind mount first */
311 { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, false, false, false }, /* Then, make it r/o */
312 #endif
313 };
314
315 unsigned k;
316 int r;
317
318 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
319 _cleanup_free_ char *where = NULL, *options = NULL;
320 const char *o;
321
322 if (in_userns != mount_table[k].in_userns)
323 continue;
324
325 if (!use_netns && mount_table[k].use_netns)
326 continue;
327
328 where = prefix_root(dest, mount_table[k].where);
329 if (!where)
330 return log_oom();
331
332 r = path_is_mount_point(where, AT_SYMLINK_FOLLOW);
333 if (r < 0 && r != -ENOENT)
334 return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where);
335
336 /* Skip this entry if it is not a remount. */
337 if (mount_table[k].what && r > 0)
338 continue;
339
340 r = mkdir_p(where, 0755);
341 if (r < 0) {
342 if (mount_table[k].fatal)
343 return log_error_errno(r, "Failed to create directory %s: %m", where);
344
345 log_warning_errno(r, "Failed to create directory %s: %m", where);
346 continue;
347 }
348
349 o = mount_table[k].options;
350 if (streq_ptr(mount_table[k].type, "tmpfs")) {
351 r = tmpfs_patch_options(o, use_userns, uid_shift, uid_range, selinux_apifs_context, &options);
352 if (r < 0)
353 return log_oom();
354 if (r > 0)
355 o = options;
356 }
357
358 if (mount(mount_table[k].what,
359 where,
360 mount_table[k].type,
361 mount_table[k].flags,
362 o) < 0) {
363
364 if (mount_table[k].fatal)
365 return log_error_errno(errno, "mount(%s) failed: %m", where);
366
367 log_warning_errno(errno, "mount(%s) failed, ignoring: %m", where);
368 }
369 }
370
371 return 0;
372 }
373
374 static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts) {
375 const char *p = options;
376 unsigned long flags = *mount_flags;
377 char *opts = NULL;
378
379 assert(options);
380
381 for (;;) {
382 _cleanup_free_ char *word = NULL;
383 int r = extract_first_word(&p, &word, ",", 0);
384 if (r < 0)
385 return log_error_errno(r, "Failed to extract mount option: %m");
386 if (r == 0)
387 break;
388
389 if (streq(word, "rbind"))
390 flags |= MS_REC;
391 else if (streq(word, "norbind"))
392 flags &= ~MS_REC;
393 else {
394 log_error("Invalid bind mount option: %s", word);
395 return -EINVAL;
396 }
397 }
398
399 *mount_flags = flags;
400 /* in the future mount_opts will hold string options for mount(2) */
401 *mount_opts = opts;
402
403 return 0;
404 }
405
406 static int mount_bind(const char *dest, CustomMount *m) {
407 struct stat source_st, dest_st;
408 const char *where;
409 unsigned long mount_flags = MS_BIND | MS_REC;
410 _cleanup_free_ char *mount_opts = NULL;
411 int r;
412
413 assert(m);
414
415 if (m->options) {
416 r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts);
417 if (r < 0)
418 return r;
419 }
420
421 if (stat(m->source, &source_st) < 0)
422 return log_error_errno(errno, "Failed to stat %s: %m", m->source);
423
424 where = prefix_roota(dest, m->destination);
425
426 if (stat(where, &dest_st) >= 0) {
427 if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode)) {
428 log_error("Cannot bind mount directory %s on file %s.", m->source, where);
429 return -EINVAL;
430 }
431
432 if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode)) {
433 log_error("Cannot bind mount file %s on directory %s.", m->source, where);
434 return -EINVAL;
435 }
436
437 } else if (errno == ENOENT) {
438 r = mkdir_parents_label(where, 0755);
439 if (r < 0)
440 return log_error_errno(r, "Failed to make parents of %s: %m", where);
441
442 /* Create the mount point. Any non-directory file can be
443 * mounted on any non-directory file (regular, fifo, socket,
444 * char, block).
445 */
446 if (S_ISDIR(source_st.st_mode))
447 r = mkdir_label(where, 0755);
448 else
449 r = touch(where);
450 if (r < 0)
451 return log_error_errno(r, "Failed to create mount point %s: %m", where);
452
453 } else {
454 return log_error_errno(errno, "Failed to stat %s: %m", where);
455 }
456
457 if (mount(m->source, where, NULL, mount_flags, mount_opts) < 0)
458 return log_error_errno(errno, "mount(%s) failed: %m", where);
459
460 if (m->read_only) {
461 r = bind_remount_recursive(where, true);
462 if (r < 0)
463 return log_error_errno(r, "Read-only bind mount failed: %m");
464 }
465
466 return 0;
467 }
468
469 static int mount_tmpfs(
470 const char *dest,
471 CustomMount *m,
472 bool userns, uid_t uid_shift, uid_t uid_range,
473 const char *selinux_apifs_context) {
474
475 const char *where, *options;
476 _cleanup_free_ char *buf = NULL;
477 int r;
478
479 assert(dest);
480 assert(m);
481
482 where = prefix_roota(dest, m->destination);
483
484 r = mkdir_p_label(where, 0755);
485 if (r < 0 && r != -EEXIST)
486 return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where);
487
488 r = tmpfs_patch_options(m->options, userns, uid_shift, uid_range, selinux_apifs_context, &buf);
489 if (r < 0)
490 return log_oom();
491 options = r > 0 ? buf : m->options;
492
493 if (mount("tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options) < 0)
494 return log_error_errno(errno, "tmpfs mount to %s failed: %m", where);
495
496 return 0;
497 }
498
499 static char *joined_and_escaped_lower_dirs(char * const *lower) {
500 _cleanup_strv_free_ char **sv = NULL;
501
502 sv = strv_copy(lower);
503 if (!sv)
504 return NULL;
505
506 strv_reverse(sv);
507
508 if (!strv_shell_escape(sv, ",:"))
509 return NULL;
510
511 return strv_join(sv, ":");
512 }
513
514 static int mount_overlay(const char *dest, CustomMount *m) {
515 _cleanup_free_ char *lower = NULL;
516 const char *where, *options;
517 int r;
518
519 assert(dest);
520 assert(m);
521
522 where = prefix_roota(dest, m->destination);
523
524 r = mkdir_label(where, 0755);
525 if (r < 0 && r != -EEXIST)
526 return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where);
527
528 (void) mkdir_p_label(m->source, 0755);
529
530 lower = joined_and_escaped_lower_dirs(m->lower);
531 if (!lower)
532 return log_oom();
533
534 if (m->read_only) {
535 _cleanup_free_ char *escaped_source = NULL;
536
537 escaped_source = shell_escape(m->source, ",:");
538 if (!escaped_source)
539 return log_oom();
540
541 options = strjoina("lowerdir=", escaped_source, ":", lower);
542 } else {
543 _cleanup_free_ char *escaped_source = NULL, *escaped_work_dir = NULL;
544
545 assert(m->work_dir);
546 (void) mkdir_label(m->work_dir, 0700);
547
548 escaped_source = shell_escape(m->source, ",:");
549 if (!escaped_source)
550 return log_oom();
551 escaped_work_dir = shell_escape(m->work_dir, ",:");
552 if (!escaped_work_dir)
553 return log_oom();
554
555 options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir);
556 }
557
558 if (mount("overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options) < 0)
559 return log_error_errno(errno, "overlay mount to %s failed: %m", where);
560
561 return 0;
562 }
563
564 int mount_custom(
565 const char *dest,
566 CustomMount *mounts, unsigned n,
567 bool userns, uid_t uid_shift, uid_t uid_range,
568 const char *selinux_apifs_context) {
569
570 unsigned i;
571 int r;
572
573 assert(dest);
574
575 for (i = 0; i < n; i++) {
576 CustomMount *m = mounts + i;
577
578 switch (m->type) {
579
580 case CUSTOM_MOUNT_BIND:
581 r = mount_bind(dest, m);
582 break;
583
584 case CUSTOM_MOUNT_TMPFS:
585 r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context);
586 break;
587
588 case CUSTOM_MOUNT_OVERLAY:
589 r = mount_overlay(dest, m);
590 break;
591
592 default:
593 assert_not_reached("Unknown custom mount type");
594 }
595
596 if (r < 0)
597 return r;
598 }
599
600 return 0;
601 }
602
603 static int mount_legacy_cgroup_hierarchy(const char *dest, const char *controller, const char *hierarchy, bool read_only) {
604 char *to;
605 int r;
606
607 to = strjoina(strempty(dest), "/sys/fs/cgroup/", hierarchy);
608
609 r = path_is_mount_point(to, 0);
610 if (r < 0 && r != -ENOENT)
611 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", to);
612 if (r > 0)
613 return 0;
614
615 mkdir_p(to, 0755);
616
617 /* The superblock mount options of the mount point need to be
618 * identical to the hosts', and hence writable... */
619 if (mount("cgroup", to, "cgroup", MS_NOSUID|MS_NOEXEC|MS_NODEV, controller) < 0)
620 return log_error_errno(errno, "Failed to mount to %s: %m", to);
621
622 /* ... hence let's only make the bind mount read-only, not the
623 * superblock. */
624 if (read_only) {
625 if (mount(NULL, to, NULL, MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL) < 0)
626 return log_error_errno(errno, "Failed to remount %s read-only: %m", to);
627 }
628 return 1;
629 }
630
631 static int mount_legacy_cgroups(
632 const char *dest,
633 bool userns, uid_t uid_shift, uid_t uid_range,
634 const char *selinux_apifs_context) {
635
636 _cleanup_set_free_free_ Set *controllers = NULL;
637 const char *cgroup_root;
638 int r;
639
640 cgroup_root = prefix_roota(dest, "/sys/fs/cgroup");
641
642 (void) mkdir_p(cgroup_root, 0755);
643
644 /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */
645 r = path_is_mount_point(cgroup_root, AT_SYMLINK_FOLLOW);
646 if (r < 0)
647 return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m");
648 if (r == 0) {
649 _cleanup_free_ char *options = NULL;
650
651 r = tmpfs_patch_options("mode=755", userns, uid_shift, uid_range, selinux_apifs_context, &options);
652 if (r < 0)
653 return log_oom();
654
655 if (mount("tmpfs", cgroup_root, "tmpfs", MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options) < 0)
656 return log_error_errno(errno, "Failed to mount /sys/fs/cgroup: %m");
657 }
658
659 if (cg_unified() > 0)
660 goto skip_controllers;
661
662 controllers = set_new(&string_hash_ops);
663 if (!controllers)
664 return log_oom();
665
666 r = cg_kernel_controllers(controllers);
667 if (r < 0)
668 return log_error_errno(r, "Failed to determine cgroup controllers: %m");
669
670 for (;;) {
671 _cleanup_free_ char *controller = NULL, *origin = NULL, *combined = NULL;
672
673 controller = set_steal_first(controllers);
674 if (!controller)
675 break;
676
677 origin = prefix_root("/sys/fs/cgroup/", controller);
678 if (!origin)
679 return log_oom();
680
681 r = readlink_malloc(origin, &combined);
682 if (r == -EINVAL) {
683 /* Not a symbolic link, but directly a single cgroup hierarchy */
684
685 r = mount_legacy_cgroup_hierarchy(dest, controller, controller, true);
686 if (r < 0)
687 return r;
688
689 } else if (r < 0)
690 return log_error_errno(r, "Failed to read link %s: %m", origin);
691 else {
692 _cleanup_free_ char *target = NULL;
693
694 target = prefix_root(dest, origin);
695 if (!target)
696 return log_oom();
697
698 /* A symbolic link, a combination of controllers in one hierarchy */
699
700 if (!filename_is_valid(combined)) {
701 log_warning("Ignoring invalid combined hierarchy %s.", combined);
702 continue;
703 }
704
705 r = mount_legacy_cgroup_hierarchy(dest, combined, combined, true);
706 if (r < 0)
707 return r;
708
709 r = symlink_idempotent(combined, target);
710 if (r == -EINVAL) {
711 log_error("Invalid existing symlink for combined hierarchy");
712 return r;
713 }
714 if (r < 0)
715 return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m");
716 }
717 }
718
719 skip_controllers:
720 r = mount_legacy_cgroup_hierarchy(dest, "none,name=systemd,xattr", "systemd", false);
721 if (r < 0)
722 return r;
723
724 if (mount(NULL, cgroup_root, NULL, MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755") < 0)
725 return log_error_errno(errno, "Failed to remount %s read-only: %m", cgroup_root);
726
727 return 0;
728 }
729
730 static int mount_unified_cgroups(const char *dest) {
731 const char *p;
732 int r;
733
734 assert(dest);
735
736 p = prefix_roota(dest, "/sys/fs/cgroup");
737
738 (void) mkdir_p(p, 0755);
739
740 r = path_is_mount_point(p, AT_SYMLINK_FOLLOW);
741 if (r < 0)
742 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p);
743 if (r > 0) {
744 p = prefix_roota(dest, "/sys/fs/cgroup/cgroup.procs");
745 if (access(p, F_OK) >= 0)
746 return 0;
747 if (errno != ENOENT)
748 return log_error_errno(errno, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p);
749
750 log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p);
751 return -EINVAL;
752 }
753
754 if (mount("cgroup", p, "cgroup2", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL) < 0)
755 return log_error_errno(errno, "Failed to mount unified cgroup hierarchy to %s: %m", p);
756
757 return 0;
758 }
759
760 int mount_cgroups(
761 const char *dest,
762 bool unified_requested,
763 bool userns, uid_t uid_shift, uid_t uid_range,
764 const char *selinux_apifs_context) {
765
766 if (unified_requested)
767 return mount_unified_cgroups(dest);
768 else
769 return mount_legacy_cgroups(dest, userns, uid_shift, uid_range, selinux_apifs_context);
770 }
771
772 int mount_systemd_cgroup_writable(
773 const char *dest,
774 bool unified_requested) {
775
776 _cleanup_free_ char *own_cgroup_path = NULL;
777 const char *systemd_root, *systemd_own;
778 int r;
779
780 assert(dest);
781
782 r = cg_pid_get_path(NULL, 0, &own_cgroup_path);
783 if (r < 0)
784 return log_error_errno(r, "Failed to determine our own cgroup path: %m");
785
786 /* If we are living in the top-level, then there's nothing to do... */
787 if (path_equal(own_cgroup_path, "/"))
788 return 0;
789
790 if (unified_requested) {
791 systemd_own = strjoina(dest, "/sys/fs/cgroup", own_cgroup_path);
792 systemd_root = prefix_roota(dest, "/sys/fs/cgroup");
793 } else {
794 systemd_own = strjoina(dest, "/sys/fs/cgroup/systemd", own_cgroup_path);
795 systemd_root = prefix_roota(dest, "/sys/fs/cgroup/systemd");
796 }
797
798 /* Make our own cgroup a (writable) bind mount */
799 if (mount(systemd_own, systemd_own, NULL, MS_BIND, NULL) < 0)
800 return log_error_errno(errno, "Failed to turn %s into a bind mount: %m", own_cgroup_path);
801
802 /* And then remount the systemd cgroup root read-only */
803 if (mount(NULL, systemd_root, NULL, MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL) < 0)
804 return log_error_errno(errno, "Failed to mount cgroup root read-only: %m");
805
806 return 0;
807 }
808
809 int setup_volatile_state(
810 const char *directory,
811 VolatileMode mode,
812 bool userns, uid_t uid_shift, uid_t uid_range,
813 const char *selinux_apifs_context) {
814
815 _cleanup_free_ char *buf = NULL;
816 const char *p, *options;
817 int r;
818
819 assert(directory);
820
821 if (mode != VOLATILE_STATE)
822 return 0;
823
824 /* --volatile=state means we simply overmount /var
825 with a tmpfs, and the rest read-only. */
826
827 r = bind_remount_recursive(directory, true);
828 if (r < 0)
829 return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
830
831 p = prefix_roota(directory, "/var");
832 r = mkdir(p, 0755);
833 if (r < 0 && errno != EEXIST)
834 return log_error_errno(errno, "Failed to create %s: %m", directory);
835
836 options = "mode=755";
837 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, selinux_apifs_context, &buf);
838 if (r < 0)
839 return log_oom();
840 if (r > 0)
841 options = buf;
842
843 if (mount("tmpfs", p, "tmpfs", MS_STRICTATIME, options) < 0)
844 return log_error_errno(errno, "Failed to mount tmpfs to /var: %m");
845
846 return 0;
847 }
848
849 int setup_volatile(
850 const char *directory,
851 VolatileMode mode,
852 bool userns, uid_t uid_shift, uid_t uid_range,
853 const char *selinux_apifs_context) {
854
855 bool tmpfs_mounted = false, bind_mounted = false;
856 char template[] = "/tmp/nspawn-volatile-XXXXXX";
857 _cleanup_free_ char *buf = NULL;
858 const char *f, *t, *options;
859 int r;
860
861 assert(directory);
862
863 if (mode != VOLATILE_YES)
864 return 0;
865
866 /* --volatile=yes means we mount a tmpfs to the root dir, and
867 the original /usr to use inside it, and that read-only. */
868
869 if (!mkdtemp(template))
870 return log_error_errno(errno, "Failed to create temporary directory: %m");
871
872 options = "mode=755";
873 r = tmpfs_patch_options(options, userns, uid_shift, uid_range, selinux_apifs_context, &buf);
874 if (r < 0)
875 return log_oom();
876 if (r > 0)
877 options = buf;
878
879 if (mount("tmpfs", template, "tmpfs", MS_STRICTATIME, options) < 0) {
880 r = log_error_errno(errno, "Failed to mount tmpfs for root directory: %m");
881 goto fail;
882 }
883
884 tmpfs_mounted = true;
885
886 f = prefix_roota(directory, "/usr");
887 t = prefix_roota(template, "/usr");
888
889 r = mkdir(t, 0755);
890 if (r < 0 && errno != EEXIST) {
891 r = log_error_errno(errno, "Failed to create %s: %m", t);
892 goto fail;
893 }
894
895 if (mount(f, t, NULL, MS_BIND|MS_REC, NULL) < 0) {
896 r = log_error_errno(errno, "Failed to create /usr bind mount: %m");
897 goto fail;
898 }
899
900 bind_mounted = true;
901
902 r = bind_remount_recursive(t, true);
903 if (r < 0) {
904 log_error_errno(r, "Failed to remount %s read-only: %m", t);
905 goto fail;
906 }
907
908 if (mount(template, directory, NULL, MS_MOVE, NULL) < 0) {
909 r = log_error_errno(errno, "Failed to move root mount: %m");
910 goto fail;
911 }
912
913 (void) rmdir(template);
914
915 return 0;
916
917 fail:
918 if (bind_mounted)
919 (void) umount(t);
920
921 if (tmpfs_mounted)
922 (void) umount(template);
923 (void) rmdir(template);
924 return r;
925 }
926
927 VolatileMode volatile_mode_from_string(const char *s) {
928 int b;
929
930 if (isempty(s))
931 return _VOLATILE_MODE_INVALID;
932
933 b = parse_boolean(s);
934 if (b > 0)
935 return VOLATILE_YES;
936 if (b == 0)
937 return VOLATILE_NO;
938
939 if (streq(s, "state"))
940 return VOLATILE_STATE;
941
942 return _VOLATILE_MODE_INVALID;
943 }