1 /* vi: set sw=4 ts=4: */
3 * Mini mount implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net>
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 // Design notes: There is no spec for mount. Remind me to write one.
13 // mount_main() calls singlemount() which calls mount_it_now().
15 // mount_main() can loop through /etc/fstab for mount -a
16 // singlemount() can loop through /etc/filesystems for fstype detection.
17 // mount_it_now() does the actual mount.
21 //config: bool "mount (30 kb)"
23 //config: select PLATFORM_LINUX
25 //config: All files and filesystems in Unix are arranged into one big directory
26 //config: tree. The 'mount' utility is used to graft a filesystem onto a
27 //config: particular part of the tree. A filesystem can either live on a block
28 //config: device, or it can be accessible over the network, as is the case with
29 //config: NFS filesystems.
31 //config:config FEATURE_MOUNT_FAKE
32 //config: bool "Support -f (fake mount)"
34 //config: depends on MOUNT
36 //config: Enable support for faking a file system mount.
38 //config:config FEATURE_MOUNT_VERBOSE
39 //config: bool "Support -v (verbose)"
41 //config: depends on MOUNT
43 //config: Enable multi-level -v[vv...] verbose messages. Useful if you
44 //config: debug mount problems and want to see what is exactly passed
45 //config: to the kernel.
47 //config:config FEATURE_MOUNT_HELPERS
48 //config: bool "Support mount helpers"
50 //config: depends on MOUNT
52 //config: Enable mounting of virtual file systems via external helpers.
53 //config: E.g. "mount obexfs#-b00.11.22.33.44.55 /mnt" will in effect call
54 //config: "obexfs -b00.11.22.33.44.55 /mnt"
55 //config: Also "mount -t sometype [-o opts] fs /mnt" will try
56 //config: "sometype [-o opts] fs /mnt" if simple mount syscall fails.
57 //config: The idea is to use such virtual filesystems in /etc/fstab.
59 //config:config FEATURE_MOUNT_LABEL
60 //config: bool "Support specifying devices by label or UUID"
62 //config: depends on MOUNT
63 //config: select VOLUMEID
65 //config: This allows for specifying a device by label or uuid, rather than by
66 //config: name. This feature utilizes the same functionality as blkid/findfs.
68 //config:config FEATURE_MOUNT_NFS
69 //config: bool "Support mounting NFS file systems on Linux < 2.6.23"
71 //config: depends on MOUNT
72 //config: select FEATURE_SYSLOG
74 //config: Enable mounting of NFS file systems on Linux kernels prior
75 //config: to version 2.6.23. Note that in this case mounting of NFS
76 //config: over IPv6 will not be possible.
78 //config: Note that this option links in RPC support from libc,
79 //config: which is rather large (~10 kbytes on uclibc).
81 //config:config FEATURE_MOUNT_CIFS
82 //config: bool "Support mounting CIFS/SMB file systems"
84 //config: depends on MOUNT
86 //config: Enable support for samba mounts.
88 //config:config FEATURE_MOUNT_FLAGS
89 //config: depends on MOUNT
90 //config: bool "Support lots of -o flags"
93 //config: Without this, mount only supports ro/rw/remount. With this, it
94 //config: supports nosuid, suid, dev, nodev, exec, noexec, sync, async, atime,
95 //config: noatime, diratime, nodiratime, loud, bind, move, shared, slave,
96 //config: private, unbindable, rshared, rslave, rprivate, and runbindable.
98 //config:config FEATURE_MOUNT_FSTAB
99 //config: depends on MOUNT
100 //config: bool "Support /etc/fstab and -a (mount all)"
103 //config: Support mount all and looking for files in /etc/fstab.
105 //config:config FEATURE_MOUNT_OTHERTAB
106 //config: depends on FEATURE_MOUNT_FSTAB
107 //config: bool "Support -T <alt_fstab>"
110 //config: Support mount -T (specifying an alternate fstab)
112 /* On full-blown systems, requires suid for user mounts.
113 * But it's not unthinkable to have it available in non-suid flavor on some systems,
114 * for viewing mount table.
115 * Therefore we use BB_SUID_MAYBE instead of BB_SUID_REQUIRE: */
116 //applet:IF_MOUNT(APPLET(mount, BB_DIR_BIN, IF_DESKTOP(BB_SUID_MAYBE) IF_NOT_DESKTOP(BB_SUID_DROP)))
118 //kbuild:lib-$(CONFIG_MOUNT) += mount.o
120 //usage:#define mount_trivial_usage
121 //usage: "[OPTIONS] [-o OPT] DEVICE NODE"
122 //usage:#define mount_full_usage "\n\n"
123 //usage: "Mount a filesystem. Filesystem autodetection requires /proc.\n"
124 //usage: "\n -a Mount all filesystems in fstab"
125 //usage: IF_FEATURE_MOUNT_FAKE(
126 //usage: IF_FEATURE_MTAB_SUPPORT(
127 //usage: "\n -f Update /etc/mtab, but don't mount"
129 //usage: IF_NOT_FEATURE_MTAB_SUPPORT(
130 //usage: "\n -f Dry run"
133 //usage: IF_FEATURE_MOUNT_HELPERS(
134 //usage: "\n -i Don't run mount helper"
136 //usage: IF_FEATURE_MTAB_SUPPORT(
137 //usage: "\n -n Don't update /etc/mtab"
139 //usage: IF_FEATURE_MOUNT_VERBOSE(
140 //usage: "\n -v Verbose"
142 ////usage: "\n -s Sloppy (ignored)"
143 //usage: "\n -r Read-only mount"
144 ////usage: "\n -w Read-write mount (default)"
145 //usage: "\n -t FSTYPE[,...] Filesystem type(s)"
146 //usage: IF_FEATURE_MOUNT_OTHERTAB(
147 //usage: "\n -T FILE Read FILE instead of /etc/fstab"
149 //usage: "\n -O OPT Mount only filesystems with option OPT (-a only)"
151 //usage: IF_FEATURE_MOUNT_LOOP(
152 //usage: "\n loop Ignored (loop devices are autodetected)"
154 //usage: IF_FEATURE_MOUNT_FLAGS(
155 //usage: "\n [a]sync Writes are [a]synchronous"
156 //usage: "\n [no]atime Disable/enable updates to inode access times"
157 //usage: "\n [no]diratime Disable/enable atime updates to directories"
158 //usage: "\n [no]relatime Disable/enable atime updates relative to modification time"
159 //usage: "\n [no]dev (Dis)allow use of special device files"
160 //usage: "\n [no]exec (Dis)allow use of executable files"
161 //usage: "\n [no]suid (Dis)allow set-user-id-root programs"
162 //usage: "\n [r]shared Convert [recursively] to a shared subtree"
163 //usage: "\n [r]slave Convert [recursively] to a slave subtree"
164 //usage: "\n [r]private Convert [recursively] to a private subtree"
165 //usage: "\n [un]bindable Make mount point [un]able to be bind mounted"
166 //usage: "\n [r]bind Bind a file or directory [recursively] to another location"
167 //usage: "\n move Relocate an existing mount point"
169 //usage: "\n remount Remount a mounted filesystem, changing flags"
170 //usage: "\n ro Same as -r"
172 //usage: "\nThere are filesystem-specific -o flags."
174 //usage:#define mount_example_usage
176 //usage: "/dev/hda3 on / type minix (rw)\n"
177 //usage: "proc on /proc type proc (rw)\n"
178 //usage: "devpts on /dev/pts type devpts (rw)\n"
179 //usage: "$ mount /dev/fd0 /mnt -t msdos -o ro\n"
180 //usage: "$ mount /tmp/diskimage /opt -t ext2 -o loop\n"
181 //usage: "$ mount cd_image.iso mydir\n"
182 //usage:#define mount_notes_usage
183 //usage: "Returns 0 for success, number of failed mounts for -a, or errno for one mount."
187 #include <sys/mount.h>
188 // Grab more as needed from util-linux's mount/mount_constants.h
190 # define MS_DIRSYNC (1 << 7) // Directory modifications are synchronous
193 # define MS_UNION (1 << 8)
196 # define MS_BIND (1 << 12)
199 # define MS_MOVE (1 << 13)
202 # define MS_RECURSIVE (1 << 14)
205 # define MS_SILENT (1 << 15)
207 // The shared subtree stuff, which went in around 2.6.15
208 #ifndef MS_UNBINDABLE
209 # define MS_UNBINDABLE (1 << 17)
212 # define MS_PRIVATE (1 << 18)
215 # define MS_SLAVE (1 << 19)
218 # define MS_SHARED (1 << 20)
221 # define MS_RELATIME (1 << 21)
223 #ifndef MS_STRICTATIME
224 # define MS_STRICTATIME (1 << 24)
227 /* Any ~MS_FOO value has this bit set: */
228 #define BB_MS_INVERTED_VALUE (1u << 31)
231 #include "common_bufsiz.h"
232 #if ENABLE_FEATURE_MOUNT_LABEL
233 # include "volume_id.h"
235 # define resolve_mount_spec(fsname) ((void)0)
238 // Needed for nfs support only
239 #include <sys/utsname.h>
242 #if ENABLE_FEATURE_MOUNT_NFS
243 /* This is just a warning of a common mistake. Possibly this should be a
244 * uclibc faq entry rather than in busybox... */
245 # if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
246 # warning "You probably need to build uClibc with UCLIBC_HAS_RPC for NFS support"
247 /* not #error, since user may be using e.g. libtirpc instead */
249 # include <rpc/rpc.h>
250 # include <rpc/pmap_prot.h>
251 # include <rpc/pmap_clnt.h>
255 #if defined(__dietlibc__)
256 // 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
257 // dietlibc-0.30 does not have implementation of getmntent_r()
258 static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
259 char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
261 struct mntent* ment = getmntent(stream);
262 return memcpy(result, ment, sizeof(*ment));
267 // Not real flags, but we want to be able to check for this.
269 MOUNT_USERS = (1 << 27) * ENABLE_DESKTOP,
270 MOUNT_NOFAIL = (1 << 28) * ENABLE_DESKTOP,
271 MOUNT_NOAUTO = (1 << 29),
272 MOUNT_SWAP = (1 << 30),
273 MOUNT_FAKEFLAGS = MOUNT_USERS | MOUNT_NOFAIL | MOUNT_NOAUTO | MOUNT_SWAP
277 #define OPTION_STR "o:*t:rwanfvsiO:" IF_FEATURE_MOUNT_OTHERTAB("T:")
293 #if ENABLE_FEATURE_MTAB_SUPPORT
294 #define USE_MTAB (!(option_mask32 & OPT_n))
299 #if ENABLE_FEATURE_MOUNT_FAKE
300 #define FAKE_IT (option_mask32 & OPT_f)
305 #if ENABLE_FEATURE_MOUNT_HELPERS
306 #define HELPERS_ALLOWED (!(option_mask32 & OPT_i))
308 #define HELPERS_ALLOWED 0
312 // TODO: more "user" flag compatibility.
313 // "user" option (from mount manpage):
314 // Only the user that mounted a filesystem can unmount it again.
315 // If any user should be able to unmount, then use users instead of user
316 // in the fstab line. The owner option is similar to the user option,
317 // with the restriction that the user must be the owner of the special file.
318 // This may be useful e.g. for /dev/fd if a login script makes
319 // the console user owner of this device.
321 // Standard mount options (from -o options or --options),
322 // with corresponding flags
323 static const int32_t mount_options[] = {
324 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
326 IF_FEATURE_MOUNT_LOOP(
330 IF_FEATURE_MOUNT_FSTAB(
332 /* "quiet" 0 - do not filter out, vfat wants to see it */
333 /* "noauto" */ MOUNT_NOAUTO,
334 /* "sw" */ MOUNT_SWAP,
335 /* "swap" */ MOUNT_SWAP,
336 IF_DESKTOP(/* "user" */ MOUNT_USERS,)
337 IF_DESKTOP(/* "users" */ MOUNT_USERS,)
338 IF_DESKTOP(/* "nofail" */ MOUNT_NOFAIL,)
340 IF_DESKTOP(/* "comment=" */ 0,) /* systemd uses this in fstab */
343 IF_FEATURE_MOUNT_FLAGS(
345 /* "nosuid" */ MS_NOSUID,
346 /* "suid" */ ~MS_NOSUID,
347 /* "dev" */ ~MS_NODEV,
348 /* "nodev" */ MS_NODEV,
349 /* "exec" */ ~MS_NOEXEC,
350 /* "noexec" */ MS_NOEXEC,
351 /* "sync" */ MS_SYNCHRONOUS,
352 /* "dirsync" */ MS_DIRSYNC,
353 /* "async" */ ~MS_SYNCHRONOUS,
354 /* "atime" */ ~MS_NOATIME,
355 /* "noatime" */ MS_NOATIME,
356 /* "diratime" */ ~MS_NODIRATIME,
357 /* "nodiratime" */ MS_NODIRATIME,
358 /* "mand" */ MS_MANDLOCK,
359 /* "nomand" */ ~MS_MANDLOCK,
360 /* "relatime" */ MS_RELATIME,
361 /* "norelatime" */ ~MS_RELATIME,
362 /* "strictatime" */ MS_STRICTATIME,
363 /* "loud" */ ~MS_SILENT,
364 /* "rbind" */ MS_BIND|MS_RECURSIVE,
367 /* "union" */ MS_UNION,
368 /* "bind" */ MS_BIND,
369 /* "move" */ MS_MOVE,
370 /* "shared" */ MS_SHARED,
371 /* "slave" */ MS_SLAVE,
372 /* "private" */ MS_PRIVATE,
373 /* "unbindable" */ MS_UNBINDABLE,
374 /* "rshared" */ MS_SHARED|MS_RECURSIVE,
375 /* "rslave" */ MS_SLAVE|MS_RECURSIVE,
376 /* "rprivate" */ MS_PRIVATE|MS_RECURSIVE,
377 /* "runbindable" */ MS_UNBINDABLE|MS_RECURSIVE,
380 // Always understood.
381 /* "ro" */ MS_RDONLY, // vfs flag
382 /* "rw" */ ~MS_RDONLY, // vfs flag
383 /* "remount" */ MS_REMOUNT // action flag
386 static const char mount_option_str[] ALIGN1 =
387 IF_FEATURE_MOUNT_LOOP(
390 IF_FEATURE_MOUNT_FSTAB(
392 // "quiet\0" - do not filter out, vfat wants to see it
397 IF_DESKTOP("users\0")
398 IF_DESKTOP("nofail\0")
400 IF_DESKTOP("comment=\0") /* systemd uses this in fstab */
402 IF_FEATURE_MOUNT_FLAGS(
439 // Always understood.
442 "remount\0" // action flag
447 #if ENABLE_FEATURE_MOUNT_NFS
448 smalluint nfs_mount_version;
450 #if ENABLE_FEATURE_MOUNT_VERBOSE
454 char getmntent_buf[1];
456 enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_buf) };
457 #define G (*(struct globals*)bb_common_bufsiz1)
458 #define nfs_mount_version (G.nfs_mount_version)
459 #if ENABLE_FEATURE_MOUNT_VERBOSE
460 #define verbose (G.verbose )
464 #define fslist (G.fslist )
465 #define getmntent_buf (G.getmntent_buf )
466 #define INIT_G() do { setup_common_bufsiz(); } while (0)
468 #if ENABLE_FEATURE_MTAB_SUPPORT
470 * update_mtab_entry_on_move() is used to update entry in case of mount --move.
471 * we are looking for existing entries mnt_dir which is equal to mnt_fsname of
472 * input mntent and replace it by new one.
474 static void FAST_FUNC update_mtab_entry_on_move(const struct mntent *mp)
476 struct mntent *entries, *m;
480 mountTable = setmntent(bb_path_mtab_file, "r");
482 bb_perror_msg(bb_path_mtab_file);
488 while ((m = getmntent(mountTable)) != NULL) {
489 entries = xrealloc_vector(entries, 3, count);
490 entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
491 entries[count].mnt_dir = xstrdup(m->mnt_dir);
492 entries[count].mnt_type = xstrdup(m->mnt_type);
493 entries[count].mnt_opts = xstrdup(m->mnt_opts);
494 entries[count].mnt_freq = m->mnt_freq;
495 entries[count].mnt_passno = m->mnt_passno;
498 endmntent(mountTable);
500 mountTable = setmntent(bb_path_mtab_file, "w");
502 for (i = 0; i < count; i++) {
503 if (strcmp(entries[i].mnt_dir, mp->mnt_fsname) != 0)
504 addmntent(mountTable, &entries[i]);
506 addmntent(mountTable, mp);
508 endmntent(mountTable);
509 } else if (errno != EROFS)
510 bb_perror_msg(bb_path_mtab_file);
512 if (ENABLE_FEATURE_CLEAN_UP) {
513 for (i = 0; i < count; i++) {
514 free(entries[i].mnt_fsname);
515 free(entries[i].mnt_dir);
516 free(entries[i].mnt_type);
517 free(entries[i].mnt_opts);
524 #if ENABLE_FEATURE_MOUNT_VERBOSE
525 static int verbose_mount(const char *source, const char *target,
526 const char *filesystemtype,
527 unsigned long mountflags, const void *data)
532 rc = mount(source, target, filesystemtype, mountflags, data);
534 bb_perror_msg("mount('%s','%s','%s',0x%08lx,'%s'):%d",
535 source, target, filesystemtype,
536 mountflags, (char*)data, rc);
540 #define verbose_mount(...) mount(__VA_ARGS__)
543 // Append mount options to string
544 static void append_mount_options(char **oldopts, const char *newopts)
546 if (*oldopts && **oldopts) {
547 // Do not insert options which are already there
550 int len = strlen(newopts);
551 p = strchr(newopts, ',');
552 if (p) len = p - newopts;
555 if (!strncmp(p, newopts, len)
556 && (p[len] == ',' || p[len] == '\0'))
562 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
567 while (newopts[0] == ',') newopts++;
570 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
571 *oldopts = xstrdup(newopts);
575 // Use the mount_options list to parse options into flags.
576 // Also update list of unrecognized options if unrecognized != NULL
577 static unsigned long parse_mount_options(char *options, char **unrecognized)
579 unsigned long flags = MS_SILENT;
581 // Loop through options
584 char *comma = strchr(options, ',');
585 const char *option_str = mount_option_str;
587 if (comma) *comma = '\0';
589 // FIXME: use hasmntopt()
590 // Find this option in mount_options
591 for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
592 unsigned opt_len = strlen(option_str);
594 if (strncasecmp(option_str, options, opt_len) == 0
595 && (options[opt_len] == '\0'
596 /* or is it "comment=" thingy in fstab? */
597 IF_FEATURE_MOUNT_FSTAB(IF_DESKTOP( || option_str[opt_len-1] == '=' ))
600 unsigned long fl = mount_options[i];
601 if (fl & BB_MS_INVERTED_VALUE)
607 option_str += opt_len + 1;
609 // We did not recognize this option.
610 // If "unrecognized" is not NULL, append option there.
611 // Note that we should not append *empty* option -
612 // in this case we want to pass NULL, not "", to "data"
613 // parameter of mount(2) syscall.
614 // This is crucial for filesystems that don't accept
615 // any arbitrary mount options, like cgroup fs:
616 // "mount -t cgroup none /mnt"
617 if (options[0] && unrecognized) {
618 // Add it to strflags, to pass on to kernel
619 char *p = *unrecognized;
620 unsigned len = p ? strlen(p) : 0;
621 *unrecognized = p = xrealloc(p, len + strlen(options) + 2);
623 // Comma separated if it's not the first one
624 if (len) p[len++] = ',';
625 strcpy(p + len, options);
630 // Advance to next option
638 // Return a list of all block device backed filesystems
639 static llist_t *get_block_backed_filesystems(void)
641 static const char filesystems[2][sizeof("/proc/filesystems")] = {
646 llist_t *list = NULL;
650 for (i = 0; i < 2; i++) {
651 f = fopen_for_read(filesystems[i]);
654 while ((buf = xmalloc_fgetline(f)) != NULL) {
655 if (is_prefixed_with(buf, "nodev") && isspace(buf[5]))
657 fs = skip_whitespace(buf);
658 if (*fs == '#' || *fs == '*' || !*fs)
661 llist_add_to_end(&list, xstrdup(fs));
665 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
671 #if ENABLE_FEATURE_CLEAN_UP
672 static void delete_block_backed_filesystems(void)
674 llist_free(fslist, free);
677 void delete_block_backed_filesystems(void);
680 // Perform actual mount of specific filesystem at specific location.
681 // NB: mp->xxx fields may be trashed on exit
682 static int mount_it_now(struct mntent *mp, unsigned long vfsflags, char *filteropts)
686 vfsflags &= ~(unsigned long)MOUNT_FAKEFLAGS;
690 bb_error_msg("would do mount('%s','%s','%s',0x%08lx,'%s')",
691 mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
692 vfsflags, filteropts);
696 // Mount, with fallback to read-only if necessary.
699 rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
700 vfsflags, filteropts);
702 // If mount failed, try
703 // helper program mount.<mnt_type>
704 if (HELPERS_ALLOWED && rc && mp->mnt_type) {
706 int errno_save = errno;
707 args[0] = xasprintf("mount.%s", mp->mnt_type);
710 args[rc++] = (char *)"-f";
711 if (ENABLE_FEATURE_MTAB_SUPPORT && !USE_MTAB)
712 args[rc++] = (char *)"-n";
713 args[rc++] = mp->mnt_fsname;
714 args[rc++] = mp->mnt_dir;
716 args[rc++] = (char *)"-o";
717 args[rc++] = filteropts;
720 rc = spawn_and_wait(args);
727 if (!rc || (vfsflags & MS_RDONLY) || (errno != EACCES && errno != EROFS))
729 if (!(vfsflags & MS_SILENT))
730 bb_error_msg("%s is write-protected, mounting read-only",
732 vfsflags |= MS_RDONLY;
735 // Abort entirely if permission denied.
737 if (rc && errno == EPERM)
738 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
740 // If the mount was successful, and we're maintaining an old-style
741 // mtab file by hand, add the new entry to it now.
743 if (USE_MTAB && !rc && !(vfsflags & MS_REMOUNT)) {
745 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
746 const char *option_str = mount_option_str;
750 bb_perror_msg(bb_path_mtab_file);
754 // Add vfs string flags
755 for (i = 0; mount_options[i] != MS_REMOUNT; i++) {
756 if (mount_options[i] > 0 && (mount_options[i] & vfsflags))
757 append_mount_options(&(mp->mnt_opts), option_str);
758 option_str += strlen(option_str) + 1;
761 // Remove trailing / (if any) from directory we mounted on
762 i = strlen(mp->mnt_dir) - 1;
763 while (i > 0 && mp->mnt_dir[i] == '/')
764 mp->mnt_dir[i--] = '\0';
766 // Convert to canonical pathnames as needed
767 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
769 if (!mp->mnt_type || !*mp->mnt_type) { // bind mount
770 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
771 mp->mnt_type = (char*)"bind";
773 mp->mnt_freq = mp->mnt_passno = 0;
776 #if ENABLE_FEATURE_MTAB_SUPPORT
777 if (vfsflags & MS_MOVE)
778 update_mtab_entry_on_move(mp);
781 addmntent(mountTable, mp);
782 endmntent(mountTable);
784 if (ENABLE_FEATURE_CLEAN_UP) {
793 #if ENABLE_FEATURE_MOUNT_NFS
797 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
799 * Licensed under GPLv2, see file LICENSE in this source tree.
801 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
802 * numbers to be specified on the command line.
804 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
805 * Omit the call to connect() for Linux version 1.3.11 or later.
807 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
808 * Implemented the "bg", "fg" and "retry" mount options for NFS.
810 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
811 * - added Native Language Support
813 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
817 #define MOUNTPORT 635
818 #define MNTPATHLEN 1024
819 #define MNTNAMLEN 255
823 typedef char fhandle[FHSIZE];
826 unsigned int fhandle3_len;
838 MNT3ERR_NAMETOOLONG = 63,
839 MNT3ERR_NOTSUPP = 10004,
840 MNT3ERR_SERVERFAULT = 10006,
842 typedef enum mountstat3 mountstat3;
845 unsigned int fhs_status;
850 typedef struct fhstatus fhstatus;
852 struct mountres3_ok {
855 unsigned int auth_flavours_len;
856 char *auth_flavours_val;
859 typedef struct mountres3_ok mountres3_ok;
862 mountstat3 fhs_status;
864 mountres3_ok mountinfo;
867 typedef struct mountres3 mountres3;
869 typedef char *dirpath;
873 typedef struct mountbody *mountlist;
877 dirpath ml_directory;
880 typedef struct mountbody mountbody;
882 typedef struct groupnode *groups;
888 typedef struct groupnode groupnode;
890 typedef struct exportnode *exports;
897 typedef struct exportnode exportnode;
910 typedef struct ppathcnf ppathcnf;
912 #define MOUNTPROG 100005
915 #define MOUNTPROC_NULL 0
916 #define MOUNTPROC_MNT 1
917 #define MOUNTPROC_DUMP 2
918 #define MOUNTPROC_UMNT 3
919 #define MOUNTPROC_UMNTALL 4
920 #define MOUNTPROC_EXPORT 5
921 #define MOUNTPROC_EXPORTALL 6
923 #define MOUNTVERS_POSIX 2
925 #define MOUNTPROC_PATHCONF 7
929 #define MOUNTPROC3_NULL 0
930 #define MOUNTPROC3_MNT 1
931 #define MOUNTPROC3_DUMP 2
932 #define MOUNTPROC3_UMNT 3
933 #define MOUNTPROC3_UMNTALL 4
934 #define MOUNTPROC3_EXPORT 5
946 * We want to be able to compile mount on old kernels in such a way
947 * that the binary will work well on more recent kernels.
948 * Thus, if necessary we teach nfsmount.c the structure of new fields
949 * that will come later.
951 * Moreover, the new kernel includes conflict with glibc includes
952 * so it is easiest to ignore the kernel altogether (at compile time).
960 unsigned char data[64];
963 struct nfs_mount_data {
966 struct nfs2_fh old_root; /* 1 */
972 int acregmin; /* 1 */
973 int acregmax; /* 1 */
974 int acdirmin; /* 1 */
975 int acdirmax; /* 1 */
976 struct sockaddr_in addr; /* 1 */
977 char hostname[256]; /* 1 */
979 unsigned int bsize; /* 3 */
980 struct nfs3_fh root; /* 4 */
983 /* bits in the flags field */
985 NFS_MOUNT_SOFT = 0x0001, /* 1 */
986 NFS_MOUNT_INTR = 0x0002, /* 1 */
987 NFS_MOUNT_SECURE = 0x0004, /* 1 */
988 NFS_MOUNT_POSIX = 0x0008, /* 1 */
989 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
990 NFS_MOUNT_NOAC = 0x0020, /* 1 */
991 NFS_MOUNT_TCP = 0x0040, /* 2 */
992 NFS_MOUNT_VER3 = 0x0080, /* 3 */
993 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
994 NFS_MOUNT_NONLM = 0x0200, /* 3 */
995 NFS_MOUNT_NOACL = 0x0800, /* 4 */
996 NFS_MOUNT_NORDIRPLUS = 0x4000
1001 * We need to translate between nfs status return values and
1002 * the local errno values which may not be the same.
1004 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
1005 * "after #include <errno.h> the symbol errno is reserved for any use,
1006 * it cannot even be used as a struct tag or field name".
1009 # define EDQUOT ENOSPC
1011 /* Convert each NFSERR_BLAH into EBLAH */
1012 static const uint8_t nfs_err_stat[] ALIGN1 = {
1014 19, 20, 21, 22, 27, 28,
1015 30, 63, 66, 69, 70, 71
1018 EPERM | ENOENT | EIO | ENXIO | EACCES| EEXIST | \
1019 ENODEV| ENOTDIR | EISDIR | EINVAL| EFBIG | ENOSPC | \
1020 EROFS | ENAMETOOLONG| ENOTEMPTY| EDQUOT| ESTALE| EREMOTE) < 256
1021 typedef uint8_t nfs_err_type;
1023 typedef uint16_t nfs_err_type;
1025 static const nfs_err_type nfs_err_errnum[] ALIGN2 = {
1026 EPERM , ENOENT , EIO , ENXIO , EACCES, EEXIST,
1027 ENODEV, ENOTDIR , EISDIR , EINVAL, EFBIG , ENOSPC,
1028 EROFS , ENAMETOOLONG, ENOTEMPTY, EDQUOT, ESTALE, EREMOTE
1030 static char *nfs_strerror(int status)
1034 for (i = 0; i < ARRAY_SIZE(nfs_err_stat); i++) {
1035 if (nfs_err_stat[i] == status)
1036 return strerror(nfs_err_errnum[i]);
1038 return xasprintf("unknown nfs status return value: %d", status);
1041 static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
1043 return xdr_opaque(xdrs, objp, FHSIZE);
1046 static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
1048 if (!xdr_u_int(xdrs, &objp->fhs_status))
1050 if (objp->fhs_status == 0)
1051 return xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle);
1055 static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
1057 return xdr_string(xdrs, objp, MNTPATHLEN);
1060 static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
1062 return xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
1063 (unsigned int *) &objp->fhandle3_len,
1067 static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
1069 if (!xdr_fhandle3(xdrs, &objp->fhandle))
1071 return xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val),
1072 &(objp->auth_flavours.auth_flavours_len),
1075 (xdrproc_t) xdr_int);
1078 static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
1080 return xdr_enum(xdrs, (enum_t *) objp);
1083 static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
1085 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
1087 if (objp->fhs_status == MNT_OK)
1088 return xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo);
1092 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
1095 * Unfortunately, the kernel prints annoying console messages
1096 * in case of an unexpected nfs mount version (instead of
1097 * just returning some error). Therefore we'll have to try
1098 * and figure out what version the kernel expects.
1101 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
1102 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
1103 * nfs_mount_version: version this source and running kernel can handle
1106 find_kernel_nfs_mount_version(void)
1110 if (nfs_mount_version)
1113 nfs_mount_version = 4; /* default */
1115 kernel_version = get_linux_version_code();
1116 if (kernel_version) {
1117 if (kernel_version < KERNEL_VERSION(2,2,18))
1118 nfs_mount_version = 3;
1119 /* else v4 since 2.3.99pre4 */
1124 get_mountport(struct pmap *pm_mnt,
1125 struct sockaddr_in *server_addr,
1127 long unsigned version,
1128 long unsigned proto,
1131 struct pmaplist *pmap;
1133 server_addr->sin_port = PMAPPORT;
1134 /* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
1135 * I understand it like "IPv6 for this is not 100% ready" */
1136 pmap = pmap_getmaps(server_addr);
1138 if (version > MAX_NFSPROT)
1139 version = MAX_NFSPROT;
1142 pm_mnt->pm_prog = prog;
1143 pm_mnt->pm_vers = version;
1144 pm_mnt->pm_prot = proto;
1145 pm_mnt->pm_port = port;
1148 if (pmap->pml_map.pm_prog != prog)
1150 if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers)
1152 if (version > 2 && pmap->pml_map.pm_vers != version)
1154 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
1156 if (pmap->pml_map.pm_vers > MAX_NFSPROT
1157 || (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto)
1158 || (port && pmap->pml_map.pm_port != port)
1162 memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt));
1164 pmap = pmap->pml_next;
1166 if (!pm_mnt->pm_vers)
1167 pm_mnt->pm_vers = MOUNTVERS;
1168 if (!pm_mnt->pm_port)
1169 pm_mnt->pm_port = MOUNTPORT;
1170 if (!pm_mnt->pm_prot)
1171 pm_mnt->pm_prot = IPPROTO_TCP;
1175 static int daemonize(void)
1178 if (pid < 0) /* error */
1180 if (pid > 0) /* parent */
1184 xopen(bb_dev_null, O_RDWR);
1188 openlog(applet_name, LOG_PID, LOG_DAEMON);
1189 logmode = LOGMODE_SYSLOG;
1193 static inline int daemonize(void) { return -ENOSYS; }
1197 static inline int we_saw_this_host_before(const char *hostname UNUSED_PARAM)
1202 /* RPC strerror analogs are terminally idiotic:
1203 * *mandatory* prefix and \n at end.
1204 * This hopefully helps. Usage:
1205 * error_msg_rpc(clnt_*error*(" ")) */
1206 static void error_msg_rpc(const char *msg)
1209 while (msg[0] == ' ' || msg[0] == ':') msg++;
1211 while (len && msg[len-1] == '\n') len--;
1212 bb_error_msg("%.*s", len, msg);
1215 /* NB: mp->xxx fields may be trashed on exit */
1216 static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
1222 /* prior to 2.6.23, kernel took NFS options in a form of this struct
1223 * only. 2.6.23+ looks at data->version, and if it's not 1..6,
1224 * then data pointer is interpreted as a string. */
1225 struct nfs_mount_data data;
1228 struct sockaddr_in server_addr;
1229 struct sockaddr_in mount_server_addr;
1232 struct fhstatus nfsv2;
1233 struct mountres3 nfsv3;
1251 /* these all are one-bit really. gcc 4.3.1 likes this combination: */
1262 find_kernel_nfs_mount_version();
1270 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
1272 filteropts = xstrdup(filteropts); /* going to trash it later... */
1274 hostname = xstrdup(mp->mnt_fsname);
1275 /* mount_main() guarantees that ':' is there */
1276 s = strchr(hostname, ':');
1279 /* Ignore all but first hostname in replicated mounts
1280 * until they can be fully supported. (mack@sgi.com) */
1281 s = strchr(hostname, ',');
1284 bb_error_msg("warning: multiple hostnames not supported");
1287 server_addr.sin_family = AF_INET;
1288 if (!inet_aton(hostname, &server_addr.sin_addr)) {
1289 hp = gethostbyname(hostname);
1291 bb_herror_msg("%s", hostname);
1294 if (hp->h_length != (int)sizeof(struct in_addr)) {
1295 bb_error_msg_and_die("only IPv4 is supported");
1297 memcpy(&server_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
1300 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
1302 /* add IP address to mtab options for use when unmounting */
1304 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
1305 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
1307 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
1308 mp->mnt_opts[0] ? "," : "",
1309 inet_ntoa(server_addr.sin_addr));
1314 /* Set default options.
1315 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
1316 * let the kernel decide.
1317 * timeo is filled in after we know whether it'll be TCP or UDP. */
1318 memset(&data, 0, sizeof(data));
1324 data.namlen = NAME_MAX;
1334 retry = 10000; /* 10000 minutes ~ 1 week */
1335 tcp = 1; /* nfs-utils uses tcp per default */
1337 mountprog = MOUNTPROG;
1345 if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
1346 char *opteq = strchr(opt, '=');
1349 static const char options[] ALIGN1 =
1354 /* 4 */ "acregmin\0"
1355 /* 5 */ "acregmax\0"
1356 /* 6 */ "acdirmin\0"
1357 /* 7 */ "acdirmax\0"
1361 /* 11 */ "mountport\0"
1362 /* 12 */ "mounthost\0"
1363 /* 13 */ "mountprog\0"
1364 /* 14 */ "mountvers\0"
1365 /* 15 */ "nfsprog\0"
1366 /* 16 */ "nfsvers\0"
1373 idx = index_in_strings(options, opt);
1375 case 12: // "mounthost"
1376 mounthost = xstrndup(opteq,
1377 strcspn(opteq, " \t\n\r,"));
1380 if (is_prefixed_with(opteq, "tcp"))
1382 else if (is_prefixed_with(opteq, "udp"))
1385 bb_error_msg("warning: unrecognized proto= option");
1387 case 20: // "addr" - ignore
1390 if (vfsflags & MS_REMOUNT)
1394 val = xatoi_positive(opteq);
1405 case 3: // "retrans"
1408 case 4: // "acregmin"
1409 data.acregmin = val;
1411 case 5: // "acregmax"
1412 data.acregmax = val;
1414 case 6: // "acdirmin"
1415 data.acdirmin = val;
1417 case 7: // "acdirmax"
1418 data.acdirmax = val;
1420 case 8: // "actimeo"
1421 data.acregmin = val;
1422 data.acregmax = val;
1423 data.acdirmin = val;
1424 data.acdirmax = val;
1432 case 11: // "mountport"
1435 case 13: // "mountprog"
1438 case 14: // "mountvers"
1441 case 15: // "nfsprog"
1444 case 16: // "nfsvers"
1448 case 19: // "namlen"
1449 //if (nfs_mount_version >= 2)
1452 // bb_error_msg("warning: option namlen is not supported\n");
1455 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
1459 else { /* not of the form opt=val */
1460 static const char options[] ALIGN1 =
1475 if (is_prefixed_with(opt, "no")) {
1479 switch (index_in_strings(options, opt)) {
1515 if (nfs_mount_version >= 3)
1518 bb_error_msg("warning: option nolock is not supported");
1527 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1532 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1534 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1535 | (intr ? NFS_MOUNT_INTR : 0)
1536 | (posix ? NFS_MOUNT_POSIX : 0)
1537 | (nocto ? NFS_MOUNT_NOCTO : 0)
1538 | (noac ? NFS_MOUNT_NOAC : 0)
1539 | (nordirplus ? NFS_MOUNT_NORDIRPLUS : 0)
1540 | (noacl ? NFS_MOUNT_NOACL : 0);
1541 if (nfs_mount_version >= 2)
1542 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1543 if (nfs_mount_version >= 3)
1544 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1545 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1546 bb_error_msg("NFSv%d not supported", nfsvers);
1549 if (nfsvers && !mountvers)
1550 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1551 if (nfsvers && nfsvers < mountvers) {
1552 mountvers = nfsvers;
1555 /* Adjust options if none specified */
1557 data.timeo = tcp ? 70 : 7;
1559 data.version = nfs_mount_version;
1561 if (vfsflags & MS_REMOUNT)
1565 * If the previous mount operation on the same host was
1566 * backgrounded, and the "bg" for this mount is also set,
1567 * give up immediately, to avoid the initial timeout.
1569 if (bg && we_saw_this_host_before(hostname)) {
1570 daemonized = daemonize();
1571 if (daemonized <= 0) { /* parent or error */
1572 retval = -daemonized;
1577 /* Create mount daemon client */
1578 /* See if the nfs host = mount host. */
1580 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1581 mount_server_addr.sin_family = AF_INET;
1582 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1584 hp = gethostbyname(mounthost);
1586 bb_herror_msg("%s", mounthost);
1589 if (hp->h_length != (int)sizeof(struct in_addr)) {
1590 bb_error_msg_and_die("only IPv4 is supported");
1592 mount_server_addr.sin_family = AF_INET;
1593 memcpy(&mount_server_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
1598 * The following loop implements the mount retries. When the mount
1599 * times out, and the "bg" option is set, we background ourself
1600 * and continue trying.
1602 * The case where the mount point is not present and the "bg"
1603 * option is set, is treated as a timeout. This is done to
1604 * support nested mounts.
1606 * The "retry" count specified by the user is the number of
1607 * minutes to retry before giving up.
1610 struct timeval total_timeout;
1611 struct timeval retry_timeout;
1617 retry_timeout.tv_sec = 3;
1618 retry_timeout.tv_usec = 0;
1619 total_timeout.tv_sec = 20;
1620 total_timeout.tv_usec = 0;
1621 /* FIXME: use monotonic()? */
1622 timeout = time(NULL) + 60 * retry;
1626 /* Be careful not to use too many CPU cycles */
1630 get_mountport(&pm_mnt, &mount_server_addr,
1635 nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers;
1637 /* contact the mount daemon via TCP */
1638 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1639 msock = RPC_ANYSOCK;
1641 switch (pm_mnt.pm_prot) {
1643 mclient = clntudp_create(&mount_server_addr,
1650 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1651 msock = RPC_ANYSOCK;
1653 mclient = clnttcp_create(&mount_server_addr,
1662 if (!daemonized && prevt == 0)
1663 error_msg_rpc(clnt_spcreateerror(" "));
1665 enum clnt_stat clnt_stat;
1667 /* Try to mount hostname:pathname */
1668 mclient->cl_auth = authunix_create_default();
1670 /* Make pointers in xdr_mountres3 NULL so
1671 * that xdr_array allocates memory for us
1673 memset(&status, 0, sizeof(status));
1675 if (pm_mnt.pm_vers == 3)
1676 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1677 (xdrproc_t) xdr_dirpath,
1678 (caddr_t) &pathname,
1679 (xdrproc_t) xdr_mountres3,
1683 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1684 (xdrproc_t) xdr_dirpath,
1685 (caddr_t) &pathname,
1686 (xdrproc_t) xdr_fhstatus,
1690 if (clnt_stat == RPC_SUCCESS)
1691 goto prepare_kernel_data; /* we're done */
1692 if (errno != ECONNREFUSED) {
1693 error_msg_rpc(clnt_sperror(mclient, " "));
1694 goto fail; /* don't retry */
1696 /* Connection refused */
1697 if (!daemonized && prevt == 0) /* print just once */
1698 error_msg_rpc(clnt_sperror(mclient, " "));
1699 auth_destroy(mclient->cl_auth);
1700 clnt_destroy(mclient);
1706 /* Timeout. We are going to retry... maybe */
1710 daemonized = daemonize();
1711 if (daemonized <= 0) { /* parent or error */
1712 retval = -daemonized;
1719 /* TODO error message */
1725 prepare_kernel_data:
1728 if (status.nfsv2.fhs_status != 0) {
1729 bb_error_msg("%s:%s failed, reason given by server: %s",
1731 nfs_strerror(status.nfsv2.fhs_status));
1734 memcpy(data.root.data,
1735 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1737 data.root.size = NFS_FHSIZE;
1738 memcpy(data.old_root.data,
1739 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1742 fhandle3 *my_fhandle;
1743 if (status.nfsv3.fhs_status != 0) {
1744 bb_error_msg("%s:%s failed, reason given by server: %s",
1746 nfs_strerror(status.nfsv3.fhs_status));
1749 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1750 memset(data.old_root.data, 0, NFS_FHSIZE);
1751 memset(&data.root, 0, sizeof(data.root));
1752 data.root.size = my_fhandle->fhandle3_len;
1753 memcpy(data.root.data,
1754 (char *) my_fhandle->fhandle3_val,
1755 my_fhandle->fhandle3_len);
1757 data.flags |= NFS_MOUNT_VER3;
1760 /* Create nfs socket for kernel */
1762 if (nfs_mount_version < 3) {
1763 bb_error_msg("NFS over TCP is not supported");
1766 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1768 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1770 bb_perror_msg("nfs socket");
1773 if (bindresvport(fsock, 0) < 0) {
1774 bb_perror_msg("nfs bindresvport");
1778 server_addr.sin_port = PMAPPORT;
1779 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1780 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1784 server_addr.sin_port = htons(port);
1786 /* Prepare data structure for kernel */
1788 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1789 strncpy(data.hostname, hostname, sizeof(data.hostname));
1792 auth_destroy(mclient->cl_auth);
1793 clnt_destroy(mclient);
1798 /* We must wait until mount directory is available */
1799 struct stat statbuf;
1801 while (stat(mp->mnt_dir, &statbuf) == -1) {
1803 daemonized = daemonize();
1804 if (daemonized <= 0) { /* parent or error */
1805 /* FIXME: parent doesn't close fsock - ??! */
1806 retval = -daemonized;
1810 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1817 /* Perform actual mount */
1819 retval = mount_it_now(mp, vfsflags, (char*)&data);
1826 auth_destroy(mclient->cl_auth);
1827 clnt_destroy(mclient);
1841 #else // !ENABLE_FEATURE_MOUNT_NFS
1843 /* Linux 2.6.23+ supports nfs mounts with options passed as a string.
1844 * For older kernels, you must build busybox with ENABLE_FEATURE_MOUNT_NFS.
1845 * (However, note that then you lose any chances that NFS over IPv6 would work).
1847 static int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
1849 len_and_sockaddr *lsa;
1855 # if ENABLE_FEATURE_IPV6
1856 end = strchr(mp->mnt_fsname, ']');
1857 if (end && end[1] == ':')
1861 /* mount_main() guarantees that ':' is there */
1862 end = strchr(mp->mnt_fsname, ':');
1865 lsa = xhost2sockaddr(mp->mnt_fsname, /*port:*/ 0);
1867 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
1868 if (ENABLE_FEATURE_CLEAN_UP) free(lsa);
1869 opts = xasprintf("%s%saddr=%s",
1870 filteropts ? filteropts : "",
1871 filteropts ? "," : "",
1874 if (ENABLE_FEATURE_CLEAN_UP) free(dotted);
1875 ret = mount_it_now(mp, vfsflags, opts);
1876 if (ENABLE_FEATURE_CLEAN_UP) free(opts);
1881 #endif // !ENABLE_FEATURE_MOUNT_NFS
1883 // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1884 // type detection. Returns 0 for success, nonzero for failure.
1885 // NB: mp->xxx fields may be trashed on exit
1886 static int singlemount(struct mntent *mp, int ignore_busy)
1890 unsigned long vfsflags;
1891 char *loopFile = NULL, *filteropts = NULL;
1897 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1899 // Treat fstype "auto" as unspecified
1900 if (mp->mnt_type && strcmp(mp->mnt_type, "auto") == 0)
1901 mp->mnt_type = NULL;
1903 // Might this be a virtual filesystem?
1904 if (ENABLE_FEATURE_MOUNT_HELPERS && strchr(mp->mnt_fsname, '#')) {
1908 // fsname: "cmd#arg1#arg2..."
1909 // WARNING: allows execution of arbitrary commands!
1910 // Try "mount 'sh#-c#sh' bogus_dir".
1911 // It is safe ONLY because non-root
1912 // cannot use two-argument mount command
1913 // and using one-argument "mount 'sh#-c#sh'" doesn't work:
1914 // "mount: can't find sh#-c#sh in /etc/fstab"
1915 // (if /etc/fstab has it, it's ok: root sets up /etc/fstab).
1920 while (*s && n < 35 - 2) {
1921 if (*s++ == '#' && *s != '#') {
1926 args[n++] = mp->mnt_dir;
1928 rc = spawn_and_wait(args);
1932 // Might this be an CIFS filesystem?
1933 if (ENABLE_FEATURE_MOUNT_CIFS
1934 && (!mp->mnt_type || strcmp(mp->mnt_type, "cifs") == 0)
1935 && (mp->mnt_fsname[0] == '/' || mp->mnt_fsname[0] == '\\')
1936 && mp->mnt_fsname[0] == mp->mnt_fsname[1]
1940 char *hostname, *share;
1941 len_and_sockaddr *lsa;
1943 // Parse mp->mnt_fsname of the form "//hostname/share[/dir1/dir2]"
1945 hostname = mp->mnt_fsname + 2;
1946 len = strcspn(hostname, "/\\");
1947 share = hostname + len + 1;
1948 if (len == 0 // 3rd char is a [back]slash (IOW: empty hostname)
1949 || share[-1] == '\0' // no [back]slash after hostname
1950 || share[0] == '\0' // empty share name
1956 len = strcspn(share, "/\\");
1958 // "unc=\\hostname\share" option is mandatory
1959 // after CIFS option parsing was rewritten in Linux 3.4.
1960 // Must use backslashes.
1961 // If /dir1/dir2 is present, also add "prefixpath=dir1/dir2"
1963 char *unc = xasprintf(
1964 share[len] != '\0' /* "/dir1/dir2" exists? */
1965 ? "unc=\\\\%s\\%.*s,prefixpath=%s"
1966 : "unc=\\\\%s\\%.*s",
1969 share + len + 1 /* "dir1/dir2" */
1971 parse_mount_options(unc, &filteropts);
1972 if (ENABLE_FEATURE_CLEAN_UP) free(unc);
1975 lsa = host2sockaddr(hostname, 0);
1980 // If there is no "ip=..." option yet
1981 if (!is_prefixed_with(filteropts, ",ip="+1)
1982 && !strstr(filteropts, ",ip=")
1985 // Insert "ip=..." option into options
1986 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
1987 if (ENABLE_FEATURE_CLEAN_UP) free(lsa);
1988 ip = xasprintf("ip=%s", dotted);
1989 if (ENABLE_FEATURE_CLEAN_UP) free(dotted);
1990 // Note: IPv6 scoped addresses ("host%iface", see RFC 4007) should be
1991 // handled by libc in getnameinfo() (inside xmalloc_sockaddr2dotted_noport()).
1992 // Currently, glibc does not support that (has no NI_NUMERICSCOPE),
1993 // musl apparently does. This results in "ip=numericIPv6%iface_name"
1994 // (instead of _numeric_ iface_id) with glibc.
1995 // This probably should be fixed in glibc, not here.
1996 // The workaround is to manually specify correct "ip=ADDR%n" option.
1997 parse_mount_options(ip, &filteropts);
1998 if (ENABLE_FEATURE_CLEAN_UP) free(ip);
2001 mp->mnt_type = (char*)"cifs";
2002 rc = mount_it_now(mp, vfsflags, filteropts);
2007 // Might this be an NFS filesystem?
2008 if ((!mp->mnt_type || is_prefixed_with(mp->mnt_type, "nfs"))
2009 && strchr(mp->mnt_fsname, ':') != NULL
2012 mp->mnt_type = (char*)"nfs";
2013 rc = nfsmount(mp, vfsflags, filteropts);
2017 // Look at the file. (Not found isn't a failure for remount, or for
2018 // a synthetic filesystem like proc or sysfs.)
2019 // (We use stat, not lstat, in order to allow
2020 // mount symlink_to_file_or_blkdev dir)
2021 if (!stat(mp->mnt_fsname, &st)
2022 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
2024 // Do we need to allocate a loopback device for it?
2025 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
2026 loopFile = bb_simplify_path(mp->mnt_fsname);
2027 mp->mnt_fsname = NULL; // will receive malloced loop dev name
2029 // mount always creates AUTOCLEARed loopdevs, so that umounting
2030 // drops them without any code in the userspace.
2031 // This happens since circa linux-2.6.25:
2032 // commit 96c5865559cee0f9cbc5173f3c949f6ce3525581
2033 // Date: Wed Feb 6 01:36:27 2008 -0800
2034 // Subject: Allow auto-destruction of loop devices
2035 loopfd = set_loop(&mp->mnt_fsname,
2038 ((vfsflags & MS_RDONLY) ? BB_LO_FLAGS_READ_ONLY : 0)
2039 | BB_LO_FLAGS_AUTOCLEAR
2042 if (errno == EPERM || errno == EACCES)
2043 bb_error_msg(bb_msg_perm_denied_are_you_root);
2045 bb_perror_msg("can't setup loop device");
2049 // Autodetect bind mounts
2050 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
2051 vfsflags |= MS_BIND;
2054 // If we know the fstype (or don't need to), jump straight
2055 // to the actual mount.
2056 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {
2059 next = mp->mnt_type ? strchr(mp->mnt_type, ',') : NULL;
2062 rc = mount_it_now(mp, vfsflags, filteropts);
2063 if (rc == 0 || !next)
2065 mp->mnt_type = next + 1;
2068 // Loop through filesystem types until mount succeeds
2071 // Initialize list of block backed filesystems.
2072 // This has to be done here so that during "mount -a",
2073 // mounts after /proc shows up can autodetect.
2075 fslist = get_block_backed_filesystems();
2076 if (ENABLE_FEATURE_CLEAN_UP && fslist)
2077 atexit(delete_block_backed_filesystems);
2080 for (fl = fslist; fl; fl = fl->link) {
2081 mp->mnt_type = fl->data;
2082 rc = mount_it_now(mp, vfsflags, filteropts);
2088 // If mount failed, clean up loop file (if any).
2089 // (Newer kernels which support LO_FLAGS_AUTOCLEAR should not need this,
2090 // merely "close(loopfd)" should do it?)
2091 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
2092 del_loop(mp->mnt_fsname);
2093 if (ENABLE_FEATURE_CLEAN_UP) {
2095 /* No, "rc != 0" needs it: free(mp->mnt_fsname); */
2100 if (ENABLE_FEATURE_CLEAN_UP)
2106 if (errno == EBUSY && ignore_busy)
2108 if (errno == ENOENT && (vfsflags & MOUNT_NOFAIL))
2111 bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
2116 // -O interprets a list of filter options which select whether a mount
2117 // point will be mounted: only mounts with options matching *all* filtering
2118 // options will be selected.
2119 // By default each -O filter option must be present in the list of mount
2120 // options, but if it is prefixed by "no" then it must be absent.
2122 // -O a,nob,c matches -o a,c but fails to match -o a,b,c
2123 // (and also fails to match -o a because -o c is absent).
2125 // It is different from -t in that each option is matched exactly; a leading
2126 // "no" at the beginning of one option does not negate the rest.
2127 static int match_opt(const char *fs_opt_in, const char *O_opt)
2133 const char *fs_opt = fs_opt_in;
2137 // If option begins with "no" then treat as an inverted match:
2138 // matching is a failure
2140 if (O_opt[0] == 'n' && O_opt[1] == 'o') {
2144 // Isolate the current O option
2145 O_len = strchrnul(O_opt, ',') - O_opt;
2146 // Check for a match against existing options
2148 if (strncmp(fs_opt, O_opt, O_len) == 0
2149 && (fs_opt[O_len] == '\0' || fs_opt[O_len] == ',')
2152 return 0; // "no" prefix, but option found
2153 match = 1; // current O option found, go check next one
2156 fs_opt = strchr(fs_opt, ',');
2162 return 0; // match wanted but not found
2163 if (O_opt[O_len] == '\0') // end?
2165 // Step to the next O option
2168 // If we get here then everything matched
2172 // Parse options, if necessary parse fstab/mtab, and call singlemount for
2173 // each directory to be mounted.
2174 int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2175 int mount_main(int argc UNUSED_PARAM, char **argv)
2177 char *cmdopts = xzalloc(1);
2178 char *fstype = NULL;
2179 char *O_optmatch = NULL;
2181 llist_t *lst_o = NULL;
2182 const char *fstabname = "/etc/fstab";
2185 int rc = EXIT_SUCCESS;
2186 unsigned long cmdopt_flags;
2188 struct mntent mtpair[2], *mtcur = mtpair;
2189 IF_NOT_DESKTOP(const int nonroot = 0;)
2191 IF_DESKTOP(int nonroot = ) sanitize_env_if_suid();
2195 // Parse long options, like --bind and --move. Note that -o option
2196 // and --option are synonymous. Yes, this means --remount,rw works.
2197 for (i = j = 1; argv[i]; i++) {
2198 if (argv[i][0] == '-' && argv[i][1] == '-')
2199 append_mount_options(&cmdopts, argv[i] + 2);
2201 argv[j++] = argv[i];
2205 // Parse remaining options
2206 // Max 2 params; -o is a list, -v is a counter
2207 opt = getopt32(argv, "^"
2209 "\0" "?2"IF_FEATURE_MOUNT_VERBOSE("vv"),
2210 &lst_o, &fstype, &O_optmatch
2211 IF_FEATURE_MOUNT_OTHERTAB(, &fstabname)
2212 IF_FEATURE_MOUNT_VERBOSE(, &verbose)
2215 while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o
2216 if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r
2217 if (opt & OPT_w) append_mount_options(&cmdopts, "rw"); // -w
2220 // If we have no arguments, show currently mounted filesystems
2222 if (!(opt & OPT_a)) {
2223 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
2226 bb_error_msg_and_die("no %s", bb_path_mtab_file);
2228 while (getmntent_r(mountTable, &mtpair[0], getmntent_buf,
2231 // Don't show rootfs. FIXME: why??
2232 // util-linux 2.12a happily shows rootfs...
2233 //if (strcmp(mtpair->mnt_fsname, "rootfs") == 0) continue;
2235 if (!fstype || strcmp(mtpair->mnt_type, fstype) == 0)
2236 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
2237 mtpair->mnt_dir, mtpair->mnt_type,
2240 if (ENABLE_FEATURE_CLEAN_UP)
2241 endmntent(mountTable);
2242 return EXIT_SUCCESS;
2244 storage_path = NULL;
2246 // When we have two arguments, the second is the directory and we can
2247 // skip looking at fstab entirely. We can always abspath() the directory
2248 // argument when we get it.
2251 bb_error_msg_and_die(bb_msg_you_must_be_root);
2252 mtpair->mnt_fsname = argv[0];
2253 mtpair->mnt_dir = argv[1];
2254 mtpair->mnt_type = fstype;
2255 mtpair->mnt_opts = cmdopts;
2256 resolve_mount_spec(&mtpair->mnt_fsname);
2257 rc = singlemount(mtpair, /*ignore_busy:*/ 0);
2260 storage_path = bb_simplify_path(argv[0]); // malloced
2263 // Past this point, we are handling either "mount -a [opts]"
2264 // or "mount [opts] single_param"
2266 cmdopt_flags = parse_mount_options(cmdopts, NULL);
2267 if (nonroot && (cmdopt_flags & ~MS_SILENT)) // Non-root users cannot specify flags
2268 bb_error_msg_and_die(bb_msg_you_must_be_root);
2270 // If we have a shared subtree flag, don't worry about fstab or mtab.
2271 if (ENABLE_FEATURE_MOUNT_FLAGS
2272 && (cmdopt_flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2274 // verbose_mount(source, target, type, flags, data)
2275 rc = verbose_mount("", argv[0], "", cmdopt_flags, "");
2277 bb_simple_perror_msg_and_die(argv[0]);
2281 // A malicious user could overmount /usr without this.
2282 if (ENABLE_FEATURE_MOUNT_OTHERTAB && nonroot)
2283 fstabname = "/etc/fstab";
2284 // Open either fstab or mtab
2285 if (cmdopt_flags & MS_REMOUNT) {
2286 // WARNING. I am not sure this matches util-linux's
2287 // behavior. It's possible util-linux does not
2288 // take -o opts from mtab (takes only mount source).
2289 fstabname = bb_path_mtab_file;
2291 fstab = setmntent(fstabname, "r");
2293 bb_perror_msg_and_die("can't read '%s'", fstabname);
2295 // Loop through entries until we find what we're looking for
2296 memset(mtpair, 0, sizeof(mtpair));
2298 struct mntent *mtother = (mtcur==mtpair ? mtpair+1 : mtpair);
2300 // Get next fstab entry
2301 if (!getmntent_r(fstab, mtcur, getmntent_buf
2302 + (mtcur==mtpair ? GETMNTENT_BUFSIZE/2 : 0),
2303 GETMNTENT_BUFSIZE/2)
2304 ) { // End of fstab/mtab is reached
2305 mtcur = mtother; // the thing we found last time
2309 // If we're trying to mount something specific and this isn't it,
2310 // skip it. Note we must match the exact text in fstab (ala
2311 // "proc") or a full path from root
2314 // Is this what we're looking for?
2315 if (strcmp(argv[0], mtcur->mnt_fsname) != 0
2316 && strcmp(storage_path, mtcur->mnt_fsname) != 0
2317 && strcmp(argv[0], mtcur->mnt_dir) != 0
2318 && strcmp(storage_path, mtcur->mnt_dir) != 0
2323 // Remember this entry. Something later may have
2324 // overmounted it, and we want the _last_ match.
2327 // If we're mounting all
2330 // No, mount -a won't mount anything,
2331 // even user mounts, for mere humans
2333 bb_error_msg_and_die(bb_msg_you_must_be_root);
2335 // Does type match? (NULL matches always)
2336 if (!fstype_matches(mtcur->mnt_type, fstype))
2339 // Skip noauto and swap anyway
2340 if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP))
2341 // swap is bogus "fstype", parse_mount_options can't check fstypes
2342 || strcasecmp(mtcur->mnt_type, "swap") == 0
2347 // Does (at least one) option match?
2348 // (NULL matches always)
2349 if (!match_opt(mtcur->mnt_opts, O_optmatch))
2352 resolve_mount_spec(&mtcur->mnt_fsname);
2354 // NFS mounts want this to be xrealloc-able
2355 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
2357 // If nothing is mounted on this directory...
2358 // (otherwise repeated "mount -a" mounts everything again)
2359 mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0);
2360 // We do not check fsname match of found mount point -
2361 // "/" may have fsname of "/dev/root" while fstab
2362 // says "/dev/something_else".
2365 bb_error_msg("according to %s, "
2366 "%s is already mounted on %s",
2368 mp->mnt_fsname, mp->mnt_dir);
2371 // ...mount this thing
2372 if (singlemount(mtcur, /*ignore_busy:*/ 1)) {
2373 // Count number of failed mounts
2377 free(mtcur->mnt_opts);
2381 // End of fstab/mtab is reached.
2382 // Were we looking for something specific?
2383 if (argv[0]) { // yes
2386 // If we didn't find anything, complain
2387 if (!mtcur->mnt_fsname)
2388 bb_error_msg_and_die("can't find %s in %s",
2389 argv[0], fstabname);
2391 // What happens when we try to "mount swap_partition"?
2392 // (fstab containts "swap_partition swap swap defaults 0 0")
2393 // util-linux-ng 2.13.1 does this:
2394 // stat("/sbin/mount.swap", 0x7fff62a3a350) = -1 ENOENT (No such file or directory)
2395 // mount("swap_partition", "swap", "swap", MS_MGC_VAL, NULL) = -1 ENOENT (No such file or directory)
2396 // lstat("swap", 0x7fff62a3a640) = -1 ENOENT (No such file or directory)
2397 // write(2, "mount: mount point swap does not exist\n", 39) = 39
2398 // exit_group(32) = ?
2400 // In case we want to simply skip swap partitions:
2401 l = parse_mount_options(mtcur->mnt_opts, NULL);
2402 if ((l & MOUNT_SWAP)
2403 // swap is bogus "fstype", parse_mount_options can't check fstypes
2404 || strcasecmp(mtcur->mnt_type, "swap") == 0
2410 // fstab must have "users" or "user"
2411 l = parse_mount_options(mtcur->mnt_opts, NULL);
2412 if (!(l & MOUNT_USERS))
2413 bb_error_msg_and_die(bb_msg_you_must_be_root);
2416 //util-linux-2.12 does not do this check.
2417 //// If nothing is mounted on this directory...
2418 //// (otherwise repeated "mount FOO" mounts FOO again)
2419 //mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0);
2421 // bb_error_msg("according to %s, "
2422 // "%s is already mounted on %s",
2423 // bb_path_mtab_file,
2424 // mp->mnt_fsname, mp->mnt_dir);
2426 // ...mount the last thing we found
2427 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
2428 append_mount_options(&(mtcur->mnt_opts), cmdopts);
2429 resolve_mount_spec(&mtpair->mnt_fsname);
2430 rc = singlemount(mtcur, /*ignore_busy:*/ 0);
2431 if (ENABLE_FEATURE_CLEAN_UP)
2432 free(mtcur->mnt_opts);
2437 if (ENABLE_FEATURE_CLEAN_UP)
2439 if (ENABLE_FEATURE_CLEAN_UP) {
2444 //TODO: exitcode should be ORed mask of (from "man mount"):
2446 // 1 incorrect invocation or permissions
2447 // 2 system error (out of memory, cannot fork, no more loop devices)
2448 // 4 internal mount bug or missing nfs support in mount
2450 //16 problems writing or locking /etc/mtab
2452 //64 some mount succeeded