richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
803fb7
From 8f149756435998d009a8edc7206c5de038e5cbf1 Mon Sep 17 00:00:00 2001
803fb7
From: Karel Zak <kzak@redhat.com>
803fb7
Date: Mon, 18 May 2015 12:30:37 +0200
803fb7
Subject: [PATCH] fstab-generator: add x-systemd.requires and
803fb7
 x-systemd.requires-mounts-for
803fb7
803fb7
Currently we have no way how to specify dependencies between fstab
803fb7
entries (or another units) in the /etc/fstab. It means that users are
803fb7
forced to bypass fstab and write .mount units manually.
803fb7
803fb7
The patch introduces new systemd fstab options:
803fb7
803fb7
x-systemd.requires=<PATH>
803fb7
803fb7
 - to specify dependence an another mount (PATH is translated to unit name)
803fb7
803fb7
x-systemd.requires=<UNIT>
803fb7
803fb7
 - to specify dependence on arbitrary UNIT
803fb7
803fb7
x-systemd.requires-mounts-for=<PATH ...>
803fb7
803fb7
 - to specify dependence on another paths, implemented by
803fb7
   RequiresMountsFor=. The option may be specified more than once.
803fb7
803fb7
For example two bind mounts where B depends on A:
803fb7
803fb7
 /mnt/test/A    /mnt/test/A     none    bind,defaults
803fb7
 /mnt/test/A    /mnt/test/B     none    bind,x-systemd.requires=/mnt/test/A
803fb7
803fb7
More complex example with overlay FS where one mount point depends on
803fb7
"low" and "upper" directories:
803fb7
803fb7
 /dev/sdc1   /mnt/low    ext4     defaults
803fb7
 /dev/sdc2   /mnt/high   ext4     defaults
803fb7
 overlay     /mnt/merged overlay  lowerdir=/mnt/low,upperdir=/mnt/high/data,workdir=/mnt/high/work,x-systemd.requires-mounts-for=/mnt/low,x-systemd.requires-mounts-for=mnt/high
803fb7
803fb7
https://bugzilla.redhat.com/show_bug.cgi?id=812826
803fb7
https://bugzilla.redhat.com/show_bug.cgi?id=1164334
803fb7
803fb7
Conflicts:
803fb7
	src/fstab-generator/fstab-generator.c
803fb7
803fb7
Cherry-picked from: 3519d230c8bafe834b2dac26ace49fcfba139823
803fb7
Resolves: #1164334
803fb7
---
803fb7
 man/systemd.mount.xml                 | 30 ++++++++++++++
803fb7
 src/fstab-generator/fstab-generator.c | 76 +++++++++++++++++++++++++++++++++++
803fb7
 src/shared/fstab-util.c               | 30 ++++++++++++++
803fb7
 src/shared/fstab-util.h               |  2 +
803fb7
 4 files changed, 138 insertions(+)
803fb7
803fb7
diff --git a/man/systemd.mount.xml b/man/systemd.mount.xml
803fb7
index fcb9a4416..8e652e133 100644
803fb7
--- a/man/systemd.mount.xml
803fb7
+++ b/man/systemd.mount.xml
803fb7
@@ -138,6 +138,36 @@
803fb7
 
803fb7
     <variablelist class='fstab-options'>
803fb7
 
803fb7
+      <varlistentry>
803fb7
+        <term><option>x-systemd.requires=</option></term>
803fb7
+
803fb7
+        <listitem><para>Configures a <varname>Requires=</varname> and
803fb7
+        an <varname>After=</varname> dependency between the created
803fb7
+        mount unit and another systemd unit, such as a device or mount
803fb7
+        unit. The argument should be a unit name, or an absolute path
803fb7
+        to a device node or mount point.  This option may be specified
803fb7
+        more than once. This option is particularly useful for mount
803fb7
+        point declarations that need an additional device to be around
803fb7
+        (such as an external journal device for journal file systems)
803fb7
+        or an additional mount to be in place (such as an overlay file
803fb7
+        system that merges multiple mount points). See
803fb7
+        <varname>After=</varname> and <varname>Requires=</varname> in
803fb7
+        <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
803fb7
+        for details.</para></listitem>
803fb7
+      </varlistentry>
803fb7
+
803fb7
+      <varlistentry>
803fb7
+        <term><option>x-systemd.requires-mounts-for=</option></term>
803fb7
+
803fb7
+        <listitem><para>Configures a
803fb7
+        <varname>RequiresMountsFor=</varname> dependency between the
803fb7
+        created mount unit and other mount units. The argument must be
803fb7
+        an absolute path. This option may be specified more than once.
803fb7
+        See <varname>RequiresMountsFor=</varname> in
803fb7
+        <citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
803fb7
+        for details.</para></listitem>
803fb7
+       </varlistentry>
803fb7
+
803fb7
       <varlistentry>
803fb7
         <term><option>x-systemd.automount</option></term>
803fb7
 
803fb7
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
803fb7
index 8e2f522bd..65ed20579 100644
803fb7
--- a/src/fstab-generator/fstab-generator.c
803fb7
+++ b/src/fstab-generator/fstab-generator.c
803fb7
@@ -155,6 +155,64 @@ static bool mount_in_initrd(struct mntent *me) {
803fb7
                streq(me->mnt_dir, "/usr");
803fb7
 }
803fb7
 
