]>
Commit | Line | Data |
---|---|---|
e83bebef LP |
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 | ||
4f5dd394 | 20 | #include <sys/mount.h> |
07630cea | 21 | #include <linux/magic.h> |
e83bebef | 22 | |
b5efdb8a | 23 | #include "alloc-util.h" |
4f5dd394 | 24 | #include "escape.h" |
0996ef00 CB |
25 | #include "fd-util.h" |
26 | #include "fileio.h" | |
f4f15635 | 27 | #include "fs-util.h" |
e83bebef | 28 | #include "label.h" |
4f5dd394 | 29 | #include "mkdir.h" |
4349cd7c | 30 | #include "mount-util.h" |
6bedfcbb LP |
31 | #include "nspawn-mount.h" |
32 | #include "parse-util.h" | |
4f5dd394 LP |
33 | #include "path-util.h" |
34 | #include "rm-rf.h" | |
e83bebef | 35 | #include "set.h" |
8fcde012 | 36 | #include "stat-util.h" |
07630cea | 37 | #include "string-util.h" |
4f5dd394 | 38 | #include "strv.h" |
ee104e11 | 39 | #include "user-util.h" |
4f5dd394 | 40 | #include "util.h" |
e83bebef LP |
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(*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 | strv_free(m->lower); | |
79 | } | |
80 | ||
81 | free(l); | |
82 | } | |
83 | ||
84 | int custom_mount_compare(const void *a, const void *b) { | |
85 | const CustomMount *x = a, *y = b; | |
86 | int r; | |
87 | ||
88 | r = path_compare(x->destination, y->destination); | |
89 | if (r != 0) | |
90 | return r; | |
91 | ||
92 | if (x->type < y->type) | |
93 | return -1; | |
94 | if (x->type > y->type) | |
95 | return 1; | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | int bind_mount_parse(CustomMount **l, unsigned *n, const char *s, bool read_only) { | |
101 | _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL; | |
102 | const char *p = s; | |
103 | CustomMount *m; | |
104 | int r; | |
105 | ||
106 | assert(l); | |
107 | assert(n); | |
108 | ||
109 | r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL); | |
110 | if (r < 0) | |
111 | return r; | |
112 | if (r == 0) | |
113 | return -EINVAL; | |
114 | ||
115 | if (r == 1) { | |
116 | destination = strdup(source); | |
117 | if (!destination) | |
118 | return -ENOMEM; | |
119 | } | |
120 | ||
121 | if (r == 2 && !isempty(p)) { | |
122 | opts = strdup(p); | |
123 | if (!opts) | |
124 | return -ENOMEM; | |
125 | } | |
126 | ||
127 | if (!path_is_absolute(source)) | |
128 | return -EINVAL; | |
129 | ||
130 | if (!path_is_absolute(destination)) | |
131 | return -EINVAL; | |
132 | ||
133 | m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND); | |
134 | if (!m) | |
135 | return log_oom(); | |
136 | ||
137 | m->source = source; | |
138 | m->destination = destination; | |
139 | m->read_only = read_only; | |
140 | m->options = opts; | |
141 | ||
142 | source = destination = opts = NULL; | |
143 | return 0; | |
144 | } | |
145 | ||
146 | int tmpfs_mount_parse(CustomMount **l, unsigned *n, const char *s) { | |
147 | _cleanup_free_ char *path = NULL, *opts = NULL; | |
148 | const char *p = s; | |
149 | CustomMount *m; | |
150 | int r; | |
151 | ||
152 | assert(l); | |
153 | assert(n); | |
154 | assert(s); | |
155 | ||
156 | r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
157 | if (r < 0) | |
158 | return r; | |
159 | if (r == 0) | |
160 | return -EINVAL; | |
161 | ||
162 | if (isempty(p)) | |
163 | opts = strdup("mode=0755"); | |
164 | else | |
165 | opts = strdup(p); | |
166 | if (!opts) | |
167 | return -ENOMEM; | |
168 | ||
169 | if (!path_is_absolute(path)) | |
170 | return -EINVAL; | |
171 | ||
172 | m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS); | |
173 | if (!m) | |
174 | return -ENOMEM; | |
175 | ||
176 | m->destination = path; | |
177 | m->options = opts; | |
178 | ||
179 | path = opts = NULL; | |
180 | return 0; | |
181 | } | |
182 | ||
183 | static int tmpfs_patch_options( | |
184 | const char *options, | |
0996ef00 CB |
185 | bool userns, |
186 | uid_t uid_shift, uid_t uid_range, | |
187 | bool patch_ids, | |
e83bebef LP |
188 | const char *selinux_apifs_context, |
189 | char **ret) { | |
190 | ||
191 | char *buf = NULL; | |
192 | ||
0996ef00 | 193 | if ((userns && uid_shift != 0) || patch_ids) { |
e83bebef LP |
194 | assert(uid_shift != UID_INVALID); |
195 | ||
9aa2169e ZJS |
196 | if (asprintf(&buf, "%s%suid=" UID_FMT ",gid=" UID_FMT, |
197 | options ?: "", options ? "," : "", | |
198 | uid_shift, uid_shift) < 0) | |
e83bebef LP |
199 | return -ENOMEM; |
200 | ||
201 | options = buf; | |
202 | } | |
203 | ||
204 | #ifdef HAVE_SELINUX | |
205 | if (selinux_apifs_context) { | |
206 | char *t; | |
207 | ||
9aa2169e ZJS |
208 | t = strjoin(options ?: "", options ? "," : "", |
209 | "context=\"", selinux_apifs_context, "\""); | |
210 | free(buf); | |
211 | if (!t) | |
e83bebef | 212 | return -ENOMEM; |
e83bebef | 213 | |
e83bebef LP |
214 | buf = t; |
215 | } | |
216 | #endif | |
217 | ||
0996ef00 CB |
218 | if (!buf && options) { |
219 | buf = strdup(options); | |
220 | if (!buf) | |
221 | return -ENOMEM; | |
222 | } | |
e83bebef | 223 | *ret = buf; |
0996ef00 | 224 | |
e83bebef LP |
225 | return !!buf; |
226 | } | |
227 | ||
4f086aab | 228 | int mount_sysfs(const char *dest, MountSettingsMask mount_settings) { |
d8fc6a00 | 229 | const char *full, *top, *x; |
d1678248 | 230 | int r; |
4f086aab | 231 | unsigned long extra_flags = 0; |
d8fc6a00 LP |
232 | |
233 | top = prefix_roota(dest, "/sys"); | |
d1678248 ILG |
234 | r = path_check_fstype(top, SYSFS_MAGIC); |
235 | if (r < 0) | |
236 | return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top); | |
237 | /* /sys might already be mounted as sysfs by the outer child in the | |
238 | * !netns case. In this case, it's all good. Don't touch it because we | |
239 | * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555. | |
240 | */ | |
241 | if (r > 0) | |
242 | return 0; | |
243 | ||
d8fc6a00 LP |
244 | full = prefix_roota(top, "/full"); |
245 | ||
246 | (void) mkdir(full, 0755); | |
247 | ||
4f086aab SU |
248 | if (mount_settings & MOUNT_APPLY_APIVFS_RO) |
249 | extra_flags |= MS_RDONLY; | |
250 | ||
60e76d48 | 251 | r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs", |
4f086aab | 252 | MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL); |
60e76d48 ZJS |
253 | if (r < 0) |
254 | return r; | |
d8fc6a00 LP |
255 | |
256 | FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") { | |
257 | _cleanup_free_ char *from = NULL, *to = NULL; | |
258 | ||
259 | from = prefix_root(full, x); | |
260 | if (!from) | |
261 | return log_oom(); | |
262 | ||
263 | to = prefix_root(top, x); | |
264 | if (!to) | |
265 | return log_oom(); | |
266 | ||
267 | (void) mkdir(to, 0755); | |
268 | ||
60e76d48 ZJS |
269 | r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL); |
270 | if (r < 0) | |
271 | return r; | |
d8fc6a00 | 272 | |
60e76d48 | 273 | r = mount_verbose(LOG_ERR, NULL, to, NULL, |
4f086aab | 274 | MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL); |
60e76d48 ZJS |
275 | if (r < 0) |
276 | return r; | |
d8fc6a00 LP |
277 | } |
278 | ||
60e76d48 ZJS |
279 | r = umount_verbose(full); |
280 | if (r < 0) | |
281 | return r; | |
d8fc6a00 LP |
282 | |
283 | if (rmdir(full) < 0) | |
284 | return log_error_errno(errno, "Failed to remove %s: %m", full); | |
285 | ||
286 | x = prefix_roota(top, "/fs/kdbus"); | |
0996ef00 CB |
287 | (void) mkdir_p(x, 0755); |
288 | ||
289 | /* Create mountpoint for cgroups. Otherwise we are not allowed since we | |
290 | * remount /sys read-only. | |
291 | */ | |
292 | if (cg_ns_supported()) { | |
293 | x = prefix_roota(top, "/fs/cgroup"); | |
294 | (void) mkdir_p(x, 0755); | |
295 | } | |
d8fc6a00 | 296 | |
60e76d48 | 297 | return mount_verbose(LOG_ERR, NULL, top, NULL, |
4f086aab | 298 | MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL); |
d8fc6a00 LP |
299 | } |
300 | ||
63eae723 EV |
301 | static int mkdir_userns(const char *path, mode_t mode, bool in_userns, uid_t uid_shift) { |
302 | int r; | |
303 | ||
304 | assert(path); | |
305 | ||
306 | r = mkdir(path, mode); | |
307 | if (r < 0 && errno != EEXIST) | |
308 | return -errno; | |
309 | ||
310 | if (!in_userns) { | |
311 | r = lchown(path, uid_shift, uid_shift); | |
312 | if (r < 0) | |
313 | return -errno; | |
314 | } | |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
319 | static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, bool in_userns, uid_t uid_shift) { | |
320 | const char *p, *e; | |
321 | int r; | |
322 | ||
323 | assert(path); | |
324 | ||
325 | if (prefix && !path_startswith(path, prefix)) | |
326 | return -ENOTDIR; | |
327 | ||
328 | /* create every parent directory in the path, except the last component */ | |
329 | p = path + strspn(path, "/"); | |
330 | for (;;) { | |
331 | char t[strlen(path) + 1]; | |
332 | ||
333 | e = p + strcspn(p, "/"); | |
334 | p = e + strspn(e, "/"); | |
335 | ||
336 | /* Is this the last component? If so, then we're done */ | |
337 | if (*p == 0) | |
338 | break; | |
339 | ||
340 | memcpy(t, path, e - path); | |
341 | t[e-path] = 0; | |
342 | ||
343 | if (prefix && path_startswith(prefix, t)) | |
344 | continue; | |
345 | ||
346 | r = mkdir_userns(t, mode, in_userns, uid_shift); | |
347 | if (r < 0) | |
348 | return r; | |
349 | } | |
350 | ||
351 | return mkdir_userns(path, mode, in_userns, uid_shift); | |
352 | } | |
353 | ||
e83bebef | 354 | int mount_all(const char *dest, |
4f086aab | 355 | MountSettingsMask mount_settings, |
403af78c | 356 | uid_t uid_shift, uid_t uid_range, |
e83bebef LP |
357 | const char *selinux_apifs_context) { |
358 | ||
359 | typedef struct MountPoint { | |
360 | const char *what; | |
361 | const char *where; | |
362 | const char *type; | |
363 | const char *options; | |
364 | unsigned long flags; | |
4f086aab | 365 | MountSettingsMask mount_settings; |
e83bebef LP |
366 | } MountPoint; |
367 | ||
368 | static const MountPoint mount_table[] = { | |
4f086aab SU |
369 | /* inner child mounts */ |
370 | { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_IN_USERNS }, | |
371 | { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND, MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ...*/ | |
372 | { "/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) */ | |
373 | { 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 */ | |
374 | { "/proc/sysrq-trigger", "/proc/sysrq-trigger", NULL, NULL, MS_BIND, MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ...*/ | |
375 | { 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 */ | |
376 | { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_STRICTATIME, MOUNT_FATAL|MOUNT_IN_USERNS }, | |
377 | ||
378 | /* outer child mounts */ | |
379 | { "tmpfs", "/sys", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS }, | |
380 | { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO }, /* skipped if above was mounted */ | |
381 | { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, MOUNT_FATAL }, /* skipped if above was mounted */ | |
382 | ||
383 | { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME, MOUNT_FATAL }, | |
384 | { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL }, | |
385 | { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME, MOUNT_FATAL }, | |
e83bebef | 386 | #ifdef HAVE_SELINUX |
4f086aab SU |
387 | { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, 0 }, /* Bind mount first */ |
388 | { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, 0 }, /* Then, make it r/o */ | |
e83bebef LP |
389 | #endif |
390 | }; | |
391 | ||
392 | unsigned k; | |
393 | int r; | |
4f086aab SU |
394 | bool use_userns = (mount_settings & MOUNT_USE_USERNS); |
395 | bool netns = (mount_settings & MOUNT_APPLY_APIVFS_NETNS); | |
396 | bool ro = (mount_settings & MOUNT_APPLY_APIVFS_RO); | |
397 | bool in_userns = (mount_settings & MOUNT_IN_USERNS); | |
e83bebef LP |
398 | |
399 | for (k = 0; k < ELEMENTSOF(mount_table); k++) { | |
400 | _cleanup_free_ char *where = NULL, *options = NULL; | |
401 | const char *o; | |
4f086aab SU |
402 | bool fatal = (mount_table[k].mount_settings & MOUNT_FATAL); |
403 | ||
404 | if (in_userns != (bool)(mount_table[k].mount_settings & MOUNT_IN_USERNS)) | |
405 | continue; | |
e83bebef | 406 | |
4f086aab | 407 | if (!netns && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_NETNS)) |
d1678248 ILG |
408 | continue; |
409 | ||
4f086aab | 410 | if (!ro && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_RO)) |
e83bebef LP |
411 | continue; |
412 | ||
413 | where = prefix_root(dest, mount_table[k].where); | |
414 | if (!where) | |
415 | return log_oom(); | |
416 | ||
417 | r = path_is_mount_point(where, AT_SYMLINK_FOLLOW); | |
418 | if (r < 0 && r != -ENOENT) | |
419 | return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where); | |
420 | ||
421 | /* Skip this entry if it is not a remount. */ | |
422 | if (mount_table[k].what && r > 0) | |
423 | continue; | |
424 | ||
63eae723 | 425 | r = mkdir_userns_p(dest, where, 0755, in_userns, uid_shift); |
920a7899 | 426 | if (r < 0 && r != -EEXIST) { |
4f086aab | 427 | if (fatal) |
e83bebef LP |
428 | return log_error_errno(r, "Failed to create directory %s: %m", where); |
429 | ||
201b13c8 | 430 | log_debug_errno(r, "Failed to create directory %s: %m", where); |
e83bebef LP |
431 | continue; |
432 | } | |
433 | ||
434 | o = mount_table[k].options; | |
435 | if (streq_ptr(mount_table[k].type, "tmpfs")) { | |
8492849e EV |
436 | if (in_userns) |
437 | r = tmpfs_patch_options(o, use_userns, 0, uid_range, true, selinux_apifs_context, &options); | |
438 | else | |
439 | r = tmpfs_patch_options(o, use_userns, uid_shift, uid_range, false, selinux_apifs_context, &options); | |
e83bebef LP |
440 | if (r < 0) |
441 | return log_oom(); | |
442 | if (r > 0) | |
443 | o = options; | |
444 | } | |
445 | ||
4f086aab | 446 | r = mount_verbose(fatal ? LOG_ERR : LOG_DEBUG, |
60e76d48 ZJS |
447 | mount_table[k].what, |
448 | where, | |
449 | mount_table[k].type, | |
450 | mount_table[k].flags, | |
451 | o); | |
4f086aab | 452 | if (r < 0 && fatal) |
60e76d48 | 453 | return r; |
e83bebef LP |
454 | } |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
459 | static int parse_mount_bind_options(const char *options, unsigned long *mount_flags, char **mount_opts) { | |
460 | const char *p = options; | |
461 | unsigned long flags = *mount_flags; | |
462 | char *opts = NULL; | |
463 | ||
464 | assert(options); | |
465 | ||
466 | for (;;) { | |
467 | _cleanup_free_ char *word = NULL; | |
468 | int r = extract_first_word(&p, &word, ",", 0); | |
469 | if (r < 0) | |
470 | return log_error_errno(r, "Failed to extract mount option: %m"); | |
471 | if (r == 0) | |
472 | break; | |
473 | ||
474 | if (streq(word, "rbind")) | |
475 | flags |= MS_REC; | |
476 | else if (streq(word, "norbind")) | |
477 | flags &= ~MS_REC; | |
478 | else { | |
479 | log_error("Invalid bind mount option: %s", word); | |
480 | return -EINVAL; | |
481 | } | |
482 | } | |
483 | ||
484 | *mount_flags = flags; | |
485 | /* in the future mount_opts will hold string options for mount(2) */ | |
486 | *mount_opts = opts; | |
487 | ||
488 | return 0; | |
489 | } | |
490 | ||
491 | static int mount_bind(const char *dest, CustomMount *m) { | |
492 | struct stat source_st, dest_st; | |
493 | const char *where; | |
494 | unsigned long mount_flags = MS_BIND | MS_REC; | |
495 | _cleanup_free_ char *mount_opts = NULL; | |
496 | int r; | |
497 | ||
498 | assert(m); | |
499 | ||
500 | if (m->options) { | |
501 | r = parse_mount_bind_options(m->options, &mount_flags, &mount_opts); | |
502 | if (r < 0) | |
503 | return r; | |
504 | } | |
505 | ||
506 | if (stat(m->source, &source_st) < 0) | |
507 | return log_error_errno(errno, "Failed to stat %s: %m", m->source); | |
508 | ||
509 | where = prefix_roota(dest, m->destination); | |
510 | ||
511 | if (stat(where, &dest_st) >= 0) { | |
512 | if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode)) { | |
513 | log_error("Cannot bind mount directory %s on file %s.", m->source, where); | |
514 | return -EINVAL; | |
515 | } | |
516 | ||
517 | if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode)) { | |
518 | log_error("Cannot bind mount file %s on directory %s.", m->source, where); | |
519 | return -EINVAL; | |
520 | } | |
521 | ||
522 | } else if (errno == ENOENT) { | |
523 | r = mkdir_parents_label(where, 0755); | |
524 | if (r < 0) | |
525 | return log_error_errno(r, "Failed to make parents of %s: %m", where); | |
b97e83cb BN |
526 | |
527 | /* Create the mount point. Any non-directory file can be | |
528 | * mounted on any non-directory file (regular, fifo, socket, | |
529 | * char, block). | |
530 | */ | |
531 | if (S_ISDIR(source_st.st_mode)) | |
532 | r = mkdir_label(where, 0755); | |
533 | else | |
534 | r = touch(where); | |
535 | if (r < 0) | |
536 | return log_error_errno(r, "Failed to create mount point %s: %m", where); | |
537 | ||
60e76d48 | 538 | } else |
e1427b13 | 539 | return log_error_errno(errno, "Failed to stat %s: %m", where); |
e83bebef | 540 | |
60e76d48 ZJS |
541 | r = mount_verbose(LOG_ERR, m->source, where, NULL, mount_flags, mount_opts); |
542 | if (r < 0) | |
543 | return r; | |
e83bebef LP |
544 | |
545 | if (m->read_only) { | |
6b7c9f8b | 546 | r = bind_remount_recursive(where, true, NULL); |
e83bebef LP |
547 | if (r < 0) |
548 | return log_error_errno(r, "Read-only bind mount failed: %m"); | |
549 | } | |
550 | ||
551 | return 0; | |
552 | } | |
553 | ||
554 | static int mount_tmpfs( | |
555 | const char *dest, | |
556 | CustomMount *m, | |
557 | bool userns, uid_t uid_shift, uid_t uid_range, | |
558 | const char *selinux_apifs_context) { | |
559 | ||
560 | const char *where, *options; | |
561 | _cleanup_free_ char *buf = NULL; | |
562 | int r; | |
563 | ||
564 | assert(dest); | |
565 | assert(m); | |
566 | ||
567 | where = prefix_roota(dest, m->destination); | |
568 | ||
569 | r = mkdir_p_label(where, 0755); | |
570 | if (r < 0 && r != -EEXIST) | |
571 | return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where); | |
572 | ||
0996ef00 | 573 | r = tmpfs_patch_options(m->options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf); |
e83bebef LP |
574 | if (r < 0) |
575 | return log_oom(); | |
576 | options = r > 0 ? buf : m->options; | |
577 | ||
60e76d48 | 578 | return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options); |
e83bebef LP |
579 | } |
580 | ||
581 | static char *joined_and_escaped_lower_dirs(char * const *lower) { | |
582 | _cleanup_strv_free_ char **sv = NULL; | |
583 | ||
584 | sv = strv_copy(lower); | |
585 | if (!sv) | |
586 | return NULL; | |
587 | ||
588 | strv_reverse(sv); | |
589 | ||
590 | if (!strv_shell_escape(sv, ",:")) | |
591 | return NULL; | |
592 | ||
593 | return strv_join(sv, ":"); | |
594 | } | |
595 | ||
596 | static int mount_overlay(const char *dest, CustomMount *m) { | |
597 | _cleanup_free_ char *lower = NULL; | |
598 | const char *where, *options; | |
599 | int r; | |
600 | ||
601 | assert(dest); | |
602 | assert(m); | |
603 | ||
604 | where = prefix_roota(dest, m->destination); | |
605 | ||
606 | r = mkdir_label(where, 0755); | |
607 | if (r < 0 && r != -EEXIST) | |
608 | return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where); | |
609 | ||
610 | (void) mkdir_p_label(m->source, 0755); | |
611 | ||
612 | lower = joined_and_escaped_lower_dirs(m->lower); | |
613 | if (!lower) | |
614 | return log_oom(); | |
615 | ||
616 | if (m->read_only) { | |
617 | _cleanup_free_ char *escaped_source = NULL; | |
618 | ||
619 | escaped_source = shell_escape(m->source, ",:"); | |
620 | if (!escaped_source) | |
621 | return log_oom(); | |
622 | ||
623 | options = strjoina("lowerdir=", escaped_source, ":", lower); | |
624 | } else { | |
625 | _cleanup_free_ char *escaped_source = NULL, *escaped_work_dir = NULL; | |
626 | ||
627 | assert(m->work_dir); | |
628 | (void) mkdir_label(m->work_dir, 0700); | |
629 | ||
630 | escaped_source = shell_escape(m->source, ",:"); | |
631 | if (!escaped_source) | |
632 | return log_oom(); | |
633 | escaped_work_dir = shell_escape(m->work_dir, ",:"); | |
634 | if (!escaped_work_dir) | |
635 | return log_oom(); | |
636 | ||
637 | options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir); | |
638 | } | |
639 | ||
60e76d48 | 640 | return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options); |
e83bebef LP |
641 | } |
642 | ||
643 | int mount_custom( | |
644 | const char *dest, | |
645 | CustomMount *mounts, unsigned n, | |
646 | bool userns, uid_t uid_shift, uid_t uid_range, | |
647 | const char *selinux_apifs_context) { | |
648 | ||
649 | unsigned i; | |
650 | int r; | |
651 | ||
652 | assert(dest); | |
653 | ||
654 | for (i = 0; i < n; i++) { | |
655 | CustomMount *m = mounts + i; | |
656 | ||
657 | switch (m->type) { | |
658 | ||
659 | case CUSTOM_MOUNT_BIND: | |
660 | r = mount_bind(dest, m); | |
661 | break; | |
662 | ||
663 | case CUSTOM_MOUNT_TMPFS: | |
664 | r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context); | |
665 | break; | |
666 | ||
667 | case CUSTOM_MOUNT_OVERLAY: | |
668 | r = mount_overlay(dest, m); | |
669 | break; | |
670 | ||
671 | default: | |
672 | assert_not_reached("Unknown custom mount type"); | |
673 | } | |
674 | ||
675 | if (r < 0) | |
676 | return r; | |
677 | } | |
678 | ||
679 | return 0; | |
680 | } | |
681 | ||
0996ef00 CB |
682 | /* Retrieve existing subsystems. This function is called in a new cgroup |
683 | * namespace. | |
684 | */ | |
685 | static int get_controllers(Set *subsystems) { | |
686 | _cleanup_fclose_ FILE *f = NULL; | |
687 | char line[LINE_MAX]; | |
688 | ||
689 | assert(subsystems); | |
690 | ||
691 | f = fopen("/proc/self/cgroup", "re"); | |
692 | if (!f) | |
693 | return errno == ENOENT ? -ESRCH : -errno; | |
694 | ||
695 | FOREACH_LINE(line, f, return -errno) { | |
696 | int r; | |
697 | char *e, *l, *p; | |
698 | ||
0996ef00 CB |
699 | l = strchr(line, ':'); |
700 | if (!l) | |
701 | continue; | |
702 | ||
703 | l++; | |
704 | e = strchr(l, ':'); | |
705 | if (!e) | |
706 | continue; | |
707 | ||
708 | *e = 0; | |
709 | ||
add554f4 | 710 | if (STR_IN_SET(l, "", "name=systemd")) |
0996ef00 CB |
711 | continue; |
712 | ||
713 | p = strdup(l); | |
add554f4 ZJS |
714 | if (!p) |
715 | return -ENOMEM; | |
716 | ||
0996ef00 CB |
717 | r = set_consume(subsystems, p); |
718 | if (r < 0) | |
719 | return r; | |
720 | } | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
5da38d07 TH |
725 | static int mount_legacy_cgroup_hierarchy(const char *dest, const char *controller, const char *hierarchy, |
726 | CGroupUnified unified_requested, bool read_only) { | |
60e76d48 | 727 | const char *to, *fstype, *opts; |
e83bebef LP |
728 | int r; |
729 | ||
ee30f6ac | 730 | to = strjoina(strempty(dest), "/sys/fs/cgroup/", hierarchy); |
e83bebef LP |
731 | |
732 | r = path_is_mount_point(to, 0); | |
733 | if (r < 0 && r != -ENOENT) | |
734 | return log_error_errno(r, "Failed to determine if %s is mounted already: %m", to); | |
735 | if (r > 0) | |
736 | return 0; | |
737 | ||
738 | mkdir_p(to, 0755); | |
739 | ||
740 | /* The superblock mount options of the mount point need to be | |
741 | * identical to the hosts', and hence writable... */ | |
5da38d07 | 742 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) { |
60e76d48 ZJS |
743 | if (unified_requested >= CGROUP_UNIFIED_SYSTEMD) { |
744 | fstype = "cgroup2"; | |
745 | opts = NULL; | |
746 | } else { | |
747 | fstype = "cgroup"; | |
748 | opts = "none,name=systemd,xattr"; | |
749 | } | |
750 | } else { | |
751 | fstype = "cgroup"; | |
752 | opts = controller; | |
753 | } | |
5da38d07 | 754 | |
60e76d48 | 755 | r = mount_verbose(LOG_ERR, "cgroup", to, fstype, MS_NOSUID|MS_NOEXEC|MS_NODEV, opts); |
5da38d07 | 756 | if (r < 0) |
60e76d48 | 757 | return r; |
e83bebef | 758 | |
60e76d48 | 759 | /* ... hence let's only make the bind mount read-only, not the superblock. */ |
e83bebef | 760 | if (read_only) { |
60e76d48 ZJS |
761 | r = mount_verbose(LOG_ERR, NULL, to, NULL, |
762 | MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL); | |
763 | if (r < 0) | |
764 | return r; | |
e83bebef | 765 | } |
60e76d48 | 766 | |
e83bebef LP |
767 | return 1; |
768 | } | |
769 | ||
0996ef00 CB |
770 | /* Mount a legacy cgroup hierarchy when cgroup namespaces are supported. */ |
771 | static int mount_legacy_cgns_supported( | |
5da38d07 TH |
772 | CGroupUnified unified_requested, bool userns, uid_t uid_shift, |
773 | uid_t uid_range, const char *selinux_apifs_context) { | |
0996ef00 CB |
774 | _cleanup_set_free_free_ Set *controllers = NULL; |
775 | const char *cgroup_root = "/sys/fs/cgroup", *c; | |
776 | int r; | |
e83bebef | 777 | |
0996ef00 CB |
778 | (void) mkdir_p(cgroup_root, 0755); |
779 | ||
780 | /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */ | |
781 | r = path_is_mount_point(cgroup_root, AT_SYMLINK_FOLLOW); | |
782 | if (r < 0) | |
783 | return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m"); | |
784 | if (r == 0) { | |
785 | _cleanup_free_ char *options = NULL; | |
786 | ||
787 | /* When cgroup namespaces are enabled and user namespaces are | |
788 | * used then the mount of the cgroupfs is done *inside* the new | |
789 | * user namespace. We're root in the new user namespace and the | |
790 | * kernel will happily translate our uid/gid to the correct | |
791 | * uid/gid as seen from e.g. /proc/1/mountinfo. So we simply | |
792 | * pass uid 0 and not uid_shift to tmpfs_patch_options(). | |
793 | */ | |
794 | r = tmpfs_patch_options("mode=755", userns, 0, uid_range, true, selinux_apifs_context, &options); | |
795 | if (r < 0) | |
796 | return log_oom(); | |
797 | ||
60e76d48 ZJS |
798 | r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs", |
799 | MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options); | |
800 | if (r < 0) | |
801 | return r; | |
0996ef00 CB |
802 | } |
803 | ||
ca2f6384 | 804 | if (cg_all_unified() > 0) |
0996ef00 CB |
805 | goto skip_controllers; |
806 | ||
807 | controllers = set_new(&string_hash_ops); | |
808 | if (!controllers) | |
809 | return log_oom(); | |
810 | ||
811 | r = get_controllers(controllers); | |
812 | if (r < 0) | |
813 | return log_error_errno(r, "Failed to determine cgroup controllers: %m"); | |
814 | ||
815 | for (;;) { | |
816 | _cleanup_free_ const char *controller = NULL; | |
817 | ||
818 | controller = set_steal_first(controllers); | |
819 | if (!controller) | |
820 | break; | |
821 | ||
5da38d07 | 822 | r = mount_legacy_cgroup_hierarchy("", controller, controller, unified_requested, !userns); |
0996ef00 CB |
823 | if (r < 0) |
824 | return r; | |
825 | ||
826 | /* When multiple hierarchies are co-mounted, make their | |
827 | * constituting individual hierarchies a symlink to the | |
828 | * co-mount. | |
829 | */ | |
830 | c = controller; | |
831 | for (;;) { | |
832 | _cleanup_free_ char *target = NULL, *tok = NULL; | |
833 | ||
834 | r = extract_first_word(&c, &tok, ",", 0); | |
835 | if (r < 0) | |
836 | return log_error_errno(r, "Failed to extract co-mounted cgroup controller: %m"); | |
837 | if (r == 0) | |
838 | break; | |
839 | ||
840 | target = prefix_root("/sys/fs/cgroup", tok); | |
841 | if (!target) | |
842 | return log_oom(); | |
843 | ||
844 | if (streq(controller, tok)) | |
845 | break; | |
846 | ||
847 | r = symlink_idempotent(controller, target); | |
848 | if (r == -EINVAL) | |
849 | return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m"); | |
850 | if (r < 0) | |
851 | return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m"); | |
852 | } | |
853 | } | |
854 | ||
855 | skip_controllers: | |
5da38d07 | 856 | r = mount_legacy_cgroup_hierarchy("", SYSTEMD_CGROUP_CONTROLLER, "systemd", unified_requested, false); |
0996ef00 CB |
857 | if (r < 0) |
858 | return r; | |
859 | ||
60e76d48 ZJS |
860 | if (!userns) |
861 | return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL, | |
862 | MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755"); | |
0996ef00 CB |
863 | |
864 | return 0; | |
865 | } | |
866 | ||
867 | /* Mount legacy cgroup hierarchy when cgroup namespaces are unsupported. */ | |
868 | static int mount_legacy_cgns_unsupported( | |
869 | const char *dest, | |
5da38d07 | 870 | CGroupUnified unified_requested, bool userns, uid_t uid_shift, uid_t uid_range, |
0996ef00 | 871 | const char *selinux_apifs_context) { |
e83bebef LP |
872 | _cleanup_set_free_free_ Set *controllers = NULL; |
873 | const char *cgroup_root; | |
874 | int r; | |
875 | ||
876 | cgroup_root = prefix_roota(dest, "/sys/fs/cgroup"); | |
877 | ||
d8fc6a00 LP |
878 | (void) mkdir_p(cgroup_root, 0755); |
879 | ||
e83bebef LP |
880 | /* Mount a tmpfs to /sys/fs/cgroup if it's not mounted there yet. */ |
881 | r = path_is_mount_point(cgroup_root, AT_SYMLINK_FOLLOW); | |
882 | if (r < 0) | |
883 | return log_error_errno(r, "Failed to determine if /sys/fs/cgroup is already mounted: %m"); | |
884 | if (r == 0) { | |
885 | _cleanup_free_ char *options = NULL; | |
886 | ||
0996ef00 | 887 | r = tmpfs_patch_options("mode=755", userns, uid_shift, uid_range, false, selinux_apifs_context, &options); |
e83bebef LP |
888 | if (r < 0) |
889 | return log_oom(); | |
890 | ||
60e76d48 ZJS |
891 | r = mount_verbose(LOG_ERR, "tmpfs", cgroup_root, "tmpfs", |
892 | MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, options); | |
893 | if (r < 0) | |
894 | return r; | |
e83bebef LP |
895 | } |
896 | ||
ca2f6384 | 897 | if (cg_all_unified() > 0) |
e83bebef LP |
898 | goto skip_controllers; |
899 | ||
900 | controllers = set_new(&string_hash_ops); | |
901 | if (!controllers) | |
902 | return log_oom(); | |
903 | ||
904 | r = cg_kernel_controllers(controllers); | |
905 | if (r < 0) | |
906 | return log_error_errno(r, "Failed to determine cgroup controllers: %m"); | |
907 | ||
908 | for (;;) { | |
909 | _cleanup_free_ char *controller = NULL, *origin = NULL, *combined = NULL; | |
910 | ||
911 | controller = set_steal_first(controllers); | |
912 | if (!controller) | |
913 | break; | |
914 | ||
915 | origin = prefix_root("/sys/fs/cgroup/", controller); | |
916 | if (!origin) | |
917 | return log_oom(); | |
918 | ||
919 | r = readlink_malloc(origin, &combined); | |
920 | if (r == -EINVAL) { | |
921 | /* Not a symbolic link, but directly a single cgroup hierarchy */ | |
922 | ||
5da38d07 | 923 | r = mount_legacy_cgroup_hierarchy(dest, controller, controller, unified_requested, true); |
e83bebef LP |
924 | if (r < 0) |
925 | return r; | |
926 | ||
927 | } else if (r < 0) | |
928 | return log_error_errno(r, "Failed to read link %s: %m", origin); | |
929 | else { | |
930 | _cleanup_free_ char *target = NULL; | |
931 | ||
932 | target = prefix_root(dest, origin); | |
933 | if (!target) | |
934 | return log_oom(); | |
935 | ||
936 | /* A symbolic link, a combination of controllers in one hierarchy */ | |
937 | ||
938 | if (!filename_is_valid(combined)) { | |
939 | log_warning("Ignoring invalid combined hierarchy %s.", combined); | |
940 | continue; | |
941 | } | |
942 | ||
5da38d07 | 943 | r = mount_legacy_cgroup_hierarchy(dest, combined, combined, unified_requested, true); |
e83bebef LP |
944 | if (r < 0) |
945 | return r; | |
946 | ||
947 | r = symlink_idempotent(combined, target); | |
0996ef00 CB |
948 | if (r == -EINVAL) |
949 | return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m"); | |
e83bebef LP |
950 | if (r < 0) |
951 | return log_error_errno(r, "Failed to create symlink for combined hierarchy: %m"); | |
952 | } | |
953 | } | |
954 | ||
955 | skip_controllers: | |
5da38d07 | 956 | r = mount_legacy_cgroup_hierarchy(dest, SYSTEMD_CGROUP_CONTROLLER, "systemd", unified_requested, false); |
e83bebef LP |
957 | if (r < 0) |
958 | return r; | |
959 | ||
60e76d48 ZJS |
960 | return mount_verbose(LOG_ERR, NULL, cgroup_root, NULL, |
961 | MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME|MS_RDONLY, "mode=755"); | |
e83bebef LP |
962 | } |
963 | ||
964 | static int mount_unified_cgroups(const char *dest) { | |
965 | const char *p; | |
966 | int r; | |
967 | ||
968 | assert(dest); | |
969 | ||
88e10572 MT |
970 | p = prefix_roota(dest, "/sys/fs/cgroup"); |
971 | ||
972 | (void) mkdir_p(p, 0755); | |
e83bebef LP |
973 | |
974 | r = path_is_mount_point(p, AT_SYMLINK_FOLLOW); | |
975 | if (r < 0) | |
976 | return log_error_errno(r, "Failed to determine if %s is mounted already: %m", p); | |
977 | if (r > 0) { | |
88e10572 | 978 | p = prefix_roota(dest, "/sys/fs/cgroup/cgroup.procs"); |
e83bebef LP |
979 | if (access(p, F_OK) >= 0) |
980 | return 0; | |
981 | if (errno != ENOENT) | |
982 | return log_error_errno(errno, "Failed to determine if mount point %s contains the unified cgroup hierarchy: %m", p); | |
983 | ||
984 | log_error("%s is already mounted but not a unified cgroup hierarchy. Refusing.", p); | |
985 | return -EINVAL; | |
986 | } | |
987 | ||
60e76d48 | 988 | return mount_verbose(LOG_ERR, "cgroup", p, "cgroup2", MS_NOSUID|MS_NOEXEC|MS_NODEV, NULL); |
e83bebef LP |
989 | } |
990 | ||
991 | int mount_cgroups( | |
992 | const char *dest, | |
5da38d07 | 993 | CGroupUnified unified_requested, |
e83bebef | 994 | bool userns, uid_t uid_shift, uid_t uid_range, |
5a8ff0e6 CB |
995 | const char *selinux_apifs_context, |
996 | bool use_cgns) { | |
e83bebef | 997 | |
5da38d07 | 998 | if (unified_requested >= CGROUP_UNIFIED_ALL) |
e83bebef | 999 | return mount_unified_cgroups(dest); |
ada54120 | 1000 | else if (use_cgns) |
5da38d07 | 1001 | return mount_legacy_cgns_supported(unified_requested, userns, uid_shift, uid_range, selinux_apifs_context); |
0996ef00 | 1002 | |
5da38d07 | 1003 | return mount_legacy_cgns_unsupported(dest, unified_requested, userns, uid_shift, uid_range, selinux_apifs_context); |
e83bebef LP |
1004 | } |
1005 | ||
1006 | int mount_systemd_cgroup_writable( | |
1007 | const char *dest, | |
5da38d07 | 1008 | CGroupUnified unified_requested) { |
e83bebef LP |
1009 | |
1010 | _cleanup_free_ char *own_cgroup_path = NULL; | |
1011 | const char *systemd_root, *systemd_own; | |
1012 | int r; | |
1013 | ||
1014 | assert(dest); | |
1015 | ||
1016 | r = cg_pid_get_path(NULL, 0, &own_cgroup_path); | |
1017 | if (r < 0) | |
1018 | return log_error_errno(r, "Failed to determine our own cgroup path: %m"); | |
1019 | ||
1020 | /* If we are living in the top-level, then there's nothing to do... */ | |
1021 | if (path_equal(own_cgroup_path, "/")) | |
1022 | return 0; | |
1023 | ||
5da38d07 | 1024 | if (unified_requested >= CGROUP_UNIFIED_ALL) { |
e83bebef LP |
1025 | systemd_own = strjoina(dest, "/sys/fs/cgroup", own_cgroup_path); |
1026 | systemd_root = prefix_roota(dest, "/sys/fs/cgroup"); | |
1027 | } else { | |
1028 | systemd_own = strjoina(dest, "/sys/fs/cgroup/systemd", own_cgroup_path); | |
1029 | systemd_root = prefix_roota(dest, "/sys/fs/cgroup/systemd"); | |
1030 | } | |
1031 | ||
1032 | /* Make our own cgroup a (writable) bind mount */ | |
60e76d48 ZJS |
1033 | r = mount_verbose(LOG_ERR, systemd_own, systemd_own, NULL, MS_BIND, NULL); |
1034 | if (r < 0) | |
1035 | return r; | |
e83bebef LP |
1036 | |
1037 | /* And then remount the systemd cgroup root read-only */ | |
60e76d48 ZJS |
1038 | return mount_verbose(LOG_ERR, NULL, systemd_root, NULL, |
1039 | MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, NULL); | |
e83bebef LP |
1040 | } |
1041 | ||
1042 | int setup_volatile_state( | |
1043 | const char *directory, | |
1044 | VolatileMode mode, | |
1045 | bool userns, uid_t uid_shift, uid_t uid_range, | |
1046 | const char *selinux_apifs_context) { | |
1047 | ||
1048 | _cleanup_free_ char *buf = NULL; | |
1049 | const char *p, *options; | |
1050 | int r; | |
1051 | ||
1052 | assert(directory); | |
1053 | ||
1054 | if (mode != VOLATILE_STATE) | |
1055 | return 0; | |
1056 | ||
1057 | /* --volatile=state means we simply overmount /var | |
1058 | with a tmpfs, and the rest read-only. */ | |
1059 | ||
6b7c9f8b | 1060 | r = bind_remount_recursive(directory, true, NULL); |
e83bebef LP |
1061 | if (r < 0) |
1062 | return log_error_errno(r, "Failed to remount %s read-only: %m", directory); | |
1063 | ||
1064 | p = prefix_roota(directory, "/var"); | |
1065 | r = mkdir(p, 0755); | |
1066 | if (r < 0 && errno != EEXIST) | |
1067 | return log_error_errno(errno, "Failed to create %s: %m", directory); | |
1068 | ||
1069 | options = "mode=755"; | |
0996ef00 | 1070 | r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf); |
e83bebef LP |
1071 | if (r < 0) |
1072 | return log_oom(); | |
1073 | if (r > 0) | |
1074 | options = buf; | |
1075 | ||
60e76d48 | 1076 | return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options); |
e83bebef LP |
1077 | } |
1078 | ||
1079 | int setup_volatile( | |
1080 | const char *directory, | |
1081 | VolatileMode mode, | |
1082 | bool userns, uid_t uid_shift, uid_t uid_range, | |
1083 | const char *selinux_apifs_context) { | |
1084 | ||
1085 | bool tmpfs_mounted = false, bind_mounted = false; | |
1086 | char template[] = "/tmp/nspawn-volatile-XXXXXX"; | |
1087 | _cleanup_free_ char *buf = NULL; | |
1088 | const char *f, *t, *options; | |
1089 | int r; | |
1090 | ||
1091 | assert(directory); | |
1092 | ||
1093 | if (mode != VOLATILE_YES) | |
1094 | return 0; | |
1095 | ||
1096 | /* --volatile=yes means we mount a tmpfs to the root dir, and | |
1097 | the original /usr to use inside it, and that read-only. */ | |
1098 | ||
1099 | if (!mkdtemp(template)) | |
1100 | return log_error_errno(errno, "Failed to create temporary directory: %m"); | |
1101 | ||
1102 | options = "mode=755"; | |
0996ef00 | 1103 | r = tmpfs_patch_options(options, userns, uid_shift, uid_range, false, selinux_apifs_context, &buf); |
e83bebef LP |
1104 | if (r < 0) |
1105 | return log_oom(); | |
1106 | if (r > 0) | |
1107 | options = buf; | |
1108 | ||
60e76d48 ZJS |
1109 | r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options); |
1110 | if (r < 0) | |
e83bebef | 1111 | goto fail; |
e83bebef LP |
1112 | |
1113 | tmpfs_mounted = true; | |
1114 | ||
1115 | f = prefix_roota(directory, "/usr"); | |
1116 | t = prefix_roota(template, "/usr"); | |
1117 | ||
1118 | r = mkdir(t, 0755); | |
1119 | if (r < 0 && errno != EEXIST) { | |
1120 | r = log_error_errno(errno, "Failed to create %s: %m", t); | |
1121 | goto fail; | |
1122 | } | |
1123 | ||
60e76d48 ZJS |
1124 | r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL); |
1125 | if (r < 0) | |
e83bebef | 1126 | goto fail; |
e83bebef LP |
1127 | |
1128 | bind_mounted = true; | |
1129 | ||
6b7c9f8b | 1130 | r = bind_remount_recursive(t, true, NULL); |
e83bebef LP |
1131 | if (r < 0) { |
1132 | log_error_errno(r, "Failed to remount %s read-only: %m", t); | |
1133 | goto fail; | |
1134 | } | |
1135 | ||
60e76d48 ZJS |
1136 | r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL); |
1137 | if (r < 0) | |
e83bebef | 1138 | goto fail; |
e83bebef LP |
1139 | |
1140 | (void) rmdir(template); | |
1141 | ||
1142 | return 0; | |
1143 | ||
1144 | fail: | |
1145 | if (bind_mounted) | |
60e76d48 | 1146 | (void) umount_verbose(t); |
e83bebef LP |
1147 | |
1148 | if (tmpfs_mounted) | |
60e76d48 | 1149 | (void) umount_verbose(template); |
e83bebef LP |
1150 | (void) rmdir(template); |
1151 | return r; | |
1152 | } | |
1153 | ||
1154 | VolatileMode volatile_mode_from_string(const char *s) { | |
1155 | int b; | |
1156 | ||
1157 | if (isempty(s)) | |
1158 | return _VOLATILE_MODE_INVALID; | |
1159 | ||
1160 | b = parse_boolean(s); | |
1161 | if (b > 0) | |
1162 | return VOLATILE_YES; | |
1163 | if (b == 0) | |
1164 | return VOLATILE_NO; | |
1165 | ||
1166 | if (streq(s, "state")) | |
1167 | return VOLATILE_STATE; | |
1168 | ||
1169 | return _VOLATILE_MODE_INVALID; | |
1170 | } |