]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mount-tool: never bind to device on explicit x-systemd.device-bound=no 36584/head
authorMike Yuan <me@yhndnzj.com>
Sat, 15 Feb 2025 17:42:31 +0000 (18:42 +0100)
committerMike Yuan <me@yhndnzj.com>
Mon, 3 Mar 2025 09:45:57 +0000 (10:45 +0100)
man/systemd-mount.xml
src/mount/mount-tool.c

index 9723af64d665cd8b080d5e6fe6d07fab87f6cbb2..effcf82dd6b18e55ab52adf2d6f933d8f68b15c1 100644 (file)
       <varlistentry>
         <term><option>--bind-device</option></term>
 
-        <listitem><para>This option only has an effect in automount mode,
-        and controls whether the automount unit shall be bound to the backing device's lifetime. If set, the
-        automount unit will be stopped automatically when the backing device vanishes. By default, the automount unit
-        stays around, and subsequent accesses will block until backing device is replugged. This option has no effect
-        in case of non-device mounts, such as network or virtual file system mounts.</para>
+        <listitem><para>Controls whether the generated mount/automount unit shall be bound to the backing device's
+        lifetime. This setting is especially useful in automount mode. If set, the units will be stopped automatically
+        when the backing device vanishes. By default, the automount unit stays around, and subsequent accesses
+        will block until backing device is replugged. This option has no effect in case of non-device mounts,
+        such as network or virtual file system mounts.</para>
 
-        <para>Note that if <option>--discover</option> is used (or only a single argument passed, which implies
-        <option>--discover</option>, see above), and the file system block device is detected to be removable, this
-        option is implied.</para>
+        <para>If <option>--discover</option> is used (or only a single argument passed, which implies
+        <option>--discover</option>, see above), and the file system block device is detected to be removable,
+        this option is implied.</para>
+
+        <para>Note that mount units by default gain a <varname>Requires=</varname> dependency on
+        the backing device. This behavior can be controlled via <option>x-systemd.device-bound=</option>
+        mount option, see <citerefentry><refentrytitle>systemd.mount</refentrytitle><manvolnum>5</manvolnum></citerefentry>
+        for details. In particular, <option>x-systemd.device-bound=no</option> takes precedence over this option,
+        which suppresses device dependencies both in the generated mount units and what's implied by service manager.
+        </para>
 
         <xi:include href="version-info.xml" xpointer="v232"/></listitem>
       </varlistentry>
index 40056eeeb0bbb318e641d9bb376e8512bfd8dc80..bda1e3a3beb491e8f3b70092b05bc415a3174894 100644 (file)
@@ -502,6 +502,24 @@ static int parse_argv(int argc, char *argv[]) {
                 if (arg_discover && arg_transport != BUS_TRANSPORT_LOCAL)
                         return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                                                "Automatic mount location discovery is only supported locally.");
+
+                _cleanup_free_ char *dev_bound = NULL;
+                r = fstab_filter_options(arg_mount_options, "x-systemd.device-bound\0",
+                                         /* ret_namefound = */ NULL, &dev_bound, /* ret_values = */ NULL, /* ret_filtered = */ NULL);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse mount options for x-systemd.device-bound=: %m");
+                if (r > 0 && !isempty(dev_bound)) {
+                        /* If x-systemd.device-bound=no is explicitly specified, never bind automount unit
+                         * to device either. */
+                        r = parse_boolean(dev_bound);
+                        if (r < 0)
+                                return log_error_errno(r, "Invalid x-systemd.device-bound= option: %s", dev_bound);
+                        if (r == 0) {
+                                log_full(arg_bind_device > 0 ? LOG_NOTICE : LOG_DEBUG,
+                                         "x-systemd.device-bound=no set, automatically disabling --bind-device.");
+                                arg_bind_device = false;
+                        }
+                }
         }
 
         return 1;