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