]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fstab-generator/fstab-generator.c
b608eff630a7cb93b9f654159191b4281d996915
[thirdparty/systemd.git] / src / fstab-generator / fstab-generator.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <mntent.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "alloc-util.h"
27 #include "fd-util.h"
28 #include "fileio.h"
29 #include "fstab-util.h"
30 #include "generator.h"
31 #include "log.h"
32 #include "mkdir.h"
33 #include "mount-setup.h"
34 #include "mount-util.h"
35 #include "parse-util.h"
36 #include "path-util.h"
37 #include "proc-cmdline.h"
38 #include "special.h"
39 #include "stat-util.h"
40 #include "string-util.h"
41 #include "strv.h"
42 #include "unit-name.h"
43 #include "util.h"
44 #include "virt.h"
45
46 static const char *arg_dest = "/tmp";
47 static bool arg_fstab_enabled = true;
48 static char *arg_root_what = NULL;
49 static char *arg_root_fstype = NULL;
50 static char *arg_root_options = NULL;
51 static int arg_root_rw = -1;
52 static char *arg_usr_what = NULL;
53 static char *arg_usr_fstype = NULL;
54 static char *arg_usr_options = NULL;
55
56 static int add_swap(
57 const char *what,
58 struct mntent *me,
59 bool noauto,
60 bool nofail) {
61
62 _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
63 _cleanup_fclose_ FILE *f = NULL;
64 int r;
65
66 assert(what);
67 assert(me);
68
69 if (access("/proc/swaps", F_OK) < 0) {
70 log_info("Swap not supported, ignoring fstab swap entry for %s.", what);
71 return 0;
72 }
73
74 if (detect_container() > 0) {
75 log_info("Running in a container, ignoring fstab swap entry for %s.", what);
76 return 0;
77 }
78
79 r = unit_name_from_path(what, ".swap", &name);
80 if (r < 0)
81 return log_error_errno(r, "Failed to generate unit name: %m");
82
83 unit = strjoin(arg_dest, "/", name, NULL);
84 if (!unit)
85 return log_oom();
86
87 f = fopen(unit, "wxe");
88 if (!f)
89 return log_error_errno(errno,
90 errno == EEXIST ?
91 "Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?" :
92 "Failed to create unit file %s: %m",
93 unit);
94
95 fprintf(f,
96 "# Automatically generated by systemd-fstab-generator\n\n"
97 "[Unit]\n"
98 "SourcePath=/etc/fstab\n"
99 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
100 "[Swap]\n"
101 "What=%s\n",
102 what);
103
104 if (!isempty(me->mnt_opts) && !streq(me->mnt_opts, "defaults"))
105 fprintf(f, "Options=%s\n", me->mnt_opts);
106
107 r = fflush_and_check(f);
108 if (r < 0)
109 return log_error_errno(r, "Failed to write unit file %s: %m", unit);
110
111 /* use what as where, to have a nicer error message */
112 r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
113 if (r < 0)
114 return r;
115
116 if (!noauto) {
117 lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET,
118 nofail ? ".wants/" : ".requires/", name, NULL);
119 if (!lnk)
120 return log_oom();
121
122 mkdir_parents_label(lnk, 0755);
123 if (symlink(unit, lnk) < 0)
124 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
125 }
126
127 return 0;
128 }
129
130 static bool mount_is_network(struct mntent *me) {
131 assert(me);
132
133 return fstab_test_option(me->mnt_opts, "_netdev\0") ||
134 fstype_is_network(me->mnt_type);
135 }
136
137 static bool mount_in_initrd(struct mntent *me) {
138 assert(me);
139
140 return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") ||
141 streq(me->mnt_dir, "/usr");
142 }
143
144 static int write_idle_timeout(FILE *f, const char *where, const char *opts) {
145 _cleanup_free_ char *timeout = NULL;
146 char timespan[FORMAT_TIMESPAN_MAX];
147 usec_t u;
148 int r;
149
150 r = fstab_filter_options(opts, "x-systemd.idle-timeout\0", NULL, &timeout, NULL);
151 if (r < 0)
152 return log_warning_errno(r, "Failed to parse options: %m");
153 if (r == 0)
154 return 0;
155
156 r = parse_sec(timeout, &u);
157 if (r < 0) {
158 log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
159 return 0;
160 }
161
162 fprintf(f, "TimeoutIdleSec=%s\n", format_timespan(timespan, sizeof(timespan), u, 0));
163
164 return 0;
165 }
166
167 static int write_requires_after(FILE *f, const char *opts) {
168 _cleanup_strv_free_ char **names = NULL, **units = NULL;
169 _cleanup_free_ char *res = NULL;
170 char **s;
171 int r;
172
173 assert(f);
174 assert(opts);
175
176 r = fstab_extract_values(opts, "x-systemd.requires", &names);
177 if (r < 0)
178 return log_warning_errno(r, "Failed to parse options: %m");
179 if (r == 0)
180 return 0;
181
182 STRV_FOREACH(s, names) {
183 char *x;
184
185 r = unit_name_mangle_with_suffix(*s, UNIT_NAME_NOGLOB, ".mount", &x);
186 if (r < 0)
187 return log_error_errno(r, "Failed to generate unit name: %m");
188 r = strv_consume(&units, x);
189 if (r < 0)
190 return log_oom();
191 }
192
193 if (units) {
194 res = strv_join(units, " ");
195 if (!res)
196 return log_oom();
197 fprintf(f, "After=%1$s\nRequires=%1$s\n", res);
198 }
199
200 return 0;
201 }
202
203 static int write_requires_mounts_for(FILE *f, const char *opts) {
204 _cleanup_strv_free_ char **paths = NULL;
205 _cleanup_free_ char *res = NULL;
206 int r;
207
208 assert(f);
209 assert(opts);
210
211 r = fstab_extract_values(opts, "x-systemd.requires-mounts-for", &paths);
212 if (r < 0)
213 return log_warning_errno(r, "Failed to parse options: %m");
214 if (r == 0)
215 return 0;
216
217 res = strv_join(paths, " ");
218 if (!res)
219 return log_oom();
220
221 fprintf(f, "RequiresMountsFor=%s\n", res);
222
223 return 0;
224 }
225
226 static int add_mount(
227 const char *what,
228 const char *where,
229 const char *fstype,
230 const char *opts,
231 int passno,
232 bool noauto,
233 bool nofail,
234 bool automount,
235 const char *post,
236 const char *source) {
237
238 _cleanup_free_ char
239 *name = NULL, *unit = NULL, *lnk = NULL,
240 *automount_name = NULL, *automount_unit = NULL,
241 *filtered = NULL;
242 _cleanup_fclose_ FILE *f = NULL;
243 int r;
244
245 assert(what);
246 assert(where);
247 assert(opts);
248 assert(post);
249 assert(source);
250
251 if (streq_ptr(fstype, "autofs"))
252 return 0;
253
254 if (!is_path(where)) {
255 log_warning("Mount point %s is not a valid path, ignoring.", where);
256 return 0;
257 }
258
259 if (mount_point_is_api(where) ||
260 mount_point_ignore(where))
261 return 0;
262
263 if (path_equal(where, "/")) {
264 if (noauto)
265 log_warning("Ignoring \"noauto\" for root device");
266 if (nofail)
267 log_warning("Ignoring \"nofail\" for root device");
268 if (automount)
269 log_warning("Ignoring automount option for root device");
270
271 noauto = nofail = automount = false;
272 }
273
274 r = unit_name_from_path(where, ".mount", &name);
275 if (r < 0)
276 return log_error_errno(r, "Failed to generate unit name: %m");
277
278 unit = strjoin(arg_dest, "/", name, NULL);
279 if (!unit)
280 return log_oom();
281
282 f = fopen(unit, "wxe");
283 if (!f)
284 return log_error_errno(errno,
285 errno == EEXIST ?
286 "Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?" :
287 "Failed to create unit file %s: %m",
288 unit);
289
290 fprintf(f,
291 "# Automatically generated by systemd-fstab-generator\n\n"
292 "[Unit]\n"
293 "SourcePath=%s\n"
294 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
295 source);
296
297 if (!noauto && !nofail && !automount)
298 fprintf(f, "Before=%s\n", post);
299
300 if (!automount && opts) {
301 r = write_requires_after(f, opts);
302 if (r < 0)
303 return r;
304 r = write_requires_mounts_for(f, opts);
305 if (r < 0)
306 return r;
307 }
308
309 if (passno != 0) {
310 r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
311 if (r < 0)
312 return r;
313 }
314
315 fprintf(f,
316 "\n"
317 "[Mount]\n"
318 "What=%s\n"
319 "Where=%s\n",
320 what,
321 where);
322
323 if (!isempty(fstype) && !streq(fstype, "auto"))
324 fprintf(f, "Type=%s\n", fstype);
325
326 r = generator_write_timeouts(arg_dest, what, where, opts, &filtered);
327 if (r < 0)
328 return r;
329
330 if (!isempty(filtered) && !streq(filtered, "defaults"))
331 fprintf(f, "Options=%s\n", filtered);
332
333 r = fflush_and_check(f);
334 if (r < 0)
335 return log_error_errno(r, "Failed to write unit file %s: %m", unit);
336
337 if (!noauto && !automount) {
338 lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", name, NULL);
339 if (!lnk)
340 return log_oom();
341
342 mkdir_parents_label(lnk, 0755);
343 if (symlink(unit, lnk) < 0)
344 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
345 }
346
347 if (automount) {
348 r = unit_name_from_path(where, ".automount", &automount_name);
349 if (r < 0)
350 return log_error_errno(r, "Failed to generate unit name: %m");
351
352 automount_unit = strjoin(arg_dest, "/", automount_name, NULL);
353 if (!automount_unit)
354 return log_oom();
355
356 fclose(f);
357 f = fopen(automount_unit, "wxe");
358 if (!f)
359 return log_error_errno(errno, "Failed to create unit file %s: %m", automount_unit);
360
361 fprintf(f,
362 "# Automatically generated by systemd-fstab-generator\n\n"
363 "[Unit]\n"
364 "SourcePath=%s\n"
365 "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
366 source);
367
368 fprintf(f, "Before=%s\n", post);
369
370 if (opts) {
371 r = write_requires_after(f, opts);
372 if (r < 0)
373 return r;
374 r = write_requires_mounts_for(f, opts);
375 if (r < 0)
376 return r;
377 }
378
379 fprintf(f,
380 "\n"
381 "[Automount]\n"
382 "Where=%s\n",
383 where);
384
385 r = write_idle_timeout(f, where, opts);
386 if (r < 0)
387 return r;
388
389 r = fflush_and_check(f);
390 if (r < 0)
391 return log_error_errno(r, "Failed to write unit file %s: %m", automount_unit);
392
393 free(lnk);
394 lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name, NULL);
395 if (!lnk)
396 return log_oom();
397
398 mkdir_parents_label(lnk, 0755);
399 if (symlink(automount_unit, lnk) < 0)
400 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
401 }
402
403 return 0;
404 }
405
406 static int parse_fstab(bool initrd) {
407 _cleanup_endmntent_ FILE *f = NULL;
408 const char *fstab_path;
409 struct mntent *me;
410 int r = 0;
411
412 fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab";
413 f = setmntent(fstab_path, "re");
414 if (!f) {
415 if (errno == ENOENT)
416 return 0;
417
418 return log_error_errno(errno, "Failed to open %s: %m", fstab_path);
419 }
420
421 while ((me = getmntent(f))) {
422 _cleanup_free_ char *where = NULL, *what = NULL;
423 bool noauto, nofail;
424 int k;
425
426 if (initrd && !mount_in_initrd(me))
427 continue;
428
429 what = fstab_node_to_udev_node(me->mnt_fsname);
430 if (!what)
431 return log_oom();
432
433 if (is_device_path(what) && path_is_read_only_fs("sys") > 0) {
434 log_info("Running in a container, ignoring fstab device entry for %s.", what);
435 continue;
436 }
437
438 where = initrd ? strappend("/sysroot/", me->mnt_dir) : strdup(me->mnt_dir);
439 if (!where)
440 return log_oom();
441
442 if (is_path(where))
443 path_kill_slashes(where);
444
445 noauto = fstab_test_yes_no_option(me->mnt_opts, "noauto\0" "auto\0");
446 nofail = fstab_test_yes_no_option(me->mnt_opts, "nofail\0" "fail\0");
447 log_debug("Found entry what=%s where=%s type=%s nofail=%s noauto=%s",
448 what, where, me->mnt_type,
449 yes_no(noauto), yes_no(nofail));
450
451 if (streq(me->mnt_type, "swap"))
452 k = add_swap(what, me, noauto, nofail);
453 else {
454 bool automount;
455 const char *post;
456
457 automount = fstab_test_option(me->mnt_opts,
458 "comment=systemd.automount\0"
459 "x-systemd.automount\0");
460 if (initrd)
461 post = SPECIAL_INITRD_FS_TARGET;
462 else if (mount_is_network(me))
463 post = SPECIAL_REMOTE_FS_TARGET;
464 else
465 post = SPECIAL_LOCAL_FS_TARGET;
466
467 k = add_mount(what,
468 where,
469 me->mnt_type,
470 me->mnt_opts,
471 me->mnt_passno,
472 noauto,
473 nofail,
474 automount,
475 post,
476 fstab_path);
477 }
478
479 if (k < 0)
480 r = k;
481 }
482
483 return r;
484 }
485
486 static int add_sysroot_mount(void) {
487 _cleanup_free_ char *what = NULL;
488 const char *opts;
489 int r;
490
491 if (isempty(arg_root_what)) {
492 log_debug("Could not find a root= entry on the kernel command line.");
493 return 0;
494 }
495
496 if (streq(arg_root_what, "gpt-auto")) {
497 /* This is handled by the gpt-auto generator */
498 log_debug("Skipping root directory handling, as gpt-auto was requested.");
499 return 0;
500 }
501
502 if (path_equal(arg_root_what, "/dev/nfs")) {
503 /* This is handled by the kernel or the initrd */
504 log_debug("Skipping root directory handling, as /dev/nfs was requested.");
505 return 0;
506 }
507
508 what = fstab_node_to_udev_node(arg_root_what);
509 if (!what)
510 return log_oom();
511
512 if (!arg_root_options)
513 opts = arg_root_rw > 0 ? "rw" : "ro";
514 else if (arg_root_rw >= 0 ||
515 !fstab_test_option(arg_root_options, "ro\0" "rw\0"))
516 opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
517 else
518 opts = arg_root_options;
519
520 log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype));
521
522 if (is_device_path(what)) {
523 r = generator_write_initrd_root_device_deps(arg_dest, what);
524 if (r < 0)
525 return r;
526 }
527
528 return add_mount(what,
529 "/sysroot",
530 arg_root_fstype,
531 opts,
532 is_device_path(what) ? 1 : 0, /* passno */
533 false, /* noauto off */
534 false, /* nofail off */
535 false, /* automount off */
536 SPECIAL_INITRD_ROOT_FS_TARGET,
537 "/proc/cmdline");
538 }
539
540 static int add_sysroot_usr_mount(void) {
541 _cleanup_free_ char *what = NULL;
542 const char *opts;
543
544 if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
545 return 0;
546
547 if (arg_root_what && !arg_usr_what) {
548 /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */
549 arg_usr_what = strdup(arg_root_what);
550 if (!arg_usr_what)
551 return log_oom();
552 }
553
554 if (arg_root_fstype && !arg_usr_fstype) {
555 arg_usr_fstype = strdup(arg_root_fstype);
556 if (!arg_usr_fstype)
557 return log_oom();
558 }
559
560 if (arg_root_options && !arg_usr_options) {
561 arg_usr_options = strdup(arg_root_options);
562 if (!arg_usr_options)
563 return log_oom();
564 }
565
566 if (!arg_usr_what)
567 return 0;
568
569 what = fstab_node_to_udev_node(arg_usr_what);
570 if (!what)
571 return log_oom();
572
573 if (!arg_usr_options)
574 opts = arg_root_rw > 0 ? "rw" : "ro";
575 else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0"))
576 opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
577 else
578 opts = arg_usr_options;
579
580 log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
581 return add_mount(what,
582 "/sysroot/usr",
583 arg_usr_fstype,
584 opts,
585 is_device_path(what) ? 1 : 0, /* passno */
586 false, /* noauto off */
587 false, /* nofail off */
588 false, /* automount off */
589 SPECIAL_INITRD_FS_TARGET,
590 "/proc/cmdline");
591 }
592
593 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
594 int r;
595
596 /* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
597 * instance should take precedence. In the case of multiple rootflags=
598 * or usrflags= the arguments should be concatenated */
599
600 if (STR_IN_SET(key, "fstab", "rd.fstab") && value) {
601
602 r = parse_boolean(value);
603 if (r < 0)
604 log_warning("Failed to parse fstab switch %s. Ignoring.", value);
605 else
606 arg_fstab_enabled = r;
607
608 } else if (streq(key, "root") && value) {
609
610 if (free_and_strdup(&arg_root_what, value) < 0)
611 return log_oom();
612
613 } else if (streq(key, "rootfstype") && value) {
614
615 if (free_and_strdup(&arg_root_fstype, value) < 0)
616 return log_oom();
617
618 } else if (streq(key, "rootflags") && value) {
619 char *o;
620
621 o = arg_root_options ?
622 strjoin(arg_root_options, ",", value, NULL) :
623 strdup(value);
624 if (!o)
625 return log_oom();
626
627 free(arg_root_options);
628 arg_root_options = o;
629
630 } else if (streq(key, "mount.usr") && value) {
631
632 if (free_and_strdup(&arg_usr_what, value) < 0)
633 return log_oom();
634
635 } else if (streq(key, "mount.usrfstype") && value) {
636
637 if (free_and_strdup(&arg_usr_fstype, value) < 0)
638 return log_oom();
639
640 } else if (streq(key, "mount.usrflags") && value) {
641 char *o;
642
643 o = arg_usr_options ?
644 strjoin(arg_usr_options, ",", value, NULL) :
645 strdup(value);
646 if (!o)
647 return log_oom();
648
649 free(arg_usr_options);
650 arg_usr_options = o;
651
652 } else if (streq(key, "rw") && !value)
653 arg_root_rw = true;
654 else if (streq(key, "ro") && !value)
655 arg_root_rw = false;
656
657 return 0;
658 }
659
660 int main(int argc, char *argv[]) {
661 int r = 0;
662
663 if (argc > 1 && argc != 4) {
664 log_error("This program takes three or no arguments.");
665 return EXIT_FAILURE;
666 }
667
668 if (argc > 1)
669 arg_dest = argv[1];
670
671 log_set_target(LOG_TARGET_SAFE);
672 log_parse_environment();
673 log_open();
674
675 umask(0022);
676
677 r = parse_proc_cmdline(parse_proc_cmdline_item, NULL);
678 if (r < 0)
679 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
680
681 /* Always honour root= and usr= in the kernel command line if we are in an initrd */
682 if (in_initrd()) {
683 int k;
684
685 r = add_sysroot_mount();
686
687 k = add_sysroot_usr_mount();
688 if (k < 0)
689 r = k;
690 } else
691 r = 0;
692
693 /* Honour /etc/fstab only when that's enabled */
694 if (arg_fstab_enabled) {
695 int k;
696
697 log_debug("Parsing /etc/fstab");
698
699 /* Parse the local /etc/fstab, possibly from the initrd */
700 k = parse_fstab(false);
701 if (k < 0)
702 r = k;
703
704 /* If running in the initrd also parse the /etc/fstab from the host */
705 if (in_initrd()) {
706 log_debug("Parsing /sysroot/etc/fstab");
707
708 k = parse_fstab(true);
709 if (k < 0)
710 r = k;
711 }
712 }
713
714 free(arg_root_what);
715 free(arg_root_fstype);
716 free(arg_root_options);
717
718 free(arg_usr_what);
719 free(arg_usr_fstype);
720 free(arg_usr_options);
721
722 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
723 }