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
))
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(FILE *out
)
389 fputs(USAGE_HEADER
, out
);
392 " %1$s -a [options]\n"
393 " %1$s [options] [--source] <source> | [--target] <directory>\n"
394 " %1$s [options] <source> <directory>\n"
395 " %1$s <operation> <mountpoint> [<target>]\n"),
396 program_invocation_short_name
);
398 fputs(USAGE_SEPARATOR
, out
);
399 fputs(_("Mount a filesystem.\n"), out
);
401 fputs(USAGE_OPTIONS
, out
);
403 " -a, --all mount all filesystems mentioned in fstab\n"
404 " -c, --no-canonicalize don't canonicalize paths\n"
405 " -f, --fake dry run; skip the mount(2) syscall\n"
406 " -F, --fork fork off for each device (use with -a)\n"
407 " -T, --fstab <path> alternative file to /etc/fstab\n"));
409 " -i, --internal-only don't call the mount.<type> helpers\n"));
411 " -l, --show-labels show also filesystem labels\n"));
413 " -n, --no-mtab don't write to /etc/mtab\n"));
415 " -o, --options <list> comma-separated list of mount options\n"
416 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
417 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
418 " -t, --types <list> limit the set of filesystem types\n"));
420 " --source <src> explicitly specifies source (path, label, uuid)\n"
421 " --target <target> explicitly specifies mountpoint\n"));
423 " -v, --verbose say what is being done\n"));
425 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
427 fputs(USAGE_SEPARATOR
, out
);
428 fputs(USAGE_HELP
, out
);
429 fputs(USAGE_VERSION
, out
);
433 " -L, --label <label> synonym for LABEL=<label>\n"
434 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
435 " LABEL=<label> specifies device by filesystem label\n"
436 " UUID=<uuid> specifies device by filesystem UUID\n"
437 " PARTLABEL=<label> specifies device by partition label\n"
438 " PARTUUID=<uuid> specifies device by partition UUID\n"));
441 " <device> specifies device by path\n"
442 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
443 " <file> regular file for loopdev setup\n"));
447 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
448 " -M, --move move a subtree to some other place\n"
449 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
451 " --make-shared mark a subtree as shared\n"
452 " --make-slave mark a subtree as slave\n"
453 " --make-private mark a subtree as private\n"
454 " --make-unbindable mark a subtree as unbindable\n"));
456 " --make-rshared recursively mark a whole subtree as shared\n"
457 " --make-rslave recursively mark a whole subtree as slave\n"
458 " --make-rprivate recursively mark a whole subtree as private\n"
459 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
461 fprintf(out
, USAGE_MAN_TAIL("mount(8)"));
463 exit(out
== stderr
? MNT_EX_USAGE
: MNT_EX_SUCCESS
);
466 int main(int argc
, char **argv
)
468 int c
, rc
= MNT_EX_SUCCESS
, all
= 0, show_labels
= 0;
469 struct libmnt_context
*cxt
;
470 struct libmnt_table
*fstab
= NULL
;
473 unsigned long oper
= 0;
477 MOUNT_OPT_SHARED
= CHAR_MAX
+ 1,
480 MOUNT_OPT_UNBINDABLE
,
484 MOUNT_OPT_RUNBINDABLE
,
489 static const struct option longopts
[] = {
490 { "all", no_argument
, NULL
, 'a' },
491 { "fake", no_argument
, NULL
, 'f' },
492 { "fstab", required_argument
, NULL
, 'T' },
493 { "fork", no_argument
, NULL
, 'F' },
494 { "help", no_argument
, NULL
, 'h' },
495 { "no-mtab", no_argument
, NULL
, 'n' },
496 { "read-only", no_argument
, NULL
, 'r' },
497 { "ro", no_argument
, NULL
, 'r' },
498 { "verbose", no_argument
, NULL
, 'v' },
499 { "version", no_argument
, NULL
, 'V' },
500 { "read-write", no_argument
, NULL
, 'w' },
501 { "rw", no_argument
, NULL
, 'w' },
502 { "options", required_argument
, NULL
, 'o' },
503 { "test-opts", required_argument
, NULL
, 'O' },
504 { "types", required_argument
, NULL
, 't' },
505 { "uuid", required_argument
, NULL
, 'U' },
506 { "label", required_argument
, NULL
, 'L' },
507 { "bind", no_argument
, NULL
, 'B' },
508 { "move", no_argument
, NULL
, 'M' },
509 { "rbind", no_argument
, NULL
, 'R' },
510 { "make-shared", no_argument
, NULL
, MOUNT_OPT_SHARED
},
511 { "make-slave", no_argument
, NULL
, MOUNT_OPT_SLAVE
},
512 { "make-private", no_argument
, NULL
, MOUNT_OPT_PRIVATE
},
513 { "make-unbindable", no_argument
, NULL
, MOUNT_OPT_UNBINDABLE
},
514 { "make-rshared", no_argument
, NULL
, MOUNT_OPT_RSHARED
},
515 { "make-rslave", no_argument
, NULL
, MOUNT_OPT_RSLAVE
},
516 { "make-rprivate", no_argument
, NULL
, MOUNT_OPT_RPRIVATE
},
517 { "make-runbindable", no_argument
, NULL
, MOUNT_OPT_RUNBINDABLE
},
518 { "no-canonicalize", no_argument
, NULL
, 'c' },
519 { "internal-only", no_argument
, NULL
, 'i' },
520 { "show-labels", no_argument
, NULL
, 'l' },
521 { "target", required_argument
, NULL
, MOUNT_OPT_TARGET
},
522 { "source", required_argument
, NULL
, MOUNT_OPT_SOURCE
},
526 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
527 { 'B','M','R' }, /* bind,move,rbind */
528 { 'L','U', MOUNT_OPT_SOURCE
}, /* label,uuid,source */
531 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
534 setlocale(LC_ALL
, "");
535 bindtextdomain(PACKAGE
, LOCALEDIR
);
537 atexit(close_stdout
);
539 strutils_set_exitcode(MNT_EX_USAGE
);
542 cxt
= mnt_new_context();
544 err(MNT_EX_SYSERR
, _("libmount context allocation failed"));
546 mnt_context_set_tables_errcb(cxt
, table_parser_errcb
);
548 while ((c
= getopt_long(argc
, argv
, "aBcfFhilL:Mno:O:rRsU:vVwt:T:",
549 longopts
, NULL
)) != -1) {
551 /* only few options are allowed for non-root users */
552 if (mnt_context_is_restricted(cxt
) &&
553 !strchr("hlLUVvrist", c
) &&
554 c
!= MOUNT_OPT_TARGET
&&
555 c
!= MOUNT_OPT_SOURCE
)
556 exit_non_root(option_to_longopt(c
, longopts
));
558 err_exclusive_options(c
, longopts
, excl
, excl_st
);
565 mnt_context_disable_canonicalize(cxt
, TRUE
);
568 mnt_context_enable_fake(cxt
, TRUE
);
571 mnt_context_enable_fork(cxt
, TRUE
);
577 mnt_context_disable_helpers(cxt
, TRUE
);
580 mnt_context_disable_mtab(cxt
, TRUE
);
583 append_option(cxt
, "ro");
584 mnt_context_enable_rwonly_mount(cxt
, FALSE
);
587 mnt_context_enable_verbose(cxt
, TRUE
);
593 append_option(cxt
, "rw");
594 mnt_context_enable_rwonly_mount(cxt
, TRUE
);
597 append_option(cxt
, optarg
);
600 if (mnt_context_set_options_pattern(cxt
, optarg
))
601 err(MNT_EX_SYSERR
, _("failed to set options pattern"));
604 xasprintf(&srcbuf
, "LABEL=\"%s\"", optarg
);
605 mnt_context_disable_swapmatch(cxt
, 1);
606 mnt_context_set_source(cxt
, srcbuf
);
610 xasprintf(&srcbuf
, "UUID=\"%s\"", optarg
);
611 mnt_context_disable_swapmatch(cxt
, 1);
612 mnt_context_set_source(cxt
, srcbuf
);
622 fstab
= append_fstab(cxt
, fstab
, optarg
);
625 mnt_context_enable_sloppy(cxt
, TRUE
);
634 oper
|= (MS_BIND
| MS_REC
);
636 case MOUNT_OPT_SHARED
:
637 append_option(cxt
, "shared");
640 case MOUNT_OPT_SLAVE
:
641 append_option(cxt
, "slave");
644 case MOUNT_OPT_PRIVATE
:
645 append_option(cxt
, "private");
648 case MOUNT_OPT_UNBINDABLE
:
649 append_option(cxt
, "unbindable");
652 case MOUNT_OPT_RSHARED
:
653 append_option(cxt
, "rshared");
656 case MOUNT_OPT_RSLAVE
:
657 append_option(cxt
, "rslave");
660 case MOUNT_OPT_RPRIVATE
:
661 append_option(cxt
, "rprivate");
664 case MOUNT_OPT_RUNBINDABLE
:
665 append_option(cxt
, "runbindable");
668 case MOUNT_OPT_TARGET
:
669 mnt_context_disable_swapmatch(cxt
, 1);
670 mnt_context_set_target(cxt
, optarg
);
672 case MOUNT_OPT_SOURCE
:
673 mnt_context_disable_swapmatch(cxt
, 1);
674 mnt_context_set_source(cxt
, optarg
);
677 errtryhelp(MNT_EX_USAGE
);
684 if (fstab
&& !mnt_context_is_nocanonicalize(cxt
)) {
686 * We have external (context independent) fstab instance, let's
687 * make a connection between the fstab and the canonicalization
690 mnt_table_set_cache(fstab
, mnt_context_get_cache(cxt
));
693 if (!mnt_context_get_source(cxt
) &&
694 !mnt_context_get_target(cxt
) &&
697 if (oper
|| mnt_context_get_options(cxt
))
699 print_all(cxt
, types
, show_labels
);
703 /* Non-root users are allowed to use -t to print_all(),
705 if (mnt_context_is_restricted(cxt
) && types
)
706 exit_non_root("types");
708 if (oper
&& (types
|| all
|| mnt_context_get_source(cxt
)))
711 if (types
&& (all
|| strchr(types
, ',') ||
712 strncmp(types
, "no", 2) == 0))
713 mnt_context_set_fstype_pattern(cxt
, types
);
715 mnt_context_set_fstype(cxt
, types
);
724 } else if (argc
== 0 && (mnt_context_get_source(cxt
) ||
725 mnt_context_get_target(cxt
))) {
727 * B) mount -L|-U|--source|--target
729 * non-root may specify source *or* target, but not both
731 if (mnt_context_is_restricted(cxt
) &&
732 mnt_context_get_source(cxt
) &&
733 mnt_context_get_target(cxt
))
736 } else if (argc
== 1 && (!mnt_context_get_source(cxt
) ||
737 !mnt_context_get_target(cxt
))) {
739 * C) mount [-L|-U|--source] <target>
740 * mount [--target <dir>] <source>
741 * mount <source|target>
743 * non-root may specify source *or* target, but not both
745 * It does not matter for libmount if we set source or target
746 * here (the library is able to swap it), but it matters for
749 int istag
= mnt_tag_is_valid(argv
[0]);
751 if (istag
&& mnt_context_get_source(cxt
))
752 /* -L, -U or --source together with LABEL= or UUID= */
753 errx(MNT_EX_USAGE
, _("source specified more than once"));
754 else if (istag
|| mnt_context_get_target(cxt
))
755 mnt_context_set_source(cxt
, argv
[0]);
757 mnt_context_set_target(cxt
, argv
[0]);
759 if (mnt_context_is_restricted(cxt
) &&
760 mnt_context_get_source(cxt
) &&
761 mnt_context_get_target(cxt
))
764 } else if (argc
== 2 && !mnt_context_get_source(cxt
)
765 && !mnt_context_get_target(cxt
)) {
767 * D) mount <source> <target>
769 if (mnt_context_is_restricted(cxt
))
772 mnt_context_set_source(cxt
, argv
[0]);
773 mnt_context_set_target(cxt
, argv
[1]);
778 if (mnt_context_is_restricted(cxt
))
782 /* BIND/MOVE operations, let's set the mount flags */
783 mnt_context_set_mflags(cxt
, oper
);
785 if ((oper
&& !has_remount_flag(cxt
)) || propa
)
786 /* For --make-* or --bind is fstab/mtab unnecessary */
787 mnt_context_set_optsmode(cxt
, MNT_OMODE_NOTAB
);
789 rc
= mnt_context_mount(cxt
);
790 rc
= mk_exit_code(cxt
, rc
);
792 if (rc
== MNT_EX_SUCCESS
&& mnt_context_is_verbose(cxt
))
793 success_message(cxt
);
795 mnt_free_context(cxt
);