]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/mount.c
libmount: Introduce new error: MNT_ERR_LOOPOVERLAP
[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"
5ebbc386 42#include "canonicalize.h"
97073441 43
38483b86
SK
44#define OPTUTILS_EXIT_CODE MOUNT_EX_USAGE
45#include "optutils.h"
46
97073441 47/*** TODO: DOCS:
374fd21a
KZ
48 *
49 * --options-mode={ignore,append,prepend,replace} MNT_OMODE_{IGNORE, ...}
50 * --options-source={fstab,mtab,disable} MNT_OMODE_{FSTAB,MTAB,NOTAB}
51 * --options-source-force MNT_OMODE_FORCE
97073441
KZ
52 */
53
ce433404 54static int readwrite;
dbae36fe 55
d946359a
KZ
56static int mk_exit_code(struct libmnt_context *cxt, int rc);
57
97073441
KZ
58static void __attribute__((__noreturn__)) exit_non_root(const char *option)
59{
60 const uid_t ruid = getuid();
61 const uid_t euid = geteuid();
62
63 if (ruid == 0 && euid != 0) {
64 /* user is root, but setuid to non-root */
65 if (option)
497d2ce7 66 errx(MOUNT_EX_USAGE, _("only root can use \"--%s\" option "
97073441
KZ
67 "(effective UID is %u)"),
68 option, euid);
497d2ce7 69 errx(MOUNT_EX_USAGE, _("only root can do that "
97073441
KZ
70 "(effective UID is %u)"), euid);
71 }
72 if (option)
497d2ce7
KZ
73 errx(MOUNT_EX_USAGE, _("only root can use \"--%s\" option"), option);
74 errx(MOUNT_EX_USAGE, _("only root can do that"));
97073441
KZ
75}
76
77static void __attribute__((__noreturn__)) print_version(void)
78{
79 const char *ver = NULL;
ac8ecab4 80 const char **features = NULL, **p;
97073441
KZ
81
82 mnt_get_library_version(&ver);
ac8ecab4
KZ
83 mnt_get_library_features(&features);
84
85 printf(_("%s from %s (libmount %s"),
86 program_invocation_short_name,
87 PACKAGE_STRING,
88 ver);
89 p = features;
90 while (p && *p) {
91 fputs(p == features ? ": " : ", ", stdout);
92 fputs(*p++, stdout);
93 }
94 fputs(")\n", stdout);
497d2ce7 95 exit(MOUNT_EX_SUCCESS);
97073441
KZ
96}
97
7fc6d2b8 98static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
9f7472b0
KZ
99 const char *filename, int line)
100{
101 if (filename)
b779c1ae 102 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
1cd9d0d7 103 return 1;
9f7472b0
KZ
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
6195f9e6 165 mnt_unref_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 {
8ab82185 199 if (mk_exit_code(cxt, mntrc) == MOUNT_EX_SUCCESS) {
16b73aae 200 nsucc++;
d946359a 201
8ab82185
KZ
202 /* Note that MOUNT_EX_SUCCESS return code does
203 * not mean that FS has been really mounted
204 * (e.g. nofail option) */
205 if (mnt_context_get_status(cxt)
206 && mnt_context_is_verbose(cxt))
d946359a 207 printf("%-25s: successfully mounted\n", tgt);
16b73aae
KZ
208 } else
209 nerrs++;
9f7472b0
KZ
210 }
211 }
212
d2c97887
KZ
213 if (mnt_context_is_parent(cxt)) {
214 /* wait for mount --fork children */
16b73aae
KZ
215 int nchildren = 0;
216
217 nerrs = 0, nsucc = 0;
d2c97887
KZ
218
219 rc = mnt_context_wait_for_children(cxt, &nchildren, &nerrs);
220 if (!rc && nchildren)
16b73aae 221 nsucc = nchildren - nerrs;
d2c97887
KZ
222 }
223
16b73aae
KZ
224 if (nerrs == 0)
225 rc = MOUNT_EX_SUCCESS; /* all success */
226 else if (nsucc == 0)
227 rc = MOUNT_EX_FAIL; /* all failed */
228 else
229 rc = MOUNT_EX_SOMEOK; /* some success, some failed */
230
25609ee1 231 mnt_free_iter(itr);
9f7472b0 232 return rc;
a9ae3955
KZ
233}
234
84600ddc
KZ
235static void success_message(struct libmnt_context *cxt)
236{
237 unsigned long mflags = 0;
f5ae1d70 238 const char *tgt, *src, *pr = program_invocation_short_name;
84600ddc
KZ
239
240 if (mnt_context_helper_executed(cxt)
241 || mnt_context_get_status(cxt) != 1)
242 return;
243
244 mnt_context_get_mflags(cxt, &mflags);
245 tgt = mnt_context_get_target(cxt);
246 src = mnt_context_get_source(cxt);
247
248 if (mflags & MS_MOVE)
f5ae1d70 249 printf(_("%s: %s moved to %s.\n"), pr, src, tgt);
84600ddc 250 else if (mflags & MS_BIND)
9b4257c8 251 printf(_("%s: %s bound on %s.\n"), pr, src, tgt);
b4ec4573
KZ
252 else if (mflags & MS_PROPAGATION) {
253 if (src && strcmp(src, "none") != 0 && tgt)
254 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
255
f5ae1d70 256 printf(_("%s: %s propagation flags changed.\n"), pr, tgt);
b4ec4573 257 } else
f5ae1d70 258 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
84600ddc
KZ
259}
260
ce433404
KZ
261/*
262 * Handles generic errors like ENOMEM, ...
263 *
264 * rc = 0 success
265 * <0 error (usually -errno)
266 *
497d2ce7 267 * Returns exit status (MOUNT_EX_*) and prints error message.
ce433404 268 */
2e86597f 269static int handle_generic_errors(int rc, const char *msg, ...)
ce433404 270{
2e86597f
KZ
271 va_list va;
272
273 va_start(va, msg);
ce433404
KZ
274 errno = -rc;
275
276 switch(errno) {
277 case EINVAL:
278 case EPERM:
2e86597f 279 vwarn(msg, va);
497d2ce7 280 rc = MOUNT_EX_USAGE;
2e86597f 281 break;
ce433404 282 case ENOMEM:
2e86597f 283 vwarn(msg, va);
497d2ce7 284 rc = MOUNT_EX_SYSERR;
2e86597f 285 break;
ce433404 286 default:
2e86597f 287 vwarn(msg, va);
497d2ce7 288 rc = MOUNT_EX_FAIL;
ce433404
KZ
289 break;
290 }
2e86597f
KZ
291 va_end(va);
292 return rc;
ce433404
KZ
293}
294
4e45dfb9
KZ
295#if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
296#include <selinux/selinux.h>
297#include <selinux/context.h>
298
299static void selinux_warning(struct libmnt_context *cxt, const char *tgt)
300{
301
302 if (tgt && mnt_context_is_verbose(cxt) && is_selinux_enabled() > 0) {
303 security_context_t raw = NULL, def = NULL;
304
305 if (getfilecon(tgt, &raw) > 0
306 && security_get_initial_context("file", &def) == 0) {
307
308 if (!selinux_file_context_cmp(raw, def))
309 printf(_(
310 "mount: %s does not contain SELinux labels.\n"
311 " You just mounted an file system that supports labels which does not\n"
312 " contain labels, onto an SELinux box. It is likely that confined\n"
313 " applications will generate AVC messages and not be allowed access to\n"
314 " this file system. For more details see restorecon(8) and mount(8).\n"),
315 tgt);
316 }
317 freecon(raw);
318 freecon(def);
319 }
320}
321#else
b8c0f4fb 322# define selinux_warning(_x, _y)
4e45dfb9
KZ
323#endif
324
fcc0413a
KZ
325/*
326 * Returns 1 if @dir parent is shared
327 */
328static int is_shared_tree(struct libmnt_context *cxt, const char *dir)
329{
330 struct libmnt_table *tb = NULL;
331 struct libmnt_fs *fs;
332 unsigned long mflags = 0;
333 char *mnt = NULL, *p;
334 int rc = 0;
335
336 if (!dir)
337 return 0;
338 if (mnt_context_get_mtab(cxt, &tb) || !tb)
339 goto done;
340 mnt = xstrdup(dir);
341 p = strrchr(mnt, '/');
342 if (!p)
343 goto done;
344 if (p > mnt)
345 *p = '\0';
346 fs = mnt_table_find_mountpoint(tb, mnt, MNT_ITER_BACKWARD);
347
348 rc = fs && mnt_fs_is_kernel(fs)
349 && mnt_fs_get_propagation(fs, &mflags) == 0
350 && (mflags & MS_SHARED);
351done:
352 free(mnt);
353 return rc;
354}
355
ce433404
KZ
356/*
357 * rc = 0 success
358 * <0 error (usually -errno or -1)
359 *
497d2ce7 360 * Returns exit status (MOUNT_EX_*) and/or prints error message.
ce433404
KZ
361 */
362static int mk_exit_code(struct libmnt_context *cxt, int rc)
363{
364 int syserr;
365 struct stat st;
366 unsigned long uflags = 0, mflags = 0;
367
368 int restricted = mnt_context_is_restricted(cxt);
369 const char *tgt = mnt_context_get_target(cxt);
370 const char *src = mnt_context_get_source(cxt);
371
372try_readonly:
be6904b9 373 if (mnt_context_helper_executed(cxt)) {
ce433404
KZ
374 /*
375 * /sbin/mount.<type> called, return status
376 */
be6904b9
KZ
377 if (rc == -MNT_ERR_APPLYFLAGS)
378 warnx(_("WARNING: failed to apply propagation flags"));
ce433404 379 return mnt_context_get_helper_status(cxt);
be6904b9 380 }
ce433404 381
4e45dfb9 382 if (rc == 0 && mnt_context_get_status(cxt) == 1) {
ce433404
KZ
383 /*
384 * Libmount success && syscall success.
385 */
4e45dfb9
KZ
386 selinux_warning(cxt, tgt);
387
497d2ce7 388 return MOUNT_EX_SUCCESS; /* mount(2) success */
4e45dfb9 389 }
ce433404 390
10389b1e
KZ
391 mnt_context_get_mflags(cxt, &mflags); /* mount(2) flags */
392 mnt_context_get_user_mflags(cxt, &uflags); /* userspace flags */
393
ce433404
KZ
394 if (!mnt_context_syscall_called(cxt)) {
395 /*
8b470b20 396 * libmount errors (extra library checks)
ce433404 397 */
8b470b20
KZ
398 switch (rc) {
399 case -EPERM:
ce433404 400 warnx(_("only root can mount %s on %s"), src, tgt);
497d2ce7 401 return MOUNT_EX_USAGE;
8b470b20
KZ
402 case -EBUSY:
403 warnx(_("%s is already mounted"), src);
497d2ce7 404 return MOUNT_EX_USAGE;
b2c2c42a
SB
405 /* -EROFS before syscall can happen only for loop mount */
406 case -EROFS:
407 warnx(_("%s is used as read only loop, mounting read-only"), src);
408 mnt_context_reset_status(cxt);
409 mnt_context_set_mflags(cxt, mflags | MS_RDONLY);
410 rc = mnt_context_mount(cxt);
411 if (!rc)
412 rc = mnt_context_finalize_mount(cxt);
413 goto try_readonly;
ba24923e 414 case -MNT_ERR_NOFSTAB:
aedeaa40
KZ
415 if (mnt_context_is_swapmatch(cxt)) {
416 warnx(_("can't find %s in %s"),
417 src ? src : tgt,
ce433404 418 mnt_get_fstab_path());
aedeaa40
KZ
419 return MOUNT_EX_USAGE;
420 }
421 /* source/target explicitly defined */
422 if (tgt)
423 warnx(_("can't find mountpoint %s in %s"),
424 tgt, mnt_get_fstab_path());
425 else
426 warnx(_("can't find mount source %s in %s"),
427 src, mnt_get_fstab_path());
497d2ce7 428 return MOUNT_EX_USAGE;
82a2c160
KZ
429 case -MNT_ERR_AMBIFS:
430 warnx(_("%s: more filesystems detected. This should not happen,\n"
431 " use -t <type> to explicitly specify the filesystem type or\n"
432 " use wipefs(8) to clean up the device."), src);
433 return MOUNT_EX_USAGE;
ba24923e 434 case -MNT_ERR_NOFSTYPE:
ce433404
KZ
435 if (restricted)
436 warnx(_("I could not determine the filesystem type, "
437 "and none was specified"));
438 else
439 warnx(_("you must specify the filesystem type"));
497d2ce7 440 return MOUNT_EX_USAGE;
ba24923e 441 case -MNT_ERR_NOSOURCE:
b6bdccc7
KZ
442 if (uflags & MNT_MS_NOFAIL)
443 return MOUNT_EX_SUCCESS;
ba24923e
KZ
444 if (src)
445 warnx(_("can't find %s"), src);
446 else
447 warnx(_("mount source not defined"));
448 return MOUNT_EX_USAGE;
61f5ff6c
KZ
449 case -MNT_ERR_MOUNTOPT:
450 if (errno)
451 warn(_("failed to parse mount options"));
452 else
453 warnx(_("failed to parse mount options"));
454 return MOUNT_EX_USAGE;
10389b1e 455 case -MNT_ERR_LOOPDEV:
5cf05c71 456 warn(_("%s: failed to setup loop device"), src);
10389b1e 457 return MOUNT_EX_FAIL;
ba24923e
KZ
458 default:
459 return handle_generic_errors(rc, _("%s: mount failed"),
460 tgt ? tgt : src);
461 }
ce433404
KZ
462 } else if (mnt_context_get_syscall_errno(cxt) == 0) {
463 /*
464 * mount(2) syscall success, but something else failed
465 * (probably error in mtab processing).
466 */
467 if (rc < 0)
468 return handle_generic_errors(rc,
2e86597f
KZ
469 _("%s: filesystem mounted, but mount(8) failed"),
470 tgt ? tgt : src);
ce433404 471
497d2ce7 472 return MOUNT_EX_SOFTWARE; /* internal error */
ce433404
KZ
473
474 }
475
476 /*
477 * mount(2) errors
478 */
479 syserr = mnt_context_get_syscall_errno(cxt);
480
ce433404
KZ
481
482 switch(syserr) {
483 case EPERM:
484 if (geteuid() == 0) {
485 if (stat(tgt, &st) || !S_ISDIR(st.st_mode))
486 warnx(_("mount point %s is not a directory"), tgt);
487 else
488 warnx(_("permission denied"));
489 } else
490 warnx(_("must be superuser to use mount"));
491 break;
492
493 case EBUSY:
494 {
495 struct libmnt_table *tb;
ce433404
KZ
496
497 if (mflags & MS_REMOUNT) {
498 warnx(_("%s is busy"), tgt);
499 break;
500 }
501
502 warnx(_("%s is already mounted or %s busy"), src, tgt);
503
504 if (src && mnt_context_get_mtab(cxt, &tb) == 0) {
505 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD);
506 struct libmnt_fs *fs;
507
508 while(mnt_table_next_fs(tb, itr, &fs) == 0) {
509 const char *s = mnt_fs_get_srcpath(fs),
510 *t = mnt_fs_get_target(fs);
511
6699e742 512 if (t && s && mnt_fs_streq_srcpath(fs, src))
ce433404
KZ
513 fprintf(stderr, _(
514 " %s is already mounted on %s\n"), s, t);
515 }
516 mnt_free_iter(itr);
517 }
518 break;
519 }
520 case ENOENT:
fcc0413a 521 if (tgt && lstat(tgt, &st))
ce433404 522 warnx(_("mount point %s does not exist"), tgt);
fcc0413a 523 else if (tgt && stat(tgt, &st))
ce433404 524 warnx(_("mount point %s is a symbolic link to nowhere"), tgt);
fcc0413a 525 else if (src && stat(src, &st)) {
ce433404 526 if (uflags & MNT_MS_NOFAIL)
497d2ce7 527 return MOUNT_EX_SUCCESS;
ce433404
KZ
528
529 warnx(_("special device %s does not exist"), src);
530 } else {
531 errno = syserr;
592fe017
SK
532 if (tgt)
533 warn("%s: %s", _("mount(2) failed"), tgt);
534 else if (src)
535 warn("%s: %s", _("mount(2) failed"), src);
536 else
537 warn(_("mount(2) failed"));
ce433404
KZ
538 }
539 break;
540
541 case ENOTDIR:
542 if (stat(tgt, &st) || ! S_ISDIR(st.st_mode))
543 warnx(_("mount point %s is not a directory"), tgt);
fcc0413a 544 else if (src && stat(src, &st) && errno == ENOTDIR) {
ce433404 545 if (uflags & MNT_MS_NOFAIL)
497d2ce7 546 return MOUNT_EX_SUCCESS;
ce433404
KZ
547
548 warnx(_("special device %s does not exist "
549 "(a path prefix is not a directory)"), src);
550 } else {
551 errno = syserr;
592fe017 552 warn("%s: %s", _("mount(2) failed"), tgt);
ce433404
KZ
553 }
554 break;
555
556 case EINVAL:
557 if (mflags & MS_REMOUNT)
558 warnx(_("%s not mounted or bad option"), tgt);
be6904b9 559 else if (rc == -MNT_ERR_APPLYFLAGS)
58f108ef 560 warnx(_("%s is not mountpoint or bad option"), tgt);
fcc0413a
KZ
561 else if ((mflags & MS_MOVE) && is_shared_tree(cxt, src))
562 warnx(_("bad option. Note that moving a mount residing under a shared\n"
563 " mount is unsupported."));
ce433404
KZ
564 else
565 warnx(_("wrong fs type, bad option, bad superblock on %s,\n"
566 " missing codepage or helper program, or other error"),
567 src);
568
569 if (mnt_fs_is_netfs(mnt_context_get_fs(cxt)))
570 fprintf(stderr, _(
571 " (for several filesystems (e.g. nfs, cifs) you might\n"
572 " need a /sbin/mount.<type> helper program)\n"));
573
fcc0413a 574 fprintf(stderr, _("\n"
ce433404 575 " In some cases useful info is found in syslog - try\n"
fcc0413a 576 " dmesg | tail or so.\n"));
ce433404
KZ
577 break;
578
579 case EMFILE:
580 warnx(_("mount table full"));
581 break;
582
583 case EIO:
584 warnx(_("%s: can't read superblock"), src);
585 break;
586
587 case ENODEV:
64a7e209
KZ
588 if (mnt_context_get_fstype(cxt))
589 warnx(_("unknown filesystem type '%s'"), mnt_context_get_fstype(cxt));
590 else
591 warnx(_("unknown filesystem type"));
ce433404
KZ
592 break;
593
594 case ENOTBLK:
595 if (uflags & MNT_MS_NOFAIL)
497d2ce7 596 return MOUNT_EX_SUCCESS;
ce433404
KZ
597
598 if (stat(src, &st))
599 warnx(_("%s is not a block device, and stat(2) fails?"), src);
600 else if (S_ISBLK(st.st_mode))
601 warnx(_("the kernel does not recognize %s as a block device\n"
602 " (maybe `modprobe driver'?)"), src);
603 else if (S_ISREG(st.st_mode))
604 warnx(_("%s is not a block device (maybe try `-o loop'?)"), src);
605 else
606 warnx(_(" %s is not a block device"), src);
607 break;
608
609 case ENXIO:
610 if (uflags & MNT_MS_NOFAIL)
497d2ce7 611 return MOUNT_EX_SUCCESS;
ce433404
KZ
612
613 warnx(_("%s is not a valid block device"), src);
614 break;
615
616 case EACCES:
617 case EROFS:
618 if (mflags & MS_RDONLY)
619 warnx(_("cannot mount %s read-only"), src);
620
621 else if (readwrite)
622 warnx(_("%s is write-protected but explicit `-w' flag given"), src);
623
624 else if (mflags & MS_REMOUNT)
625 warnx(_("cannot remount %s read-write, is write-protected"), src);
626
8b5b9468
KZ
627 else if (mflags & MS_BIND)
628 warn(_("mount %s on %s failed"), src, tgt);
629
ce433404
KZ
630 else {
631 warnx(_("%s is write-protected, mounting read-only"), src);
632
633 mnt_context_reset_status(cxt);
634 mnt_context_set_mflags(cxt, mflags | MS_RDONLY);
635 rc = mnt_context_do_mount(cxt);
636 if (!rc)
637 rc = mnt_context_finalize_mount(cxt);
638
639 goto try_readonly;
640 }
641 break;
642
643 case ENOMEDIUM:
e4520bf2
MF
644 if (uflags & MNT_MS_NOFAIL)
645 return MOUNT_EX_SUCCESS;
646
ce433404
KZ
647 warnx(_("no medium found on %s"), src);
648 break;
649
650 default:
651 warn(_("mount %s on %s failed"), src, tgt);
652 break;
653 }
654
497d2ce7 655 return MOUNT_EX_FAIL;
ce433404
KZ
656}
657
64b6bc4f
KZ
658static struct libmnt_table *append_fstab(struct libmnt_context *cxt,
659 struct libmnt_table *fstab,
660 const char *path)
661{
662
663 if (!fstab) {
664 fstab = mnt_new_table();
665 if (!fstab)
666 err(MOUNT_EX_SYSERR, _("failed to initialize libmount table"));
667
668 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
669 mnt_context_set_fstab(cxt, fstab);
50fccba1
KZ
670
671 mnt_unref_table(fstab); /* reference is handled by @cxt now */
64b6bc4f
KZ
672 }
673
674 if (mnt_table_parse_fstab(fstab, path))
675 errx(MOUNT_EX_USAGE,_("%s: failed to parse"), path);
676
677 return fstab;
678}
679
5ebbc386
KZ
680/*
681 * Check source and target paths -- non-root user should not be able to
682 * resolve paths which are unreadable for him.
683 */
684static void sanitize_paths(struct libmnt_context *cxt)
685{
686 const char *p;
687 struct libmnt_fs *fs = mnt_context_get_fs(cxt);
688
689 if (!fs)
690 return;
691
692 p = mnt_fs_get_target(fs);
693 if (p) {
694 char *np = canonicalize_path_restricted(p);
695 if (!np)
696 err(MOUNT_EX_USAGE, "%s", p);
697 mnt_fs_set_target(fs, np);
698 free(np);
699 }
700
701 p = mnt_fs_get_srcpath(fs);
702 if (p) {
703 char *np = canonicalize_path_restricted(p);
704 if (!np)
705 err(MOUNT_EX_USAGE, "%s", p);
706 mnt_fs_set_source(fs, np);
707 free(np);
708 }
709}
710
be6904b9
KZ
711static void append_option(struct libmnt_context *cxt, const char *opt)
712{
8225bb78
KZ
713 if (opt && (*opt == '=' || *opt == '\'' || *opt == '\"' || isblank(*opt)))
714 errx(MOUNT_EX_USAGE, _("unsupported option format: %s"), opt);
be6904b9
KZ
715 if (mnt_context_append_options(cxt, opt))
716 err(MOUNT_EX_SYSERR, _("failed to append option '%s'"), opt);
717}
718
ba986e81
KZ
719static int has_remount_flag(struct libmnt_context *cxt)
720{
721 unsigned long mflags = 0;
722
723 if (mnt_context_get_mflags(cxt, &mflags))
724 return 0;
725
726 return mflags & MS_REMOUNT;
727}
728
97073441
KZ
729static void __attribute__((__noreturn__)) usage(FILE *out)
730{
e4c92d06
KZ
731 fputs(USAGE_HEADER, out);
732 fprintf(out, _(
97073441
KZ
733 " %1$s [-lhV]\n"
734 " %1$s -a [options]\n"
aedeaa40 735 " %1$s [options] [--source] <source> | [--target] <directory>\n"
97073441
KZ
736 " %1$s [options] <source> <directory>\n"
737 " %1$s <operation> <mountpoint> [<target>]\n"),
738 program_invocation_short_name);
739
451dbcfa
BS
740 fputs(USAGE_SEPARATOR, out);
741 fputs(_("Mount a filesystem.\n"), out);
742
e4c92d06 743 fputs(USAGE_OPTIONS, out);
97073441 744 fprintf(out, _(
97073441 745 " -a, --all mount all filesystems mentioned in fstab\n"
97073441 746 " -c, --no-canonicalize don't canonicalize paths\n"
eaca47f7 747 " -f, --fake dry run; skip the mount(2) syscall\n"
64b6bc4f
KZ
748 " -F, --fork fork off for each device (use with -a)\n"
749 " -T, --fstab <path> alternative file to /etc/fstab\n"));
eaca47f7 750 fprintf(out, _(
89de71b3
BS
751 " -i, --internal-only don't call the mount.<type> helpers\n"));
752 fprintf(out, _(
753 " -l, --show-labels show also filesystem labels\n"));
754 fprintf(out, _(
eaca47f7
BS
755 " -n, --no-mtab don't write to /etc/mtab\n"));
756 fprintf(out, _(
757 " -o, --options <list> comma-separated list of mount options\n"
758 " -O, --test-opts <list> limit the set of filesystems (use with -a)\n"
759 " -r, --read-only mount the filesystem read-only (same as -o ro)\n"
760 " -t, --types <list> limit the set of filesystem types\n"));
761 fprintf(out, _(
aedeaa40
KZ
762 " --source <src> explicitly specifies source (path, label, uuid)\n"
763 " --target <target> explicitly specifies mountpoint\n"));
764 fprintf(out, _(
89de71b3
BS
765 " -v, --verbose say what is being done\n"));
766 fprintf(out, _(
6d402bbe 767 " -w, --rw, --read-write mount the filesystem read-write (default)\n"));
97073441 768
e4c92d06
KZ
769 fputs(USAGE_SEPARATOR, out);
770 fputs(USAGE_HELP, out);
771 fputs(USAGE_VERSION, out);
772
eaca47f7 773 fprintf(out, _(
97073441
KZ
774 "\nSource:\n"
775 " -L, --label <label> synonym for LABEL=<label>\n"
776 " -U, --uuid <uuid> synonym for UUID=<uuid>\n"
777 " LABEL=<label> specifies device by filesystem label\n"
eb0eb262
KZ
778 " UUID=<uuid> specifies device by filesystem UUID\n"
779 " PARTLABEL=<label> specifies device by partition label\n"
780 " PARTUUID=<uuid> specifies device by partition UUID\n"));
781
eaca47f7 782 fprintf(out, _(
97073441
KZ
783 " <device> specifies device by path\n"
784 " <directory> mountpoint for bind mounts (see --bind/rbind)\n"
eaca47f7 785 " <file> regular file for loopdev setup\n"));
97073441 786
eaca47f7 787 fprintf(out, _(
97073441
KZ
788 "\nOperations:\n"
789 " -B, --bind mount a subtree somewhere else (same as -o bind)\n"
790 " -M, --move move a subtree to some other place\n"
eaca47f7
BS
791 " -R, --rbind mount a subtree and all submounts somewhere else\n"));
792 fprintf(out, _(
97073441
KZ
793 " --make-shared mark a subtree as shared\n"
794 " --make-slave mark a subtree as slave\n"
795 " --make-private mark a subtree as private\n"
eaca47f7
BS
796 " --make-unbindable mark a subtree as unbindable\n"));
797 fprintf(out, _(
97073441
KZ
798 " --make-rshared recursively mark a whole subtree as shared\n"
799 " --make-rslave recursively mark a whole subtree as slave\n"
800 " --make-rprivate recursively mark a whole subtree as private\n"
eaca47f7 801 " --make-runbindable recursively mark a whole subtree as unbindable\n"));
97073441 802
e4c92d06 803 fprintf(out, USAGE_MAN_TAIL("mount(8)"));
97073441 804
497d2ce7 805 exit(out == stderr ? MOUNT_EX_USAGE : MOUNT_EX_SUCCESS);
97073441
KZ
806}
807
808int main(int argc, char **argv)
809{
497d2ce7 810 int c, rc = MOUNT_EX_SUCCESS, all = 0, show_labels = 0;
68164f6c 811 struct libmnt_context *cxt;
64b6bc4f 812 struct libmnt_table *fstab = NULL;
aedeaa40 813 char *srcbuf = NULL;
97073441
KZ
814 char *types = NULL;
815 unsigned long oper = 0;
be6904b9 816 int propa = 0;
97073441 817
2492f713
KZ
818 enum {
819 MOUNT_OPT_SHARED = CHAR_MAX + 1,
820 MOUNT_OPT_SLAVE,
821 MOUNT_OPT_PRIVATE,
822 MOUNT_OPT_UNBINDABLE,
823 MOUNT_OPT_RSHARED,
824 MOUNT_OPT_RSLAVE,
825 MOUNT_OPT_RPRIVATE,
aedeaa40
KZ
826 MOUNT_OPT_RUNBINDABLE,
827 MOUNT_OPT_TARGET,
828 MOUNT_OPT_SOURCE
2492f713
KZ
829 };
830
6c7d5ae9 831 static const struct option longopts[] = {
97073441
KZ
832 { "all", 0, 0, 'a' },
833 { "fake", 0, 0, 'f' },
64b6bc4f 834 { "fstab", 1, 0, 'T' },
97073441
KZ
835 { "fork", 0, 0, 'F' },
836 { "help", 0, 0, 'h' },
837 { "no-mtab", 0, 0, 'n' },
838 { "read-only", 0, 0, 'r' },
839 { "ro", 0, 0, 'r' },
840 { "verbose", 0, 0, 'v' },
841 { "version", 0, 0, 'V' },
842 { "read-write", 0, 0, 'w' },
843 { "rw", 0, 0, 'w' },
844 { "options", 1, 0, 'o' },
845 { "test-opts", 1, 0, 'O' },
846 { "types", 1, 0, 't' },
847 { "uuid", 1, 0, 'U' },
848 { "label", 1, 0, 'L'},
849 { "bind", 0, 0, 'B' },
850 { "move", 0, 0, 'M' },
851 { "rbind", 0, 0, 'R' },
2492f713
KZ
852 { "make-shared", 0, 0, MOUNT_OPT_SHARED },
853 { "make-slave", 0, 0, MOUNT_OPT_SLAVE },
854 { "make-private", 0, 0, MOUNT_OPT_PRIVATE },
855 { "make-unbindable", 0, 0, MOUNT_OPT_UNBINDABLE },
856 { "make-rshared", 0, 0, MOUNT_OPT_RSHARED },
857 { "make-rslave", 0, 0, MOUNT_OPT_RSLAVE },
858 { "make-rprivate", 0, 0, MOUNT_OPT_RPRIVATE },
859 { "make-runbindable", 0, 0, MOUNT_OPT_RUNBINDABLE },
97073441
KZ
860 { "no-canonicalize", 0, 0, 'c' },
861 { "internal-only", 0, 0, 'i' },
862 { "show-labels", 0, 0, 'l' },
aedeaa40
KZ
863 { "target", 1, 0, MOUNT_OPT_TARGET },
864 { "source", 1, 0, MOUNT_OPT_SOURCE },
97073441
KZ
865 { NULL, 0, 0, 0 }
866 };
867
51a37c19 868 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
be6904b9 869 { 'B','M','R' }, /* bind,move,rbind */
51a37c19
KZ
870 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
871 { 0 }
872 };
873 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
874
6189ace3 875 sanitize_env();
97073441
KZ
876 setlocale(LC_ALL, "");
877 bindtextdomain(PACKAGE, LOCALEDIR);
878 textdomain(PACKAGE);
efb8854f 879 atexit(close_stdout);
97073441
KZ
880
881 mnt_init_debug(0);
882 cxt = mnt_new_context();
883 if (!cxt)
497d2ce7 884 err(MOUNT_EX_SYSERR, _("libmount context allocation failed"));
97073441 885
9f7472b0
KZ
886 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
887
961d69f7 888 while ((c = getopt_long(argc, argv, "aBcfFhilL:Mno:O:rRsU:vVwt:T:",
97073441
KZ
889 longopts, NULL)) != -1) {
890
891 /* only few options are allowed for non-root users */
aedeaa40 892 if (mnt_context_is_restricted(cxt) &&
961d69f7 893 !strchr("hlLUVvrist", c) &&
aedeaa40
KZ
894 c != MOUNT_OPT_TARGET &&
895 c != MOUNT_OPT_SOURCE)
732a6311 896 exit_non_root(option_to_longopt(c, longopts));
97073441 897
51a37c19
KZ
898 err_exclusive_options(c, longopts, excl, excl_st);
899
97073441
KZ
900 switch(c) {
901 case 'a':
902 all = 1;
97073441
KZ
903 break;
904 case 'c':
905 mnt_context_disable_canonicalize(cxt, TRUE);
906 break;
907 case 'f':
908 mnt_context_enable_fake(cxt, TRUE);
909 break;
910 case 'F':
d2c97887 911 mnt_context_enable_fork(cxt, TRUE);
97073441
KZ
912 break;
913 case 'h':
914 usage(stdout);
915 break;
916 case 'i':
917 mnt_context_disable_helpers(cxt, TRUE);
918 break;
919 case 'n':
920 mnt_context_disable_mtab(cxt, TRUE);
921 break;
922 case 'r':
be6904b9 923 append_option(cxt, "ro");
ce433404 924 readwrite = 0;
97073441
KZ
925 break;
926 case 'v':
927 mnt_context_enable_verbose(cxt, TRUE);
928 break;
929 case 'V':
930 print_version();
931 break;
932 case 'w':
be6904b9 933 append_option(cxt, "rw");
ce433404 934 readwrite = 1;
97073441
KZ
935 break;
936 case 'o':
be6904b9 937 append_option(cxt, optarg);
97073441
KZ
938 break;
939 case 'O':
940 if (mnt_context_set_options_pattern(cxt, optarg))
497d2ce7 941 err(MOUNT_EX_SYSERR, _("failed to set options pattern"));
97073441
KZ
942 break;
943 case 'L':
aedeaa40
KZ
944 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
945 mnt_context_disable_swapmatch(cxt, 1);
946 mnt_context_set_source(cxt, srcbuf);
947 free(srcbuf);
948 break;
97073441 949 case 'U':
aedeaa40
KZ
950 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
951 mnt_context_disable_swapmatch(cxt, 1);
952 mnt_context_set_source(cxt, srcbuf);
953 free(srcbuf);
97073441
KZ
954 break;
955 case 'l':
956 show_labels = 1;
957 break;
958 case 't':
959 types = optarg;
960 break;
64b6bc4f
KZ
961 case 'T':
962 fstab = append_fstab(cxt, fstab, optarg);
963 break;
97073441
KZ
964 case 's':
965 mnt_context_enable_sloppy(cxt, TRUE);
966 break;
967 case 'B':
d7890778 968 oper |= MS_BIND;
97073441
KZ
969 break;
970 case 'M':
d7890778 971 oper |= MS_MOVE;
97073441
KZ
972 break;
973 case 'R':
d7890778 974 oper |= (MS_BIND | MS_REC);
97073441 975 break;
2492f713 976 case MOUNT_OPT_SHARED:
be6904b9
KZ
977 append_option(cxt, "shared");
978 propa = 1;
97073441 979 break;
2492f713 980 case MOUNT_OPT_SLAVE:
be6904b9
KZ
981 append_option(cxt, "slave");
982 propa = 1;
97073441 983 break;
2492f713 984 case MOUNT_OPT_PRIVATE:
be6904b9
KZ
985 append_option(cxt, "private");
986 propa = 1;
97073441 987 break;
2492f713 988 case MOUNT_OPT_UNBINDABLE:
be6904b9
KZ
989 append_option(cxt, "unbindable");
990 propa = 1;
97073441 991 break;
2492f713 992 case MOUNT_OPT_RSHARED:
be6904b9
KZ
993 append_option(cxt, "rshared");
994 propa = 1;
97073441 995 break;
2492f713 996 case MOUNT_OPT_RSLAVE:
be6904b9
KZ
997 append_option(cxt, "rslave");
998 propa = 1;
97073441 999 break;
2492f713 1000 case MOUNT_OPT_RPRIVATE:
be6904b9
KZ
1001 append_option(cxt, "rprivate");
1002 propa = 1;
97073441 1003 break;
2492f713 1004 case MOUNT_OPT_RUNBINDABLE:
be6904b9
KZ
1005 append_option(cxt, "runbindable");
1006 propa = 1;
97073441 1007 break;
aedeaa40
KZ
1008 case MOUNT_OPT_TARGET:
1009 mnt_context_disable_swapmatch(cxt, 1);
1010 mnt_context_set_target(cxt, optarg);
1011 break;
1012 case MOUNT_OPT_SOURCE:
aedeaa40
KZ
1013 mnt_context_disable_swapmatch(cxt, 1);
1014 mnt_context_set_source(cxt, optarg);
1015 break;
97073441
KZ
1016 default:
1017 usage(stderr);
1018 break;
1019 }
1020 }
1021
1022 argc -= optind;
1023 argv += optind;
1024
59414c6b
KZ
1025 if (fstab && !mnt_context_is_nocanonicalize(cxt)) {
1026 /*
1027 * We have external (context independent) fstab instance, let's
1028 * make a connection between the fstab and the canonicalization
1029 * cache.
1030 */
6195f9e6 1031 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
59414c6b
KZ
1032 }
1033
aedeaa40
KZ
1034 if (!mnt_context_get_source(cxt) &&
1035 !mnt_context_get_target(cxt) &&
1036 !argc &&
1037 !all) {
be6904b9 1038 if (oper || mnt_context_get_options(cxt))
97073441 1039 usage(stderr);
11754572 1040 print_all(cxt, types, show_labels);
97073441
KZ
1041 goto done;
1042 }
1043
1707b9b1
RT
1044 /* Non-root users are allowed to use -t to print_all(),
1045 but not to mount */
1046 if (mnt_context_is_restricted(cxt) && types)
1047 exit_non_root("types");
1048
aedeaa40 1049 if (oper && (types || all || mnt_context_get_source(cxt)))
97073441
KZ
1050 usage(stderr);
1051
9f7472b0
KZ
1052 if (types && (all || strchr(types, ',') ||
1053 strncmp(types, "no", 2) == 0))
97073441
KZ
1054 mnt_context_set_fstype_pattern(cxt, types);
1055 else if (types)
1056 mnt_context_set_fstype(cxt, types);
1057
a9ae3955
KZ
1058 if (all) {
1059 /*
1060 * A) Mount all
1061 */
d2c97887 1062 rc = mount_all(cxt);
11754572 1063 goto done;
a9ae3955 1064
aedeaa40
KZ
1065 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
1066 mnt_context_get_target(cxt))) {
a9ae3955 1067 /*
aedeaa40 1068 * B) mount -L|-U|--source|--target
6751f062
KZ
1069 *
1070 * non-root may specify source *or* target, but not both
a9ae3955 1071 */
aedeaa40
KZ
1072 if (mnt_context_is_restricted(cxt) &&
1073 mnt_context_get_source(cxt) &&
1074 mnt_context_get_target(cxt))
1075 exit_non_root(NULL);
97073441 1076
6751f062
KZ
1077 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
1078 !mnt_context_get_target(cxt))) {
a9ae3955 1079 /*
aedeaa40 1080 * C) mount [-L|-U|--source] <target>
6751f062 1081 * mount [--target <dir>] <source>
a9ae3955 1082 * mount <source|target>
aedeaa40
KZ
1083 *
1084 * non-root may specify source *or* target, but not both
6751f062
KZ
1085 *
1086 * It does not matter for libmount if we set source or target
1087 * here (the library is able to swap it), but it matters for
1088 * sanitize_paths().
a9ae3955 1089 */
6751f062
KZ
1090 int istag = mnt_tag_is_valid(argv[0]);
1091
1092 if (istag && mnt_context_get_source(cxt))
1093 /* -L, -U or --source together with LABEL= or UUID= */
1094 errx(MOUNT_EX_USAGE, _("source specified more than once"));
1095 else if (istag || mnt_context_get_target(cxt))
1096 mnt_context_set_source(cxt, argv[0]);
1097 else
1098 mnt_context_set_target(cxt, argv[0]);
1099
aedeaa40 1100 if (mnt_context_is_restricted(cxt) &&
6751f062
KZ
1101 mnt_context_get_source(cxt) &&
1102 mnt_context_get_target(cxt))
aedeaa40
KZ
1103 exit_non_root(NULL);
1104
aedeaa40
KZ
1105 } else if (argc == 2 && !mnt_context_get_source(cxt)
1106 && !mnt_context_get_target(cxt)) {
a9ae3955
KZ
1107 /*
1108 * D) mount <source> <target>
1109 */
97073441
KZ
1110 if (mnt_context_is_restricted(cxt))
1111 exit_non_root(NULL);
6751f062 1112
97073441
KZ
1113 mnt_context_set_source(cxt, argv[0]);
1114 mnt_context_set_target(cxt, argv[1]);
aedeaa40 1115
97073441
KZ
1116 } else
1117 usage(stderr);
1118
5ebbc386
KZ
1119 if (mnt_context_is_restricted(cxt))
1120 sanitize_paths(cxt);
1121
be6904b9
KZ
1122 if (oper)
1123 /* BIND/MOVE operations, let's set the mount flags */
68164f6c 1124 mnt_context_set_mflags(cxt, oper);
97073441 1125
ba986e81
KZ
1126 if ((oper && !has_remount_flag(cxt)) || propa)
1127 /* For --make-* or --bind is fstab/mtab unnecessary */
374fd21a 1128 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
374fd21a 1129
cfb9db30 1130 rc = mnt_context_mount(cxt);
ce433404
KZ
1131 rc = mk_exit_code(cxt, rc);
1132
84600ddc
KZ
1133 if (rc == MOUNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
1134 success_message(cxt);
97073441 1135done:
97073441
KZ
1136 mnt_free_context(cxt);
1137 return rc;
1138}
1139