diff options
-rw-r--r-- | man/systemd.mount.xml | 30 | ||||
-rw-r--r-- | src/fstab-generator/fstab-generator.c | 77 | ||||
-rw-r--r-- | src/shared/fstab-util.c | 30 | ||||
-rw-r--r-- | src/shared/fstab-util.h | 2 |
4 files changed, 139 insertions, 0 deletions
diff --git a/man/systemd.mount.xml b/man/systemd.mount.xml index 862f42e594..ffffc56936 100644 --- a/man/systemd.mount.xml +++ b/man/systemd.mount.xml @@ -139,6 +139,36 @@ <variablelist class='fstab-options'> <varlistentry> + <term><option>x-systemd.requires=</option></term> + + <listitem><para>Configures a <varname>Requires=</varname> and + an <varname>After=</varname> dependency between the created + mount unit and another systemd unit, such as a device or mount + unit. The argument should be a unit name, or an absolute path + to a device node or mount point. This option may be specified + more than once. This option is particularly useful for mount + point declarations that need an additional device to be around + (such as an external journal device for journal file systems) + or an additional mount to be in place (such as an overlay file + system that merges multiple mount points). See + <varname>After=</varname> and <varname>Requires=</varname> in + <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> + for details.</para></listitem> + </varlistentry> + + <varlistentry> + <term><option>x-systemd.requires-mounts-for=</option></term> + + <listitem><para>Configures a + <varname>RequiresMountsFor=</varname> dependency between the + created mount unit and other mount units. The argument must be + an absolute path. This option may be specified more than once. + See <varname>RequiresMountsFor=</varname> in + <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry> + for details.</para></listitem> + </varlistentry> + + <varlistentry> <term><option>x-systemd.automount</option></term> <listitem><para>An automount unit will be created for the file diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 11df81a97e..302fad37bb 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -177,6 +177,65 @@ static int write_idle_timeout(FILE *f, const char *where, const char *opts) { return 0; } +static int write_requires_after(FILE *f, const char *opts) { + _cleanup_strv_free_ char **names = NULL, **units = NULL; + _cleanup_free_ char *res = NULL; + char **s; + int r; + + assert(f); + assert(opts); + + r = fstab_extract_values(opts, "x-systemd.requires", &names); + if (r < 0) + return log_warning_errno(r, "Failed to parse options: %m"); + if (r == 0) + return 0; + + STRV_FOREACH(s, names) { + char *x; + + r = unit_name_mangle_with_suffix(*s, UNIT_NAME_NOGLOB, ".mount", &x); + if (r < 0) + return log_error_errno(r, "Failed to generate unit name: %m"); + r = strv_consume(&units, x); + if (r < 0) + return log_oom(); + } + + if (units) { + res = strv_join(units, " "); + if (!res) + return log_oom(); + fprintf(f, "After=%1$s\nRequires=%1$s\n", res); + } + + return 0; +} + +static int write_requires_mounts_for(FILE *f, const char *opts) { + _cleanup_strv_free_ char **paths = NULL; + _cleanup_free_ char *res = NULL; + int r; + + assert(f); + assert(opts); + + r = fstab_extract_values(opts, "x-systemd.requires-mounts-for", &paths); + if (r < 0) + return log_warning_errno(r, "Failed to parse options: %m"); + if (r == 0) + return 0; + + res = strv_join(paths, " "); + if (!res) + return log_oom(); + + fprintf(f, "RequiresMountsFor=%s\n", res); + + return 0; +} + static int add_mount( const char *what, const char *where, @@ -251,6 +310,15 @@ static int add_mount( if (post && !noauto && !nofail && !automount) fprintf(f, "Before=%s\n", post); + if (!automount && opts) { + r = write_requires_after(f, opts); + if (r < 0) + return r; + r = write_requires_mounts_for(f, opts); + if (r < 0) + return r; + } + if (passno != 0) { r = generator_write_fsck_deps(f, arg_dest, what, where, fstype); if (r < 0) @@ -315,6 +383,15 @@ static int add_mount( "Before=%s\n", post); + if (opts) { + r = write_requires_after(f, opts); + if (r < 0) + return r; + r = write_requires_mounts_for(f, opts); + if (r < 0) + return r; + } + fprintf(f, "[Automount]\n" "Where=%s\n", diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index cf317e17bd..e231a0ff80 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -125,6 +125,36 @@ answer: return !!n; } +int fstab_extract_values(const char *opts, const char *name, char ***values) { + _cleanup_strv_free_ char **optsv = NULL, **res = NULL; + char **s; + + assert(opts); + assert(name); + assert(values); + + optsv = strv_split(opts, ","); + if (!optsv) + return -ENOMEM; + + STRV_FOREACH(s, optsv) { + char *arg; + int r; + + arg = startswith(*s, name); + if (!arg || *arg != '=') + continue; + r = strv_extend(&res, arg + 1); + if (r < 0) + return r; + } + + *values = res; + res = NULL; + + return !!*values; +} + int fstab_find_pri(const char *options, int *ret) { _cleanup_free_ char *opt = NULL; int r; diff --git a/src/shared/fstab-util.h b/src/shared/fstab-util.h index 9f6b32eaf4..387c562a96 100644 --- a/src/shared/fstab-util.h +++ b/src/shared/fstab-util.h @@ -28,6 +28,8 @@ int fstab_filter_options(const char *opts, const char *names, const char **namefound, char **value, char **filtered); +int fstab_extract_values(const char *opts, const char *name, char ***values); + static inline bool fstab_test_option(const char *opts, const char *names) { return !!fstab_filter_options(opts, names, NULL, NULL, NULL); } |