* 'fstrim-bug-789' of https://github.com/kerolasa/util-linux:
  fstrim: add --quiet option to suppress error messages
This commit is contained in:
Karel Zak 2019-05-09 13:15:10 +02:00
commit ddd9ffb152
3 changed files with 31 additions and 6 deletions

View 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

View 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':

View 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