fstab-generator: disable default deps if x-systemd.{wanted,required}-by= is used

Fixes #30273
This commit is contained in:
Mike Yuan 2023-12-01 19:32:45 +08:00
parent d3a7602d83
commit 39a4c452d2
No known key found for this signature in database
GPG Key ID: 417471C0A40F58B3
13 changed files with 136 additions and 60 deletions

View File

@ -134,15 +134,17 @@
<listitem><para>Mount units referring to local file systems automatically gain
an <varname>After=</varname> dependency on <filename>local-fs-pre.target</filename>, and a
<varname>Before=</varname> dependency on <filename>local-fs.target</filename> unless
<option>nofail</option> mount option is set.</para></listitem>
<varname>Before=</varname> dependency on <filename>local-fs.target</filename> unless one or more
mount options among <option>nofail</option>, <option>x-systemd.wanted-by=</option>,
and <option>x-systemd.required-by=</option> is set. See below for detailed information.
</para></listitem>
<listitem><para>Network mount units
automatically acquire <varname>After=</varname> dependencies on <filename>remote-fs-pre.target</filename>,
<filename>network.target</filename> and <filename>network-online.target</filename>, and gain a
<varname>Before=</varname> dependency on <filename>remote-fs.target</filename> unless
<option>nofail</option> mount option is set. Towards the latter a
<varname>Wants=</varname> unit is added as well.</para></listitem>
<listitem><para>Network mount units automatically acquire <varname>After=</varname> dependencies on
<filename>remote-fs-pre.target</filename>, <filename>network.target</filename>,
plus <varname>After=</varname> and <varname>Wants=</varname> dependencies on <filename>network-online.target</filename>,
and a <varname>Before=</varname> dependency on <filename>remote-fs.target</filename>, unless
one or more mount options among <option>nofail</option>, <option>x-systemd.wanted-by=</option>,
and <option>x-systemd.required-by=</option> is set.</para></listitem>
</itemizedlist>
<para>Mount units referring to local and network file systems are distinguished by their file system type
@ -240,13 +242,13 @@
<term><option>x-systemd.wanted-by=</option></term>
<term><option>x-systemd.required-by=</option></term>
<listitem><para>In the created mount unit, configures a
<varname>WantedBy=</varname> or <varname>RequiredBy=</varname>
dependency on another unit. This option may be
specified more than once. If this is specified, the normal
automatic dependencies on the created mount unit, e.g.,
<filename>local-fs.target</filename>, are not automatically
created. See <varname>WantedBy=</varname> and <varname>RequiredBy=</varname> in
<listitem><para>In the created mount unit, configures a <varname>WantedBy=</varname> or
<varname>RequiredBy=</varname> dependency on another unit. This option may be specified more than once.
If this is specified, the default dependencies (see above) other than <filename>umount.target</filename>
on the created mount unit, e.g. <filename>local-fs.target</filename>, are not automatically created.
Hence it is likely that some ordering dependencies need to be set up manually through
<option>x-systemd.before=</option> and <option>x-systemd.after=</option>. See <varname>WantedBy=</varname>
and <varname>RequiredBy=</varname> in
<citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>
for details.</para>

View File

