]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fstrim: don't trigger autofs
authorKarel Zak <kzak@redhat.com>
Mon, 4 Oct 2021 09:14:01 +0000 (11:14 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 4 Oct 2021 09:14:01 +0000 (11:14 +0200)
- ignore read-only entries
- ignore autofs entries (for example from /proc/self/mountinfo)
- ignore autofs mountpoints where automounter has not been triggered yet

Fixes: https://github.com/karelzak/util-linux/issues/1463
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/fstrim.8.adoc
sys-utils/fstrim.c

index 8dc9495a0880bc04ffe7ecedf3af28844554fd29..d8e4e6ad9f8290125ed6fe2e92ccee6d571e9a0e 100644 (file)
@@ -29,7 +29,7 @@ Running *fstrim* frequently, or even using *mount -o discard*, might negatively
 The _offset_, _length_, and _minimum-size_ arguments may be followed by the multiplicative suffixes KiB (=1024), MiB (=1024*1024), and so on for GiB, TiB, PiB, EiB, ZiB and YiB (the "iB" is optional, e.g., "K" has the same meaning as "KiB") or the suffixes KB (=1000), MB (=1000*1000), and so on for GB, TB, PB, EB, ZB and YB.
 
 *-A, --fstab*::
-Trim all mounted filesystems mentioned in _/etc/fstab_ on devices that support the discard operation. The root filesystem is determined from kernel command line if missing in the file. The other supplied options, like *--offset*, *--length* and *--minimum*, are applied to all these devices. Errors from filesystems that do not support the discard operation, read-only devices and read-only filesystems are silently ignored.
+Trim all mounted filesystems mentioned in _/etc/fstab_ on devices that support the discard operation. The root filesystem is determined from kernel command line if missing in the file. The other supplied options, like *--offset*, *--length* and *--minimum*, are applied to all these devices. Errors from filesystems that do not support the discard operation, read-only devices, autofs and read-only filesystems are silently ignored.
 
 *-a, --all*::
 Trim all mounted filesystems on devices that support the discard operation. The other supplied options, like *--offset*, *--length* and *--minimum*, are applied to all these devices. Errors from filesystems that do not support the discard operation, read-only devices and read-only filesystems are silently ignored.
index d2aec4f715147d46c54970bb86a330686fd57aa1..ea787f42cf0e8bb44d9ec74dd2db8c808da4e400 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <sys/vfs.h>
 #include <linux/fs.h>
 
 #include "nls.h"
@@ -45,6 +46,7 @@
 #include "pathnames.h"
 #include "sysfs.h"
 #include "optutils.h"
+#include "statfs_magic.h"
 
 #include <libmount.h>
 
@@ -207,6 +209,30 @@ fail:
        return 1;
 }
 
+static int is_unwanted_fs(struct libmnt_fs *fs, const char *tgt)
+{
+       struct statfs vfs;
+       int fd, rc;
+
+       if (mnt_fs_is_pseudofs(fs))
+               return 1;
+       if (mnt_fs_is_netfs(fs))
+               return 1;
+       if (mnt_fs_is_swaparea(fs))
+               return 1;
+       if (mnt_fs_match_fstype(fs, "autofs"))
+               return 1;
+       if (mnt_fs_match_options(fs, "ro"))
+               return 1;
+
+       fd = open(tgt, O_PATH);
+       if (!fd)
+               return 1;
+       rc = fstatfs(fd, &vfs) != 0 || vfs.f_type == STATFS_AUTOFS_MAGIC;
+       close(fd);
+
+       return rc;
+}
 
 static int uniq_fs_target_cmp(
                struct libmnt_table *tb __attribute__((__unused__)),
@@ -292,7 +318,7 @@ static int fstrim_all_from_file(struct fstrim_control *ctl, const char *filename
                const char *src = mnt_fs_get_srcpath(fs),
                           *tgt = mnt_fs_get_target(fs);
 
-               if (!tgt || mnt_fs_is_pseudofs(fs) || mnt_fs_is_netfs(fs)) {
+               if (!tgt || is_unwanted_fs(fs, tgt)) {
                        mnt_table_remove_fs(tab, fs);
                        continue;
                }