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__
)) mount_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 */
236 * mount -a -o remount
238 static int remount_all(struct libmnt_context
*cxt
)
240 struct libmnt_iter
*itr
;
241 struct libmnt_fs
*fs
;
242 int mntrc
, ignored
, rc
= MNT_EX_SUCCESS
;
244 int nsucc
= 0, nerrs
= 0;
246 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
248 warn(_("failed to initialize libmount iterator"));
249 return MNT_EX_SYSERR
;
252 while (mnt_context_next_remount(cxt
, itr
, &fs
, &mntrc
, &ignored
) == 0) {
254 const char *tgt
= mnt_fs_get_target(fs
);
257 if (mnt_context_is_verbose(cxt
))
258 printf(_("%-25s: ignored\n"), tgt
);
260 if (mk_exit_code(cxt
, mntrc
) == MNT_EX_SUCCESS
) {
263 /* Note that MNT_EX_SUCCESS return code does
264 * not mean that FS has been really mounted
265 * (e.g. nofail option) */
266 if (mnt_context_get_status(cxt
)
267 && mnt_context_is_verbose(cxt
))
268 printf("%-25s: successfully remounted\n", tgt
);
275 rc
= MNT_EX_SUCCESS
; /* all success */
277 rc
= MNT_EX_FAIL
; /* all failed */
279 rc
= MNT_EX_SOMEOK
; /* some success, some failed */
285 static void success_message(struct libmnt_context
*cxt
)
287 unsigned long mflags
= 0;
288 const char *tgt
, *src
, *pr
= program_invocation_short_name
;
290 if (mnt_context_helper_executed(cxt
)
291 || mnt_context_get_status(cxt
) != 1)
294 mnt_context_get_mflags(cxt
, &mflags
);
295 tgt
= mnt_context_get_target(cxt
);
296 src
= mnt_context_get_source(cxt
);
298 if (mflags
& MS_MOVE
)
299 printf(_("%s: %s moved to %s.\n"), pr
, src
, tgt
);
300 else if (mflags
& MS_BIND
)
301 printf(_("%s: %s bound on %s.\n"), pr
, src
, tgt
);
302 else if (mflags
& MS_PROPAGATION
) {
303 if (src
&& strcmp(src
, "none") != 0 && tgt
)
304 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
306 printf(_("%s: %s propagation flags changed.\n"), pr
, tgt
);
308 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
311 #if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
312 #include <selinux/selinux.h>
313 #include <selinux/context.h>
315 static void selinux_warning(struct libmnt_context
*cxt
, const char *tgt
)
318 if (tgt
&& mnt_context_is_verbose(cxt
) && is_selinux_enabled() > 0) {
319 security_context_t raw
= NULL
, def
= NULL
;
321 if (getfilecon(tgt
, &raw
) > 0
322 && security_get_initial_context("file", &def
) == 0) {
324 if (!selinux_file_context_cmp(raw
, def
))
326 "mount: %s does not contain SELinux labels.\n"
327 " You just mounted an file system that supports labels which does not\n"
328 " contain labels, onto an SELinux box. It is likely that confined\n"
329 " applications will generate AVC messages and not be allowed access to\n"
330 " this file system. For more details see restorecon(8) and mount(8).\n"),
338 # define selinux_warning(_x, _y)
342 * Returns exit status (MNT_EX_*) and/or prints error message.
344 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
)
347 char buf
[BUFSIZ
] = { 0 };
349 rc
= mnt_context_get_excode(cxt
, rc
, buf
, sizeof(buf
));
350 tgt
= mnt_context_get_target(cxt
);
353 const char *spec
= tgt
;
355 spec
= mnt_context_get_source(cxt
);
358 warnx("%s: %s.", spec
, buf
);
361 if (rc
== MNT_EX_SUCCESS
&& mnt_context_get_status(cxt
) == 1) {
362 selinux_warning(cxt
, tgt
);
367 static struct libmnt_table
*append_fstab(struct libmnt_context
*cxt
,
368 struct libmnt_table
*fstab
,
373 fstab
= mnt_new_table();
375 err(MNT_EX_SYSERR
, _("failed to initialize libmount table"));
377 mnt_table_set_parser_errcb(fstab
, table_parser_errcb
);
378 mnt_context_set_fstab(cxt
, fstab
);
380 mnt_unref_table(fstab
); /* reference is handled by @cxt now */
383 if (mnt_table_parse_fstab(fstab
, path
))
384 errx(MNT_EX_USAGE
,_("%s: failed to parse"), path
);
390 * Check source and target paths -- non-root user should not be able to
391 * resolve paths which are unreadable for him.
393 static void sanitize_paths(struct libmnt_context
*cxt
)
396 struct libmnt_fs
*fs
= mnt_context_get_fs(cxt
);
401 p
= mnt_fs_get_target(fs
);
403 char *np
= canonicalize_path_restricted(p
);
405 err(MNT_EX_USAGE
, "%s", p
);
406 mnt_fs_set_target(fs
, np
);
410 p
= mnt_fs_get_srcpath(fs
);
412 char *np
= canonicalize_path_restricted(p
);
414 err(MNT_EX_USAGE
, "%s", p
);
415 mnt_fs_set_source(fs
, np
);
420 static void append_option(struct libmnt_context
*cxt
, const char *opt
)
422 if (opt
&& (*opt
== '=' || *opt
== '\'' || *opt
== '\"' || isblank(*opt
)))
423 errx(MNT_EX_USAGE
, _("unsupported option format: %s"), opt
);
424 if (mnt_context_append_options(cxt
, opt
))
425 err(MNT_EX_SYSERR
, _("failed to append option '%s'"), opt
);
428 static int has_remount_flag(struct libmnt_context
*cxt
)
430 unsigned long mflags
= 0;
432 if (mnt_context_get_mflags(cxt
, &mflags
))
435 return mflags
& MS_REMOUNT
;
438 static void __attribute__((__noreturn__
)) usage(void)
441 fputs(USAGE_HEADER
, out
);
444 " %1$s -a [options]\n"
445 " %1$s [options] [--source] <source> | [--target] <directory>\n"
446 " %1$s [options] <source> <directory>\n"
447 " %1$s <operation> <mountpoint> [<target>]\n"),
448 program_invocation_short_name
);
450 fputs(USAGE_SEPARATOR
, out
);
451 fputs(_("Mount a filesystem.\n"), out
);
453 fputs(USAGE_OPTIONS
, out
);
455 " -a, --all mount all filesystems mentioned in fstab\n"
456 " -c, --no-canonicalize don't canonicalize paths\n"
457 " -f, --fake dry run; skip the mount(2) syscall\n"
458 " -F, --fork fork off for each device (use with -a)\n"
459 " -T, --fstab <path> alternative file to /etc/fstab\n"));
461 " -i, --internal-only don't call the mount.<type> helpers\n"));
463 " -l, --show-labels show also filesystem labels\n"));
465 " -n, --no-mtab don't write to /etc/mtab\n"));
467 " --options-mode <mode>\n"
468 " what to do with options loaded from fstab\n"
469 " --options-source <source>\n"
470 " mount options source\n"
471 " --options-source-force\n"
472 " force use of options from fstab/mtab\n"));
474 " -o, --options <list> comma-separated list of mount options\n"
475 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
476 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
477 " -t, --types <list> limit the set of filesystem types\n"));
479 " --source <src> explicitly specifies source (path, label, uuid)\n"
480 " --target <target> explicitly specifies mountpoint\n"));
482 " -v, --verbose say what is being done\n"));
484 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
486 " -N, --namespace <ns> perform mount in another namespace\n"));
488 fputs(USAGE_SEPARATOR
, out
);
489 printf(USAGE_HELP_OPTIONS(25));
493 " -L, --label <label> synonym for LABEL=<label>\n"
494 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
495 " LABEL=<label> specifies device by filesystem label\n"
496 " UUID=<uuid> specifies device by filesystem UUID\n"
497 " PARTLABEL=<label> specifies device by partition label\n"
498 " PARTUUID=<uuid> specifies device by partition UUID\n"));
501 " <device> specifies device by path\n"
502 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
503 " <file> regular file for loopdev setup\n"));
507 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
508 " -M, --move move a subtree to some other place\n"
509 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
511 " --make-shared mark a subtree as shared\n"
512 " --make-slave mark a subtree as slave\n"
513 " --make-private mark a subtree as private\n"
514 " --make-unbindable mark a subtree as unbindable\n"));
516 " --make-rshared recursively mark a whole subtree as shared\n"
517 " --make-rslave recursively mark a whole subtree as slave\n"
518 " --make-rprivate recursively mark a whole subtree as private\n"
519 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
521 printf(USAGE_MAN_TAIL("mount(8)"));
523 exit(MNT_EX_SUCCESS
);
531 static int omode2mask(const char *str
)
535 static const struct flag_str flags
[] = {
536 { MNT_OMODE_IGNORE
, "ignore" },
537 { MNT_OMODE_APPEND
, "append" },
538 { MNT_OMODE_PREPEND
, "prepend" },
539 { MNT_OMODE_REPLACE
, "replace" },
542 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++) {
543 if (!strcmp(str
, flags
[i
].str
))
544 return flags
[i
].value
;
549 static long osrc2mask(const char *str
, size_t len
)
553 static const struct flag_str flags
[] = {
554 { MNT_OMODE_FSTAB
, "fstab" },
555 { MNT_OMODE_MTAB
, "mtab" },
556 { MNT_OMODE_NOTAB
, "disable" },
559 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++) {
560 if (!strncmp(str
, flags
[i
].str
, len
) && !flags
[i
].str
[len
])
561 return flags
[i
].value
;
566 static pid_t
parse_pid(const char *str
)
572 ret
= strtoul(str
, &end
, 10);
574 if (ret
< 0 || errno
|| end
== str
|| (end
&& *end
))
579 int main(int argc
, char **argv
)
581 int c
, rc
= MNT_EX_SUCCESS
, all
= 0, show_labels
= 0;
582 struct libmnt_context
*cxt
;
583 struct libmnt_table
*fstab
= NULL
;
586 int oper
= 0, is_move
= 0;
588 int optmode
= 0, optmode_mode
= 0, optmode_src
= 0;
591 MOUNT_OPT_SHARED
= CHAR_MAX
+ 1,
594 MOUNT_OPT_UNBINDABLE
,
598 MOUNT_OPT_RUNBINDABLE
,
603 MOUNT_OPT_OPTSRC_FORCE
606 static const struct option longopts
[] = {
607 { "all", no_argument
, NULL
, 'a' },
608 { "fake", no_argument
, NULL
, 'f' },
609 { "fstab", required_argument
, NULL
, 'T' },
610 { "fork", no_argument
, NULL
, 'F' },
611 { "help", no_argument
, NULL
, 'h' },
612 { "no-mtab", no_argument
, NULL
, 'n' },
613 { "read-only", no_argument
, NULL
, 'r' },
614 { "ro", no_argument
, NULL
, 'r' },
615 { "verbose", no_argument
, NULL
, 'v' },
616 { "version", no_argument
, NULL
, 'V' },
617 { "read-write", no_argument
, NULL
, 'w' },
618 { "rw", no_argument
, NULL
, 'w' },
619 { "options", required_argument
, NULL
, 'o' },
620 { "test-opts", required_argument
, NULL
, 'O' },
621 { "types", required_argument
, NULL
, 't' },
622 { "uuid", required_argument
, NULL
, 'U' },
623 { "label", required_argument
, NULL
, 'L' },
624 { "bind", no_argument
, NULL
, 'B' },
625 { "move", no_argument
, NULL
, 'M' },
626 { "rbind", no_argument
, NULL
, 'R' },
627 { "make-shared", no_argument
, NULL
, MOUNT_OPT_SHARED
},
628 { "make-slave", no_argument
, NULL
, MOUNT_OPT_SLAVE
},
629 { "make-private", no_argument
, NULL
, MOUNT_OPT_PRIVATE
},
630 { "make-unbindable", no_argument
, NULL
, MOUNT_OPT_UNBINDABLE
},
631 { "make-rshared", no_argument
, NULL
, MOUNT_OPT_RSHARED
},
632 { "make-rslave", no_argument
, NULL
, MOUNT_OPT_RSLAVE
},
633 { "make-rprivate", no_argument
, NULL
, MOUNT_OPT_RPRIVATE
},
634 { "make-runbindable", no_argument
, NULL
, MOUNT_OPT_RUNBINDABLE
},
635 { "no-canonicalize", no_argument
, NULL
, 'c' },
636 { "internal-only", no_argument
, NULL
, 'i' },
637 { "show-labels", no_argument
, NULL
, 'l' },
638 { "target", required_argument
, NULL
, MOUNT_OPT_TARGET
},
639 { "source", required_argument
, NULL
, MOUNT_OPT_SOURCE
},
640 { "options-mode", required_argument
, NULL
, MOUNT_OPT_OPTMODE
},
641 { "options-source", required_argument
, NULL
, MOUNT_OPT_OPTSRC
},
642 { "options-source-force", no_argument
, NULL
, MOUNT_OPT_OPTSRC_FORCE
},
643 { "namespace", required_argument
, NULL
, 'N' },
647 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
648 { 'B','M','R' }, /* bind,move,rbind */
649 { 'L','U', MOUNT_OPT_SOURCE
}, /* label,uuid,source */
652 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
655 setlocale(LC_ALL
, "");
656 bindtextdomain(PACKAGE
, LOCALEDIR
);
658 close_stdout_atexit();
660 strutils_set_exitcode(MNT_EX_USAGE
);
663 cxt
= mnt_new_context();
665 err(MNT_EX_SYSERR
, _("libmount context allocation failed"));
667 mnt_context_set_tables_errcb(cxt
, table_parser_errcb
);
669 while ((c
= getopt_long(argc
, argv
, "aBcfFhilL:Mno:O:rRsU:vVwt:T:N:",
670 longopts
, NULL
)) != -1) {
672 /* only few options are allowed for non-root users */
673 if (mnt_context_is_restricted(cxt
) &&
674 !strchr("hlLUVvrist", c
) &&
675 c
!= MOUNT_OPT_TARGET
&&
676 c
!= MOUNT_OPT_SOURCE
)
677 exit_non_root(option_to_longopt(c
, longopts
));
679 err_exclusive_options(c
, longopts
, excl
, excl_st
);
686 mnt_context_disable_canonicalize(cxt
, TRUE
);
689 mnt_context_enable_fake(cxt
, TRUE
);
692 mnt_context_enable_fork(cxt
, TRUE
);
695 mnt_context_disable_helpers(cxt
, TRUE
);
698 mnt_context_disable_mtab(cxt
, TRUE
);
701 append_option(cxt
, "ro");
702 mnt_context_enable_rwonly_mount(cxt
, FALSE
);
705 mnt_context_enable_verbose(cxt
, TRUE
);
708 append_option(cxt
, "rw");
709 mnt_context_enable_rwonly_mount(cxt
, TRUE
);
712 append_option(cxt
, optarg
);
715 if (mnt_context_set_options_pattern(cxt
, optarg
))
716 err(MNT_EX_SYSERR
, _("failed to set options pattern"));
719 xasprintf(&srcbuf
, "LABEL=\"%s\"", optarg
);
720 mnt_context_disable_swapmatch(cxt
, 1);
721 mnt_context_set_source(cxt
, srcbuf
);
725 xasprintf(&srcbuf
, "UUID=\"%s\"", optarg
);
726 mnt_context_disable_swapmatch(cxt
, 1);
727 mnt_context_set_source(cxt
, srcbuf
);
737 fstab
= append_fstab(cxt
, fstab
, optarg
);
740 mnt_context_enable_sloppy(cxt
, TRUE
);
744 append_option(cxt
, "bind");
752 append_option(cxt
, "rbind");
757 pid_t pid
= parse_pid(optarg
);
760 snprintf(path
, sizeof(path
), "/proc/%i/ns/mnt", pid
);
762 if (mnt_context_set_target_ns(cxt
, pid
? path
: optarg
))
763 err(MNT_EX_SYSERR
, _("failed to set target namespace to %s"), pid
? path
: optarg
);
766 case MOUNT_OPT_SHARED
:
767 append_option(cxt
, "shared");
770 case MOUNT_OPT_SLAVE
:
771 append_option(cxt
, "slave");
774 case MOUNT_OPT_PRIVATE
:
775 append_option(cxt
, "private");
778 case MOUNT_OPT_UNBINDABLE
:
779 append_option(cxt
, "unbindable");
782 case MOUNT_OPT_RSHARED
:
783 append_option(cxt
, "rshared");
786 case MOUNT_OPT_RSLAVE
:
787 append_option(cxt
, "rslave");
790 case MOUNT_OPT_RPRIVATE
:
791 append_option(cxt
, "rprivate");
794 case MOUNT_OPT_RUNBINDABLE
:
795 append_option(cxt
, "runbindable");
798 case MOUNT_OPT_TARGET
:
799 mnt_context_disable_swapmatch(cxt
, 1);
800 mnt_context_set_target(cxt
, optarg
);
802 case MOUNT_OPT_SOURCE
:
803 mnt_context_disable_swapmatch(cxt
, 1);
804 mnt_context_set_source(cxt
, optarg
);
806 case MOUNT_OPT_OPTMODE
:
807 optmode_mode
= omode2mask(optarg
);
808 if (optmode_mode
== -EINVAL
) {
809 warnx(_("bad usage"));
810 errtryhelp(MNT_EX_USAGE
);
813 case MOUNT_OPT_OPTSRC
:
815 unsigned long tmp
= 0;
816 if (string_to_bitmask(optarg
, &tmp
, osrc2mask
)) {
817 warnx(_("bad usage"));
818 errtryhelp(MNT_EX_USAGE
);
823 case MOUNT_OPT_OPTSRC_FORCE
:
824 optmode
|= MNT_OMODE_FORCE
;
828 mnt_free_context(cxt
);
831 mnt_free_context(cxt
);
832 mount_print_version();
834 errtryhelp(MNT_EX_USAGE
);
841 optmode
|= optmode_mode
| optmode_src
;
844 optmode
|= MNT_OMODE_PREPEND
;
846 optmode
|= MNT_OMODE_FSTAB
| MNT_OMODE_MTAB
;
847 mnt_context_set_optsmode(cxt
, optmode
);
850 if (fstab
&& !mnt_context_is_nocanonicalize(cxt
)) {
852 * We have external (context independent) fstab instance, let's
853 * make a connection between the fstab and the canonicalization
856 mnt_table_set_cache(fstab
, mnt_context_get_cache(cxt
));
859 if (!mnt_context_get_source(cxt
) &&
860 !mnt_context_get_target(cxt
) &&
863 if (oper
|| mnt_context_get_options(cxt
)) {
864 warnx(_("bad usage"));
865 errtryhelp(MNT_EX_USAGE
);
867 print_all(cxt
, types
, show_labels
);
871 /* Non-root users are allowed to use -t to print_all(),
873 if (mnt_context_is_restricted(cxt
) && types
)
874 exit_non_root("types");
876 if (oper
&& (types
|| all
|| mnt_context_get_source(cxt
))) {
877 warnx(_("bad usage"));
878 errtryhelp(MNT_EX_USAGE
);
881 if (types
&& (all
|| strchr(types
, ',') ||
882 strncmp(types
, "no", 2) == 0))
883 mnt_context_set_fstype_pattern(cxt
, types
);
885 mnt_context_set_fstype(cxt
, types
);
891 if (has_remount_flag(cxt
))
892 rc
= remount_all(cxt
);
897 } else if (argc
== 0 && (mnt_context_get_source(cxt
) ||
898 mnt_context_get_target(cxt
))) {
900 * B) mount -L|-U|--source|--target
902 * non-root may specify source *or* target, but not both
904 if (mnt_context_is_restricted(cxt
) &&
905 mnt_context_get_source(cxt
) &&
906 mnt_context_get_target(cxt
))
909 } else if (argc
== 1 && (!mnt_context_get_source(cxt
) ||
910 !mnt_context_get_target(cxt
))) {
912 * C) mount [-L|-U|--source] <target>
913 * mount [--target <dir>] <source>
914 * mount <source|target>
916 * non-root may specify source *or* target, but not both
918 * It does not matter for libmount if we set source or target
919 * here (the library is able to swap it), but it matters for
922 int istag
= mnt_tag_is_valid(argv
[0]);
924 if (istag
&& mnt_context_get_source(cxt
))
925 /* -L, -U or --source together with LABEL= or UUID= */
926 errx(MNT_EX_USAGE
, _("source specified more than once"));
927 else if (istag
|| mnt_context_get_target(cxt
))
928 mnt_context_set_source(cxt
, argv
[0]);
930 mnt_context_set_target(cxt
, argv
[0]);
932 if (mnt_context_is_restricted(cxt
) &&
933 mnt_context_get_source(cxt
) &&
934 mnt_context_get_target(cxt
))
937 } else if (argc
== 2 && !mnt_context_get_source(cxt
)
938 && !mnt_context_get_target(cxt
)) {
940 * D) mount <source> <target>
942 if (mnt_context_is_restricted(cxt
))
945 mnt_context_set_source(cxt
, argv
[0]);
946 mnt_context_set_target(cxt
, argv
[1]);
949 warnx(_("bad usage"));
950 errtryhelp(MNT_EX_USAGE
);
953 if (mnt_context_is_restricted(cxt
))
957 /* "move" as option string is not supported by libmount */
958 mnt_context_set_mflags(cxt
, MS_MOVE
);
960 if ((oper
&& !has_remount_flag(cxt
)) || propa
)
961 /* For --make-* or --bind is fstab/mtab unnecessary */
962 mnt_context_set_optsmode(cxt
, MNT_OMODE_NOTAB
);
964 rc
= mnt_context_mount(cxt
);
965 rc
= mk_exit_code(cxt
, rc
);
967 if (rc
== MNT_EX_SUCCESS
&& mnt_context_is_verbose(cxt
))
968 success_message(cxt
);
970 mnt_free_context(cxt
);