chiark / gitweb /
fstab-generator: Do not check deviceless filesystems
[elogind.git] / src / shared / generator.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <unistd.h>
23
24 #include "util.h"
25 #include "special.h"
26 #include "mkdir.h"
27 #include "unit-name.h"
28 #include "generator.h"
29 #include "path-util.h"
30 #include "fstab-util.h"
31 #include "dropin.h"
32
33 int generator_write_fsck_deps(
34                 FILE *f,
35                 const char *dest,
36                 const char *what,
37                 const char *where,
38                 const char *fstype) {
39
40         assert(f);
41         assert(dest);
42         assert(what);
43         assert(where);
44
45         if (fstype_is_deviceless(fstype)) {
46                 log_debug("Not checking deviceless filesystem \"%s\".", fstype);
47                 return 0;
48         }
49
50         if (!is_device_path(what)) {
51                 log_warning("Checking was requested for \"%s\", but it is not a device.", what);
52                 return 0;
53         }
54
55         if (!isempty(fstype) && !streq(fstype, "auto")) {
56                 int r;
57                 r = fsck_exists(fstype);
58                 if (r == -ENOENT) {
59                         /* treat missing check as essentially OK */
60                         log_debug_errno(r, "Checking was requested for %s, but fsck.%s does not exist: %m", what, fstype);
61                         return 0;
62                 } else if (r < 0)
63                         return log_warning_errno(r, "Checking was requested for %s, but fsck.%s cannot be used: %m", what, fstype);
64         }
65
66         if (streq(where, "/")) {
67                 char *lnk;
68
69                 lnk = strjoina(dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
70
71                 mkdir_parents(lnk, 0755);
72                 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0)
73                         return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
74
75         } else {
76                 _cleanup_free_ char *fsck = NULL;
77
78                 fsck = unit_name_from_path_instance("systemd-fsck", what, ".service");
79                 if (!fsck)
80                         return log_oom();
81
82                 fprintf(f,
83                         "RequiresOverridable=%s\n"
84                         "After=%s\n",
85                         fsck,
86                         fsck);
87         }
88
89         return 0;
90 }
91
92 int generator_write_timeouts(const char *dir, const char *what, const char *where,
93                              const char *opts, char **filtered) {
94
95         /* Allow configuration how long we wait for a device that
96          * backs a mount point to show up. This is useful to support
97          * endless device timeouts for devices that show up only after
98          * user input, like crypto devices. */
99
100         _cleanup_free_ char *node = NULL, *unit = NULL, *timeout = NULL;
101         usec_t u;
102         int r;
103
104         r = fstab_filter_options(opts, "comment=systemd.device-timeout\0" "x-systemd.device-timeout\0",
105                                  NULL, &timeout, filtered);
106         if (r <= 0)
107                 return r;
108
109         r = parse_sec(timeout, &u);
110         if (r < 0) {
111                 log_warning("Failed to parse timeout for %s, ignoring: %s",
112                             where, timeout);
113                 return 0;
114         }
115
116         node = fstab_node_to_udev_node(what);
117         if (!node)
118                 return log_oom();
119
120         unit = unit_name_from_path(node, ".device");
121         if (!unit)
122                 return log_oom();
123
124         return write_drop_in_format(dir, unit, 50, "device-timeout",
125                                     "# Automatically generated by %s\n\n"
126                                     "[Unit]\nJobTimeoutSec=" USEC_FMT,
127                                     program_invocation_short_name,
128                                     u / USEC_PER_SEC);
129 }