2 * mount(8) -- mount a filesystem
4 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
5 * Written by Karel Zak <kzak@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/types.h>
39 #include "closestream.h"
40 #include "canonicalize.h"
42 #define XALLOC_EXIT_CODE MNT_EX_SYSERR
45 #define OPTUTILS_EXIT_CODE MNT_EX_USAGE
50 * --options-mode={ignore,append,prepend,replace} MNT_OMODE_{IGNORE, ...}
51 * --options-source={fstab,mtab,disable} MNT_OMODE_{FSTAB,MTAB,NOTAB}
52 * --options-source-force MNT_OMODE_FORCE
55 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
);
57 static void __attribute__((__noreturn__
)) exit_non_root(const char *option
)
59 const uid_t ruid
= getuid();
60 const uid_t euid
= geteuid();
62 if (ruid
== 0 && euid
!= 0) {
63 /* user is root, but setuid to non-root */
65 errx(MNT_EX_USAGE
, _("only root can use \"--%s\" option "
66 "(effective UID is %u)"),
68 errx(MNT_EX_USAGE
, _("only root can do that "
69 "(effective UID is %u)"), euid
);
72 errx(MNT_EX_USAGE
, _("only root can use \"--%s\" option"), option
);
73 errx(MNT_EX_USAGE
, _("only root can do that"));
76 static void __attribute__((__noreturn__
)) print_version(void)
78 const char *ver
= NULL
;
79 const char **features
= NULL
, **p
;
81 mnt_get_library_version(&ver
);
82 mnt_get_library_features(&features
);
84 printf(_("%s from %s (libmount %s"),
85 program_invocation_short_name
,
90 fputs(p
== features
? ": " : ", ", stdout
);
97 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
98 const char *filename
, int line
)
101 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
106 * Replace control chars with '?' to be compatible with coreutils. For more
107 * robust solution use findmnt(1) where we use \x?? hex encoding.
109 static void safe_fputs(const char *data
)
113 for (p
= data
; p
&& *p
; p
++) {
114 if (iscntrl((unsigned char) *p
))
121 static void print_all(struct libmnt_context
*cxt
, char *pattern
, int show_label
)
123 struct libmnt_table
*tb
;
124 struct libmnt_iter
*itr
= NULL
;
125 struct libmnt_fs
*fs
;
126 struct libmnt_cache
*cache
= NULL
;
128 if (mnt_context_get_mtab(cxt
, &tb
))
129 err(MNT_EX_SYSERR
, _("failed to read mtab"));
131 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
133 err(MNT_EX_SYSERR
, _("failed to initialize libmount iterator"));
135 cache
= mnt_new_cache();
137 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
138 const char *type
= mnt_fs_get_fstype(fs
);
139 const char *src
= mnt_fs_get_source(fs
);
140 const char *optstr
= mnt_fs_get_options(fs
);
143 if (type
&& pattern
&& !mnt_match_fstype(type
, pattern
))
146 if (!mnt_fs_is_pseudofs(fs
) && !mnt_fs_is_netfs(fs
))
147 xsrc
= mnt_pretty_path(src
, cache
);
148 printf ("%s on ", xsrc
? xsrc
: src
);
149 safe_fputs(mnt_fs_get_target(fs
));
152 printf (" type %s", type
);
154 printf (" (%s)", optstr
);
155 if (show_label
&& src
) {
156 char *lb
= mnt_cache_find_tag_value(cache
, src
, "LABEL");
158 printf (" [%s]", lb
);
164 mnt_unref_cache(cache
);
171 static int mount_all(struct libmnt_context
*cxt
)
173 struct libmnt_iter
*itr
;
174 struct libmnt_fs
*fs
;
175 int mntrc
, ignored
, rc
= MNT_EX_SUCCESS
;
177 int nsucc
= 0, nerrs
= 0;
179 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
181 warn(_("failed to initialize libmount iterator"));
182 return MNT_EX_SYSERR
;
185 while (mnt_context_next_mount(cxt
, itr
, &fs
, &mntrc
, &ignored
) == 0) {
187 const char *tgt
= mnt_fs_get_target(fs
);
190 if (mnt_context_is_verbose(cxt
))
191 printf(ignored
== 1 ? _("%-25s: ignored\n") :
192 _("%-25s: already mounted\n"),
194 } else if (mnt_context_is_fork(cxt
)) {
195 if (mnt_context_is_verbose(cxt
))
196 printf("%-25s: mount successfully forked\n", tgt
);
198 if (mk_exit_code(cxt
, mntrc
) == MNT_EX_SUCCESS
) {
201 /* Note that MNT_EX_SUCCESS return code does
202 * not mean that FS has been really mounted
203 * (e.g. nofail option) */
204 if (mnt_context_get_status(cxt
)
205 && mnt_context_is_verbose(cxt
))
206 printf("%-25s: successfully mounted\n", tgt
);
212 if (mnt_context_is_parent(cxt
)) {
213 /* wait for mount --fork children */
216 nerrs
= 0, nsucc
= 0;
218 rc
= mnt_context_wait_for_children(cxt
, &nchildren
, &nerrs
);
219 if (!rc
&& nchildren
)
220 nsucc
= nchildren
- nerrs
;
224 rc
= MNT_EX_SUCCESS
; /* all success */
226 rc
= MNT_EX_FAIL
; /* all failed */
228 rc
= MNT_EX_SOMEOK
; /* some success, some failed */
234 static void success_message(struct libmnt_context
*cxt
)
236 unsigned long mflags
= 0;
237 const char *tgt
, *src
, *pr
= program_invocation_short_name
;
239 if (mnt_context_helper_executed(cxt
)
240 || mnt_context_get_status(cxt
) != 1)
243 mnt_context_get_mflags(cxt
, &mflags
);
244 tgt
= mnt_context_get_target(cxt
);
245 src
= mnt_context_get_source(cxt
);
247 if (mflags
& MS_MOVE
)
248 printf(_("%s: %s moved to %s.\n"), pr
, src
, tgt
);
249 else if (mflags
& MS_BIND
)
250 printf(_("%s: %s bound on %s.\n"), pr
, src
, tgt
);
251 else if (mflags
& MS_PROPAGATION
) {
252 if (src
&& strcmp(src
, "none") != 0 && tgt
)
253 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
255 printf(_("%s: %s propagation flags changed.\n"), pr
, tgt
);
257 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
260 #if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
261 #include <selinux/selinux.h>
262 #include <selinux/context.h>
264 static void selinux_warning(struct libmnt_context
*cxt
, const char *tgt
)
267 if (tgt
&& mnt_context_is_verbose(cxt
) && is_selinux_enabled() > 0) {
268 security_context_t raw
= NULL
, def
= NULL
;
270 if (getfilecon(tgt
, &raw
) > 0
271 && security_get_initial_context("file", &def
) == 0) {
273 if (!selinux_file_context_cmp(raw
, def
))
275 "mount: %s does not contain SELinux labels.\n"
276 " You just mounted an file system that supports labels which does not\n"
277 " contain labels, onto an SELinux box. It is likely that confined\n"
278 " applications will generate AVC messages and not be allowed access to\n"
279 " this file system. For more details see restorecon(8) and mount(8).\n"),
287 # define selinux_warning(_x, _y)
291 * Returns exit status (MNT_EX_*) and/or prints error message.
293 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
)
296 char buf
[BUFSIZ
] = { 0 };
298 rc
= mnt_context_get_excode(cxt
, rc
, buf
, sizeof(buf
));
299 tgt
= mnt_context_get_target(cxt
);
302 const char *spec
= tgt
;
304 spec
= mnt_context_get_source(cxt
);
307 warnx("%s: %s.", spec
, buf
);
310 if (rc
== MNT_EX_SUCCESS
&& mnt_context_get_status(cxt
) == 1) {
311 selinux_warning(cxt
, tgt
);
316 static struct libmnt_table
*append_fstab(struct libmnt_context
*cxt
,
317 struct libmnt_table
*fstab
,
322 fstab
= mnt_new_table();
324 err(MNT_EX_SYSERR
, _("failed to initialize libmount table"));
326 mnt_table_set_parser_errcb(fstab
, table_parser_errcb
);
327 mnt_context_set_fstab(cxt
, fstab
);
329 mnt_unref_table(fstab
); /* reference is handled by @cxt now */
332 if (mnt_table_parse_fstab(fstab
, path
))
333 errx(MNT_EX_USAGE
,_("%s: failed to parse"), path
);
339 * Check source and target paths -- non-root user should not be able to
340 * resolve paths which are unreadable for him.
342 static void sanitize_paths(struct libmnt_context
*cxt
)
345 struct libmnt_fs
*fs
= mnt_context_get_fs(cxt
);
350 p
= mnt_fs_get_target(fs
);
352 char *np
= canonicalize_path_restricted(p
);
354 err(MNT_EX_USAGE
, "%s", p
);
355 mnt_fs_set_target(fs
, np
);
359 p
= mnt_fs_get_srcpath(fs
);
361 char *np
= canonicalize_path_restricted(p
);
363 err(MNT_EX_USAGE
, "%s", p
);
364 mnt_fs_set_source(fs
, np
);
369 static void append_option(struct libmnt_context
*cxt
, const char *opt
)
371 if (opt
&& (*opt
== '=' || *opt
== '\'' || *opt
== '\"' || isblank(*opt
)))
372 errx(MNT_EX_USAGE
, _("unsupported option format: %s"), opt
);
373 if (mnt_context_append_options(cxt
, opt
))
374 err(MNT_EX_SYSERR
, _("failed to append option '%s'"), opt
);
377 static int has_remount_flag(struct libmnt_context
*cxt
)
379 unsigned long mflags
= 0;
381 if (mnt_context_get_mflags(cxt
, &mflags
))
384 return mflags
& MS_REMOUNT
;
387 static void __attribute__((__noreturn__
)) usage(void)
390 fputs(USAGE_HEADER
, out
);
393 " %1$s -a [options]\n"
394 " %1$s [options] [--source] <source> | [--target] <directory>\n"
395 " %1$s [options] <source> <directory>\n"
396 " %1$s <operation> <mountpoint> [<target>]\n"),
397 program_invocation_short_name
);
399 fputs(USAGE_SEPARATOR
, out
);
400 fputs(_("Mount a filesystem.\n"), out
);
402 fputs(USAGE_OPTIONS
, out
);
404 " -a, --all mount all filesystems mentioned in fstab\n"
405 " -c, --no-canonicalize don't canonicalize paths\n"
406 " -f, --fake dry run; skip the mount(2) syscall\n"
407 " -F, --fork fork off for each device (use with -a)\n"
408 " -T, --fstab <path> alternative file to /etc/fstab\n"));
410 " -i, --internal-only don't call the mount.<type> helpers\n"));
412 " -l, --show-labels show also filesystem labels\n"));
414 " -n, --no-mtab don't write to /etc/mtab\n"));
416 " --options-mode <mode>\n"
417 " what to do with options loaded from fstab\n"
418 " --options-source <source>\n"
419 " mount options source\n"
420 " --options-source-force\n"
421 " force use of options from fstab/mtab\n"));
423 " -o, --options <list> comma-separated list of mount options\n"
424 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
425 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
426 " -t, --types <list> limit the set of filesystem types\n"));
428 " --source <src> explicitly specifies source (path, label, uuid)\n"
429 " --target <target> explicitly specifies mountpoint\n"));
431 " -v, --verbose say what is being done\n"));
433 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
435 " -N, --namespace <ns> perform mount in another namespace\n"));
437 fputs(USAGE_SEPARATOR
, out
);
438 printf(USAGE_HELP_OPTIONS(25));
442 " -L, --label <label> synonym for LABEL=<label>\n"
443 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
444 " LABEL=<label> specifies device by filesystem label\n"
445 " UUID=<uuid> specifies device by filesystem UUID\n"
446 " PARTLABEL=<label> specifies device by partition label\n"
447 " PARTUUID=<uuid> specifies device by partition UUID\n"));
450 " <device> specifies device by path\n"
451 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
452 " <file> regular file for loopdev setup\n"));
456 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
457 " -M, --move move a subtree to some other place\n"
458 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
460 " --make-shared mark a subtree as shared\n"
461 " --make-slave mark a subtree as slave\n"
462 " --make-private mark a subtree as private\n"
463 " --make-unbindable mark a subtree as unbindable\n"));
465 " --make-rshared recursively mark a whole subtree as shared\n"
466 " --make-rslave recursively mark a whole subtree as slave\n"
467 " --make-rprivate recursively mark a whole subtree as private\n"
468 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
470 printf(USAGE_MAN_TAIL("mount(8)"));
472 exit(MNT_EX_SUCCESS
);
480 static int omode2mask(const char *str
)
484 static const struct flag_str flags
[] = {
485 { MNT_OMODE_IGNORE
, "ignore" },
486 { MNT_OMODE_APPEND
, "append" },
487 { MNT_OMODE_PREPEND
, "prepend" },
488 { MNT_OMODE_REPLACE
, "replace" },
491 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++) {
492 if (!strcmp(str
, flags
[i
].str
))
493 return flags
[i
].value
;
498 static long osrc2mask(const char *str
, size_t len
)
502 static const struct flag_str flags
[] = {
503 { MNT_OMODE_FSTAB
, "fstab" },
504 { MNT_OMODE_MTAB
, "mtab" },
505 { MNT_OMODE_NOTAB
, "disable" },
508 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++) {
509 if (!strncmp(str
, flags
[i
].str
, len
) && !flags
[i
].str
[len
])
510 return flags
[i
].value
;
515 static pid_t
parse_pid(const char *str
)
521 ret
= strtoul(str
, &end
, 10);
523 if (ret
< 0 || errno
|| end
== str
|| (end
&& *end
))
528 int main(int argc
, char **argv
)
530 int c
, rc
= MNT_EX_SUCCESS
, all
= 0, show_labels
= 0;
531 struct libmnt_context
*cxt
;
532 struct libmnt_table
*fstab
= NULL
;
535 int oper
= 0, is_move
= 0;
537 int optmode
= 0, optmode_mode
= 0, optmode_src
= 0;
540 MOUNT_OPT_SHARED
= CHAR_MAX
+ 1,
543 MOUNT_OPT_UNBINDABLE
,
547 MOUNT_OPT_RUNBINDABLE
,
552 MOUNT_OPT_OPTSRC_FORCE
555 static const struct option longopts
[] = {
556 { "all", no_argument
, NULL
, 'a' },
557 { "fake", no_argument
, NULL
, 'f' },
558 { "fstab", required_argument
, NULL
, 'T' },
559 { "fork", no_argument
, NULL
, 'F' },
560 { "help", no_argument
, NULL
, 'h' },
561 { "no-mtab", no_argument
, NULL
, 'n' },
562 { "read-only", no_argument
, NULL
, 'r' },
563 { "ro", no_argument
, NULL
, 'r' },
564 { "verbose", no_argument
, NULL
, 'v' },
565 { "version", no_argument
, NULL
, 'V' },
566 { "read-write", no_argument
, NULL
, 'w' },
567 { "rw", no_argument
, NULL
, 'w' },
568 { "options", required_argument
, NULL
, 'o' },
569 { "test-opts", required_argument
, NULL
, 'O' },
570 { "types", required_argument
, NULL
, 't' },
571 { "uuid", required_argument
, NULL
, 'U' },
572 { "label", required_argument
, NULL
, 'L' },
573 { "bind", no_argument
, NULL
, 'B' },
574 { "move", no_argument
, NULL
, 'M' },
575 { "rbind", no_argument
, NULL
, 'R' },
576 { "make-shared", no_argument
, NULL
, MOUNT_OPT_SHARED
},
577 { "make-slave", no_argument
, NULL
, MOUNT_OPT_SLAVE
},
578 { "make-private", no_argument
, NULL
, MOUNT_OPT_PRIVATE
},
579 { "make-unbindable", no_argument
, NULL
, MOUNT_OPT_UNBINDABLE
},
580 { "make-rshared", no_argument
, NULL
, MOUNT_OPT_RSHARED
},
581 { "make-rslave", no_argument
, NULL
, MOUNT_OPT_RSLAVE
},
582 { "make-rprivate", no_argument
, NULL
, MOUNT_OPT_RPRIVATE
},
583 { "make-runbindable", no_argument
, NULL
, MOUNT_OPT_RUNBINDABLE
},
584 { "no-canonicalize", no_argument
, NULL
, 'c' },
585 { "internal-only", no_argument
, NULL
, 'i' },
586 { "show-labels", no_argument
, NULL
, 'l' },
587 { "target", required_argument
, NULL
, MOUNT_OPT_TARGET
},
588 { "source", required_argument
, NULL
, MOUNT_OPT_SOURCE
},
589 { "options-mode", required_argument
, NULL
, MOUNT_OPT_OPTMODE
},
590 { "options-source", required_argument
, NULL
, MOUNT_OPT_OPTSRC
},
591 { "options-source-force", no_argument
, NULL
, MOUNT_OPT_OPTSRC_FORCE
},
592 { "namespace", required_argument
, NULL
, 'N' },
596 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
597 { 'B','M','R' }, /* bind,move,rbind */
598 { 'L','U', MOUNT_OPT_SOURCE
}, /* label,uuid,source */
601 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
604 setlocale(LC_ALL
, "");
605 bindtextdomain(PACKAGE
, LOCALEDIR
);
607 atexit(close_stdout
);
609 strutils_set_exitcode(MNT_EX_USAGE
);
612 cxt
= mnt_new_context();
614 err(MNT_EX_SYSERR
, _("libmount context allocation failed"));
616 mnt_context_set_tables_errcb(cxt
, table_parser_errcb
);
618 while ((c
= getopt_long(argc
, argv
, "aBcfFhilL:Mno:O:rRsU:vVwt:T:N:",
619 longopts
, NULL
)) != -1) {
621 /* only few options are allowed for non-root users */
622 if (mnt_context_is_restricted(cxt
) &&
623 !strchr("hlLUVvrist", c
) &&
624 c
!= MOUNT_OPT_TARGET
&&
625 c
!= MOUNT_OPT_SOURCE
)
626 exit_non_root(option_to_longopt(c
, longopts
));
628 err_exclusive_options(c
, longopts
, excl
, excl_st
);
635 mnt_context_disable_canonicalize(cxt
, TRUE
);
638 mnt_context_enable_fake(cxt
, TRUE
);
641 mnt_context_enable_fork(cxt
, TRUE
);
647 mnt_context_disable_helpers(cxt
, TRUE
);
650 mnt_context_disable_mtab(cxt
, TRUE
);
653 append_option(cxt
, "ro");
654 mnt_context_enable_rwonly_mount(cxt
, FALSE
);
657 mnt_context_enable_verbose(cxt
, TRUE
);
663 append_option(cxt
, "rw");
664 mnt_context_enable_rwonly_mount(cxt
, TRUE
);
667 append_option(cxt
, optarg
);
670 if (mnt_context_set_options_pattern(cxt
, optarg
))
671 err(MNT_EX_SYSERR
, _("failed to set options pattern"));
674 xasprintf(&srcbuf
, "LABEL=\"%s\"", optarg
);
675 mnt_context_disable_swapmatch(cxt
, 1);
676 mnt_context_set_source(cxt
, srcbuf
);
680 xasprintf(&srcbuf
, "UUID=\"%s\"", optarg
);
681 mnt_context_disable_swapmatch(cxt
, 1);
682 mnt_context_set_source(cxt
, srcbuf
);
692 fstab
= append_fstab(cxt
, fstab
, optarg
);
695 mnt_context_enable_sloppy(cxt
, TRUE
);
699 append_option(cxt
, "bind");
707 append_option(cxt
, "rbind");
712 pid_t pid
= parse_pid(optarg
);
715 snprintf(path
, sizeof(path
), "/proc/%i/ns/mnt", pid
);
717 if (mnt_context_set_target_ns(cxt
, pid
? path
: optarg
))
718 err(MNT_EX_SYSERR
, _("failed to set target namespace to %s"), pid
? path
: optarg
);
721 case MOUNT_OPT_SHARED
:
722 append_option(cxt
, "shared");
725 case MOUNT_OPT_SLAVE
:
726 append_option(cxt
, "slave");
729 case MOUNT_OPT_PRIVATE
:
730 append_option(cxt
, "private");
733 case MOUNT_OPT_UNBINDABLE
:
734 append_option(cxt
, "unbindable");
737 case MOUNT_OPT_RSHARED
:
738 append_option(cxt
, "rshared");
741 case MOUNT_OPT_RSLAVE
:
742 append_option(cxt
, "rslave");
745 case MOUNT_OPT_RPRIVATE
:
746 append_option(cxt
, "rprivate");
749 case MOUNT_OPT_RUNBINDABLE
:
750 append_option(cxt
, "runbindable");
753 case MOUNT_OPT_TARGET
:
754 mnt_context_disable_swapmatch(cxt
, 1);
755 mnt_context_set_target(cxt
, optarg
);
757 case MOUNT_OPT_SOURCE
:
758 mnt_context_disable_swapmatch(cxt
, 1);
759 mnt_context_set_source(cxt
, optarg
);
761 case MOUNT_OPT_OPTMODE
:
762 optmode_mode
= omode2mask(optarg
);
763 if (optmode_mode
== -EINVAL
) {
764 warnx(_("bad usage"));
765 errtryhelp(MNT_EX_USAGE
);
768 case MOUNT_OPT_OPTSRC
:
770 unsigned long tmp
= 0;
771 if (string_to_bitmask(optarg
, &tmp
, osrc2mask
)) {
772 warnx(_("bad usage"));
773 errtryhelp(MNT_EX_USAGE
);
778 case MOUNT_OPT_OPTSRC_FORCE
:
779 optmode
|= MNT_OMODE_FORCE
;
782 errtryhelp(MNT_EX_USAGE
);
789 optmode
|= optmode_mode
| optmode_src
;
792 optmode
|= MNT_OMODE_PREPEND
;
794 optmode
|= MNT_OMODE_FSTAB
| MNT_OMODE_MTAB
;
795 mnt_context_set_optsmode(cxt
, optmode
);
798 if (fstab
&& !mnt_context_is_nocanonicalize(cxt
)) {
800 * We have external (context independent) fstab instance, let's
801 * make a connection between the fstab and the canonicalization
804 mnt_table_set_cache(fstab
, mnt_context_get_cache(cxt
));
807 if (!mnt_context_get_source(cxt
) &&
808 !mnt_context_get_target(cxt
) &&
811 if (oper
|| mnt_context_get_options(cxt
)) {
812 warnx(_("bad usage"));
813 errtryhelp(MNT_EX_USAGE
);
815 print_all(cxt
, types
, show_labels
);
819 /* Non-root users are allowed to use -t to print_all(),
821 if (mnt_context_is_restricted(cxt
) && types
)
822 exit_non_root("types");
824 if (oper
&& (types
|| all
|| mnt_context_get_source(cxt
))) {
825 warnx(_("bad usage"));
826 errtryhelp(MNT_EX_USAGE
);
829 if (types
&& (all
|| strchr(types
, ',') ||
830 strncmp(types
, "no", 2) == 0))
831 mnt_context_set_fstype_pattern(cxt
, types
);
833 mnt_context_set_fstype(cxt
, types
);
842 } else if (argc
== 0 && (mnt_context_get_source(cxt
) ||
843 mnt_context_get_target(cxt
))) {
845 * B) mount -L|-U|--source|--target
847 * non-root may specify source *or* target, but not both
849 if (mnt_context_is_restricted(cxt
) &&
850 mnt_context_get_source(cxt
) &&
851 mnt_context_get_target(cxt
))
854 } else if (argc
== 1 && (!mnt_context_get_source(cxt
) ||
855 !mnt_context_get_target(cxt
))) {
857 * C) mount [-L|-U|--source] <target>
858 * mount [--target <dir>] <source>
859 * mount <source|target>
861 * non-root may specify source *or* target, but not both
863 * It does not matter for libmount if we set source or target
864 * here (the library is able to swap it), but it matters for
867 int istag
= mnt_tag_is_valid(argv
[0]);
869 if (istag
&& mnt_context_get_source(cxt
))
870 /* -L, -U or --source together with LABEL= or UUID= */
871 errx(MNT_EX_USAGE
, _("source specified more than once"));
872 else if (istag
|| mnt_context_get_target(cxt
))
873 mnt_context_set_source(cxt
, argv
[0]);
875 mnt_context_set_target(cxt
, argv
[0]);
877 if (mnt_context_is_restricted(cxt
) &&
878 mnt_context_get_source(cxt
) &&
879 mnt_context_get_target(cxt
))
882 } else if (argc
== 2 && !mnt_context_get_source(cxt
)
883 && !mnt_context_get_target(cxt
)) {
885 * D) mount <source> <target>
887 if (mnt_context_is_restricted(cxt
))
890 mnt_context_set_source(cxt
, argv
[0]);
891 mnt_context_set_target(cxt
, argv
[1]);
894 warnx(_("bad usage"));
895 errtryhelp(MNT_EX_USAGE
);
898 if (mnt_context_is_restricted(cxt
))
902 /* "move" as option string is not supported by libmount */
903 mnt_context_set_mflags(cxt
, MS_MOVE
);
905 if ((oper
&& !has_remount_flag(cxt
)) || propa
)
906 /* For --make-* or --bind is fstab/mtab unnecessary */
907 mnt_context_set_optsmode(cxt
, MNT_OMODE_NOTAB
);
909 rc
= mnt_context_mount(cxt
);
910 rc
= mk_exit_code(cxt
, rc
);
912 if (rc
== MNT_EX_SUCCESS
&& mnt_context_is_verbose(cxt
))
913 success_message(cxt
);
915 mnt_free_context(cxt
);