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