]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fstrim: trim also root FS on --fstab
authorKarel Zak <kzak@redhat.com>
Mon, 10 Dec 2018 10:45:03 +0000 (11:45 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 10 Dec 2018 10:45:03 +0000 (11:45 +0100)
The root fs ("/") is optional in many case in /etc/fstab. This patch
forces fstrim to read kernel command line for the root= option to get
root FS if the entry is missing in your fstab.

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

index ff572a44b2fe1f9c0dd5789457846d4ad43caeb6..f7e7aa9ef6cb57ab21bd5765b47effd1e7cf4656 100644 (file)
@@ -48,7 +48,8 @@ KB (=1000), MB (=1000*1000), and so on for GB, TB, PB, EB, ZB and YB.
 
 .IP "\fB\-A, \-\-fstab\fP"
 Trim all mounted filesystems mentioned in \fI/etc/fstab\fR on devices that support the
-discard operation.
+discard operation.  The root filesystem is determined from kernel command line if missing
+in the file.
 The other supplied options, like \fB\-\-offset\fR, \fB\-\-length\fR and
 \fB-\-minimum\fR, are applied to all these devices.
 Errors from filesystems that do not support the discard operation are silently
index b1823f5c5f927df7942f771b42ea8277291871de..00652cbb196854f02722281213d4f2e1950ecc46 100644 (file)
@@ -242,9 +242,26 @@ static int fstrim_all(struct fstrim_control *ctl)
        mnt_table_uniq_fs(tab, MNT_UNIQ_FORWARD, uniq_fs_source_cmp);
 
        if (ctl->fstab) {
+               char *rootdev = NULL;
+
                cache = mnt_new_cache();
                if (!cache)
                        err(MNT_EX_FAIL, _("failed to initialize libmount cache"));
+
+               /* Make sure we trim also root FS on --fstab */
+               if (mnt_table_find_target(tab, "/", MNT_ITER_FORWARD) == NULL &&
+                   mnt_guess_system_root(0, cache, &rootdev) == 0) {
+
+                       fs = mnt_new_fs();
+                       if (!fs)
+                               err(MNT_EX_FAIL, _("failed to allocate FS handler"));
+                       mnt_fs_set_target(fs, "/");
+                       mnt_fs_set_source(fs, rootdev);
+                       mnt_fs_set_fstype(fs, "auto");
+                       mnt_table_add_fs(tab, fs);
+                       mnt_unref_fs(fs);
+                       fs = NULL;
+               }
        }
 
        while (mnt_table_next_fs(tab, itr, &fs) == 0) {