chiark / gitweb /
swap: handle nofail/noauto in core
authorTom Gundersen <teg@jklm.no>
Wed, 21 Aug 2013 14:48:56 +0000 (22:48 +0800)
committerTom Gundersen <teg@jklm.no>
Wed, 11 Sep 2013 12:43:24 +0000 (14:43 +0200)
src/core/swap.c
src/fstab-generator/fstab-generator.c

index f0e19ad..57d15eb 100644 (file)
@@ -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;
 }
 
index e780018..6ebe8aa 100644 (file)
 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;
 }