]>
Commit | Line | Data |
---|---|---|
6b1dc2bd LP |
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 | ||
6b1dc2bd | 20 | #include <errno.h> |
07630cea LP |
21 | #include <mntent.h> |
22 | #include <stdio.h> | |
6b1dc2bd LP |
23 | #include <string.h> |
24 | #include <unistd.h> | |
25 | ||
b5efdb8a | 26 | #include "alloc-util.h" |
3ffd4af2 | 27 | #include "fd-util.h" |
0d39fa9c | 28 | #include "fileio.h" |
d15d0333 | 29 | #include "fstab-util.h" |
07630cea LP |
30 | #include "generator.h" |
31 | #include "log.h" | |
32 | #include "mkdir.h" | |
6b1dc2bd | 33 | #include "mount-setup.h" |
4349cd7c | 34 | #include "mount-util.h" |
6bedfcbb | 35 | #include "parse-util.h" |
07630cea | 36 | #include "path-util.h" |
4e731273 | 37 | #include "proc-cmdline.h" |
6b1dc2bd | 38 | #include "special.h" |
8fcde012 | 39 | #include "stat-util.h" |
07630cea | 40 | #include "string-util.h" |
059cb385 | 41 | #include "strv.h" |
07630cea LP |
42 | #include "unit-name.h" |
43 | #include "util.h" | |
689aede8 | 44 | #include "virt.h" |
6b1dc2bd LP |
45 | |
46 | static const char *arg_dest = "/tmp"; | |
e48fdd84 | 47 | static bool arg_fstab_enabled = true; |
6db615c1 LP |
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; | |
9f103625 TH |
52 | static char *arg_usr_what = NULL; |
53 | static char *arg_usr_fstype = NULL; | |
54 | static char *arg_usr_options = NULL; | |
6b1dc2bd | 55 | |
5607d856 ZJS |
56 | static int add_swap( |
57 | const char *what, | |
58 | struct mntent *me, | |
59 | bool noauto, | |
60 | bool nofail) { | |
61 | ||
bf1d7ba7 | 62 | _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL; |
7fd1b19b | 63 | _cleanup_fclose_ FILE *f = NULL; |
bf1d7ba7 | 64 | int r; |
6b1dc2bd LP |
65 | |
66 | assert(what); | |
67 | assert(me); | |
68 | ||
00b4ffde LP |
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 | ||
75f86906 | 74 | if (detect_container() > 0) { |
689aede8 LP |
75 | log_info("Running in a container, ignoring fstab swap entry for %s.", what); |
76 | return 0; | |
77 | } | |
78 | ||
7410616c LP |
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"); | |
6b1dc2bd | 82 | |
b7def684 | 83 | unit = strjoin(arg_dest, "/", name, NULL); |
d0aa9ce5 ZJS |
84 | if (!unit) |
85 | return log_oom(); | |
6b1dc2bd LP |
86 | |
87 | f = fopen(unit, "wxe"); | |
d710aaf7 ZJS |
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); | |
6b1dc2bd | 94 | |
6b1dc2bd | 95 | fprintf(f, |
64347fc2 TG |
96 | "# Automatically generated by systemd-fstab-generator\n\n" |
97 | "[Unit]\n" | |
c3834f9b LP |
98 | "SourcePath=/etc/fstab\n" |
99 | "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n" | |
6b1dc2bd LP |
100 | "[Swap]\n" |
101 | "What=%s\n", | |
102 | what); | |
103 | ||
bf1d7ba7 KZ |
104 | if (!isempty(me->mnt_opts) && !streq(me->mnt_opts, "defaults")) |
105 | fprintf(f, "Options=%s\n", me->mnt_opts); | |
6b1dc2bd | 106 | |
47cb901e | 107 | r = fflush_and_check(f); |
23bbb0de MS |
108 | if (r < 0) |
109 | return log_error_errno(r, "Failed to write unit file %s: %m", unit); | |
6b1dc2bd | 110 | |
b3208b66 | 111 | /* use what as where, to have a nicer error message */ |
bf1d7ba7 | 112 | r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL); |
b3208b66 ZJS |
113 | if (r < 0) |
114 | return r; | |
115 | ||
4e82fe52 | 116 | if (!noauto) { |
5607d856 ZJS |
117 | lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET, |
118 | nofail ? ".wants/" : ".requires/", name, NULL); | |
4e82fe52 TG |
119 | if (!lnk) |
120 | return log_oom(); | |
121 | ||
122 | mkdir_parents_label(lnk, 0755); | |
4a62c710 MS |
123 | if (symlink(unit, lnk) < 0) |
124 | return log_error_errno(errno, "Failed to create symlink %s: %m", lnk); | |
4e82fe52 TG |
125 | } |
126 | ||
d0aa9ce5 | 127 | return 0; |
6b1dc2bd LP |
128 | } |
129 | ||
6b1dc2bd LP |
130 | static bool mount_is_network(struct mntent *me) { |
131 | assert(me); | |
132 | ||
b9f111b9 ZJS |
133 | return fstab_test_option(me->mnt_opts, "_netdev\0") || |
134 | fstype_is_network(me->mnt_type); | |
6b1dc2bd LP |
135 | } |
136 | ||
3d22d1ab TG |
137 | static bool mount_in_initrd(struct mntent *me) { |
138 | assert(me); | |
139 | ||
b9f111b9 ZJS |
140 | return fstab_test_option(me->mnt_opts, "x-initrd.mount\0") || |
141 | streq(me->mnt_dir, "/usr"); | |
3d22d1ab TG |
142 | } |
143 | ||
336b5c61 | 144 | static int write_idle_timeout(FILE *f, const char *where, const char *opts) { |
deb0a77c MO |
145 | _cleanup_free_ char *timeout = NULL; |
146 | char timespan[FORMAT_TIMESPAN_MAX]; | |
147 | usec_t u; | |
148 | int r; | |
149 | ||
336b5c61 | 150 | r = fstab_filter_options(opts, "x-systemd.idle-timeout\0", NULL, &timeout, NULL); |
deb0a77c MO |
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 | } | |
2e852276 | 166 | |
3519d230 KZ |
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 | ||
e8d2f6cd LP |
226 | static int add_mount( |
227 | const char *what, | |
228 | const char *where, | |
6db615c1 | 229 | const char *fstype, |
e8d2f6cd LP |
230 | const char *opts, |
231 | int passno, | |
232 | bool noauto, | |
233 | bool nofail, | |
234 | bool automount, | |
e8d2f6cd | 235 | const char *post, |
e8d2f6cd | 236 | const char *source) { |
e48fdd84 | 237 | |
7fd1b19b | 238 | _cleanup_free_ char |
1dc2ced4 | 239 | *name = NULL, *unit = NULL, *lnk = NULL, |
29686440 ZJS |
240 | *automount_name = NULL, *automount_unit = NULL, |
241 | *filtered = NULL; | |
7fd1b19b | 242 | _cleanup_fclose_ FILE *f = NULL; |
94192cda | 243 | int r; |
6b1dc2bd LP |
244 | |
245 | assert(what); | |
246 | assert(where); | |
5e398e54 | 247 | assert(opts); |
0d3d3be1 | 248 | assert(post); |
5e398e54 | 249 | assert(source); |
6b1dc2bd | 250 | |
6db615c1 | 251 | if (streq_ptr(fstype, "autofs")) |
6b1dc2bd LP |
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 | ||
e48fdd84 | 263 | if (path_equal(where, "/")) { |
2e852276 ZJS |
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; | |
e48fdd84 LP |
272 | } |
273 | ||
7410616c LP |
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"); | |
6b1dc2bd | 277 | |
b7def684 | 278 | unit = strjoin(arg_dest, "/", name, NULL); |
d0aa9ce5 ZJS |
279 | if (!unit) |
280 | return log_oom(); | |
6b1dc2bd LP |
281 | |
282 | f = fopen(unit, "wxe"); | |
d710aaf7 ZJS |
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); | |
6b1dc2bd | 289 | |
5e398e54 | 290 | fprintf(f, |
c3834f9b LP |
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); | |
6b1dc2bd | 296 | |
0d3d3be1 | 297 | if (!noauto && !nofail && !automount) |
5ecdcf41 | 298 | fprintf(f, "Before=%s\n", post); |
6b1dc2bd | 299 | |
3519d230 KZ |
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 | ||
e48fdd84 | 309 | if (passno != 0) { |
6db615c1 | 310 | r = generator_write_fsck_deps(f, arg_dest, what, where, fstype); |
e48fdd84 LP |
311 | if (r < 0) |
312 | return r; | |
313 | } | |
64e70e4b | 314 | |
6b1dc2bd LP |
315 | fprintf(f, |
316 | "\n" | |
317 | "[Mount]\n" | |
318 | "What=%s\n" | |
6db615c1 | 319 | "Where=%s\n", |
6b1dc2bd | 320 | what, |
6db615c1 LP |
321 | where); |
322 | ||
323 | if (!isempty(fstype) && !streq(fstype, "auto")) | |
324 | fprintf(f, "Type=%s\n", fstype); | |
6b1dc2bd | 325 | |
29686440 ZJS |
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); | |
5e398e54 | 332 | |
4652c56c ZJS |
333 | r = fflush_and_check(f); |
334 | if (r < 0) | |
335 | return log_error_errno(r, "Failed to write unit file %s: %m", unit); | |
6b1dc2bd | 336 | |
2b14df4a FB |
337 | if (!noauto && !automount) { |
338 | lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", name, NULL); | |
5ecdcf41 LP |
339 | if (!lnk) |
340 | return log_oom(); | |
341 | ||
342 | mkdir_parents_label(lnk, 0755); | |
4a62c710 MS |
343 | if (symlink(unit, lnk) < 0) |
344 | return log_error_errno(errno, "Failed to create symlink %s: %m", lnk); | |
6b1dc2bd LP |
345 | } |
346 | ||
e48fdd84 | 347 | if (automount) { |
7410616c LP |
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"); | |
6b1dc2bd | 351 | |
b7def684 | 352 | automount_unit = strjoin(arg_dest, "/", automount_name, NULL); |
d0aa9ce5 ZJS |
353 | if (!automount_unit) |
354 | return log_oom(); | |
6b1dc2bd LP |
355 | |
356 | fclose(f); | |
357 | f = fopen(automount_unit, "wxe"); | |
4a62c710 MS |
358 | if (!f) |
359 | return log_error_errno(errno, "Failed to create unit file %s: %m", automount_unit); | |
6b1dc2bd LP |
360 | |
361 | fprintf(f, | |
362 | "# Automatically generated by systemd-fstab-generator\n\n" | |
363 | "[Unit]\n" | |
c3834f9b LP |
364 | "SourcePath=%s\n" |
365 | "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n", | |
700e07ff HH |
366 | source); |
367 | ||
0d3d3be1 | 368 | fprintf(f, "Before=%s\n", post); |
700e07ff | 369 | |
3519d230 KZ |
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 | ||
700e07ff | 379 | fprintf(f, |
bd10a84b | 380 | "\n" |
6b1dc2bd LP |
381 | "[Automount]\n" |
382 | "Where=%s\n", | |
6b1dc2bd LP |
383 | where); |
384 | ||
336b5c61 | 385 | r = write_idle_timeout(f, where, opts); |
deb0a77c MO |
386 | if (r < 0) |
387 | return r; | |
388 | ||
4652c56c ZJS |
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); | |
6b1dc2bd LP |
392 | |
393 | free(lnk); | |
b7def684 | 394 | lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name, NULL); |
d0aa9ce5 ZJS |
395 | if (!lnk) |
396 | return log_oom(); | |
6b1dc2bd | 397 | |
d2e54fae | 398 | mkdir_parents_label(lnk, 0755); |
4a62c710 MS |
399 | if (symlink(automount_unit, lnk) < 0) |
400 | return log_error_errno(errno, "Failed to create symlink %s: %m", lnk); | |
6b1dc2bd LP |
401 | } |
402 | ||
d0aa9ce5 | 403 | return 0; |
6b1dc2bd LP |
404 | } |
405 | ||
e48fdd84 | 406 | static int parse_fstab(bool initrd) { |
6db615c1 | 407 | _cleanup_endmntent_ FILE *f = NULL; |
e48fdd84 | 408 | const char *fstab_path; |
6b1dc2bd | 409 | struct mntent *me; |
e48fdd84 | 410 | int r = 0; |
6b1dc2bd | 411 | |
e48fdd84 LP |
412 | fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab"; |
413 | f = setmntent(fstab_path, "re"); | |
6b1dc2bd LP |
414 | if (!f) { |
415 | if (errno == ENOENT) | |
416 | return 0; | |
417 | ||
592288a2 | 418 | return log_error_errno(errno, "Failed to open %s: %m", fstab_path); |
6b1dc2bd LP |
419 | } |
420 | ||
421 | while ((me = getmntent(f))) { | |
7fd1b19b | 422 | _cleanup_free_ char *where = NULL, *what = NULL; |
5607d856 | 423 | bool noauto, nofail; |
6b1dc2bd LP |
424 | int k; |
425 | ||
3d22d1ab TG |
426 | if (initrd && !mount_in_initrd(me)) |
427 | continue; | |
428 | ||
6b1dc2bd | 429 | what = fstab_node_to_udev_node(me->mnt_fsname); |
e48fdd84 LP |
430 | if (!what) |
431 | return log_oom(); | |
432 | ||
00b4ffde | 433 | if (is_device_path(what) && path_is_read_only_fs("sys") > 0) { |
689aede8 LP |
434 | log_info("Running in a container, ignoring fstab device entry for %s.", what); |
435 | continue; | |
436 | } | |
437 | ||
e48fdd84 LP |
438 | where = initrd ? strappend("/sysroot/", me->mnt_dir) : strdup(me->mnt_dir); |
439 | if (!where) | |
5862d652 | 440 | return log_oom(); |
6b1dc2bd | 441 | |
ec6ceb18 | 442 | if (is_path(where)) |
6b1dc2bd LP |
443 | path_kill_slashes(where); |
444 | ||
b9f111b9 ZJS |
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"); | |
5607d856 ZJS |
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)); | |
6b1dc2bd LP |
450 | |
451 | if (streq(me->mnt_type, "swap")) | |
5607d856 | 452 | k = add_swap(what, me, noauto, nofail); |
5e398e54 | 453 | else { |
5607d856 | 454 | bool automount; |
80c3b720 | 455 | const char *post; |
5e398e54 | 456 | |
b9f111b9 ZJS |
457 | automount = fstab_test_option(me->mnt_opts, |
458 | "comment=systemd.automount\0" | |
459 | "x-systemd.automount\0"); | |
e48fdd84 | 460 | if (initrd) |
700e07ff | 461 | post = SPECIAL_INITRD_FS_TARGET; |
e48fdd84 | 462 | else if (mount_is_network(me)) |
0c17fbce | 463 | post = SPECIAL_REMOTE_FS_TARGET; |
e48fdd84 | 464 | else |
0c17fbce | 465 | post = SPECIAL_LOCAL_FS_TARGET; |
5e398e54 | 466 | |
6db615c1 LP |
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); | |
5e398e54 | 477 | } |
6b1dc2bd | 478 | |
6b1dc2bd LP |
479 | if (k < 0) |
480 | r = k; | |
481 | } | |
482 | ||
6b1dc2bd LP |
483 | return r; |
484 | } | |
485 | ||
2e852276 | 486 | static int add_sysroot_mount(void) { |
75a59316 ZJS |
487 | _cleanup_free_ char *what = NULL; |
488 | const char *opts; | |
7163e1ca | 489 | int r; |
5e398e54 | 490 | |
093c2cfe TH |
491 | if (isempty(arg_root_what)) { |
492 | log_debug("Could not find a root= entry on the kernel command line."); | |
493 | return 0; | |
135b5212 | 494 | } |
5e398e54 | 495 | |
138f4c69 LP |
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 | ||
e34e72fb | 502 | if (path_equal(arg_root_what, "/dev/nfs")) { |
3ade16c9 HH |
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 | ||
093c2cfe TH |
508 | what = fstab_node_to_udev_node(arg_root_what); |
509 | if (!what) | |
171181bc | 510 | return log_oom(); |
093c2cfe | 511 | |
6db615c1 | 512 | if (!arg_root_options) |
75a59316 ZJS |
513 | opts = arg_root_rw > 0 ? "rw" : "ro"; |
514 | else if (arg_root_rw >= 0 || | |
d15d0333 | 515 | !fstab_test_option(arg_root_options, "ro\0" "rw\0")) |
63c372cb | 516 | opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro"); |
75a59316 ZJS |
517 | else |
518 | opts = arg_root_options; | |
5e398e54 | 519 | |
6db615c1 | 520 | log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype)); |
7163e1ca DD |
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 | ||
6db615c1 LP |
528 | return add_mount(what, |
529 | "/sysroot", | |
530 | arg_root_fstype, | |
75a59316 | 531 | opts, |
f113f8e3 LP |
532 | is_device_path(what) ? 1 : 0, /* passno */ |
533 | false, /* noauto off */ | |
534 | false, /* nofail off */ | |
535 | false, /* automount off */ | |
6db615c1 LP |
536 | SPECIAL_INITRD_ROOT_FS_TARGET, |
537 | "/proc/cmdline"); | |
5e398e54 TG |
538 | } |
539 | ||
2e852276 | 540 | static int add_sysroot_usr_mount(void) { |
9f103625 TH |
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) { | |
40472036 | 548 | /* Copy over the root device, in case the /usr mount just differs in a mount option (consider btrfs subvolumes) */ |
9f103625 | 549 | arg_usr_what = strdup(arg_root_what); |
9f103625 TH |
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); | |
9f103625 TH |
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); | |
9f103625 TH |
562 | if (!arg_usr_options) |
563 | return log_oom(); | |
564 | } | |
565 | ||
eb580002 | 566 | if (!arg_usr_what) |
9f103625 TH |
567 | return 0; |
568 | ||
569 | what = fstab_node_to_udev_node(arg_usr_what); | |
47be5f07 LP |
570 | if (!what) |
571 | return log_oom(); | |
9f103625 | 572 | |
eb580002 MM |
573 | if (!arg_usr_options) |
574 | opts = arg_root_rw > 0 ? "rw" : "ro"; | |
d15d0333 | 575 | else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0")) |
63c372cb | 576 | opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro"); |
eb580002 MM |
577 | else |
578 | opts = arg_usr_options; | |
9f103625 TH |
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, | |
f113f8e3 LP |
585 | is_device_path(what) ? 1 : 0, /* passno */ |
586 | false, /* noauto off */ | |
587 | false, /* nofail off */ | |
588 | false, /* automount off */ | |
104bc12f | 589 | SPECIAL_INITRD_FS_TARGET, |
9f103625 TH |
590 | "/proc/cmdline"); |
591 | } | |
592 | ||
96287a49 | 593 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
74df0fca | 594 | int r; |
94734142 | 595 | |
9f103625 TH |
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 */ | |
6db615c1 | 599 | |
059cb385 | 600 | if (STR_IN_SET(key, "fstab", "rd.fstab") && value) { |
e48fdd84 | 601 | |
059cb385 | 602 | r = parse_boolean(value); |
141a79f4 | 603 | if (r < 0) |
059cb385 | 604 | log_warning("Failed to parse fstab switch %s. Ignoring.", value); |
141a79f4 | 605 | else |
e48fdd84 | 606 | arg_fstab_enabled = r; |
94734142 | 607 | |
6db615c1 LP |
608 | } else if (streq(key, "root") && value) { |
609 | ||
f88dc3ed | 610 | if (free_and_strdup(&arg_root_what, value) < 0) |
6db615c1 LP |
611 | return log_oom(); |
612 | ||
613 | } else if (streq(key, "rootfstype") && value) { | |
614 | ||
f88dc3ed | 615 | if (free_and_strdup(&arg_root_fstype, value) < 0) |
6db615c1 LP |
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 | ||
9f103625 TH |
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 | ||
6db615c1 LP |
652 | } else if (streq(key, "rw") && !value) |
653 | arg_root_rw = true; | |
654 | else if (streq(key, "ro") && !value) | |
655 | arg_root_rw = false; | |
94734142 | 656 | |
d0aa9ce5 | 657 | return 0; |
94734142 LP |
658 | } |
659 | ||
6b1dc2bd | 660 | int main(int argc, char *argv[]) { |
e48fdd84 | 661 | int r = 0; |
6b1dc2bd | 662 | |
07719a21 LP |
663 | if (argc > 1 && argc != 4) { |
664 | log_error("This program takes three or no arguments."); | |
6b1dc2bd LP |
665 | return EXIT_FAILURE; |
666 | } | |
667 | ||
668 | if (argc > 1) | |
669 | arg_dest = argv[1]; | |
670 | ||
a6903061 | 671 | log_set_target(LOG_TARGET_SAFE); |
6b1dc2bd LP |
672 | log_parse_environment(); |
673 | log_open(); | |
674 | ||
6b1dc2bd LP |
675 | umask(0022); |
676 | ||
d7f69e16 | 677 | r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, false); |
b5884878 | 678 | if (r < 0) |
da927ba9 | 679 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
94734142 | 680 | |
9f103625 TH |
681 | /* Always honour root= and usr= in the kernel command line if we are in an initrd */ |
682 | if (in_initrd()) { | |
003cba39 LP |
683 | int k; |
684 | ||
2e852276 | 685 | r = add_sysroot_mount(); |
003cba39 LP |
686 | |
687 | k = add_sysroot_usr_mount(); | |
688 | if (k < 0) | |
689 | r = k; | |
690 | } else | |
691 | r = 0; | |
5e398e54 | 692 | |
e48fdd84 LP |
693 | /* Honour /etc/fstab only when that's enabled */ |
694 | if (arg_fstab_enabled) { | |
695 | int k; | |
ac4785b0 | 696 | |
75a59316 ZJS |
697 | log_debug("Parsing /etc/fstab"); |
698 | ||
e48fdd84 LP |
699 | /* Parse the local /etc/fstab, possibly from the initrd */ |
700 | k = parse_fstab(false); | |
701 | if (k < 0) | |
702 | r = k; | |
ac4785b0 | 703 | |
e48fdd84 LP |
704 | /* If running in the initrd also parse the /etc/fstab from the host */ |
705 | if (in_initrd()) { | |
75a59316 ZJS |
706 | log_debug("Parsing /sysroot/etc/fstab"); |
707 | ||
e48fdd84 LP |
708 | k = parse_fstab(true); |
709 | if (k < 0) | |
710 | r = k; | |
711 | } | |
712 | } | |
6b1dc2bd | 713 | |
126cc760 | 714 | free(arg_root_what); |
59cfa62f LP |
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); | |
126cc760 | 721 | |
e48fdd84 | 722 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
6b1dc2bd | 723 | } |