]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mount.c
mount: losetup: remove obsolete encryption support
[thirdparty/util-linux.git] / sys-utils / mount.c
CommitLineData
97073441
KZ
1/*
2 * mount(8) -- mount a filesystem
3 *
4 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
5 * Written by Karel Zak <kzak@redhat.com>
6 *
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.
11 *
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.
16 *
7cebf0bb
SK
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.
97073441
KZ
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <errno.h>
25#include <string.h>
26#include <getopt.h>
97073441
KZ
27#include <unistd.h>
28#include <sys/types.h>
dbae36fe 29#include <sys/mman.h>
ce433404 30#include <sys/stat.h>
2e86597f 31#include <stdarg.h>
2a1f429a 32#include <libmount.h>
5f7c1890 33#include <ctype.h>
2a1f429a 34
97073441
KZ
35#include "nls.h"
36#include "c.h"
6189ace3 37#include "env.h"
dbae36fe 38#include "strutils.h"
497d2ce7 39#include "exitcodes.h"
a5ebeca5 40#include "xalloc.h"
efb8854f 41#include "closestream.h"
97073441 42
38483b86
SK
43#define OPTUTILS_EXIT_CODE MOUNT_EX_USAGE
44#include "optutils.h"
45
97073441 46/*** TODO: DOCS:
374fd21a
KZ
47 *
48 * --options-mode={ignore,append,prepend,replace} MNT_OMODE_{IGNORE, ...}
49 * --options-source={fstab,mtab,disable} MNT_OMODE_{FSTAB,MTAB,NOTAB}
50 * --options-source-force MNT_OMODE_FORCE
97073441
KZ
51 */
52
ce433404 53static int readwrite;
dbae36fe 54
d946359a
KZ
55static int mk_exit_code(struct libmnt_context *cxt, int rc);
56
97073441
KZ
57static void __attribute__((__noreturn__)) exit_non_root(const char *option)
58{
59 const uid_t ruid = getuid();
60 const uid_t euid = geteuid();
61
62 if (ruid == 0 && euid != 0) {
63 /* user is root, but setuid to non-root */
64 if (option)
497d2ce7 65 errx(MOUNT_EX_USAGE, _("only root can use \"--%s\" option "
97073441
KZ
66 "(effective UID is %u)"),
67 option, euid);
497d2ce7 68 errx(MOUNT_EX_USAGE, _("only root can do that "
97073441
KZ
69 "(effective UID is %u)"), euid);
70 }
71 if (option)
497d2ce7
KZ
72 errx(MOUNT_EX_USAGE, _("only root can use \"--%s\" option"), option);
73 errx(MOUNT_EX_USAGE, _("only root can do that"));
97073441
KZ
74}
75
76static void __attribute__((__noreturn__)) print_version(void)
77{
78 const char *ver = NULL;
ac8ecab4 79 const char **features = NULL, **p;
97073441
KZ
80
81 mnt_get_library_version(&ver);
ac8ecab4
KZ
82 mnt_get_library_features(&features);
83
84 printf(_("%s from %s (libmount %s"),
85 program_invocation_short_name,
86 PACKAGE_STRING,
87 ver);
88 p = features;
89 while (p && *p) {
90 fputs(p == features ? ": " : ", ", stdout);
91 fputs(*p++, stdout);
92 }
93 fputs(")\n", stdout);
497d2ce7 94 exit(MOUNT_EX_SUCCESS);
97073441
KZ
95}
96
7fc6d2b8 97static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
9f7472b0
KZ
98 const char *filename, int line)
99{
100 if (filename)
101 warnx(_("%s: parse error: ignore entry at line %d."),
102 filename, line);
103 return 0;
104}
105
5f7c1890
KZ
106/*
107 * Replace control chars with '?' to be compatible with coreutils. For more
108 * robust solution use findmnt(1) where we use \x?? hex encoding.
109 */
110static void safe_fputs(const char *data)
111{
112 const char *p;
113
114 for (p = data; p && *p; p++) {
115 if (iscntrl((unsigned char) *p))
116 fputc('?', stdout);
117 else
118 fputc(*p, stdout);
119 }
120}
121
11754572 122static void print_all(struct libmnt_context *cxt, char *pattern, int show_label)
97073441 123{
68164f6c 124 struct libmnt_table *tb;
a0c014dc 125 struct libmnt_iter *itr = NULL;
68164f6c
KZ
126 struct libmnt_fs *fs;
127 struct libmnt_cache *cache = NULL;
97073441 128
11754572 129 if (mnt_context_get_mtab(cxt, &tb))
497d2ce7 130 err(MOUNT_EX_SYSERR, _("failed to read mtab"));
97073441
KZ
131
132 itr = mnt_new_iter(MNT_ITER_FORWARD);
11754572 133 if (!itr)
497d2ce7 134 err(MOUNT_EX_SYSERR, _("failed to initialize libmount iterator"));
97073441
KZ
135 if (show_label)
136 cache = mnt_new_cache();
137
9f7472b0 138 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
97073441
KZ
139 const char *type = mnt_fs_get_fstype(fs);
140 const char *src = mnt_fs_get_source(fs);
7cf389f7 141 const char *optstr = mnt_fs_get_options(fs);
aa397ce5 142 char *xsrc = NULL;
97073441
KZ
143
144 if (type && pattern && !mnt_match_fstype(type, pattern))
145 continue;
146
aa397ce5
DR
147 if (!mnt_fs_is_pseudofs(fs))
148 xsrc = mnt_pretty_path(src, cache);
5f7c1890
KZ
149 printf ("%s on ", xsrc ? xsrc : src);
150 safe_fputs(mnt_fs_get_target(fs));
151
97073441
KZ
152 if (type)
153 printf (" type %s", type);
154 if (optstr)
155 printf (" (%s)", optstr);
156 if (show_label && src) {
157 char *lb = mnt_cache_find_tag_value(cache, src, "LABEL");
158 if (lb)
159 printf (" [%s]", lb);
160 }
161 fputc('\n', stdout);
2576b4e7 162 free(xsrc);
97073441 163 }
11754572 164
97073441 165 mnt_free_cache(cache);
b192b7b9 166 mnt_free_iter(itr);
97073441
KZ
167}
168
9f7472b0
KZ
169/*
170 * mount -a [-F]
9f7472b0 171 */
d2c97887 172static int mount_all(struct libmnt_context *cxt)
a9ae3955 173{
9f7472b0
KZ
174 struct libmnt_iter *itr;
175 struct libmnt_fs *fs;
497d2ce7 176 int mntrc, ignored, rc = MOUNT_EX_SUCCESS;
9f7472b0 177
16b73aae
KZ
178 int nsucc = 0, nerrs = 0;
179
9f7472b0
KZ
180 itr = mnt_new_iter(MNT_ITER_FORWARD);
181 if (!itr) {
182 warn(_("failed to initialize libmount iterator"));
497d2ce7 183 return MOUNT_EX_SYSERR;
9f7472b0
KZ
184 }
185
186 while (mnt_context_next_mount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
187
188 const char *tgt = mnt_fs_get_target(fs);
189
190 if (ignored) {
191 if (mnt_context_is_verbose(cxt))
b3f7a0ec
KZ
192 printf(ignored == 1 ? _("%-25s: ignored\n") :
193 _("%-25s: already mounted\n"),
9f7472b0 194 tgt);
d2c97887 195 } else if (mnt_context_is_fork(cxt)) {
d946359a
KZ
196 if (mnt_context_is_verbose(cxt))
197 printf("%-25s: mount successfully forked\n", tgt);
9f7472b0 198 } else {
16b73aae 199 mk_exit_code(cxt, mntrc); /* to print warnings */
9f7472b0 200
d946359a 201 if (mnt_context_get_status(cxt)) {
16b73aae 202 nsucc++;
d946359a
KZ
203
204 if (mnt_context_is_verbose(cxt))
205 printf("%-25s: successfully mounted\n", tgt);
16b73aae
KZ
206 } else
207 nerrs++;
9f7472b0
KZ
208 }
209 }
210
d2c97887
KZ
211 if (mnt_context_is_parent(cxt)) {
212 /* wait for mount --fork children */
16b73aae
KZ
213 int nchildren = 0;
214
215 nerrs = 0, nsucc = 0;
d2c97887
KZ
216
217 rc = mnt_context_wait_for_children(cxt, &nchildren, &nerrs);
218 if (!rc && nchildren)
16b73aae 219 nsucc = nchildren - nerrs;
d2c97887
KZ
220 }
221
16b73aae
KZ
222 if (nerrs == 0)
223 rc = MOUNT_EX_SUCCESS; /* all success */
224 else if (nsucc == 0)
225 rc = MOUNT_EX_FAIL; /* all failed */
226 else
227 rc = MOUNT_EX_SOMEOK; /* some success, some failed */
228
25609ee1 229 mnt_free_iter(itr);
9f7472b0 230 return rc;
a9ae3955
KZ
231}
232
ce433404
KZ
233/*
234 * Handles generic errors like ENOMEM, ...
235 *
236 * rc = 0 success
237 * <0 error (usually -errno)
238 *
497d2ce7 239 * Returns exit status (MOUNT_EX_*) and prints error message.
ce433404 240 */
2e86597f 241static int handle_generic_errors(int rc, const char *msg, ...)
ce433404 242{
2e86597f
KZ
243 va_list va;
244
245 va_start(va, msg);
ce433404
KZ
246 errno = -rc;
247
248 switch(errno) {
249 case EINVAL:
250 case EPERM:
2e86597f 251 vwarn(msg, va);
497d2ce7 252 rc = MOUNT_EX_USAGE;
2e86597f 253 break;
ce433404 254 case ENOMEM:
2e86597f 255 vwarn(msg, va);
497d2ce7 256 rc = MOUNT_EX_SYSERR;
2e86597f 257 break;
ce433404 258 default:
2e86597f 259 vwarn(msg, va);
497d2ce7 260 rc = MOUNT_EX_FAIL;
ce433404
KZ
261 break;
262 }
2e86597f
KZ
263 va_end(va);
264 return rc;
ce433404
KZ
265}
266
4e45dfb9
KZ
267#if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
268#include <selinux/selinux.h>
269#include <selinux/context.h>
270
271static void selinux_warning(struct libmnt_context *cxt, const char *tgt)
272{
273
274 if (tgt && mnt_context_is_verbose(cxt) && is_selinux_enabled() > 0) {
275 security_context_t raw = NULL, def = NULL;
276
277 if (getfilecon(tgt, &raw) > 0
278 && security_get_initial_context("file", &def) == 0) {
279
280 if (!selinux_file_context_cmp(raw, def))
281 printf(_(
282 "mount: %s does not contain SELinux labels.\n"
283 " You just mounted an file system that supports labels which does not\n"
284 " contain labels, onto an SELinux box. It is likely that confined\n"
285 " applications will generate AVC messages and not be allowed access to\n"
286 " this file system. For more details see restorecon(8) and mount(8).\n"),
287 tgt);
288 }
289 freecon(raw);
290 freecon(def);
291 }
292}
293#else
b8c0f4fb 294# define selinux_warning(_x, _y)
4e45dfb9
KZ
295#endif
296
297
ce433404
KZ
298/*
299 * rc = 0 success
300 * <0 error (usually -errno or -1)
301 *
497d2ce7 302 * Returns exit status (MOUNT_EX_*) and/or prints error message.
ce433404
KZ
303 */
304static int mk_exit_code(struct libmnt_context *cxt, int rc)
305{
306 int syserr;
307 struct stat st;
308 unsigned long uflags = 0, mflags = 0;
309
310 int restricted = mnt_context_is_restricted(cxt);
311 const char *tgt = mnt_context_get_target(cxt);
312 const char *src = mnt_context_get_source(cxt);
313
314try_readonly:
ce433404
KZ
315 if (mnt_context_helper_executed(cxt))
316 /*
317 * /sbin/mount.<type> called, return status
318 */
319 return mnt_context_get_helper_status(cxt);
320
4e45dfb9 321 if (rc == 0 && mnt_context_get_status(cxt) == 1) {
ce433404
KZ
322 /*
323 * Libmount success && syscall success.
324 */
4e45dfb9
KZ
325 selinux_warning(cxt, tgt);
326
497d2ce7 327 return MOUNT_EX_SUCCESS; /* mount(2) success */
4e45dfb9 328 }
ce433404 329
10389b1e
KZ
330 mnt_context_get_mflags(cxt, &mflags); /* mount(2) flags */
331 mnt_context_get_user_mflags(cxt, &uflags); /* userspace flags */
332
ce433404
KZ
333 if (!mnt_context_syscall_called(cxt)) {
334 /*
8b470b20 335 * libmount errors (extra library checks)
ce433404 336 */
8b470b20
KZ
337 switch (rc) {
338 case -EPERM:
ce433404 339 warnx(_("only root can mount %s on %s"), src, tgt);
497d2ce7 340 return MOUNT_EX_USAGE;
8b470b20
KZ
341 case -EBUSY:
342 warnx(_("%s is already mounted"), src);
497d2ce7 343 return MOUNT_EX_USAGE;
ba24923e 344 case -MNT_ERR_NOFSTAB:
aedeaa40
KZ
345 if (mnt_context_is_swapmatch(cxt)) {
346 warnx(_("can't find %s in %s"),
347 src ? src : tgt,
ce433404 348 mnt_get_fstab_path());
aedeaa40
KZ
349 return MOUNT_EX_USAGE;
350 }
351 /* source/target explicitly defined */
352 if (tgt)
353 warnx(_("can't find mountpoint %s in %s"),
354 tgt, mnt_get_fstab_path());
355 else
356 warnx(_("can't find mount source %s in %s"),
357 src, mnt_get_fstab_path());
497d2ce7 358 return MOUNT_EX_USAGE;
ba24923e 359 case -MNT_ERR_NOFSTYPE:
ce433404
KZ
360 if (restricted)
361 warnx(_("I could not determine the filesystem type, "
362 "and none was specified"));
363 else
364 warnx(_("you must specify the filesystem type"));
497d2ce7 365 return MOUNT_EX_USAGE;
ba24923e
KZ
366 case -MNT_ERR_NOSOURCE:
367 if (src)
368 warnx(_("can't find %s"), src);
369 else
370 warnx(_("mount source not defined"));
371 return MOUNT_EX_USAGE;
61f5ff6c
KZ
372 case -MNT_ERR_MOUNTOPT:
373 if (errno)
374 warn(_("failed to parse mount options"));
375 else
376 warnx(_("failed to parse mount options"));
377 return MOUNT_EX_USAGE;
10389b1e 378 case -MNT_ERR_LOOPDEV:
5cf05c71 379 warn(_("%s: failed to setup loop device"), src);
10389b1e 380 return MOUNT_EX_FAIL;
ba24923e
KZ
381 default:
382 return handle_generic_errors(rc, _("%s: mount failed"),
383 tgt ? tgt : src);
384 }
ce433404
KZ
385 } else if (mnt_context_get_syscall_errno(cxt) == 0) {
386 /*
387 * mount(2) syscall success, but something else failed
388 * (probably error in mtab processing).
389 */
390 if (rc < 0)
391 return handle_generic_errors(rc,
2e86597f
KZ
392 _("%s: filesystem mounted, but mount(8) failed"),
393 tgt ? tgt : src);
ce433404 394
497d2ce7 395 return MOUNT_EX_SOFTWARE; /* internal error */
ce433404
KZ
396
397 }
398
399 /*
400 * mount(2) errors
401 */
402 syserr = mnt_context_get_syscall_errno(cxt);
403
ce433404
KZ
404
405 switch(syserr) {
406 case EPERM:
407 if (geteuid() == 0) {
408 if (stat(tgt, &st) || !S_ISDIR(st.st_mode))
409 warnx(_("mount point %s is not a directory"), tgt);
410 else
411 warnx(_("permission denied"));
412 } else
413 warnx(_("must be superuser to use mount"));
414 break;
415
416 case EBUSY:
417 {
418 struct libmnt_table *tb;
ce433404
KZ
419
420 if (mflags & MS_REMOUNT) {
421 warnx(_("%s is busy"), tgt);
422 break;
423 }
424
425 warnx(_("%s is already mounted or %s busy"), src, tgt);
426
427 if (src && mnt_context_get_mtab(cxt, &tb) == 0) {
428 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD);
429 struct libmnt_fs *fs;
430
431 while(mnt_table_next_fs(tb, itr, &fs) == 0) {
432 const char *s = mnt_fs_get_srcpath(fs),
433 *t = mnt_fs_get_target(fs);
434
6699e742 435 if (t && s && mnt_fs_streq_srcpath(fs, src))
ce433404
KZ
436 fprintf(stderr, _(
437 " %s is already mounted on %s\n"), s, t);
438 }
439 mnt_free_iter(itr);
440 }
441 break;
442 }
443 case ENOENT:
444 if (lstat(tgt, &st))
445 warnx(_("mount point %s does not exist"), tgt);
446 else if (stat(tgt, &st))
447 warnx(_("mount point %s is a symbolic link to nowhere"), tgt);
448 else if (stat(src, &st)) {
449 if (uflags & MNT_MS_NOFAIL)
497d2ce7 450 return MOUNT_EX_SUCCESS;
ce433404
KZ
451
452 warnx(_("special device %s does not exist"), src);
453 } else {
454 errno = syserr;
455 warn(_("mount(2) failed")); /* print errno */
456 }
457 break;
458
459 case ENOTDIR:
460 if (stat(tgt, &st) || ! S_ISDIR(st.st_mode))
461 warnx(_("mount point %s is not a directory"), tgt);
462 else if (stat(src, &st) && errno == ENOTDIR) {
463 if (uflags & MNT_MS_NOFAIL)
497d2ce7 464 return MOUNT_EX_SUCCESS;
ce433404
KZ
465
466 warnx(_("special device %s does not exist "
467 "(a path prefix is not a directory)"), src);
468 } else {
469 errno = syserr;
470 warn(_("mount(2) failed")); /* print errno */
471 }
472 break;
473
474 case EINVAL:
475 if (mflags & MS_REMOUNT)
476 warnx(_("%s not mounted or bad option"), tgt);
58f108ef
KZ
477 else if (mflags & MS_PROPAGATION)
478 warnx(_("%s is not mountpoint or bad option"), tgt);
ce433404
KZ
479 else
480 warnx(_("wrong fs type, bad option, bad superblock on %s,\n"
481 " missing codepage or helper program, or other error"),
482 src);
483
484 if (mnt_fs_is_netfs(mnt_context_get_fs(cxt)))
485 fprintf(stderr, _(
486 " (for several filesystems (e.g. nfs, cifs) you might\n"
487 " need a /sbin/mount.<type> helper program)\n"));
488
489 fprintf(stderr, _(
490 " In some cases useful info is found in syslog - try\n"
491 " dmesg | tail or so\n"));
492 break;
493
494 case EMFILE:
495 warnx(_("mount table full"));
496 break;
497
498 case EIO:
499 warnx(_("%s: can't read superblock"), src);
500 break;
501
502 case ENODEV:
503 warnx(_("unknown filesystem type '%s'"), mnt_context_get_fstype(cxt));
504 break;
505
506 case ENOTBLK:
507 if (uflags & MNT_MS_NOFAIL)
497d2ce7 508 return MOUNT_EX_SUCCESS;
ce433404
KZ
509
510 if (stat(src, &st))
511 warnx(_("%s is not a block device, and stat(2) fails?"), src);
512 else if (S_ISBLK(st.st_mode))
513 warnx(_("the kernel does not recognize %s as a block device\n"
514 " (maybe `modprobe driver'?)"), src);
515 else if (S_ISREG(st.st_mode))
516 warnx(_("%s is not a block device (maybe try `-o loop'?)"), src);
517 else
518 warnx(_(" %s is not a block device"), src);
519 break;
520
521 case ENXIO:
522 if (uflags & MNT_MS_NOFAIL)
497d2ce7 523 return MOUNT_EX_SUCCESS;
ce433404
KZ
524
525 warnx(_("%s is not a valid block device"), src);
526 break;
527
528 case EACCES:
529 case EROFS:
530 if (mflags & MS_RDONLY)
531 warnx(_("cannot mount %s read-only"), src);
532
533 else if (readwrite)
534 warnx(_("%s is write-protected but explicit `-w' flag given"), src);
535
536 else if (mflags & MS_REMOUNT)
537 warnx(_("cannot remount %s read-write, is write-protected"), src);
538
539 else {
540 warnx(_("%s is write-protected, mounting read-only"), src);
541
542 mnt_context_reset_status(cxt);
543 mnt_context_set_mflags(cxt, mflags | MS_RDONLY);
544 rc = mnt_context_do_mount(cxt);
545 if (!rc)
546 rc = mnt_context_finalize_mount(cxt);
547
548 goto try_readonly;
549 }
550 break;
551
552 case ENOMEDIUM:
553 warnx(_("no medium found on %s"), src);
554 break;
555
556 default:
557 warn(_("mount %s on %s failed"), src, tgt);
558 break;
559 }
560
497d2ce7 561 return MOUNT_EX_FAIL;
ce433404
KZ
562}
563
64b6bc4f
KZ
564static struct libmnt_table *append_fstab(struct libmnt_context *cxt,
565 struct libmnt_table *fstab,
566 const char *path)
567{
568
569 if (!fstab) {
570 fstab = mnt_new_table();
571 if (!fstab)
572 err(MOUNT_EX_SYSERR, _("failed to initialize libmount table"));
573
574 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
575 mnt_context_set_fstab(cxt, fstab);
576 }
577
578 if (mnt_table_parse_fstab(fstab, path))
579 errx(MOUNT_EX_USAGE,_("%s: failed to parse"), path);
580
581 return fstab;
582}
583
97073441
KZ
584static void __attribute__((__noreturn__)) usage(FILE *out)
585{
e4c92d06
KZ
586 fputs(USAGE_HEADER, out);
587 fprintf(out, _(
97073441
KZ
588 " %1$s [-lhV]\n"
589 " %1$s -a [options]\n"
aedeaa40 590 " %1$s [options] [--source] <source> | [--target] <directory>\n"
97073441
KZ
591 " %1$s [options] <source> <directory>\n"
592 " %1$s <operation> <mountpoint> [<target>]\n"),
593 program_invocation_short_name);
594
e4c92d06 595 fputs(USAGE_OPTIONS, out);
97073441 596 fprintf(out, _(
97073441 597 " -a, --all mount all filesystems mentioned in fstab\n"
97073441 598 " -c, --no-canonicalize don't canonicalize paths\n"
eaca47f7 599 " -f, --fake dry run; skip the mount(2) syscall\n"
64b6bc4f
KZ
600 " -F, --fork fork off for each device (use with -a)\n"
601 " -T, --fstab <path> alternative file to /etc/fstab\n"));
eaca47f7
BS
602 fprintf(out, _(
603 " -h, --help display this help text and exit\n"
97073441
KZ
604 " -i, --internal-only don't call the mount.<type> helpers\n"
605 " -l, --show-labels lists all mounts with LABELs\n"
eaca47f7
BS
606 " -n, --no-mtab don't write to /etc/mtab\n"));
607 fprintf(out, _(
608 " -o, --options <list> comma-separated list of mount options\n"
609 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
610 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
611 " -t, --types <list> limit the set of filesystem types\n"));
612 fprintf(out, _(
aedeaa40
KZ
613 " --source <src> explicitly specifies source (path, label, uuid)\n"
614 " --target <target> explicitly specifies mountpoint\n"));
615 fprintf(out, _(
eaca47f7
BS
616 " -v, --verbose say what is being done\n"
617 " -V, --version display version information and exit\n"
618 " -w, --read-write mount the filesystem read-write (default)\n"));
97073441 619
e4c92d06
KZ
620 fputs(USAGE_SEPARATOR, out);
621 fputs(USAGE_HELP, out);
622 fputs(USAGE_VERSION, out);
623
eaca47f7 624 fprintf(out, _(
97073441
KZ
625 "\nSource:\n"
626 " -L, --label <label> synonym for LABEL=<label>\n"
627 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
628 " LABEL=<label> specifies device by filesystem label\n"
eb0eb262
KZ
629 " UUID=<uuid> specifies device by filesystem UUID\n"
630 " PARTLABEL=<label> specifies device by partition label\n"
631 " PARTUUID=<uuid> specifies device by partition UUID\n"));
632
eaca47f7 633 fprintf(out, _(
97073441
KZ
634 " <device> specifies device by path\n"
635 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
eaca47f7 636 " <file> regular file for loopdev setup\n"));
97073441 637
eaca47f7 638 fprintf(out, _(
97073441
KZ
639 "\nOperations:\n"
640 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
641 " -M, --move move a subtree to some other place\n"
eaca47f7
BS
642 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
643 fprintf(out, _(
97073441
KZ
644 " --make-shared mark a subtree as shared\n"
645 " --make-slave mark a subtree as slave\n"
646 " --make-private mark a subtree as private\n"
eaca47f7
BS
647 " --make-unbindable mark a subtree as unbindable\n"));
648 fprintf(out, _(
97073441
KZ
649 " --make-rshared recursively mark a whole subtree as shared\n"
650 " --make-rslave recursively mark a whole subtree as slave\n"
651 " --make-rprivate recursively mark a whole subtree as private\n"
eaca47f7 652 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
97073441 653
e4c92d06 654 fprintf(out, USAGE_MAN_TAIL("mount(8)"));
97073441 655
497d2ce7 656 exit(out == stderr ? MOUNT_EX_USAGE : MOUNT_EX_SUCCESS);
97073441
KZ
657}
658
659int main(int argc, char **argv)
660{
497d2ce7 661 int c, rc = MOUNT_EX_SUCCESS, all = 0, show_labels = 0;
68164f6c 662 struct libmnt_context *cxt;
64b6bc4f 663 struct libmnt_table *fstab = NULL;
aedeaa40 664 char *srcbuf = NULL;
97073441
KZ
665 char *types = NULL;
666 unsigned long oper = 0;
667
2492f713
KZ
668 enum {
669 MOUNT_OPT_SHARED = CHAR_MAX + 1,
670 MOUNT_OPT_SLAVE,
671 MOUNT_OPT_PRIVATE,
672 MOUNT_OPT_UNBINDABLE,
673 MOUNT_OPT_RSHARED,
674 MOUNT_OPT_RSLAVE,
675 MOUNT_OPT_RPRIVATE,
aedeaa40
KZ
676 MOUNT_OPT_RUNBINDABLE,
677 MOUNT_OPT_TARGET,
678 MOUNT_OPT_SOURCE
2492f713
KZ
679 };
680
6c7d5ae9 681 static const struct option longopts[] = {
97073441
KZ
682 { "all", 0, 0, 'a' },
683 { "fake", 0, 0, 'f' },
64b6bc4f 684 { "fstab", 1, 0, 'T' },
97073441
KZ
685 { "fork", 0, 0, 'F' },
686 { "help", 0, 0, 'h' },
687 { "no-mtab", 0, 0, 'n' },
688 { "read-only", 0, 0, 'r' },
689 { "ro", 0, 0, 'r' },
690 { "verbose", 0, 0, 'v' },
691 { "version", 0, 0, 'V' },
692 { "read-write", 0, 0, 'w' },
693 { "rw", 0, 0, 'w' },
694 { "options", 1, 0, 'o' },
695 { "test-opts", 1, 0, 'O' },
dbae36fe 696 { "pass-fd", 1, 0, 'p' },
97073441
KZ
697 { "types", 1, 0, 't' },
698 { "uuid", 1, 0, 'U' },
699 { "label", 1, 0, 'L'},
700 { "bind", 0, 0, 'B' },
701 { "move", 0, 0, 'M' },
702 { "rbind", 0, 0, 'R' },
2492f713
KZ
703 { "make-shared", 0, 0, MOUNT_OPT_SHARED },
704 { "make-slave", 0, 0, MOUNT_OPT_SLAVE },
705 { "make-private", 0, 0, MOUNT_OPT_PRIVATE },
706 { "make-unbindable", 0, 0, MOUNT_OPT_UNBINDABLE },
707 { "make-rshared", 0, 0, MOUNT_OPT_RSHARED },
708 { "make-rslave", 0, 0, MOUNT_OPT_RSLAVE },
709 { "make-rprivate", 0, 0, MOUNT_OPT_RPRIVATE },
710 { "make-runbindable", 0, 0, MOUNT_OPT_RUNBINDABLE },
97073441
KZ
711 { "no-canonicalize", 0, 0, 'c' },
712 { "internal-only", 0, 0, 'i' },
713 { "show-labels", 0, 0, 'l' },
aedeaa40
KZ
714 { "target", 1, 0, MOUNT_OPT_TARGET },
715 { "source", 1, 0, MOUNT_OPT_SOURCE },
97073441
KZ
716 { NULL, 0, 0, 0 }
717 };
718
51a37c19
KZ
719 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
720 { 'B','M','R', /* bind,move,rbind */
721 MOUNT_OPT_SHARED, MOUNT_OPT_SLAVE,
722 MOUNT_OPT_PRIVATE, MOUNT_OPT_UNBINDABLE,
723 MOUNT_OPT_RSHARED, MOUNT_OPT_RSLAVE,
724 MOUNT_OPT_RPRIVATE, MOUNT_OPT_RUNBINDABLE },
725
726 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
727 { 0 }
728 };
729 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
730
6189ace3 731 sanitize_env();
97073441
KZ
732 setlocale(LC_ALL, "");
733 bindtextdomain(PACKAGE, LOCALEDIR);
734 textdomain(PACKAGE);
efb8854f 735 atexit(close_stdout);
97073441
KZ
736
737 mnt_init_debug(0);
738 cxt = mnt_new_context();
739 if (!cxt)
497d2ce7 740 err(MOUNT_EX_SYSERR, _("libmount context allocation failed"));
97073441 741
9f7472b0
KZ
742 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
743
64b6bc4f 744 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:p:rRsU:vVwt:T:",
97073441
KZ
745 longopts, NULL)) != -1) {
746
747 /* only few options are allowed for non-root users */
aedeaa40
KZ
748 if (mnt_context_is_restricted(cxt) &&
749 !strchr("hlLUVvpris", c) &&
750 c != MOUNT_OPT_TARGET &&
751 c != MOUNT_OPT_SOURCE)
732a6311 752 exit_non_root(option_to_longopt(c, longopts));
97073441 753
51a37c19
KZ
754 err_exclusive_options(c, longopts, excl, excl_st);
755
97073441
KZ
756 switch(c) {
757 case 'a':
758 all = 1;
97073441
KZ
759 break;
760 case 'c':
761 mnt_context_disable_canonicalize(cxt, TRUE);
762 break;
763 case 'f':
764 mnt_context_enable_fake(cxt, TRUE);
765 break;
766 case 'F':
d2c97887 767 mnt_context_enable_fork(cxt, TRUE);
97073441
KZ
768 break;
769 case 'h':
770 usage(stdout);
771 break;
772 case 'i':
773 mnt_context_disable_helpers(cxt, TRUE);
774 break;
775 case 'n':
776 mnt_context_disable_mtab(cxt, TRUE);
777 break;
778 case 'r':
779 if (mnt_context_append_options(cxt, "ro"))
497d2ce7 780 err(MOUNT_EX_SYSERR, _("failed to append options"));
ce433404 781 readwrite = 0;
97073441
KZ
782 break;
783 case 'v':
784 mnt_context_enable_verbose(cxt, TRUE);
785 break;
786 case 'V':
787 print_version();
788 break;
789 case 'w':
790 if (mnt_context_append_options(cxt, "rw"))
497d2ce7 791 err(MOUNT_EX_SYSERR, _("failed to append options"));
ce433404 792 readwrite = 1;
97073441
KZ
793 break;
794 case 'o':
795 if (mnt_context_append_options(cxt, optarg))
497d2ce7 796 err(MOUNT_EX_SYSERR, _("failed to append options"));
97073441
KZ
797 break;
798 case 'O':
799 if (mnt_context_set_options_pattern(cxt, optarg))
497d2ce7 800 err(MOUNT_EX_SYSERR, _("failed to set options pattern"));
97073441 801 break;
dbae36fe 802 case 'p':
5cf05c71 803 warnx(_("--pass-fd is no longer supported"));
dbae36fe 804 break;
97073441 805 case 'L':
aedeaa40
KZ
806 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
807 mnt_context_disable_swapmatch(cxt, 1);
808 mnt_context_set_source(cxt, srcbuf);
809 free(srcbuf);
810 break;
97073441 811 case 'U':
aedeaa40
KZ
812 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
813 mnt_context_disable_swapmatch(cxt, 1);
814 mnt_context_set_source(cxt, srcbuf);
815 free(srcbuf);
97073441
KZ
816 break;
817 case 'l':
818 show_labels = 1;
819 break;
820 case 't':
821 types = optarg;
822 break;
64b6bc4f
KZ
823 case 'T':
824 fstab = append_fstab(cxt, fstab, optarg);
825 break;
97073441
KZ
826 case 's':
827 mnt_context_enable_sloppy(cxt, TRUE);
828 break;
829 case 'B':
d7890778 830 oper |= MS_BIND;
97073441
KZ
831 break;
832 case 'M':
d7890778 833 oper |= MS_MOVE;
97073441
KZ
834 break;
835 case 'R':
d7890778 836 oper |= (MS_BIND | MS_REC);
97073441 837 break;
2492f713 838 case MOUNT_OPT_SHARED:
d7890778 839 oper |= MS_SHARED;
97073441 840 break;
2492f713 841 case MOUNT_OPT_SLAVE:
d7890778 842 oper |= MS_SLAVE;
97073441 843 break;
2492f713 844 case MOUNT_OPT_PRIVATE:
d7890778 845 oper |= MS_PRIVATE;
97073441 846 break;
2492f713 847 case MOUNT_OPT_UNBINDABLE:
d7890778 848 oper |= MS_UNBINDABLE;
97073441 849 break;
2492f713 850 case MOUNT_OPT_RSHARED:
d7890778 851 oper |= (MS_SHARED | MS_REC);
97073441 852 break;
2492f713 853 case MOUNT_OPT_RSLAVE:
d7890778 854 oper |= (MS_SLAVE | MS_REC);
97073441 855 break;
2492f713 856 case MOUNT_OPT_RPRIVATE:
d7890778 857 oper |= (MS_PRIVATE | MS_REC);
97073441 858 break;
2492f713 859 case MOUNT_OPT_RUNBINDABLE:
d7890778 860 oper |= (MS_UNBINDABLE | MS_REC);
97073441 861 break;
aedeaa40
KZ
862 case MOUNT_OPT_TARGET:
863 mnt_context_disable_swapmatch(cxt, 1);
864 mnt_context_set_target(cxt, optarg);
865 break;
866 case MOUNT_OPT_SOURCE:
aedeaa40
KZ
867 mnt_context_disable_swapmatch(cxt, 1);
868 mnt_context_set_source(cxt, optarg);
869 break;
97073441
KZ
870 default:
871 usage(stderr);
872 break;
873 }
874 }
875
876 argc -= optind;
877 argv += optind;
878
aedeaa40
KZ
879 if (!mnt_context_get_source(cxt) &&
880 !mnt_context_get_target(cxt) &&
881 !argc &&
882 !all) {
97073441
KZ
883 if (oper)
884 usage(stderr);
11754572 885 print_all(cxt, types, show_labels);
97073441
KZ
886 goto done;
887 }
888
aedeaa40 889 if (oper && (types || all || mnt_context_get_source(cxt)))
97073441
KZ
890 usage(stderr);
891
9f7472b0
KZ
892 if (types && (all || strchr(types, ',') ||
893 strncmp(types, "no", 2) == 0))
97073441
KZ
894 mnt_context_set_fstype_pattern(cxt, types);
895 else if (types)
896 mnt_context_set_fstype(cxt, types);
897
a9ae3955
KZ
898 if (all) {
899 /*
900 * A) Mount all
901 */
d2c97887 902 rc = mount_all(cxt);
11754572 903 goto done;
a9ae3955 904
aedeaa40
KZ
905 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
906 mnt_context_get_target(cxt))) {
a9ae3955 907 /*
aedeaa40 908 * B) mount -L|-U|--source|--target
a9ae3955 909 */
aedeaa40
KZ
910 if (mnt_context_is_restricted(cxt) &&
911 mnt_context_get_source(cxt) &&
912 mnt_context_get_target(cxt))
913 exit_non_root(NULL);
97073441
KZ
914
915 } else if (argc == 1) {
a9ae3955 916 /*
aedeaa40 917 * C) mount [-L|-U|--source] <target>
a9ae3955 918 * mount <source|target>
aedeaa40
KZ
919 *
920 * non-root may specify source *or* target, but not both
a9ae3955 921 */
aedeaa40
KZ
922 if (mnt_context_is_restricted(cxt) &&
923 mnt_context_get_source(cxt))
924 exit_non_root(NULL);
925
97073441
KZ
926 mnt_context_set_target(cxt, argv[0]);
927
aedeaa40
KZ
928 } else if (argc == 2 && !mnt_context_get_source(cxt)
929 && !mnt_context_get_target(cxt)) {
a9ae3955
KZ
930 /*
931 * D) mount <source> <target>
932 */
97073441
KZ
933 if (mnt_context_is_restricted(cxt))
934 exit_non_root(NULL);
935 mnt_context_set_source(cxt, argv[0]);
936 mnt_context_set_target(cxt, argv[1]);
aedeaa40 937
97073441
KZ
938 } else
939 usage(stderr);
940
374fd21a
KZ
941 if (oper) {
942 /* MS_PROPAGATION operations, let's set the mount flags */
68164f6c 943 mnt_context_set_mflags(cxt, oper);
97073441 944
374fd21a
KZ
945 /* For -make* or --bind is fstab unnecessary */
946 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
947 }
948
cfb9db30 949 rc = mnt_context_mount(cxt);
ce433404
KZ
950 rc = mk_exit_code(cxt, rc);
951
97073441 952done:
97073441 953 mnt_free_context(cxt);
64b6bc4f 954 mnt_free_table(fstab);
97073441
KZ
955 return rc;
956}
957