]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mount: add --exclusive command line option
authorKarel Zak <kzak@redhat.com>
Thu, 31 Jul 2025 10:31:36 +0000 (12:31 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 31 Jul 2025 10:44:57 +0000 (12:44 +0200)
The new option ensures that the kernel does not reuse existing
superblock.

The new option is available to non-root users as it does not affect
the superblock itself or any other aspects of the mount process. It
makes the current mounting more restrictive, so it makes sense to
allow it for non-root users.

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/mount.8.adoc
sys-utils/mount.c

index e038181f654ebee8f607deb8b260740fba760972..c873d1e4d6a28984092c420b04bc905261f97ae6 100644 (file)
@@ -325,6 +325,18 @@ Do not canonicalize any paths or tags during the mount process. The *mount* comm
 +
 Note that *mount* does not pass this option to the **/sbin/mount.**__type__ helpers.
 
+*--exclusive*::
+Ensures that the filesystem is mounted as a unique instance and that the
+filesystem superblock is not reused by the kernel. The filesystem may be reused
+later if mounted without the option. The option affects only the current mount
+and is allowed for non-root users as well.
++
+See also the **--onlyonce** option. The difference between *--onlyonce* and
+*--exclusive* is that "onlyonce" ensures the same source is not mounted on the
+same mount point; this means mounting twice on /A is not allowed, but mounting
+on /A and /B is allowed. The "exclusive" mount ensures the filesystem itself is
+not reused, regardless of the mount point.
+
 *-F*, *--fork*::
 (Used in conjunction with *-a*.) Fork off a new incarnation of *mount* for each device. This will do the mounts on different devices or different NFS servers in parallel. This has the advantage that it is faster; also NFS timeouts proceed in parallel. A disadvantage is that the order of the mount operations is undefined. Thus, you cannot use this option if you want to mount both _/usr_ and _/usr/spool_.
 
@@ -392,7 +404,10 @@ Also, options on the command line override options from fstab.
 For more details, see the *FILESYSTEM-INDEPENDENT MOUNT OPTIONS* and *FILESYSTEM-SPECIFIC MOUNT OPTIONS* sections.
 
 *--onlyonce*::
-Forces *mount* command to check if the filesystem is already mounted. This behavior is the default for *--all*; otherwise, it depends on the kernel filesystem driver. Some filesystems may be mounted more than once on the same mount point (e.g. tmpfs).
+Forces *mount* command to check if the filesystem is already mounted on
+specified mountpoint. This behavior is the default for *--all*; otherwise, it
+depends on the kernel filesystem driver. Some filesystems may be mounted more
+than once on the same mount point (e.g. tmpfs). See also *--exclusive*.
 
 *--options-mode* _mode_::
 Controls how to combine options from _fstab_/_mtab_ with options from the command line. _mode_ can be one of *ignore*, *append*, *prepend* or *replace*. For example, *append* means that options from _fstab_ are appended to options from the command line. The default value is *prepend* -- it means command line options are evaluated after _fstab_ options. Note that the last option wins if there are conflicting ones.
index b4b8fb63a53e2ea3035b2c6584a792a75dd45e71..858da6579cfdd11a8753d4562108b21ee69e0c3e 100644 (file)
@@ -559,6 +559,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -a, --all               mount all filesystems mentioned in fstab\n"), out);
        fputs(_(" -c, --no-canonicalize   don't canonicalize paths\n"), out);
+       fputs(_("     --exclusive         allow only one filesystem instance"), out);
        fputs(_(" -f, --fake              dry run; skip the mount(2) syscall\n"), out);
        fputs(_(" -F, --fork              fork off for each device (use with -a)\n"), out);
        fputs(_(" -T, --fstab <path>      alternative file to /etc/fstab\n"), out);
@@ -578,7 +579,7 @@ static void __attribute__((__noreturn__)) usage(void)
                "                         mount options source\n"), out);
        fputs(_("     --options-source-force\n"
                "                         force use of options from fstab/mtab\n"), out);
-       fputs(_("     --onlyonce          check if filesystem is already mounted\n"), out);
+       fputs(_("     --onlyonce          check if filesystem is already mounted on target\n"), out);
        fputs(_(" -o, --options <list>    comma-separated list of mount options\n"), out);
        fputs(_(" -O, --test-opts <list>  limit the set of filesystems (use with -a)\n"), out);
        fputs(_(" -r, --read-only         mount the filesystem read-only (same as -o ro)\n"), out);
@@ -708,11 +709,13 @@ int main(int argc, char **argv)
                MOUNT_OPT_OPTMODE,
                MOUNT_OPT_OPTSRC,
                MOUNT_OPT_OPTSRC_FORCE,
-               MOUNT_OPT_ONLYONCE
+               MOUNT_OPT_ONLYONCE,
+               MOUNT_OPT_EXCL
        };
 
        static const struct option longopts[] = {
                { "all",              no_argument,       NULL, 'a'                   },
+               { "exclusive",        no_argument,       NULL, MOUNT_OPT_EXCL        },
                { "fake",             no_argument,       NULL, 'f'                   },
                { "fstab",            required_argument, NULL, 'T'                   },
                { "fork",             no_argument,       NULL, 'F'                   },
@@ -786,7 +789,8 @@ int main(int argc, char **argv)
                if (mnt_context_is_restricted(cxt) &&
                    !strchr("hlLUVvrist", c) &&
                    c != MOUNT_OPT_TARGET &&
-                   c != MOUNT_OPT_SOURCE)
+                   c != MOUNT_OPT_SOURCE &&
+                   c != MOUNT_OPT_EXCL)
                        suid_drop(cxt);
 
                err_exclusive_options(c, longopts, excl, excl_st);
@@ -975,6 +979,10 @@ int main(int argc, char **argv)
                case MOUNT_OPT_ONLYONCE:
                        mnt_context_enable_onlyonce(cxt, 1);
                        break;
+               case MOUNT_OPT_EXCL:
+                       mnt_context_enable_exclusive(cxt, 1);
+                       break;
+
                case 'h':
                        mnt_free_context(cxt);
                        usage();