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