]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
60f067b4 | 2 | |
4c89c718 | 3 | #include <errno.h> |
1d42b86d | 4 | #include <stdio_ext.h> |
60f067b4 JS |
5 | #include <unistd.h> |
6 | ||
db2df898 MP |
7 | #include "alloc-util.h" |
8 | #include "dropin.h" | |
9 | #include "escape.h" | |
10 | #include "fd-util.h" | |
11 | #include "fileio.h" | |
12 | #include "fstab-util.h" | |
60f067b4 | 13 | #include "generator.h" |
4c89c718 MP |
14 | #include "log.h" |
15 | #include "macro.h" | |
db2df898 | 16 | #include "mkdir.h" |
60f067b4 | 17 | #include "path-util.h" |
db2df898 | 18 | #include "special.h" |
52ad194e | 19 | #include "specifier.h" |
db2df898 | 20 | #include "string-util.h" |
4c89c718 | 21 | #include "time-util.h" |
db2df898 MP |
22 | #include "unit-name.h" |
23 | #include "util.h" | |
60f067b4 | 24 | |
1d42b86d MB |
25 | int generator_open_unit_file( |
26 | const char *dest, | |
27 | const char *source, | |
28 | const char *name, | |
29 | FILE **file) { | |
30 | ||
31 | const char *unit; | |
32 | FILE *f; | |
33 | ||
34 | unit = strjoina(dest, "/", name); | |
35 | ||
36 | f = fopen(unit, "wxe"); | |
37 | if (!f) { | |
38 | if (source && errno == EEXIST) | |
39 | return log_error_errno(errno, | |
40 | "Failed to create unit file %s, as it already exists. Duplicate entry in %s?", | |
41 | unit, source); | |
42 | else | |
43 | return log_error_errno(errno, | |
44 | "Failed to create unit file %s: %m", | |
45 | unit); | |
46 | } | |
47 | ||
48 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); | |
49 | ||
50 | fprintf(f, | |
51 | "# Automatically generated by %s\n\n", | |
52 | program_invocation_short_name); | |
53 | ||
54 | *file = f; | |
55 | return 0; | |
56 | } | |
57 | ||
bb4f798a MB |
58 | int generator_add_symlink(const char *dir, const char *dst, const char *dep_type, const char *src) { |
59 | /* Adds a symlink from <dst>.<dep_type>/ to <src> (if src is absolute) | |
60 | * or ../<src> (otherwise). */ | |
f5e65279 MB |
61 | |
62 | const char *from, *to; | |
63 | ||
bb4f798a MB |
64 | from = path_is_absolute(src) ? src : strjoina("../", src); |
65 | to = strjoina(dir, "/", dst, ".", dep_type, "/", basename(src)); | |
f5e65279 MB |
66 | |
67 | mkdir_parents_label(to, 0755); | |
68 | if (symlink(from, to) < 0) | |
69 | if (errno != EEXIST) | |
70 | return log_error_errno(errno, "Failed to create symlink \"%s\": %m", to); | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
e3bff60a | 75 | static int write_fsck_sysroot_service(const char *dir, const char *what) { |
52ad194e | 76 | _cleanup_free_ char *device = NULL, *escaped = NULL, *escaped2 = NULL; |
e3bff60a | 77 | _cleanup_fclose_ FILE *f = NULL; |
db2df898 | 78 | const char *unit; |
e3bff60a MP |
79 | int r; |
80 | ||
52ad194e | 81 | escaped = specifier_escape(what); |
86f210e9 MP |
82 | if (!escaped) |
83 | return log_oom(); | |
84 | ||
52ad194e MB |
85 | escaped2 = cescape(escaped); |
86 | if (!escaped2) | |
87 | return log_oom(); | |
88 | ||
bb4f798a | 89 | unit = strjoina(dir, "/"SPECIAL_FSCK_ROOT_SERVICE); |
e3bff60a MP |
90 | log_debug("Creating %s", unit); |
91 | ||
92 | r = unit_name_from_path(what, ".device", &device); | |
93 | if (r < 0) | |
94 | return log_error_errno(r, "Failed to convert device \"%s\" to unit name: %m", what); | |
95 | ||
96 | f = fopen(unit, "wxe"); | |
97 | if (!f) | |
98 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
99 | ||
100 | fprintf(f, | |
101 | "# Automatically generated by %1$s\n\n" | |
102 | "[Unit]\n" | |
e3bff60a | 103 | "Description=File System Check on %2$s\n" |
52ad194e | 104 | "Documentation=man:systemd-fsck-root.service(8)\n" |
e3bff60a MP |
105 | "DefaultDependencies=no\n" |
106 | "BindsTo=%3$s\n" | |
6e866b33 | 107 | "Conflicts=shutdown.target\n" |
2897b343 | 108 | "After=initrd-root-device.target local-fs-pre.target %3$s\n" |
e3bff60a MP |
109 | "Before=shutdown.target\n" |
110 | "\n" | |
111 | "[Service]\n" | |
112 | "Type=oneshot\n" | |
113 | "RemainAfterExit=yes\n" | |
86f210e9 | 114 | "ExecStart=" SYSTEMD_FSCK_PATH " %4$s\n" |
e3bff60a MP |
115 | "TimeoutSec=0\n", |
116 | program_invocation_short_name, | |
52ad194e | 117 | escaped, |
86f210e9 | 118 | device, |
52ad194e | 119 | escaped2); |
e3bff60a MP |
120 | |
121 | r = fflush_and_check(f); | |
122 | if (r < 0) | |
123 | return log_error_errno(r, "Failed to write unit file %s: %m", unit); | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
60f067b4 JS |
128 | int generator_write_fsck_deps( |
129 | FILE *f, | |
e3bff60a | 130 | const char *dir, |
60f067b4 JS |
131 | const char *what, |
132 | const char *where, | |
133 | const char *fstype) { | |
134 | ||
e3bff60a MP |
135 | int r; |
136 | ||
60f067b4 | 137 | assert(f); |
e3bff60a | 138 | assert(dir); |
60f067b4 JS |
139 | assert(what); |
140 | assert(where); | |
141 | ||
142 | if (!is_device_path(what)) { | |
143 | log_warning("Checking was requested for \"%s\", but it is not a device.", what); | |
144 | return 0; | |
145 | } | |
146 | ||
147 | if (!isempty(fstype) && !streq(fstype, "auto")) { | |
60f067b4 | 148 | r = fsck_exists(fstype); |
db2df898 MP |
149 | if (r < 0) |
150 | log_warning_errno(r, "Checking was requested for %s, but couldn't detect if fsck.%s may be used, proceeding: %m", what, fstype); | |
151 | else if (r == 0) { | |
60f067b4 | 152 | /* treat missing check as essentially OK */ |
db2df898 | 153 | log_debug("Checking was requested for %s, but fsck.%s does not exist.", what, fstype); |
e842803a | 154 | return 0; |
db2df898 | 155 | } |
60f067b4 JS |
156 | } |
157 | ||
e3bff60a | 158 | if (path_equal(where, "/")) { |
db2df898 | 159 | const char *lnk; |
60f067b4 | 160 | |
bb4f798a | 161 | lnk = strjoina(dir, "/" SPECIAL_LOCAL_FS_TARGET ".wants/"SPECIAL_FSCK_ROOT_SERVICE); |
60f067b4 JS |
162 | |
163 | mkdir_parents(lnk, 0755); | |
bb4f798a | 164 | if (symlink(SYSTEM_DATA_UNIT_PATH "/"SPECIAL_FSCK_ROOT_SERVICE, lnk) < 0) |
f47781d8 | 165 | return log_error_errno(errno, "Failed to create symlink %s: %m", lnk); |
60f067b4 JS |
166 | |
167 | } else { | |
e3bff60a MP |
168 | _cleanup_free_ char *_fsck = NULL; |
169 | const char *fsck; | |
170 | ||
171 | if (in_initrd() && path_equal(where, "/sysroot")) { | |
172 | r = write_fsck_sysroot_service(dir, what); | |
173 | if (r < 0) | |
174 | return r; | |
175 | ||
bb4f798a | 176 | fsck = SPECIAL_FSCK_ROOT_SERVICE; |
e3bff60a MP |
177 | } else { |
178 | r = unit_name_from_path_instance("systemd-fsck", what, ".service", &_fsck); | |
179 | if (r < 0) | |
180 | return log_error_errno(r, "Failed to create fsck service name: %m"); | |
60f067b4 | 181 | |
e3bff60a MP |
182 | fsck = _fsck; |
183 | } | |
60f067b4 JS |
184 | |
185 | fprintf(f, | |
db2df898 | 186 | "Requires=%1$s\n" |
e3bff60a | 187 | "After=%1$s\n", |
60f067b4 JS |
188 | fsck); |
189 | } | |
190 | ||
191 | return 0; | |
192 | } | |
e842803a | 193 | |
e3bff60a MP |
194 | int generator_write_timeouts( |
195 | const char *dir, | |
196 | const char *what, | |
197 | const char *where, | |
198 | const char *opts, | |
199 | char **filtered) { | |
e842803a | 200 | |
6e866b33 MB |
201 | /* Configure how long we wait for a device that backs a mount point or a |
202 | * swap partition to show up. This is useful to support endless device timeouts | |
203 | * for devices that show up only after user input, like crypto devices. */ | |
e842803a | 204 | |
e735f4d4 | 205 | _cleanup_free_ char *node = NULL, *unit = NULL, *timeout = NULL; |
e842803a MB |
206 | usec_t u; |
207 | int r; | |
e842803a | 208 | |
81c58355 MB |
209 | r = fstab_filter_options(opts, "comment=systemd.device-timeout\0" |
210 | "x-systemd.device-timeout\0", | |
e735f4d4 MP |
211 | NULL, &timeout, filtered); |
212 | if (r <= 0) | |
213 | return r; | |
e842803a | 214 | |
81c58355 | 215 | r = parse_sec_fix_0(timeout, &u); |
e842803a | 216 | if (r < 0) { |
e3bff60a | 217 | log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout); |
e842803a MB |
218 | return 0; |
219 | } | |
220 | ||
221 | node = fstab_node_to_udev_node(what); | |
222 | if (!node) | |
223 | return log_oom(); | |
f5e65279 MB |
224 | if (!is_device_path(node)) { |
225 | log_warning("x-systemd.device-timeout ignored for %s", what); | |
226 | return 0; | |
227 | } | |
e842803a | 228 | |
e3bff60a MP |
229 | r = unit_name_from_path(node, ".device", &unit); |
230 | if (r < 0) | |
231 | return log_error_errno(r, "Failed to make unit name from path: %m"); | |
e842803a MB |
232 | |
233 | return write_drop_in_format(dir, unit, 50, "device-timeout", | |
234 | "# Automatically generated by %s\n\n" | |
f5e65279 MB |
235 | "[Unit]\n" |
236 | "JobRunningTimeoutSec=%s", | |
237 | program_invocation_short_name, | |
238 | timeout); | |
e842803a | 239 | } |
aa27b158 | 240 | |
81c58355 MB |
241 | int generator_write_device_deps( |
242 | const char *dir, | |
243 | const char *what, | |
244 | const char *where, | |
245 | const char *opts) { | |
246 | ||
247 | /* fstab records that specify _netdev option should apply the network | |
248 | * ordering on the actual device depending on network connection. If we | |
249 | * are not mounting real device (NFS, CIFS), we rely on _netdev effect | |
250 | * on the mount unit itself. */ | |
251 | ||
252 | _cleanup_free_ char *node = NULL, *unit = NULL; | |
253 | int r; | |
254 | ||
255 | if (!fstab_test_option(opts, "_netdev\0")) | |
256 | return 0; | |
257 | ||
258 | node = fstab_node_to_udev_node(what); | |
259 | if (!node) | |
260 | return log_oom(); | |
261 | ||
262 | /* Nothing to apply dependencies to. */ | |
263 | if (!is_device_path(node)) | |
264 | return 0; | |
265 | ||
266 | r = unit_name_from_path(node, ".device", &unit); | |
267 | if (r < 0) | |
52ad194e MB |
268 | return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", |
269 | node); | |
81c58355 MB |
270 | |
271 | /* See mount_add_default_dependencies for explanation why we create such | |
272 | * dependencies. */ | |
273 | return write_drop_in_format(dir, unit, 50, "netdev-dependencies", | |
274 | "# Automatically generated by %s\n\n" | |
275 | "[Unit]\n" | |
276 | "After=" SPECIAL_NETWORK_ONLINE_TARGET " " SPECIAL_NETWORK_TARGET "\n" | |
277 | "Wants=" SPECIAL_NETWORK_ONLINE_TARGET "\n", | |
278 | program_invocation_short_name); | |
279 | } | |
280 | ||
aa27b158 MP |
281 | int generator_write_initrd_root_device_deps(const char *dir, const char *what) { |
282 | _cleanup_free_ char *unit = NULL; | |
283 | int r; | |
284 | ||
285 | r = unit_name_from_path(what, ".device", &unit); | |
286 | if (r < 0) | |
52ad194e MB |
287 | return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", |
288 | what); | |
aa27b158 MP |
289 | |
290 | return write_drop_in_format(dir, SPECIAL_INITRD_ROOT_DEVICE_TARGET, 50, "root-device", | |
291 | "# Automatically generated by %s\n\n" | |
f5e65279 MB |
292 | "[Unit]\n" |
293 | "Requires=%s\n" | |
294 | "After=%s", | |
295 | program_invocation_short_name, | |
296 | unit, | |
297 | unit); | |
aa27b158 | 298 | } |
52ad194e MB |
299 | |
300 | int generator_hook_up_mkswap( | |
301 | const char *dir, | |
302 | const char *what) { | |
303 | ||
304 | _cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL; | |
305 | _cleanup_fclose_ FILE *f = NULL; | |
306 | const char *unit_file; | |
307 | int r; | |
308 | ||
309 | node = fstab_node_to_udev_node(what); | |
310 | if (!node) | |
311 | return log_oom(); | |
312 | ||
313 | /* Nothing to work on. */ | |
6e866b33 MB |
314 | if (!is_device_path(node)) |
315 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
316 | "Cannot format something that is not a device node: %s", | |
317 | node); | |
52ad194e MB |
318 | |
319 | r = unit_name_from_path_instance("systemd-mkswap", node, ".service", &unit); | |
320 | if (r < 0) | |
321 | return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m", | |
322 | node); | |
323 | ||
324 | unit_file = strjoina(dir, "/", unit); | |
325 | log_debug("Creating %s", unit_file); | |
326 | ||
327 | escaped = cescape(node); | |
328 | if (!escaped) | |
329 | return log_oom(); | |
330 | ||
331 | r = unit_name_from_path(what, ".swap", &where_unit); | |
332 | if (r < 0) | |
333 | return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", | |
334 | what); | |
335 | ||
336 | f = fopen(unit_file, "wxe"); | |
337 | if (!f) | |
338 | return log_error_errno(errno, "Failed to create unit file %s: %m", | |
339 | unit_file); | |
340 | ||
341 | fprintf(f, | |
342 | "# Automatically generated by %s\n\n" | |
343 | "[Unit]\n" | |
344 | "Description=Make Swap on %%f\n" | |
345 | "Documentation=man:systemd-mkswap@.service(8)\n" | |
346 | "DefaultDependencies=no\n" | |
347 | "BindsTo=%%i.device\n" | |
6e866b33 | 348 | "Conflicts=shutdown.target\n" |
52ad194e | 349 | "After=%%i.device\n" |
6e866b33 | 350 | "Before=shutdown.target %s\n" |
52ad194e MB |
351 | "\n" |
352 | "[Service]\n" | |
353 | "Type=oneshot\n" | |
354 | "RemainAfterExit=yes\n" | |
355 | "ExecStart="SYSTEMD_MAKEFS_PATH " swap %s\n" | |
356 | "TimeoutSec=0\n", | |
357 | program_invocation_short_name, | |
358 | where_unit, | |
359 | escaped); | |
360 | ||
361 | r = fflush_and_check(f); | |
362 | if (r < 0) | |
363 | return log_error_errno(r, "Failed to write unit file %s: %m", unit_file); | |
364 | ||
365 | return generator_add_symlink(dir, where_unit, "requires", unit); | |
366 | } | |
367 | ||
368 | int generator_hook_up_mkfs( | |
369 | const char *dir, | |
370 | const char *what, | |
371 | const char *where, | |
372 | const char *type) { | |
373 | ||
374 | _cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL; | |
375 | _cleanup_fclose_ FILE *f = NULL; | |
376 | const char *unit_file; | |
377 | int r; | |
378 | ||
379 | node = fstab_node_to_udev_node(what); | |
380 | if (!node) | |
381 | return log_oom(); | |
382 | ||
383 | /* Nothing to work on. */ | |
6e866b33 MB |
384 | if (!is_device_path(node)) |
385 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
386 | "Cannot format something that is not a device node: %s", | |
387 | node); | |
52ad194e | 388 | |
6e866b33 MB |
389 | if (!type || streq(type, "auto")) |
390 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
391 | "Cannot format partition %s, filesystem type is not specified", | |
392 | node); | |
52ad194e | 393 | |
bb4f798a | 394 | r = unit_name_from_path_instance("systemd-makefs", node, ".service", &unit); |
52ad194e MB |
395 | if (r < 0) |
396 | return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m", | |
397 | node); | |
398 | ||
399 | unit_file = strjoina(dir, "/", unit); | |
400 | log_debug("Creating %s", unit_file); | |
401 | ||
402 | escaped = cescape(node); | |
403 | if (!escaped) | |
404 | return log_oom(); | |
405 | ||
406 | r = unit_name_from_path(where, ".mount", &where_unit); | |
407 | if (r < 0) | |
408 | return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", | |
409 | where); | |
410 | ||
411 | f = fopen(unit_file, "wxe"); | |
412 | if (!f) | |
413 | return log_error_errno(errno, "Failed to create unit file %s: %m", | |
414 | unit_file); | |
415 | ||
416 | fprintf(f, | |
417 | "# Automatically generated by %s\n\n" | |
418 | "[Unit]\n" | |
419 | "Description=Make File System on %%f\n" | |
bb4f798a | 420 | "Documentation=man:systemd-makefs@.service(8)\n" |
52ad194e MB |
421 | "DefaultDependencies=no\n" |
422 | "BindsTo=%%i.device\n" | |
6e866b33 | 423 | "Conflicts=shutdown.target\n" |
52ad194e MB |
424 | "After=%%i.device\n" |
425 | /* fsck might or might not be used, so let's be safe and order | |
426 | * ourselves before both systemd-fsck@.service and the mount unit. */ | |
6e866b33 | 427 | "Before=shutdown.target systemd-fsck@%%i.service %s\n" |
52ad194e MB |
428 | "\n" |
429 | "[Service]\n" | |
430 | "Type=oneshot\n" | |
431 | "RemainAfterExit=yes\n" | |
432 | "ExecStart="SYSTEMD_MAKEFS_PATH " %s %s\n" | |
433 | "TimeoutSec=0\n", | |
434 | program_invocation_short_name, | |
435 | where_unit, | |
436 | type, | |
437 | escaped); | |
438 | // XXX: what about local-fs-pre.target? | |
439 | ||
440 | r = fflush_and_check(f); | |
441 | if (r < 0) | |
442 | return log_error_errno(r, "Failed to write unit file %s: %m", unit_file); | |
443 | ||
444 | return generator_add_symlink(dir, where_unit, "requires", unit); | |
445 | } | |
446 | ||
447 | int generator_hook_up_growfs( | |
448 | const char *dir, | |
449 | const char *where, | |
450 | const char *target) { | |
451 | ||
452 | _cleanup_free_ char *unit = NULL, *escaped = NULL, *where_unit = NULL; | |
453 | _cleanup_fclose_ FILE *f = NULL; | |
454 | const char *unit_file; | |
455 | int r; | |
456 | ||
457 | escaped = cescape(where); | |
458 | if (!escaped) | |
459 | return log_oom(); | |
460 | ||
461 | r = unit_name_from_path_instance("systemd-growfs", where, ".service", &unit); | |
462 | if (r < 0) | |
463 | return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m", | |
464 | where); | |
465 | ||
466 | r = unit_name_from_path(where, ".mount", &where_unit); | |
467 | if (r < 0) | |
468 | return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", | |
469 | where); | |
470 | ||
471 | unit_file = strjoina(dir, "/", unit); | |
472 | log_debug("Creating %s", unit_file); | |
473 | ||
474 | f = fopen(unit_file, "wxe"); | |
475 | if (!f) | |
476 | return log_error_errno(errno, "Failed to create unit file %s: %m", | |
477 | unit_file); | |
478 | ||
479 | fprintf(f, | |
480 | "# Automatically generated by %s\n\n" | |
481 | "[Unit]\n" | |
482 | "Description=Grow File System on %%f\n" | |
483 | "Documentation=man:systemd-growfs@.service(8)\n" | |
484 | "DefaultDependencies=no\n" | |
485 | "BindsTo=%%i.mount\n" | |
6e866b33 | 486 | "Conflicts=shutdown.target\n" |
52ad194e | 487 | "After=%%i.mount\n" |
6e866b33 | 488 | "Before=shutdown.target %s\n" |
52ad194e MB |
489 | "\n" |
490 | "[Service]\n" | |
491 | "Type=oneshot\n" | |
492 | "RemainAfterExit=yes\n" | |
493 | "ExecStart="SYSTEMD_GROWFS_PATH " %s\n" | |
494 | "TimeoutSec=0\n", | |
495 | program_invocation_short_name, | |
496 | target, | |
497 | escaped); | |
498 | ||
499 | return generator_add_symlink(dir, where_unit, "wants", unit); | |
500 | } | |
6e866b33 | 501 | |
bb4f798a MB |
502 | int generator_enable_remount_fs_service(const char *dir) { |
503 | /* Pull in systemd-remount-fs.service */ | |
504 | return generator_add_symlink(dir, SPECIAL_LOCAL_FS_TARGET, "wants", | |
505 | SYSTEM_DATA_UNIT_PATH "/" SPECIAL_REMOUNT_FS_SERVICE); | |
506 | } | |
507 | ||
6e866b33 MB |
508 | void log_setup_generator(void) { |
509 | log_set_prohibit_ipc(true); | |
510 | log_setup_service(); | |
511 | } |