From d5fd9ac6b208c5acdb6c0023757a7e2827474482 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 1 Aug 2018 09:10:07 +0200 Subject: umount: add --quiet option The command umount supports things like --all-targets and --recursive to umount all nodes in specified tree. Sometimes it makes sense to aggressively use wildcards like /dev/sdb* and in this case --quiet seems like a good choice umount --quiet --all-targets /dev/sdb* to suppress 'not mounted' error messages. The new option suppress only these error messages and nothing else. Addresses: https://github.com/karelzak/util-linux/issues/672 Signed-off-by: Karel Zak --- sys-utils/umount.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'sys-utils/umount.c') diff --git a/sys-utils/umount.c b/sys-utils/umount.c index 5478c55b8..14c455908 100644 --- a/sys-utils/umount.c +++ b/sys-utils/umount.c @@ -42,6 +42,8 @@ #define OPTUTILS_EXIT_CODE MNT_EX_USAGE #include "optutils.h" +static int quiet; + static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)), const char *filename, int line) { @@ -100,6 +102,7 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -r, --read-only in case unmounting fails, try to remount read-only\n"), out); fputs(_(" -t, --types limit the set of filesystem types\n"), out); fputs(_(" -v, --verbose say what is being done\n"), out); + fputs(_(" -q, --quiet suppress 'not mounted' error messages\n"), out); fputs(_(" -N, --namespace perform umount in another namespace\n"), out); fputs(USAGE_SEPARATOR, out); @@ -153,6 +156,15 @@ static int mk_exit_code(struct libmnt_context *cxt, int rc) char buf[BUFSIZ] = { 0 }; rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf)); + + /* suppress "not mounted" error message */ + if (quiet && + rc == MNT_EX_FAIL && + mnt_context_syscall_called(cxt) && + mnt_context_get_syscall_errno(cxt) == EINVAL) + return rc; + + /* print errors/warnings */ if (*buf) { const char *spec = mnt_context_get_target(cxt); if (!spec) @@ -315,7 +327,8 @@ static int umount_recursive(struct libmnt_context *cxt, const char *spec) rc = umount_do_recurse(cxt, tb, fs); else { rc = MNT_EX_USAGE; - warnx(access(spec, F_OK) == 0 ? + if (!quiet) + warnx(access(spec, F_OK) == 0 ? _("%s: not mounted") : _("%s: not found"), spec); } @@ -338,7 +351,8 @@ static int umount_alltargets(struct libmnt_context *cxt, const char *spec, int r rc = mnt_context_find_umount_fs(cxt, spec, &fs); if (rc == 1) { rc = MNT_EX_USAGE; - warnx(access(spec, F_OK) == 0 ? + if (!quiet) + warnx(access(spec, F_OK) == 0 ? _("%s: not mounted") : _("%s: not found"), spec); return rc; @@ -441,6 +455,7 @@ int main(int argc, char **argv) { "lazy", no_argument, NULL, 'l' }, { "no-canonicalize", no_argument, NULL, 'c' }, { "no-mtab", no_argument, NULL, 'n' }, + { "quiet", no_argument, NULL, 'q' }, { "read-only", no_argument, NULL, 'r' }, { "recursive", no_argument, NULL, 'R' }, { "test-opts", required_argument, NULL, 'O' }, @@ -478,7 +493,7 @@ int main(int argc, char **argv) /* only few options are allowed for non-root users */ - if (mnt_context_is_restricted(cxt) && !strchr("hdilVv", c)) + if (mnt_context_is_restricted(cxt) && !strchr("hdilqVv", c)) exit_non_root(option_to_longopt(c, longopts)); err_exclusive_options(c, longopts, excl, excl_st); @@ -514,6 +529,9 @@ int main(int argc, char **argv) case 'n': mnt_context_disable_mtab(cxt, TRUE); break; + case 'q': + quiet = 1; + break; case 'r': mnt_context_enable_rdonly_umount(cxt, TRUE); break; -- cgit v1.2.3-55-g7522