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"
41 #include "pathnames.h"
43 #define XALLOC_EXIT_CODE MNT_EX_SYSERR
46 #define OPTUTILS_EXIT_CODE MNT_EX_USAGE
49 static struct ul_env_list
*envs_removed
;
51 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
);
53 static void suid_drop(struct libmnt_context
*cxt
)
55 const uid_t ruid
= getuid();
56 const uid_t euid
= geteuid();
58 if (ruid
!= 0 && euid
== 0 && drop_permissions() != 0)
59 err(MNT_EX_FAIL
, _("drop permissions failed"));
61 /* be paranoid and check it, setuid(0) has to fail */
62 if (ruid
!= 0 && setuid(0) == 0)
63 errx(MNT_EX_FAIL
, _("drop permissions failed."));
65 mnt_context_force_unrestricted(cxt
);
67 /* restore "bad" environment variables */
69 env_list_setenv(envs_removed
);
70 env_list_free(envs_removed
);
75 static void __attribute__((__noreturn__
)) mount_print_version(void)
77 const char *ver
= NULL
;
78 const char **features
= NULL
, **p
;
80 mnt_get_library_version(&ver
);
81 mnt_get_library_features(&features
);
83 printf(_("%s from %s (libmount %s"),
84 program_invocation_short_name
,
89 fputs(p
== features
? ": " : ", ", stdout
);
96 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
97 const char *filename
, int line
)
100 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
105 * Replace control chars with '?' to be compatible with coreutils. For more
106 * robust solution use findmnt(1) where we use \x?? hex encoding.
108 static void safe_fputs(const char *data
)
112 for (p
= data
; p
&& *p
; p
++) {
113 if (iscntrl((unsigned char) *p
))
120 static void print_all(struct libmnt_context
*cxt
, char *pattern
, int show_label
)
122 struct libmnt_table
*tb
;
123 struct libmnt_iter
*itr
= NULL
;
124 struct libmnt_fs
*fs
;
125 struct libmnt_cache
*cache
= NULL
;
127 mnt_context_enable_noautofs(cxt
, 1);
129 if (mnt_context_get_mtab(cxt
, &tb
))
130 err(MNT_EX_SYSERR
, _("failed to read mtab"));
132 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
134 err(MNT_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_regularfs(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
= MNT_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 MNT_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
) == MNT_EX_SUCCESS
) {
202 /* Note that MNT_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
= MNT_EX_SUCCESS
; /* all success */
227 rc
= MNT_EX_FAIL
; /* all failed */
229 rc
= MNT_EX_SOMEOK
; /* some success, some failed */
237 * mount -a -o remount
239 static int remount_all(struct libmnt_context
*cxt
)
241 struct libmnt_iter
*itr
;
242 struct libmnt_fs
*fs
;
243 int mntrc
, ignored
, rc
= MNT_EX_SUCCESS
;
245 int nsucc
= 0, nerrs
= 0;
247 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
249 warn(_("failed to initialize libmount iterator"));
250 return MNT_EX_SYSERR
;
253 while (mnt_context_next_remount(cxt
, itr
, &fs
, &mntrc
, &ignored
) == 0) {
255 const char *tgt
= mnt_fs_get_target(fs
);
258 if (mnt_context_is_verbose(cxt
))
259 printf(_("%-25s: ignored\n"), tgt
);
261 if (mk_exit_code(cxt
, mntrc
) == MNT_EX_SUCCESS
) {
264 /* Note that MNT_EX_SUCCESS return code does
265 * not mean that FS has been really mounted
266 * (e.g. nofail option) */
267 if (mnt_context_get_status(cxt
)
268 && mnt_context_is_verbose(cxt
))
269 printf("%-25s: successfully remounted\n", tgt
);
276 rc
= MNT_EX_SUCCESS
; /* all success */
278 rc
= MNT_EX_FAIL
; /* all failed */
280 rc
= MNT_EX_SOMEOK
; /* some success, some failed */
286 static void success_message(struct libmnt_context
*cxt
)
288 unsigned long mflags
= 0;
289 const char *tgt
, *src
, *pr
= program_invocation_short_name
;
291 if (mnt_context_helper_executed(cxt
)
292 || mnt_context_get_status(cxt
) != 1)
295 mnt_context_get_mflags(cxt
, &mflags
);
296 tgt
= mnt_context_get_target(cxt
);
297 src
= mnt_context_get_source(cxt
);
299 if (mflags
& MS_MOVE
)
300 printf(_("%s: %s moved to %s.\n"), pr
, src
, tgt
);
301 else if (mflags
& MS_BIND
)
302 printf(_("%s: %s bound on %s.\n"), pr
, src
, tgt
);
303 else if (mflags
& MS_PROPAGATION
) {
304 if (src
&& strcmp(src
, "none") != 0 && tgt
)
305 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
307 printf(_("%s: %s propagation flags changed.\n"), pr
, tgt
);
309 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
312 #if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
313 # include <selinux/selinux.h>
314 # include <selinux/context.h>
316 static void selinux_warning(struct libmnt_context
*cxt
, const char *tgt
)
319 if (tgt
&& mnt_context_is_verbose(cxt
) && is_selinux_enabled() > 0) {
320 char *raw
= NULL
, *def
= NULL
;
322 if (getfilecon(tgt
, &raw
) > 0
323 && security_get_initial_context("file", &def
) == 0) {
325 if (!selinux_file_context_cmp(raw
, def
))
327 "mount: %s does not contain SELinux labels.\n"
328 " You just mounted a file system that supports labels which does not\n"
329 " contain labels, onto an SELinux box. It is likely that confined\n"
330 " applications will generate AVC messages and not be allowed access to\n"
331 " this file system. For more details see restorecon(8) and mount(8).\n"),
339 # define selinux_warning(_x, _y)
344 static void systemd_hint(void)
346 static int fstab_check_done
= 0;
348 if (fstab_check_done
== 0) {
351 if (isatty(STDERR_FILENO
) &&
352 stat(_PATH_SD_UNITSLOAD
, &a
) == 0 &&
353 stat(_PATH_MNTTAB
, &b
) == 0 &&
354 cmp_stat_mtime(&a
, &b
, <))
356 "mount: (hint) your fstab has been modified, but systemd still uses\n"
357 " the old version; use 'systemctl daemon-reload' to reload.\n"));
359 fstab_check_done
= 1;
363 # define systemd_hint()
368 * Returns exit status (MNT_EX_*) and/or prints error message.
370 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
)
373 char buf
[BUFSIZ
] = { 0 };
375 rc
= mnt_context_get_excode(cxt
, rc
, buf
, sizeof(buf
));
376 tgt
= mnt_context_get_target(cxt
);
379 const char *spec
= tgt
;
381 spec
= mnt_context_get_source(cxt
);
384 warnx("%s: %s.", spec
, buf
);
386 if (mnt_context_syscall_called(cxt
) &&
387 mnt_context_get_syscall_errno(cxt
) != 0)
388 fprintf(stderr
, _(" dmesg(1) may have more information after failed mount system call.\n"));
391 if (rc
== MNT_EX_SUCCESS
&& mnt_context_get_status(cxt
) == 1) {
392 selinux_warning(cxt
, tgt
);
400 static struct libmnt_table
*append_fstab(struct libmnt_context
*cxt
,
401 struct libmnt_table
*fstab
,
406 fstab
= mnt_new_table();
408 err(MNT_EX_SYSERR
, _("failed to initialize libmount table"));
410 mnt_table_set_parser_errcb(fstab
, table_parser_errcb
);
411 mnt_context_set_fstab(cxt
, fstab
);
413 mnt_unref_table(fstab
); /* reference is handled by @cxt now */
416 if (mnt_table_parse_fstab(fstab
, path
))
417 errx(MNT_EX_USAGE
,_("%s: failed to parse"), path
);
423 * Check source and target paths -- non-root user should not be able to
424 * resolve paths which are unreadable for them.
426 static int sanitize_paths(struct libmnt_context
*cxt
)
429 struct libmnt_fs
*fs
= mnt_context_get_fs(cxt
);
434 p
= mnt_fs_get_target(fs
);
436 char *np
= canonicalize_path_restricted(p
);
439 mnt_fs_set_target(fs
, np
);
443 p
= mnt_fs_get_srcpath(fs
);
445 char *np
= canonicalize_path_restricted(p
);
448 mnt_fs_set_source(fs
, np
);
454 static void append_option(struct libmnt_context
*cxt
, const char *opt
, const char *arg
)
458 if (opt
&& (*opt
== '=' || *opt
== '\'' || *opt
== '\"' || isblank(*opt
)))
459 errx(MNT_EX_USAGE
, _("unsupported option format: %s"), opt
);
462 xasprintf(&o
, "%s=\"%s\"", opt
, arg
);
464 if (mnt_context_append_options(cxt
, o
? : opt
))
465 err(MNT_EX_SYSERR
, _("failed to append option '%s'"), o
? : opt
);
470 static int has_remount_flag(struct libmnt_context
*cxt
)
472 unsigned long mflags
= 0;
474 if (mnt_context_get_mflags(cxt
, &mflags
))
477 return mflags
& MS_REMOUNT
;
480 static void __attribute__((__noreturn__
)) usage(void)
484 fputs(USAGE_HEADER
, out
);
487 " %1$s -a [options]\n"
488 " %1$s [options] [--source] <source> | [--target] <directory>\n"
489 " %1$s [options] <source> <directory>\n"
490 " %1$s <operation> <mountpoint> [<target>]\n"),
491 program_invocation_short_name
);
493 fputs(USAGE_SEPARATOR
, out
);
494 fputs(_("Mount a filesystem.\n"), out
);
496 fputs(USAGE_OPTIONS
, out
);
497 fputs(_(" -a, --all mount all filesystems mentioned in fstab\n"), out
);
498 fputs(_(" -c, --no-canonicalize don't canonicalize paths\n"), out
);
499 fputs(_(" -f, --fake dry run; skip the mount(2) syscall\n"), out
);
500 fputs(_(" -F, --fork fork off for each device (use with -a)\n"), out
);
501 fputs(_(" -T, --fstab <path> alternative file to /etc/fstab\n"), out
);
502 fputs(_(" -i, --internal-only don't call the mount.<type> helpers\n"), out
);
503 fputs(_(" -l, --show-labels show also filesystem labels\n"), out
);
504 fputs(_(" --map-groups <inner>:<outer>:<count>\n"
505 " add the specified GID map to an ID-mapped mount\n"), out
);
506 fputs(_(" --map-users <inner>:<outer>:<count>\n"
507 " add the specified UID map to an ID-mapped mount\n"), out
);
508 fputs(_(" --map-users /proc/<pid>/ns/user\n"
509 " specify the user namespace for an ID-mapped mount\n"), out
);
510 fputs(_(" -m, --mkdir[=<mode>] alias to '-o X-mount.mkdir[=<mode>]'\n"), out
);
511 fputs(_(" -n, --no-mtab don't write to /etc/mtab\n"), out
);
512 fputs(_(" --options-mode <mode>\n"
513 " what to do with options loaded from fstab\n"), out
);
514 fputs(_(" --options-source <source>\n"
515 " mount options source\n"), out
);
516 fputs(_(" --options-source-force\n"
517 " force use of options from fstab/mtab\n"), out
);
518 fputs(_(" --onlyonce check if filesystem is already mounted\n"), out
);
519 fputs(_(" -o, --options <list> comma-separated list of mount options\n"), out
);
520 fputs(_(" -O, --test-opts <list> limit the set of filesystems (use with -a)\n"), out
);
521 fputs(_(" -r, --read-only mount the filesystem read-only (same as -o ro)\n"), out
);
522 fputs(_(" -t, --types <list> limit the set of filesystem types\n"), out
);
523 fputs(_(" --source <src> explicitly specifies source (path, label, uuid)\n"), out
);
524 fputs(_(" --target <target> explicitly specifies mountpoint\n"), out
);
525 fputs(_(" --target-prefix <path>\n"
526 " specifies path used for all mountpoints\n"), out
);
527 fputs(_(" -v, --verbose say what is being done\n"), out
);
528 fputs(_(" -w, --rw, --read-write mount the filesystem read-write (default)\n"), out
);
529 fputs(_(" -N, --namespace <ns> perform mount in another namespace\n"), out
);
531 fputs(USAGE_SEPARATOR
, out
);
532 fprintf(out
, USAGE_HELP_OPTIONS(25));
534 fputs(USAGE_SEPARATOR
, out
);
535 fputs(_("Source:\n"), out
);
536 fputs(_(" -L, --label <label> synonym for LABEL=<label>\n"), out
);
537 fputs(_(" -U, --uuid <uuid> synonym for UUID=<uuid>\n"), out
);
538 fputs(_(" LABEL=<label> specifies device by filesystem label\n"), out
);
539 fputs(_(" UUID=<uuid> specifies device by filesystem UUID\n"), out
);
540 fputs(_(" PARTLABEL=<label> specifies device by partition label\n"), out
);
541 fputs(_(" PARTUUID=<uuid> specifies device by partition UUID\n"), out
);
542 fputs(_(" ID=<id> specifies device by udev hardware ID\n"), out
);
543 fputs(_(" <device> specifies device by path\n"), out
);
544 fputs(_(" <directory> mountpoint for bind mounts (see --bind/rbind)\n"), out
);
545 fputs(_(" <file> regular file for loopdev setup\n"), out
);
547 fputs(USAGE_SEPARATOR
, out
);
548 fputs(_("Operations:\n"), out
);
549 fputs(_(" -B, --bind mount a subtree somewhere else (same as -o bind)\n"), out
);
550 fputs(_(" -M, --move move a subtree to some other place\n"), out
);
551 fputs(_(" -R, --rbind mount a subtree and all submounts somewhere else\n"), out
);
552 fputs(_(" --make-shared mark a subtree as shared\n"), out
);
553 fputs(_(" --make-slave mark a subtree as slave\n"), out
);
554 fputs(_(" --make-private mark a subtree as private\n"), out
);
555 fputs(_(" --make-unbindable mark a subtree as unbindable\n"), out
);
556 fputs(_(" --make-rshared recursively mark a whole subtree as shared\n"), out
);
557 fputs(_(" --make-rslave recursively mark a whole subtree as slave\n"), out
);
558 fputs(_(" --make-rprivate recursively mark a whole subtree as private\n"), out
);
559 fputs(_(" --make-runbindable recursively mark a whole subtree as unbindable\n"), out
);
561 fprintf(out
, USAGE_MAN_TAIL("mount(8)"));
563 exit(MNT_EX_SUCCESS
);
571 static int omode2mask(const char *str
)
575 static const struct flag_str flags
[] = {
576 { MNT_OMODE_IGNORE
, "ignore" },
577 { MNT_OMODE_APPEND
, "append" },
578 { MNT_OMODE_PREPEND
, "prepend" },
579 { MNT_OMODE_REPLACE
, "replace" },
582 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++) {
583 if (!strcmp(str
, flags
[i
].str
))
584 return flags
[i
].value
;
589 static long osrc2mask(const char *str
, size_t len
)
593 static const struct flag_str flags
[] = {
594 { MNT_OMODE_FSTAB
, "fstab" },
595 { MNT_OMODE_MTAB
, "mtab" },
596 { MNT_OMODE_NOTAB
, "disable" },
599 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++) {
600 if (!strncmp(str
, flags
[i
].str
, len
) && !flags
[i
].str
[len
])
601 return flags
[i
].value
;
606 static pid_t
parse_pid(const char *str
)
612 ret
= strtoul(str
, &end
, 10);
614 if (ret
< 0 || errno
|| end
== str
|| (end
&& *end
))
619 int main(int argc
, char **argv
)
621 int c
, rc
= MNT_EX_SUCCESS
, all
= 0, show_labels
= 0;
622 struct libmnt_context
*cxt
;
623 struct libmnt_table
*fstab
= NULL
;
627 int oper
= 0, is_move
= 0;
629 int optmode
= 0, optmode_mode
= 0, optmode_src
= 0;
632 MOUNT_OPT_SHARED
= CHAR_MAX
+ 1,
635 MOUNT_OPT_UNBINDABLE
,
639 MOUNT_OPT_RUNBINDABLE
,
640 MOUNT_OPT_MAP_GROUPS
,
643 MOUNT_OPT_TARGET_PREFIX
,
647 MOUNT_OPT_OPTSRC_FORCE
,
651 static const struct option longopts
[] = {
652 { "all", no_argument
, NULL
, 'a' },
653 { "fake", no_argument
, NULL
, 'f' },
654 { "fstab", required_argument
, NULL
, 'T' },
655 { "fork", no_argument
, NULL
, 'F' },
656 { "help", no_argument
, NULL
, 'h' },
657 { "no-mtab", no_argument
, NULL
, 'n' },
658 { "read-only", no_argument
, NULL
, 'r' },
659 { "ro", no_argument
, NULL
, 'r' },
660 { "verbose", no_argument
, NULL
, 'v' },
661 { "version", no_argument
, NULL
, 'V' },
662 { "read-write", no_argument
, NULL
, 'w' },
663 { "rw", no_argument
, NULL
, 'w' },
664 { "options", required_argument
, NULL
, 'o' },
665 { "test-opts", required_argument
, NULL
, 'O' },
666 { "types", required_argument
, NULL
, 't' },
667 { "uuid", required_argument
, NULL
, 'U' },
668 { "label", required_argument
, NULL
, 'L' },
669 { "bind", no_argument
, NULL
, 'B' },
670 { "move", no_argument
, NULL
, 'M' },
671 { "rbind", no_argument
, NULL
, 'R' },
672 { "make-shared", no_argument
, NULL
, MOUNT_OPT_SHARED
},
673 { "make-slave", no_argument
, NULL
, MOUNT_OPT_SLAVE
},
674 { "make-private", no_argument
, NULL
, MOUNT_OPT_PRIVATE
},
675 { "make-unbindable", no_argument
, NULL
, MOUNT_OPT_UNBINDABLE
},
676 { "make-rshared", no_argument
, NULL
, MOUNT_OPT_RSHARED
},
677 { "make-rslave", no_argument
, NULL
, MOUNT_OPT_RSLAVE
},
678 { "make-rprivate", no_argument
, NULL
, MOUNT_OPT_RPRIVATE
},
679 { "make-runbindable", no_argument
, NULL
, MOUNT_OPT_RUNBINDABLE
},
680 { "map-groups", required_argument
, NULL
, MOUNT_OPT_MAP_GROUPS
},
681 { "map-users", required_argument
, NULL
, MOUNT_OPT_MAP_USERS
},
682 { "mkdir", optional_argument
, NULL
, 'm' },
683 { "no-canonicalize", no_argument
, NULL
, 'c' },
684 { "internal-only", no_argument
, NULL
, 'i' },
685 { "show-labels", no_argument
, NULL
, 'l' },
686 { "target", required_argument
, NULL
, MOUNT_OPT_TARGET
},
687 { "target-prefix", required_argument
, NULL
, MOUNT_OPT_TARGET_PREFIX
},
688 { "source", required_argument
, NULL
, MOUNT_OPT_SOURCE
},
689 { "onlyonce", no_argument
, NULL
, MOUNT_OPT_ONLYONCE
},
690 { "options-mode", required_argument
, NULL
, MOUNT_OPT_OPTMODE
},
691 { "options-source", required_argument
, NULL
, MOUNT_OPT_OPTSRC
},
692 { "options-source-force", no_argument
, NULL
, MOUNT_OPT_OPTSRC_FORCE
},
693 { "namespace", required_argument
, NULL
, 'N' },
697 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
698 { 'B','M','R' }, /* bind,move,rbind */
699 { 'L','U', MOUNT_OPT_SOURCE
}, /* label,uuid,source */
702 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
704 __sanitize_env(&envs_removed
);
705 setlocale(LC_ALL
, "");
706 bindtextdomain(PACKAGE
, LOCALEDIR
);
708 close_stdout_atexit();
710 strutils_set_exitcode(MNT_EX_USAGE
);
713 cxt
= mnt_new_context();
715 err(MNT_EX_SYSERR
, _("libmount context allocation failed"));
717 mnt_context_set_tables_errcb(cxt
, table_parser_errcb
);
719 while ((c
= getopt_long(argc
, argv
, "aBcfFhilL:m::Mno:O:rRsU:vVwt:T:N:",
720 longopts
, NULL
)) != -1) {
722 /* only few options are allowed for non-root users */
723 if (mnt_context_is_restricted(cxt
) &&
724 !strchr("hlLUVvrist", c
) &&
725 c
!= MOUNT_OPT_TARGET
&&
726 c
!= MOUNT_OPT_SOURCE
)
729 err_exclusive_options(c
, longopts
, excl
, excl_st
);
736 mnt_context_disable_canonicalize(cxt
, TRUE
);
739 mnt_context_enable_fake(cxt
, TRUE
);
742 mnt_context_enable_fork(cxt
, TRUE
);
745 mnt_context_disable_helpers(cxt
, TRUE
);
748 mnt_context_disable_mtab(cxt
, TRUE
);
751 append_option(cxt
, "ro", NULL
);
752 mnt_context_enable_rwonly_mount(cxt
, FALSE
);
755 mnt_context_enable_verbose(cxt
, TRUE
);
758 append_option(cxt
, "rw", NULL
);
759 mnt_context_enable_rwonly_mount(cxt
, TRUE
);
762 /* "move" is not supported as option string in libmount
763 * to avoid use in fstab */
764 if (mnt_optstr_get_option(optarg
, "move", NULL
, 0) == 0) {
765 char *o
= xstrdup(optarg
);
767 mnt_optstr_remove_option(&o
, "move");
769 append_option(cxt
, o
, NULL
);
773 append_option(cxt
, optarg
, NULL
);
776 if (mnt_context_set_options_pattern(cxt
, optarg
))
777 err(MNT_EX_SYSERR
, _("failed to set options pattern"));
780 xasprintf(&srcbuf
, "LABEL=\"%s\"", optarg
);
781 mnt_context_disable_swapmatch(cxt
, 1);
782 mnt_context_set_source(cxt
, srcbuf
);
786 xasprintf(&srcbuf
, "UUID=\"%s\"", optarg
);
787 mnt_context_disable_swapmatch(cxt
, 1);
788 mnt_context_set_source(cxt
, srcbuf
);
798 fstab
= append_fstab(cxt
, fstab
, optarg
);
801 mnt_context_enable_sloppy(cxt
, TRUE
);
805 append_option(cxt
, "bind", NULL
);
812 if (optarg
&& *optarg
== '=')
814 append_option(cxt
, "X-mount.mkdir", optarg
);
818 append_option(cxt
, "rbind", NULL
);
823 pid_t pid
= parse_pid(optarg
);
826 snprintf(path
, sizeof(path
), "/proc/%i/ns/mnt", pid
);
828 if (mnt_context_set_target_ns(cxt
, pid
? path
: optarg
))
829 err(MNT_EX_SYSERR
, _("failed to set target namespace to %s"), pid
? path
: optarg
);
832 case MOUNT_OPT_SHARED
:
833 append_option(cxt
, "shared", NULL
);
836 case MOUNT_OPT_SLAVE
:
837 append_option(cxt
, "slave", NULL
);
840 case MOUNT_OPT_PRIVATE
:
841 append_option(cxt
, "private", NULL
);
844 case MOUNT_OPT_UNBINDABLE
:
845 append_option(cxt
, "unbindable", NULL
);
848 case MOUNT_OPT_RSHARED
:
849 append_option(cxt
, "rshared", NULL
);
852 case MOUNT_OPT_RSLAVE
:
853 append_option(cxt
, "rslave", NULL
);
856 case MOUNT_OPT_RPRIVATE
:
857 append_option(cxt
, "rprivate", NULL
);
860 case MOUNT_OPT_RUNBINDABLE
:
861 append_option(cxt
, "runbindable", NULL
);
864 case MOUNT_OPT_MAP_GROUPS
:
865 case MOUNT_OPT_MAP_USERS
:
868 if (idmap
&& (*idmap
== '/' || *optarg
== '/')) {
869 warnx(_("bad usage"));
870 errtryhelp(MNT_EX_USAGE
);
871 } else if (*optarg
== '/') {
872 idmap
= xstrdup(optarg
);
875 xasprintf(&tmp
, "%s%s%s%s", idmap
? idmap
: "", idmap
? " " : "",
876 c
== MOUNT_OPT_MAP_GROUPS
? "g:" : "u:", optarg
);
881 case MOUNT_OPT_TARGET
:
882 mnt_context_disable_swapmatch(cxt
, 1);
883 mnt_context_set_target(cxt
, optarg
);
885 case MOUNT_OPT_TARGET_PREFIX
:
886 mnt_context_set_target_prefix(cxt
, optarg
);
888 case MOUNT_OPT_SOURCE
:
889 mnt_context_disable_swapmatch(cxt
, 1);
890 mnt_context_set_source(cxt
, optarg
);
892 case MOUNT_OPT_OPTMODE
:
893 optmode_mode
= omode2mask(optarg
);
894 if (optmode_mode
== -EINVAL
) {
895 warnx(_("bad usage"));
896 errtryhelp(MNT_EX_USAGE
);
899 case MOUNT_OPT_OPTSRC
:
901 unsigned long tmp
= 0;
902 if (string_to_bitmask(optarg
, &tmp
, osrc2mask
)) {
903 warnx(_("bad usage"));
904 errtryhelp(MNT_EX_USAGE
);
909 case MOUNT_OPT_OPTSRC_FORCE
:
910 optmode
|= MNT_OMODE_FORCE
;
912 case MOUNT_OPT_ONLYONCE
:
913 mnt_context_enable_onlyonce(cxt
, 1);
916 mnt_free_context(cxt
);
919 mnt_free_context(cxt
);
920 mount_print_version();
922 errtryhelp(MNT_EX_USAGE
);
930 append_option(cxt
, "X-mount.idmap", idmap
);
932 optmode
|= optmode_mode
| optmode_src
;
935 optmode
|= MNT_OMODE_PREPEND
;
937 optmode
|= MNT_OMODE_FSTAB
| MNT_OMODE_MTAB
;
938 mnt_context_set_optsmode(cxt
, optmode
);
941 if (fstab
&& !mnt_context_is_nocanonicalize(cxt
)) {
943 * We have external (context independent) fstab instance, let's
944 * make a connection between the fstab and the canonicalization
947 mnt_table_set_cache(fstab
, mnt_context_get_cache(cxt
));
950 if (!mnt_context_get_source(cxt
) &&
951 !mnt_context_get_target(cxt
) &&
954 if (oper
|| mnt_context_get_options(cxt
)) {
955 warnx(_("bad usage"));
956 errtryhelp(MNT_EX_USAGE
);
958 print_all(cxt
, types
, show_labels
);
962 /* Non-root users are allowed to use -t to print_all(),
964 if (mnt_context_is_restricted(cxt
) && types
)
967 if (oper
&& (types
|| all
|| mnt_context_get_source(cxt
))) {
968 warnx(_("bad usage"));
969 errtryhelp(MNT_EX_USAGE
);
972 if (types
&& (all
|| strchr(types
, ',') ||
973 strncmp(types
, "no", 2) == 0))
974 mnt_context_set_fstype_pattern(cxt
, types
);
976 mnt_context_set_fstype(cxt
, types
);
982 if (has_remount_flag(cxt
))
983 rc
= remount_all(cxt
);
988 } else if (argc
== 0 && (mnt_context_get_source(cxt
) ||
989 mnt_context_get_target(cxt
))) {
991 * B) mount -L|-U|--source|--target
993 * non-root may specify source *or* target, but not both
995 if (mnt_context_is_restricted(cxt
) &&
996 mnt_context_get_source(cxt
) &&
997 mnt_context_get_target(cxt
))
1000 } else if (argc
== 1 && (!mnt_context_get_source(cxt
) ||
1001 !mnt_context_get_target(cxt
))) {
1003 * C) mount [-L|-U|--source] <target>
1004 * mount [--target <dir>] <source>
1005 * mount <source|target>
1007 * non-root may specify source *or* target, but not both
1009 * It does not matter for libmount if we set source or target
1010 * here (the library is able to swap it), but it matters for
1013 int istag
= mnt_tag_is_valid(argv
[0]);
1015 if (istag
&& mnt_context_get_source(cxt
))
1016 /* -L, -U or --source together with LABEL= or UUID= */
1017 errx(MNT_EX_USAGE
, _("source specified more than once"));
1018 else if (istag
|| mnt_context_get_target(cxt
))
1019 mnt_context_set_source(cxt
, argv
[0]);
1021 mnt_context_set_target(cxt
, argv
[0]);
1023 if (mnt_context_is_restricted(cxt
) &&
1024 mnt_context_get_source(cxt
) &&
1025 mnt_context_get_target(cxt
))
1028 } else if (argc
== 2 && !mnt_context_get_source(cxt
)
1029 && !mnt_context_get_target(cxt
)) {
1031 * D) mount <source> <target>
1033 if (mnt_context_is_restricted(cxt
))
1036 mnt_context_set_source(cxt
, argv
[0]);
1037 mnt_context_set_target(cxt
, argv
[1]);
1040 warnx(_("bad usage"));
1041 errtryhelp(MNT_EX_USAGE
);
1044 if (mnt_context_is_restricted(cxt
) && sanitize_paths(cxt
) != 0)
1048 /* "move" as option string is not supported by libmount */
1049 mnt_context_set_mflags(cxt
, MS_MOVE
);
1051 if ((oper
&& !has_remount_flag(cxt
)) || propa
)
1052 /* For --make-* or --bind is fstab/mtab unnecessary */
1053 mnt_context_set_optsmode(cxt
, MNT_OMODE_NOTAB
);
1055 rc
= mnt_context_mount(cxt
);
1058 && mnt_context_is_restricted(cxt
)
1059 && !mnt_context_syscall_called(cxt
)) {
1060 /* Try it again without permissions */
1062 rc
= mnt_context_mount(cxt
);
1064 rc
= mk_exit_code(cxt
, rc
);
1066 if (rc
== MNT_EX_SUCCESS
&& mnt_context_is_verbose(cxt
))
1067 success_message(cxt
);
1069 mnt_free_context(cxt
);
1070 env_list_free(envs_removed
);