]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
umount: add --quiet option
authorKarel Zak <kzak@redhat.com>
Wed, 1 Aug 2018 07:10:07 +0000 (09:10 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 1 Aug 2018 07:10:07 +0000 (09:10 +0200)
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 <kzak@redhat.com>
sys-utils/umount.8
sys-utils/umount.c

index cad7d2aae1a5b37f1cf87b1a3cd1b5169989264a..136f6179c802dbe920d22750502fd4feb5d18d7e 100644 (file)
@@ -155,6 +155,9 @@ Each option can be prefixed with
 .B no
 to indicate that no action should be taken for this option.
 .TP
+.BR \-q , " \-\-quiet"
+Suppress "not mounted" error messages.
+.TP
 .BR \-R , " \-\-recursive"
 Recursively unmount each specified directory.  Recursion for each directory will
 stop if any unmount operation in the chain fails for any reason.  The relationship
index 5478c55b8775d3ba96c92ab1d5ffd91d62219c49..14c45590831ce62c410f34cddb5e57459467fb39 100644 (file)
@@ -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 <list>      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 <ns>    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;