2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2014-03-05 22:26:10 +01:00
|
|
|
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <errno.h>
|
2014-03-05 22:26:10 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2020-09-30 16:19:12 +02:00
|
|
|
#include "cgroup-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "dropin.h"
|
|
|
|
#include "escape.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "fileio.h"
|
|
|
|
#include "fstab-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "generator.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
2014-03-05 22:26:10 +01:00
|
|
|
#include "mkdir.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "path-util.h"
|
|
|
|
#include "special.h"
|
2017-11-21 20:09:31 +01:00
|
|
|
#include "specifier.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "time-util.h"
|
2014-03-05 22:26:10 +01:00
|
|
|
#include "unit-name.h"
|
2015-10-23 18:52:53 +02:00
|
|
|
#include "util.h"
|
2014-03-05 22:26:10 +01:00
|
|
|
|
2017-12-09 19:23:26 +01:00
|
|
|
int generator_open_unit_file(
|
|
|
|
const char *dest,
|
|
|
|
const char *source,
|
|
|
|
const char *name,
|
|
|
|
FILE **file) {
|
|
|
|
|
|
|
|
const char *unit;
|
|
|
|
FILE *f;
|
2019-04-04 11:02:11 +02:00
|
|
|
int r;
|
2017-12-09 19:23:26 +01:00
|
|
|
|
2019-06-24 17:24:23 +02:00
|
|
|
unit = prefix_roota(dest, name);
|
2017-12-09 19:23:26 +01:00
|
|
|
|
2019-04-04 11:02:11 +02:00
|
|
|
r = fopen_unlocked(unit, "wxe", &f);
|
|
|
|
if (r < 0) {
|
|
|
|
if (source && r == -EEXIST)
|
|
|
|
return log_error_errno(r,
|
2017-12-09 19:23:26 +01:00
|
|
|
"Failed to create unit file %s, as it already exists. Duplicate entry in %s?",
|
|
|
|
unit, source);
|
|
|
|
else
|
2019-04-04 11:02:11 +02:00
|
|
|
return log_error_errno(r,
|
2017-12-09 19:23:26 +01:00
|
|
|
"Failed to create unit file %s: %m",
|
|
|
|
unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"# Automatically generated by %s\n\n",
|
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
*file = f;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:10:36 +01:00
|
|
|
int generator_add_symlink(const char *dir, const char *dst, const char *dep_type, const char *src) {
|
|
|
|
/* Adds a symlink from <dst>.<dep_type>/ to <src> (if src is absolute)
|
|
|
|
* or ../<src> (otherwise). */
|
2017-07-10 05:31:47 +02:00
|
|
|
|
|
|
|
const char *from, *to;
|
|
|
|
|
2018-12-20 12:10:36 +01:00
|
|
|
from = path_is_absolute(src) ? src : strjoina("../", src);
|
|
|
|
to = strjoina(dir, "/", dst, ".", dep_type, "/", basename(src));
|
2017-07-10 05:31:47 +02:00
|
|
|
|
|
|
|
mkdir_parents_label(to, 0755);
|
|
|
|
if (symlink(from, to) < 0)
|
2017-07-10 05:59:30 +02:00
|
|
|
if (errno != EEXIST)
|
|
|
|
return log_error_errno(errno, "Failed to create symlink \"%s\": %m", to);
|
2017-07-10 05:31:47 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-06 07:09:53 +02:00
|
|
|
static int write_fsck_sysroot_service(const char *dir, const char *what) {
|
2017-11-21 20:09:31 +01:00
|
|
|
_cleanup_free_ char *device = NULL, *escaped = NULL, *escaped2 = NULL;
|
2015-05-06 07:09:53 +02:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2015-10-22 18:24:59 +02:00
|
|
|
const char *unit;
|
2015-05-06 07:09:53 +02:00
|
|
|
int r;
|
|
|
|
|
2017-11-21 20:09:31 +01:00
|
|
|
escaped = specifier_escape(what);
|
2015-06-03 19:50:59 +02:00
|
|
|
if (!escaped)
|
|
|
|
return log_oom();
|
|
|
|
|
2017-11-21 20:09:31 +01:00
|
|
|
escaped2 = cescape(escaped);
|
|
|
|
if (!escaped2)
|
|
|
|
return log_oom();
|
|
|
|
|
2018-12-20 16:20:06 +01:00
|
|
|
unit = strjoina(dir, "/"SPECIAL_FSCK_ROOT_SERVICE);
|
2015-05-06 07:09:53 +02:00
|
|
|
log_debug("Creating %s", unit);
|
|
|
|
|
|
|
|
r = unit_name_from_path(what, ".device", &device);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to convert device \"%s\" to unit name: %m", what);
|
|
|
|
|
|
|
|
f = fopen(unit, "wxe");
|
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"# Automatically generated by %1$s\n\n"
|
|
|
|
"[Unit]\n"
|
|
|
|
"Description=File System Check on %2$s\n"
|
2017-11-21 23:18:05 +01:00
|
|
|
"Documentation=man:systemd-fsck-root.service(8)\n"
|
2015-05-06 07:09:53 +02:00
|
|
|
"DefaultDependencies=no\n"
|
|
|
|
"BindsTo=%3$s\n"
|
2018-11-26 17:38:52 +01:00
|
|
|
"Conflicts=shutdown.target\n"
|
2016-12-07 20:00:31 +01:00
|
|
|
"After=initrd-root-device.target local-fs-pre.target %3$s\n"
|
2015-05-06 07:09:53 +02:00
|
|
|
"Before=shutdown.target\n"
|
|
|
|
"\n"
|
|
|
|
"[Service]\n"
|
|
|
|
"Type=oneshot\n"
|
|
|
|
"RemainAfterExit=yes\n"
|
2015-06-03 19:50:59 +02:00
|
|
|
"ExecStart=" SYSTEMD_FSCK_PATH " %4$s\n"
|
2015-05-06 07:09:53 +02:00
|
|
|
"TimeoutSec=0\n",
|
|
|
|
program_invocation_short_name,
|
2017-11-21 20:09:31 +01:00
|
|
|
escaped,
|
2015-06-03 19:50:59 +02:00
|
|
|
device,
|
2017-11-21 20:09:31 +01:00
|
|
|
escaped2);
|
2015-05-06 07:09:53 +02:00
|
|
|
|
2015-05-15 17:06:59 +02:00
|
|
|
r = fflush_and_check(f);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to write unit file %s: %m", unit);
|
2015-05-06 07:09:53 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
int generator_write_fsck_deps(
|
|
|
|
FILE *f,
|
2015-05-02 19:01:29 +02:00
|
|
|
const char *dir,
|
2014-03-05 22:26:10 +01:00
|
|
|
const char *what,
|
|
|
|
const char *where,
|
2014-03-06 21:14:26 +01:00
|
|
|
const char *fstype) {
|
2014-03-05 22:26:10 +01:00
|
|
|
|
2015-04-30 20:21:00 +02:00
|
|
|
int r;
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
assert(f);
|
2015-05-02 19:01:29 +02:00
|
|
|
assert(dir);
|
2014-03-06 21:14:26 +01:00
|
|
|
assert(what);
|
|
|
|
assert(where);
|
2014-03-05 22:26:10 +01:00
|
|
|
|
|
|
|
if (!is_device_path(what)) {
|
|
|
|
log_warning("Checking was requested for \"%s\", but it is not a device.", what);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
if (!isempty(fstype) && !streq(fstype, "auto")) {
|
2014-04-12 22:07:45 +02:00
|
|
|
r = fsck_exists(fstype);
|
2015-10-22 18:24:59 +02:00
|
|
|
if (r < 0)
|
|
|
|
log_warning_errno(r, "Checking was requested for %s, but couldn't detect if fsck.%s may be used, proceeding: %m", what, fstype);
|
|
|
|
else if (r == 0) {
|
2014-03-05 22:26:10 +01:00
|
|
|
/* treat missing check as essentially OK */
|
2015-10-22 18:24:59 +02:00
|
|
|
log_debug("Checking was requested for %s, but fsck.%s does not exist.", what, fstype);
|
2014-06-24 19:37:22 +02:00
|
|
|
return 0;
|
2015-10-22 18:24:59 +02:00
|
|
|
}
|
2014-03-05 22:26:10 +01:00
|
|
|
}
|
|
|
|
|
2015-05-02 19:01:29 +02:00
|
|
|
if (path_equal(where, "/")) {
|
2015-10-22 18:24:59 +02:00
|
|
|
const char *lnk;
|
2014-03-05 22:26:10 +01:00
|
|
|
|
2019-07-10 16:18:05 +02:00
|
|
|
lnk = strjoina(dir, "/" SPECIAL_LOCAL_FS_TARGET ".wants/" SPECIAL_FSCK_ROOT_SERVICE);
|
2014-03-05 22:26:10 +01:00
|
|
|
|
2019-06-04 08:57:54 +02:00
|
|
|
(void) mkdir_parents(lnk, 0755);
|
2019-07-10 16:18:05 +02:00
|
|
|
if (symlink(SYSTEM_DATA_UNIT_PATH "/" SPECIAL_FSCK_ROOT_SERVICE, lnk) < 0)
|
2014-11-28 19:57:32 +01:00
|
|
|
return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
|
2014-03-05 22:26:10 +01:00
|
|
|
|
|
|
|
} else {
|
2015-05-06 07:09:53 +02:00
|
|
|
_cleanup_free_ char *_fsck = NULL;
|
2019-07-10 16:18:28 +02:00
|
|
|
const char *fsck, *dep;
|
2015-05-06 07:09:53 +02:00
|
|
|
|
|
|
|
if (in_initrd() && path_equal(where, "/sysroot")) {
|
|
|
|
r = write_fsck_sysroot_service(dir, what);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-12-20 16:20:06 +01:00
|
|
|
fsck = SPECIAL_FSCK_ROOT_SERVICE;
|
2019-07-10 16:18:28 +02:00
|
|
|
dep = "Requires";
|
2015-05-06 07:09:53 +02:00
|
|
|
} else {
|
2019-07-10 16:18:28 +02:00
|
|
|
/* When this is /usr, then let's add a Wants= dependency, otherwise a Requires=
|
|
|
|
* dependency. Why? We can't possibly unmount /usr during shutdown, but if we have a
|
|
|
|
* Requires= from /usr onto a fsck@.service unit and that unit is shut down, then
|
|
|
|
* we'd have to unmount /usr too. */
|
|
|
|
|
|
|
|
dep = !in_initrd() && path_equal(where, "/usr") ? "Wants" : "Requires";
|
|
|
|
|
2015-05-06 07:09:53 +02:00
|
|
|
r = unit_name_from_path_instance("systemd-fsck", what, ".service", &_fsck);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to create fsck service name: %m");
|
|
|
|
|
|
|
|
fsck = _fsck;
|
|
|
|
}
|
2014-03-05 22:26:10 +01:00
|
|
|
|
|
|
|
fprintf(f,
|
2019-07-10 16:18:28 +02:00
|
|
|
"%1$s=%2$s\n"
|
|
|
|
"After=%2$s\n",
|
|
|
|
dep, fsck);
|
2014-03-05 22:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-27 18:12:07 +02:00
|
|
|
|
2015-05-02 19:01:29 +02:00
|
|
|
int generator_write_timeouts(
|
|
|
|
const char *dir,
|
|
|
|
const char *what,
|
|
|
|
const char *where,
|
|
|
|
const char *opts,
|
|
|
|
char **filtered) {
|
2014-06-27 18:12:07 +02:00
|
|
|
|
2018-12-03 23:54:20 +01:00
|
|
|
/* Configure how long we wait for a device that backs a mount point or a
|
|
|
|
* swap partition to show up. This is useful to support endless device timeouts
|
|
|
|
* for devices that show up only after user input, like crypto devices. */
|
2014-06-27 18:12:07 +02:00
|
|
|
|
2015-01-09 22:58:29 +01:00
|
|
|
_cleanup_free_ char *node = NULL, *unit = NULL, *timeout = NULL;
|
2014-06-27 18:12:07 +02:00
|
|
|
usec_t u;
|
|
|
|
int r;
|
|
|
|
|
2017-07-03 14:29:32 +02:00
|
|
|
r = fstab_filter_options(opts, "comment=systemd.device-timeout\0"
|
|
|
|
"x-systemd.device-timeout\0",
|
2015-01-09 22:58:29 +01:00
|
|
|
NULL, &timeout, filtered);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
2014-06-27 21:03:05 +02:00
|
|
|
|
2017-07-03 14:29:32 +02:00
|
|
|
r = parse_sec_fix_0(timeout, &u);
|
2014-06-27 18:12:07 +02:00
|
|
|
if (r < 0) {
|
2015-04-30 20:21:00 +02:00
|
|
|
log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
|
2014-06-27 18:12:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = fstab_node_to_udev_node(what);
|
|
|
|
if (!node)
|
|
|
|
return log_oom();
|
2017-07-17 10:03:34 +02:00
|
|
|
if (!is_device_path(node)) {
|
|
|
|
log_warning("x-systemd.device-timeout ignored for %s", what);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-27 18:12:07 +02:00
|
|
|
|
2015-04-30 20:21:00 +02:00
|
|
|
r = unit_name_from_path(node, ".device", &unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make unit name from path: %m");
|
2014-06-27 18:12:07 +02:00
|
|
|
|
2014-07-01 00:41:17 +02:00
|
|
|
return write_drop_in_format(dir, unit, 50, "device-timeout",
|
2019-12-03 16:06:47 +01:00
|
|
|
"# Automatically generated by %s\n"
|
|
|
|
"# from supplied options \"%s\"\n\n"
|
2017-09-27 17:33:09 +02:00
|
|
|
"[Unit]\n"
|
|
|
|
"JobRunningTimeoutSec=%s",
|
|
|
|
program_invocation_short_name,
|
2019-12-03 16:06:47 +01:00
|
|
|
opts,
|
2017-09-27 17:33:09 +02:00
|
|
|
timeout);
|
2014-06-27 18:12:07 +02:00
|
|
|
}
|
2016-05-12 18:42:39 +02:00
|
|
|
|
2017-01-24 17:04:32 +01:00
|
|
|
int generator_write_device_deps(
|
|
|
|
const char *dir,
|
|
|
|
const char *what,
|
|
|
|
const char *where,
|
|
|
|
const char *opts) {
|
|
|
|
|
|
|
|
/* fstab records that specify _netdev option should apply the network
|
|
|
|
* ordering on the actual device depending on network connection. If we
|
|
|
|
* are not mounting real device (NFS, CIFS), we rely on _netdev effect
|
|
|
|
* on the mount unit itself. */
|
|
|
|
|
|
|
|
_cleanup_free_ char *node = NULL, *unit = NULL;
|
|
|
|
int r;
|
|
|
|
|
2020-03-30 10:47:31 +02:00
|
|
|
if (fstab_is_extrinsic(where, opts))
|
|
|
|
return 0;
|
|
|
|
|
2017-01-24 17:04:32 +01:00
|
|
|
if (!fstab_test_option(opts, "_netdev\0"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
node = fstab_node_to_udev_node(what);
|
|
|
|
if (!node)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
/* Nothing to apply dependencies to. */
|
|
|
|
if (!is_device_path(node))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = unit_name_from_path(node, ".device", &unit);
|
|
|
|
if (r < 0)
|
2017-11-21 23:18:05 +01:00
|
|
|
return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
|
|
|
|
node);
|
2017-01-24 17:04:32 +01:00
|
|
|
|
|
|
|
/* See mount_add_default_dependencies for explanation why we create such
|
|
|
|
* dependencies. */
|
|
|
|
return write_drop_in_format(dir, unit, 50, "netdev-dependencies",
|
|
|
|
"# Automatically generated by %s\n\n"
|
|
|
|
"[Unit]\n"
|
|
|
|
"After=" SPECIAL_NETWORK_ONLINE_TARGET " " SPECIAL_NETWORK_TARGET "\n"
|
|
|
|
"Wants=" SPECIAL_NETWORK_ONLINE_TARGET "\n",
|
|
|
|
program_invocation_short_name);
|
|
|
|
}
|
|
|
|
|
2016-05-12 18:42:39 +02:00
|
|
|
int generator_write_initrd_root_device_deps(const char *dir, const char *what) {
|
|
|
|
_cleanup_free_ char *unit = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = unit_name_from_path(what, ".device", &unit);
|
|
|
|
if (r < 0)
|
2017-11-21 23:18:05 +01:00
|
|
|
return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
|
|
|
|
what);
|
2016-05-12 18:42:39 +02:00
|
|
|
|
|
|
|
return write_drop_in_format(dir, SPECIAL_INITRD_ROOT_DEVICE_TARGET, 50, "root-device",
|
|
|
|
"# Automatically generated by %s\n\n"
|
2017-09-27 17:33:09 +02:00
|
|
|
"[Unit]\n"
|
|
|
|
"Requires=%s\n"
|
|
|
|
"After=%s",
|
|
|
|
program_invocation_short_name,
|
|
|
|
unit,
|
|
|
|
unit);
|
2016-05-12 18:42:39 +02:00
|
|
|
}
|
2017-11-21 23:18:05 +01:00
|
|
|
|
|
|
|
int generator_hook_up_mkswap(
|
|
|
|
const char *dir,
|
|
|
|
const char *what) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL;
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
const char *unit_file;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
node = fstab_node_to_udev_node(what);
|
|
|
|
if (!node)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
/* Nothing to work on. */
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!is_device_path(node))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Cannot format something that is not a device node: %s",
|
|
|
|
node);
|
2017-11-21 23:18:05 +01:00
|
|
|
|
|
|
|
r = unit_name_from_path_instance("systemd-mkswap", node, ".service", &unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
|
|
|
|
node);
|
|
|
|
|
2019-06-24 17:24:23 +02:00
|
|
|
unit_file = prefix_roota(dir, unit);
|
2017-11-21 23:18:05 +01:00
|
|
|
log_debug("Creating %s", unit_file);
|
|
|
|
|
|
|
|
escaped = cescape(node);
|
|
|
|
if (!escaped)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = unit_name_from_path(what, ".swap", &where_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
|
|
|
|
what);
|
|
|
|
|
|
|
|
f = fopen(unit_file, "wxe");
|
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to create unit file %s: %m",
|
|
|
|
unit_file);
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"# Automatically generated by %s\n\n"
|
|
|
|
"[Unit]\n"
|
|
|
|
"Description=Make Swap on %%f\n"
|
|
|
|
"Documentation=man:systemd-mkswap@.service(8)\n"
|
|
|
|
"DefaultDependencies=no\n"
|
|
|
|
"BindsTo=%%i.device\n"
|
2018-11-26 17:38:52 +01:00
|
|
|
"Conflicts=shutdown.target\n"
|
2017-11-21 23:18:05 +01:00
|
|
|
"After=%%i.device\n"
|
2018-11-26 17:38:52 +01:00
|
|
|
"Before=shutdown.target %s\n"
|
2017-11-21 23:18:05 +01:00
|
|
|
"\n"
|
|
|
|
"[Service]\n"
|
|
|
|
"Type=oneshot\n"
|
|
|
|
"RemainAfterExit=yes\n"
|
|
|
|
"ExecStart="SYSTEMD_MAKEFS_PATH " swap %s\n"
|
|
|
|
"TimeoutSec=0\n",
|
|
|
|
program_invocation_short_name,
|
|
|
|
where_unit,
|
|
|
|
escaped);
|
|
|
|
|
|
|
|
r = fflush_and_check(f);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to write unit file %s: %m", unit_file);
|
|
|
|
|
|
|
|
return generator_add_symlink(dir, where_unit, "requires", unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
int generator_hook_up_mkfs(
|
|
|
|
const char *dir,
|
|
|
|
const char *what,
|
|
|
|
const char *where,
|
|
|
|
const char *type) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL;
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
const char *unit_file;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
node = fstab_node_to_udev_node(what);
|
|
|
|
if (!node)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
/* Nothing to work on. */
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!is_device_path(node))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Cannot format something that is not a device node: %s",
|
|
|
|
node);
|
2017-11-21 23:18:05 +01:00
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!type || streq(type, "auto"))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Cannot format partition %s, filesystem type is not specified",
|
|
|
|
node);
|
2017-11-21 23:18:05 +01:00
|
|
|
|
2019-02-21 07:29:25 +01:00
|
|
|
r = unit_name_from_path_instance("systemd-makefs", node, ".service", &unit);
|
2017-11-21 23:18:05 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
|
|
|
|
node);
|
|
|
|
|
2019-06-24 17:24:23 +02:00
|
|
|
unit_file = prefix_roota(dir, unit);
|
2017-11-21 23:18:05 +01:00
|
|
|
log_debug("Creating %s", unit_file);
|
|
|
|
|
|
|
|
escaped = cescape(node);
|
|
|
|
if (!escaped)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = unit_name_from_path(where, ".mount", &where_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
|
|
|
|
where);
|
|
|
|
|
|
|
|
f = fopen(unit_file, "wxe");
|
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to create unit file %s: %m",
|
|
|
|
unit_file);
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"# Automatically generated by %s\n\n"
|
|
|
|
"[Unit]\n"
|
|
|
|
"Description=Make File System on %%f\n"
|
2019-02-21 07:29:25 +01:00
|
|
|
"Documentation=man:systemd-makefs@.service(8)\n"
|
2017-11-21 23:18:05 +01:00
|
|
|
"DefaultDependencies=no\n"
|
|
|
|
"BindsTo=%%i.device\n"
|
2018-11-26 17:38:52 +01:00
|
|
|
"Conflicts=shutdown.target\n"
|
2017-11-21 23:18:05 +01:00
|
|
|
"After=%%i.device\n"
|
|
|
|
/* fsck might or might not be used, so let's be safe and order
|
|
|
|
* ourselves before both systemd-fsck@.service and the mount unit. */
|
2018-11-26 17:38:52 +01:00
|
|
|
"Before=shutdown.target systemd-fsck@%%i.service %s\n"
|
2017-11-21 23:18:05 +01:00
|
|
|
"\n"
|
|
|
|
"[Service]\n"
|
|
|
|
"Type=oneshot\n"
|
|
|
|
"RemainAfterExit=yes\n"
|
|
|
|
"ExecStart="SYSTEMD_MAKEFS_PATH " %s %s\n"
|
|
|
|
"TimeoutSec=0\n",
|
|
|
|
program_invocation_short_name,
|
|
|
|
where_unit,
|
|
|
|
type,
|
|
|
|
escaped);
|
|
|
|
// XXX: what about local-fs-pre.target?
|
|
|
|
|
|
|
|
r = fflush_and_check(f);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to write unit file %s: %m", unit_file);
|
|
|
|
|
|
|
|
return generator_add_symlink(dir, where_unit, "requires", unit);
|
|
|
|
}
|
2017-11-29 20:02:11 +01:00
|
|
|
|
|
|
|
int generator_hook_up_growfs(
|
|
|
|
const char *dir,
|
|
|
|
const char *where,
|
|
|
|
const char *target) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *unit = NULL, *escaped = NULL, *where_unit = NULL;
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
|
|
|
const char *unit_file;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
escaped = cescape(where);
|
|
|
|
if (!escaped)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = unit_name_from_path_instance("systemd-growfs", where, ".service", &unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
|
|
|
|
where);
|
|
|
|
|
|
|
|
r = unit_name_from_path(where, ".mount", &where_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
|
|
|
|
where);
|
|
|
|
|
2019-06-24 17:24:23 +02:00
|
|
|
unit_file = prefix_roota(dir, unit);
|
2017-11-29 20:02:11 +01:00
|
|
|
log_debug("Creating %s", unit_file);
|
|
|
|
|
|
|
|
f = fopen(unit_file, "wxe");
|
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to create unit file %s: %m",
|
|
|
|
unit_file);
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"# Automatically generated by %s\n\n"
|
|
|
|
"[Unit]\n"
|
|
|
|
"Description=Grow File System on %%f\n"
|
|
|
|
"Documentation=man:systemd-growfs@.service(8)\n"
|
|
|
|
"DefaultDependencies=no\n"
|
|
|
|
"BindsTo=%%i.mount\n"
|
2018-11-26 17:38:52 +01:00
|
|
|
"Conflicts=shutdown.target\n"
|
2017-11-29 20:02:11 +01:00
|
|
|
"After=%%i.mount\n"
|
2020-01-21 10:40:18 +01:00
|
|
|
"Before=shutdown.target %s\n",
|
|
|
|
program_invocation_short_name,
|
|
|
|
target);
|
|
|
|
|
|
|
|
if (empty_or_root(where)) /* Make sure the root fs is actually writable before we resize it */
|
|
|
|
fprintf(f,
|
|
|
|
"After=systemd-remount-fs.service\n");
|
|
|
|
|
|
|
|
fprintf(f,
|
2017-11-29 20:02:11 +01:00
|
|
|
"\n"
|
|
|
|
"[Service]\n"
|
|
|
|
"Type=oneshot\n"
|
|
|
|
"RemainAfterExit=yes\n"
|
|
|
|
"ExecStart="SYSTEMD_GROWFS_PATH " %s\n"
|
|
|
|
"TimeoutSec=0\n",
|
|
|
|
escaped);
|
|
|
|
|
|
|
|
return generator_add_symlink(dir, where_unit, "wants", unit);
|
|
|
|
}
|
2018-11-19 21:32:11 +01:00
|
|
|
|
2018-12-20 15:52:52 +01:00
|
|
|
int generator_enable_remount_fs_service(const char *dir) {
|
|
|
|
/* Pull in systemd-remount-fs.service */
|
|
|
|
return generator_add_symlink(dir, SPECIAL_LOCAL_FS_TARGET, "wants",
|
|
|
|
SYSTEM_DATA_UNIT_PATH "/" SPECIAL_REMOUNT_FS_SERVICE);
|
|
|
|
}
|
|
|
|
|
2019-12-19 17:38:55 +01:00
|
|
|
int generator_write_blockdev_dependency(
|
|
|
|
FILE *f,
|
|
|
|
const char *what) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *escaped = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(f);
|
|
|
|
assert(what);
|
|
|
|
|
|
|
|
if (!path_startswith(what, "/dev/"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = unit_name_path_escape(what, &escaped);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to escape device node path %s: %m", what);
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"After=blockdev@%s.target\n",
|
|
|
|
escaped);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int generator_write_cryptsetup_unit_section(
|
|
|
|
FILE *f,
|
|
|
|
const char *source) {
|
|
|
|
|
|
|
|
assert(f);
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"[Unit]\n"
|
|
|
|
"Description=Cryptography Setup for %%I\n"
|
|
|
|
"Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n");
|
|
|
|
|
|
|
|
if (source)
|
|
|
|
fprintf(f, "SourcePath=%s\n", source);
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"DefaultDependencies=no\n"
|
|
|
|
"IgnoreOnIsolate=true\n"
|
2020-12-04 03:38:28 +01:00
|
|
|
"After=cryptsetup-pre.target systemd-udevd-kernel.socket\n"
|
2019-12-19 17:38:55 +01:00
|
|
|
"Before=blockdev@dev-mapper-%%i.target\n"
|
|
|
|
"Wants=blockdev@dev-mapper-%%i.target\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int generator_write_cryptsetup_service_section(
|
|
|
|
FILE *f,
|
|
|
|
const char *name,
|
|
|
|
const char *what,
|
|
|
|
const char *password,
|
|
|
|
const char *options) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *name_escaped = NULL, *what_escaped = NULL, *password_escaped = NULL, *options_escaped = NULL;
|
|
|
|
|
|
|
|
assert(f);
|
|
|
|
assert(name);
|
|
|
|
assert(what);
|
|
|
|
|
|
|
|
name_escaped = specifier_escape(name);
|
|
|
|
if (!name_escaped)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
what_escaped = specifier_escape(what);
|
|
|
|
if (!what_escaped)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
if (password) {
|
|
|
|
password_escaped = specifier_escape(password);
|
|
|
|
if (!password_escaped)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options) {
|
|
|
|
options_escaped = specifier_escape(options);
|
|
|
|
if (!options_escaped)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"\n"
|
|
|
|
"[Service]\n"
|
|
|
|
"Type=oneshot\n"
|
|
|
|
"RemainAfterExit=yes\n"
|
|
|
|
"TimeoutSec=0\n" /* The binary handles timeouts on its own */
|
|
|
|
"KeyringMode=shared\n" /* Make sure we can share cached keys among instances */
|
|
|
|
"OOMScoreAdjust=500\n" /* Unlocking can allocate a lot of memory if Argon2 is used */
|
|
|
|
"ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
|
|
|
|
"ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
|
|
|
|
name_escaped, what_escaped, strempty(password_escaped), strempty(options_escaped),
|
|
|
|
name_escaped);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-19 21:32:11 +01:00
|
|
|
void log_setup_generator(void) {
|
2020-09-30 16:19:12 +02:00
|
|
|
/* Disable talking to syslog/journal (i.e. the two IPC-based loggers) if we run in system context. */
|
|
|
|
if (cg_pid_get_owner_uid(0, NULL) == -ENXIO /* not running in a per-user slice */)
|
|
|
|
log_set_prohibit_ipc(true);
|
|
|
|
|
|
|
|
log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); /* This effectively means: journal for per-user generators, kmsg otherwise */
|
|
|
|
log_parse_environment();
|
|
|
|
(void) log_open();
|
2018-11-19 21:32:11 +01:00
|
|
|
}
|