swap: use swapon -o
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
This commit is contained in:
parent
49f582c0a0
commit
bf1d7ba70a
2
README
2
README
|
@ -136,7 +136,7 @@ REQUIREMENTS:
|
||||||
During runtime, you need the following additional
|
During runtime, you need the following additional
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|
||||||
util-linux >= v2.25 required
|
util-linux >= v2.26 required
|
||||||
dbus >= 1.4.0 (strictly speaking optional, but recommended)
|
dbus >= 1.4.0 (strictly speaking optional, but recommended)
|
||||||
dracut (optional)
|
dracut (optional)
|
||||||
PolicyKit (optional)
|
PolicyKit (optional)
|
||||||
|
|
|
@ -177,7 +177,8 @@
|
||||||
|
|
||||||
<listitem><para>Swap priority to use when activating the swap
|
<listitem><para>Swap priority to use when activating the swap
|
||||||
device or file. This takes an integer. This setting is
|
device or file. This takes an integer. This setting is
|
||||||
optional.</para></listitem>
|
optional and ignored when priotiry is set by <option>pri=</option> in the
|
||||||
|
<varname>Options=</varname> option.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
|
|
|
@ -717,8 +717,8 @@ fail:
|
||||||
}
|
}
|
||||||
|
|
||||||
static void swap_enter_activating(Swap *s) {
|
static void swap_enter_activating(Swap *s) {
|
||||||
_cleanup_free_ char *discard = NULL;
|
_cleanup_free_ char *opts = NULL;
|
||||||
int r, priority = -1;
|
int r;
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
|
|
||||||
|
@ -726,13 +726,21 @@ static void swap_enter_activating(Swap *s) {
|
||||||
s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
|
s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
|
||||||
|
|
||||||
if (s->from_fragment) {
|
if (s->from_fragment) {
|
||||||
fstab_filter_options(s->parameters_fragment.options, "discard\0", NULL, &discard, NULL);
|
int priority = -1;
|
||||||
|
|
||||||
priority = s->parameters_fragment.priority;
|
|
||||||
if (priority < 0) {
|
|
||||||
r = fstab_find_pri(s->parameters_fragment.options, &priority);
|
r = fstab_find_pri(s->parameters_fragment.options, &priority);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
log_notice_errno(r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options);
|
log_warning_errno(r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options);
|
||||||
|
else if (r == 1 && s->parameters_fragment.priority >= 0)
|
||||||
|
log_warning("Duplicate swap priority configuration by Priority and Options fields.");
|
||||||
|
|
||||||
|
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)
|
||||||
|
goto fail;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -740,24 +748,9 @@ static void swap_enter_activating(Swap *s) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
if (priority >= 0) {
|
if (s->parameters_fragment.options || opts) {
|
||||||
char p[DECIMAL_STR_MAX(int)];
|
r = exec_command_append(s->control_command, "-o",
|
||||||
|
opts ? : s->parameters_fragment.options, NULL);
|
||||||
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 (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,10 +53,9 @@ static int add_swap(
|
||||||
bool noauto,
|
bool noauto,
|
||||||
bool nofail) {
|
bool nofail) {
|
||||||
|
|
||||||
_cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL, *filtered = NULL;
|
_cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
|
||||||
_cleanup_fclose_ FILE *f = NULL;
|
_cleanup_fclose_ FILE *f = NULL;
|
||||||
int r, pri = -1;
|
int r;
|
||||||
const char *opts;
|
|
||||||
|
|
||||||
assert(what);
|
assert(what);
|
||||||
assert(me);
|
assert(me);
|
||||||
|
@ -71,18 +70,6 @@ static int add_swap(
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
opts = me->mnt_opts;
|
|
||||||
r = fstab_find_pri(opts, &pri);
|
|
||||||
if (r < 0) {
|
|
||||||
log_error_errno(r, "Failed to parse priority, ignoring: %m");
|
|
||||||
|
|
||||||
/* Remove invalid pri field */
|
|
||||||
r = fstab_filter_options(opts, "pri\0", NULL, NULL, &filtered);
|
|
||||||
if (r < 0)
|
|
||||||
return log_error_errno(r, "Failed to parse options: %m");
|
|
||||||
opts = filtered;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = unit_name_from_path(what, ".swap", &name);
|
r = unit_name_from_path(what, ".swap", &name);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_error_errno(r, "Failed to generate unit name: %m");
|
return log_error_errno(r, "Failed to generate unit name: %m");
|
||||||
|
@ -109,20 +96,15 @@ static int add_swap(
|
||||||
"What=%s\n",
|
"What=%s\n",
|
||||||
what);
|
what);
|
||||||
|
|
||||||
/* Note that we currently pass the priority field twice, once
|
if (!isempty(me->mnt_opts) && !streq(me->mnt_opts, "defaults"))
|
||||||
* in Priority=, and once in Options= */
|
fprintf(f, "Options=%s\n", me->mnt_opts);
|
||||||
if (pri >= 0)
|
|
||||||
fprintf(f, "Priority=%i\n", pri);
|
|
||||||
|
|
||||||
if (!isempty(opts) && !streq(opts, "defaults"))
|
|
||||||
fprintf(f, "Options=%s\n", opts);
|
|
||||||
|
|
||||||
r = fflush_and_check(f);
|
r = fflush_and_check(f);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_error_errno(r, "Failed to write unit file %s: %m", unit);
|
return log_error_errno(r, "Failed to write unit file %s: %m", unit);
|
||||||
|
|
||||||
/* use what as where, to have a nicer error message */
|
/* use what as where, to have a nicer error message */
|
||||||
r = generator_write_timeouts(arg_dest, what, what, opts, NULL);
|
r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue