]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
6b1dc2bd | 2 | |
6b1dc2bd | 3 | #include <errno.h> |
07630cea | 4 | #include <stdio.h> |
6b1dc2bd LP |
5 | #include <unistd.h> |
6 | ||
b5efdb8a | 7 | #include "alloc-util.h" |
028a981c ZJS |
8 | #include "bus-error.h" |
9 | #include "bus-locator.h" | |
a9399358 | 10 | #include "bus-unit-util.h" |
f461a28d | 11 | #include "chase.h" |
6ac62485 | 12 | #include "creds-util.h" |
6c51b49c | 13 | #include "efi-loader.h" |
905dd992 | 14 | #include "env-util.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
0d39fa9c | 16 | #include "fileio.h" |
d15d0333 | 17 | #include "fstab-util.h" |
07630cea | 18 | #include "generator.h" |
77b8e92d | 19 | #include "in-addr-util.h" |
baa6a42d | 20 | #include "initrd-util.h" |
07630cea | 21 | #include "log.h" |
a4ef3e4d | 22 | #include "main-func.h" |
07630cea | 23 | #include "mkdir.h" |
6b1dc2bd | 24 | #include "mount-setup.h" |
4349cd7c | 25 | #include "mount-util.h" |
049af8ad | 26 | #include "mountpoint-util.h" |
55365b0a | 27 | #include "nulstr-util.h" |
6bedfcbb | 28 | #include "parse-util.h" |
07630cea | 29 | #include "path-util.h" |
4e731273 | 30 | #include "proc-cmdline.h" |
028a981c | 31 | #include "process-util.h" |
6b1dc2bd | 32 | #include "special.h" |
98bad05e | 33 | #include "specifier.h" |
8fcde012 | 34 | #include "stat-util.h" |
07630cea | 35 | #include "string-util.h" |
059cb385 | 36 | #include "strv.h" |
07630cea | 37 | #include "unit-name.h" |
689aede8 | 38 | #include "virt.h" |
91214a37 | 39 | #include "volatile-util.h" |
6b1dc2bd | 40 | |
d2194e15 LP |
41 | typedef enum MountPointFlags { |
42 | MOUNT_NOAUTO = 1 << 0, | |
43 | MOUNT_NOFAIL = 1 << 1, | |
44 | MOUNT_AUTOMOUNT = 1 << 2, | |
45 | MOUNT_MAKEFS = 1 << 3, | |
46 | MOUNT_GROWFS = 1 << 4, | |
47 | MOUNT_RW_ONLY = 1 << 5, | |
04959faa | 48 | MOUNT_PCRFS = 1 << 6, |
fc5c6ecc | 49 | MOUNT_QUOTA = 1 << 7, |
d2194e15 | 50 | } MountPointFlags; |
4191418b | 51 | |
55365b0a | 52 | typedef struct Mount { |
45c535dd | 53 | bool for_initrd; |
55365b0a YW |
54 | char *what; |
55 | char *where; | |
56 | char *fstype; | |
57 | char *options; | |
58 | } Mount; | |
59 | ||
60 | static void mount_array_free(Mount *mounts, size_t n); | |
61 | ||
028a981c | 62 | static bool arg_sysroot_check = false; |
7a44c7e3 ZJS |
63 | static const char *arg_dest = NULL; |
64 | static const char *arg_dest_late = NULL; | |
e48fdd84 | 65 | static bool arg_fstab_enabled = true; |
567a5307 | 66 | static bool arg_swap_enabled = true; |
6db615c1 LP |
67 | static char *arg_root_what = NULL; |
68 | static char *arg_root_fstype = NULL; | |
69 | static char *arg_root_options = NULL; | |
2f3dfc6f | 70 | static char *arg_root_hash = NULL; |
6db615c1 | 71 | static int arg_root_rw = -1; |
9f103625 TH |
72 | static char *arg_usr_what = NULL; |
73 | static char *arg_usr_fstype = NULL; | |
74 | static char *arg_usr_options = NULL; | |
c1b9e3df | 75 | static char *arg_usr_hash = NULL; |
91214a37 | 76 | static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID; |
200268c6 | 77 | static bool arg_verity = true; |
55365b0a YW |
78 | static Mount *arg_mounts = NULL; |
79 | static size_t arg_n_mounts = 0; | |
6b1dc2bd | 80 | |
a4ef3e4d YW |
81 | STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep); |
82 | STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep); | |
83 | STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep); | |
84 | STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep); | |
85 | STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep); | |
86 | STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep); | |
87 | STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep); | |
c1b9e3df | 88 | STATIC_DESTRUCTOR_REGISTER(arg_usr_hash, freep); |
55365b0a YW |
89 | STATIC_ARRAY_DESTRUCTOR_REGISTER(arg_mounts, arg_n_mounts, mount_array_free); |
90 | ||
91 | static void mount_done(Mount *m) { | |
92 | assert(m); | |
93 | ||
94 | free(m->what); | |
95 | free(m->where); | |
96 | free(m->fstype); | |
97 | free(m->options); | |
98 | } | |
99 | ||
100 | static void mount_array_free(Mount *mounts, size_t n) { | |
101 | FOREACH_ARRAY(m, mounts, n) | |
102 | mount_done(m); | |
103 | ||
104 | free(mounts); | |
105 | } | |
106 | ||
45c535dd YW |
107 | static int mount_array_add_internal( |
108 | bool for_initrd, | |
109 | char *in_what, | |
110 | char *in_where, | |
111 | const char *in_fstype, | |
112 | const char *in_options) { | |
113 | ||
55365b0a YW |
114 | _cleanup_free_ char *what = NULL, *where = NULL, *fstype = NULL, *options = NULL; |
115 | int r; | |
116 | ||
117 | /* This takes what and where. */ | |
118 | ||
119 | what = ASSERT_PTR(in_what); | |
120 | where = in_where; | |
121 | ||
122 | fstype = strdup(isempty(in_fstype) ? "auto" : in_fstype); | |
123 | if (!fstype) | |
124 | return -ENOMEM; | |
125 | ||
126 | if (streq(fstype, "swap")) | |
127 | where = mfree(where); | |
128 | ||
129 | if (!isempty(in_options)) { | |
130 | _cleanup_strv_free_ char **options_strv = NULL; | |
131 | ||
132 | r = strv_split_full(&options_strv, in_options, ",", 0); | |
133 | if (r < 0) | |
134 | return r; | |
135 | ||
136 | r = strv_make_nulstr(options_strv, &options, NULL); | |
137 | } else | |
138 | r = strv_make_nulstr(STRV_MAKE("defaults"), &options, NULL); | |
139 | if (r < 0) | |
140 | return r; | |
141 | ||
142 | if (!GREEDY_REALLOC(arg_mounts, arg_n_mounts + 1)) | |
143 | return -ENOMEM; | |
144 | ||
145 | arg_mounts[arg_n_mounts++] = (Mount) { | |
45c535dd | 146 | .for_initrd = for_initrd, |
55365b0a YW |
147 | .what = TAKE_PTR(what), |
148 | .where = TAKE_PTR(where), | |
149 | .fstype = TAKE_PTR(fstype), | |
150 | .options = TAKE_PTR(options), | |
151 | }; | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
45c535dd | 156 | static int mount_array_add(bool for_initrd, const char *str) { |
55365b0a YW |
157 | _cleanup_free_ char *what = NULL, *where = NULL, *fstype = NULL, *options = NULL; |
158 | int r; | |
159 | ||
160 | assert(str); | |
161 | ||
162 | r = extract_many_words(&str, ":", EXTRACT_CUNESCAPE | EXTRACT_DONT_COALESCE_SEPARATORS, | |
4f495126 | 163 | &what, &where, &fstype, &options); |
55365b0a YW |
164 | if (r < 0) |
165 | return r; | |
166 | if (r < 2) | |
167 | return -EINVAL; | |
168 | if (!isempty(str)) | |
169 | return -EINVAL; | |
170 | ||
45c535dd | 171 | return mount_array_add_internal(for_initrd, TAKE_PTR(what), TAKE_PTR(where), fstype, options); |
55365b0a YW |
172 | } |
173 | ||
45c535dd | 174 | static int mount_array_add_swap(bool for_initrd, const char *str) { |
55365b0a YW |
175 | _cleanup_free_ char *what = NULL, *options = NULL; |
176 | int r; | |
177 | ||
178 | assert(str); | |
179 | ||
180 | r = extract_many_words(&str, ":", EXTRACT_CUNESCAPE | EXTRACT_DONT_COALESCE_SEPARATORS, | |
4f495126 | 181 | &what, &options); |
55365b0a YW |
182 | if (r < 0) |
183 | return r; | |
184 | if (r < 1) | |
185 | return -EINVAL; | |
186 | if (!isempty(str)) | |
187 | return -EINVAL; | |
188 | ||
45c535dd | 189 | return mount_array_add_internal(for_initrd, TAKE_PTR(what), NULL, "swap", options); |
55365b0a | 190 | } |
a4ef3e4d | 191 | |
d5cc4be2 LP |
192 | static int write_options(FILE *f, const char *options) { |
193 | _cleanup_free_ char *o = NULL; | |
194 | ||
27db64bc MY |
195 | assert(f); |
196 | ||
d5cc4be2 LP |
197 | if (isempty(options)) |
198 | return 0; | |
199 | ||
200 | if (streq(options, "defaults")) | |
201 | return 0; | |
202 | ||
98bad05e | 203 | o = specifier_escape(options); |
d5cc4be2 LP |
204 | if (!o) |
205 | return log_oom(); | |
206 | ||
207 | fprintf(f, "Options=%s\n", o); | |
208 | return 1; | |
209 | } | |
210 | ||
19d0833b LP |
211 | static int write_what(FILE *f, const char *what) { |
212 | _cleanup_free_ char *w = NULL; | |
213 | ||
27db64bc MY |
214 | assert(f); |
215 | assert(what); | |
216 | ||
98bad05e | 217 | w = specifier_escape(what); |
19d0833b LP |
218 | if (!w) |
219 | return log_oom(); | |
220 | ||
221 | fprintf(f, "What=%s\n", w); | |
222 | return 1; | |
223 | } | |
224 | ||
5607d856 | 225 | static int add_swap( |
7772c177 | 226 | const char *source, |
5607d856 | 227 | const char *what, |
cfeb4d37 | 228 | const char *options, |
d2194e15 | 229 | MountPointFlags flags) { |
5607d856 | 230 | |
fb883e75 | 231 | _cleanup_free_ char *name = NULL; |
7fd1b19b | 232 | _cleanup_fclose_ FILE *f = NULL; |
bf1d7ba7 | 233 | int r; |
6b1dc2bd LP |
234 | |
235 | assert(what); | |
6b1dc2bd | 236 | |
00b4ffde | 237 | if (access("/proc/swaps", F_OK) < 0) { |
55365b0a | 238 | log_info("Swap not supported, ignoring swap entry for %s.", what); |
00b4ffde LP |
239 | return 0; |
240 | } | |
241 | ||
75f86906 | 242 | if (detect_container() > 0) { |
55365b0a | 243 | log_info("Running in a container, ignoring swap entry for %s.", what); |
689aede8 LP |
244 | return 0; |
245 | } | |
246 | ||
028a981c ZJS |
247 | if (arg_sysroot_check) { |
248 | log_info("%s should be enabled in the initrd, will request daemon-reload.", what); | |
249 | return true; | |
250 | } | |
251 | ||
256604cc YW |
252 | log_debug("Found swap entry what=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s", |
253 | what, | |
254 | yes_no(flags & MOUNT_MAKEFS), yes_no(flags & MOUNT_GROWFS), yes_no(flags & MOUNT_PCRFS), | |
255 | yes_no(flags & MOUNT_NOAUTO), yes_no(flags & MOUNT_NOFAIL)); | |
256 | ||
7410616c LP |
257 | r = unit_name_from_path(what, ".swap", &name); |
258 | if (r < 0) | |
259 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
6b1dc2bd | 260 | |
7772c177 | 261 | r = generator_open_unit_file(arg_dest, source, name, &f); |
fb883e75 ZJS |
262 | if (r < 0) |
263 | return r; | |
0d536673 | 264 | |
ed4ad488 ZJS |
265 | fprintf(f, |
266 | "[Unit]\n" | |
a7e88558 LP |
267 | "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n" |
268 | "SourcePath=%s\n", | |
7772c177 | 269 | source); |
19d0833b | 270 | |
a7e88558 LP |
271 | r = generator_write_blockdev_dependency(f, what); |
272 | if (r < 0) | |
273 | return r; | |
274 | ||
275 | fprintf(f, | |
276 | "\n" | |
277 | "[Swap]\n"); | |
278 | ||
19d0833b LP |
279 | r = write_what(f, what); |
280 | if (r < 0) | |
281 | return r; | |
6b1dc2bd | 282 | |
cfeb4d37 | 283 | r = write_options(f, options); |
d5cc4be2 LP |
284 | if (r < 0) |
285 | return r; | |
6b1dc2bd | 286 | |
47cb901e | 287 | r = fflush_and_check(f); |
23bbb0de | 288 | if (r < 0) |
fb883e75 | 289 | return log_error_errno(r, "Failed to write unit file %s: %m", name); |
6b1dc2bd | 290 | |
b3208b66 | 291 | /* use what as where, to have a nicer error message */ |
cfeb4d37 | 292 | r = generator_write_timeouts(arg_dest, what, what, options, NULL); |
b3208b66 ZJS |
293 | if (r < 0) |
294 | return r; | |
295 | ||
d2194e15 | 296 | if (flags & MOUNT_MAKEFS) { |
da495a03 ZJS |
297 | r = generator_hook_up_mkswap(arg_dest, what); |
298 | if (r < 0) | |
299 | return r; | |
300 | } | |
301 | ||
d2194e15 | 302 | if (flags & MOUNT_GROWFS) |
7f2806d5 ZJS |
303 | /* TODO: swap devices must be wiped and recreated */ |
304 | log_warning("%s: growing swap devices is currently unsupported.", what); | |
04959faa LP |
305 | if (flags & MOUNT_PCRFS) |
306 | log_warning("%s: measuring swap devices is currently unsupported.", what); | |
7f2806d5 | 307 | |
d2194e15 | 308 | if (!(flags & MOUNT_NOAUTO)) { |
630d30d3 | 309 | r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, |
d2194e15 | 310 | (flags & MOUNT_NOFAIL) ? "wants" : "requires", name); |
630d30d3 ZJS |
311 | if (r < 0) |
312 | return r; | |
4e82fe52 TG |
313 | } |
314 | ||
028a981c | 315 | return true; |
6b1dc2bd LP |
316 | } |
317 | ||
cfeb4d37 YW |
318 | static bool mount_is_network(const char *fstype, const char *options) { |
319 | return fstab_test_option(options, "_netdev\0") || | |
320 | (fstype && fstype_is_network(fstype)); | |
6b1dc2bd LP |
321 | } |
322 | ||
22f5a825 | 323 | static bool mount_in_initrd(const char *where, const char *options, bool accept_root) { |
cfeb4d37 | 324 | return fstab_test_option(options, "x-initrd.mount\0") || |
22f5a825 | 325 | (where && PATH_IN_SET(where, "/usr", accept_root ? "/" : NULL)); |
3d22d1ab TG |
326 | } |
327 | ||
33a4c983 LP |
328 | static int write_timeout( |
329 | FILE *f, | |
330 | const char *where, | |
331 | const char *opts, | |
332 | const char *filter, | |
74467890 | 333 | const char *unit_setting) { |
33a4c983 | 334 | |
deb0a77c | 335 | _cleanup_free_ char *timeout = NULL; |
deb0a77c MO |
336 | usec_t u; |
337 | int r; | |
338 | ||
74467890 MY |
339 | assert(f); |
340 | assert(where); | |
341 | assert(filter); | |
342 | assert(unit_setting); | |
343 | ||
ff0c31bc | 344 | r = fstab_filter_options(opts, filter, NULL, &timeout, NULL, NULL); |
deb0a77c | 345 | if (r < 0) |
74467890 | 346 | return log_error_errno(r, "Failed to parse options for '%s': %m", where); |
deb0a77c MO |
347 | if (r == 0) |
348 | return 0; | |
349 | ||
0004f698 | 350 | r = parse_sec_fix_0(timeout, &u); |
deb0a77c | 351 | if (r < 0) { |
74467890 | 352 | log_warning_errno(r, "Failed to parse timeout '%s' for '%s', ignoring: %m", timeout, where); |
deb0a77c MO |
353 | return 0; |
354 | } | |
355 | ||
74467890 | 356 | fprintf(f, "%s=%s\n", unit_setting, FORMAT_TIMESPAN(u, 0)); |
deb0a77c MO |
357 | |
358 | return 0; | |
359 | } | |
2e852276 | 360 | |
110773f6 CH |
361 | static int write_idle_timeout(FILE *f, const char *where, const char *opts) { |
362 | return write_timeout(f, where, opts, | |
363 | "x-systemd.idle-timeout\0", "TimeoutIdleSec"); | |
364 | } | |
365 | ||
366 | static int write_mount_timeout(FILE *f, const char *where, const char *opts) { | |
367 | return write_timeout(f, where, opts, | |
368 | "x-systemd.mount-timeout\0", "TimeoutSec"); | |
369 | } | |
370 | ||
33a4c983 LP |
371 | static int write_dependency( |
372 | FILE *f, | |
74467890 | 373 | const char *where, |
33a4c983 LP |
374 | const char *opts, |
375 | const char *filter, | |
74467890 | 376 | const char* const *unit_settings) { |
33a4c983 | 377 | |
74467890 MY |
378 | _cleanup_strv_free_ char **unit_names = NULL; |
379 | _cleanup_free_ char *units = NULL; | |
3519d230 KZ |
380 | int r; |
381 | ||
382 | assert(f); | |
74467890 MY |
383 | assert(filter); |
384 | assert(unit_settings); | |
3519d230 | 385 | |
74467890 | 386 | r = fstab_filter_options(opts, filter, NULL, NULL, &unit_names, NULL); |
3519d230 | 387 | if (r < 0) |
74467890 | 388 | return log_error_errno(r, "Failed to parse options for '%s': %m", where); |
3519d230 KZ |
389 | if (r == 0) |
390 | return 0; | |
391 | ||
74467890 MY |
392 | STRV_FOREACH(s, unit_names) { |
393 | _cleanup_free_ char *mangled = NULL; | |
3519d230 | 394 | |
74467890 | 395 | r = unit_name_mangle_with_suffix(*s, "as dependency", 0, ".mount", &mangled); |
3519d230 | 396 | if (r < 0) |
74467890 | 397 | return log_error_errno(r, "Failed to generate dependency unit name for '%s': %m", where); |
33a4c983 | 398 | |
74467890 | 399 | if (!strextend_with_separator(&units, " ", mangled)) |
3519d230 KZ |
400 | return log_oom(); |
401 | } | |
402 | ||
74467890 MY |
403 | STRV_FOREACH(setting, unit_settings) |
404 | fprintf(f, "%s=%s\n", *setting, units); | |
3519d230 KZ |
405 | |
406 | return 0; | |
407 | } | |
408 | ||
74467890 MY |
409 | static int write_after(FILE *f, const char *where, const char *opts) { |
410 | return write_dependency(f, where, opts, | |
411 | "x-systemd.after\0", STRV_MAKE_CONST("After")); | |
ae325185 RB |
412 | } |
413 | ||
74467890 MY |
414 | static int write_requires_after(FILE *f, const char *where, const char *opts) { |
415 | return write_dependency(f, where, opts, | |
416 | "x-systemd.requires\0", STRV_MAKE_CONST("Requires", "After")); | |
ae325185 RB |
417 | } |
418 | ||
74467890 MY |
419 | static int write_before(FILE *f, const char *where, const char *opts) { |
420 | return write_dependency(f, where, opts, | |
421 | "x-systemd.before\0", STRV_MAKE_CONST("Before")); | |
ae325185 RB |
422 | } |
423 | ||
74467890 MY |
424 | static int write_mounts_for( |
425 | FILE *f, | |
426 | const char *where, | |
427 | const char *opts, | |
428 | const char *filter, | |
429 | const char *unit_setting) { | |
430 | ||
98bad05e | 431 | _cleanup_strv_free_ char **paths = NULL, **paths_escaped = NULL; |
3519d230 KZ |
432 | int r; |
433 | ||
434 | assert(f); | |
74467890 MY |
435 | assert(where); |
436 | assert(filter); | |
437 | assert(unit_setting); | |
3519d230 | 438 | |
74467890 | 439 | r = fstab_filter_options(opts, filter, NULL, NULL, &paths, NULL); |
3519d230 | 440 | if (r < 0) |
74467890 | 441 | return log_error_errno(r, "Failed to parse options for '%s': %m", where); |
3519d230 KZ |
442 | if (r == 0) |
443 | return 0; | |
444 | ||
98bad05e LP |
445 | r = specifier_escape_strv(paths, &paths_escaped); |
446 | if (r < 0) | |
74467890 | 447 | return log_error_errno(r, "Failed to escape paths for '%s': %m", where); |
98bad05e | 448 | |
74467890 MY |
449 | fprintf(f, "%s=", unit_setting); |
450 | fputstrv(f, paths_escaped, NULL, NULL); | |
451 | fputc('\n', f); | |
3519d230 KZ |
452 | |
453 | return 0; | |
454 | } | |
455 | ||
74467890 | 456 | static int write_extra_dependencies(FILE *f, const char *where, const char *opts) { |
6371e69b FB |
457 | int r; |
458 | ||
459 | assert(f); | |
460 | ||
74467890 MY |
461 | if (isempty(opts)) |
462 | return 0; | |
463 | ||
464 | r = write_after(f, where, opts); | |
465 | if (r < 0) | |
466 | return r; | |
467 | ||
468 | r = write_requires_after(f, where, opts); | |
469 | if (r < 0) | |
470 | return r; | |
471 | ||
472 | r = write_before(f, where, opts); | |
473 | if (r < 0) | |
474 | return r; | |
475 | ||
476 | r = write_mounts_for(f, where, opts, | |
477 | "x-systemd.requires-mounts-for\0", "RequiresMountsFor"); | |
478 | if (r < 0) | |
479 | return r; | |
480 | ||
481 | r = write_mounts_for(f, where, opts, | |
482 | "x-systemd.wants-mounts-for\0", "WantsMountsFor"); | |
483 | if (r < 0) | |
484 | return r; | |
6371e69b FB |
485 | |
486 | return 0; | |
487 | } | |
488 | ||
b3ee0148 MY |
489 | static int mandatory_mount_drop_unapplicable_options( |
490 | MountPointFlags *flags, | |
491 | const char *where, | |
492 | const char *options, | |
493 | char **ret_options) { | |
494 | ||
495 | int r; | |
496 | ||
497 | assert(flags); | |
498 | assert(where); | |
b3ee0148 MY |
499 | assert(ret_options); |
500 | ||
6a705f12 ZJS |
501 | if (!(*flags & (MOUNT_NOAUTO|MOUNT_NOFAIL|MOUNT_AUTOMOUNT))) |
502 | return strdup_to(ret_options, options); | |
b3ee0148 MY |
503 | |
504 | log_debug("Mount '%s' is mandatory, ignoring 'noauto', 'nofail', and 'x-systemd.automount' options.", | |
505 | where); | |
506 | ||
507 | *flags &= ~(MOUNT_NOAUTO|MOUNT_NOFAIL|MOUNT_AUTOMOUNT); | |
508 | ||
509 | r = fstab_filter_options(options, "noauto\0nofail\0x-systemd.automount\0", NULL, NULL, NULL, ret_options); | |
510 | if (r < 0) | |
511 | return r; | |
512 | ||
513 | return 1; | |
514 | } | |
515 | ||
e8d2f6cd | 516 | static int add_mount( |
7772c177 | 517 | const char *source, |
91214a37 | 518 | const char *dest, |
e8d2f6cd LP |
519 | const char *what, |
520 | const char *where, | |
634735b5 | 521 | const char *original_where, |
6db615c1 | 522 | const char *fstype, |
e8d2f6cd LP |
523 | const char *opts, |
524 | int passno, | |
d2194e15 | 525 | MountPointFlags flags, |
da69e8e4 | 526 | const char *target_unit) { |
e48fdd84 | 527 | |
b3ee0148 MY |
528 | _cleanup_free_ char *name = NULL, *automount_name = NULL, *filtered = NULL, *where_escaped = NULL, |
529 | *opts_root_filtered = NULL; | |
be02c1cf | 530 | _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL; |
7fd1b19b | 531 | _cleanup_fclose_ FILE *f = NULL; |
94192cda | 532 | int r; |
6b1dc2bd LP |
533 | |
534 | assert(what); | |
535 | assert(where); | |
da69e8e4 | 536 | assert(target_unit); |
5e398e54 | 537 | assert(source); |
6b1dc2bd | 538 | |
6db615c1 | 539 | if (streq_ptr(fstype, "autofs")) |
6b1dc2bd LP |
540 | return 0; |
541 | ||
542 | if (!is_path(where)) { | |
543 | log_warning("Mount point %s is not a valid path, ignoring.", where); | |
544 | return 0; | |
545 | } | |
546 | ||
547 | if (mount_point_is_api(where) || | |
548 | mount_point_ignore(where)) | |
549 | return 0; | |
550 | ||
028a981c ZJS |
551 | if (arg_sysroot_check) { |
552 | log_info("%s should be mounted in the initrd, will request daemon-reload.", where); | |
553 | return true; | |
554 | } | |
555 | ||
d6cef552 | 556 | r = fstab_filter_options(opts, "x-systemd.wanted-by\0", NULL, NULL, &wanted_by, NULL); |
be02c1cf AR |
557 | if (r < 0) |
558 | return r; | |
559 | ||
d6cef552 | 560 | r = fstab_filter_options(opts, "x-systemd.required-by\0", NULL, NULL, &required_by, NULL); |
be02c1cf AR |
561 | if (r < 0) |
562 | return r; | |
563 | ||
9f6c32ac | 564 | if (PATH_IN_SET(where, "/", "/usr")) { |
b3ee0148 MY |
565 | r = mandatory_mount_drop_unapplicable_options(&flags, where, opts, &opts_root_filtered); |
566 | if (r < 0) | |
567 | return r; | |
568 | opts = opts_root_filtered; | |
569 | ||
be02c1cf | 570 | if (!strv_isempty(wanted_by)) |
9f6c32ac | 571 | log_debug("Ignoring 'x-systemd.wanted-by=' option for root/usr device."); |
be02c1cf | 572 | if (!strv_isempty(required_by)) |
9f6c32ac | 573 | log_debug("Ignoring 'x-systemd.required-by=' option for root/usr device."); |
2e852276 | 574 | |
be02c1cf AR |
575 | required_by = strv_free(required_by); |
576 | wanted_by = strv_free(wanted_by); | |
e48fdd84 LP |
577 | } |
578 | ||
7410616c LP |
579 | r = unit_name_from_path(where, ".mount", &name); |
580 | if (r < 0) | |
581 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
6b1dc2bd | 582 | |
7772c177 | 583 | r = generator_open_unit_file(dest, source, name, &f); |
fb883e75 ZJS |
584 | if (r < 0) |
585 | return r; | |
0d536673 | 586 | |
5e398e54 | 587 | fprintf(f, |
c3834f9b | 588 | "[Unit]\n" |
a7e88558 LP |
589 | "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n" |
590 | "SourcePath=%s\n", | |
c3834f9b | 591 | source); |
6b1dc2bd | 592 | |
d2194e15 | 593 | if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & MOUNT_AUTOMOUNT) && |
65e1dee7 N |
594 | fstab_test_yes_no_option(opts, "bg\0" "fg\0")) { |
595 | /* The default retry timeout that mount.nfs uses for 'bg' mounts | |
596 | * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts. | |
597 | * As we are making 'bg' mounts look like an 'fg' mount to | |
598 | * mount.nfs (so systemd can manage the job-control aspects of 'bg'), | |
599 | * we need to explicitly preserve that default, and also ensure | |
600 | * the systemd mount-timeout doesn't interfere. | |
5a12d1ca | 601 | * By placing these options first, they can be overridden by |
65e1dee7 | 602 | * settings in /etc/fstab. */ |
e66d2eee | 603 | opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,nofail,", opts, ",fg"); |
d2194e15 | 604 | SET_FLAG(flags, MOUNT_NOFAIL, true); |
65e1dee7 N |
605 | } |
606 | ||
39a4c452 MY |
607 | if (!strv_isempty(wanted_by) || !strv_isempty(required_by)) { |
608 | /* If x-systemd.{wanted,required}-by= is specified, target_unit is not used */ | |
609 | target_unit = NULL; | |
610 | ||
611 | /* Don't set default ordering dependencies on local-fs.target or remote-fs.target, but we | |
612 | * still need to conflict with umount.target. */ | |
613 | fputs("DefaultDependencies=no\n" | |
614 | "Conflicts=umount.target\n" | |
615 | "Before=umount.target\n", | |
616 | f); | |
617 | } | |
618 | ||
74467890 | 619 | r = write_extra_dependencies(f, where, opts); |
6371e69b FB |
620 | if (r < 0) |
621 | return r; | |
3519d230 | 622 | |
da69e8e4 | 623 | /* Order the mount unit we generate relative to target_unit, so that DefaultDependencies= on the |
fa138f5e | 624 | * target unit won't affect us. */ |
39a4c452 | 625 | if (target_unit && !FLAGS_SET(flags, MOUNT_NOFAIL)) |
da69e8e4 | 626 | fprintf(f, "Before=%s\n", target_unit); |
fa138f5e | 627 | |
e48fdd84 | 628 | if (passno != 0) { |
91214a37 | 629 | r = generator_write_fsck_deps(f, dest, what, where, fstype); |
e48fdd84 LP |
630 | if (r < 0) |
631 | return r; | |
632 | } | |
64e70e4b | 633 | |
a7e88558 LP |
634 | r = generator_write_blockdev_dependency(f, what); |
635 | if (r < 0) | |
636 | return r; | |
637 | ||
638 | fprintf(f, | |
639 | "\n" | |
640 | "[Mount]\n"); | |
641 | ||
b2efed52 ZJS |
642 | r = write_what(f, what); |
643 | if (r < 0) | |
644 | return r; | |
645 | ||
634735b5 CW |
646 | if (original_where) |
647 | fprintf(f, "# Canonicalized from %s\n", original_where); | |
98bad05e LP |
648 | |
649 | where_escaped = specifier_escape(where); | |
650 | if (!where_escaped) | |
651 | return log_oom(); | |
652 | fprintf(f, "Where=%s\n", where_escaped); | |
6db615c1 | 653 | |
98bad05e | 654 | if (!isempty(fstype) && !streq(fstype, "auto")) { |
c2b2df60 | 655 | _cleanup_free_ char *t = NULL; |
98bad05e LP |
656 | |
657 | t = specifier_escape(fstype); | |
658 | if (!t) | |
659 | return -ENOMEM; | |
660 | ||
661 | fprintf(f, "Type=%s\n", t); | |
662 | } | |
6b1dc2bd | 663 | |
91214a37 | 664 | r = generator_write_timeouts(dest, what, where, opts, &filtered); |
29686440 ZJS |
665 | if (r < 0) |
666 | return r; | |
667 | ||
4195077a MK |
668 | r = generator_write_device_deps(dest, what, where, opts); |
669 | if (r < 0) | |
670 | return r; | |
671 | ||
9cf22035 LF |
672 | if (in_initrd() && path_equal(where, "/sysroot") && is_device_path(what)) { |
673 | r = generator_write_initrd_root_device_deps(dest, what); | |
674 | if (r < 0) | |
675 | return r; | |
676 | } | |
677 | ||
110773f6 CH |
678 | r = write_mount_timeout(f, where, opts); |
679 | if (r < 0) | |
680 | return r; | |
681 | ||
d5cc4be2 LP |
682 | r = write_options(f, filtered); |
683 | if (r < 0) | |
684 | return r; | |
5e398e54 | 685 | |
d2194e15 | 686 | if (flags & MOUNT_RW_ONLY) |
f42aa416 MH |
687 | fprintf(f, "ReadWriteOnly=yes\n"); |
688 | ||
4652c56c ZJS |
689 | r = fflush_and_check(f); |
690 | if (r < 0) | |
fb883e75 | 691 | return log_error_errno(r, "Failed to write unit file %s: %m", name); |
6b1dc2bd | 692 | |
d2194e15 | 693 | if (flags & MOUNT_MAKEFS) { |
da495a03 ZJS |
694 | r = generator_hook_up_mkfs(dest, what, where, fstype); |
695 | if (r < 0) | |
696 | return r; | |
697 | } | |
698 | ||
d2194e15 | 699 | if (flags & MOUNT_GROWFS) { |
da69e8e4 | 700 | r = generator_hook_up_growfs(dest, where, target_unit); |
7f2806d5 ZJS |
701 | if (r < 0) |
702 | return r; | |
703 | } | |
704 | ||
04959faa | 705 | if (flags & MOUNT_PCRFS) { |
be8f478c | 706 | r = efi_measured_uki(LOG_WARNING); |
2f6c52b9 | 707 | if (r == 0) |
6c51b49c | 708 | log_debug("Kernel stub did not measure kernel image into PCR, skipping userspace measurement, too."); |
2f6c52b9 | 709 | else if (r > 0) { |
6c51b49c LP |
710 | r = generator_hook_up_pcrfs(dest, where, target_unit); |
711 | if (r < 0) | |
712 | return r; | |
713 | } | |
04959faa LP |
714 | } |
715 | ||
fc5c6ecc TB |
716 | if (flags & MOUNT_QUOTA) { |
717 | r = generator_hook_up_quotacheck(dest, what, where, target_unit, fstype); | |
718 | if (r < 0) { | |
719 | if (r != -EOPNOTSUPP) | |
720 | return r; | |
721 | } else { | |
722 | r = generator_hook_up_quotaon(dest, where, target_unit); | |
723 | if (r < 0) | |
724 | return r; | |
725 | } | |
726 | } | |
727 | ||
39a4c452 | 728 | if (FLAGS_SET(flags, MOUNT_AUTOMOUNT)) { |
7410616c LP |
729 | r = unit_name_from_path(where, ".automount", &automount_name); |
730 | if (r < 0) | |
731 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
6b1dc2bd | 732 | |
8a7033ac | 733 | f = safe_fclose(f); |
6b1dc2bd | 734 | |
7772c177 | 735 | r = generator_open_unit_file(dest, source, automount_name, &f); |
fb883e75 ZJS |
736 | if (r < 0) |
737 | return r; | |
0d536673 | 738 | |
6b1dc2bd | 739 | fprintf(f, |
6b1dc2bd | 740 | "[Unit]\n" |
c3834f9b LP |
741 | "SourcePath=%s\n" |
742 | "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n", | |
700e07ff HH |
743 | source); |
744 | ||
700e07ff | 745 | fprintf(f, |
bd10a84b | 746 | "\n" |
6b1dc2bd LP |
747 | "[Automount]\n" |
748 | "Where=%s\n", | |
98bad05e | 749 | where_escaped); |
6b1dc2bd | 750 | |
336b5c61 | 751 | r = write_idle_timeout(f, where, opts); |
deb0a77c MO |
752 | if (r < 0) |
753 | return r; | |
754 | ||
4652c56c ZJS |
755 | r = fflush_and_check(f); |
756 | if (r < 0) | |
fb883e75 | 757 | return log_error_errno(r, "Failed to write unit file %s: %m", automount_name); |
39a4c452 | 758 | } |
6b1dc2bd | 759 | |
39a4c452 MY |
760 | if (target_unit) { |
761 | assert(strv_isempty(wanted_by)); | |
762 | assert(strv_isempty(required_by)); | |
763 | ||
764 | /* noauto has no effect if x-systemd.automount is used */ | |
765 | if (!FLAGS_SET(flags, MOUNT_NOAUTO) || automount_name) { | |
766 | r = generator_add_symlink(dest, target_unit, | |
767 | FLAGS_SET(flags, MOUNT_NOFAIL) ? "wants" : "requires", | |
768 | automount_name ?: name); | |
769 | if (r < 0) | |
770 | return r; | |
771 | } | |
772 | } else { | |
773 | const char *unit_name = automount_name ?: name; | |
774 | ||
775 | STRV_FOREACH(s, wanted_by) { | |
776 | r = generator_add_symlink(dest, *s, "wants", unit_name); | |
777 | if (r < 0) | |
778 | return r; | |
779 | } | |
780 | ||
781 | STRV_FOREACH(s, required_by) { | |
782 | r = generator_add_symlink(dest, *s, "requires", unit_name); | |
783 | if (r < 0) | |
784 | return r; | |
785 | } | |
786 | ||
787 | if ((flags & (MOUNT_NOAUTO|MOUNT_NOFAIL)) != 0) | |
788 | log_warning("x-systemd.wanted-by= and/or x-systemd.required-by= specified, 'noauto' and 'nofail' have no effect."); | |
6b1dc2bd LP |
789 | } |
790 | ||
028a981c ZJS |
791 | return true; |
792 | } | |
793 | ||
794 | static int do_daemon_reload(void) { | |
795 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
028a981c ZJS |
796 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
797 | int r, k; | |
798 | ||
799 | log_debug("Calling org.freedesktop.systemd1.Manager.Reload()..."); | |
800 | ||
801 | r = bus_connect_system_systemd(&bus); | |
802 | if (r < 0) | |
803 | return log_error_errno(r, "Failed to get D-Bus connection: %m"); | |
804 | ||
a9399358 | 805 | r = bus_service_manager_reload(bus); |
028a981c | 806 | if (r < 0) |
a9399358 | 807 | return r; |
028a981c ZJS |
808 | |
809 | /* We need to requeue the two targets so that any new units which previously were not part of the | |
810 | * targets, and which we now added, will be started. */ | |
811 | ||
812 | r = 0; | |
813 | FOREACH_STRING(unit, SPECIAL_INITRD_FS_TARGET, SPECIAL_SWAP_TARGET) { | |
814 | log_info("Requesting %s/start/replace...", unit); | |
815 | ||
59228d0d | 816 | k = bus_call_method(bus, bus_systemd_mgr, "StartUnit", &error, NULL, "ss", unit, "replace"); |
028a981c ZJS |
817 | if (k < 0) { |
818 | log_error_errno(k, "Failed to (re)start %s: %s", unit, bus_error_message(&error, r)); | |
9ef648cc | 819 | RET_GATHER(r, k); |
028a981c ZJS |
820 | } |
821 | } | |
822 | ||
823 | return r; | |
6b1dc2bd LP |
824 | } |
825 | ||
99e3d476 ZJS |
826 | static const char* sysroot_fstab_path(void) { |
827 | return getenv("SYSTEMD_SYSROOT_FSTAB") ?: "/sysroot/etc/fstab"; | |
828 | } | |
829 | ||
cfeb4d37 YW |
830 | static bool sysfs_check(void) { |
831 | static int cached = -1; | |
832 | int r; | |
833 | ||
834 | if (cached < 0) { | |
efb9b3ba | 835 | r = secure_getenv_bool("SYSTEMD_SYSFS_CHECK"); |
cfeb4d37 YW |
836 | if (r < 0 && r != -ENXIO) |
837 | log_debug_errno(r, "Failed to parse $SYSTEMD_SYSFS_CHECK, ignoring: %m"); | |
838 | cached = r != 0; | |
839 | } | |
840 | ||
841 | return cached; | |
842 | } | |
843 | ||
dfce61dd LF |
844 | static int add_sysusr_sysroot_usr_bind_mount(const char *source) { |
845 | return add_mount(source, | |
846 | arg_dest, | |
847 | "/sysusr/usr", | |
848 | "/sysroot/usr", | |
849 | NULL, | |
850 | NULL, | |
851 | "bind", | |
852 | 0, | |
853 | 0, | |
854 | SPECIAL_INITRD_FS_TARGET); | |
855 | } | |
856 | ||
cfeb4d37 YW |
857 | static MountPointFlags fstab_options_to_flags(const char *options, bool is_swap) { |
858 | MountPointFlags flags = 0; | |
859 | ||
c521ce42 MY |
860 | if (isempty(options)) |
861 | return 0; | |
862 | ||
cfeb4d37 YW |
863 | if (fstab_test_option(options, "x-systemd.makefs\0")) |
864 | flags |= MOUNT_MAKEFS; | |
865 | if (fstab_test_option(options, "x-systemd.growfs\0")) | |
866 | flags |= MOUNT_GROWFS; | |
867 | if (fstab_test_option(options, "x-systemd.pcrfs\0")) | |
868 | flags |= MOUNT_PCRFS; | |
fc5c6ecc TB |
869 | if (fstab_test_option(options, "usrquota\0" "grpquota\0" "quota\0" "usrjquota\0" "grpjquota\0" "prjquota\0")) |
870 | flags |= MOUNT_QUOTA; | |
cfeb4d37 YW |
871 | if (fstab_test_yes_no_option(options, "noauto\0" "auto\0")) |
872 | flags |= MOUNT_NOAUTO; | |
873 | if (fstab_test_yes_no_option(options, "nofail\0" "fail\0")) | |
874 | flags |= MOUNT_NOFAIL; | |
875 | ||
876 | if (!is_swap) { | |
877 | if (fstab_test_option(options, "x-systemd.rw-only\0")) | |
878 | flags |= MOUNT_RW_ONLY; | |
879 | if (fstab_test_option(options, | |
880 | "comment=systemd.automount\0" | |
881 | "x-systemd.automount\0")) | |
882 | flags |= MOUNT_AUTOMOUNT; | |
883 | } | |
884 | ||
885 | return flags; | |
886 | } | |
887 | ||
8f88e573 | 888 | static int canonicalize_mount_path(const char *path, const char *type, bool prefix_sysroot, char **ret) { |
b5fd3956 MY |
889 | _cleanup_free_ char *p = NULL; |
890 | bool changed; | |
891 | int r; | |
892 | ||
893 | assert(path); | |
894 | assert(type); | |
895 | assert(STR_IN_SET(type, "where", "what")); | |
896 | assert(ret); | |
897 | ||
898 | // FIXME: when chase() learns to chase non-existent paths, use this here and drop the prefixing with | |
899 | // /sysroot on error below. | |
8f88e573 | 900 | r = chase(path, prefix_sysroot ? "/sysroot" : NULL, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &p, NULL); |
b5fd3956 MY |
901 | if (r < 0) { |
902 | log_debug_errno(r, "Failed to chase '%s', using as-is: %m", path); | |
903 | ||
8f88e573 | 904 | if (prefix_sysroot) |
b5fd3956 MY |
905 | p = path_join("/sysroot", path); |
906 | else | |
907 | p = strdup(path); | |
908 | if (!p) | |
909 | return log_oom(); | |
910 | ||
911 | path_simplify(p); | |
912 | } | |
913 | ||
914 | changed = !streq(path, p); | |
915 | if (changed) | |
916 | log_debug("Canonicalized %s=%s to %s", type, path, p); | |
917 | ||
918 | *ret = TAKE_PTR(p); | |
919 | return changed; | |
920 | } | |
921 | ||
cfeb4d37 YW |
922 | static int parse_fstab_one( |
923 | const char *source, | |
924 | const char *what_original, | |
925 | const char *where_original, | |
926 | const char *fstype, | |
927 | const char *options, | |
928 | int passno, | |
8f88e573 | 929 | bool prefix_sysroot, |
22f5a825 | 930 | bool accept_root, /* This takes an effect only when prefix_sysroot is true. */ |
55365b0a | 931 | bool use_swap_enabled) { |
cfeb4d37 | 932 | |
1e9b2e4f | 933 | _cleanup_free_ char *what = NULL, *where = NULL, *opts = NULL; |
cfeb4d37 | 934 | MountPointFlags flags; |
b5fd3956 | 935 | bool is_swap, where_changed; |
cfeb4d37 YW |
936 | int r; |
937 | ||
938 | assert(what_original); | |
cfeb4d37 | 939 | assert(fstype); |
cfeb4d37 | 940 | |
22f5a825 | 941 | if (prefix_sysroot && !mount_in_initrd(where_original, options, accept_root)) |
cfeb4d37 YW |
942 | return 0; |
943 | ||
256604cc | 944 | is_swap = streq_ptr(fstype, "swap"); |
55365b0a | 945 | if (is_swap && use_swap_enabled && !arg_swap_enabled) { |
3aed2593 | 946 | log_info("Swap unit generation disabled on kernel command line, ignoring swap entry for %s.", what_original); |
94456233 YW |
947 | return 0; |
948 | } | |
256604cc | 949 | |
cfeb4d37 YW |
950 | what = fstab_node_to_udev_node(what_original); |
951 | if (!what) | |
952 | return log_oom(); | |
953 | ||
954 | if (path_is_read_only_fs("/sys") > 0 && | |
955 | (streq(what, "sysfs") || | |
956 | (sysfs_check() && is_device_path(what)))) { | |
957 | log_info("/sys/ is read-only (running in a container?), ignoring mount for %s.", what); | |
958 | return 0; | |
959 | } | |
960 | ||
256604cc YW |
961 | flags = fstab_options_to_flags(options, is_swap); |
962 | ||
963 | if (is_swap) | |
964 | return add_swap(source, what, options, flags); | |
965 | ||
239cce38 YW |
966 | if (passno < 0) |
967 | passno = is_device_path(what); | |
968 | ||
256604cc YW |
969 | assert(where_original); /* 'where' is not necessary for swap entry. */ |
970 | ||
6742eca1 | 971 | if (!is_path(where_original)) { |
b5fd3956 | 972 | log_warning("Mount point %s is not a valid path, ignoring.", where_original); |
6742eca1 YW |
973 | return 0; |
974 | } | |
cfeb4d37 | 975 | |
6742eca1 YW |
976 | /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for |
977 | * mount units, but causes problems since it historically worked to have symlinks in e.g. | |
978 | * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case | |
979 | * where a symlink refers to another mount target; this works assuming the sub-mountpoint | |
b5fd3956 | 980 | * target is the final directory. */ |
8f88e573 | 981 | r = canonicalize_mount_path(where_original, "where", prefix_sysroot, &where); |
b5fd3956 MY |
982 | if (r < 0) |
983 | return r; | |
984 | where_changed = r > 0; | |
cfeb4d37 | 985 | |
8f88e573 | 986 | if (prefix_sysroot && fstab_is_bind(options, fstype)) { |
b5fd3956 MY |
987 | /* When in initrd, the source of bind mount needs to be prepended with /sysroot as well. */ |
988 | _cleanup_free_ char *p = NULL; | |
6742eca1 | 989 | |
8f88e573 | 990 | r = canonicalize_mount_path(what, "what", prefix_sysroot, &p); |
b5fd3956 MY |
991 | if (r < 0) |
992 | return r; | |
cfeb4d37 | 993 | |
b5fd3956 MY |
994 | free_and_replace(what, p); |
995 | } | |
6742eca1 | 996 | |
cfeb4d37 YW |
997 | log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s pcrfs=%s noauto=%s nofail=%s", |
998 | what, where, strna(fstype), | |
999 | yes_no(flags & MOUNT_MAKEFS), yes_no(flags & MOUNT_GROWFS), yes_no(flags & MOUNT_PCRFS), | |
1000 | yes_no(flags & MOUNT_NOAUTO), yes_no(flags & MOUNT_NOFAIL)); | |
1001 | ||
b5fd3956 | 1002 | bool is_sysroot = in_initrd() && path_equal(where, "/sysroot"); |
cfeb4d37 YW |
1003 | /* See comment from add_sysroot_usr_mount() about the need for extra indirection in case /usr needs |
1004 | * to be mounted in order for the root fs to be synthesized based on configuration included in /usr/, | |
1005 | * e.g. systemd-repart. */ | |
b5fd3956 | 1006 | bool is_sysroot_usr = in_initrd() && path_equal(where, "/sysroot/usr"); |
cfeb4d37 YW |
1007 | |
1008 | const char *target_unit = | |
cfeb4d37 YW |
1009 | is_sysroot ? SPECIAL_INITRD_ROOT_FS_TARGET : |
1010 | is_sysroot_usr ? SPECIAL_INITRD_USR_FS_TARGET : | |
b93d9e06 | 1011 | prefix_sysroot ? SPECIAL_INITRD_FS_TARGET : |
cfeb4d37 YW |
1012 | mount_is_network(fstype, options) ? SPECIAL_REMOTE_FS_TARGET : |
1013 | SPECIAL_LOCAL_FS_TARGET; | |
1014 | ||
b3ee0148 MY |
1015 | /* nofail, noauto and x-systemd.automount don't make sense for critical filesystems we must mount in initrd. */ |
1016 | if (is_sysroot || is_sysroot_usr) { | |
1017 | r = mandatory_mount_drop_unapplicable_options(&flags, where, options, &opts); | |
1e9b2e4f MS |
1018 | if (r < 0) |
1019 | return r; | |
1e9b2e4f MS |
1020 | options = opts; |
1021 | } | |
1022 | ||
cfeb4d37 YW |
1023 | r = add_mount(source, |
1024 | arg_dest, | |
1025 | what, | |
b5fd3956 MY |
1026 | is_sysroot_usr ? "/sysusr/usr" : where, |
1027 | !is_sysroot_usr && where_changed ? where_original : NULL, | |
cfeb4d37 YW |
1028 | fstype, |
1029 | options, | |
1030 | passno, | |
1031 | flags, | |
1032 | target_unit); | |
1033 | if (r <= 0) | |
1034 | return r; | |
1035 | ||
1036 | if (is_sysroot_usr) { | |
1037 | log_debug("Synthesizing fstab entry what=/sysusr/usr where=/sysroot/usr opts=bind"); | |
1038 | r = add_sysusr_sysroot_usr_bind_mount(source); | |
1039 | if (r < 0) | |
1040 | return r; | |
1041 | } | |
1042 | ||
1043 | return true; | |
1044 | } | |
1045 | ||
8f88e573 | 1046 | static int parse_fstab(bool prefix_sysroot) { |
6db615c1 | 1047 | _cleanup_endmntent_ FILE *f = NULL; |
ed4ad488 | 1048 | const char *fstab; |
6b1dc2bd | 1049 | struct mntent *me; |
cfeb4d37 | 1050 | int r, ret = 0; |
6b1dc2bd | 1051 | |
8f88e573 | 1052 | if (prefix_sysroot) |
99e3d476 | 1053 | fstab = sysroot_fstab_path(); |
028a981c | 1054 | else { |
99e3d476 | 1055 | fstab = fstab_path(); |
028a981c ZJS |
1056 | assert(!arg_sysroot_check); |
1057 | } | |
99e3d476 | 1058 | |
ed4ad488 | 1059 | log_debug("Parsing %s...", fstab); |
01a0f7d0 | 1060 | |
ed4ad488 | 1061 | f = setmntent(fstab, "re"); |
6b1dc2bd LP |
1062 | if (!f) { |
1063 | if (errno == ENOENT) | |
1064 | return 0; | |
1065 | ||
ed4ad488 | 1066 | return log_error_errno(errno, "Failed to open %s: %m", fstab); |
6b1dc2bd LP |
1067 | } |
1068 | ||
1069 | while ((me = getmntent(f))) { | |
55365b0a YW |
1070 | r = parse_fstab_one(fstab, |
1071 | me->mnt_fsname, me->mnt_dir, me->mnt_type, me->mnt_opts, me->mnt_passno, | |
22f5a825 YW |
1072 | prefix_sysroot, |
1073 | /* accept_root = */ false, | |
1074 | /* use_swap_enabled = */ true); | |
cfeb4d37 YW |
1075 | if (r < 0 && ret >= 0) |
1076 | ret = r; | |
1077 | if (arg_sysroot_check && r > 0) | |
028a981c | 1078 | return true; /* We found a mount or swap that would be started… */ |
6b1dc2bd LP |
1079 | } |
1080 | ||
cfeb4d37 | 1081 | return ret; |
6b1dc2bd LP |
1082 | } |
1083 | ||
77b8e92d YW |
1084 | static int sysroot_is_nfsroot(void) { |
1085 | union in_addr_union u; | |
1086 | const char *sep, *a; | |
1087 | int r; | |
1088 | ||
1089 | assert(arg_root_what); | |
1090 | ||
1091 | /* From dracut.cmdline(7). | |
1092 | * | |
1093 | * root=[<server-ip>:]<root-dir>[:<nfs-options>] | |
1094 | * root=nfs:[<server-ip>:]<root-dir>[:<nfs-options>], | |
1095 | * root=nfs4:[<server-ip>:]<root-dir>[:<nfs-options>], | |
1096 | * root={dhcp|dhcp6} | |
1097 | * | |
1098 | * mount nfs share from <server-ip>:/<root-dir>, if no server-ip is given, use dhcp next_server. | |
1099 | * If server-ip is an IPv6 address it has to be put in brackets, e.g. [2001:DB8::1]. NFS options | |
1100 | * can be appended with the prefix ":" or "," and are separated by ",". */ | |
1101 | ||
1102 | if (path_equal(arg_root_what, "/dev/nfs") || | |
1103 | STR_IN_SET(arg_root_what, "dhcp", "dhcp6") || | |
1104 | STARTSWITH_SET(arg_root_what, "nfs:", "nfs4:")) | |
1105 | return true; | |
1106 | ||
1107 | /* IPv6 address */ | |
1108 | if (arg_root_what[0] == '[') { | |
1109 | sep = strchr(arg_root_what + 1, ']'); | |
1110 | if (!sep) | |
1111 | return -EINVAL; | |
1112 | ||
ea1c9d3f | 1113 | a = strndupa_safe(arg_root_what + 1, sep - arg_root_what - 1); |
77b8e92d YW |
1114 | |
1115 | r = in_addr_from_string(AF_INET6, a, &u); | |
1116 | if (r < 0) | |
1117 | return r; | |
1118 | ||
1119 | return true; | |
1120 | } | |
1121 | ||
1122 | /* IPv4 address */ | |
1123 | sep = strchr(arg_root_what, ':'); | |
1124 | if (sep) { | |
ea1c9d3f | 1125 | a = strndupa_safe(arg_root_what, sep - arg_root_what); |
77b8e92d YW |
1126 | |
1127 | if (in_addr_from_string(AF_INET, a, &u) >= 0) | |
1128 | return true; | |
1129 | } | |
1130 | ||
1131 | /* root directory without address */ | |
1132 | return path_is_absolute(arg_root_what) && !path_startswith(arg_root_what, "/dev"); | |
1133 | } | |
1134 | ||
2e852276 | 1135 | static int add_sysroot_mount(void) { |
75a59316 | 1136 | _cleanup_free_ char *what = NULL; |
725ad3b0 | 1137 | const char *opts, *fstype; |
01fdfbb8 WF |
1138 | bool default_rw, makefs; |
1139 | MountPointFlags flags; | |
7163e1ca | 1140 | int r; |
5e398e54 | 1141 | |
093c2cfe TH |
1142 | if (isempty(arg_root_what)) { |
1143 | log_debug("Could not find a root= entry on the kernel command line."); | |
1144 | return 0; | |
135b5212 | 1145 | } |
5e398e54 | 1146 | |
138f4c69 | 1147 | if (streq(arg_root_what, "gpt-auto")) { |
4981427c | 1148 | /* This is handled by gpt-auto-generator */ |
138f4c69 LP |
1149 | log_debug("Skipping root directory handling, as gpt-auto was requested."); |
1150 | return 0; | |
dfce61dd LF |
1151 | } else if (streq(arg_root_what, "fstab")) { |
1152 | /* This is handled by parse_fstab */ | |
1153 | log_debug("Using initrd's fstab for /sysroot/ configuration."); | |
1154 | return 0; | |
138f4c69 LP |
1155 | } |
1156 | ||
77b8e92d YW |
1157 | r = sysroot_is_nfsroot(); |
1158 | if (r < 0) | |
1159 | log_debug_errno(r, "Failed to determine if the root directory is on NFS, assuming not: %m"); | |
1160 | else if (r > 0) { | |
3ade16c9 | 1161 | /* This is handled by the kernel or the initrd */ |
77b8e92d | 1162 | log_debug("Skipping root directory handling, as root on NFS was requested."); |
155e1bb4 YW |
1163 | return 0; |
1164 | } | |
1165 | ||
1166 | if (startswith(arg_root_what, "cifs://")) { | |
1167 | log_debug("Skipping root directory handling, as root on CIFS was requested."); | |
1168 | return 0; | |
1169 | } | |
1170 | ||
1171 | if (startswith(arg_root_what, "iscsi:")) { | |
1172 | log_debug("Skipping root directory handling, as root on iSCSI was requested."); | |
1173 | return 0; | |
1174 | } | |
1175 | ||
1176 | if (startswith(arg_root_what, "live:")) { | |
1177 | log_debug("Skipping root directory handling, as root on live image was requested."); | |
3ade16c9 HH |
1178 | return 0; |
1179 | } | |
1180 | ||
725ad3b0 LP |
1181 | if (streq(arg_root_what, "tmpfs")) { |
1182 | /* If root=tmpfs is specified, then take this as shortcut for a writable tmpfs mount as root */ | |
1183 | ||
1184 | what = strdup("rootfs"); /* just a pretty name, to show up in /proc/self/mountinfo */ | |
1185 | if (!what) | |
1186 | return log_oom(); | |
1187 | ||
5a12d1ca | 1188 | fstype = arg_root_fstype ?: "tmpfs"; /* tmpfs, unless overridden */ |
725ad3b0 | 1189 | |
5a12d1ca | 1190 | default_rw = true; /* writable, unless overridden */; |
725ad3b0 LP |
1191 | } else { |
1192 | ||
1193 | what = fstab_node_to_udev_node(arg_root_what); | |
1194 | if (!what) | |
1195 | return log_oom(); | |
1196 | ||
1197 | fstype = arg_root_fstype; /* if not specified explicitly, don't default to anything here */ | |
1198 | ||
5a12d1ca | 1199 | default_rw = false; /* read-only, unless overridden */ |
725ad3b0 | 1200 | } |
093c2cfe | 1201 | |
6db615c1 | 1202 | if (!arg_root_options) |
725ad3b0 | 1203 | opts = arg_root_rw > 0 || (arg_root_rw < 0 && default_rw) ? "rw" : "ro"; |
75a59316 | 1204 | else if (arg_root_rw >= 0 || |
d15d0333 | 1205 | !fstab_test_option(arg_root_options, "ro\0" "rw\0")) |
63c372cb | 1206 | opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro"); |
75a59316 ZJS |
1207 | else |
1208 | opts = arg_root_options; | |
5e398e54 | 1209 | |
e19ae92a | 1210 | log_debug("Found entry what=%s where=/sysroot type=%s opts=%s", what, strna(arg_root_fstype), strempty(opts)); |
7163e1ca | 1211 | |
01fdfbb8 WF |
1212 | makefs = fstab_test_option(opts, "x-systemd.makefs\0"); |
1213 | flags = makefs * MOUNT_MAKEFS; | |
1214 | ||
7772c177 ZJS |
1215 | return add_mount("/proc/cmdline", |
1216 | arg_dest, | |
91214a37 | 1217 | what, |
6db615c1 | 1218 | "/sysroot", |
634735b5 | 1219 | NULL, |
725ad3b0 | 1220 | fstype, |
75a59316 | 1221 | opts, |
f113f8e3 | 1222 | is_device_path(what) ? 1 : 0, /* passno */ |
fc5c6ecc | 1223 | flags, /* makefs off, pcrfs off, quota off, noauto off, nofail off, automount off */ |
7772c177 | 1224 | SPECIAL_INITRD_ROOT_FS_TARGET); |
5e398e54 TG |
1225 | } |
1226 | ||
2e852276 | 1227 | static int add_sysroot_usr_mount(void) { |
9f103625 TH |
1228 | _cleanup_free_ char *what = NULL; |
1229 | const char *opts; | |
01fdfbb8 WF |
1230 | bool makefs; |
1231 | MountPointFlags flags; | |
29a24ab2 LP |
1232 | int r; |
1233 | ||
1234 | /* Returns 0 if we didn't do anything, > 0 if we either generated a unit for the /usr/ mount, or we | |
1235 | * know for sure something else did */ | |
9f103625 TH |
1236 | |
1237 | if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options) | |
1238 | return 0; | |
1239 | ||
1240 | if (arg_root_what && !arg_usr_what) { | |
40472036 | 1241 | /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */ |
9f103625 | 1242 | arg_usr_what = strdup(arg_root_what); |
9f103625 TH |
1243 | if (!arg_usr_what) |
1244 | return log_oom(); | |
1245 | } | |
1246 | ||
1247 | if (arg_root_fstype && !arg_usr_fstype) { | |
1248 | arg_usr_fstype = strdup(arg_root_fstype); | |
9f103625 TH |
1249 | if (!arg_usr_fstype) |
1250 | return log_oom(); | |
1251 | } | |
1252 | ||
1253 | if (arg_root_options && !arg_usr_options) { | |
1254 | arg_usr_options = strdup(arg_root_options); | |
9f103625 TH |
1255 | if (!arg_usr_options) |
1256 | return log_oom(); | |
1257 | } | |
1258 | ||
29a24ab2 | 1259 | if (isempty(arg_usr_what)) { |
32fe629a | 1260 | log_debug("Could not find a mount.usr= entry on the kernel command line."); |
9f103625 | 1261 | return 0; |
29a24ab2 LP |
1262 | } |
1263 | ||
1264 | if (streq(arg_usr_what, "gpt-auto")) { | |
1265 | /* This is handled by the gpt-auto generator */ | |
1266 | log_debug("Skipping /usr/ directory handling, as gpt-auto was requested."); | |
1267 | return 1; /* systemd-gpt-auto-generator will generate a unit for this, hence report that a | |
1268 | * unit file is being created for the host /usr/ mount. */ | |
dfce61dd LF |
1269 | } else if (streq(arg_usr_what, "fstab")) { |
1270 | /* This is handled by parse_fstab */ | |
1271 | log_debug("Using initrd's fstab for /sysroot/usr/ configuration."); | |
1272 | return 1; /* parse_fstab will generate a unit for this, hence report that a | |
1273 | * unit file is being created for the host /usr/ mount. */ | |
29a24ab2 LP |
1274 | } |
1275 | ||
1276 | if (path_equal(arg_usr_what, "/dev/nfs")) { | |
1277 | /* This is handled by the initrd (if at all supported, that is) */ | |
1278 | log_debug("Skipping /usr/ directory handling, as /dev/nfs was requested."); | |
1279 | return 1; /* As above, report that NFS code will create the unit */ | |
1280 | } | |
9f103625 TH |
1281 | |
1282 | what = fstab_node_to_udev_node(arg_usr_what); | |
47be5f07 LP |
1283 | if (!what) |
1284 | return log_oom(); | |
9f103625 | 1285 | |
eb580002 MM |
1286 | if (!arg_usr_options) |
1287 | opts = arg_root_rw > 0 ? "rw" : "ro"; | |
d15d0333 | 1288 | else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0")) |
63c372cb | 1289 | opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro"); |
eb580002 MM |
1290 | else |
1291 | opts = arg_usr_options; | |
9f103625 | 1292 | |
29a24ab2 LP |
1293 | /* When mounting /usr from the initrd, we add an extra level of indirection: we first mount the /usr/ |
1294 | * partition to /sysusr/usr/, and then afterwards bind mount that to /sysroot/usr/. We do this so | |
1295 | * that we can cover for systems that initially only have a /usr/ around and where the root fs needs | |
1296 | * to be synthesized, based on configuration included in /usr/, e.g. systemd-repart. Software like | |
1297 | * this should order itself after initrd-usr-fs.target and before initrd-fs.target; and it should | |
1298 | * look into both /sysusr/ and /sysroot/ for the configuration data to apply. */ | |
1299 | ||
1300 | log_debug("Found entry what=%s where=/sysusr/usr type=%s opts=%s", what, strna(arg_usr_fstype), strempty(opts)); | |
1301 | ||
01fdfbb8 WF |
1302 | makefs = fstab_test_option(opts, "x-systemd.makefs\0"); |
1303 | flags = makefs * MOUNT_MAKEFS; | |
1304 | ||
7772c177 ZJS |
1305 | r = add_mount("/proc/cmdline", |
1306 | arg_dest, | |
29a24ab2 LP |
1307 | what, |
1308 | "/sysusr/usr", | |
1309 | NULL, | |
1310 | arg_usr_fstype, | |
1311 | opts, | |
1312 | is_device_path(what) ? 1 : 0, /* passno */ | |
01fdfbb8 | 1313 | flags, |
7772c177 | 1314 | SPECIAL_INITRD_USR_FS_TARGET); |
29a24ab2 LP |
1315 | if (r < 0) |
1316 | return r; | |
1317 | ||
dfce61dd | 1318 | log_debug("Synthesizing entry what=/sysusr/usr where=/sysroot/usr opts=bind"); |
29a24ab2 | 1319 | |
dfce61dd | 1320 | r = add_sysusr_sysroot_usr_bind_mount("/proc/cmdline"); |
29a24ab2 LP |
1321 | if (r < 0) |
1322 | return r; | |
1323 | ||
1324 | return 1; | |
1325 | } | |
1326 | ||
1327 | static int add_sysroot_usr_mount_or_fallback(void) { | |
1328 | int r; | |
1329 | ||
1330 | r = add_sysroot_usr_mount(); | |
1331 | if (r != 0) | |
1332 | return r; | |
1333 | ||
1334 | /* OK, so we didn't write anything out for /sysusr/usr/ nor /sysroot/usr/. In this case, let's make | |
1335 | * sure that initrd-usr-fs.target is at least ordered after sysroot.mount so that services that order | |
de47cd06 | 1336 | * themselves after it get the guarantee that /usr/ is definitely mounted somewhere. */ |
29a24ab2 LP |
1337 | |
1338 | return generator_add_symlink( | |
1339 | arg_dest, | |
1340 | SPECIAL_INITRD_USR_FS_TARGET, | |
1341 | "requires", | |
1342 | "sysroot.mount"); | |
9f103625 TH |
1343 | } |
1344 | ||
91214a37 | 1345 | static int add_volatile_root(void) { |
1de7f825 | 1346 | |
91214a37 | 1347 | /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is |
1de7f825 | 1348 | * requested (or as an overlayfs), leaving only /usr from the root mount inside. */ |
91214a37 | 1349 | |
1de7f825 | 1350 | if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY)) |
00bb366d | 1351 | return 0; |
91214a37 | 1352 | |
00bb366d | 1353 | return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires", |
835cf75a | 1354 | SYSTEM_DATA_UNIT_DIR "/" SPECIAL_VOLATILE_ROOT_SERVICE); |
91214a37 LP |
1355 | } |
1356 | ||
1357 | static int add_volatile_var(void) { | |
1358 | ||
1359 | if (arg_volatile_mode != VOLATILE_STATE) | |
1360 | return 0; | |
1361 | ||
1362 | /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */ | |
1363 | ||
7772c177 ZJS |
1364 | return add_mount("/proc/cmdline", |
1365 | arg_dest_late, | |
91214a37 LP |
1366 | "tmpfs", |
1367 | "/var", | |
634735b5 | 1368 | NULL, |
91214a37 | 1369 | "tmpfs", |
7d85383e | 1370 | "mode=0755" TMPFS_LIMITS_VAR, |
91214a37 | 1371 | 0, |
4191418b | 1372 | 0, |
7772c177 | 1373 | SPECIAL_LOCAL_FS_TARGET); |
91214a37 LP |
1374 | } |
1375 | ||
55365b0a | 1376 | static int add_mounts_from_cmdline(void) { |
ba2f3ec8 | 1377 | int r = 0; |
55365b0a YW |
1378 | |
1379 | /* Handle each entries found in cmdline as a fstab entry. */ | |
1380 | ||
1381 | FOREACH_ARRAY(m, arg_mounts, arg_n_mounts) { | |
45c535dd YW |
1382 | if (m->for_initrd && !in_initrd()) |
1383 | continue; | |
1384 | ||
ba2f3ec8 MY |
1385 | RET_GATHER(r, parse_fstab_one("/proc/cmdline", |
1386 | m->what, | |
1387 | m->where, | |
1388 | m->fstype, | |
1389 | m->options, | |
1390 | /* passno = */ -1, | |
1391 | /* prefix_sysroot = */ !m->for_initrd && in_initrd(), | |
1392 | /* accept_root = */ true, | |
1393 | /* use_swap_enabled = */ false)); | |
55365b0a YW |
1394 | } |
1395 | ||
ba2f3ec8 | 1396 | return r; |
55365b0a YW |
1397 | } |
1398 | ||
dfd10549 | 1399 | static int add_mounts_from_creds(bool prefix_sysroot) { |
6ac62485 LP |
1400 | _cleanup_free_ void *b = NULL; |
1401 | struct mntent *me; | |
6ac62485 | 1402 | size_t bs; |
ba2f3ec8 | 1403 | int r; |
6ac62485 | 1404 | |
dfd10549 YW |
1405 | assert(in_initrd() || !prefix_sysroot); |
1406 | ||
6ac62485 | 1407 | r = read_credential_with_decryption( |
dfd10549 | 1408 | in_initrd() && !prefix_sysroot ? "fstab.extra.initrd" : "fstab.extra", |
6ac62485 LP |
1409 | &b, &bs); |
1410 | if (r <= 0) | |
1411 | return r; | |
1412 | ||
1413 | _cleanup_fclose_ FILE *f = NULL; | |
1414 | f = fmemopen_unlocked(b, bs, "r"); | |
1415 | if (!f) | |
1416 | return log_oom(); | |
1417 | ||
ba2f3ec8 | 1418 | r = 0; |
6ac62485 | 1419 | |
ba2f3ec8 MY |
1420 | while ((me = getmntent(f))) |
1421 | RET_GATHER(r, parse_fstab_one("/run/credentials", | |
1422 | me->mnt_fsname, | |
1423 | me->mnt_dir, | |
1424 | me->mnt_type, | |
1425 | me->mnt_opts, | |
1426 | me->mnt_passno, | |
1427 | /* prefix_sysroot = */ prefix_sysroot, | |
1428 | /* accept_root = */ true, | |
1429 | /* use_swap_enabled = */ true)); | |
1430 | ||
1431 | return r; | |
6ac62485 LP |
1432 | } |
1433 | ||
96287a49 | 1434 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
74df0fca | 1435 | int r; |
94734142 | 1436 | |
a8d3315b YW |
1437 | assert(key); |
1438 | ||
9f103625 TH |
1439 | /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last |
1440 | * instance should take precedence. In the case of multiple rootflags= | |
1441 | * or usrflags= the arguments should be concatenated */ | |
6db615c1 | 1442 | |
1d84ad94 | 1443 | if (STR_IN_SET(key, "fstab", "rd.fstab")) { |
e48fdd84 | 1444 | |
1d84ad94 | 1445 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 1446 | if (r < 0) |
059cb385 | 1447 | log_warning("Failed to parse fstab switch %s. Ignoring.", value); |
141a79f4 | 1448 | else |
b564224b | 1449 | arg_fstab_enabled = fstab_set_enabled(r); |
94734142 | 1450 | |
1d84ad94 LP |
1451 | } else if (streq(key, "root")) { |
1452 | ||
1453 | if (proc_cmdline_value_missing(key, value)) | |
1454 | return 0; | |
6db615c1 | 1455 | |
338da501 | 1456 | return free_and_strdup_warn(&arg_root_what, empty_to_null(value)); |
6db615c1 | 1457 | |
1d84ad94 LP |
1458 | } else if (streq(key, "rootfstype")) { |
1459 | ||
1460 | if (proc_cmdline_value_missing(key, value)) | |
1461 | return 0; | |
6db615c1 | 1462 | |
338da501 | 1463 | return free_and_strdup_warn(&arg_root_fstype, empty_to_null(value)); |
6db615c1 | 1464 | |
1d84ad94 | 1465 | } else if (streq(key, "rootflags")) { |
6db615c1 | 1466 | |
1d84ad94 LP |
1467 | if (proc_cmdline_value_missing(key, value)) |
1468 | return 0; | |
1469 | ||
c2bc710b | 1470 | if (!strextend_with_separator(&arg_root_options, ",", value)) |
6db615c1 LP |
1471 | return log_oom(); |
1472 | ||
2f3dfc6f LP |
1473 | } else if (streq(key, "roothash")) { |
1474 | ||
1475 | if (proc_cmdline_value_missing(key, value)) | |
1476 | return 0; | |
1477 | ||
338da501 | 1478 | return free_and_strdup_warn(&arg_root_hash, empty_to_null(value)); |
6db615c1 | 1479 | |
1d84ad94 LP |
1480 | } else if (streq(key, "mount.usr")) { |
1481 | ||
1482 | if (proc_cmdline_value_missing(key, value)) | |
1483 | return 0; | |
9f103625 | 1484 | |
338da501 | 1485 | return free_and_strdup_warn(&arg_usr_what, empty_to_null(value)); |
9f103625 | 1486 | |
1d84ad94 LP |
1487 | } else if (streq(key, "mount.usrfstype")) { |
1488 | ||
1489 | if (proc_cmdline_value_missing(key, value)) | |
1490 | return 0; | |
9f103625 | 1491 | |
338da501 | 1492 | return free_and_strdup_warn(&arg_usr_fstype, empty_to_null(value)); |
9f103625 | 1493 | |
1d84ad94 | 1494 | } else if (streq(key, "mount.usrflags")) { |
9f103625 | 1495 | |
1d84ad94 LP |
1496 | if (proc_cmdline_value_missing(key, value)) |
1497 | return 0; | |
1498 | ||
c2bc710b | 1499 | if (!strextend_with_separator(&arg_usr_options, ",", value)) |
9f103625 TH |
1500 | return log_oom(); |
1501 | ||
c1b9e3df MB |
1502 | } else if (streq(key, "usrhash")) { |
1503 | ||
1504 | if (proc_cmdline_value_missing(key, value)) | |
1505 | return 0; | |
1506 | ||
338da501 | 1507 | return free_and_strdup_warn(&arg_usr_hash, empty_to_null(value)); |
c1b9e3df | 1508 | |
6db615c1 LP |
1509 | } else if (streq(key, "rw") && !value) |
1510 | arg_root_rw = true; | |
1511 | else if (streq(key, "ro") && !value) | |
1512 | arg_root_rw = false; | |
91214a37 LP |
1513 | else if (streq(key, "systemd.volatile")) { |
1514 | VolatileMode m; | |
1515 | ||
1516 | if (value) { | |
1517 | m = volatile_mode_from_string(value); | |
1518 | if (m < 0) | |
7211c853 | 1519 | log_warning_errno(m, "Failed to parse systemd.volatile= argument: %s", value); |
91214a37 LP |
1520 | else |
1521 | arg_volatile_mode = m; | |
1522 | } else | |
1523 | arg_volatile_mode = VOLATILE_YES; | |
567a5307 | 1524 | |
1525 | } else if (streq(key, "systemd.swap")) { | |
1526 | ||
1527 | r = value ? parse_boolean(value) : 1; | |
1528 | if (r < 0) | |
1529 | log_warning("Failed to parse systemd.swap switch %s. Ignoring.", value); | |
1530 | else | |
1531 | arg_swap_enabled = r; | |
200268c6 DDM |
1532 | |
1533 | } else if (streq(key, "systemd.verity")) { | |
1534 | ||
1535 | r = value ? parse_boolean(value) : 1; | |
1536 | if (r < 0) | |
1537 | log_warning("Failed to parse systemd.verity= kernel command line switch %s. Ignoring.", value); | |
1538 | else | |
1539 | arg_verity = r; | |
55365b0a | 1540 | |
45c535dd | 1541 | } else if (STR_IN_SET(key, "systemd.mount-extra", "rd.systemd.mount-extra")) { |
55365b0a YW |
1542 | |
1543 | if (proc_cmdline_value_missing(key, value)) | |
1544 | return 0; | |
1545 | ||
45c535dd | 1546 | r = mount_array_add(startswith(key, "rd."), value); |
55365b0a YW |
1547 | if (r < 0) |
1548 | log_warning("Failed to parse systemd.mount-extra= option, ignoring: %s", value); | |
1549 | ||
45c535dd | 1550 | } else if (STR_IN_SET(key, "systemd.swap-extra", "rd.systemd.swap-extra")) { |
55365b0a YW |
1551 | |
1552 | if (proc_cmdline_value_missing(key, value)) | |
1553 | return 0; | |
1554 | ||
45c535dd | 1555 | r = mount_array_add_swap(startswith(key, "rd."), value); |
55365b0a YW |
1556 | if (r < 0) |
1557 | log_warning("Failed to parse systemd.swap-extra= option, ignoring: %s", value); | |
91214a37 | 1558 | } |
94734142 | 1559 | |
d0aa9ce5 | 1560 | return 0; |
94734142 LP |
1561 | } |
1562 | ||
2130a2e5 LP |
1563 | static int determine_device( |
1564 | char **what, | |
1565 | int *rw, | |
1566 | char **options, | |
1567 | const char *hash, | |
1568 | const char *name) { | |
2f3dfc6f | 1569 | |
c1b9e3df MB |
1570 | assert(what); |
1571 | assert(name); | |
1572 | ||
1573 | /* If we have a hash but no device then Verity is used, and we use the DM device. */ | |
1574 | if (*what) | |
2f3dfc6f LP |
1575 | return 0; |
1576 | ||
c1b9e3df | 1577 | if (!hash) |
2f3dfc6f LP |
1578 | return 0; |
1579 | ||
200268c6 DDM |
1580 | if (!arg_verity) |
1581 | return 0; | |
1582 | ||
c1b9e3df MB |
1583 | *what = path_join("/dev/mapper/", name); |
1584 | if (!*what) | |
2f3dfc6f LP |
1585 | return log_oom(); |
1586 | ||
2130a2e5 LP |
1587 | /* Verity is always read-only */ |
1588 | if (rw) | |
1589 | *rw = false; | |
1590 | if (options && !strextend_with_separator(options, ",", "ro")) | |
1591 | return log_oom(); | |
2f3dfc6f | 1592 | |
2130a2e5 | 1593 | log_info("Using verity %s device %s.", name, *what); |
2f3dfc6f LP |
1594 | return 1; |
1595 | } | |
1596 | ||
c1b9e3df | 1597 | static int determine_root(void) { |
2130a2e5 | 1598 | return determine_device(&arg_root_what, &arg_root_rw, NULL, arg_root_hash, "root"); |
c1b9e3df MB |
1599 | } |
1600 | ||
1601 | static int determine_usr(void) { | |
2130a2e5 | 1602 | return determine_device(&arg_usr_what, NULL, &arg_usr_options, arg_usr_hash, "usr"); |
c1b9e3df MB |
1603 | } |
1604 | ||
028a981c ZJS |
1605 | /* If arg_sysroot_check is false, run as generator in the usual fashion. |
1606 | * If it is true, check /sysroot/etc/fstab for any units that we'd want to mount | |
1607 | * in the initrd, and call daemon-reload. We will get reinvoked as a generator, | |
1608 | * with /sysroot/etc/fstab available, and then we can write additional units based | |
1609 | * on that file. */ | |
1610 | static int run_generator(void) { | |
4c7cc696 | 1611 | int r; |
6b1dc2bd | 1612 | |
1d84ad94 | 1613 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); |
b5884878 | 1614 | if (r < 0) |
da927ba9 | 1615 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
94734142 | 1616 | |
2f3dfc6f | 1617 | (void) determine_root(); |
c1b9e3df | 1618 | (void) determine_usr(); |
2f3dfc6f | 1619 | |
028a981c | 1620 | if (arg_sysroot_check) { |
8f88e573 | 1621 | r = parse_fstab(/* prefix_sysroot = */ true); |
028a981c ZJS |
1622 | if (r == 0) |
1623 | log_debug("Nothing interesting found, not doing daemon-reload."); | |
1624 | if (r > 0) | |
1625 | r = do_daemon_reload(); | |
1626 | return r; | |
1627 | } | |
1628 | ||
ba2f3ec8 MY |
1629 | r = 0; |
1630 | ||
9f103625 TH |
1631 | /* Always honour root= and usr= in the kernel command line if we are in an initrd */ |
1632 | if (in_initrd()) { | |
ba2f3ec8 | 1633 | RET_GATHER(r, add_sysroot_mount()); |
003cba39 | 1634 | |
ba2f3ec8 | 1635 | RET_GATHER(r, add_sysroot_usr_mount_or_fallback()); |
91214a37 | 1636 | |
ba2f3ec8 MY |
1637 | RET_GATHER(r, add_volatile_root()); |
1638 | } else | |
1639 | RET_GATHER(r, add_volatile_var()); | |
5e398e54 | 1640 | |
e48fdd84 LP |
1641 | /* Honour /etc/fstab only when that's enabled */ |
1642 | if (arg_fstab_enabled) { | |
e48fdd84 | 1643 | /* Parse the local /etc/fstab, possibly from the initrd */ |
ba2f3ec8 | 1644 | RET_GATHER(r, parse_fstab(/* prefix_sysroot = */ false)); |
ac4785b0 | 1645 | |
e48fdd84 | 1646 | /* If running in the initrd also parse the /etc/fstab from the host */ |
2572957e | 1647 | if (in_initrd()) |
ba2f3ec8 | 1648 | RET_GATHER(r, parse_fstab(/* prefix_sysroot = */ true)); |
9b69569d | 1649 | else |
ba2f3ec8 | 1650 | RET_GATHER(r, generator_enable_remount_fs_service(arg_dest)); |
e48fdd84 | 1651 | } |
6b1dc2bd | 1652 | |
ba2f3ec8 | 1653 | RET_GATHER(r, add_mounts_from_cmdline()); |
55365b0a | 1654 | |
ba2f3ec8 | 1655 | RET_GATHER(r, add_mounts_from_creds(/* prefix_sysroot = */ false)); |
6ac62485 | 1656 | |
ba2f3ec8 MY |
1657 | if (in_initrd()) |
1658 | RET_GATHER(r, add_mounts_from_creds(/* prefix_sysroot = */ true)); | |
dfd10549 | 1659 | |
ba2f3ec8 | 1660 | return r; |
6b1dc2bd | 1661 | } |
a4ef3e4d | 1662 | |
028a981c ZJS |
1663 | static int run(int argc, char **argv) { |
1664 | arg_sysroot_check = invoked_as(argv, "systemd-sysroot-fstab-check"); | |
1665 | ||
1666 | if (arg_sysroot_check) { | |
1667 | /* Run as in systemd-sysroot-fstab-check mode */ | |
1668 | log_setup(); | |
1669 | ||
1670 | if (strv_length(argv) > 1) | |
1671 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1672 | "This program takes no arguments."); | |
1673 | if (!in_initrd()) | |
1674 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1675 | "This program is only useful in the initrd."); | |
1676 | } else { | |
1677 | /* Run in generator mode */ | |
1678 | log_setup_generator(); | |
1679 | ||
1680 | if (!IN_SET(strv_length(argv), 2, 4)) | |
1681 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1682 | "This program takes one or three arguments."); | |
1683 | ||
1684 | arg_dest = ASSERT_PTR(argv[1]); | |
1685 | arg_dest_late = ASSERT_PTR(argv[argc > 3 ? 3 : 1]); | |
1686 | } | |
1687 | ||
1688 | return run_generator(); | |
1689 | } | |
1690 | ||
1691 | DEFINE_MAIN_FUNCTION(run); |