1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
12 #include "fstab-util.h"
13 #include "generator.h"
17 #include "path-util.h"
19 #include "specifier.h"
20 #include "string-util.h"
21 #include "time-util.h"
22 #include "unit-name.h"
25 int generator_open_unit_file(
34 unit
= strjoina(dest
, "/", name
);
36 f
= fopen(unit
, "wxe");
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?",
43 return log_error_errno(errno
,
44 "Failed to create unit file %s: %m",
48 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
51 "# Automatically generated by %s\n\n",
52 program_invocation_short_name
);
58 int generator_add_symlink(const char *root
, const char *dst
, const char *dep_type
, const char *src
) {
59 /* Adds a symlink from <dst>.<dep_type>.d/ to ../<src> */
61 const char *from
, *to
;
63 from
= strjoina("../", src
);
64 to
= strjoina(root
, "/", dst
, ".", dep_type
, "/", src
);
66 mkdir_parents_label(to
, 0755);
67 if (symlink(from
, to
) < 0)
69 return log_error_errno(errno
, "Failed to create symlink \"%s\": %m", to
);
74 static int write_fsck_sysroot_service(const char *dir
, const char *what
) {
75 _cleanup_free_
char *device
= NULL
, *escaped
= NULL
, *escaped2
= NULL
;
76 _cleanup_fclose_
FILE *f
= NULL
;
80 escaped
= specifier_escape(what
);
84 escaped2
= cescape(escaped
);
88 unit
= strjoina(dir
, "/systemd-fsck-root.service");
89 log_debug("Creating %s", unit
);
91 r
= unit_name_from_path(what
, ".device", &device
);
93 return log_error_errno(r
, "Failed to convert device \"%s\" to unit name: %m", what
);
95 f
= fopen(unit
, "wxe");
97 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
100 "# Automatically generated by %1$s\n\n"
102 "Description=File System Check on %2$s\n"
103 "Documentation=man:systemd-fsck-root.service(8)\n"
104 "DefaultDependencies=no\n"
106 "After=initrd-root-device.target local-fs-pre.target %3$s\n"
107 "Before=shutdown.target\n"
111 "RemainAfterExit=yes\n"
112 "ExecStart=" SYSTEMD_FSCK_PATH
" %4$s\n"
114 program_invocation_short_name
,
119 r
= fflush_and_check(f
);
121 return log_error_errno(r
, "Failed to write unit file %s: %m", unit
);
126 int generator_write_fsck_deps(
131 const char *fstype
) {
140 if (!is_device_path(what
)) {
141 log_warning("Checking was requested for \"%s\", but it is not a device.", what
);
145 if (!isempty(fstype
) && !streq(fstype
, "auto")) {
146 r
= fsck_exists(fstype
);
148 log_warning_errno(r
, "Checking was requested for %s, but couldn't detect if fsck.%s may be used, proceeding: %m", what
, fstype
);
150 /* treat missing check as essentially OK */
151 log_debug("Checking was requested for %s, but fsck.%s does not exist.", what
, fstype
);
156 if (path_equal(where
, "/")) {
159 lnk
= strjoina(dir
, "/" SPECIAL_LOCAL_FS_TARGET
".wants/systemd-fsck-root.service");
161 mkdir_parents(lnk
, 0755);
162 if (symlink(SYSTEM_DATA_UNIT_PATH
"/systemd-fsck-root.service", lnk
) < 0)
163 return log_error_errno(errno
, "Failed to create symlink %s: %m", lnk
);
166 _cleanup_free_
char *_fsck
= NULL
;
169 if (in_initrd() && path_equal(where
, "/sysroot")) {
170 r
= write_fsck_sysroot_service(dir
, what
);
174 fsck
= "systemd-fsck-root.service";
176 r
= unit_name_from_path_instance("systemd-fsck", what
, ".service", &_fsck
);
178 return log_error_errno(r
, "Failed to create fsck service name: %m");
192 int generator_write_timeouts(
199 /* Allow configuration how long we wait for a device that
200 * backs a mount point to show up. This is useful to support
201 * endless device timeouts for devices that show up only after
202 * user input, like crypto devices. */
204 _cleanup_free_
char *node
= NULL
, *unit
= NULL
, *timeout
= NULL
;
208 r
= fstab_filter_options(opts
, "comment=systemd.device-timeout\0"
209 "x-systemd.device-timeout\0",
210 NULL
, &timeout
, filtered
);
214 r
= parse_sec_fix_0(timeout
, &u
);
216 log_warning("Failed to parse timeout for %s, ignoring: %s", where
, timeout
);
220 node
= fstab_node_to_udev_node(what
);
223 if (!is_device_path(node
)) {
224 log_warning("x-systemd.device-timeout ignored for %s", what
);
228 r
= unit_name_from_path(node
, ".device", &unit
);
230 return log_error_errno(r
, "Failed to make unit name from path: %m");
232 return write_drop_in_format(dir
, unit
, 50, "device-timeout",
233 "# Automatically generated by %s\n\n"
235 "JobRunningTimeoutSec=%s",
236 program_invocation_short_name
,
240 int generator_write_device_deps(
246 /* fstab records that specify _netdev option should apply the network
247 * ordering on the actual device depending on network connection. If we
248 * are not mounting real device (NFS, CIFS), we rely on _netdev effect
249 * on the mount unit itself. */
251 _cleanup_free_
char *node
= NULL
, *unit
= NULL
;
254 if (!fstab_test_option(opts
, "_netdev\0"))
257 node
= fstab_node_to_udev_node(what
);
261 /* Nothing to apply dependencies to. */
262 if (!is_device_path(node
))
265 r
= unit_name_from_path(node
, ".device", &unit
);
267 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
270 /* See mount_add_default_dependencies for explanation why we create such
272 return write_drop_in_format(dir
, unit
, 50, "netdev-dependencies",
273 "# Automatically generated by %s\n\n"
275 "After=" SPECIAL_NETWORK_ONLINE_TARGET
" " SPECIAL_NETWORK_TARGET
"\n"
276 "Wants=" SPECIAL_NETWORK_ONLINE_TARGET
"\n",
277 program_invocation_short_name
);
280 int generator_write_initrd_root_device_deps(const char *dir
, const char *what
) {
281 _cleanup_free_
char *unit
= NULL
;
284 r
= unit_name_from_path(what
, ".device", &unit
);
286 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
289 return write_drop_in_format(dir
, SPECIAL_INITRD_ROOT_DEVICE_TARGET
, 50, "root-device",
290 "# Automatically generated by %s\n\n"
294 program_invocation_short_name
,
299 int generator_hook_up_mkswap(
303 _cleanup_free_
char *node
= NULL
, *unit
= NULL
, *escaped
= NULL
, *where_unit
= NULL
;
304 _cleanup_fclose_
FILE *f
= NULL
;
305 const char *unit_file
;
308 node
= fstab_node_to_udev_node(what
);
312 /* Nothing to work on. */
313 if (!is_device_path(node
)) {
314 log_error("Cannot format something that is not a device node: %s", node
);
318 r
= unit_name_from_path_instance("systemd-mkswap", node
, ".service", &unit
);
320 return log_error_errno(r
, "Failed to make unit instance name from path \"%s\": %m",
323 unit_file
= strjoina(dir
, "/", unit
);
324 log_debug("Creating %s", unit_file
);
326 escaped
= cescape(node
);
330 r
= unit_name_from_path(what
, ".swap", &where_unit
);
332 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
335 f
= fopen(unit_file
, "wxe");
337 return log_error_errno(errno
, "Failed to create unit file %s: %m",
341 "# Automatically generated by %s\n\n"
343 "Description=Make Swap on %%f\n"
344 "Documentation=man:systemd-mkswap@.service(8)\n"
345 "DefaultDependencies=no\n"
346 "BindsTo=%%i.device\n"
349 "Before=shutdown.target\n"
353 "RemainAfterExit=yes\n"
354 "ExecStart="SYSTEMD_MAKEFS_PATH
" swap %s\n"
356 program_invocation_short_name
,
360 r
= fflush_and_check(f
);
362 return log_error_errno(r
, "Failed to write unit file %s: %m", unit_file
);
364 return generator_add_symlink(dir
, where_unit
, "requires", unit
);
367 int generator_hook_up_mkfs(
373 _cleanup_free_
char *node
= NULL
, *unit
= NULL
, *escaped
= NULL
, *where_unit
= NULL
;
374 _cleanup_fclose_
FILE *f
= NULL
;
375 const char *unit_file
;
378 node
= fstab_node_to_udev_node(what
);
382 /* Nothing to work on. */
383 if (!is_device_path(node
)) {
384 log_error("Cannot format something that is not a device node: %s", node
);
388 if (!type
|| streq(type
, "auto")) {
389 log_error("Cannot format partition %s, filesystem type is not specified", node
);
393 r
= unit_name_from_path_instance("systemd-mkfs", node
, ".service", &unit
);
395 return log_error_errno(r
, "Failed to make unit instance name from path \"%s\": %m",
398 unit_file
= strjoina(dir
, "/", unit
);
399 log_debug("Creating %s", unit_file
);
401 escaped
= cescape(node
);
405 r
= unit_name_from_path(where
, ".mount", &where_unit
);
407 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
410 f
= fopen(unit_file
, "wxe");
412 return log_error_errno(errno
, "Failed to create unit file %s: %m",
416 "# Automatically generated by %s\n\n"
418 "Description=Make File System on %%f\n"
419 "Documentation=man:systemd-mkfs@.service(8)\n"
420 "DefaultDependencies=no\n"
421 "BindsTo=%%i.device\n"
423 /* fsck might or might not be used, so let's be safe and order
424 * ourselves before both systemd-fsck@.service and the mount unit. */
425 "Before=systemd-fsck@%%i.service\n"
427 "Before=shutdown.target\n"
431 "RemainAfterExit=yes\n"
432 "ExecStart="SYSTEMD_MAKEFS_PATH
" %s %s\n"
434 program_invocation_short_name
,
438 // XXX: what about local-fs-pre.target?
440 r
= fflush_and_check(f
);
442 return log_error_errno(r
, "Failed to write unit file %s: %m", unit_file
);
444 return generator_add_symlink(dir
, where_unit
, "requires", unit
);
447 int generator_hook_up_growfs(
450 const char *target
) {
452 _cleanup_free_
char *unit
= NULL
, *escaped
= NULL
, *where_unit
= NULL
;
453 _cleanup_fclose_
FILE *f
= NULL
;
454 const char *unit_file
;
457 escaped
= cescape(where
);
461 r
= unit_name_from_path_instance("systemd-growfs", where
, ".service", &unit
);
463 return log_error_errno(r
, "Failed to make unit instance name from path \"%s\": %m",
466 r
= unit_name_from_path(where
, ".mount", &where_unit
);
468 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
471 unit_file
= strjoina(dir
, "/", unit
);
472 log_debug("Creating %s", unit_file
);
474 f
= fopen(unit_file
, "wxe");
476 return log_error_errno(errno
, "Failed to create unit file %s: %m",
480 "# Automatically generated by %s\n\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"
487 "Before=shutdown.target\n"
492 "RemainAfterExit=yes\n"
493 "ExecStart="SYSTEMD_GROWFS_PATH
" %s\n"
495 program_invocation_short_name
,
499 return generator_add_symlink(dir
, where_unit
, "wants", unit
);
502 void log_setup_generator(void) {
503 log_set_prohibit_ipc(true);