naccyde / rpms / systemd

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