[systemd-devel] [PATCH] fstab-generator: add x-systemd.{after, requires-mounts-for}=

Karel Zak kzak at redhat.com
Tue May 12 09:04:50 PDT 2015


Currently we have no way how to specify dependencies between fstab
entries (or another units) in the /etc/fstab. It means that users are
forced to bypass fstab and write .mount units manually.

Years ago Lennart suggested to add

x-systemd.after=<PATH>

 - to specify dependence an another mount (PATH is translated to <PATH>.mount)

x-systemd.after=<UNIT>

 - to specify dependence on arbitrary UNIT

The x-systemd.after= is implemented by After= and Requires=.

x-systemd.requires-mounts-for=<PATH ...>

 - to specify dependence on another paths, implemented by
   RequiresMountsFor=.

For example two bind mounts where B depends on A:

 /mnt/test/A    /mnt/test/A     none    bind,defaults
 /mnt/test/A    /mnt/test/B     none    bind,x-systemd.after=/mnt/test/A

More complex example with overlay FS where one mount point depends on
"low" and "upper" directories:

 /dev/sdc1   /mnt/low    ext4     defaults
 /dev/sdc2   /mnt/high   ext4     defaults
 overlay     /mnt/merged overlay  lowerdir=/mnt/low,upperdir=/mnt/high/data,workdir=/mnt/high/work,x-systemd.requires-mounts-for=/mnt/low\040/mnt/high

References: https://bugzilla.redhat.com/show_bug.cgi?id=812826
Signed-off-by: Karel Zak <kzak at redhat.com>
---
 man/systemd.mount.xml                 | 21 +++++++++++++++
 src/fstab-generator/fstab-generator.c | 49 +++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/man/systemd.mount.xml b/man/systemd.mount.xml
index 862f42e..34614cb 100644
--- a/man/systemd.mount.xml
+++ b/man/systemd.mount.xml
@@ -139,6 +139,27 @@
     <variablelist class='fstab-options'>
 
       <varlistentry>
+        <term><option>x-systemd.after=</option></term>
+
+        <listitem><para>Configures <varname>After=</varname> and <varname>Requires=</varname> dependence
+        between the mount and another mount or systemd unit. The argument is absolute path to
+        another mount point or unit name. 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 <varname>RequiresMountsFor=</varname> dependence between the mount and
+        another mount. The argument is a space-separated list of absolute paths. Note
+        that <filename>/etc/fstab</filename> format requires to escape space as \040.
+        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 167ec60..a5f13fd 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -177,6 +177,45 @@ 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_free_ char *arg = NULL, *unit = NULL;
+        int r;
+
+        r = fstab_filter_options(opts, "x-systemd.after\0", NULL, &arg, NULL);
+        if (r < 0)
+                return log_warning_errno(r, "Failed to parse options: %m");
+        if (r == 0)
+                return 0;
+
+        if (*arg == '/') {
+                r = unit_name_from_path(arg, ".mount", &unit);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to generate unit name: %m");
+        } else
+                unit = arg;
+
+        fprintf(f, "After=%1$s\nRequires=%1$s\n", unit);
+
+        if (unit == arg)
+                unit = NULL;
+
+        return 0;
+}
+
+static int write_requires_mounts_for(FILE *f, const char *opts) {
+        _cleanup_free_ char *arg;
+        int r;
+
+        r = fstab_filter_options(opts, "x-systemd.requires-mounts-for\0", NULL, &arg, NULL);
+        if (r < 0)
+                return log_warning_errno(r, "Failed to parse options: %m");
+        if (r == 0)
+                return 0;
+
+        fprintf(f, "RequiresMountsFor=%s\n", arg);
+        return 0;
+}
+
 static int add_mount(
                 const char *what,
                 const char *where,
@@ -251,6 +290,11 @@ static int add_mount(
         if (post && !noauto && !nofail && !automount)
                 fprintf(f, "Before=%s\n", post);
 
+        if (!automount && opts) {
+                 write_requires_after(f, opts);
+                 write_requires_mounts_for(f, opts);
+        }
+
         if (passno != 0) {
                 r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
                 if (r < 0)
@@ -315,6 +359,11 @@ static int add_mount(
                                 "Before=%s\n",
                                 post);
 
+                if (opts) {
+                        write_requires_after(f, opts);
+                        write_requires_mounts_for(f, opts);
+                }
+
                 fprintf(f,
                         "[Automount]\n"
                         "Where=%s\n",
-- 
2.1.0



More information about the systemd-devel mailing list