1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
11 #include "fstab-util.h"
12 #include "generator.h"
16 #include "path-util.h"
18 #include "specifier.h"
19 #include "string-util.h"
20 #include "time-util.h"
21 #include "unit-name.h"
24 int generator_open_unit_file(
34 unit
= strjoina(dest
, "/", name
);
36 r
= fopen_unlocked(unit
, "wxe", &f
);
38 if (source
&& r
== -EEXIST
)
39 return log_error_errno(r
,
40 "Failed to create unit file %s, as it already exists. Duplicate entry in %s?",
43 return log_error_errno(r
,
44 "Failed to create unit file %s: %m",
49 "# Automatically generated by %s\n\n",
50 program_invocation_short_name
);
56 int generator_add_symlink(const char *dir
, const char *dst
, const char *dep_type
, const char *src
) {
57 /* Adds a symlink from <dst>.<dep_type>/ to <src> (if src is absolute)
58 * or ../<src> (otherwise). */
60 const char *from
, *to
;
62 from
= path_is_absolute(src
) ? src
: strjoina("../", src
);
63 to
= strjoina(dir
, "/", dst
, ".", dep_type
, "/", basename(src
));
65 mkdir_parents_label(to
, 0755);
66 if (symlink(from
, to
) < 0)
68 return log_error_errno(errno
, "Failed to create symlink \"%s\": %m", to
);
73 static int write_fsck_sysroot_service(const char *dir
, const char *what
) {
74 _cleanup_free_
char *device
= NULL
, *escaped
= NULL
, *escaped2
= NULL
;
75 _cleanup_fclose_
FILE *f
= NULL
;
79 escaped
= specifier_escape(what
);
83 escaped2
= cescape(escaped
);
87 unit
= strjoina(dir
, "/"SPECIAL_FSCK_ROOT_SERVICE
);
88 log_debug("Creating %s", unit
);
90 r
= unit_name_from_path(what
, ".device", &device
);
92 return log_error_errno(r
, "Failed to convert device \"%s\" to unit name: %m", what
);
94 f
= fopen(unit
, "wxe");
96 return log_error_errno(errno
, "Failed to create unit file %s: %m", unit
);
99 "# Automatically generated by %1$s\n\n"
101 "Description=File System Check on %2$s\n"
102 "Documentation=man:systemd-fsck-root.service(8)\n"
103 "DefaultDependencies=no\n"
105 "Conflicts=shutdown.target\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/"SPECIAL_FSCK_ROOT_SERVICE
);
161 (void) mkdir_parents(lnk
, 0755);
162 if (symlink(SYSTEM_DATA_UNIT_PATH
"/"SPECIAL_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
= SPECIAL_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 /* Configure how long we wait for a device that backs a mount point or a
200 * swap partition to show up. This is useful to support endless device timeouts
201 * for devices that show up only after user input, like crypto devices. */
203 _cleanup_free_
char *node
= NULL
, *unit
= NULL
, *timeout
= NULL
;
207 r
= fstab_filter_options(opts
, "comment=systemd.device-timeout\0"
208 "x-systemd.device-timeout\0",
209 NULL
, &timeout
, filtered
);
213 r
= parse_sec_fix_0(timeout
, &u
);
215 log_warning("Failed to parse timeout for %s, ignoring: %s", where
, timeout
);
219 node
= fstab_node_to_udev_node(what
);
222 if (!is_device_path(node
)) {
223 log_warning("x-systemd.device-timeout ignored for %s", what
);
227 r
= unit_name_from_path(node
, ".device", &unit
);
229 return log_error_errno(r
, "Failed to make unit name from path: %m");
231 return write_drop_in_format(dir
, unit
, 50, "device-timeout",
232 "# Automatically generated by %s\n\n"
234 "JobRunningTimeoutSec=%s",
235 program_invocation_short_name
,
239 int generator_write_device_deps(
245 /* fstab records that specify _netdev option should apply the network
246 * ordering on the actual device depending on network connection. If we
247 * are not mounting real device (NFS, CIFS), we rely on _netdev effect
248 * on the mount unit itself. */
250 _cleanup_free_
char *node
= NULL
, *unit
= NULL
;
253 if (!fstab_test_option(opts
, "_netdev\0"))
256 node
= fstab_node_to_udev_node(what
);
260 /* Nothing to apply dependencies to. */
261 if (!is_device_path(node
))
264 r
= unit_name_from_path(node
, ".device", &unit
);
266 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
269 /* See mount_add_default_dependencies for explanation why we create such
271 return write_drop_in_format(dir
, unit
, 50, "netdev-dependencies",
272 "# Automatically generated by %s\n\n"
274 "After=" SPECIAL_NETWORK_ONLINE_TARGET
" " SPECIAL_NETWORK_TARGET
"\n"
275 "Wants=" SPECIAL_NETWORK_ONLINE_TARGET
"\n",
276 program_invocation_short_name
);
279 int generator_write_initrd_root_device_deps(const char *dir
, const char *what
) {
280 _cleanup_free_
char *unit
= NULL
;
283 r
= unit_name_from_path(what
, ".device", &unit
);
285 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
288 return write_drop_in_format(dir
, SPECIAL_INITRD_ROOT_DEVICE_TARGET
, 50, "root-device",
289 "# Automatically generated by %s\n\n"
293 program_invocation_short_name
,
298 int generator_hook_up_mkswap(
302 _cleanup_free_
char *node
= NULL
, *unit
= NULL
, *escaped
= NULL
, *where_unit
= NULL
;
303 _cleanup_fclose_
FILE *f
= NULL
;
304 const char *unit_file
;
307 node
= fstab_node_to_udev_node(what
);
311 /* Nothing to work on. */
312 if (!is_device_path(node
))
313 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
314 "Cannot format something that is not a device node: %s",
317 r
= unit_name_from_path_instance("systemd-mkswap", node
, ".service", &unit
);
319 return log_error_errno(r
, "Failed to make unit instance name from path \"%s\": %m",
322 unit_file
= strjoina(dir
, "/", unit
);
323 log_debug("Creating %s", unit_file
);
325 escaped
= cescape(node
);
329 r
= unit_name_from_path(what
, ".swap", &where_unit
);
331 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
334 f
= fopen(unit_file
, "wxe");
336 return log_error_errno(errno
, "Failed to create unit file %s: %m",
340 "# Automatically generated by %s\n\n"
342 "Description=Make Swap on %%f\n"
343 "Documentation=man:systemd-mkswap@.service(8)\n"
344 "DefaultDependencies=no\n"
345 "BindsTo=%%i.device\n"
346 "Conflicts=shutdown.target\n"
348 "Before=shutdown.target %s\n"
352 "RemainAfterExit=yes\n"
353 "ExecStart="SYSTEMD_MAKEFS_PATH
" swap %s\n"
355 program_invocation_short_name
,
359 r
= fflush_and_check(f
);
361 return log_error_errno(r
, "Failed to write unit file %s: %m", unit_file
);
363 return generator_add_symlink(dir
, where_unit
, "requires", unit
);
366 int generator_hook_up_mkfs(
372 _cleanup_free_
char *node
= NULL
, *unit
= NULL
, *escaped
= NULL
, *where_unit
= NULL
;
373 _cleanup_fclose_
FILE *f
= NULL
;
374 const char *unit_file
;
377 node
= fstab_node_to_udev_node(what
);
381 /* Nothing to work on. */
382 if (!is_device_path(node
))
383 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
384 "Cannot format something that is not a device node: %s",
387 if (!type
|| streq(type
, "auto"))
388 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
389 "Cannot format partition %s, filesystem type is not specified",
392 r
= unit_name_from_path_instance("systemd-makefs", node
, ".service", &unit
);
394 return log_error_errno(r
, "Failed to make unit instance name from path \"%s\": %m",
397 unit_file
= strjoina(dir
, "/", unit
);
398 log_debug("Creating %s", unit_file
);
400 escaped
= cescape(node
);
404 r
= unit_name_from_path(where
, ".mount", &where_unit
);
406 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
409 f
= fopen(unit_file
, "wxe");
411 return log_error_errno(errno
, "Failed to create unit file %s: %m",
415 "# Automatically generated by %s\n\n"
417 "Description=Make File System on %%f\n"
418 "Documentation=man:systemd-makefs@.service(8)\n"
419 "DefaultDependencies=no\n"
420 "BindsTo=%%i.device\n"
421 "Conflicts=shutdown.target\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=shutdown.target systemd-fsck@%%i.service %s\n"
429 "RemainAfterExit=yes\n"
430 "ExecStart="SYSTEMD_MAKEFS_PATH
" %s %s\n"
432 program_invocation_short_name
,
436 // XXX: what about local-fs-pre.target?
438 r
= fflush_and_check(f
);
440 return log_error_errno(r
, "Failed to write unit file %s: %m", unit_file
);
442 return generator_add_symlink(dir
, where_unit
, "requires", unit
);
445 int generator_hook_up_growfs(
448 const char *target
) {
450 _cleanup_free_
char *unit
= NULL
, *escaped
= NULL
, *where_unit
= NULL
;
451 _cleanup_fclose_
FILE *f
= NULL
;
452 const char *unit_file
;
455 escaped
= cescape(where
);
459 r
= unit_name_from_path_instance("systemd-growfs", where
, ".service", &unit
);
461 return log_error_errno(r
, "Failed to make unit instance name from path \"%s\": %m",
464 r
= unit_name_from_path(where
, ".mount", &where_unit
);
466 return log_error_errno(r
, "Failed to make unit name from path \"%s\": %m",
469 unit_file
= strjoina(dir
, "/", unit
);
470 log_debug("Creating %s", unit_file
);
472 f
= fopen(unit_file
, "wxe");
474 return log_error_errno(errno
, "Failed to create unit file %s: %m",
478 "# Automatically generated by %s\n\n"
480 "Description=Grow File System on %%f\n"
481 "Documentation=man:systemd-growfs@.service(8)\n"
482 "DefaultDependencies=no\n"
483 "BindsTo=%%i.mount\n"
484 "Conflicts=shutdown.target\n"
486 "Before=shutdown.target %s\n"
490 "RemainAfterExit=yes\n"
491 "ExecStart="SYSTEMD_GROWFS_PATH
" %s\n"
493 program_invocation_short_name
,
497 return generator_add_symlink(dir
, where_unit
, "wants", unit
);
500 int generator_enable_remount_fs_service(const char *dir
) {
501 /* Pull in systemd-remount-fs.service */
502 return generator_add_symlink(dir
, SPECIAL_LOCAL_FS_TARGET
, "wants",
503 SYSTEM_DATA_UNIT_PATH
"/" SPECIAL_REMOUNT_FS_SERVICE
);
506 void log_setup_generator(void) {
507 log_set_prohibit_ipc(true);