From 374baa6f64cafe7f1455d68da4fe7069ffbfe6c8 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Tue, 7 May 2019 20:18:08 +0100 Subject: fstrim: add --quiet option to suppress error messages 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 Reference: https://bugs.archlinux.org/task/62288 Addresses: https://github.com/karelzak/util-linux/issues/789 Signed-off-by: Sami Kerola --- sys-utils/fstrim.8 | 10 +++++++++- sys-utils/fstrim.c | 25 +++++++++++++++++++++---- sys-utils/fstrim.service.in | 2 +- 3 files changed, 31 insertions(+), 6 deletions(-) (limited to 'sys-utils') diff --git a/sys-utils/fstrim.8 b/sys-utils/fstrim.8 index c6ecca706..7e04d46be 100644 --- a/sys-utils/fstrim.8 +++ b/sys-utils/fstrim.8 @@ -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 diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c index 87a9031b7..9491079c3 100644 --- a/sys-utils/fstrim.c +++ b/sys-utils/fstrim.c @@ -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 the number of bytes to discard\n"), out); fputs(_(" -m, --minimum 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': diff --git a/sys-utils/fstrim.service.in b/sys-utils/fstrim.service.in index d58accd7f..516023ed8 100644 --- a/sys-utils/fstrim.service.in +++ b/sys-utils/fstrim.service.in @@ -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 -- cgit v1.2.3-55-g7522