[systemd-devel] [PATCH 7/7] swap: handle nofail/noauto in core

Tom Gundersen teg at jklm.no
Fri Aug 23 00:09:34 PDT 2013


---
 src/core/swap.c                       | 15 +++++++++
 src/fstab-generator/fstab-generator.c | 59 ++---------------------------------
 2 files changed, 18 insertions(+), 56 deletions(-)

diff --git a/src/core/swap.c b/src/core/swap.c
index f0e19ad..57d15eb 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -197,6 +197,7 @@ static int swap_add_device_links(Swap *s) {
 }
 
 static int swap_add_default_dependencies(Swap *s) {
+        bool nofail = false, noauto = false;
         int r;
 
         assert(s);
@@ -211,6 +212,20 @@ static int swap_add_default_dependencies(Swap *s) {
         if (r < 0)
                 return r;
 
+        if (s->from_fragment) {
+                SwapParameters *p = &s->parameters_fragment;
+
+                nofail = p->nofail;
+                noauto = p->noauto;
+        }
+
+        if (!noauto) {
+                r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, (nofail ? UNIT_WANTED_BY : UNIT_REQUIRED_BY),
+                                                      SPECIAL_SWAP_TARGET, NULL, true);
+                if (r < 0)
+                        return r;
+        }
+
         return 0;
 }
 
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
index e780018..6ebe8aa 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -38,22 +38,6 @@
 static const char *arg_dest = "/tmp";
 static bool arg_enabled = true;
 
-static int device_name(const char *path, char **unit) {
-        char *p;
-
-        assert(path);
-
-        if (!is_device_path(path))
-                return 0;
-
-        p = unit_name_from_path(path, ".device");
-        if (!p)
-                return log_oom();
-
-        *unit = p;
-        return 1;
-}
-
 static int mount_find_pri(struct mntent *me, int *ret) {
         char *end, *pri;
         unsigned long r;
@@ -82,7 +66,6 @@ static int mount_find_pri(struct mntent *me, int *ret) {
 static int add_swap(const char *what, struct mntent *me) {
         _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL, *device = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        bool noauto, nofail;
         int r, pri = -1;
 
         assert(what);
@@ -94,9 +77,6 @@ static int add_swap(const char *what, struct mntent *me) {
                 return pri;
         }
 
-        noauto = !!hasmntopt(me, "noauto");
-        nofail = !!hasmntopt(me, "nofail");
-
         name = unit_name_from_path(what, ".swap");
         if (!name)
                 return log_oom();
@@ -114,14 +94,10 @@ static int add_swap(const char *what, struct mntent *me) {
                 return -errno;
         }
 
-        fputs("# Automatically generated by systemd-fstab-generator\n\n"
-              "[Unit]\n"
-              "SourcePath=/etc/fstab\n", f);
-
-        if (!noauto && !nofail)
-                fputs("Before=" SPECIAL_SWAP_TARGET "\n", f);
-
         fprintf(f,
+                "# Automatically generated by systemd-fstab-generator\n\n"
+                "[Unit]\n"
+                "SourcePath=/etc/fstab\n"
                 "\n"
                 "[Swap]\n"
                 "What=%s\n",
@@ -138,35 +114,6 @@ static int add_swap(const char *what, struct mntent *me) {
                 return -errno;
         }
 
-        if (!noauto) {
-                lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
-                if (!lnk)
-                        return log_oom();
-
-                mkdir_parents_label(lnk, 0755);
-                if (symlink(unit, lnk) < 0) {
-                        log_error("Failed to create symlink %s: %m", lnk);
-                        return -errno;
-                }
-
-                r = device_name(what, &device);
-                if (r < 0)
-                        return r;
-
-                if (r > 0) {
-                        free(lnk);
-                        lnk = strjoin(arg_dest, "/", device, ".wants/", name, NULL);
-                        if (!lnk)
-                                return log_oom();
-
-                        mkdir_parents_label(lnk, 0755);
-                        if (symlink(unit, lnk) < 0) {
-                                log_error("Failed to create symlink %s: %m", lnk);
-                                return -errno;
-                        }
-                }
-        }
-
         return 0;
 }
 
-- 
1.8.3.4



More information about the systemd-devel mailing list