@ -599,13 +599,25 @@ static int add_mount(
SET_FLAG(flags, MOUNT_NOFAIL, true);
}
if (!strv_isempty(wanted_by) || !strv_isempty(required_by)) {
/* If x-systemd.{wanted,required}-by= is specified, target_unit is not used */
target_unit = NULL;
/* Don't set default ordering dependencies on local-fs.target or remote-fs.target, but we
* still need to conflict with umount.target. */
fputs("DefaultDependencies=no\n"
"Conflicts=umount.target\n"
"Before=umount.target\n",
f);
}
r = write_extra_dependencies(f, opts);
if (r < 0)
return r;
/* Order the mount unit we generate relative to target_unit, so that DefaultDependencies= on the
* target unit won't affect us. */
if (!FLAGS_SET(flags, MOUNT_NOFAIL))
if (target_unit && !FLAGS_SET(flags, MOUNT_NOFAIL))
fprintf(f, "Before=%s\n", target_unit);
if (passno != 0) {
@ -696,26 +708,7 @@ static int add_mount(
}
}
if (!FLAGS_SET(flags, MOUNT_AUTOMOUNT)) {
if (!FLAGS_SET(flags, MOUNT_NOAUTO) && strv_isempty(wanted_by) && strv_isempty(required_by)) {
r = generator_add_symlink(dest, target_unit,
(flags & MOUNT_NOFAIL) ? "wants" : "requires", name);
if (r < 0)
return r;
} else {
STRV_FOREACH(s, wanted_by) {
r = generator_add_symlink(dest, *s, "wants", name);
if (r < 0)
return r;
}
STRV_FOREACH(s, required_by) {
r = generator_add_symlink(dest, *s, "requires", name);
if (r < 0)
return r;
}
}
} else {
if (FLAGS_SET(flags, MOUNT_AUTOMOUNT)) {
r = unit_name_from_path(where, ".automount", &automount_name);
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
@ -745,12 +738,38 @@ static int add_mount(
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
}
if (target_unit) {
assert(strv_isempty(wanted_by));
assert(strv_isempty(required_by));
/* noauto has no effect if x-systemd.automount is used */
if (!FLAGS_SET(flags, MOUNT_NOAUTO) || automount_name) {
r = generator_add_symlink(dest, target_unit,
(flags & MOUNT_NOFAIL) ? "wants" : "requires", automount_name);
FLAGS_SET(flags, MOUNT_NOFAIL) ? "wants" : "requires",
automount_name ?: name);
if (r < 0)
return r;
}
} else {
const char *unit_name = automount_name ?: name;
STRV_FOREACH(s, wanted_by) {
r = generator_add_symlink(dest, *s, "wants", unit_name);
if (r < 0)
return r;
}
STRV_FOREACH(s, required_by) {
r = generator_add_symlink(dest, *s, "requires", unit_name);
if (r < 0)
return r;
}
if ((flags & (MOUNT_NOAUTO|MOUNT_NOFAIL)) != 0)
log_warning("x-systemd.wanted-by= and/or x-systemd.required-by= specified, 'noauto' and 'nofail' have no effect.");
}
return true;
}

View File

@ -0,0 +1 @@
../mnt-wantedby-automount.automount

View File

@ -3,7 +3,9 @@
[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
SourcePath=/etc/fstab
Before=local-fs.target
DefaultDependencies=no
Conflicts=umount.target
Before=umount.target
After=blockdev@dev-sdx8.target
[Mount]

View File

@ -0,0 +1,8 @@
# Automatically generated by systemd-fstab-generator
[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
[Automount]
Where=/mnt/wantedby-automount

View File

@ -0,0 +1,14 @@
# Automatically generated by systemd-fstab-generator
[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
SourcePath=/etc/fstab
DefaultDependencies=no
Conflicts=umount.target
Before=umount.target
After=blockdev@dev-sdx17.target
[Mount]
What=/dev/sdx17
Where=/mnt/wantedby
Options=x-systemd.wanted-by=foo.service,x-systemd.automount

View File

@ -3,7 +3,9 @@
[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
SourcePath=/etc/fstab
Before=local-fs.target
DefaultDependencies=no
Conflicts=umount.target
Before=umount.target
After=blockdev@dev-sdx7.target
[Mount]

View File

@ -0,0 +1 @@
../mnt-wantedby-automount.automount

View File

@ -3,7 +3,9 @@
[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
SourcePath=/etc/fstab
Before=local-fs.target
DefaultDependencies=no
Conflicts=umount.target
Before=umount.target
After=blockdev@dev-sdx8.target
[Mount]

View File

@ -0,0 +1,8 @@
# Automatically generated by systemd-fstab-generator
[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
[Automount]
Where=/mnt/wantedby-automount

View File

@ -0,0 +1,14 @@
# Automatically generated by systemd-fstab-generator
[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
SourcePath=/etc/fstab
DefaultDependencies=no
Conflicts=umount.target
Before=umount.target
After=blockdev@dev-sdx17.target
[Mount]
What=/dev/sdx17
Where=/mnt/wantedby
Options=x-systemd.wanted-by=foo.service,x-systemd.automount

View File

@ -3,7 +3,9 @@
[Unit]
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
SourcePath=/etc/fstab
Before=local-fs.target
DefaultDependencies=no
Conflicts=umount.target
Before=umount.target
After=blockdev@dev-sdx7.target
[Mount]

View File

@ -14,3 +14,4 @@
/dev/sdx14 /mnt/pcrfs auto x-systemd.pcrfs 0 0
/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