]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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" |
3ffd4af2 | 8 | #include "fd-util.h" |
0d39fa9c | 9 | #include "fileio.h" |
98bad05e | 10 | #include "fs-util.h" |
d15d0333 | 11 | #include "fstab-util.h" |
07630cea LP |
12 | #include "generator.h" |
13 | #include "log.h" | |
a4ef3e4d | 14 | #include "main-func.h" |
07630cea | 15 | #include "mkdir.h" |
6b1dc2bd | 16 | #include "mount-setup.h" |
4349cd7c | 17 | #include "mount-util.h" |
049af8ad | 18 | #include "mountpoint-util.h" |
6bedfcbb | 19 | #include "parse-util.h" |
07630cea | 20 | #include "path-util.h" |
4e731273 | 21 | #include "proc-cmdline.h" |
6b1dc2bd | 22 | #include "special.h" |
98bad05e | 23 | #include "specifier.h" |
8fcde012 | 24 | #include "stat-util.h" |
07630cea | 25 | #include "string-util.h" |
059cb385 | 26 | #include "strv.h" |
07630cea LP |
27 | #include "unit-name.h" |
28 | #include "util.h" | |
689aede8 | 29 | #include "virt.h" |
91214a37 | 30 | #include "volatile-util.h" |
6b1dc2bd | 31 | |
4191418b ZJS |
32 | typedef enum MountpointFlags { |
33 | NOAUTO = 1 << 0, | |
34 | NOFAIL = 1 << 1, | |
35 | AUTOMOUNT = 1 << 2, | |
da495a03 | 36 | MAKEFS = 1 << 3, |
7f2806d5 | 37 | GROWFS = 1 << 4, |
4191418b ZJS |
38 | } MountpointFlags; |
39 | ||
7a44c7e3 ZJS |
40 | static const char *arg_dest = NULL; |
41 | static const char *arg_dest_late = NULL; | |
e48fdd84 | 42 | static bool arg_fstab_enabled = true; |
6db615c1 LP |
43 | static char *arg_root_what = NULL; |
44 | static char *arg_root_fstype = NULL; | |
45 | static char *arg_root_options = NULL; | |
2f3dfc6f | 46 | static char *arg_root_hash = NULL; |
6db615c1 | 47 | static int arg_root_rw = -1; |
9f103625 TH |
48 | static char *arg_usr_what = NULL; |
49 | static char *arg_usr_fstype = NULL; | |
50 | static char *arg_usr_options = NULL; | |
91214a37 | 51 | static VolatileMode arg_volatile_mode = _VOLATILE_MODE_INVALID; |
6b1dc2bd | 52 | |
a4ef3e4d YW |
53 | STATIC_DESTRUCTOR_REGISTER(arg_root_what, freep); |
54 | STATIC_DESTRUCTOR_REGISTER(arg_root_fstype, freep); | |
55 | STATIC_DESTRUCTOR_REGISTER(arg_root_options, freep); | |
56 | STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep); | |
57 | STATIC_DESTRUCTOR_REGISTER(arg_usr_what, freep); | |
58 | STATIC_DESTRUCTOR_REGISTER(arg_usr_fstype, freep); | |
59 | STATIC_DESTRUCTOR_REGISTER(arg_usr_options, freep); | |
60 | ||
d5cc4be2 LP |
61 | static int write_options(FILE *f, const char *options) { |
62 | _cleanup_free_ char *o = NULL; | |
63 | ||
64 | if (isempty(options)) | |
65 | return 0; | |
66 | ||
67 | if (streq(options, "defaults")) | |
68 | return 0; | |
69 | ||
98bad05e | 70 | o = specifier_escape(options); |
d5cc4be2 LP |
71 | if (!o) |
72 | return log_oom(); | |
73 | ||
74 | fprintf(f, "Options=%s\n", o); | |
75 | return 1; | |
76 | } | |
77 | ||
19d0833b LP |
78 | static int write_what(FILE *f, const char *what) { |
79 | _cleanup_free_ char *w = NULL; | |
80 | ||
98bad05e | 81 | w = specifier_escape(what); |
19d0833b LP |
82 | if (!w) |
83 | return log_oom(); | |
84 | ||
85 | fprintf(f, "What=%s\n", w); | |
86 | return 1; | |
87 | } | |
88 | ||
5607d856 ZJS |
89 | static int add_swap( |
90 | const char *what, | |
91 | struct mntent *me, | |
4191418b | 92 | MountpointFlags flags) { |
5607d856 | 93 | |
fb883e75 | 94 | _cleanup_free_ char *name = NULL; |
7fd1b19b | 95 | _cleanup_fclose_ FILE *f = NULL; |
bf1d7ba7 | 96 | int r; |
6b1dc2bd LP |
97 | |
98 | assert(what); | |
99 | assert(me); | |
100 | ||
00b4ffde LP |
101 | if (access("/proc/swaps", F_OK) < 0) { |
102 | log_info("Swap not supported, ignoring fstab swap entry for %s.", what); | |
103 | return 0; | |
104 | } | |
105 | ||
75f86906 | 106 | if (detect_container() > 0) { |
689aede8 LP |
107 | log_info("Running in a container, ignoring fstab swap entry for %s.", what); |
108 | return 0; | |
109 | } | |
110 | ||
7410616c LP |
111 | r = unit_name_from_path(what, ".swap", &name); |
112 | if (r < 0) | |
113 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
6b1dc2bd | 114 | |
ed4ad488 | 115 | r = generator_open_unit_file(arg_dest, fstab_path(), name, &f); |
fb883e75 ZJS |
116 | if (r < 0) |
117 | return r; | |
0d536673 | 118 | |
ed4ad488 ZJS |
119 | fprintf(f, |
120 | "[Unit]\n" | |
a7e88558 LP |
121 | "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n" |
122 | "SourcePath=%s\n", | |
ed4ad488 | 123 | fstab_path()); |
19d0833b | 124 | |
a7e88558 LP |
125 | r = generator_write_blockdev_dependency(f, what); |
126 | if (r < 0) | |
127 | return r; | |
128 | ||
129 | fprintf(f, | |
130 | "\n" | |
131 | "[Swap]\n"); | |
132 | ||
19d0833b LP |
133 | r = write_what(f, what); |
134 | if (r < 0) | |
135 | return r; | |
6b1dc2bd | 136 | |
d5cc4be2 LP |
137 | r = write_options(f, me->mnt_opts); |
138 | if (r < 0) | |
139 | return r; | |
6b1dc2bd | 140 | |
47cb901e | 141 | r = fflush_and_check(f); |
23bbb0de | 142 | if (r < 0) |
fb883e75 | 143 | return log_error_errno(r, "Failed to write unit file %s: %m", name); |
6b1dc2bd | 144 | |
b3208b66 | 145 | /* use what as where, to have a nicer error message */ |
bf1d7ba7 | 146 | r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL); |
b3208b66 ZJS |
147 | if (r < 0) |
148 | return r; | |
149 | ||
da495a03 ZJS |
150 | if (flags & MAKEFS) { |
151 | r = generator_hook_up_mkswap(arg_dest, what); | |
152 | if (r < 0) | |
153 | return r; | |
154 | } | |
155 | ||
7f2806d5 ZJS |
156 | if (flags & GROWFS) |
157 | /* TODO: swap devices must be wiped and recreated */ | |
158 | log_warning("%s: growing swap devices is currently unsupported.", what); | |
159 | ||
4191418b | 160 | if (!(flags & NOAUTO)) { |
630d30d3 | 161 | r = generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, |
4191418b | 162 | (flags & NOFAIL) ? "wants" : "requires", name); |
630d30d3 ZJS |
163 | if (r < 0) |
164 | return r; | |
4e82fe52 TG |
165 | } |
166 | ||
d0aa9ce5 | 167 | return 0; |
6b1dc2bd LP |
168 | } |
169 | ||
6b1dc2bd LP |
170 | static bool mount_is_network(struct mntent *me) { |
171 | assert(me); | |
172 | ||
b9f111b9 ZJS |
173 | return fstab_test_option(me->mnt_opts, "_netdev\0") || |
174 | fstype_is_network(me->mnt_type); | |
6b1dc2bd LP |
175 | } |
176 | ||
3d22d1ab TG |
177 | static bool mount_in_initrd(struct mntent *me) { |
178 | assert(me); | |
179 | ||
b9f111b9 ZJS |
180 | return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") || |
181 | streq(me->mnt_dir, "/usr"); | |
3d22d1ab TG |
182 | } |
183 | ||
33a4c983 LP |
184 | static int write_timeout( |
185 | FILE *f, | |
186 | const char *where, | |
187 | const char *opts, | |
188 | const char *filter, | |
189 | const char *variable) { | |
190 | ||
deb0a77c MO |
191 | _cleanup_free_ char *timeout = NULL; |
192 | char timespan[FORMAT_TIMESPAN_MAX]; | |
193 | usec_t u; | |
194 | int r; | |
195 | ||
110773f6 | 196 | r = fstab_filter_options(opts, filter, NULL, &timeout, NULL); |
deb0a77c MO |
197 | if (r < 0) |
198 | return log_warning_errno(r, "Failed to parse options: %m"); | |
199 | if (r == 0) | |
200 | return 0; | |
201 | ||
0004f698 | 202 | r = parse_sec_fix_0(timeout, &u); |
deb0a77c MO |
203 | if (r < 0) { |
204 | log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout); | |
205 | return 0; | |
206 | } | |
207 | ||
110773f6 | 208 | fprintf(f, "%s=%s\n", variable, format_timespan(timespan, sizeof(timespan), u, 0)); |
deb0a77c MO |
209 | |
210 | return 0; | |
211 | } | |
2e852276 | 212 | |
110773f6 CH |
213 | static int write_idle_timeout(FILE *f, const char *where, const char *opts) { |
214 | return write_timeout(f, where, opts, | |
215 | "x-systemd.idle-timeout\0", "TimeoutIdleSec"); | |
216 | } | |
217 | ||
218 | static int write_mount_timeout(FILE *f, const char *where, const char *opts) { | |
219 | return write_timeout(f, where, opts, | |
220 | "x-systemd.mount-timeout\0", "TimeoutSec"); | |
221 | } | |
222 | ||
33a4c983 LP |
223 | static int write_dependency( |
224 | FILE *f, | |
225 | const char *opts, | |
226 | const char *filter, | |
227 | const char *format) { | |
228 | ||
3519d230 KZ |
229 | _cleanup_strv_free_ char **names = NULL, **units = NULL; |
230 | _cleanup_free_ char *res = NULL; | |
231 | char **s; | |
232 | int r; | |
233 | ||
234 | assert(f); | |
235 | assert(opts); | |
236 | ||
ae325185 | 237 | r = fstab_extract_values(opts, filter, &names); |
3519d230 KZ |
238 | if (r < 0) |
239 | return log_warning_errno(r, "Failed to parse options: %m"); | |
240 | if (r == 0) | |
241 | return 0; | |
242 | ||
243 | STRV_FOREACH(s, names) { | |
244 | char *x; | |
245 | ||
df7c4eb6 | 246 | r = unit_name_mangle_with_suffix(*s, "as dependency", 0, ".mount", &x); |
3519d230 KZ |
247 | if (r < 0) |
248 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
33a4c983 | 249 | |
3519d230 KZ |
250 | r = strv_consume(&units, x); |
251 | if (r < 0) | |
252 | return log_oom(); | |
253 | } | |
254 | ||
255 | if (units) { | |
256 | res = strv_join(units, " "); | |
257 | if (!res) | |
258 | return log_oom(); | |
4a027e19 MP |
259 | #pragma GCC diagnostic push |
260 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" | |
ae325185 | 261 | fprintf(f, format, res); |
4a027e19 | 262 | #pragma GCC diagnostic pop |
3519d230 KZ |
263 | } |
264 | ||
265 | return 0; | |
266 | } | |
267 | ||
ae325185 | 268 | static int write_after(FILE *f, const char *opts) { |
33a4c983 LP |
269 | return write_dependency(f, opts, |
270 | "x-systemd.after", "After=%1$s\n"); | |
ae325185 RB |
271 | } |
272 | ||
273 | static int write_requires_after(FILE *f, const char *opts) { | |
274 | return write_dependency(f, opts, | |
275 | "x-systemd.requires", "After=%1$s\nRequires=%1$s\n"); | |
276 | } | |
277 | ||
278 | static int write_before(FILE *f, const char *opts) { | |
279 | return write_dependency(f, opts, | |
280 | "x-systemd.before", "Before=%1$s\n"); | |
281 | } | |
282 | ||
3519d230 | 283 | static int write_requires_mounts_for(FILE *f, const char *opts) { |
98bad05e | 284 | _cleanup_strv_free_ char **paths = NULL, **paths_escaped = NULL; |
3519d230 KZ |
285 | _cleanup_free_ char *res = NULL; |
286 | int r; | |
287 | ||
288 | assert(f); | |
289 | assert(opts); | |
290 | ||
291 | r = fstab_extract_values(opts, "x-systemd.requires-mounts-for", &paths); | |
292 | if (r < 0) | |
293 | return log_warning_errno(r, "Failed to parse options: %m"); | |
294 | if (r == 0) | |
295 | return 0; | |
296 | ||
98bad05e LP |
297 | r = specifier_escape_strv(paths, &paths_escaped); |
298 | if (r < 0) | |
299 | return log_error_errno(r, "Failed to escape paths: %m"); | |
300 | ||
301 | res = strv_join(paths_escaped, " "); | |
3519d230 KZ |
302 | if (!res) |
303 | return log_oom(); | |
304 | ||
305 | fprintf(f, "RequiresMountsFor=%s\n", res); | |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
e8d2f6cd | 310 | static int add_mount( |
91214a37 | 311 | const char *dest, |
e8d2f6cd LP |
312 | const char *what, |
313 | const char *where, | |
634735b5 | 314 | const char *original_where, |
6db615c1 | 315 | const char *fstype, |
e8d2f6cd LP |
316 | const char *opts, |
317 | int passno, | |
4191418b | 318 | MountpointFlags flags, |
e8d2f6cd | 319 | const char *post, |
e8d2f6cd | 320 | const char *source) { |
e48fdd84 | 321 | |
7fd1b19b | 322 | _cleanup_free_ char |
da495a03 | 323 | *name = NULL, |
fb883e75 | 324 | *automount_name = NULL, |
98bad05e LP |
325 | *filtered = NULL, |
326 | *where_escaped = NULL; | |
be02c1cf | 327 | _cleanup_strv_free_ char **wanted_by = NULL, **required_by = NULL; |
7fd1b19b | 328 | _cleanup_fclose_ FILE *f = NULL; |
94192cda | 329 | int r; |
6b1dc2bd LP |
330 | |
331 | assert(what); | |
332 | assert(where); | |
5e398e54 | 333 | assert(opts); |
0d3d3be1 | 334 | assert(post); |
5e398e54 | 335 | assert(source); |
6b1dc2bd | 336 | |
6db615c1 | 337 | if (streq_ptr(fstype, "autofs")) |
6b1dc2bd LP |
338 | return 0; |
339 | ||
340 | if (!is_path(where)) { | |
341 | log_warning("Mount point %s is not a valid path, ignoring.", where); | |
342 | return 0; | |
343 | } | |
344 | ||
345 | if (mount_point_is_api(where) || | |
346 | mount_point_ignore(where)) | |
347 | return 0; | |
348 | ||
be02c1cf AR |
349 | r = fstab_extract_values(opts, "x-systemd.wanted-by", &wanted_by); |
350 | if (r < 0) | |
351 | return r; | |
352 | ||
353 | r = fstab_extract_values(opts, "x-systemd.required-by", &required_by); | |
354 | if (r < 0) | |
355 | return r; | |
356 | ||
e48fdd84 | 357 | if (path_equal(where, "/")) { |
4191418b | 358 | if (flags & NOAUTO) |
2e852276 | 359 | log_warning("Ignoring \"noauto\" for root device"); |
4191418b | 360 | if (flags & NOFAIL) |
2e852276 | 361 | log_warning("Ignoring \"nofail\" for root device"); |
4191418b | 362 | if (flags & AUTOMOUNT) |
2e852276 | 363 | log_warning("Ignoring automount option for root device"); |
be02c1cf AR |
364 | if (!strv_isempty(wanted_by)) |
365 | log_warning("Ignoring \"x-systemd.wanted-by=\" for root device"); | |
366 | if (!strv_isempty(required_by)) | |
367 | log_warning("Ignoring \"x-systemd.required-by=\" for root device"); | |
2e852276 | 368 | |
be02c1cf AR |
369 | required_by = strv_free(required_by); |
370 | wanted_by = strv_free(wanted_by); | |
4191418b | 371 | SET_FLAG(flags, NOAUTO | NOFAIL | AUTOMOUNT, false); |
e48fdd84 LP |
372 | } |
373 | ||
7410616c LP |
374 | r = unit_name_from_path(where, ".mount", &name); |
375 | if (r < 0) | |
376 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
6b1dc2bd | 377 | |
ed4ad488 | 378 | r = generator_open_unit_file(dest, fstab_path(), name, &f); |
fb883e75 ZJS |
379 | if (r < 0) |
380 | return r; | |
0d536673 | 381 | |
5e398e54 | 382 | fprintf(f, |
c3834f9b | 383 | "[Unit]\n" |
a7e88558 LP |
384 | "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n" |
385 | "SourcePath=%s\n", | |
c3834f9b | 386 | source); |
6b1dc2bd | 387 | |
30fdb896 JL |
388 | /* All mounts under /sysroot need to happen later, at initrd-fs.target time. IOW, it's not |
389 | * technically part of the basic initrd filesystem itself, and so shouldn't inherit the default | |
390 | * Before=local-fs.target dependency. */ | |
391 | if (in_initrd() && path_startswith(where, "/sysroot")) | |
392 | fprintf(f, "DefaultDependencies=no\n"); | |
393 | ||
4191418b | 394 | if (STRPTR_IN_SET(fstype, "nfs", "nfs4") && !(flags & AUTOMOUNT) && |
65e1dee7 N |
395 | fstab_test_yes_no_option(opts, "bg\0" "fg\0")) { |
396 | /* The default retry timeout that mount.nfs uses for 'bg' mounts | |
397 | * is 10000 minutes, where as it uses 2 minutes for 'fg' mounts. | |
398 | * As we are making 'bg' mounts look like an 'fg' mount to | |
399 | * mount.nfs (so systemd can manage the job-control aspects of 'bg'), | |
400 | * we need to explicitly preserve that default, and also ensure | |
401 | * the systemd mount-timeout doesn't interfere. | |
402 | * By placing these options first, they can be over-ridden by | |
403 | * settings in /etc/fstab. */ | |
404 | opts = strjoina("x-systemd.mount-timeout=infinity,retry=10000,", opts, ",fg"); | |
4191418b | 405 | SET_FLAG(flags, NOFAIL, true); |
65e1dee7 N |
406 | } |
407 | ||
4191418b | 408 | if (!(flags & NOFAIL) && !(flags & AUTOMOUNT)) |
5ecdcf41 | 409 | fprintf(f, "Before=%s\n", post); |
6b1dc2bd | 410 | |
4191418b | 411 | if (!(flags & AUTOMOUNT) && opts) { |
ae325185 RB |
412 | r = write_after(f, opts); |
413 | if (r < 0) | |
414 | return r; | |
3519d230 | 415 | r = write_requires_after(f, opts); |
ae325185 RB |
416 | if (r < 0) |
417 | return r; | |
418 | r = write_before(f, opts); | |
3519d230 KZ |
419 | if (r < 0) |
420 | return r; | |
421 | r = write_requires_mounts_for(f, opts); | |
422 | if (r < 0) | |
423 | return r; | |
424 | } | |
425 | ||
e48fdd84 | 426 | if (passno != 0) { |
91214a37 | 427 | r = generator_write_fsck_deps(f, dest, what, where, fstype); |
e48fdd84 LP |
428 | if (r < 0) |
429 | return r; | |
430 | } | |
64e70e4b | 431 | |
a7e88558 LP |
432 | r = generator_write_blockdev_dependency(f, what); |
433 | if (r < 0) | |
434 | return r; | |
435 | ||
436 | fprintf(f, | |
437 | "\n" | |
438 | "[Mount]\n"); | |
439 | ||
634735b5 CW |
440 | if (original_where) |
441 | fprintf(f, "# Canonicalized from %s\n", original_where); | |
98bad05e LP |
442 | |
443 | where_escaped = specifier_escape(where); | |
444 | if (!where_escaped) | |
445 | return log_oom(); | |
446 | fprintf(f, "Where=%s\n", where_escaped); | |
6db615c1 | 447 | |
19d0833b LP |
448 | r = write_what(f, what); |
449 | if (r < 0) | |
450 | return r; | |
451 | ||
98bad05e LP |
452 | if (!isempty(fstype) && !streq(fstype, "auto")) { |
453 | _cleanup_free_ char *t; | |
454 | ||
455 | t = specifier_escape(fstype); | |
456 | if (!t) | |
457 | return -ENOMEM; | |
458 | ||
459 | fprintf(f, "Type=%s\n", t); | |
460 | } | |
6b1dc2bd | 461 | |
91214a37 | 462 | r = generator_write_timeouts(dest, what, where, opts, &filtered); |
29686440 ZJS |
463 | if (r < 0) |
464 | return r; | |
465 | ||
4195077a MK |
466 | r = generator_write_device_deps(dest, what, where, opts); |
467 | if (r < 0) | |
468 | return r; | |
469 | ||
110773f6 CH |
470 | r = write_mount_timeout(f, where, opts); |
471 | if (r < 0) | |
472 | return r; | |
473 | ||
d5cc4be2 LP |
474 | r = write_options(f, filtered); |
475 | if (r < 0) | |
476 | return r; | |
5e398e54 | 477 | |
4652c56c ZJS |
478 | r = fflush_and_check(f); |
479 | if (r < 0) | |
fb883e75 | 480 | return log_error_errno(r, "Failed to write unit file %s: %m", name); |
6b1dc2bd | 481 | |
da495a03 ZJS |
482 | if (flags & MAKEFS) { |
483 | r = generator_hook_up_mkfs(dest, what, where, fstype); | |
484 | if (r < 0) | |
485 | return r; | |
486 | } | |
487 | ||
7f2806d5 ZJS |
488 | if (flags & GROWFS) { |
489 | r = generator_hook_up_growfs(dest, where, post); | |
490 | if (r < 0) | |
491 | return r; | |
492 | } | |
493 | ||
be02c1cf AR |
494 | if (!FLAGS_SET(flags, AUTOMOUNT)) { |
495 | if (!FLAGS_SET(flags, NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) { | |
496 | r = generator_add_symlink(dest, post, | |
497 | (flags & NOFAIL) ? "wants" : "requires", name); | |
498 | if (r < 0) | |
499 | return r; | |
500 | } else { | |
501 | char **s; | |
502 | ||
503 | STRV_FOREACH(s, wanted_by) { | |
504 | r = generator_add_symlink(dest, *s, "wants", name); | |
505 | if (r < 0) | |
506 | return r; | |
507 | } | |
508 | ||
509 | STRV_FOREACH(s, required_by) { | |
510 | r = generator_add_symlink(dest, *s, "requires", name); | |
511 | if (r < 0) | |
512 | return r; | |
513 | } | |
514 | } | |
515 | } else { | |
7410616c LP |
516 | r = unit_name_from_path(where, ".automount", &automount_name); |
517 | if (r < 0) | |
518 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
6b1dc2bd | 519 | |
8a7033ac | 520 | f = safe_fclose(f); |
6b1dc2bd | 521 | |
ed4ad488 | 522 | r = generator_open_unit_file(dest, fstab_path(), automount_name, &f); |
fb883e75 ZJS |
523 | if (r < 0) |
524 | return r; | |
0d536673 | 525 | |
6b1dc2bd | 526 | fprintf(f, |
6b1dc2bd | 527 | "[Unit]\n" |
c3834f9b LP |
528 | "SourcePath=%s\n" |
529 | "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n", | |
700e07ff HH |
530 | source); |
531 | ||
0d3d3be1 | 532 | fprintf(f, "Before=%s\n", post); |
700e07ff | 533 | |
3519d230 | 534 | if (opts) { |
ae325185 RB |
535 | r = write_after(f, opts); |
536 | if (r < 0) | |
537 | return r; | |
3519d230 | 538 | r = write_requires_after(f, opts); |
ae325185 RB |
539 | if (r < 0) |
540 | return r; | |
541 | r = write_before(f, opts); | |
3519d230 KZ |
542 | if (r < 0) |
543 | return r; | |
544 | r = write_requires_mounts_for(f, opts); | |
545 | if (r < 0) | |
546 | return r; | |
547 | } | |
548 | ||
700e07ff | 549 | fprintf(f, |
bd10a84b | 550 | "\n" |
6b1dc2bd LP |
551 | "[Automount]\n" |
552 | "Where=%s\n", | |
98bad05e | 553 | where_escaped); |
6b1dc2bd | 554 | |
336b5c61 | 555 | r = write_idle_timeout(f, where, opts); |
deb0a77c MO |
556 | if (r < 0) |
557 | return r; | |
558 | ||
4652c56c ZJS |
559 | r = fflush_and_check(f); |
560 | if (r < 0) | |
fb883e75 | 561 | return log_error_errno(r, "Failed to write unit file %s: %m", automount_name); |
6b1dc2bd | 562 | |
630d30d3 | 563 | r = generator_add_symlink(dest, post, |
4191418b | 564 | (flags & NOFAIL) ? "wants" : "requires", automount_name); |
630d30d3 ZJS |
565 | if (r < 0) |
566 | return r; | |
6b1dc2bd LP |
567 | } |
568 | ||
d0aa9ce5 | 569 | return 0; |
6b1dc2bd LP |
570 | } |
571 | ||
e48fdd84 | 572 | static int parse_fstab(bool initrd) { |
6db615c1 | 573 | _cleanup_endmntent_ FILE *f = NULL; |
ed4ad488 | 574 | const char *fstab; |
6b1dc2bd | 575 | struct mntent *me; |
e48fdd84 | 576 | int r = 0; |
6b1dc2bd | 577 | |
ed4ad488 ZJS |
578 | fstab = initrd ? "/sysroot/etc/fstab" : fstab_path(); |
579 | log_debug("Parsing %s...", fstab); | |
01a0f7d0 | 580 | |
ed4ad488 | 581 | f = setmntent(fstab, "re"); |
6b1dc2bd LP |
582 | if (!f) { |
583 | if (errno == ENOENT) | |
584 | return 0; | |
585 | ||
ed4ad488 | 586 | return log_error_errno(errno, "Failed to open %s: %m", fstab); |
6b1dc2bd LP |
587 | } |
588 | ||
589 | while ((me = getmntent(f))) { | |
634735b5 | 590 | _cleanup_free_ char *where = NULL, *what = NULL, *canonical_where = NULL; |
7f2806d5 | 591 | bool makefs, growfs, noauto, nofail; |
6b1dc2bd LP |
592 | int k; |
593 | ||
3d22d1ab TG |
594 | if (initrd && !mount_in_initrd(me)) |
595 | continue; | |
596 | ||
6b1dc2bd | 597 | what = fstab_node_to_udev_node(me->mnt_fsname); |
e48fdd84 LP |
598 | if (!what) |
599 | return log_oom(); | |
600 | ||
a3e7ea02 | 601 | if (is_device_path(what) && path_is_read_only_fs("/sys") > 0) { |
689aede8 LP |
602 | log_info("Running in a container, ignoring fstab device entry for %s.", what); |
603 | continue; | |
604 | } | |
605 | ||
98eda38a | 606 | where = strdup(me->mnt_dir); |
e48fdd84 | 607 | if (!where) |
5862d652 | 608 | return log_oom(); |
6b1dc2bd | 609 | |
634735b5 | 610 | if (is_path(where)) { |
858d36c1 | 611 | path_simplify(where, false); |
f1a2c758 | 612 | |
634735b5 CW |
613 | /* Follow symlinks here; see 5261ba901845c084de5a8fd06500ed09bfb0bd80 which makes sense for |
614 | * mount units, but causes problems since it historically worked to have symlinks in e.g. | |
615 | * /etc/fstab. So we canonicalize here. Note that we use CHASE_NONEXISTENT to handle the case | |
616 | * where a symlink refers to another mount target; this works assuming the sub-mountpoint | |
f1a2c758 | 617 | * target is the final directory. */ |
634735b5 CW |
618 | r = chase_symlinks(where, initrd ? "/sysroot" : NULL, |
619 | CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, | |
a5648b80 | 620 | &canonical_where, NULL); |
f1a2c758 LP |
621 | if (r < 0) /* If we can't canonicalize we continue on as if it wasn't a symlink */ |
622 | log_debug_errno(r, "Failed to read symlink target for %s, ignoring: %m", where); | |
623 | else if (streq(canonical_where, where)) /* If it was fully canonicalized, suppress the change */ | |
624 | canonical_where = mfree(canonical_where); | |
625 | else | |
626 | log_debug("Canonicalized what=%s where=%s to %s", what, where, canonical_where); | |
634735b5 | 627 | } |
6b1dc2bd | 628 | |
da495a03 | 629 | makefs = fstab_test_option(me->mnt_opts, "x-systemd.makefs\0"); |
7f2806d5 | 630 | growfs = fstab_test_option(me->mnt_opts, "x-systemd.growfs\0"); |
b9f111b9 ZJS |
631 | noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0"); |
632 | nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0"); | |
f1a2c758 | 633 | |
ac1d4c79 | 634 | log_debug("Found entry what=%s where=%s type=%s makefs=%s growfs=%s noauto=%s nofail=%s", |
5607d856 | 635 | what, where, me->mnt_type, |
ac1d4c79 | 636 | yes_no(makefs), yes_no(growfs), |
5607d856 | 637 | yes_no(noauto), yes_no(nofail)); |
6b1dc2bd LP |
638 | |
639 | if (streq(me->mnt_type, "swap")) | |
4191418b | 640 | k = add_swap(what, me, |
7f2806d5 | 641 | makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL); |
5e398e54 | 642 | else { |
5607d856 | 643 | bool automount; |
80c3b720 | 644 | const char *post; |
5e398e54 | 645 | |
b9f111b9 ZJS |
646 | automount = fstab_test_option(me->mnt_opts, |
647 | "comment=systemd.automount\0" | |
648 | "x-systemd.automount\0"); | |
e48fdd84 | 649 | if (initrd) |
700e07ff | 650 | post = SPECIAL_INITRD_FS_TARGET; |
e48fdd84 | 651 | else if (mount_is_network(me)) |
0c17fbce | 652 | post = SPECIAL_REMOTE_FS_TARGET; |
e48fdd84 | 653 | else |
0c17fbce | 654 | post = SPECIAL_LOCAL_FS_TARGET; |
5e398e54 | 655 | |
91214a37 LP |
656 | k = add_mount(arg_dest, |
657 | what, | |
634735b5 CW |
658 | canonical_where ?: where, |
659 | canonical_where ? where: NULL, | |
6db615c1 LP |
660 | me->mnt_type, |
661 | me->mnt_opts, | |
662 | me->mnt_passno, | |
7f2806d5 | 663 | makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL | automount*AUTOMOUNT, |
6db615c1 | 664 | post, |
ed4ad488 | 665 | fstab); |
5e398e54 | 666 | } |
6b1dc2bd | 667 | |
7f2806d5 | 668 | if (r >= 0 && k < 0) |
6b1dc2bd LP |
669 | r = k; |
670 | } | |
671 | ||
6b1dc2bd LP |
672 | return r; |
673 | } | |
674 | ||
2e852276 | 675 | static int add_sysroot_mount(void) { |
75a59316 ZJS |
676 | _cleanup_free_ char *what = NULL; |
677 | const char *opts; | |
7163e1ca | 678 | int r; |
5e398e54 | 679 | |
093c2cfe TH |
680 | if (isempty(arg_root_what)) { |
681 | log_debug("Could not find a root= entry on the kernel command line."); | |
682 | return 0; | |
135b5212 | 683 | } |
5e398e54 | 684 | |
138f4c69 LP |
685 | if (streq(arg_root_what, "gpt-auto")) { |
686 | /* This is handled by the gpt-auto generator */ | |
687 | log_debug("Skipping root directory handling, as gpt-auto was requested."); | |
688 | return 0; | |
689 | } | |
690 | ||
e34e72fb | 691 | if (path_equal(arg_root_what, "/dev/nfs")) { |
3ade16c9 HH |
692 | /* This is handled by the kernel or the initrd */ |
693 | log_debug("Skipping root directory handling, as /dev/nfs was requested."); | |
694 | return 0; | |
695 | } | |
696 | ||
093c2cfe TH |
697 | what = fstab_node_to_udev_node(arg_root_what); |
698 | if (!what) | |
171181bc | 699 | return log_oom(); |
093c2cfe | 700 | |
6db615c1 | 701 | if (!arg_root_options) |
75a59316 ZJS |
702 | opts = arg_root_rw > 0 ? "rw" : "ro"; |
703 | else if (arg_root_rw >= 0 || | |
d15d0333 | 704 | !fstab_test_option(arg_root_options, "ro\0" "rw\0")) |
63c372cb | 705 | opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro"); |
75a59316 ZJS |
706 | else |
707 | opts = arg_root_options; | |
5e398e54 | 708 | |
6db615c1 | 709 | log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype)); |
7163e1ca DD |
710 | |
711 | if (is_device_path(what)) { | |
712 | r = generator_write_initrd_root_device_deps(arg_dest, what); | |
713 | if (r < 0) | |
714 | return r; | |
715 | } | |
716 | ||
91214a37 LP |
717 | return add_mount(arg_dest, |
718 | what, | |
6db615c1 | 719 | "/sysroot", |
634735b5 | 720 | NULL, |
6db615c1 | 721 | arg_root_fstype, |
75a59316 | 722 | opts, |
f113f8e3 | 723 | is_device_path(what) ? 1 : 0, /* passno */ |
7f2806d5 | 724 | 0, /* makefs off, growfs off, noauto off, nofail off, automount off */ |
6db615c1 LP |
725 | SPECIAL_INITRD_ROOT_FS_TARGET, |
726 | "/proc/cmdline"); | |
5e398e54 TG |
727 | } |
728 | ||
2e852276 | 729 | static int add_sysroot_usr_mount(void) { |
9f103625 TH |
730 | _cleanup_free_ char *what = NULL; |
731 | const char *opts; | |
732 | ||
733 | if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options) | |
734 | return 0; | |
735 | ||
736 | if (arg_root_what && !arg_usr_what) { | |
40472036 | 737 | /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */ |
9f103625 | 738 | arg_usr_what = strdup(arg_root_what); |
9f103625 TH |
739 | if (!arg_usr_what) |
740 | return log_oom(); | |
741 | } | |
742 | ||
743 | if (arg_root_fstype && !arg_usr_fstype) { | |
744 | arg_usr_fstype = strdup(arg_root_fstype); | |
9f103625 TH |
745 | if (!arg_usr_fstype) |
746 | return log_oom(); | |
747 | } | |
748 | ||
749 | if (arg_root_options && !arg_usr_options) { | |
750 | arg_usr_options = strdup(arg_root_options); | |
9f103625 TH |
751 | if (!arg_usr_options) |
752 | return log_oom(); | |
753 | } | |
754 | ||
eb580002 | 755 | if (!arg_usr_what) |
9f103625 TH |
756 | return 0; |
757 | ||
758 | what = fstab_node_to_udev_node(arg_usr_what); | |
47be5f07 LP |
759 | if (!what) |
760 | return log_oom(); | |
9f103625 | 761 | |
eb580002 MM |
762 | if (!arg_usr_options) |
763 | opts = arg_root_rw > 0 ? "rw" : "ro"; | |
d15d0333 | 764 | else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0")) |
63c372cb | 765 | opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro"); |
eb580002 MM |
766 | else |
767 | opts = arg_usr_options; | |
9f103625 TH |
768 | |
769 | log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype)); | |
91214a37 LP |
770 | return add_mount(arg_dest, |
771 | what, | |
9f103625 | 772 | "/sysroot/usr", |
634735b5 | 773 | NULL, |
9f103625 TH |
774 | arg_usr_fstype, |
775 | opts, | |
f113f8e3 | 776 | is_device_path(what) ? 1 : 0, /* passno */ |
4191418b | 777 | 0, |
104bc12f | 778 | SPECIAL_INITRD_FS_TARGET, |
9f103625 TH |
779 | "/proc/cmdline"); |
780 | } | |
781 | ||
91214a37 | 782 | static int add_volatile_root(void) { |
1de7f825 | 783 | |
91214a37 | 784 | /* Let's add in systemd-remount-volatile.service which will remount the root device to tmpfs if this is |
1de7f825 | 785 | * requested (or as an overlayfs), leaving only /usr from the root mount inside. */ |
91214a37 | 786 | |
1de7f825 | 787 | if (!IN_SET(arg_volatile_mode, VOLATILE_YES, VOLATILE_OVERLAY)) |
00bb366d | 788 | return 0; |
91214a37 | 789 | |
00bb366d | 790 | return generator_add_symlink(arg_dest, SPECIAL_INITRD_ROOT_FS_TARGET, "requires", |
312da637 | 791 | SYSTEM_DATA_UNIT_PATH "/" SPECIAL_VOLATILE_ROOT_SERVICE); |
91214a37 LP |
792 | } |
793 | ||
794 | static int add_volatile_var(void) { | |
795 | ||
796 | if (arg_volatile_mode != VOLATILE_STATE) | |
797 | return 0; | |
798 | ||
799 | /* If requested, mount /var as tmpfs, but do so only if there's nothing else defined for this. */ | |
800 | ||
801 | return add_mount(arg_dest_late, | |
802 | "tmpfs", | |
803 | "/var", | |
634735b5 | 804 | NULL, |
91214a37 LP |
805 | "tmpfs", |
806 | "mode=0755", | |
807 | 0, | |
4191418b | 808 | 0, |
91214a37 LP |
809 | SPECIAL_LOCAL_FS_TARGET, |
810 | "/proc/cmdline"); | |
811 | } | |
812 | ||
96287a49 | 813 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
74df0fca | 814 | int r; |
94734142 | 815 | |
9f103625 TH |
816 | /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last |
817 | * instance should take precedence. In the case of multiple rootflags= | |
818 | * or usrflags= the arguments should be concatenated */ | |
6db615c1 | 819 | |
1d84ad94 | 820 | if (STR_IN_SET(key, "fstab", "rd.fstab")) { |
e48fdd84 | 821 | |
1d84ad94 | 822 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 823 | if (r < 0) |
059cb385 | 824 | log_warning("Failed to parse fstab switch %s. Ignoring.", value); |
141a79f4 | 825 | else |
e48fdd84 | 826 | arg_fstab_enabled = r; |
94734142 | 827 | |
1d84ad94 LP |
828 | } else if (streq(key, "root")) { |
829 | ||
830 | if (proc_cmdline_value_missing(key, value)) | |
831 | return 0; | |
6db615c1 | 832 | |
f88dc3ed | 833 | if (free_and_strdup(&arg_root_what, value) < 0) |
6db615c1 LP |
834 | return log_oom(); |
835 | ||
1d84ad94 LP |
836 | } else if (streq(key, "rootfstype")) { |
837 | ||
838 | if (proc_cmdline_value_missing(key, value)) | |
839 | return 0; | |
6db615c1 | 840 | |
f88dc3ed | 841 | if (free_and_strdup(&arg_root_fstype, value) < 0) |
6db615c1 LP |
842 | return log_oom(); |
843 | ||
1d84ad94 | 844 | } else if (streq(key, "rootflags")) { |
6db615c1 | 845 | |
1d84ad94 LP |
846 | if (proc_cmdline_value_missing(key, value)) |
847 | return 0; | |
848 | ||
6c782b7a | 849 | if (!strextend_with_separator(&arg_root_options, ",", value, NULL)) |
6db615c1 LP |
850 | return log_oom(); |
851 | ||
2f3dfc6f LP |
852 | } else if (streq(key, "roothash")) { |
853 | ||
854 | if (proc_cmdline_value_missing(key, value)) | |
855 | return 0; | |
856 | ||
857 | if (free_and_strdup(&arg_root_hash, value) < 0) | |
858 | return log_oom(); | |
6db615c1 | 859 | |
1d84ad94 LP |
860 | } else if (streq(key, "mount.usr")) { |
861 | ||
862 | if (proc_cmdline_value_missing(key, value)) | |
863 | return 0; | |
9f103625 TH |
864 | |
865 | if (free_and_strdup(&arg_usr_what, value) < 0) | |
866 | return log_oom(); | |
867 | ||
1d84ad94 LP |
868 | } else if (streq(key, "mount.usrfstype")) { |
869 | ||
870 | if (proc_cmdline_value_missing(key, value)) | |
871 | return 0; | |
9f103625 TH |
872 | |
873 | if (free_and_strdup(&arg_usr_fstype, value) < 0) | |
874 | return log_oom(); | |
875 | ||
1d84ad94 | 876 | } else if (streq(key, "mount.usrflags")) { |
9f103625 | 877 | |
1d84ad94 LP |
878 | if (proc_cmdline_value_missing(key, value)) |
879 | return 0; | |
880 | ||
6c782b7a | 881 | if (!strextend_with_separator(&arg_usr_options, ",", value, NULL)) |
9f103625 TH |
882 | return log_oom(); |
883 | ||
6db615c1 LP |
884 | } else if (streq(key, "rw") && !value) |
885 | arg_root_rw = true; | |
886 | else if (streq(key, "ro") && !value) | |
887 | arg_root_rw = false; | |
91214a37 LP |
888 | else if (streq(key, "systemd.volatile")) { |
889 | VolatileMode m; | |
890 | ||
891 | if (value) { | |
892 | m = volatile_mode_from_string(value); | |
893 | if (m < 0) | |
894 | log_warning("Failed to parse systemd.volatile= argument: %s", value); | |
895 | else | |
896 | arg_volatile_mode = m; | |
897 | } else | |
898 | arg_volatile_mode = VOLATILE_YES; | |
899 | } | |
94734142 | 900 | |
d0aa9ce5 | 901 | return 0; |
94734142 LP |
902 | } |
903 | ||
2f3dfc6f LP |
904 | static int determine_root(void) { |
905 | /* If we have a root hash but no root device then Verity is used, and we use the "root" DM device as root. */ | |
906 | ||
907 | if (arg_root_what) | |
908 | return 0; | |
909 | ||
910 | if (!arg_root_hash) | |
911 | return 0; | |
912 | ||
913 | arg_root_what = strdup("/dev/mapper/root"); | |
914 | if (!arg_root_what) | |
915 | return log_oom(); | |
916 | ||
917 | log_info("Using verity root device %s.", arg_root_what); | |
918 | ||
919 | return 1; | |
920 | } | |
921 | ||
7a44c7e3 | 922 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
2572957e | 923 | int r, r2 = 0, r3 = 0; |
6b1dc2bd | 924 | |
7a44c7e3 ZJS |
925 | assert_se(arg_dest = dest); |
926 | assert_se(arg_dest_late = dest_late); | |
6b1dc2bd | 927 | |
1d84ad94 | 928 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); |
b5884878 | 929 | if (r < 0) |
da927ba9 | 930 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
94734142 | 931 | |
2f3dfc6f LP |
932 | (void) determine_root(); |
933 | ||
9f103625 TH |
934 | /* Always honour root= and usr= in the kernel command line if we are in an initrd */ |
935 | if (in_initrd()) { | |
2e852276 | 936 | r = add_sysroot_mount(); |
003cba39 | 937 | |
2572957e | 938 | r2 = add_sysroot_usr_mount(); |
91214a37 | 939 | |
2572957e | 940 | r3 = add_volatile_root(); |
003cba39 | 941 | } else |
91214a37 | 942 | r = add_volatile_var(); |
5e398e54 | 943 | |
e48fdd84 LP |
944 | /* Honour /etc/fstab only when that's enabled */ |
945 | if (arg_fstab_enabled) { | |
e48fdd84 | 946 | /* Parse the local /etc/fstab, possibly from the initrd */ |
2572957e | 947 | r2 = parse_fstab(false); |
ac4785b0 | 948 | |
e48fdd84 | 949 | /* If running in the initrd also parse the /etc/fstab from the host */ |
2572957e ZJS |
950 | if (in_initrd()) |
951 | r3 = parse_fstab(true); | |
9b69569d ZJS |
952 | else |
953 | r3 = generator_enable_remount_fs_service(arg_dest); | |
e48fdd84 | 954 | } |
6b1dc2bd | 955 | |
2572957e | 956 | return r < 0 ? r : r2 < 0 ? r2 : r3; |
6b1dc2bd | 957 | } |
a4ef3e4d | 958 | |
7a44c7e3 | 959 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |