]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e83bebef | 2 | |
4f5dd394 | 3 | #include <sys/mount.h> |
07630cea | 4 | #include <linux/magic.h> |
e83bebef | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
4f5dd394 | 7 | #include "escape.h" |
0996ef00 | 8 | #include "fd-util.h" |
f4f15635 | 9 | #include "fs-util.h" |
e83bebef | 10 | #include "label.h" |
4f5dd394 | 11 | #include "mkdir.h" |
4349cd7c | 12 | #include "mount-util.h" |
049af8ad | 13 | #include "mountpoint-util.h" |
6bedfcbb LP |
14 | #include "nspawn-mount.h" |
15 | #include "parse-util.h" | |
4f5dd394 LP |
16 | #include "path-util.h" |
17 | #include "rm-rf.h" | |
e83bebef | 18 | #include "set.h" |
8fcde012 | 19 | #include "stat-util.h" |
07630cea | 20 | #include "string-util.h" |
4f5dd394 | 21 | #include "strv.h" |
e4de7287 | 22 | #include "tmpfile-util.h" |
ee104e11 | 23 | #include "user-util.h" |
4f5dd394 | 24 | #include "util.h" |
e83bebef | 25 | |
88614c8a | 26 | CustomMount* custom_mount_add(CustomMount **l, size_t *n, CustomMountType t) { |
e83bebef LP |
27 | CustomMount *c, *ret; |
28 | ||
29 | assert(l); | |
30 | assert(n); | |
31 | assert(t >= 0); | |
32 | assert(t < _CUSTOM_MOUNT_TYPE_MAX); | |
33 | ||
aa484f35 | 34 | c = reallocarray(*l, *n + 1, sizeof(CustomMount)); |
e83bebef LP |
35 | if (!c) |
36 | return NULL; | |
37 | ||
38 | *l = c; | |
39 | ret = *l + *n; | |
40 | (*n)++; | |
41 | ||
42 | *ret = (CustomMount) { .type = t }; | |
43 | ||
44 | return ret; | |
45 | } | |
46 | ||
88614c8a LP |
47 | void custom_mount_free_all(CustomMount *l, size_t n) { |
48 | size_t i; | |
e83bebef LP |
49 | |
50 | for (i = 0; i < n; i++) { | |
51 | CustomMount *m = l + i; | |
52 | ||
53 | free(m->source); | |
54 | free(m->destination); | |
55 | free(m->options); | |
56 | ||
57 | if (m->work_dir) { | |
58 | (void) rm_rf(m->work_dir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
59 | free(m->work_dir); | |
60 | } | |
61 | ||
c7a4890c LP |
62 | if (m->rm_rf_tmpdir) { |
63 | (void) rm_rf(m->rm_rf_tmpdir, REMOVE_ROOT|REMOVE_PHYSICAL); | |
64 | free(m->rm_rf_tmpdir); | |
65 | } | |
66 | ||
e83bebef LP |
67 | strv_free(m->lower); |
68 | } | |
69 | ||
70 | free(l); | |
71 | } | |
72 | ||
93bab288 | 73 | static int custom_mount_compare(const CustomMount *a, const CustomMount *b) { |
e83bebef LP |
74 | int r; |
75 | ||
93bab288 | 76 | r = path_compare(a->destination, b->destination); |
e83bebef LP |
77 | if (r != 0) |
78 | return r; | |
79 | ||
93bab288 | 80 | return CMP(a->type, b->type); |
e83bebef LP |
81 | } |
82 | ||
86c0dd4a LP |
83 | static bool source_path_is_valid(const char *p) { |
84 | assert(p); | |
85 | ||
86 | if (*p == '+') | |
87 | p++; | |
88 | ||
89 | return path_is_absolute(p); | |
90 | } | |
91 | ||
92 | static char *resolve_source_path(const char *dest, const char *source) { | |
93 | ||
94 | if (!source) | |
95 | return NULL; | |
96 | ||
97 | if (source[0] == '+') | |
98 | return prefix_root(dest, source + 1); | |
99 | ||
100 | return strdup(source); | |
101 | } | |
102 | ||
88614c8a LP |
103 | int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) { |
104 | size_t i; | |
86c0dd4a LP |
105 | int r; |
106 | ||
107 | /* Prepare all custom mounts. This will make source we know all temporary directories. This is called in the | |
108 | * parent process, so that we know the temporary directories to remove on exit before we fork off the | |
109 | * children. */ | |
110 | ||
111 | assert(l || n == 0); | |
112 | ||
113 | /* Order the custom mounts, and make sure we have a working directory */ | |
93bab288 | 114 | typesafe_qsort(l, n, custom_mount_compare); |
86c0dd4a LP |
115 | |
116 | for (i = 0; i < n; i++) { | |
117 | CustomMount *m = l + i; | |
118 | ||
119 | if (m->source) { | |
120 | char *s; | |
121 | ||
122 | s = resolve_source_path(dest, m->source); | |
123 | if (!s) | |
124 | return log_oom(); | |
125 | ||
10af01a5 | 126 | free_and_replace(m->source, s); |
c7a4890c LP |
127 | } else { |
128 | /* No source specified? In that case, use a throw-away temporary directory in /var/tmp */ | |
129 | ||
130 | m->rm_rf_tmpdir = strdup("/var/tmp/nspawn-temp-XXXXXX"); | |
131 | if (!m->rm_rf_tmpdir) | |
132 | return log_oom(); | |
133 | ||
134 | if (!mkdtemp(m->rm_rf_tmpdir)) { | |
135 | m->rm_rf_tmpdir = mfree(m->rm_rf_tmpdir); | |
136 | return log_error_errno(errno, "Failed to acquire temporary directory: %m"); | |
137 | } | |
138 | ||
139 | m->source = strjoin(m->rm_rf_tmpdir, "/src"); | |
140 | if (!m->source) | |
141 | return log_oom(); | |
142 | ||
143 | if (mkdir(m->source, 0755) < 0) | |
144 | return log_error_errno(errno, "Failed to create %s: %m", m->source); | |
86c0dd4a LP |
145 | } |
146 | ||
147 | if (m->type == CUSTOM_MOUNT_OVERLAY) { | |
148 | char **j; | |
149 | ||
150 | STRV_FOREACH(j, m->lower) { | |
151 | char *s; | |
152 | ||
153 | s = resolve_source_path(dest, *j); | |
154 | if (!s) | |
155 | return log_oom(); | |
156 | ||
10af01a5 | 157 | free_and_replace(*j, s); |
86c0dd4a LP |
158 | } |
159 | ||
160 | if (m->work_dir) { | |
161 | char *s; | |
162 | ||
163 | s = resolve_source_path(dest, m->work_dir); | |
164 | if (!s) | |
165 | return log_oom(); | |
166 | ||
10af01a5 | 167 | free_and_replace(m->work_dir, s); |
86c0dd4a LP |
168 | } else { |
169 | assert(m->source); | |
170 | ||
171 | r = tempfn_random(m->source, NULL, &m->work_dir); | |
172 | if (r < 0) | |
173 | return log_error_errno(r, "Failed to acquire working directory: %m"); | |
174 | } | |
175 | ||
176 | (void) mkdir_label(m->work_dir, 0700); | |
177 | } | |
178 | } | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
88614c8a | 183 | int bind_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) { |
e83bebef LP |
184 | _cleanup_free_ char *source = NULL, *destination = NULL, *opts = NULL; |
185 | const char *p = s; | |
186 | CustomMount *m; | |
187 | int r; | |
188 | ||
189 | assert(l); | |
190 | assert(n); | |
191 | ||
192 | r = extract_many_words(&p, ":", EXTRACT_DONT_COALESCE_SEPARATORS, &source, &destination, NULL); | |
193 | if (r < 0) | |
194 | return r; | |
195 | if (r == 0) | |
196 | return -EINVAL; | |
e83bebef | 197 | if (r == 1) { |
86c0dd4a | 198 | destination = strdup(source[0] == '+' ? source+1 : source); |
e83bebef LP |
199 | if (!destination) |
200 | return -ENOMEM; | |
201 | } | |
e83bebef LP |
202 | if (r == 2 && !isempty(p)) { |
203 | opts = strdup(p); | |
204 | if (!opts) | |
205 | return -ENOMEM; | |
206 | } | |
207 | ||
c7a4890c LP |
208 | if (isempty(source)) |
209 | source = NULL; | |
210 | else if (!source_path_is_valid(source)) | |
e83bebef | 211 | return -EINVAL; |
c7a4890c | 212 | |
e83bebef LP |
213 | if (!path_is_absolute(destination)) |
214 | return -EINVAL; | |
215 | ||
216 | m = custom_mount_add(l, n, CUSTOM_MOUNT_BIND); | |
217 | if (!m) | |
48cbe5f8 | 218 | return -ENOMEM; |
e83bebef LP |
219 | |
220 | m->source = source; | |
221 | m->destination = destination; | |
222 | m->read_only = read_only; | |
223 | m->options = opts; | |
224 | ||
225 | source = destination = opts = NULL; | |
226 | return 0; | |
227 | } | |
228 | ||
88614c8a | 229 | int tmpfs_mount_parse(CustomMount **l, size_t *n, const char *s) { |
e83bebef LP |
230 | _cleanup_free_ char *path = NULL, *opts = NULL; |
231 | const char *p = s; | |
232 | CustomMount *m; | |
233 | int r; | |
234 | ||
235 | assert(l); | |
236 | assert(n); | |
237 | assert(s); | |
238 | ||
239 | r = extract_first_word(&p, &path, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
240 | if (r < 0) | |
241 | return r; | |
242 | if (r == 0) | |
243 | return -EINVAL; | |
244 | ||
245 | if (isempty(p)) | |
246 | opts = strdup("mode=0755"); | |
247 | else | |
248 | opts = strdup(p); | |
249 | if (!opts) | |
250 | return -ENOMEM; | |
251 | ||
252 | if (!path_is_absolute(path)) | |
253 | return -EINVAL; | |
254 | ||
255 | m = custom_mount_add(l, n, CUSTOM_MOUNT_TMPFS); | |
256 | if (!m) | |
257 | return -ENOMEM; | |
258 | ||
1cc6c93a YW |
259 | m->destination = TAKE_PTR(path); |
260 | m->options = TAKE_PTR(opts); | |
e83bebef | 261 | |
e83bebef LP |
262 | return 0; |
263 | } | |
264 | ||
88614c8a | 265 | int overlay_mount_parse(CustomMount **l, size_t *n, const char *s, bool read_only) { |
ad85779a LP |
266 | _cleanup_free_ char *upper = NULL, *destination = NULL; |
267 | _cleanup_strv_free_ char **lower = NULL; | |
268 | CustomMount *m; | |
86c0dd4a | 269 | int k; |
ad85779a | 270 | |
86c0dd4a LP |
271 | k = strv_split_extract(&lower, s, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
272 | if (k < 0) | |
273 | return k; | |
ad85779a LP |
274 | if (k < 2) |
275 | return -EADDRNOTAVAIL; | |
276 | if (k == 2) { | |
86c0dd4a LP |
277 | /* If two parameters are specified, the first one is the lower, the second one the upper directory. And |
278 | * we'll also define the destination mount point the same as the upper. */ | |
279 | ||
280 | if (!source_path_is_valid(lower[0]) || | |
281 | !source_path_is_valid(lower[1])) | |
282 | return -EINVAL; | |
283 | ||
ae2a15bc | 284 | upper = TAKE_PTR(lower[1]); |
ad85779a | 285 | |
86c0dd4a | 286 | destination = strdup(upper[0] == '+' ? upper+1 : upper); /* take the destination without "+" prefix */ |
ad85779a LP |
287 | if (!destination) |
288 | return -ENOMEM; | |
ad85779a | 289 | } else { |
c7a4890c | 290 | char **i; |
86c0dd4a LP |
291 | |
292 | /* If more than two parameters are specified, the last one is the destination, the second to last one | |
293 | * the "upper", and all before that the "lower" directories. */ | |
294 | ||
ad85779a | 295 | destination = lower[k - 1]; |
ae2a15bc | 296 | upper = TAKE_PTR(lower[k - 2]); |
86c0dd4a | 297 | |
c7a4890c LP |
298 | STRV_FOREACH(i, lower) |
299 | if (!source_path_is_valid(*i)) | |
300 | return -EINVAL; | |
301 | ||
302 | /* If the upper directory is unspecified, then let's create it automatically as a throw-away directory | |
303 | * in /var/tmp */ | |
304 | if (isempty(upper)) | |
305 | upper = NULL; | |
306 | else if (!source_path_is_valid(upper)) | |
307 | return -EINVAL; | |
308 | ||
86c0dd4a LP |
309 | if (!path_is_absolute(destination)) |
310 | return -EINVAL; | |
ad85779a LP |
311 | } |
312 | ||
313 | m = custom_mount_add(l, n, CUSTOM_MOUNT_OVERLAY); | |
314 | if (!m) | |
315 | return -ENOMEM; | |
316 | ||
1cc6c93a YW |
317 | m->destination = TAKE_PTR(destination); |
318 | m->source = TAKE_PTR(upper); | |
319 | m->lower = TAKE_PTR(lower); | |
ad85779a LP |
320 | m->read_only = read_only; |
321 | ||
ad85779a LP |
322 | return 0; |
323 | } | |
324 | ||
04029482 | 325 | int tmpfs_patch_options( |
e83bebef | 326 | const char *options, |
2fa017f1 | 327 | uid_t uid_shift, |
e83bebef LP |
328 | const char *selinux_apifs_context, |
329 | char **ret) { | |
330 | ||
331 | char *buf = NULL; | |
332 | ||
2fa017f1 | 333 | if (uid_shift != UID_INVALID) { |
9aa2169e | 334 | if (asprintf(&buf, "%s%suid=" UID_FMT ",gid=" UID_FMT, |
87e4e28d | 335 | strempty(options), options ? "," : "", |
9aa2169e | 336 | uid_shift, uid_shift) < 0) |
e83bebef LP |
337 | return -ENOMEM; |
338 | ||
339 | options = buf; | |
340 | } | |
341 | ||
349cc4a5 | 342 | #if HAVE_SELINUX |
e83bebef LP |
343 | if (selinux_apifs_context) { |
344 | char *t; | |
345 | ||
87e4e28d | 346 | t = strjoin(strempty(options), options ? "," : "", |
9aa2169e ZJS |
347 | "context=\"", selinux_apifs_context, "\""); |
348 | free(buf); | |
349 | if (!t) | |
e83bebef | 350 | return -ENOMEM; |
e83bebef | 351 | |
e83bebef LP |
352 | buf = t; |
353 | } | |
354 | #endif | |
355 | ||
0996ef00 CB |
356 | if (!buf && options) { |
357 | buf = strdup(options); | |
358 | if (!buf) | |
359 | return -ENOMEM; | |
360 | } | |
e83bebef | 361 | *ret = buf; |
0996ef00 | 362 | |
e83bebef LP |
363 | return !!buf; |
364 | } | |
365 | ||
4f086aab | 366 | int mount_sysfs(const char *dest, MountSettingsMask mount_settings) { |
d8fc6a00 | 367 | const char *full, *top, *x; |
d1678248 | 368 | int r; |
4f086aab | 369 | unsigned long extra_flags = 0; |
d8fc6a00 LP |
370 | |
371 | top = prefix_roota(dest, "/sys"); | |
40fd52f2 | 372 | r = path_is_fs_type(top, SYSFS_MAGIC); |
d1678248 ILG |
373 | if (r < 0) |
374 | return log_error_errno(r, "Failed to determine filesystem type of %s: %m", top); | |
375 | /* /sys might already be mounted as sysfs by the outer child in the | |
376 | * !netns case. In this case, it's all good. Don't touch it because we | |
377 | * don't have the right to do so, see https://github.com/systemd/systemd/issues/1555. | |
378 | */ | |
379 | if (r > 0) | |
380 | return 0; | |
381 | ||
d8fc6a00 LP |
382 | full = prefix_roota(top, "/full"); |
383 | ||
384 | (void) mkdir(full, 0755); | |
385 | ||
4f086aab SU |
386 | if (mount_settings & MOUNT_APPLY_APIVFS_RO) |
387 | extra_flags |= MS_RDONLY; | |
388 | ||
60e76d48 | 389 | r = mount_verbose(LOG_ERR, "sysfs", full, "sysfs", |
4f086aab | 390 | MS_NOSUID|MS_NOEXEC|MS_NODEV|extra_flags, NULL); |
60e76d48 ZJS |
391 | if (r < 0) |
392 | return r; | |
d8fc6a00 LP |
393 | |
394 | FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") { | |
395 | _cleanup_free_ char *from = NULL, *to = NULL; | |
396 | ||
397 | from = prefix_root(full, x); | |
398 | if (!from) | |
399 | return log_oom(); | |
400 | ||
401 | to = prefix_root(top, x); | |
402 | if (!to) | |
403 | return log_oom(); | |
404 | ||
405 | (void) mkdir(to, 0755); | |
406 | ||
60e76d48 ZJS |
407 | r = mount_verbose(LOG_ERR, from, to, NULL, MS_BIND, NULL); |
408 | if (r < 0) | |
409 | return r; | |
d8fc6a00 | 410 | |
60e76d48 | 411 | r = mount_verbose(LOG_ERR, NULL, to, NULL, |
4f086aab | 412 | MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL); |
60e76d48 ZJS |
413 | if (r < 0) |
414 | return r; | |
d8fc6a00 LP |
415 | } |
416 | ||
60e76d48 ZJS |
417 | r = umount_verbose(full); |
418 | if (r < 0) | |
419 | return r; | |
d8fc6a00 LP |
420 | |
421 | if (rmdir(full) < 0) | |
422 | return log_error_errno(errno, "Failed to remove %s: %m", full); | |
423 | ||
0996ef00 CB |
424 | /* Create mountpoint for cgroups. Otherwise we are not allowed since we |
425 | * remount /sys read-only. | |
426 | */ | |
677a72cd LS |
427 | x = prefix_roota(top, "/fs/cgroup"); |
428 | (void) mkdir_p(x, 0755); | |
d8fc6a00 | 429 | |
60e76d48 | 430 | return mount_verbose(LOG_ERR, NULL, top, NULL, |
4f086aab | 431 | MS_BIND|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT|extra_flags, NULL); |
d8fc6a00 LP |
432 | } |
433 | ||
9c0fad5f | 434 | static int mkdir_userns(const char *path, mode_t mode, uid_t uid_shift) { |
63eae723 EV |
435 | int r; |
436 | ||
437 | assert(path); | |
438 | ||
dae8b82e ZJS |
439 | r = mkdir_errno_wrapper(path, mode); |
440 | if (r < 0 && r != -EEXIST) | |
441 | return r; | |
63eae723 | 442 | |
9c0fad5f | 443 | if (uid_shift == UID_INVALID) |
acbbf69b LP |
444 | return 0; |
445 | ||
dae8b82e | 446 | if (lchown(path, uid_shift, uid_shift) < 0) |
acbbf69b | 447 | return -errno; |
63eae723 EV |
448 | |
449 | return 0; | |
450 | } | |
451 | ||
9c0fad5f | 452 | static int mkdir_userns_p(const char *prefix, const char *path, mode_t mode, uid_t uid_shift) { |
63eae723 EV |
453 | const char *p, *e; |
454 | int r; | |
455 | ||
456 | assert(path); | |
457 | ||
458 | if (prefix && !path_startswith(path, prefix)) | |
459 | return -ENOTDIR; | |
460 | ||
461 | /* create every parent directory in the path, except the last component */ | |
462 | p = path + strspn(path, "/"); | |
463 | for (;;) { | |
464 | char t[strlen(path) + 1]; | |
465 | ||
466 | e = p + strcspn(p, "/"); | |
467 | p = e + strspn(e, "/"); | |
468 | ||
469 | /* Is this the last component? If so, then we're done */ | |
470 | if (*p == 0) | |
471 | break; | |
472 | ||
473 | memcpy(t, path, e - path); | |
474 | t[e-path] = 0; | |
475 | ||
476 | if (prefix && path_startswith(prefix, t)) | |
477 | continue; | |
478 | ||
9c0fad5f | 479 | r = mkdir_userns(t, mode, uid_shift); |
63eae723 EV |
480 | if (r < 0) |
481 | return r; | |
482 | } | |
483 | ||
9c0fad5f | 484 | return mkdir_userns(path, mode, uid_shift); |
63eae723 EV |
485 | } |
486 | ||
e83bebef | 487 | int mount_all(const char *dest, |
4f086aab | 488 | MountSettingsMask mount_settings, |
2fa017f1 | 489 | uid_t uid_shift, |
e83bebef LP |
490 | const char *selinux_apifs_context) { |
491 | ||
d4b653c5 LP |
492 | #define PROC_INACCESSIBLE(path) \ |
493 | { NULL, (path), NULL, NULL, MS_BIND, \ | |
494 | MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_INACCESSIBLE_REG }, /* Bind mount first ... */ \ | |
495 | { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \ | |
496 | MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */ | |
497 | ||
498 | #define PROC_READ_ONLY(path) \ | |
499 | { (path), (path), NULL, NULL, MS_BIND, \ | |
500 | MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ \ | |
501 | { NULL, (path), NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, \ | |
502 | MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO } /* Then, make it r/o */ | |
503 | ||
e83bebef LP |
504 | typedef struct MountPoint { |
505 | const char *what; | |
506 | const char *where; | |
507 | const char *type; | |
508 | const char *options; | |
509 | unsigned long flags; | |
4f086aab | 510 | MountSettingsMask mount_settings; |
e83bebef LP |
511 | } MountPoint; |
512 | ||
513 | static const MountPoint mount_table[] = { | |
d4b653c5 LP |
514 | /* First we list inner child mounts (i.e. mounts applied *after* entering user namespacing) */ |
515 | { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
516 | MOUNT_FATAL|MOUNT_IN_USERNS }, | |
517 | ||
518 | { "/proc/sys", "/proc/sys", NULL, NULL, MS_BIND, | |
519 | MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* Bind mount first ... */ | |
520 | ||
521 | { "/proc/sys/net", "/proc/sys/net", NULL, NULL, MS_BIND, | |
522 | MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO|MOUNT_APPLY_APIVFS_NETNS }, /* (except for this) */ | |
523 | ||
524 | { NULL, "/proc/sys", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, | |
525 | MOUNT_FATAL|MOUNT_IN_USERNS|MOUNT_APPLY_APIVFS_RO }, /* ... then, make it r/o */ | |
526 | ||
527 | /* Make these files inaccessible to container payloads: they potentially leak information about kernel | |
528 | * internals or the host's execution environment to the container */ | |
529 | PROC_INACCESSIBLE("/proc/kallsyms"), | |
530 | PROC_INACCESSIBLE("/proc/kcore"), | |
531 | PROC_INACCESSIBLE("/proc/keys"), | |
532 | PROC_INACCESSIBLE("/proc/sysrq-trigger"), | |
533 | PROC_INACCESSIBLE("/proc/timer_list"), | |
534 | ||
535 | /* Make these directories read-only to container payloads: they show hardware information, and in some | |
536 | * cases contain tunables the container really shouldn't have access to. */ | |
537 | PROC_READ_ONLY("/proc/acpi"), | |
538 | PROC_READ_ONLY("/proc/apm"), | |
539 | PROC_READ_ONLY("/proc/asound"), | |
540 | PROC_READ_ONLY("/proc/bus"), | |
541 | PROC_READ_ONLY("/proc/fs"), | |
542 | PROC_READ_ONLY("/proc/irq"), | |
543 | PROC_READ_ONLY("/proc/scsi"), | |
544 | ||
545 | /* Then we list outer child mounts (i.e. mounts applied *before* entering user namespacing) */ | |
546 | { "tmpfs", "/tmp", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, | |
1099ceeb | 547 | MOUNT_FATAL|MOUNT_APPLY_TMPFS_TMP }, |
03d0f4b5 | 548 | { "tmpfs", "/sys", "tmpfs", "mode=555", MS_NOSUID|MS_NOEXEC|MS_NODEV, |
d4b653c5 LP |
549 | MOUNT_FATAL|MOUNT_APPLY_APIVFS_NETNS }, |
550 | { "sysfs", "/sys", "sysfs", NULL, MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
551 | MOUNT_FATAL|MOUNT_APPLY_APIVFS_RO }, /* skipped if above was mounted */ | |
552 | { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
553 | MOUNT_FATAL }, /* skipped if above was mounted */ | |
554 | { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID|MS_STRICTATIME, | |
555 | MOUNT_FATAL }, | |
556 | { "tmpfs", "/dev/shm", "tmpfs", "mode=1777", MS_NOSUID|MS_NODEV|MS_STRICTATIME, | |
557 | MOUNT_FATAL }, | |
558 | { "tmpfs", "/run", "tmpfs", "mode=755", MS_NOSUID|MS_NODEV|MS_STRICTATIME, | |
559 | MOUNT_FATAL }, | |
17c58ba9 LP |
560 | { "mqueue", "/dev/mqueue", "mqueue", NULL, 0, |
561 | MOUNT_FATAL }, | |
d4b653c5 | 562 | |
349cc4a5 | 563 | #if HAVE_SELINUX |
d4b653c5 LP |
564 | { "/sys/fs/selinux", "/sys/fs/selinux", NULL, NULL, MS_BIND, |
565 | 0 }, /* Bind mount first */ | |
566 | { NULL, "/sys/fs/selinux", NULL, NULL, MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, | |
567 | 0 }, /* Then, make it r/o */ | |
e83bebef LP |
568 | #endif |
569 | }; | |
570 | ||
d4b653c5 | 571 | _cleanup_(unlink_and_freep) char *inaccessible = NULL; |
4f086aab SU |
572 | bool use_userns = (mount_settings & MOUNT_USE_USERNS); |
573 | bool netns = (mount_settings & MOUNT_APPLY_APIVFS_NETNS); | |
574 | bool ro = (mount_settings & MOUNT_APPLY_APIVFS_RO); | |
575 | bool in_userns = (mount_settings & MOUNT_IN_USERNS); | |
1099ceeb | 576 | bool tmpfs_tmp = (mount_settings & MOUNT_APPLY_TMPFS_TMP); |
d4b653c5 | 577 | size_t k; |
88614c8a | 578 | int r; |
e83bebef LP |
579 | |
580 | for (k = 0; k < ELEMENTSOF(mount_table); k++) { | |
581 | _cleanup_free_ char *where = NULL, *options = NULL; | |
d4b653c5 | 582 | const char *o, *what; |
4f086aab SU |
583 | bool fatal = (mount_table[k].mount_settings & MOUNT_FATAL); |
584 | ||
585 | if (in_userns != (bool)(mount_table[k].mount_settings & MOUNT_IN_USERNS)) | |
586 | continue; | |
e83bebef | 587 | |
4f086aab | 588 | if (!netns && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_NETNS)) |
d1678248 ILG |
589 | continue; |
590 | ||
4f086aab | 591 | if (!ro && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_APIVFS_RO)) |
e83bebef LP |
592 | continue; |
593 | ||
1099ceeb LP |
594 | if (!tmpfs_tmp && (bool)(mount_table[k].mount_settings & MOUNT_APPLY_TMPFS_TMP)) |
595 | continue; | |
596 | ||
cb638b5e | 597 | r = chase_symlinks(mount_table[k].where, dest, CHASE_NONEXISTENT|CHASE_PREFIX_ROOT, &where); |
8ce48cf0 | 598 | if (r < 0) |
ec57bd42 | 599 | return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, mount_table[k].where); |
e83bebef | 600 | |
d4b653c5 LP |
601 | if (mount_table[k].mount_settings & MOUNT_INACCESSIBLE_REG) { |
602 | ||
603 | if (!inaccessible) { | |
604 | _cleanup_free_ char *np = NULL; | |
605 | ||
606 | r = tempfn_random_child(NULL, "inaccessible", &np); | |
607 | if (r < 0) | |
608 | return log_error_errno(r, "Failed to generate inaccessible file node path: %m"); | |
609 | ||
610 | r = touch_file(np, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0000); | |
611 | if (r < 0) | |
612 | return log_error_errno(r, "Failed to create inaccessible file node '%s': %m", np); | |
613 | ||
614 | inaccessible = TAKE_PTR(np); | |
615 | } | |
616 | ||
617 | what = inaccessible; | |
618 | } else | |
619 | what = mount_table[k].what; | |
620 | ||
8ce48cf0 | 621 | r = path_is_mount_point(where, NULL, 0); |
e83bebef LP |
622 | if (r < 0 && r != -ENOENT) |
623 | return log_error_errno(r, "Failed to detect whether %s is a mount point: %m", where); | |
624 | ||
625 | /* Skip this entry if it is not a remount. */ | |
d4b653c5 | 626 | if (what && r > 0) |
e83bebef LP |
627 | continue; |
628 | ||
9c0fad5f | 629 | r = mkdir_userns_p(dest, where, 0755, (use_userns && !in_userns) ? uid_shift : UID_INVALID); |
920a7899 | 630 | if (r < 0 && r != -EEXIST) { |
4f13e534 | 631 | if (fatal && r != -EROFS) |
e83bebef LP |
632 | return log_error_errno(r, "Failed to create directory %s: %m", where); |
633 | ||
201b13c8 | 634 | log_debug_errno(r, "Failed to create directory %s: %m", where); |
4f13e534 LT |
635 | /* If we failed mkdir() or chown() due to the root |
636 | * directory being read only, attempt to mount this fs | |
637 | * anyway and let mount_verbose log any errors */ | |
638 | if (r != -EROFS) | |
639 | continue; | |
e83bebef LP |
640 | } |
641 | ||
642 | o = mount_table[k].options; | |
643 | if (streq_ptr(mount_table[k].type, "tmpfs")) { | |
2fa017f1 | 644 | r = tmpfs_patch_options(o, in_userns ? 0 : uid_shift, selinux_apifs_context, &options); |
e83bebef LP |
645 | if (r < 0) |
646 | return log_oom(); | |
647 | if (r > 0) | |
648 | o = options; | |
649 | } | |
650 | ||
4f086aab | 651 | r = mount_verbose(fatal ? LOG_ERR : LOG_DEBUG, |
d4b653c5 | 652 | what, |
60e76d48 ZJS |
653 | where, |
654 | mount_table[k].type, | |
655 | mount_table[k].flags, | |
656 | o); | |
4f086aab | 657 | if (r < 0 && fatal) |
60e76d48 | 658 | return r; |
e83bebef LP |
659 | } |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
e83bebef | 664 | static int mount_bind(const char *dest, CustomMount *m) { |
68cf43c3 | 665 | |
72d967df | 666 | _cleanup_free_ char *where = NULL; |
68cf43c3 | 667 | struct stat source_st, dest_st; |
e83bebef LP |
668 | int r; |
669 | ||
86c0dd4a | 670 | assert(dest); |
e83bebef LP |
671 | assert(m); |
672 | ||
e83bebef LP |
673 | if (stat(m->source, &source_st) < 0) |
674 | return log_error_errno(errno, "Failed to stat %s: %m", m->source); | |
675 | ||
cb638b5e | 676 | r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where); |
68cf43c3 | 677 | if (r < 0) |
ec57bd42 | 678 | return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination); |
8ce48cf0 LP |
679 | if (r > 0) { /* Path exists already? */ |
680 | ||
681 | if (stat(where, &dest_st) < 0) | |
682 | return log_error_errno(errno, "Failed to stat %s: %m", where); | |
e83bebef | 683 | |
baaa35ad ZJS |
684 | if (S_ISDIR(source_st.st_mode) && !S_ISDIR(dest_st.st_mode)) |
685 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
686 | "Cannot bind mount directory %s on file %s.", | |
687 | m->source, where); | |
688 | ||
689 | if (!S_ISDIR(source_st.st_mode) && S_ISDIR(dest_st.st_mode)) | |
690 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
691 | "Cannot bind mount file %s on directory %s.", | |
692 | m->source, where); | |
e83bebef | 693 | |
8ce48cf0 | 694 | } else { /* Path doesn't exist yet? */ |
e83bebef LP |
695 | r = mkdir_parents_label(where, 0755); |
696 | if (r < 0) | |
697 | return log_error_errno(r, "Failed to make parents of %s: %m", where); | |
b97e83cb BN |
698 | |
699 | /* Create the mount point. Any non-directory file can be | |
700 | * mounted on any non-directory file (regular, fifo, socket, | |
701 | * char, block). | |
702 | */ | |
703 | if (S_ISDIR(source_st.st_mode)) | |
704 | r = mkdir_label(where, 0755); | |
705 | else | |
706 | r = touch(where); | |
707 | if (r < 0) | |
708 | return log_error_errno(r, "Failed to create mount point %s: %m", where); | |
709 | ||
8ce48cf0 | 710 | } |
e83bebef | 711 | |
72d967df | 712 | r = mount_verbose(LOG_ERR, m->source, where, NULL, MS_BIND | MS_REC, m->options); |
60e76d48 ZJS |
713 | if (r < 0) |
714 | return r; | |
e83bebef LP |
715 | |
716 | if (m->read_only) { | |
6b7c9f8b | 717 | r = bind_remount_recursive(where, true, NULL); |
e83bebef LP |
718 | if (r < 0) |
719 | return log_error_errno(r, "Read-only bind mount failed: %m"); | |
720 | } | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
725 | static int mount_tmpfs( | |
726 | const char *dest, | |
727 | CustomMount *m, | |
728 | bool userns, uid_t uid_shift, uid_t uid_range, | |
729 | const char *selinux_apifs_context) { | |
730 | ||
68cf43c3 LP |
731 | const char *options; |
732 | _cleanup_free_ char *buf = NULL, *where = NULL; | |
e83bebef LP |
733 | int r; |
734 | ||
735 | assert(dest); | |
736 | assert(m); | |
737 | ||
cb638b5e | 738 | r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where); |
68cf43c3 | 739 | if (r < 0) |
ec57bd42 | 740 | return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination); |
8ce48cf0 LP |
741 | if (r == 0) { /* Doesn't exist yet? */ |
742 | r = mkdir_p_label(where, 0755); | |
743 | if (r < 0) | |
744 | return log_error_errno(r, "Creating mount point for tmpfs %s failed: %m", where); | |
745 | } | |
e83bebef | 746 | |
2fa017f1 | 747 | r = tmpfs_patch_options(m->options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); |
e83bebef LP |
748 | if (r < 0) |
749 | return log_oom(); | |
750 | options = r > 0 ? buf : m->options; | |
751 | ||
60e76d48 | 752 | return mount_verbose(LOG_ERR, "tmpfs", where, "tmpfs", MS_NODEV|MS_STRICTATIME, options); |
e83bebef LP |
753 | } |
754 | ||
86c0dd4a | 755 | static char *joined_and_escaped_lower_dirs(char **lower) { |
e83bebef LP |
756 | _cleanup_strv_free_ char **sv = NULL; |
757 | ||
758 | sv = strv_copy(lower); | |
759 | if (!sv) | |
760 | return NULL; | |
761 | ||
762 | strv_reverse(sv); | |
763 | ||
764 | if (!strv_shell_escape(sv, ",:")) | |
765 | return NULL; | |
766 | ||
767 | return strv_join(sv, ":"); | |
768 | } | |
769 | ||
770 | static int mount_overlay(const char *dest, CustomMount *m) { | |
68cf43c3 | 771 | |
86c0dd4a | 772 | _cleanup_free_ char *lower = NULL, *where = NULL, *escaped_source = NULL; |
68cf43c3 | 773 | const char *options; |
e83bebef LP |
774 | int r; |
775 | ||
776 | assert(dest); | |
777 | assert(m); | |
778 | ||
cb638b5e | 779 | r = chase_symlinks(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where); |
68cf43c3 | 780 | if (r < 0) |
ec57bd42 | 781 | return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination); |
8ce48cf0 LP |
782 | if (r == 0) { /* Doesn't exist yet? */ |
783 | r = mkdir_label(where, 0755); | |
784 | if (r < 0) | |
785 | return log_error_errno(r, "Creating mount point for overlay %s failed: %m", where); | |
786 | } | |
e83bebef LP |
787 | |
788 | (void) mkdir_p_label(m->source, 0755); | |
789 | ||
790 | lower = joined_and_escaped_lower_dirs(m->lower); | |
791 | if (!lower) | |
792 | return log_oom(); | |
793 | ||
86c0dd4a LP |
794 | escaped_source = shell_escape(m->source, ",:"); |
795 | if (!escaped_source) | |
796 | return log_oom(); | |
e83bebef | 797 | |
86c0dd4a | 798 | if (m->read_only) |
e83bebef | 799 | options = strjoina("lowerdir=", escaped_source, ":", lower); |
86c0dd4a LP |
800 | else { |
801 | _cleanup_free_ char *escaped_work_dir = NULL; | |
e83bebef | 802 | |
e83bebef LP |
803 | escaped_work_dir = shell_escape(m->work_dir, ",:"); |
804 | if (!escaped_work_dir) | |
805 | return log_oom(); | |
806 | ||
807 | options = strjoina("lowerdir=", lower, ",upperdir=", escaped_source, ",workdir=", escaped_work_dir); | |
808 | } | |
809 | ||
60e76d48 | 810 | return mount_verbose(LOG_ERR, "overlay", where, "overlay", m->read_only ? MS_RDONLY : 0, options); |
e83bebef LP |
811 | } |
812 | ||
813 | int mount_custom( | |
814 | const char *dest, | |
88614c8a | 815 | CustomMount *mounts, size_t n, |
e83bebef LP |
816 | bool userns, uid_t uid_shift, uid_t uid_range, |
817 | const char *selinux_apifs_context) { | |
818 | ||
88614c8a | 819 | size_t i; |
e83bebef LP |
820 | int r; |
821 | ||
822 | assert(dest); | |
823 | ||
824 | for (i = 0; i < n; i++) { | |
825 | CustomMount *m = mounts + i; | |
826 | ||
827 | switch (m->type) { | |
828 | ||
829 | case CUSTOM_MOUNT_BIND: | |
830 | r = mount_bind(dest, m); | |
831 | break; | |
832 | ||
833 | case CUSTOM_MOUNT_TMPFS: | |
834 | r = mount_tmpfs(dest, m, userns, uid_shift, uid_range, selinux_apifs_context); | |
835 | break; | |
836 | ||
837 | case CUSTOM_MOUNT_OVERLAY: | |
838 | r = mount_overlay(dest, m); | |
839 | break; | |
840 | ||
841 | default: | |
842 | assert_not_reached("Unknown custom mount type"); | |
843 | } | |
844 | ||
845 | if (r < 0) | |
846 | return r; | |
847 | } | |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
e83bebef LP |
852 | int setup_volatile_state( |
853 | const char *directory, | |
854 | VolatileMode mode, | |
855 | bool userns, uid_t uid_shift, uid_t uid_range, | |
856 | const char *selinux_apifs_context) { | |
857 | ||
858 | _cleanup_free_ char *buf = NULL; | |
859 | const char *p, *options; | |
860 | int r; | |
861 | ||
862 | assert(directory); | |
863 | ||
864 | if (mode != VOLATILE_STATE) | |
865 | return 0; | |
866 | ||
867 | /* --volatile=state means we simply overmount /var | |
868 | with a tmpfs, and the rest read-only. */ | |
869 | ||
6b7c9f8b | 870 | r = bind_remount_recursive(directory, true, NULL); |
e83bebef LP |
871 | if (r < 0) |
872 | return log_error_errno(r, "Failed to remount %s read-only: %m", directory); | |
873 | ||
874 | p = prefix_roota(directory, "/var"); | |
875 | r = mkdir(p, 0755); | |
876 | if (r < 0 && errno != EEXIST) | |
877 | return log_error_errno(errno, "Failed to create %s: %m", directory); | |
878 | ||
879 | options = "mode=755"; | |
2fa017f1 | 880 | r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); |
e83bebef LP |
881 | if (r < 0) |
882 | return log_oom(); | |
883 | if (r > 0) | |
884 | options = buf; | |
885 | ||
60e76d48 | 886 | return mount_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options); |
e83bebef LP |
887 | } |
888 | ||
889 | int setup_volatile( | |
890 | const char *directory, | |
891 | VolatileMode mode, | |
892 | bool userns, uid_t uid_shift, uid_t uid_range, | |
893 | const char *selinux_apifs_context) { | |
894 | ||
895 | bool tmpfs_mounted = false, bind_mounted = false; | |
896 | char template[] = "/tmp/nspawn-volatile-XXXXXX"; | |
897 | _cleanup_free_ char *buf = NULL; | |
898 | const char *f, *t, *options; | |
899 | int r; | |
900 | ||
901 | assert(directory); | |
902 | ||
903 | if (mode != VOLATILE_YES) | |
904 | return 0; | |
905 | ||
906 | /* --volatile=yes means we mount a tmpfs to the root dir, and | |
907 | the original /usr to use inside it, and that read-only. */ | |
908 | ||
909 | if (!mkdtemp(template)) | |
910 | return log_error_errno(errno, "Failed to create temporary directory: %m"); | |
911 | ||
912 | options = "mode=755"; | |
2fa017f1 | 913 | r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf); |
e83bebef LP |
914 | if (r < 0) |
915 | return log_oom(); | |
916 | if (r > 0) | |
917 | options = buf; | |
918 | ||
60e76d48 ZJS |
919 | r = mount_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options); |
920 | if (r < 0) | |
e83bebef | 921 | goto fail; |
e83bebef LP |
922 | |
923 | tmpfs_mounted = true; | |
924 | ||
925 | f = prefix_roota(directory, "/usr"); | |
926 | t = prefix_roota(template, "/usr"); | |
927 | ||
928 | r = mkdir(t, 0755); | |
929 | if (r < 0 && errno != EEXIST) { | |
930 | r = log_error_errno(errno, "Failed to create %s: %m", t); | |
931 | goto fail; | |
932 | } | |
933 | ||
60e76d48 ZJS |
934 | r = mount_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL); |
935 | if (r < 0) | |
e83bebef | 936 | goto fail; |
e83bebef LP |
937 | |
938 | bind_mounted = true; | |
939 | ||
6b7c9f8b | 940 | r = bind_remount_recursive(t, true, NULL); |
e83bebef LP |
941 | if (r < 0) { |
942 | log_error_errno(r, "Failed to remount %s read-only: %m", t); | |
943 | goto fail; | |
944 | } | |
945 | ||
60e76d48 ZJS |
946 | r = mount_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL); |
947 | if (r < 0) | |
e83bebef | 948 | goto fail; |
e83bebef LP |
949 | |
950 | (void) rmdir(template); | |
951 | ||
952 | return 0; | |
953 | ||
954 | fail: | |
955 | if (bind_mounted) | |
60e76d48 | 956 | (void) umount_verbose(t); |
e83bebef LP |
957 | |
958 | if (tmpfs_mounted) | |
60e76d48 | 959 | (void) umount_verbose(template); |
e83bebef LP |
960 | (void) rmdir(template); |
961 | return r; | |
962 | } | |
b53ede69 PW |
963 | |
964 | /* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */ | |
965 | int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) { | |
966 | _cleanup_free_ char *root_new = NULL, *root_old = NULL; | |
967 | const char *p = s; | |
968 | int r; | |
969 | ||
970 | assert(pivot_root_new); | |
971 | assert(pivot_root_old); | |
972 | ||
973 | r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS); | |
974 | if (r < 0) | |
975 | return r; | |
976 | if (r == 0) | |
977 | return -EINVAL; | |
978 | ||
979 | if (isempty(p)) | |
980 | root_old = NULL; | |
981 | else { | |
982 | root_old = strdup(p); | |
983 | if (!root_old) | |
984 | return -ENOMEM; | |
985 | } | |
986 | ||
987 | if (!path_is_absolute(root_new)) | |
988 | return -EINVAL; | |
989 | if (root_old && !path_is_absolute(root_old)) | |
990 | return -EINVAL; | |
991 | ||
992 | free_and_replace(*pivot_root_new, root_new); | |
993 | free_and_replace(*pivot_root_old, root_old); | |
994 | ||
995 | return 0; | |
996 | } | |
997 | ||
998 | int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) { | |
999 | _cleanup_free_ char *directory_pivot_root_new = NULL; | |
1000 | _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL; | |
1001 | char pivot_tmp[] = "/tmp/nspawn-pivot-XXXXXX"; | |
1002 | bool remove_pivot_tmp = false; | |
1003 | int r; | |
1004 | ||
1005 | assert(directory); | |
1006 | ||
1007 | if (!pivot_root_new) | |
1008 | return 0; | |
1009 | ||
1010 | /* Pivot pivot_root_new to / and the existing / to pivot_root_old. | |
1011 | * If pivot_root_old is NULL, the existing / disappears. | |
1012 | * This requires a temporary directory, pivot_tmp, which is | |
1013 | * not a child of either. | |
1014 | * | |
1015 | * This is typically used for OSTree-style containers, where | |
1016 | * the root partition contains several sysroots which could be | |
1017 | * run. Normally, one would be chosen by the bootloader and | |
1018 | * pivoted to / by initramfs. | |
1019 | * | |
1020 | * For example, for an OSTree deployment, pivot_root_new | |
1021 | * would be: /ostree/deploy/$os/deploy/$checksum. Note that this | |
1022 | * code doesn’t do the /var mount which OSTree expects: use | |
1023 | * --bind +/sysroot/ostree/deploy/$os/var:/var for that. | |
1024 | * | |
1025 | * So in the OSTree case, we’ll end up with something like: | |
1026 | * - directory = /tmp/nspawn-root-123456 | |
1027 | * - pivot_root_new = /ostree/deploy/os/deploy/123abc | |
1028 | * - pivot_root_old = /sysroot | |
1029 | * - directory_pivot_root_new = | |
1030 | * /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc | |
1031 | * - pivot_tmp = /tmp/nspawn-pivot-123456 | |
1032 | * - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot | |
1033 | * | |
1034 | * Requires all file systems at directory and below to be mounted | |
1035 | * MS_PRIVATE or MS_SLAVE so they can be moved. | |
1036 | */ | |
1037 | directory_pivot_root_new = prefix_root(directory, pivot_root_new); | |
1038 | ||
1039 | /* Remount directory_pivot_root_new to make it movable. */ | |
1040 | r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL); | |
1041 | if (r < 0) | |
1042 | goto done; | |
1043 | ||
1044 | if (pivot_root_old) { | |
1045 | if (!mkdtemp(pivot_tmp)) { | |
1046 | r = log_error_errno(errno, "Failed to create temporary directory: %m"); | |
1047 | goto done; | |
1048 | } | |
1049 | ||
1050 | remove_pivot_tmp = true; | |
1051 | pivot_tmp_pivot_root_old = prefix_root(pivot_tmp, pivot_root_old); | |
1052 | ||
1053 | r = mount_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL); | |
1054 | if (r < 0) | |
1055 | goto done; | |
1056 | ||
1057 | r = mount_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL); | |
1058 | if (r < 0) | |
1059 | goto done; | |
1060 | ||
1061 | r = mount_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL); | |
1062 | if (r < 0) | |
1063 | goto done; | |
1064 | } else { | |
1065 | r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL); | |
1066 | if (r < 0) | |
1067 | goto done; | |
1068 | } | |
1069 | ||
1070 | done: | |
1071 | if (remove_pivot_tmp) | |
1072 | (void) rmdir(pivot_tmp); | |
1073 | ||
1074 | return r; | |
1075 | } |