diff --git a/man/systemd.mount.xml b/man/systemd.mount.xml
index 035069437b..107c07aca9 100644
--- a/man/systemd.mount.xml
+++ b/man/systemd.mount.xml
@@ -284,6 +284,25 @@
+
+
+
+ In the created mount unit, configures an
+ OnFailure= dependency. The argument is a unit name, see
+ above. This option may be specified more than
+ once. See OnFailure= in
+ systemd.unit5
+ for details.
+
+ Note that this option always applies to the created mount
+ unit only, regardless of whether
+ has been specified.
+
+ Note that this option is silently ignored on swap entries.
+
+
+
+
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
index b723868f51..82bbcbcc4d 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -341,6 +341,7 @@ static int write_dependency(
const char *where,
const char *opts,
const char *filter,
+ const char *suffix,
const char* const *unit_settings) {
_cleanup_strv_free_ char **unit_names = NULL;
@@ -360,7 +361,9 @@ static int write_dependency(
STRV_FOREACH(s, unit_names) {
_cleanup_free_ char *mangled = NULL;
- r = unit_name_mangle_with_suffix(*s, "as dependency", 0, ".mount", &mangled);
+ r = suffix ?
+ unit_name_mangle_with_suffix(*s, "as dependency", 0, suffix, &mangled) :
+ unit_name_mangle(*s, 0, &mangled);
if (r < 0)
return log_error_errno(r, "Failed to generate dependency unit name for '%s': %m", where);
@@ -376,22 +379,24 @@ static int write_dependency(
static int write_after(FILE *f, const char *where, const char *opts) {
return write_dependency(f, where, opts,
- "x-systemd.after\0", STRV_MAKE_CONST("After"));
+ "x-systemd.after\0", /* suffix= */ ".mount", STRV_MAKE_CONST("After"));
}
static int write_requires_after(FILE *f, const char *where, const char *opts) {
return write_dependency(f, where, opts,
- "x-systemd.requires\0", STRV_MAKE_CONST("Requires", "After"));
+ "x-systemd.requires\0", /* suffix= */ ".mount",
+ STRV_MAKE_CONST("Requires", "After"));
}
static int write_wants_after(FILE *f, const char *where, const char *opts) {
return write_dependency(f, where, opts,
- "x-systemd.wants\0", STRV_MAKE_CONST("Wants", "After"));
+ "x-systemd.wants\0", /* suffix= */ ".mount",
+ STRV_MAKE_CONST("Wants", "After"));
}
static int write_before(FILE *f, const char *where, const char *opts) {
return write_dependency(f, where, opts,
- "x-systemd.before\0", STRV_MAKE_CONST("Before"));
+ "x-systemd.before\0", /* suffix= */ ".mount", STRV_MAKE_CONST("Before"));
}
static int write_mounts_for(
@@ -426,6 +431,11 @@ static int write_mounts_for(
return 0;
}
+static int write_on_failure(FILE *f, const char *where, const char *opts) {
+ return write_dependency(f, where, opts,
+ "x-systemd.onfailure\0", /* suffix= */ NULL, STRV_MAKE_CONST("OnFailure"));
+}
+
static int write_extra_dependencies(FILE *f, const char *where, const char *opts) {
int r;
@@ -460,6 +470,10 @@ static int write_extra_dependencies(FILE *f, const char *where, const char *opts
if (r < 0)
return r;
+ r = write_on_failure(f, where, opts);
+ if (r < 0)
+ return r;
+
return 0;
}
diff --git a/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/local-fs.target.requires/mnt-onfailure.mount b/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/local-fs.target.requires/mnt-onfailure.mount
new file mode 120000
index 0000000000..3802a7f058
--- /dev/null
+++ b/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/local-fs.target.requires/mnt-onfailure.mount
@@ -0,0 +1 @@
+../mnt-onfailure.mount
\ No newline at end of file
diff --git a/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/local-fs.target.requires/mnt-onfailuremulti.mount b/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/local-fs.target.requires/mnt-onfailuremulti.mount
new file mode 120000
index 0000000000..7d31474405
--- /dev/null
+++ b/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/local-fs.target.requires/mnt-onfailuremulti.mount
@@ -0,0 +1 @@
+../mnt-onfailuremulti.mount
\ No newline at end of file
diff --git a/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/mnt-onfailure.mount b/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/mnt-onfailure.mount
new file mode 100644
index 0000000000..65aaf7c45b
--- /dev/null
+++ b/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/mnt-onfailure.mount
@@ -0,0 +1,13 @@
+# Automatically generated by systemd-fstab-generator
+
+[Unit]
+Documentation=man:fstab(5) man:systemd-fstab-generator(8)
+SourcePath=/etc/fstab
+OnFailure=foo.service
+Before=local-fs.target
+After=blockdev@dev-sdx18.target
+
+[Mount]
+What=/dev/sdx18
+Where=/mnt/onfailure
+Options=x-systemd.onfailure=foo.service
diff --git a/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/mnt-onfailuremulti.mount b/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/mnt-onfailuremulti.mount
new file mode 100644
index 0000000000..387cb02f46
--- /dev/null
+++ b/test/test-fstab-generator/test-18-options.fstab.expected.sysroot/mnt-onfailuremulti.mount
@@ -0,0 +1,13 @@
+# Automatically generated by systemd-fstab-generator
+
+[Unit]
+Documentation=man:fstab(5) man:systemd-fstab-generator(8)
+SourcePath=/etc/fstab
+OnFailure=foo.service bar.service
+Before=local-fs.target
+After=blockdev@dev-sdx19.target
+
+[Mount]
+What=/dev/sdx19
+Where=/mnt/onfailuremulti
+Options=x-systemd.onfailure=foo.service,x-systemd.onfailure=bar.service
diff --git a/test/test-fstab-generator/test-18-options.fstab.expected/local-fs.target.requires/mnt-onfailure.mount b/test/test-fstab-generator/test-18-options.fstab.expected/local-fs.target.requires/mnt-onfailure.mount
new file mode 120000
index 0000000000..3802a7f058
--- /dev/null
+++ b/test/test-fstab-generator/test-18-options.fstab.expected/local-fs.target.requires/mnt-onfailure.mount
@@ -0,0 +1 @@
+../mnt-onfailure.mount
\ No newline at end of file
diff --git a/test/test-fstab-generator/test-18-options.fstab.expected/local-fs.target.requires/mnt-onfailuremulti.mount b/test/test-fstab-generator/test-18-options.fstab.expected/local-fs.target.requires/mnt-onfailuremulti.mount
new file mode 120000
index 0000000000..7d31474405
--- /dev/null
+++ b/test/test-fstab-generator/test-18-options.fstab.expected/local-fs.target.requires/mnt-onfailuremulti.mount
@@ -0,0 +1 @@
+../mnt-onfailuremulti.mount
\ No newline at end of file
diff --git a/test/test-fstab-generator/test-18-options.fstab.expected/mnt-onfailure.mount b/test/test-fstab-generator/test-18-options.fstab.expected/mnt-onfailure.mount
new file mode 100644
index 0000000000..65aaf7c45b
--- /dev/null
+++ b/test/test-fstab-generator/test-18-options.fstab.expected/mnt-onfailure.mount
@@ -0,0 +1,13 @@
+# Automatically generated by systemd-fstab-generator
+
+[Unit]
+Documentation=man:fstab(5) man:systemd-fstab-generator(8)
+SourcePath=/etc/fstab
+OnFailure=foo.service
+Before=local-fs.target
+After=blockdev@dev-sdx18.target
+
+[Mount]
+What=/dev/sdx18
+Where=/mnt/onfailure
+Options=x-systemd.onfailure=foo.service
diff --git a/test/test-fstab-generator/test-18-options.fstab.expected/mnt-onfailuremulti.mount b/test/test-fstab-generator/test-18-options.fstab.expected/mnt-onfailuremulti.mount
new file mode 100644
index 0000000000..387cb02f46
--- /dev/null
+++ b/test/test-fstab-generator/test-18-options.fstab.expected/mnt-onfailuremulti.mount
@@ -0,0 +1,13 @@
+# Automatically generated by systemd-fstab-generator
+
+[Unit]
+Documentation=man:fstab(5) man:systemd-fstab-generator(8)
+SourcePath=/etc/fstab
+OnFailure=foo.service bar.service
+Before=local-fs.target
+After=blockdev@dev-sdx19.target
+
+[Mount]
+What=/dev/sdx19
+Where=/mnt/onfailuremulti
+Options=x-systemd.onfailure=foo.service,x-systemd.onfailure=bar.service
diff --git a/test/test-fstab-generator/test-18-options.fstab.input b/test/test-fstab-generator/test-18-options.fstab.input
index 2112d68828..d5b48f3c0e 100644
--- a/test/test-fstab-generator/test-18-options.fstab.input
+++ b/test/test-fstab-generator/test-18-options.fstab.input
@@ -15,3 +15,5 @@
/dev/sdx15 /mnt/noauto auto noauto 0 0
/dev/sdx16 /mnt/nofail auto nofail 0 0
/dev/sdx17 /mnt/wantedby-automount auto x-systemd.wanted-by=foo.service,x-systemd.automount 0 0
+/dev/sdx18 /mnt/onfailure auto x-systemd.onfailure=foo.service 0 0
+/dev/sdx19 /mnt/onfailuremulti auto x-systemd.onfailure=foo.service,x-systemd.onfailure=bar.service 0 0