]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fstrim: add --quiet option to suppress error messages
authorSami Kerola <kerolasa@iki.fi>
Tue, 7 May 2019 19:18:08 +0000 (20:18 +0100)
committerSami Kerola <kerolasa@iki.fi>
Tue, 7 May 2019 19:18:08 +0000 (20:18 +0100)
When fstrim interacts with NTFS it result can be error reporting bad file
descriptor.  That seems to be a bug in NTFS.  While waiting driver to get on
top of the issue and be commonly available lets add to fstrim option to make
it be more silent about errno 9 aka EBADF, Bad file descriptor.

Reported-by: https://github.com/moviuro
Proposed-by: Dave Reisner <dreisner@archlinux.org>
Reference: https://bugs.archlinux.org/task/62288
Addresses: https://github.com/karelzak/util-linux/issues/789
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/fstrim.8
sys-utils/fstrim.c
sys-utils/fstrim.service.in

index c6ecca706488a83c41b26f3e1209c782560c43cf..7e04d46be0a5b154c081b1ed3620f77a4c08bd24 100644 (file)
@@ -1,4 +1,4 @@
-.TH FSTRIM 8 "July 2014" "util-linux" "System Administration"
+.TH FSTRIM 8 "May 2019" "util-linux" "System Administration"
 .SH NAME
 fstrim \- discard unused blocks on a mounted filesystem
 .SH SYNOPSIS
@@ -100,6 +100,14 @@ LVM setup, etc.  These reductions would not be reflected in fstrim_range.len
 .B --length
 option).
 .TP
+.B \-\-quiet
+Suppress error messages.  This option is meant to be used in systemd service
+file to hide warnings that are result of known problems, such as NTFS driver
+reporting
+.I Bad file descriptor
+when device is mounted read-only, or lack of file system support for ioctl
+FITRIM call.
+.TP
 .BR \-V , " \-\-version"
 Display version information and exit.
 .TP
index 87a9031b7f146cba9cf02f914594a645728b4668..9491079c38e784380d28751560b695bd48c46638 100644 (file)
@@ -60,6 +60,7 @@ struct fstrim_control {
        struct fstrim_range range;
 
        unsigned int verbose : 1,
+                    quiet   : 1,
                     fstab   : 1,
                     dryrun : 1;
 };
@@ -108,9 +109,18 @@ static int fstrim_filesystem(struct fstrim_control *ctl, const char *path, const
 
        errno = 0;
        if (ioctl(fd, FITRIM, &range)) {
-               rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -errno;
-
-               if (rc != 1)
+               switch (errno) {
+               case EBADF:
+               case ENOTTY:
+               case EOPNOTSUPP:
+                       if (ctl->quiet) {
+                               rc = 1;
+                               break;
+                       }
+               default:
+                       rc = -errno;
+               }
+               if (rc < 0)
                        warn(_("%s: FITRIM ioctl failed"), path);
                goto done;
        }
@@ -349,6 +359,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -l, --length <num>  the number of bytes to discard\n"), out);
        fputs(_(" -m, --minimum <num> the minimum extent length to discard\n"), out);
        fputs(_(" -v, --verbose       print number of discarded bytes\n"), out);
+       fputs(_("     --quiet         suppress error messages\n"), out);
        fputs(_(" -n, --dry-run       does everything, but trim\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
@@ -364,6 +375,9 @@ int main(int argc, char **argv)
        struct fstrim_control ctl = {
                        .range = { .len = ULLONG_MAX }
        };
+       enum {
+               OPT_QUIET = CHAR_MAX + 1
+       };
 
        static const struct option longopts[] = {
            { "all",       no_argument,       NULL, 'a' },
@@ -374,6 +388,7 @@ int main(int argc, char **argv)
            { "length",    required_argument, NULL, 'l' },
            { "minimum",   required_argument, NULL, 'm' },
            { "verbose",   no_argument,       NULL, 'v' },
+           { "quiet",     no_argument,       NULL, OPT_QUIET },
            { "dry-run",   no_argument,       NULL, 'n' },
            { NULL, 0, NULL, 0 }
        };
@@ -409,7 +424,9 @@ int main(int argc, char **argv)
                case 'v':
                        ctl.verbose = 1;
                        break;
-
+               case OPT_QUIET:
+                       ctl.quiet = 1;
+                       break;
                case 'h':
                        usage();
                case 'V':
index d58accd7f4a67b3f33b2700392ab58fac9ed77a1..516023ed836448f77de845f6720afe9b59f7e188 100644 (file)
@@ -4,7 +4,7 @@ Documentation=man:fstrim(8)
 
 [Service]
 Type=oneshot
-ExecStart=@sbindir@/fstrim --fstab --verbose
+ExecStart=@sbindir@/fstrim --fstab --verbose --quiet
 ProtectSystem=strict
 ProtectHome=yes
 PrivateDevices=no