803fb7
+static int write_requires_after(FILE *f, const char *opts) {
803fb7
+        _cleanup_strv_free_ char **names = NULL, **units = NULL;
803fb7
+        _cleanup_free_ char *res = NULL;
803fb7
+        char **s;
803fb7
+        int r;
803fb7
+
803fb7
+        assert(f);
803fb7
+        assert(opts);
803fb7
+
803fb7
+        r = fstab_extract_values(opts, "x-systemd.requires", &names);
803fb7
+        if (r < 0)
803fb7
+                return log_warning_errno(r, "Failed to parse options: %m");
803fb7
+        if (r == 0)
803fb7
+                return 0;
803fb7
+
803fb7
+        STRV_FOREACH(s, names) {
803fb7
+                char *x;
803fb7
+
803fb7
+                x = unit_name_mangle_with_suffix(*s, MANGLE_NOGLOB, ".mount");
803fb7
+                if (!x)
803fb7
+                        return log_error_errno(r, "Failed to generate unit name: %m");
803fb7
+                r = strv_consume(&units, x);
803fb7
+                if (r < 0)
803fb7
+                        return log_oom();
803fb7
+        }
803fb7
+
803fb7
+        if (units) {
803fb7
+                res = strv_join(units, " ");
803fb7
+                if (!res)
803fb7
+                        return log_oom();
803fb7
+                fprintf(f, "After=%1$s\nRequires=%1$s\n", res);
803fb7
+        }
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
+
803fb7
+static int write_requires_mounts_for(FILE *f, const char *opts) {
803fb7
+        _cleanup_strv_free_ char **paths = NULL;
803fb7
+        _cleanup_free_ char *res = NULL;
803fb7
+        int r;
803fb7
+
803fb7
+        assert(f);
803fb7
+        assert(opts);
803fb7
+
803fb7
+        r = fstab_extract_values(opts, "x-systemd.requires-mounts-for", &paths);
803fb7
+        if (r < 0)
803fb7
+                return log_warning_errno(r, "Failed to parse options: %m");
803fb7
+        if (r == 0)
803fb7
+                return 0;
803fb7
+
803fb7
+        res = strv_join(paths, " ");
803fb7
+        if (!res)
803fb7
+                return log_oom();
803fb7
+
803fb7
+        fprintf(f, "RequiresMountsFor=%s\n", res);
803fb7
+
803fb7
+        return 0;
803fb7
+}
803fb7
 static int add_mount(
803fb7
                 const char *what,
803fb7
                 const char *where,
803fb7
@@ -225,6 +283,15 @@ static int add_mount(
803fb7
         if (post && !noauto && !nofail && !automount)
803fb7
                 fprintf(f, "Before=%s\n", post);
803fb7
 
803fb7
+        if (!automount && opts) {
803fb7
+                 r = write_requires_after(f, opts);
803fb7
+                 if (r < 0)
803fb7
+                         return r;
803fb7
+                 r = write_requires_mounts_for(f, opts);
803fb7
+                 if (r < 0)
803fb7
+                         return r;
803fb7
+        }
803fb7
+
803fb7
         if (passno != 0) {
803fb7
                 r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
803fb7
                 if (r < 0)
803fb7
@@ -289,6 +356,15 @@ static int add_mount(
803fb7
                                 "Before=%s\n",
803fb7
                                 post);
803fb7
 
803fb7
+                if (opts) {
803fb7
+                        r = write_requires_after(f, opts);
803fb7
+                        if (r < 0)
803fb7
+                                return r;
803fb7
+                        r = write_requires_mounts_for(f, opts);
803fb7
+                        if (r < 0)
803fb7
+                                return r;
803fb7
+                }
803fb7
+
803fb7
                 fprintf(f,
803fb7
                         "[Automount]\n"
803fb7
                         "Where=%s\n",
803fb7
diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c
803fb7
index cf317e17b..e231a0ff8 100644
803fb7
--- a/src/shared/fstab-util.c
803fb7
+++ b/src/shared/fstab-util.c
803fb7
@@ -125,6 +125,36 @@ answer:
803fb7
         return !!n;
803fb7
 }
803fb7
 
803fb7
+int fstab_extract_values(const char *opts, const char *name, char ***values) {
803fb7
+        _cleanup_strv_free_ char **optsv = NULL, **res = NULL;
803fb7
+        char **s;
803fb7
+
803fb7
+        assert(opts);
803fb7
+        assert(name);
803fb7
+        assert(values);
803fb7
+
803fb7
+        optsv = strv_split(opts, ",");
803fb7
+        if (!optsv)
803fb7
+                return -ENOMEM;
803fb7
+
803fb7
+        STRV_FOREACH(s, optsv) {
803fb7
+                char *arg;
803fb7
+                int r;
803fb7
+
803fb7
+                arg = startswith(*s, name);
803fb7
+                if (!arg || *arg != '=')
803fb7
+                        continue;
803fb7
+                r = strv_extend(&res, arg + 1);
803fb7
+                if (r < 0)
803fb7
+                        return r;
803fb7
+        }
803fb7
+
803fb7
+        *values = res;
803fb7
+        res = NULL;
803fb7
+
803fb7
+        return !!*values;
803fb7
+}
803fb7
+
803fb7
 int fstab_find_pri(const char *options, int *ret) {
803fb7
         _cleanup_free_ char *opt = NULL;
803fb7
         int r;
803fb7
diff --git a/src/shared/fstab-util.h b/src/shared/fstab-util.h
803fb7
index 9f6b32eaf..387c562a9 100644
803fb7
--- a/src/shared/fstab-util.h
803fb7
+++ b/src/shared/fstab-util.h
803fb7
@@ -28,6 +28,8 @@
803fb7
 int fstab_filter_options(const char *opts, const char *names,
803fb7
                          const char **namefound, char **value, char **filtered);
803fb7
 
803fb7
+int fstab_extract_values(const char *opts, const char *name, char ***values);
803fb7
+
803fb7
 static inline bool fstab_test_option(const char *opts, const char *names) {
803fb7
         return !!fstab_filter_options(opts, names, NULL, NULL, NULL);
803fb7
 }