[systemd-devel] [PATCH] swap: use swapon -o

Karel Zak kzak at redhat.com
Wed May 20 08:54:20 PDT 2015


This patch simplify swapon usage in systemd. The command swapon(8)
since util-linux v2.26 supports "-o <list>". The idea is exactly the
same like for mount(8). The -o specifies options in fstab-compatible
way. For systemd it means that it does not have to care about things
like "discard" or another swapon specific options.

        swapon -o <options-from-fstab>

For backward compatibility the code cares about "Priority:" swap unit
field (for a case when Priority: is set, but pri= in the Options: is
missing).

References: http://lists.freedesktop.org/archives/systemd-devel/2014-October/023576.html
---
 src/core/swap.c | 41 ++++++++++++++++-------------------------
 1 file changed, 16 insertions(+), 25 deletions(-)

diff --git a/src/core/swap.c b/src/core/swap.c
index 12ebf84..658ac8b 100644
--- a/src/core/swap.c
+++ b/src/core/swap.c
@@ -717,8 +717,8 @@ fail:
 }
 
 static void swap_enter_activating(Swap *s) {
-        _cleanup_free_ char *discard = NULL;
-        int r, priority = -1;
+        _cleanup_free_ char *opts = NULL;
+        int r;
 
         assert(s);
 
@@ -726,13 +726,19 @@ static void swap_enter_activating(Swap *s) {
         s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
 
         if (s->from_fragment) {
-                fstab_filter_options(s->parameters_fragment.options, "discard\0", NULL, &discard, NULL);
+                int priority = -1;
+
+                r = fstab_find_pri(s->parameters_fragment.options, &priority);
+                if (r < 0)
+                        log_notice_errno(r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options);
 
-                priority = s->parameters_fragment.priority;
-                if (priority < 0) {
-                        r = fstab_find_pri(s->parameters_fragment.options, &priority);
+                if (r <= 0 && s->parameters_fragment.priority >= 0) {
+                        if (s->parameters_fragment.options)
+                                r = asprintf(&opts, "%s,pri=%i", s->parameters_fragment.options, s->parameters_fragment.priority);
+                        else
+                                r = asprintf(&opts, "pri=%i", s->parameters_fragment.priority);
                         if (r < 0)
-                                log_notice_errno(r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options);
+                                goto fail;
                 }
         }
 
@@ -740,24 +746,9 @@ static void swap_enter_activating(Swap *s) {
         if (r < 0)
                 goto fail;
 
-        if (priority >= 0) {
-                char p[DECIMAL_STR_MAX(int)];
-
-                sprintf(p, "%i", priority);
-                r = exec_command_append(s->control_command, "-p", p, NULL);
-                if (r < 0)
-                        goto fail;
-        }
-
-        if (discard && !streq(discard, "none")) {
-                const char *discard_arg;
-
-                if (streq(discard, "all"))
-                        discard_arg = "--discard";
-                else
-                        discard_arg = strjoina("--discard=", discard);
-
-                r = exec_command_append(s->control_command, discard_arg, NULL);
+        if (s->parameters_fragment.options || opts) {
+                r = exec_command_append(s->control_command, "-o",
+                                opts ? : s->parameters_fragment.options, NULL);
                 if (r < 0)
                         goto fail;
         }
-- 
2.1.0



More information about the systemd-devel mailing list