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