Harald Hoyer d4ee25
From c1f9c15f319f7dfb96bcbf2c3cf879f2b4457350 Mon Sep 17 00:00:00 2001
Harald Hoyer d4ee25
From: Tom Gundersen <teg@jklm.no>
Harald Hoyer d4ee25
Date: Mon, 16 Sep 2013 01:08:32 +0200
Harald Hoyer d4ee25
Subject: [PATCH] swap: create .wants symlink to 'auto' swap devices
Harald Hoyer d4ee25
Harald Hoyer d4ee25
As we load unit files lazily, we need to make sure something pulls in swap
Harald Hoyer d4ee25
units that should be started automatically, otherwise the default dependencies
Harald Hoyer d4ee25
will never be applied.
Harald Hoyer d4ee25
Harald Hoyer d4ee25
This partially reinstates code removed in
Harald Hoyer d4ee25
commit 64347fc2b983f33e7efb0fd2bb44e133fb9f30f4.
Harald Hoyer d4ee25
Harald Hoyer d4ee25
Also don't order swap devices after swap.target when they are 'nofail'.
Harald Hoyer d4ee25
---
Harald Hoyer d4ee25
 src/core/swap.c                       |  8 ++++++--
Harald Hoyer d4ee25
 src/fstab-generator/fstab-generator.c | 18 ++++++++++++++++--
Harald Hoyer d4ee25
 2 files changed, 22 insertions(+), 4 deletions(-)
Harald Hoyer d4ee25
Harald Hoyer d4ee25
diff --git a/src/core/swap.c b/src/core/swap.c
Harald Hoyer d4ee25
index 3950860..76c7d45 100644
Harald Hoyer d4ee25
--- a/src/core/swap.c
Harald Hoyer d4ee25
+++ b/src/core/swap.c
Harald Hoyer d4ee25
@@ -220,8 +220,12 @@ static int swap_add_default_dependencies(Swap *s) {
Harald Hoyer d4ee25
         }
Harald Hoyer d4ee25
 
Harald Hoyer d4ee25
         if (!noauto) {
Harald Hoyer d4ee25
-                r = unit_add_two_dependencies_by_name_inverse(UNIT(s), UNIT_AFTER, (nofail ? UNIT_WANTS : UNIT_REQUIRES),
Harald Hoyer d4ee25
-                                                      SPECIAL_SWAP_TARGET, NULL, true);
Harald Hoyer d4ee25
+                if (nofail)
Harald Hoyer d4ee25
+                        r = unit_add_dependency_by_name_inverse(UNIT(s),
Harald Hoyer d4ee25
+                                UNIT_WANTS, SPECIAL_SWAP_TARGET, NULL, true);
Harald Hoyer d4ee25
+                else
Harald Hoyer d4ee25
+                        r = unit_add_two_dependencies_by_name_inverse(UNIT(s),
Harald Hoyer d4ee25
+                                UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SWAP_TARGET, NULL, true);
Harald Hoyer d4ee25
                 if (r < 0)
Harald Hoyer d4ee25
                         return r;
Harald Hoyer d4ee25
         }
Harald Hoyer d4ee25
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
Harald Hoyer d4ee25
index 6ebe8aa..b73dfa4 100644
Harald Hoyer d4ee25
--- a/src/fstab-generator/fstab-generator.c
Harald Hoyer d4ee25
+++ b/src/fstab-generator/fstab-generator.c
Harald Hoyer d4ee25
@@ -66,6 +66,7 @@ static int mount_find_pri(struct mntent *me, int *ret) {
Harald Hoyer d4ee25
 static int add_swap(const char *what, struct mntent *me) {
Harald Hoyer d4ee25
         _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL, *device = NULL;
Harald Hoyer d4ee25
         _cleanup_fclose_ FILE *f = NULL;
Harald Hoyer d4ee25
+        bool noauto;
Harald Hoyer d4ee25
         int r, pri = -1;
Harald Hoyer d4ee25
 
Harald Hoyer d4ee25
         assert(what);
Harald Hoyer d4ee25
@@ -77,6 +78,8 @@ static int add_swap(const char *what, struct mntent *me) {
Harald Hoyer d4ee25
                 return pri;
Harald Hoyer d4ee25
         }
Harald Hoyer d4ee25
 
Harald Hoyer d4ee25
+        noauto = !!hasmntopt(me, "noauto");
Harald Hoyer d4ee25
+
Harald Hoyer d4ee25
         name = unit_name_from_path(what, ".swap");
Harald Hoyer d4ee25
         if (!name)
Harald Hoyer d4ee25
                 return log_oom();
Harald Hoyer d4ee25
@@ -97,8 +100,7 @@ static int add_swap(const char *what, struct mntent *me) {
Harald Hoyer d4ee25
         fprintf(f,
Harald Hoyer d4ee25
                 "# Automatically generated by systemd-fstab-generator\n\n"
Harald Hoyer d4ee25
                 "[Unit]\n"
Harald Hoyer d4ee25
-                "SourcePath=/etc/fstab\n"
Harald Hoyer d4ee25
-                "\n"
Harald Hoyer d4ee25
+                "SourcePath=/etc/fstab\n\n"
Harald Hoyer d4ee25
                 "[Swap]\n"
Harald Hoyer d4ee25
                 "What=%s\n",
Harald Hoyer d4ee25
                 what);
Harald Hoyer d4ee25
@@ -114,6 +116,18 @@ static int add_swap(const char *what, struct mntent *me) {
Harald Hoyer d4ee25
                 return -errno;
Harald Hoyer d4ee25
         }
Harald Hoyer d4ee25
 
Harald Hoyer d4ee25
+        if (!noauto) {
Harald Hoyer d4ee25
+                lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
Harald Hoyer d4ee25
+                if (!lnk)
Harald Hoyer d4ee25
+                        return log_oom();
Harald Hoyer d4ee25
+
Harald Hoyer d4ee25
+                mkdir_parents_label(lnk, 0755);
Harald Hoyer d4ee25
+                if (symlink(unit, lnk) < 0) {
Harald Hoyer d4ee25
+                        log_error("Failed to create symlink %s: %m", lnk);
Harald Hoyer d4ee25
+                        return -errno;
Harald Hoyer d4ee25
+                }
Harald Hoyer d4ee25
+        }
Harald Hoyer d4ee25
+
Harald Hoyer d4ee25
         return 0;
Harald Hoyer d4ee25
 }
Harald Hoyer d4ee25