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 "exitcodes.h"
41 #include "closestream.h"
42 #include "canonicalize.h"
44 #define OPTUTILS_EXIT_CODE MOUNT_EX_USAGE
49 * --options-mode={ignore,append,prepend,replace} MNT_OMODE_{IGNORE, ...}
50 * --options-source={fstab,mtab,disable} MNT_OMODE_{FSTAB,MTAB,NOTAB}
51 * --options-source-force MNT_OMODE_FORCE
56 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
);
58 static void __attribute__((__noreturn__
)) exit_non_root(const char *option
)
60 const uid_t ruid
= getuid();
61 const uid_t euid
= geteuid();
63 if (ruid
== 0 && euid
!= 0) {
64 /* user is root, but setuid to non-root */
66 errx(MOUNT_EX_USAGE
, _("only root can use \"--%s\" option "
67 "(effective UID is %u)"),
69 errx(MOUNT_EX_USAGE
, _("only root can do that "
70 "(effective UID is %u)"), euid
);
73 errx(MOUNT_EX_USAGE
, _("only root can use \"--%s\" option"), option
);
74 errx(MOUNT_EX_USAGE
, _("only root can do that"));
77 static void __attribute__((__noreturn__
)) print_version(void)
79 const char *ver
= NULL
;
80 const char **features
= NULL
, **p
;
82 mnt_get_library_version(&ver
);
83 mnt_get_library_features(&features
);
85 printf(_("%s from %s (libmount %s"),
86 program_invocation_short_name
,
91 fputs(p
== features
? ": " : ", ", stdout
);
95 exit(MOUNT_EX_SUCCESS
);
98 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
99 const char *filename
, int line
)
102 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
107 * Replace control chars with '?' to be compatible with coreutils. For more
108 * robust solution use findmnt(1) where we use \x?? hex encoding.
110 static void safe_fputs(const char *data
)
114 for (p
= data
; p
&& *p
; p
++) {
115 if (iscntrl((unsigned char) *p
))
122 static void print_all(struct libmnt_context
*cxt
, char *pattern
, int show_label
)
124 struct libmnt_table
*tb
;
125 struct libmnt_iter
*itr
= NULL
;
126 struct libmnt_fs
*fs
;
127 struct libmnt_cache
*cache
= NULL
;
129 if (mnt_context_get_mtab(cxt
, &tb
))
130 err(MOUNT_EX_SYSERR
, _("failed to read mtab"));
132 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
134 err(MOUNT_EX_SYSERR
, _("failed to initialize libmount iterator"));
136 cache
= mnt_new_cache();
138 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
139 const char *type
= mnt_fs_get_fstype(fs
);
140 const char *src
= mnt_fs_get_source(fs
);
141 const char *optstr
= mnt_fs_get_options(fs
);
144 if (type
&& pattern
&& !mnt_match_fstype(type
, pattern
))
147 if (!mnt_fs_is_pseudofs(fs
))
148 xsrc
= mnt_pretty_path(src
, cache
);
149 printf ("%s on ", xsrc
? xsrc
: src
);
150 safe_fputs(mnt_fs_get_target(fs
));
153 printf (" type %s", type
);
155 printf (" (%s)", optstr
);
156 if (show_label
&& src
) {
157 char *lb
= mnt_cache_find_tag_value(cache
, src
, "LABEL");
159 printf (" [%s]", lb
);
165 mnt_unref_cache(cache
);
172 static int mount_all(struct libmnt_context
*cxt
)
174 struct libmnt_iter
*itr
;
175 struct libmnt_fs
*fs
;
176 int mntrc
, ignored
, rc
= MOUNT_EX_SUCCESS
;
178 int nsucc
= 0, nerrs
= 0;
180 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
182 warn(_("failed to initialize libmount iterator"));
183 return MOUNT_EX_SYSERR
;
186 while (mnt_context_next_mount(cxt
, itr
, &fs
, &mntrc
, &ignored
) == 0) {
188 const char *tgt
= mnt_fs_get_target(fs
);
191 if (mnt_context_is_verbose(cxt
))
192 printf(ignored
== 1 ? _("%-25s: ignored\n") :
193 _("%-25s: already mounted\n"),
195 } else if (mnt_context_is_fork(cxt
)) {
196 if (mnt_context_is_verbose(cxt
))
197 printf("%-25s: mount successfully forked\n", tgt
);
199 if (mk_exit_code(cxt
, mntrc
) == MOUNT_EX_SUCCESS
) {
202 /* Note that MOUNT_EX_SUCCESS return code does
203 * not mean that FS has been really mounted
204 * (e.g. nofail option) */
205 if (mnt_context_get_status(cxt
)
206 && mnt_context_is_verbose(cxt
))
207 printf("%-25s: successfully mounted\n", tgt
);
213 if (mnt_context_is_parent(cxt
)) {
214 /* wait for mount --fork children */
217 nerrs
= 0, nsucc
= 0;
219 rc
= mnt_context_wait_for_children(cxt
, &nchildren
, &nerrs
);
220 if (!rc
&& nchildren
)
221 nsucc
= nchildren
- nerrs
;
225 rc
= MOUNT_EX_SUCCESS
; /* all success */
227 rc
= MOUNT_EX_FAIL
; /* all failed */
229 rc
= MOUNT_EX_SOMEOK
; /* some success, some failed */
235 static void success_message(struct libmnt_context
*cxt
)
237 unsigned long mflags
= 0;
238 const char *tgt
, *src
, *pr
= program_invocation_short_name
;
240 if (mnt_context_helper_executed(cxt
)
241 || mnt_context_get_status(cxt
) != 1)
244 mnt_context_get_mflags(cxt
, &mflags
);
245 tgt
= mnt_context_get_target(cxt
);
246 src
= mnt_context_get_source(cxt
);
248 if (mflags
& MS_MOVE
)
249 printf(_("%s: %s moved to %s.\n"), pr
, src
, tgt
);
250 else if (mflags
& MS_BIND
)
251 printf(_("%s: %s bound on %s.\n"), pr
, src
, tgt
);
252 else if (mflags
& MS_PROPAGATION
) {
253 if (src
&& strcmp(src
, "none") != 0 && tgt
)
254 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
256 printf(_("%s: %s propagation flags changed.\n"), pr
, tgt
);
258 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
262 * Handles generic errors like ENOMEM, ...
265 * <0 error (usually -errno)
267 * Returns exit status (MOUNT_EX_*) and prints error message.
269 static int handle_generic_errors(int rc
, const char *msg
, ...)
284 rc
= MOUNT_EX_SYSERR
;
295 #if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
296 #include <selinux/selinux.h>
297 #include <selinux/context.h>
299 static void selinux_warning(struct libmnt_context
*cxt
, const char *tgt
)
302 if (tgt
&& mnt_context_is_verbose(cxt
) && is_selinux_enabled() > 0) {
303 security_context_t raw
= NULL
, def
= NULL
;
305 if (getfilecon(tgt
, &raw
) > 0
306 && security_get_initial_context("file", &def
) == 0) {
308 if (!selinux_file_context_cmp(raw
, def
))
310 "mount: %s does not contain SELinux labels.\n"
311 " You just mounted an file system that supports labels which does not\n"
312 " contain labels, onto an SELinux box. It is likely that confined\n"
313 " applications will generate AVC messages and not be allowed access to\n"
314 " this file system. For more details see restorecon(8) and mount(8).\n"),
322 # define selinux_warning(_x, _y)
326 * Returns 1 if @dir parent is shared
328 static int is_shared_tree(struct libmnt_context
*cxt
, const char *dir
)
330 struct libmnt_table
*tb
= NULL
;
331 struct libmnt_fs
*fs
;
332 unsigned long mflags
= 0;
333 char *mnt
= NULL
, *p
;
338 if (mnt_context_get_mtab(cxt
, &tb
) || !tb
)
341 p
= strrchr(mnt
, '/');
346 fs
= mnt_table_find_mountpoint(tb
, mnt
, MNT_ITER_BACKWARD
);
348 rc
= fs
&& mnt_fs_is_kernel(fs
)
349 && mnt_fs_get_propagation(fs
, &mflags
) == 0
350 && (mflags
& MS_SHARED
);
358 * <0 error (usually -errno or -1)
360 * Returns exit status (MOUNT_EX_*) and/or prints error message.
362 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
)
366 unsigned long uflags
= 0, mflags
= 0;
368 int restricted
= mnt_context_is_restricted(cxt
);
369 const char *tgt
= mnt_context_get_target(cxt
);
370 const char *src
= mnt_context_get_source(cxt
);
373 if (mnt_context_helper_executed(cxt
)) {
375 * /sbin/mount.<type> called, return status
377 if (rc
== -MNT_ERR_APPLYFLAGS
)
378 warnx(_("WARNING: failed to apply propagation flags"));
379 return mnt_context_get_helper_status(cxt
);
382 if (rc
== 0 && mnt_context_get_status(cxt
) == 1) {
384 * Libmount success && syscall success.
386 selinux_warning(cxt
, tgt
);
388 return MOUNT_EX_SUCCESS
; /* mount(2) success */
391 mnt_context_get_mflags(cxt
, &mflags
); /* mount(2) flags */
392 mnt_context_get_user_mflags(cxt
, &uflags
); /* userspace flags */
394 if (!mnt_context_syscall_called(cxt
)) {
396 * libmount errors (extra library checks)
400 warnx(_("only root can mount %s on %s"), src
, tgt
);
401 return MOUNT_EX_USAGE
;
403 warnx(_("%s is already mounted"), src
);
404 return MOUNT_EX_USAGE
;
405 /* -EROFS before syscall can happen only for loop mount */
407 warnx(_("%s is used as read only loop, mounting read-only"), src
);
408 mnt_context_reset_status(cxt
);
409 mnt_context_set_mflags(cxt
, mflags
| MS_RDONLY
);
410 rc
= mnt_context_mount(cxt
);
412 rc
= mnt_context_finalize_mount(cxt
);
414 case -MNT_ERR_NOFSTAB
:
415 if (mnt_context_is_swapmatch(cxt
)) {
416 warnx(_("can't find %s in %s"),
418 mnt_get_fstab_path());
419 return MOUNT_EX_USAGE
;
421 /* source/target explicitly defined */
423 warnx(_("can't find mountpoint %s in %s"),
424 tgt
, mnt_get_fstab_path());
426 warnx(_("can't find mount source %s in %s"),
427 src
, mnt_get_fstab_path());
428 return MOUNT_EX_USAGE
;
429 case -MNT_ERR_AMBIFS
:
430 warnx(_("%s: more filesystems detected. This should not happen,\n"
431 " use -t <type> to explicitly specify the filesystem type or\n"
432 " use wipefs(8) to clean up the device."), src
);
433 return MOUNT_EX_USAGE
;
434 case -MNT_ERR_NOFSTYPE
:
436 warnx(_("I could not determine the filesystem type, "
437 "and none was specified"));
439 warnx(_("you must specify the filesystem type"));
440 return MOUNT_EX_USAGE
;
441 case -MNT_ERR_NOSOURCE
:
442 if (uflags
& MNT_MS_NOFAIL
)
443 return MOUNT_EX_SUCCESS
;
445 warnx(_("can't find %s"), src
);
447 warnx(_("mount source not defined"));
448 return MOUNT_EX_USAGE
;
449 case -MNT_ERR_MOUNTOPT
:
451 warn(_("failed to parse mount options"));
453 warnx(_("failed to parse mount options"));
454 return MOUNT_EX_USAGE
;
455 case -MNT_ERR_LOOPDEV
:
456 warn(_("%s: failed to setup loop device"), src
);
457 return MOUNT_EX_FAIL
;
458 case -MNT_ERR_LOOPOVERLAP
:
459 warnx(_("%s: overlapping loop device exists"), src
);
460 return MOUNT_EX_FAIL
;
462 return handle_generic_errors(rc
, _("%s: mount failed"),
465 } else if (mnt_context_get_syscall_errno(cxt
) == 0) {
467 * mount(2) syscall success, but something else failed
468 * (probably error in mtab processing).
471 return handle_generic_errors(rc
,
472 _("%s: filesystem mounted, but mount(8) failed"),
475 return MOUNT_EX_SOFTWARE
; /* internal error */
482 syserr
= mnt_context_get_syscall_errno(cxt
);
487 if (geteuid() == 0) {
488 if (stat(tgt
, &st
) || !S_ISDIR(st
.st_mode
))
489 warnx(_("mount point %s is not a directory"), tgt
);
491 warnx(_("permission denied"));
493 warnx(_("must be superuser to use mount"));
498 struct libmnt_table
*tb
;
500 if (mflags
& MS_REMOUNT
) {
501 warnx(_("%s is busy"), tgt
);
505 warnx(_("%s is already mounted or %s busy"), src
, tgt
);
507 if (src
&& mnt_context_get_mtab(cxt
, &tb
) == 0) {
508 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
509 struct libmnt_fs
*fs
;
511 while(mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
512 const char *s
= mnt_fs_get_srcpath(fs
),
513 *t
= mnt_fs_get_target(fs
);
515 if (t
&& s
&& mnt_fs_streq_srcpath(fs
, src
))
517 " %s is already mounted on %s\n"), s
, t
);
524 if (tgt
&& lstat(tgt
, &st
))
525 warnx(_("mount point %s does not exist"), tgt
);
526 else if (tgt
&& stat(tgt
, &st
))
527 warnx(_("mount point %s is a symbolic link to nowhere"), tgt
);
528 else if (src
&& stat(src
, &st
)) {
529 if (uflags
& MNT_MS_NOFAIL
)
530 return MOUNT_EX_SUCCESS
;
532 warnx(_("special device %s does not exist"), src
);
536 warn("%s: %s", _("mount(2) failed"), tgt
);
538 warn("%s: %s", _("mount(2) failed"), src
);
540 warn(_("mount(2) failed"));
545 if (stat(tgt
, &st
) || ! S_ISDIR(st
.st_mode
))
546 warnx(_("mount point %s is not a directory"), tgt
);
547 else if (src
&& stat(src
, &st
) && errno
== ENOTDIR
) {
548 if (uflags
& MNT_MS_NOFAIL
)
549 return MOUNT_EX_SUCCESS
;
551 warnx(_("special device %s does not exist "
552 "(a path prefix is not a directory)"), src
);
555 warn("%s: %s", _("mount(2) failed"), tgt
);
560 if (mflags
& MS_REMOUNT
)
561 warnx(_("%s not mounted or bad option"), tgt
);
562 else if (rc
== -MNT_ERR_APPLYFLAGS
)
563 warnx(_("%s is not mountpoint or bad option"), tgt
);
564 else if ((mflags
& MS_MOVE
) && is_shared_tree(cxt
, src
))
565 warnx(_("bad option. Note that moving a mount residing under a shared\n"
566 " mount is unsupported."));
568 warnx(_("wrong fs type, bad option, bad superblock on %s,\n"
569 " missing codepage or helper program, or other error"),
572 if (mnt_fs_is_netfs(mnt_context_get_fs(cxt
)))
574 " (for several filesystems (e.g. nfs, cifs) you might\n"
575 " need a /sbin/mount.<type> helper program)\n"));
577 fprintf(stderr
, _("\n"
578 " In some cases useful info is found in syslog - try\n"
579 " dmesg | tail or so.\n"));
583 warnx(_("mount table full"));
587 warnx(_("%s: can't read superblock"), src
);
591 if (mnt_context_get_fstype(cxt
))
592 warnx(_("unknown filesystem type '%s'"), mnt_context_get_fstype(cxt
));
594 warnx(_("unknown filesystem type"));
598 if (uflags
& MNT_MS_NOFAIL
)
599 return MOUNT_EX_SUCCESS
;
602 warnx(_("%s is not a block device, and stat(2) fails?"), src
);
603 else if (S_ISBLK(st
.st_mode
))
604 warnx(_("the kernel does not recognize %s as a block device\n"
605 " (maybe `modprobe driver'?)"), src
);
606 else if (S_ISREG(st
.st_mode
))
607 warnx(_("%s is not a block device (maybe try `-o loop'?)"), src
);
609 warnx(_(" %s is not a block device"), src
);
613 if (uflags
& MNT_MS_NOFAIL
)
614 return MOUNT_EX_SUCCESS
;
616 warnx(_("%s is not a valid block device"), src
);
621 if (mflags
& MS_RDONLY
)
622 warnx(_("cannot mount %s read-only"), src
);
625 warnx(_("%s is write-protected but explicit `-w' flag given"), src
);
627 else if (mflags
& MS_REMOUNT
)
628 warnx(_("cannot remount %s read-write, is write-protected"), src
);
630 else if (mflags
& MS_BIND
)
631 warn(_("mount %s on %s failed"), src
, tgt
);
634 warnx(_("%s is write-protected, mounting read-only"), src
);
636 mnt_context_reset_status(cxt
);
637 mnt_context_set_mflags(cxt
, mflags
| MS_RDONLY
);
638 rc
= mnt_context_do_mount(cxt
);
640 rc
= mnt_context_finalize_mount(cxt
);
647 if (uflags
& MNT_MS_NOFAIL
)
648 return MOUNT_EX_SUCCESS
;
650 warnx(_("no medium found on %s"), src
);
654 warn(_("mount %s on %s failed"), src
, tgt
);
658 return MOUNT_EX_FAIL
;
661 static struct libmnt_table
*append_fstab(struct libmnt_context
*cxt
,
662 struct libmnt_table
*fstab
,
667 fstab
= mnt_new_table();
669 err(MOUNT_EX_SYSERR
, _("failed to initialize libmount table"));
671 mnt_table_set_parser_errcb(fstab
, table_parser_errcb
);
672 mnt_context_set_fstab(cxt
, fstab
);
674 mnt_unref_table(fstab
); /* reference is handled by @cxt now */
677 if (mnt_table_parse_fstab(fstab
, path
))
678 errx(MOUNT_EX_USAGE
,_("%s: failed to parse"), path
);
684 * Check source and target paths -- non-root user should not be able to
685 * resolve paths which are unreadable for him.
687 static void sanitize_paths(struct libmnt_context
*cxt
)
690 struct libmnt_fs
*fs
= mnt_context_get_fs(cxt
);
695 p
= mnt_fs_get_target(fs
);
697 char *np
= canonicalize_path_restricted(p
);
699 err(MOUNT_EX_USAGE
, "%s", p
);
700 mnt_fs_set_target(fs
, np
);
704 p
= mnt_fs_get_srcpath(fs
);
706 char *np
= canonicalize_path_restricted(p
);
708 err(MOUNT_EX_USAGE
, "%s", p
);
709 mnt_fs_set_source(fs
, np
);
714 static void append_option(struct libmnt_context
*cxt
, const char *opt
)
716 if (opt
&& (*opt
== '=' || *opt
== '\'' || *opt
== '\"' || isblank(*opt
)))
717 errx(MOUNT_EX_USAGE
, _("unsupported option format: %s"), opt
);
718 if (mnt_context_append_options(cxt
, opt
))
719 err(MOUNT_EX_SYSERR
, _("failed to append option '%s'"), opt
);
722 static int has_remount_flag(struct libmnt_context
*cxt
)
724 unsigned long mflags
= 0;
726 if (mnt_context_get_mflags(cxt
, &mflags
))
729 return mflags
& MS_REMOUNT
;
732 static void __attribute__((__noreturn__
)) usage(FILE *out
)
734 fputs(USAGE_HEADER
, out
);
737 " %1$s -a [options]\n"
738 " %1$s [options] [--source] <source> | [--target] <directory>\n"
739 " %1$s [options] <source> <directory>\n"
740 " %1$s <operation> <mountpoint> [<target>]\n"),
741 program_invocation_short_name
);
743 fputs(USAGE_SEPARATOR
, out
);
744 fputs(_("Mount a filesystem.\n"), out
);
746 fputs(USAGE_OPTIONS
, out
);
748 " -a, --all mount all filesystems mentioned in fstab\n"
749 " -c, --no-canonicalize don't canonicalize paths\n"
750 " -f, --fake dry run; skip the mount(2) syscall\n"
751 " -F, --fork fork off for each device (use with -a)\n"
752 " -T, --fstab <path> alternative file to /etc/fstab\n"));
754 " -i, --internal-only don't call the mount.<type> helpers\n"));
756 " -l, --show-labels show also filesystem labels\n"));
758 " -n, --no-mtab don't write to /etc/mtab\n"));
760 " -o, --options <list> comma-separated list of mount options\n"
761 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
762 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
763 " -t, --types <list> limit the set of filesystem types\n"));
765 " --source <src> explicitly specifies source (path, label, uuid)\n"
766 " --target <target> explicitly specifies mountpoint\n"));
768 " -v, --verbose say what is being done\n"));
770 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
772 fputs(USAGE_SEPARATOR
, out
);
773 fputs(USAGE_HELP
, out
);
774 fputs(USAGE_VERSION
, out
);
778 " -L, --label <label> synonym for LABEL=<label>\n"
779 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
780 " LABEL=<label> specifies device by filesystem label\n"
781 " UUID=<uuid> specifies device by filesystem UUID\n"
782 " PARTLABEL=<label> specifies device by partition label\n"
783 " PARTUUID=<uuid> specifies device by partition UUID\n"));
786 " <device> specifies device by path\n"
787 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
788 " <file> regular file for loopdev setup\n"));
792 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
793 " -M, --move move a subtree to some other place\n"
794 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
796 " --make-shared mark a subtree as shared\n"
797 " --make-slave mark a subtree as slave\n"
798 " --make-private mark a subtree as private\n"
799 " --make-unbindable mark a subtree as unbindable\n"));
801 " --make-rshared recursively mark a whole subtree as shared\n"
802 " --make-rslave recursively mark a whole subtree as slave\n"
803 " --make-rprivate recursively mark a whole subtree as private\n"
804 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
806 fprintf(out
, USAGE_MAN_TAIL("mount(8)"));
808 exit(out
== stderr
? MOUNT_EX_USAGE
: MOUNT_EX_SUCCESS
);
811 int main(int argc
, char **argv
)
813 int c
, rc
= MOUNT_EX_SUCCESS
, all
= 0, show_labels
= 0;
814 struct libmnt_context
*cxt
;
815 struct libmnt_table
*fstab
= NULL
;
818 unsigned long oper
= 0;
822 MOUNT_OPT_SHARED
= CHAR_MAX
+ 1,
825 MOUNT_OPT_UNBINDABLE
,
829 MOUNT_OPT_RUNBINDABLE
,
834 static const struct option longopts
[] = {
835 { "all", 0, 0, 'a' },
836 { "fake", 0, 0, 'f' },
837 { "fstab", 1, 0, 'T' },
838 { "fork", 0, 0, 'F' },
839 { "help", 0, 0, 'h' },
840 { "no-mtab", 0, 0, 'n' },
841 { "read-only", 0, 0, 'r' },
843 { "verbose", 0, 0, 'v' },
844 { "version", 0, 0, 'V' },
845 { "read-write", 0, 0, 'w' },
847 { "options", 1, 0, 'o' },
848 { "test-opts", 1, 0, 'O' },
849 { "types", 1, 0, 't' },
850 { "uuid", 1, 0, 'U' },
851 { "label", 1, 0, 'L'},
852 { "bind", 0, 0, 'B' },
853 { "move", 0, 0, 'M' },
854 { "rbind", 0, 0, 'R' },
855 { "make-shared", 0, 0, MOUNT_OPT_SHARED
},
856 { "make-slave", 0, 0, MOUNT_OPT_SLAVE
},
857 { "make-private", 0, 0, MOUNT_OPT_PRIVATE
},
858 { "make-unbindable", 0, 0, MOUNT_OPT_UNBINDABLE
},
859 { "make-rshared", 0, 0, MOUNT_OPT_RSHARED
},
860 { "make-rslave", 0, 0, MOUNT_OPT_RSLAVE
},
861 { "make-rprivate", 0, 0, MOUNT_OPT_RPRIVATE
},
862 { "make-runbindable", 0, 0, MOUNT_OPT_RUNBINDABLE
},
863 { "no-canonicalize", 0, 0, 'c' },
864 { "internal-only", 0, 0, 'i' },
865 { "show-labels", 0, 0, 'l' },
866 { "target", 1, 0, MOUNT_OPT_TARGET
},
867 { "source", 1, 0, MOUNT_OPT_SOURCE
},
871 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
872 { 'B','M','R' }, /* bind,move,rbind */
873 { 'L','U', MOUNT_OPT_SOURCE
}, /* label,uuid,source */
876 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
879 setlocale(LC_ALL
, "");
880 bindtextdomain(PACKAGE
, LOCALEDIR
);
882 atexit(close_stdout
);
885 cxt
= mnt_new_context();
887 err(MOUNT_EX_SYSERR
, _("libmount context allocation failed"));
889 mnt_context_set_tables_errcb(cxt
, table_parser_errcb
);
891 while ((c
= getopt_long(argc
, argv
, "aBcfFhilL:Mno:O:rRsU:vVwt:T:",
892 longopts
, NULL
)) != -1) {
894 /* only few options are allowed for non-root users */
895 if (mnt_context_is_restricted(cxt
) &&
896 !strchr("hlLUVvrist", c
) &&
897 c
!= MOUNT_OPT_TARGET
&&
898 c
!= MOUNT_OPT_SOURCE
)
899 exit_non_root(option_to_longopt(c
, longopts
));
901 err_exclusive_options(c
, longopts
, excl
, excl_st
);
908 mnt_context_disable_canonicalize(cxt
, TRUE
);
911 mnt_context_enable_fake(cxt
, TRUE
);
914 mnt_context_enable_fork(cxt
, TRUE
);
920 mnt_context_disable_helpers(cxt
, TRUE
);
923 mnt_context_disable_mtab(cxt
, TRUE
);
926 append_option(cxt
, "ro");
930 mnt_context_enable_verbose(cxt
, TRUE
);
936 append_option(cxt
, "rw");
940 append_option(cxt
, optarg
);
943 if (mnt_context_set_options_pattern(cxt
, optarg
))
944 err(MOUNT_EX_SYSERR
, _("failed to set options pattern"));
947 xasprintf(&srcbuf
, "LABEL=\"%s\"", optarg
);
948 mnt_context_disable_swapmatch(cxt
, 1);
949 mnt_context_set_source(cxt
, srcbuf
);
953 xasprintf(&srcbuf
, "UUID=\"%s\"", optarg
);
954 mnt_context_disable_swapmatch(cxt
, 1);
955 mnt_context_set_source(cxt
, srcbuf
);
965 fstab
= append_fstab(cxt
, fstab
, optarg
);
968 mnt_context_enable_sloppy(cxt
, TRUE
);
977 oper
|= (MS_BIND
| MS_REC
);
979 case MOUNT_OPT_SHARED
:
980 append_option(cxt
, "shared");
983 case MOUNT_OPT_SLAVE
:
984 append_option(cxt
, "slave");
987 case MOUNT_OPT_PRIVATE
:
988 append_option(cxt
, "private");
991 case MOUNT_OPT_UNBINDABLE
:
992 append_option(cxt
, "unbindable");
995 case MOUNT_OPT_RSHARED
:
996 append_option(cxt
, "rshared");
999 case MOUNT_OPT_RSLAVE
:
1000 append_option(cxt
, "rslave");
1003 case MOUNT_OPT_RPRIVATE
:
1004 append_option(cxt
, "rprivate");
1007 case MOUNT_OPT_RUNBINDABLE
:
1008 append_option(cxt
, "runbindable");
1011 case MOUNT_OPT_TARGET
:
1012 mnt_context_disable_swapmatch(cxt
, 1);
1013 mnt_context_set_target(cxt
, optarg
);
1015 case MOUNT_OPT_SOURCE
:
1016 mnt_context_disable_swapmatch(cxt
, 1);
1017 mnt_context_set_source(cxt
, optarg
);
1020 errtryhelp(MOUNT_EX_USAGE
);
1027 if (fstab
&& !mnt_context_is_nocanonicalize(cxt
)) {
1029 * We have external (context independent) fstab instance, let's
1030 * make a connection between the fstab and the canonicalization
1033 mnt_table_set_cache(fstab
, mnt_context_get_cache(cxt
));
1036 if (!mnt_context_get_source(cxt
) &&
1037 !mnt_context_get_target(cxt
) &&
1040 if (oper
|| mnt_context_get_options(cxt
))
1042 print_all(cxt
, types
, show_labels
);
1046 /* Non-root users are allowed to use -t to print_all(),
1048 if (mnt_context_is_restricted(cxt
) && types
)
1049 exit_non_root("types");
1051 if (oper
&& (types
|| all
|| mnt_context_get_source(cxt
)))
1054 if (types
&& (all
|| strchr(types
, ',') ||
1055 strncmp(types
, "no", 2) == 0))
1056 mnt_context_set_fstype_pattern(cxt
, types
);
1058 mnt_context_set_fstype(cxt
, types
);
1064 rc
= mount_all(cxt
);
1067 } else if (argc
== 0 && (mnt_context_get_source(cxt
) ||
1068 mnt_context_get_target(cxt
))) {
1070 * B) mount -L|-U|--source|--target
1072 * non-root may specify source *or* target, but not both
1074 if (mnt_context_is_restricted(cxt
) &&
1075 mnt_context_get_source(cxt
) &&
1076 mnt_context_get_target(cxt
))
1077 exit_non_root(NULL
);
1079 } else if (argc
== 1 && (!mnt_context_get_source(cxt
) ||
1080 !mnt_context_get_target(cxt
))) {
1082 * C) mount [-L|-U|--source] <target>
1083 * mount [--target <dir>] <source>
1084 * mount <source|target>
1086 * non-root may specify source *or* target, but not both
1088 * It does not matter for libmount if we set source or target
1089 * here (the library is able to swap it), but it matters for
1092 int istag
= mnt_tag_is_valid(argv
[0]);
1094 if (istag
&& mnt_context_get_source(cxt
))
1095 /* -L, -U or --source together with LABEL= or UUID= */
1096 errx(MOUNT_EX_USAGE
, _("source specified more than once"));
1097 else if (istag
|| mnt_context_get_target(cxt
))
1098 mnt_context_set_source(cxt
, argv
[0]);
1100 mnt_context_set_target(cxt
, argv
[0]);
1102 if (mnt_context_is_restricted(cxt
) &&
1103 mnt_context_get_source(cxt
) &&
1104 mnt_context_get_target(cxt
))
1105 exit_non_root(NULL
);
1107 } else if (argc
== 2 && !mnt_context_get_source(cxt
)
1108 && !mnt_context_get_target(cxt
)) {
1110 * D) mount <source> <target>
1112 if (mnt_context_is_restricted(cxt
))
1113 exit_non_root(NULL
);
1115 mnt_context_set_source(cxt
, argv
[0]);
1116 mnt_context_set_target(cxt
, argv
[1]);
1121 if (mnt_context_is_restricted(cxt
))
1122 sanitize_paths(cxt
);
1125 /* BIND/MOVE operations, let's set the mount flags */
1126 mnt_context_set_mflags(cxt
, oper
);
1128 if ((oper
&& !has_remount_flag(cxt
)) || propa
)
1129 /* For --make-* or --bind is fstab/mtab unnecessary */
1130 mnt_context_set_optsmode(cxt
, MNT_OMODE_NOTAB
);
1132 rc
= mnt_context_mount(cxt
);
1133 rc
= mk_exit_code(cxt
, rc
);
1135 if (rc
== MOUNT_EX_SUCCESS
&& mnt_context_is_verbose(cxt
))
1136 success_message(cxt
);
1138 mnt_free_context(cxt
);