2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * mount(8) -- mount a filesystem
6 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
7 * Written by Karel Zak <kzak@redhat.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
20 #include <sys/types.h>
31 #include "closestream.h"
32 #include "canonicalize.h"
33 #include "pathnames.h"
36 #define XALLOC_EXIT_CODE MNT_EX_SYSERR
39 #define OPTUTILS_EXIT_CODE MNT_EX_USAGE
42 static struct ul_env_list
*envs_removed
;
44 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
);
46 static void suid_drop(struct libmnt_context
*cxt
)
48 const uid_t ruid
= getuid();
49 const uid_t euid
= geteuid();
51 if (ruid
!= 0 && euid
== 0 && drop_permissions() != 0)
52 err(MNT_EX_FAIL
, _("drop permissions failed"));
54 /* be paranoid and check it, setuid(0) has to fail */
55 if (ruid
!= 0 && setuid(0) == 0)
56 errx(MNT_EX_FAIL
, _("drop permissions failed."));
58 mnt_context_force_unrestricted(cxt
);
60 /* restore "bad" environment variables */
62 env_list_setenv(envs_removed
, 0);
63 env_list_free(envs_removed
);
68 static void __attribute__((__noreturn__
)) mount_print_version(void)
70 const char *ver
= NULL
;
71 const char **features
= NULL
, **p
;
73 mnt_get_library_version(&ver
);
74 mnt_get_library_features(&features
);
76 printf(_("%s from %s (libmount %s"),
77 program_invocation_short_name
,
82 fputs(p
== features
? ": " : ", ", stdout
);
89 static int table_parser_errcb(struct libmnt_table
*tb
__attribute__((__unused__
)),
90 const char *filename
, int line
)
93 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
98 * Replace control chars with '?' to be compatible with coreutils. For more
99 * robust solution use findmnt(1) where we use \x?? hex encoding.
101 static void safe_fputs(const char *data
)
105 for (p
= data
; p
&& *p
; p
++) {
106 if (iscntrl((unsigned char) *p
))
113 static void print_all(struct libmnt_context
*cxt
, char *pattern
, int show_label
)
115 struct libmnt_table
*tb
;
116 struct libmnt_iter
*itr
= NULL
;
117 struct libmnt_fs
*fs
;
118 struct libmnt_cache
*cache
= NULL
;
120 mnt_context_enable_noautofs(cxt
, 1);
122 if (mnt_context_get_mtab(cxt
, &tb
))
123 err(MNT_EX_SYSERR
, _("failed to read mtab"));
125 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
127 err(MNT_EX_SYSERR
, _("failed to initialize libmount iterator"));
129 cache
= mnt_new_cache();
131 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
132 const char *type
= mnt_fs_get_fstype(fs
);
133 const char *src
= mnt_fs_get_source(fs
);
134 const char *optstr
= mnt_fs_get_options(fs
);
137 if (type
&& pattern
&& !mnt_match_fstype(type
, pattern
))
140 if (mnt_fs_is_regularfs(fs
))
141 xsrc
= mnt_pretty_path(src
, cache
);
142 printf ("%s on ", xsrc
? xsrc
: src
);
143 safe_fputs(mnt_fs_get_target(fs
));
146 printf (" type %s", type
);
148 printf (" (%s)", optstr
);
149 if (show_label
&& src
) {
150 char *lb
= mnt_cache_find_tag_value(cache
, src
, "LABEL");
152 printf (" [%s]", lb
);
158 mnt_unref_cache(cache
);
165 static int mount_all(struct libmnt_context
*cxt
)
167 struct libmnt_iter
*itr
;
168 struct libmnt_fs
*fs
;
169 int mntrc
, ignored
, rc
= MNT_EX_SUCCESS
;
171 int nsucc
= 0, nerrs
= 0;
173 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
175 warn(_("failed to initialize libmount iterator"));
176 return MNT_EX_SYSERR
;
179 while (mnt_context_next_mount(cxt
, itr
, &fs
, &mntrc
, &ignored
) == 0) {
181 const char *tgt
= mnt_fs_get_target(fs
);
184 if (mnt_context_is_verbose(cxt
))
185 printf(ignored
== 1 ? _("%-25s: ignored\n") :
186 _("%-25s: already mounted\n"),
188 } else if (mnt_context_is_fork(cxt
)) {
189 if (mnt_context_is_verbose(cxt
))
190 printf("%-25s: mount successfully forked\n", tgt
);
192 if (mk_exit_code(cxt
, mntrc
) == MNT_EX_SUCCESS
) {
195 /* Note that MNT_EX_SUCCESS return code does
196 * not mean that FS has been really mounted
197 * (e.g. nofail option) */
198 if (mnt_context_get_status(cxt
)
199 && mnt_context_is_verbose(cxt
))
200 printf("%-25s: successfully mounted\n", tgt
);
206 if (mnt_context_is_parent(cxt
)) {
207 /* wait for mount --fork children */
210 nerrs
= 0, nsucc
= 0;
212 rc
= mnt_context_wait_for_children(cxt
, &nchildren
, &nerrs
);
213 if (!rc
&& nchildren
)
214 nsucc
= nchildren
- nerrs
;
218 rc
= MNT_EX_SUCCESS
; /* all success */
220 rc
= MNT_EX_FAIL
; /* all failed */
222 rc
= MNT_EX_SOMEOK
; /* some success, some failed */
230 * mount -a -o remount
232 static int remount_all(struct libmnt_context
*cxt
)
234 struct libmnt_iter
*itr
;
235 struct libmnt_fs
*fs
;
236 int mntrc
, ignored
, rc
= MNT_EX_SUCCESS
;
238 int nsucc
= 0, nerrs
= 0;
240 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
242 warn(_("failed to initialize libmount iterator"));
243 return MNT_EX_SYSERR
;
246 while (mnt_context_next_remount(cxt
, itr
, &fs
, &mntrc
, &ignored
) == 0) {
248 const char *tgt
= mnt_fs_get_target(fs
);
251 if (mnt_context_is_verbose(cxt
))
252 printf(_("%-25s: ignored\n"), tgt
);
254 if (mk_exit_code(cxt
, mntrc
) == MNT_EX_SUCCESS
) {
257 /* Note that MNT_EX_SUCCESS return code does
258 * not mean that FS has been really mounted
259 * (e.g. nofail option) */
260 if (mnt_context_get_status(cxt
)
261 && mnt_context_is_verbose(cxt
))
262 printf("%-25s: successfully remounted\n", tgt
);
269 rc
= MNT_EX_SUCCESS
; /* all success */
271 rc
= MNT_EX_FAIL
; /* all failed */
273 rc
= MNT_EX_SOMEOK
; /* some success, some failed */
279 static void success_message(struct libmnt_context
*cxt
)
281 unsigned long mflags
= 0;
282 const char *tgt
, *src
, *pr
= program_invocation_short_name
;
284 if (mnt_context_helper_executed(cxt
)
285 || mnt_context_get_status(cxt
) != 1)
288 mnt_context_get_mflags(cxt
, &mflags
);
289 tgt
= mnt_context_get_target(cxt
);
290 src
= mnt_context_get_source(cxt
);
292 if (mflags
& MS_MOVE
)
293 printf(_("%s: %s moved to %s.\n"), pr
, src
, tgt
);
294 else if (mflags
& MS_BIND
)
295 printf(_("%s: %s bound on %s.\n"), pr
, src
, tgt
);
296 else if (mflags
& MS_PROPAGATION
) {
297 if (src
&& strcmp(src
, "none") != 0 && tgt
)
298 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
300 printf(_("%s: %s propagation flags changed.\n"), pr
, tgt
);
302 printf(_("%s: %s mounted on %s.\n"), pr
, src
, tgt
);
305 #if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
306 # include <selinux/selinux.h>
307 # include <selinux/context.h>
309 static void selinux_warning(struct libmnt_context
*cxt
, const char *tgt
)
312 if (tgt
&& mnt_context_is_verbose(cxt
) && is_selinux_enabled() > 0) {
313 char *raw
= NULL
, *def
= NULL
;
315 if (getfilecon(tgt
, &raw
) > 0
316 && security_get_initial_context("file", &def
) == 0) {
318 if (!selinux_file_context_cmp(raw
, def
))
320 "mount: %s does not contain SELinux labels.\n"
321 " You just mounted a file system that supports labels which does not\n"
322 " contain labels, onto an SELinux box. It is likely that confined\n"
323 " applications will generate AVC messages and not be allowed access to\n"
324 " this file system. For more details see restorecon(8) and mount(8).\n"),
332 # define selinux_warning(_x, _y)
338 * Note that this mount(8) message may generate thousands of lines of output
339 * when mount(8) is called from any script in systems with large fstab, etc.
341 * The goal is to avoid spamming system logs (don't print on stderr) and hide
342 * the hint if stderr is redirected/piped (in this case mount(8) is probably
343 * executed in a script).
345 * The target audience is users on a terminal who directly use mount(8).
347 static void systemd_hint(void)
349 static int fstab_check_done
= 0;
351 if (fstab_check_done
== 0) {
354 if (isatty(STDERR_FILENO
) &&
355 isatty(STDOUT_FILENO
) &&
356 stat(_PATH_SD_UNITSLOAD
, &a
) == 0 &&
357 stat(_PATH_MNTTAB
, &b
) == 0 &&
358 cmp_stat_mtime(&a
, &b
, <))
360 "mount: (hint) your fstab has been modified, but systemd still uses\n"
361 " the old version; use 'systemctl daemon-reload' to reload.\n"));
363 fstab_check_done
= 1;
367 # define systemd_hint()
370 static size_t libmount_mesgs(struct libmnt_context
*cxt
, char type
)
372 size_t n
= mnt_context_get_nmesgs(cxt
, type
);
373 char **mesgs
= mnt_context_get_mesgs(cxt
);
382 fputs(P_("mount error:\n", "mount errors:\n", n
), stderr
);
385 fputs(P_("mount warning:\n", "mount warnings:\n", n
), stdout
);
388 fputs(P_("mount info:\n", "mount infos:\n", n
), stdout
);
393 UL_STRV_FOREACH(s
, mesgs
) {
396 if (!ul_startswith(*s
, "e "))
398 fprintf(stderr
, " * %s\n", (*s
) + 2);
401 if (!ul_startswith(*s
, "w "))
403 fprintf(stdout
, " * %s\n", (*s
) + 2);
406 if (!ul_startswith(*s
, "i "))
408 fprintf(stdout
, " * %s\n", (*s
) + 2);
417 * Returns exit status (MNT_EX_*) and/or prints error message.
419 static int mk_exit_code(struct libmnt_context
*cxt
, int rc
)
422 char buf
[BUFSIZ
] = { 0 };
424 rc
= mnt_context_get_excode(cxt
, rc
, buf
, sizeof(buf
));
425 tgt
= mnt_context_get_target(cxt
);
429 * Note that mnt_context_get_excode() is used for backward compatibility and
430 * will fill @buf with error messages from mnt_context_get_mesgs(). Therefore,
431 * calling libmount_mesgs(cxt, 'e') is currently unnecessary.
434 const char *spec
= tgt
;
436 spec
= mnt_context_get_source(cxt
);
439 warnx("%s: %s.", spec
, buf
);
441 if (mnt_context_syscall_called(cxt
) &&
442 mnt_context_get_syscall_errno(cxt
) != 0)
443 fprintf(stderr
, _(" dmesg(1) may have more information after failed mount system call.\n"));
446 /* warning messages */
447 libmount_mesgs(cxt
, 'w');
450 if (mnt_context_is_verbose(cxt
))
451 libmount_mesgs(cxt
, 'i');
453 /* extra mount(8) messages */
454 if (rc
== MNT_EX_SUCCESS
&& mnt_context_get_status(cxt
) == 1) {
455 selinux_warning(cxt
, tgt
);
463 static struct libmnt_table
*append_fstab(struct libmnt_context
*cxt
,
464 struct libmnt_table
*fstab
,
469 fstab
= mnt_new_table();
471 err(MNT_EX_SYSERR
, _("failed to initialize libmount table"));
473 mnt_table_set_parser_errcb(fstab
, table_parser_errcb
);
474 mnt_context_set_fstab(cxt
, fstab
);
476 mnt_unref_table(fstab
); /* reference is handled by @cxt now */
479 if (mnt_table_parse_fstab(fstab
, path
))
480 errx(MNT_EX_USAGE
,_("%s: failed to parse"), path
);
486 * Check source and target paths -- non-root user should not be able to
487 * resolve paths which are unreadable for them.
489 static int sanitize_paths(struct libmnt_context
*cxt
)
492 struct libmnt_fs
*fs
= mnt_context_get_fs(cxt
);
497 p
= mnt_fs_get_target(fs
);
499 char *np
= canonicalize_path_restricted(p
);
502 mnt_fs_set_target(fs
, np
);
506 p
= mnt_fs_get_srcpath(fs
);
508 char *np
= canonicalize_path_restricted(p
);
511 mnt_fs_set_source(fs
, np
);
517 static void append_option(struct libmnt_context
*cxt
, const char *opt
, const char *arg
)
521 if (opt
&& !ul_optstr_is_valid(opt
))
522 errx(MNT_EX_USAGE
, _("unsupported option format: %s"), opt
);
524 if (opt
&& arg
&& *arg
)
525 xasprintf(&o
, "%s=\"%s\"", opt
, arg
);
527 if (mnt_context_append_options(cxt
, o
? : opt
))
528 err(MNT_EX_SYSERR
, _("failed to append option '%s'"), o
? : opt
);
533 static int has_remount_flag(struct libmnt_context
*cxt
)
535 unsigned long mflags
= 0;
537 if (mnt_context_get_mflags(cxt
, &mflags
))
540 return mflags
& MS_REMOUNT
;
543 static void __attribute__((__noreturn__
)) usage(void)
547 fputs(USAGE_HEADER
, out
);
550 " %1$s -a [options]\n"
551 " %1$s [options] [--source] <source> | [--target] <directory>\n"
552 " %1$s [options] <source> <directory>\n"
553 " %1$s <operation> <mountpoint> [<target>]\n"),
554 program_invocation_short_name
);
556 fputs(USAGE_SEPARATOR
, out
);
557 fputs(_("Mount a filesystem.\n"), out
);
559 fputs(USAGE_OPTIONS
, out
);
560 fputs(_(" -a, --all mount all filesystems mentioned in fstab\n"), out
);
561 fputs(_(" -c, --no-canonicalize don't canonicalize paths\n"), out
);
562 fputs(_(" -f, --fake dry run; skip the mount(2) syscall\n"), out
);
563 fputs(_(" -F, --fork fork off for each device (use with -a)\n"), out
);
564 fputs(_(" -T, --fstab <path> alternative file to /etc/fstab\n"), out
);
565 fputs(_(" -i, --internal-only don't call the mount.<type> helpers\n"), out
);
566 fputs(_(" -l, --show-labels show also filesystem labels\n"), out
);
567 fputs(_(" --map-groups <inner>:<outer>:<count>\n"
568 " add the specified GID map to an ID-mapped mount\n"), out
);
569 fputs(_(" --map-users <inner>:<outer>:<count>\n"
570 " add the specified UID map to an ID-mapped mount\n"), out
);
571 fputs(_(" --map-users /proc/<pid>/ns/user\n"
572 " specify the user namespace for an ID-mapped mount\n"), out
);
573 fputs(_(" -m, --mkdir[=<mode>] alias to '-o X-mount.mkdir[=<mode>]'\n"), out
);
574 fputs(_(" -n, --no-mtab don't write to /etc/mtab\n"), out
);
575 fputs(_(" --options-mode <mode>\n"
576 " what to do with options loaded from fstab\n"), out
);
577 fputs(_(" --options-source <source>\n"
578 " mount options source\n"), out
);
579 fputs(_(" --options-source-force\n"
580 " force use of options from fstab/mtab\n"), out
);
581 fputs(_(" --onlyonce check if filesystem is already mounted\n"), out
);
582 fputs(_(" -o, --options <list> comma-separated list of mount options\n"), out
);
583 fputs(_(" -O, --test-opts <list> limit the set of filesystems (use with -a)\n"), out
);
584 fputs(_(" -r, --read-only mount the filesystem read-only (same as -o ro)\n"), out
);
585 fputs(_(" -t, --types <list> limit the set of filesystem types\n"), out
);
586 fputs(_(" --source <src> explicitly specifies source (path, label, uuid)\n"), out
);
587 fputs(_(" --target <target> explicitly specifies mountpoint\n"), out
);
588 fputs(_(" --target-prefix <path>\n"
589 " specifies path used for all mountpoints\n"), out
);
590 fputs(_(" -v, --verbose say what is being done\n"), out
);
591 fputs(_(" -w, --rw, --read-write mount the filesystem read-write (default)\n"), out
);
592 fputs(_(" -N, --namespace <ns> perform mount in another namespace\n"), out
);
594 fputs(USAGE_SEPARATOR
, out
);
595 fprintf(out
, USAGE_HELP_OPTIONS(25));
597 fputs(USAGE_SEPARATOR
, out
);
598 fputs(_("Source:\n"), out
);
599 fputs(_(" -L, --label <label> synonym for LABEL=<label>\n"), out
);
600 fputs(_(" -U, --uuid <uuid> synonym for UUID=<uuid>\n"), out
);
601 fputs(_(" LABEL=<label> specifies device by filesystem label\n"), out
);
602 fputs(_(" UUID=<uuid> specifies device by filesystem UUID\n"), out
);
603 fputs(_(" PARTLABEL=<label> specifies device by partition label\n"), out
);
604 fputs(_(" PARTUUID=<uuid> specifies device by partition UUID\n"), out
);
605 fputs(_(" ID=<id> specifies device by udev hardware ID\n"), out
);
606 fputs(_(" <device> specifies device by path\n"), out
);
607 fputs(_(" <directory> mountpoint for bind mounts (see --bind/rbind)\n"), out
);
608 fputs(_(" <file> regular file for loopdev setup\n"), out
);
610 fputs(USAGE_SEPARATOR
, out
);
611 fputs(_("Operations:\n"), out
);
612 fputs(_(" -B, --bind mount a subtree somewhere else (same as -o bind)\n"), out
);
613 fputs(_(" -M, --move move a subtree to some other place\n"), out
);
614 fputs(_(" -R, --rbind mount a subtree and all submounts somewhere else\n"), out
);
615 fputs(_(" --make-shared mark a subtree as shared\n"), out
);
616 fputs(_(" --make-slave mark a subtree as slave\n"), out
);
617 fputs(_(" --make-private mark a subtree as private\n"), out
);
618 fputs(_(" --make-unbindable mark a subtree as unbindable\n"), out
);
619 fputs(_(" --make-rshared recursively mark a whole subtree as shared\n"), out
);
620 fputs(_(" --make-rslave recursively mark a whole subtree as slave\n"), out
);
621 fputs(_(" --make-rprivate recursively mark a whole subtree as private\n"), out
);
622 fputs(_(" --make-runbindable recursively mark a whole subtree as unbindable\n"), out
);
624 fprintf(out
, USAGE_MAN_TAIL("mount(8)"));
626 exit(MNT_EX_SUCCESS
);
634 static int omode2mask(const char *str
)
638 static const struct flag_str flags
[] = {
639 { MNT_OMODE_IGNORE
, "ignore" },
640 { MNT_OMODE_APPEND
, "append" },
641 { MNT_OMODE_PREPEND
, "prepend" },
642 { MNT_OMODE_REPLACE
, "replace" },
645 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++) {
646 if (!strcmp(str
, flags
[i
].str
))
647 return flags
[i
].value
;
652 static long osrc2mask(const char *str
, size_t len
)
656 static const struct flag_str flags
[] = {
657 { MNT_OMODE_FSTAB
, "fstab" },
658 { MNT_OMODE_MTAB
, "mtab" },
659 { MNT_OMODE_NOTAB
, "disable" },
662 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++) {
663 if (!strncmp(str
, flags
[i
].str
, len
) && !flags
[i
].str
[len
])
664 return flags
[i
].value
;
669 static pid_t
parse_pid(const char *str
)
675 ret
= strtoul(str
, &end
, 10);
677 if (ret
< 0 || errno
|| end
== str
|| (end
&& *end
))
682 int main(int argc
, char **argv
)
684 int c
, rc
= MNT_EX_SUCCESS
, all
= 0, show_labels
= 0;
685 struct libmnt_context
*cxt
;
686 struct libmnt_table
*fstab
= NULL
;
690 int oper
= 0, is_move
= 0;
692 int optmode
= 0, optmode_mode
= 0, optmode_src
= 0;
695 MOUNT_OPT_SHARED
= CHAR_MAX
+ 1,
698 MOUNT_OPT_UNBINDABLE
,
702 MOUNT_OPT_RUNBINDABLE
,
703 MOUNT_OPT_MAP_GROUPS
,
706 MOUNT_OPT_TARGET_PREFIX
,
710 MOUNT_OPT_OPTSRC_FORCE
,
714 static const struct option longopts
[] = {
715 { "all", no_argument
, NULL
, 'a' },
716 { "fake", no_argument
, NULL
, 'f' },
717 { "fstab", required_argument
, NULL
, 'T' },
718 { "fork", no_argument
, NULL
, 'F' },
719 { "help", no_argument
, NULL
, 'h' },
720 { "no-mtab", no_argument
, NULL
, 'n' },
721 { "read-only", no_argument
, NULL
, 'r' },
722 { "ro", no_argument
, NULL
, 'r' },
723 { "verbose", no_argument
, NULL
, 'v' },
724 { "version", no_argument
, NULL
, 'V' },
725 { "read-write", no_argument
, NULL
, 'w' },
726 { "rw", no_argument
, NULL
, 'w' },
727 { "options", required_argument
, NULL
, 'o' },
728 { "test-opts", required_argument
, NULL
, 'O' },
729 { "types", required_argument
, NULL
, 't' },
730 { "uuid", required_argument
, NULL
, 'U' },
731 { "label", required_argument
, NULL
, 'L' },
732 { "bind", no_argument
, NULL
, 'B' },
733 { "move", no_argument
, NULL
, 'M' },
734 { "rbind", no_argument
, NULL
, 'R' },
735 { "make-shared", no_argument
, NULL
, MOUNT_OPT_SHARED
},
736 { "make-slave", no_argument
, NULL
, MOUNT_OPT_SLAVE
},
737 { "make-private", no_argument
, NULL
, MOUNT_OPT_PRIVATE
},
738 { "make-unbindable", no_argument
, NULL
, MOUNT_OPT_UNBINDABLE
},
739 { "make-rshared", no_argument
, NULL
, MOUNT_OPT_RSHARED
},
740 { "make-rslave", no_argument
, NULL
, MOUNT_OPT_RSLAVE
},
741 { "make-rprivate", no_argument
, NULL
, MOUNT_OPT_RPRIVATE
},
742 { "make-runbindable", no_argument
, NULL
, MOUNT_OPT_RUNBINDABLE
},
743 { "map-groups", required_argument
, NULL
, MOUNT_OPT_MAP_GROUPS
},
744 { "map-users", required_argument
, NULL
, MOUNT_OPT_MAP_USERS
},
745 { "mkdir", optional_argument
, NULL
, 'm' },
746 { "no-canonicalize", no_argument
, NULL
, 'c' },
747 { "internal-only", no_argument
, NULL
, 'i' },
748 { "show-labels", no_argument
, NULL
, 'l' },
749 { "target", required_argument
, NULL
, MOUNT_OPT_TARGET
},
750 { "target-prefix", required_argument
, NULL
, MOUNT_OPT_TARGET_PREFIX
},
751 { "source", required_argument
, NULL
, MOUNT_OPT_SOURCE
},
752 { "onlyonce", no_argument
, NULL
, MOUNT_OPT_ONLYONCE
},
753 { "options-mode", required_argument
, NULL
, MOUNT_OPT_OPTMODE
},
754 { "options-source", required_argument
, NULL
, MOUNT_OPT_OPTSRC
},
755 { "options-source-force", no_argument
, NULL
, MOUNT_OPT_OPTSRC_FORCE
},
756 { "namespace", required_argument
, NULL
, 'N' },
760 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
761 { 'B','M','R' }, /* bind,move,rbind */
762 { 'L','U', MOUNT_OPT_SOURCE
}, /* label,uuid,source */
765 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
767 __sanitize_env(&envs_removed
);
768 setlocale(LC_ALL
, "");
769 bindtextdomain(PACKAGE
, LOCALEDIR
);
771 close_stdout_atexit();
773 strutils_set_exitcode(MNT_EX_USAGE
);
776 cxt
= mnt_new_context();
778 err(MNT_EX_SYSERR
, _("libmount context allocation failed"));
780 mnt_context_set_tables_errcb(cxt
, table_parser_errcb
);
782 while ((c
= getopt_long(argc
, argv
, "aBcfFhilL:m::Mno:O:rRsU:vVwt:T:N:",
783 longopts
, NULL
)) != -1) {
785 /* only few options are allowed for non-root users */
786 if (mnt_context_is_restricted(cxt
) &&
787 !strchr("hlLUVvrist", c
) &&
788 c
!= MOUNT_OPT_TARGET
&&
789 c
!= MOUNT_OPT_SOURCE
)
792 err_exclusive_options(c
, longopts
, excl
, excl_st
);
799 mnt_context_disable_canonicalize(cxt
, TRUE
);
802 mnt_context_enable_fake(cxt
, TRUE
);
805 mnt_context_enable_fork(cxt
, TRUE
);
808 mnt_context_disable_helpers(cxt
, TRUE
);
811 mnt_context_disable_mtab(cxt
, TRUE
);
814 append_option(cxt
, "ro", NULL
);
815 mnt_context_enable_rwonly_mount(cxt
, FALSE
);
818 mnt_context_enable_verbose(cxt
, TRUE
);
821 append_option(cxt
, "rw", NULL
);
822 mnt_context_enable_rwonly_mount(cxt
, TRUE
);
825 /* "move" is not supported as option string in libmount
826 * to avoid use in fstab */
827 if (mnt_optstr_get_option(optarg
, "move", NULL
, 0) == 0) {
828 char *o
= xstrdup(optarg
);
830 mnt_optstr_remove_option(&o
, "move");
832 append_option(cxt
, o
, NULL
);
836 append_option(cxt
, optarg
, NULL
);
839 if (mnt_context_set_options_pattern(cxt
, optarg
))
840 err(MNT_EX_SYSERR
, _("failed to set options pattern"));
843 xasprintf(&srcbuf
, "LABEL=\"%s\"", optarg
);
844 mnt_context_disable_swapmatch(cxt
, 1);
845 mnt_context_set_source(cxt
, srcbuf
);
849 xasprintf(&srcbuf
, "UUID=\"%s\"", optarg
);
850 mnt_context_disable_swapmatch(cxt
, 1);
851 mnt_context_set_source(cxt
, srcbuf
);
861 fstab
= append_fstab(cxt
, fstab
, optarg
);
864 mnt_context_enable_sloppy(cxt
, TRUE
);
868 append_option(cxt
, "bind", NULL
);
875 if (optarg
&& *optarg
== '=')
877 append_option(cxt
, "X-mount.mkdir", optarg
);
881 append_option(cxt
, "rbind", NULL
);
886 pid_t pid
= parse_pid(optarg
);
889 snprintf(path
, sizeof(path
), "/proc/%i/ns/mnt", pid
);
891 if (mnt_context_set_target_ns(cxt
, pid
? path
: optarg
))
892 err(MNT_EX_SYSERR
, _("failed to set target namespace to %s"), pid
? path
: optarg
);
895 case MOUNT_OPT_SHARED
:
896 append_option(cxt
, "shared", NULL
);
899 case MOUNT_OPT_SLAVE
:
900 append_option(cxt
, "slave", NULL
);
903 case MOUNT_OPT_PRIVATE
:
904 append_option(cxt
, "private", NULL
);
907 case MOUNT_OPT_UNBINDABLE
:
908 append_option(cxt
, "unbindable", NULL
);
911 case MOUNT_OPT_RSHARED
:
912 append_option(cxt
, "rshared", NULL
);
915 case MOUNT_OPT_RSLAVE
:
916 append_option(cxt
, "rslave", NULL
);
919 case MOUNT_OPT_RPRIVATE
:
920 append_option(cxt
, "rprivate", NULL
);
923 case MOUNT_OPT_RUNBINDABLE
:
924 append_option(cxt
, "runbindable", NULL
);
927 case MOUNT_OPT_MAP_GROUPS
:
928 case MOUNT_OPT_MAP_USERS
:
931 if (idmap
&& (*idmap
== '/' || *optarg
== '/')) {
932 warnx(_("bad usage"));
933 errtryhelp(MNT_EX_USAGE
);
934 } else if (*optarg
== '/') {
935 idmap
= xstrdup(optarg
);
938 xasprintf(&tmp
, "%s%s%s%s", idmap
? idmap
: "", idmap
? " " : "",
939 c
== MOUNT_OPT_MAP_GROUPS
? "g:" : "u:", optarg
);
944 case MOUNT_OPT_TARGET
:
945 mnt_context_disable_swapmatch(cxt
, 1);
946 mnt_context_set_target(cxt
, optarg
);
948 case MOUNT_OPT_TARGET_PREFIX
:
949 mnt_context_set_target_prefix(cxt
, optarg
);
951 case MOUNT_OPT_SOURCE
:
952 mnt_context_disable_swapmatch(cxt
, 1);
953 mnt_context_set_source(cxt
, optarg
);
955 case MOUNT_OPT_OPTMODE
:
956 optmode_mode
= omode2mask(optarg
);
957 if (optmode_mode
== -EINVAL
) {
958 warnx(_("bad usage"));
959 errtryhelp(MNT_EX_USAGE
);
962 case MOUNT_OPT_OPTSRC
:
964 unsigned long tmp
= 0;
965 if (string_to_bitmask(optarg
, &tmp
, osrc2mask
)) {
966 warnx(_("bad usage"));
967 errtryhelp(MNT_EX_USAGE
);
972 case MOUNT_OPT_OPTSRC_FORCE
:
973 optmode
|= MNT_OMODE_FORCE
;
975 case MOUNT_OPT_ONLYONCE
:
976 mnt_context_enable_onlyonce(cxt
, 1);
979 mnt_free_context(cxt
);
982 mnt_free_context(cxt
);
983 mount_print_version();
985 errtryhelp(MNT_EX_USAGE
);
993 append_option(cxt
, "X-mount.idmap", idmap
);
995 optmode
|= optmode_mode
| optmode_src
;
998 optmode
|= MNT_OMODE_PREPEND
;
1000 optmode
|= MNT_OMODE_FSTAB
| MNT_OMODE_MTAB
;
1001 mnt_context_set_optsmode(cxt
, optmode
);
1004 if (fstab
&& !mnt_context_is_nocanonicalize(cxt
)) {
1006 * We have external (context independent) fstab instance, let's
1007 * make a connection between the fstab and the canonicalization
1010 mnt_table_set_cache(fstab
, mnt_context_get_cache(cxt
));
1013 if (!mnt_context_get_source(cxt
) &&
1014 !mnt_context_get_target(cxt
) &&
1017 if (oper
|| mnt_context_get_options(cxt
)) {
1018 warnx(_("bad usage"));
1019 errtryhelp(MNT_EX_USAGE
);
1021 print_all(cxt
, types
, show_labels
);
1025 /* Non-root users are allowed to use -t to print_all(),
1027 if (mnt_context_is_restricted(cxt
) && types
)
1030 if (oper
&& (types
|| all
|| mnt_context_get_source(cxt
))) {
1031 warnx(_("bad usage"));
1032 errtryhelp(MNT_EX_USAGE
);
1035 if (types
&& (all
|| strchr(types
, ',') ||
1036 strncmp(types
, "no", 2) == 0))
1037 mnt_context_set_fstype_pattern(cxt
, types
);
1039 mnt_context_set_fstype(cxt
, types
);
1045 if (has_remount_flag(cxt
))
1046 rc
= remount_all(cxt
);
1048 rc
= mount_all(cxt
);
1051 } else if (argc
== 0 && (mnt_context_get_source(cxt
) ||
1052 mnt_context_get_target(cxt
))) {
1054 * B) mount -L|-U|--source|--target
1056 * non-root may specify source *or* target, but not both
1058 if (mnt_context_is_restricted(cxt
) &&
1059 mnt_context_get_source(cxt
) &&
1060 mnt_context_get_target(cxt
))
1063 } else if (argc
== 1 && (!mnt_context_get_source(cxt
) ||
1064 !mnt_context_get_target(cxt
))) {
1066 * C) mount [-L|-U|--source] <target>
1067 * mount [--target <dir>] <source>
1068 * mount <source|target>
1070 * non-root may specify source *or* target, but not both
1072 * It does not matter for libmount if we set source or target
1073 * here (the library is able to swap it), but it matters for
1076 int istag
= mnt_tag_is_valid(argv
[0]);
1078 if (istag
&& mnt_context_get_source(cxt
))
1079 /* -L, -U or --source together with LABEL= or UUID= */
1080 errx(MNT_EX_USAGE
, _("source specified more than once"));
1081 else if (istag
|| mnt_context_get_target(cxt
))
1082 mnt_context_set_source(cxt
, argv
[0]);
1084 mnt_context_set_target(cxt
, argv
[0]);
1086 if (mnt_context_is_restricted(cxt
) &&
1087 mnt_context_get_source(cxt
) &&
1088 mnt_context_get_target(cxt
))
1091 } else if (argc
== 2 && !mnt_context_get_source(cxt
)
1092 && !mnt_context_get_target(cxt
)) {
1094 * D) mount <source> <target>
1096 if (mnt_context_is_restricted(cxt
))
1099 mnt_context_set_source(cxt
, argv
[0]);
1100 mnt_context_set_target(cxt
, argv
[1]);
1103 warnx(_("bad usage"));
1104 errtryhelp(MNT_EX_USAGE
);
1107 if (mnt_context_is_restricted(cxt
) && sanitize_paths(cxt
) != 0)
1111 /* "move" as option string is not supported by libmount */
1112 mnt_context_set_mflags(cxt
, MS_MOVE
);
1114 if ((oper
&& !has_remount_flag(cxt
)) || propa
)
1115 /* For --make-* or --bind is fstab/mtab unnecessary */
1116 mnt_context_set_optsmode(cxt
, MNT_OMODE_NOTAB
);
1118 rc
= mnt_context_mount(cxt
);
1121 && mnt_context_is_restricted(cxt
)
1122 && !mnt_context_syscall_called(cxt
)) {
1123 /* Try it again without permissions */
1125 rc
= mnt_context_mount(cxt
);
1127 rc
= mk_exit_code(cxt
, rc
);
1129 if (rc
== MNT_EX_SUCCESS
&& mnt_context_is_verbose(cxt
))
1130 success_message(cxt
);
1132 mnt_free_context(cxt
);
1133 env_list_free(envs_removed
);