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"
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. Most people using BusyBox will also want to enable
30 //config: the 'mount' utility.
32 //config:config FEATURE_MOUNT_FAKE
33 //config: bool "Support option -f"
35 //config: depends on MOUNT
37 //config: Enable support for faking a file system mount.
39 //config:config FEATURE_MOUNT_VERBOSE
40 //config: bool "Support option -v"
42 //config: depends on MOUNT
44 //config: Enable multi-level -v[vv...] verbose messages. Useful if you
45 //config: debug mount problems and want to see what is exactly passed
46 //config: to the kernel.
48 //config:config FEATURE_MOUNT_HELPERS
49 //config: bool "Support mount helpers"
51 //config: depends on MOUNT
53 //config: Enable mounting of virtual file systems via external helpers.
54 //config: E.g. "mount obexfs#-b00.11.22.33.44.55 /mnt" will in effect call
55 //config: "obexfs -b00.11.22.33.44.55 /mnt"
56 //config: Also "mount -t sometype [-o opts] fs /mnt" will try
57 //config: "sometype [-o opts] fs /mnt" if simple mount syscall fails.
58 //config: The idea is to use such virtual filesystems in /etc/fstab.
60 //config:config FEATURE_MOUNT_LABEL
61 //config: bool "Support specifying devices by label or UUID"
63 //config: depends on MOUNT
64 //config: select VOLUMEID
66 //config: This allows for specifying a device by label or uuid, rather than by
67 //config: name. This feature utilizes the same functionality as blkid/findfs.
68 //config: This also enables label or uuid support for swapon.
70 //config:config FEATURE_MOUNT_NFS
71 //config: bool "Support mounting NFS file systems on Linux < 2.6.23"
73 //config: depends on MOUNT
74 //config: select FEATURE_HAVE_RPC
75 //config: select FEATURE_SYSLOG
77 //config: Enable mounting of NFS file systems on Linux kernels prior
78 //config: to version 2.6.23. Note that in this case mounting of NFS
79 //config: over IPv6 will not be possible.
81 //config: Note that this option links in RPC support from libc,
82 //config: which is rather large (~10 kbytes on uclibc).
84 //config:config FEATURE_MOUNT_CIFS
85 //config: bool "Support mounting CIFS/SMB file systems"
87 //config: depends on MOUNT
89 //config: Enable support for samba mounts.
91 //config:config FEATURE_MOUNT_FLAGS
92 //config: depends on MOUNT
93 //config: bool "Support lots of -o flags in mount"
96 //config: Without this, mount only supports ro/rw/remount. With this, it
97 //config: supports nosuid, suid, dev, nodev, exec, noexec, sync, async, atime,
98 //config: noatime, diratime, nodiratime, loud, bind, move, shared, slave,
99 //config: private, unbindable, rshared, rslave, rprivate, and runbindable.
101 //config:config FEATURE_MOUNT_FSTAB
102 //config: depends on MOUNT
103 //config: bool "Support /etc/fstab and -a"
106 //config: Support mount all and looking for files in /etc/fstab.
108 //config:config FEATURE_MOUNT_OTHERTAB
109 //config: depends on FEATURE_MOUNT_FSTAB
110 //config: bool "Support -T <alt_fstab>"
113 //config: Support mount -T (specifying an alternate fstab)
115 //usage:#define mount_trivial_usage
116 //usage: "[OPTIONS] [-o OPT] DEVICE NODE"
117 //usage:#define mount_full_usage "\n\n"
118 //usage: "Mount a filesystem. Filesystem autodetection requires /proc.\n"
119 //usage: "\n -a Mount all filesystems in fstab"
120 //usage: IF_FEATURE_MOUNT_FAKE(
121 //usage: IF_FEATURE_MTAB_SUPPORT(
122 //usage: "\n -f Update /etc/mtab, but don't mount"
124 //usage: IF_NOT_FEATURE_MTAB_SUPPORT(
125 //usage: "\n -f Dry run"
128 //usage: IF_FEATURE_MOUNT_HELPERS(
129 //usage: "\n -i Don't run mount helper"
131 //usage: IF_FEATURE_MTAB_SUPPORT(
132 //usage: "\n -n Don't update /etc/mtab"
134 //usage: IF_FEATURE_MOUNT_VERBOSE(
135 //usage: "\n -v Verbose"
137 ////usage: "\n -s Sloppy (ignored)"
138 //usage: "\n -r Read-only mount"
139 ////usage: "\n -w Read-write mount (default)"
140 //usage: "\n -t FSTYPE[,...] Filesystem type(s)"
141 //usage: IF_FEATURE_MOUNT_OTHERTAB(
142 //usage: "\n -T FILE Read FILE instead of /etc/fstab"
144 //usage: "\n -O OPT Mount only filesystems with option OPT (-a only)"
146 //usage: IF_FEATURE_MOUNT_LOOP(
147 //usage: "\n loop Ignored (loop devices are autodetected)"
149 //usage: IF_FEATURE_MOUNT_FLAGS(
150 //usage: "\n [a]sync Writes are [a]synchronous"
151 //usage: "\n [no]atime Disable/enable updates to inode access times"
152 //usage: "\n [no]diratime Disable/enable atime updates to directories"
153 //usage: "\n [no]relatime Disable/enable atime updates relative to modification time"
154 //usage: "\n [no]dev (Dis)allow use of special device files"
155 //usage: "\n [no]exec (Dis)allow use of executable files"
156 //usage: "\n [no]suid (Dis)allow set-user-id-root programs"
157 //usage: "\n [r]shared Convert [recursively] to a shared subtree"
158 //usage: "\n [r]slave Convert [recursively] to a slave subtree"
159 //usage: "\n [r]private Convert [recursively] to a private subtree"
160 //usage: "\n [un]bindable Make mount point [un]able to be bind mounted"
161 //usage: "\n [r]bind Bind a file or directory [recursively] to another location"
162 //usage: "\n move Relocate an existing mount point"
164 //usage: "\n remount Remount a mounted filesystem, changing flags"
165 //usage: "\n ro Same as -r"
167 //usage: "\nThere are filesystem-specific -o flags."
169 //usage:#define mount_example_usage
171 //usage: "/dev/hda3 on / type minix (rw)\n"
172 //usage: "proc on /proc type proc (rw)\n"
173 //usage: "devpts on /dev/pts type devpts (rw)\n"
174 //usage: "$ mount /dev/fd0 /mnt -t msdos -o ro\n"
175 //usage: "$ mount /tmp/diskimage /opt -t ext2 -o loop\n"
176 //usage: "$ mount cd_image.iso mydir\n"
177 //usage:#define mount_notes_usage
178 //usage: "Returns 0 for success, number of failed mounts for -a, or errno for one mount."
182 #include <sys/mount.h>
183 // Grab more as needed from util-linux's mount/mount_constants.h
185 # define MS_DIRSYNC (1 << 7) // Directory modifications are synchronous
188 # define MS_UNION (1 << 8)
191 # define MS_BIND (1 << 12)
194 # define MS_MOVE (1 << 13)
197 # define MS_RECURSIVE (1 << 14)
200 # define MS_SILENT (1 << 15)
202 // The shared subtree stuff, which went in around 2.6.15
203 #ifndef MS_UNBINDABLE
204 # define MS_UNBINDABLE (1 << 17)
207 # define MS_PRIVATE (1 << 18)
210 # define MS_SLAVE (1 << 19)
213 # define MS_SHARED (1 << 20)
216 # define MS_RELATIME (1 << 21)
218 #ifndef MS_STRICTATIME
219 # define MS_STRICTATIME (1 << 24)
222 /* Any ~MS_FOO value has this bit set: */
223 #define BB_MS_INVERTED_VALUE (1u << 31)
226 #include "common_bufsiz.h"
227 #if ENABLE_FEATURE_MOUNT_LABEL
228 # include "volume_id.h"
230 # define resolve_mount_spec(fsname) ((void)0)
233 // Needed for nfs support only
234 #include <sys/utsname.h>
237 #if ENABLE_FEATURE_MOUNT_NFS
238 /* This is just a warning of a common mistake. Possibly this should be a
239 * uclibc faq entry rather than in busybox... */
240 # if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
241 # error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support"
243 # include <rpc/rpc.h>
244 # include <rpc/pmap_prot.h>
245 # include <rpc/pmap_clnt.h>
249 #if defined(__dietlibc__)
250 // 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
251 // dietlibc-0.30 does not have implementation of getmntent_r()
252 static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
253 char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
255 struct mntent* ment = getmntent(stream);
256 return memcpy(result, ment, sizeof(*ment));
261 // Not real flags, but we want to be able to check for this.
263 MOUNT_USERS = (1 << 27) * ENABLE_DESKTOP,
264 MOUNT_NOFAIL = (1 << 28) * ENABLE_DESKTOP,
265 MOUNT_NOAUTO = (1 << 29),
266 MOUNT_SWAP = (1 << 30),
267 MOUNT_FAKEFLAGS = MOUNT_USERS | MOUNT_NOFAIL | MOUNT_NOAUTO | MOUNT_SWAP
271 #define OPTION_STR "o:t:rwanfvsiO:" IF_FEATURE_MOUNT_OTHERTAB("T:")
287 #if ENABLE_FEATURE_MTAB_SUPPORT
288 #define USE_MTAB (!(option_mask32 & OPT_n))
293 #if ENABLE_FEATURE_MOUNT_FAKE
294 #define FAKE_IT (option_mask32 & OPT_f)
299 #if ENABLE_FEATURE_MOUNT_HELPERS
300 #define HELPERS_ALLOWED (!(option_mask32 & OPT_i))
302 #define HELPERS_ALLOWED 0
306 // TODO: more "user" flag compatibility.
307 // "user" option (from mount manpage):
308 // Only the user that mounted a filesystem can unmount it again.
309 // If any user should be able to unmount, then use users instead of user
310 // in the fstab line. The owner option is similar to the user option,
311 // with the restriction that the user must be the owner of the special file.
312 // This may be useful e.g. for /dev/fd if a login script makes
313 // the console user owner of this device.
315 // Standard mount options (from -o options or --options),
316 // with corresponding flags
317 static const int32_t mount_options[] = {
318 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
320 IF_FEATURE_MOUNT_LOOP(
324 IF_FEATURE_MOUNT_FSTAB(
326 /* "quiet" 0 - do not filter out, vfat wants to see it */
327 /* "noauto" */ MOUNT_NOAUTO,
328 /* "sw" */ MOUNT_SWAP,
329 /* "swap" */ MOUNT_SWAP,
330 IF_DESKTOP(/* "user" */ MOUNT_USERS,)
331 IF_DESKTOP(/* "users" */ MOUNT_USERS,)
332 IF_DESKTOP(/* "nofail" */ MOUNT_NOFAIL,)
334 IF_DESKTOP(/* "comment=" */ 0,) /* systemd uses this in fstab */
337 IF_FEATURE_MOUNT_FLAGS(
339 /* "nosuid" */ MS_NOSUID,
340 /* "suid" */ ~MS_NOSUID,
341 /* "dev" */ ~MS_NODEV,
342 /* "nodev" */ MS_NODEV,
343 /* "exec" */ ~MS_NOEXEC,
344 /* "noexec" */ MS_NOEXEC,
345 /* "sync" */ MS_SYNCHRONOUS,
346 /* "dirsync" */ MS_DIRSYNC,
347 /* "async" */ ~MS_SYNCHRONOUS,
348 /* "atime" */ ~MS_NOATIME,
349 /* "noatime" */ MS_NOATIME,
350 /* "diratime" */ ~MS_NODIRATIME,
351 /* "nodiratime" */ MS_NODIRATIME,
352 /* "mand" */ MS_MANDLOCK,
353 /* "nomand" */ ~MS_MANDLOCK,
354 /* "relatime" */ MS_RELATIME,
355 /* "norelatime" */ ~MS_RELATIME,
356 /* "strictatime" */ MS_STRICTATIME,
357 /* "loud" */ ~MS_SILENT,
358 /* "rbind" */ MS_BIND|MS_RECURSIVE,
361 /* "union" */ MS_UNION,
362 /* "bind" */ MS_BIND,
363 /* "move" */ MS_MOVE,
364 /* "shared" */ MS_SHARED,
365 /* "slave" */ MS_SLAVE,
366 /* "private" */ MS_PRIVATE,
367 /* "unbindable" */ MS_UNBINDABLE,
368 /* "rshared" */ MS_SHARED|MS_RECURSIVE,
369 /* "rslave" */ MS_SLAVE|MS_RECURSIVE,
370 /* "rprivate" */ MS_PRIVATE|MS_RECURSIVE,
371 /* "runbindable" */ MS_UNBINDABLE|MS_RECURSIVE,
374 // Always understood.
375 /* "ro" */ MS_RDONLY, // vfs flag
376 /* "rw" */ ~MS_RDONLY, // vfs flag
377 /* "remount" */ MS_REMOUNT // action flag
380 static const char mount_option_str[] =
381 IF_FEATURE_MOUNT_LOOP(
384 IF_FEATURE_MOUNT_FSTAB(
386 // "quiet\0" - do not filter out, vfat wants to see it
391 IF_DESKTOP("users\0")
392 IF_DESKTOP("nofail\0")
394 IF_DESKTOP("comment=\0") /* systemd uses this in fstab */
396 IF_FEATURE_MOUNT_FLAGS(
433 // Always understood.
436 "remount\0" // action flag
441 #if ENABLE_FEATURE_MOUNT_NFS
442 smalluint nfs_mount_version;
444 #if ENABLE_FEATURE_MOUNT_VERBOSE
448 char getmntent_buf[1];
450 enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_buf) };
451 #define G (*(struct globals*)bb_common_bufsiz1)
452 #define nfs_mount_version (G.nfs_mount_version)
453 #if ENABLE_FEATURE_MOUNT_VERBOSE
454 #define verbose (G.verbose )
458 #define fslist (G.fslist )
459 #define getmntent_buf (G.getmntent_buf )
460 #define INIT_G() do { } while (0)
462 #if ENABLE_FEATURE_MTAB_SUPPORT
464 * update_mtab_entry_on_move() is used to update entry in case of mount --move.
465 * we are looking for existing entries mnt_dir which is equal to mnt_fsname of
466 * input mntent and replace it by new one.
468 static void FAST_FUNC update_mtab_entry_on_move(const struct mntent *mp)
470 struct mntent *entries, *m;
474 mountTable = setmntent(bb_path_mtab_file, "r");
476 bb_perror_msg(bb_path_mtab_file);
482 while ((m = getmntent(mountTable)) != NULL) {
483 entries = xrealloc_vector(entries, 3, count);
484 entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
485 entries[count].mnt_dir = xstrdup(m->mnt_dir);
486 entries[count].mnt_type = xstrdup(m->mnt_type);
487 entries[count].mnt_opts = xstrdup(m->mnt_opts);
488 entries[count].mnt_freq = m->mnt_freq;
489 entries[count].mnt_passno = m->mnt_passno;
492 endmntent(mountTable);
494 mountTable = setmntent(bb_path_mtab_file, "w");
496 for (i = 0; i < count; i++) {
497 if (strcmp(entries[i].mnt_dir, mp->mnt_fsname) != 0)
498 addmntent(mountTable, &entries[i]);
500 addmntent(mountTable, mp);
502 endmntent(mountTable);
503 } else if (errno != EROFS)
504 bb_perror_msg(bb_path_mtab_file);
506 if (ENABLE_FEATURE_CLEAN_UP) {
507 for (i = 0; i < count; i++) {
508 free(entries[i].mnt_fsname);
509 free(entries[i].mnt_dir);
510 free(entries[i].mnt_type);
511 free(entries[i].mnt_opts);
518 #if ENABLE_FEATURE_MOUNT_VERBOSE
519 static int verbose_mount(const char *source, const char *target,
520 const char *filesystemtype,
521 unsigned long mountflags, const void *data)
526 rc = mount(source, target, filesystemtype, mountflags, data);
528 bb_perror_msg("mount('%s','%s','%s',0x%08lx,'%s'):%d",
529 source, target, filesystemtype,
530 mountflags, (char*)data, rc);
534 #define verbose_mount(...) mount(__VA_ARGS__)
537 // Append mount options to string
538 static void append_mount_options(char **oldopts, const char *newopts)
540 if (*oldopts && **oldopts) {
541 // Do not insert options which are already there
544 int len = strlen(newopts);
545 p = strchr(newopts, ',');
546 if (p) len = p - newopts;
549 if (!strncmp(p, newopts, len)
550 && (p[len] == ',' || p[len] == '\0'))
556 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
561 while (newopts[0] == ',') newopts++;
564 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
565 *oldopts = xstrdup(newopts);
569 // Use the mount_options list to parse options into flags.
570 // Also update list of unrecognized options if unrecognized != NULL
571 static unsigned long parse_mount_options(char *options, char **unrecognized)
573 unsigned long flags = MS_SILENT;
575 // Loop through options
578 char *comma = strchr(options, ',');
579 const char *option_str = mount_option_str;
581 if (comma) *comma = '\0';
583 // FIXME: use hasmntopt()
584 // Find this option in mount_options
585 for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
586 unsigned opt_len = strlen(option_str);
588 if (strncasecmp(option_str, options, opt_len) == 0
589 && (options[opt_len] == '\0'
590 /* or is it "comment=" thingy in fstab? */
591 IF_FEATURE_MOUNT_FSTAB(IF_DESKTOP( || option_str[opt_len-1] == '=' ))
594 unsigned long fl = mount_options[i];
595 if (fl & BB_MS_INVERTED_VALUE)
601 option_str += opt_len + 1;
603 // We did not recognize this option.
604 // If "unrecognized" is not NULL, append option there.
605 // Note that we should not append *empty* option -
606 // in this case we want to pass NULL, not "", to "data"
607 // parameter of mount(2) syscall.
608 // This is crucial for filesystems that don't accept
609 // any arbitrary mount options, like cgroup fs:
610 // "mount -t cgroup none /mnt"
611 if (options[0] && unrecognized) {
612 // Add it to strflags, to pass on to kernel
613 char *p = *unrecognized;
614 unsigned len = p ? strlen(p) : 0;
615 *unrecognized = p = xrealloc(p, len + strlen(options) + 2);
617 // Comma separated if it's not the first one
618 if (len) p[len++] = ',';
619 strcpy(p + len, options);
624 // Advance to next option
632 // Return a list of all block device backed filesystems
633 static llist_t *get_block_backed_filesystems(void)
635 static const char filesystems[2][sizeof("/proc/filesystems")] = {
640 llist_t *list = NULL;
644 for (i = 0; i < 2; i++) {
645 f = fopen_for_read(filesystems[i]);
648 while ((buf = xmalloc_fgetline(f)) != NULL) {
649 if (is_prefixed_with(buf, "nodev") && isspace(buf[5]))
651 fs = skip_whitespace(buf);
652 if (*fs == '#' || *fs == '*' || !*fs)
655 llist_add_to_end(&list, xstrdup(fs));
659 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
665 #if ENABLE_FEATURE_CLEAN_UP
666 static void delete_block_backed_filesystems(void)
668 llist_free(fslist, free);
671 void delete_block_backed_filesystems(void);
674 // Perform actual mount of specific filesystem at specific location.
675 // NB: mp->xxx fields may be trashed on exit
676 static int mount_it_now(struct mntent *mp, unsigned long vfsflags, char *filteropts)
680 vfsflags &= ~(unsigned long)MOUNT_FAKEFLAGS;
684 bb_error_msg("would do mount('%s','%s','%s',0x%08lx,'%s')",
685 mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
686 vfsflags, filteropts);
690 // Mount, with fallback to read-only if necessary.
693 rc = verbose_mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
694 vfsflags, filteropts);
696 // If mount failed, try
697 // helper program mount.<mnt_type>
698 if (HELPERS_ALLOWED && rc && mp->mnt_type) {
700 int errno_save = errno;
701 args[0] = xasprintf("mount.%s", mp->mnt_type);
704 args[rc++] = (char *)"-f";
705 if (ENABLE_FEATURE_MTAB_SUPPORT && !USE_MTAB)
706 args[rc++] = (char *)"-n";
707 args[rc++] = mp->mnt_fsname;
708 args[rc++] = mp->mnt_dir;
710 args[rc++] = (char *)"-o";
711 args[rc++] = filteropts;
714 rc = spawn_and_wait(args);
721 if (!rc || (vfsflags & MS_RDONLY) || (errno != EACCES && errno != EROFS))
723 if (!(vfsflags & MS_SILENT))
724 bb_error_msg("%s is write-protected, mounting read-only",
726 vfsflags |= MS_RDONLY;
729 // Abort entirely if permission denied.
731 if (rc && errno == EPERM)
732 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
734 // If the mount was successful, and we're maintaining an old-style
735 // mtab file by hand, add the new entry to it now.
737 if (USE_MTAB && !rc && !(vfsflags & MS_REMOUNT)) {
739 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
740 const char *option_str = mount_option_str;
744 bb_perror_msg(bb_path_mtab_file);
748 // Add vfs string flags
749 for (i = 0; mount_options[i] != MS_REMOUNT; i++) {
750 if (mount_options[i] > 0 && (mount_options[i] & vfsflags))
751 append_mount_options(&(mp->mnt_opts), option_str);
752 option_str += strlen(option_str) + 1;
755 // Remove trailing / (if any) from directory we mounted on
756 i = strlen(mp->mnt_dir) - 1;
757 while (i > 0 && mp->mnt_dir[i] == '/')
758 mp->mnt_dir[i--] = '\0';
760 // Convert to canonical pathnames as needed
761 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
763 if (!mp->mnt_type || !*mp->mnt_type) { // bind mount
764 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
765 mp->mnt_type = (char*)"bind";
767 mp->mnt_freq = mp->mnt_passno = 0;
770 #if ENABLE_FEATURE_MTAB_SUPPORT
771 if (vfsflags & MS_MOVE)
772 update_mtab_entry_on_move(mp);
775 addmntent(mountTable, mp);
776 endmntent(mountTable);
778 if (ENABLE_FEATURE_CLEAN_UP) {
787 #if ENABLE_FEATURE_MOUNT_NFS
791 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
793 * Licensed under GPLv2, see file LICENSE in this source tree.
795 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
796 * numbers to be specified on the command line.
798 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
799 * Omit the call to connect() for Linux version 1.3.11 or later.
801 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
802 * Implemented the "bg", "fg" and "retry" mount options for NFS.
804 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
805 * - added Native Language Support
807 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
811 #define MOUNTPORT 635
812 #define MNTPATHLEN 1024
813 #define MNTNAMLEN 255
817 typedef char fhandle[FHSIZE];
820 unsigned int fhandle3_len;
832 MNT3ERR_NAMETOOLONG = 63,
833 MNT3ERR_NOTSUPP = 10004,
834 MNT3ERR_SERVERFAULT = 10006,
836 typedef enum mountstat3 mountstat3;
839 unsigned int fhs_status;
844 typedef struct fhstatus fhstatus;
846 struct mountres3_ok {
849 unsigned int auth_flavours_len;
850 char *auth_flavours_val;
853 typedef struct mountres3_ok mountres3_ok;
856 mountstat3 fhs_status;
858 mountres3_ok mountinfo;
861 typedef struct mountres3 mountres3;
863 typedef char *dirpath;
867 typedef struct mountbody *mountlist;
871 dirpath ml_directory;
874 typedef struct mountbody mountbody;
876 typedef struct groupnode *groups;
882 typedef struct groupnode groupnode;
884 typedef struct exportnode *exports;
891 typedef struct exportnode exportnode;
904 typedef struct ppathcnf ppathcnf;
906 #define MOUNTPROG 100005
909 #define MOUNTPROC_NULL 0
910 #define MOUNTPROC_MNT 1
911 #define MOUNTPROC_DUMP 2
912 #define MOUNTPROC_UMNT 3
913 #define MOUNTPROC_UMNTALL 4
914 #define MOUNTPROC_EXPORT 5
915 #define MOUNTPROC_EXPORTALL 6
917 #define MOUNTVERS_POSIX 2
919 #define MOUNTPROC_PATHCONF 7
923 #define MOUNTPROC3_NULL 0
924 #define MOUNTPROC3_MNT 1
925 #define MOUNTPROC3_DUMP 2
926 #define MOUNTPROC3_UMNT 3
927 #define MOUNTPROC3_UMNTALL 4
928 #define MOUNTPROC3_EXPORT 5
940 * We want to be able to compile mount on old kernels in such a way
941 * that the binary will work well on more recent kernels.
942 * Thus, if necessary we teach nfsmount.c the structure of new fields
943 * that will come later.
945 * Moreover, the new kernel includes conflict with glibc includes
946 * so it is easiest to ignore the kernel altogether (at compile time).
954 unsigned char data[64];
957 struct nfs_mount_data {
960 struct nfs2_fh old_root; /* 1 */
966 int acregmin; /* 1 */
967 int acregmax; /* 1 */
968 int acdirmin; /* 1 */
969 int acdirmax; /* 1 */
970 struct sockaddr_in addr; /* 1 */
971 char hostname[256]; /* 1 */
973 unsigned int bsize; /* 3 */
974 struct nfs3_fh root; /* 4 */
977 /* bits in the flags field */
979 NFS_MOUNT_SOFT = 0x0001, /* 1 */
980 NFS_MOUNT_INTR = 0x0002, /* 1 */
981 NFS_MOUNT_SECURE = 0x0004, /* 1 */
982 NFS_MOUNT_POSIX = 0x0008, /* 1 */
983 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
984 NFS_MOUNT_NOAC = 0x0020, /* 1 */
985 NFS_MOUNT_TCP = 0x0040, /* 2 */
986 NFS_MOUNT_VER3 = 0x0080, /* 3 */
987 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
988 NFS_MOUNT_NONLM = 0x0200, /* 3 */
989 NFS_MOUNT_NOACL = 0x0800, /* 4 */
990 NFS_MOUNT_NORDIRPLUS = 0x4000
995 * We need to translate between nfs status return values and
996 * the local errno values which may not be the same.
998 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
999 * "after #include <errno.h> the symbol errno is reserved for any use,
1000 * it cannot even be used as a struct tag or field name".
1003 # define EDQUOT ENOSPC
1005 /* Convert each NFSERR_BLAH into EBLAH */
1006 static const uint8_t nfs_err_stat[] = {
1008 19, 20, 21, 22, 27, 28,
1009 30, 63, 66, 69, 70, 71
1012 EPERM | ENOENT | EIO | ENXIO | EACCES| EEXIST | \
1013 ENODEV| ENOTDIR | EISDIR | EINVAL| EFBIG | ENOSPC | \
1014 EROFS | ENAMETOOLONG| ENOTEMPTY| EDQUOT| ESTALE| EREMOTE) < 256
1015 typedef uint8_t nfs_err_type;
1017 typedef uint16_t nfs_err_type;
1019 static const nfs_err_type nfs_err_errnum[] = {
1020 EPERM , ENOENT , EIO , ENXIO , EACCES, EEXIST,
1021 ENODEV, ENOTDIR , EISDIR , EINVAL, EFBIG , ENOSPC,
1022 EROFS , ENAMETOOLONG, ENOTEMPTY, EDQUOT, ESTALE, EREMOTE
1024 static char *nfs_strerror(int status)
1028 for (i = 0; i < ARRAY_SIZE(nfs_err_stat); i++) {
1029 if (nfs_err_stat[i] == status)
1030 return strerror(nfs_err_errnum[i]);
1032 return xasprintf("unknown nfs status return value: %d", status);
1035 static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
1037 return xdr_opaque(xdrs, objp, FHSIZE);
1040 static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
1042 if (!xdr_u_int(xdrs, &objp->fhs_status))
1044 if (objp->fhs_status == 0)
1045 return xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle);
1049 static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
1051 return xdr_string(xdrs, objp, MNTPATHLEN);
1054 static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
1056 return xdr_bytes(xdrs, (char **)&objp->fhandle3_val,
1057 (unsigned int *) &objp->fhandle3_len,
1061 static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
1063 if (!xdr_fhandle3(xdrs, &objp->fhandle))
1065 return xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val),
1066 &(objp->auth_flavours.auth_flavours_len),
1069 (xdrproc_t) xdr_int);
1072 static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
1074 return xdr_enum(xdrs, (enum_t *) objp);
1077 static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
1079 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
1081 if (objp->fhs_status == MNT_OK)
1082 return xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo);
1086 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
1089 * Unfortunately, the kernel prints annoying console messages
1090 * in case of an unexpected nfs mount version (instead of
1091 * just returning some error). Therefore we'll have to try
1092 * and figure out what version the kernel expects.
1095 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
1096 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
1097 * nfs_mount_version: version this source and running kernel can handle
1100 find_kernel_nfs_mount_version(void)
1104 if (nfs_mount_version)
1107 nfs_mount_version = 4; /* default */
1109 kernel_version = get_linux_version_code();
1110 if (kernel_version) {
1111 if (kernel_version < KERNEL_VERSION(2,2,18))
1112 nfs_mount_version = 3;
1113 /* else v4 since 2.3.99pre4 */
1118 get_mountport(struct pmap *pm_mnt,
1119 struct sockaddr_in *server_addr,
1121 long unsigned version,
1122 long unsigned proto,
1125 struct pmaplist *pmap;
1127 server_addr->sin_port = PMAPPORT;
1128 /* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
1129 * I understand it like "IPv6 for this is not 100% ready" */
1130 pmap = pmap_getmaps(server_addr);
1132 if (version > MAX_NFSPROT)
1133 version = MAX_NFSPROT;
1136 pm_mnt->pm_prog = prog;
1137 pm_mnt->pm_vers = version;
1138 pm_mnt->pm_prot = proto;
1139 pm_mnt->pm_port = port;
1142 if (pmap->pml_map.pm_prog != prog)
1144 if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers)
1146 if (version > 2 && pmap->pml_map.pm_vers != version)
1148 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
1150 if (pmap->pml_map.pm_vers > MAX_NFSPROT
1151 || (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto)
1152 || (port && pmap->pml_map.pm_port != port)
1156 memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt));
1158 pmap = pmap->pml_next;
1160 if (!pm_mnt->pm_vers)
1161 pm_mnt->pm_vers = MOUNTVERS;
1162 if (!pm_mnt->pm_port)
1163 pm_mnt->pm_port = MOUNTPORT;
1164 if (!pm_mnt->pm_prot)
1165 pm_mnt->pm_prot = IPPROTO_TCP;
1169 static int daemonize(void)
1172 if (pid < 0) /* error */
1174 if (pid > 0) /* parent */
1178 xopen(bb_dev_null, O_RDWR);
1182 openlog(applet_name, LOG_PID, LOG_DAEMON);
1183 logmode = LOGMODE_SYSLOG;
1187 static inline int daemonize(void) { return -ENOSYS; }
1191 static inline int we_saw_this_host_before(const char *hostname UNUSED_PARAM)
1196 /* RPC strerror analogs are terminally idiotic:
1197 * *mandatory* prefix and \n at end.
1198 * This hopefully helps. Usage:
1199 * error_msg_rpc(clnt_*error*(" ")) */
1200 static void error_msg_rpc(const char *msg)
1203 while (msg[0] == ' ' || msg[0] == ':') msg++;
1205 while (len && msg[len-1] == '\n') len--;
1206 bb_error_msg("%.*s", len, msg);
1209 /* NB: mp->xxx fields may be trashed on exit */
1210 static NOINLINE int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
1216 /* prior to 2.6.23, kernel took NFS options in a form of this struct
1217 * only. 2.6.23+ looks at data->version, and if it's not 1..6,
1218 * then data pointer is interpreted as a string. */
1219 struct nfs_mount_data data;
1222 struct sockaddr_in server_addr;
1223 struct sockaddr_in mount_server_addr;
1226 struct fhstatus nfsv2;
1227 struct mountres3 nfsv3;
1245 /* these all are one-bit really. gcc 4.3.1 likes this combination: */
1256 find_kernel_nfs_mount_version();
1264 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
1266 filteropts = xstrdup(filteropts); /* going to trash it later... */
1268 hostname = xstrdup(mp->mnt_fsname);
1269 /* mount_main() guarantees that ':' is there */
1270 s = strchr(hostname, ':');
1273 /* Ignore all but first hostname in replicated mounts
1274 * until they can be fully supported. (mack@sgi.com) */
1275 s = strchr(hostname, ',');
1278 bb_error_msg("warning: multiple hostnames not supported");
1281 server_addr.sin_family = AF_INET;
1282 if (!inet_aton(hostname, &server_addr.sin_addr)) {
1283 hp = gethostbyname(hostname);
1285 bb_herror_msg("%s", hostname);
1288 if (hp->h_length != (int)sizeof(struct in_addr)) {
1289 bb_error_msg_and_die("only IPv4 is supported");
1291 memcpy(&server_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
1294 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
1296 /* add IP address to mtab options for use when unmounting */
1298 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
1299 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
1301 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
1302 mp->mnt_opts[0] ? "," : "",
1303 inet_ntoa(server_addr.sin_addr));
1308 /* Set default options.
1309 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
1310 * let the kernel decide.
1311 * timeo is filled in after we know whether it'll be TCP or UDP. */
1312 memset(&data, 0, sizeof(data));
1318 data.namlen = NAME_MAX;
1328 retry = 10000; /* 10000 minutes ~ 1 week */
1329 tcp = 1; /* nfs-utils uses tcp per default */
1331 mountprog = MOUNTPROG;
1339 if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
1340 char *opteq = strchr(opt, '=');
1343 static const char options[] ALIGN1 =
1348 /* 4 */ "acregmin\0"
1349 /* 5 */ "acregmax\0"
1350 /* 6 */ "acdirmin\0"
1351 /* 7 */ "acdirmax\0"
1355 /* 11 */ "mountport\0"
1356 /* 12 */ "mounthost\0"
1357 /* 13 */ "mountprog\0"
1358 /* 14 */ "mountvers\0"
1359 /* 15 */ "nfsprog\0"
1360 /* 16 */ "nfsvers\0"
1367 idx = index_in_strings(options, opt);
1369 case 12: // "mounthost"
1370 mounthost = xstrndup(opteq,
1371 strcspn(opteq, " \t\n\r,"));
1374 if (is_prefixed_with(opteq, "tcp"))
1376 else if (is_prefixed_with(opteq, "udp"))
1379 bb_error_msg("warning: unrecognized proto= option");
1381 case 20: // "addr" - ignore
1384 if (vfsflags & MS_REMOUNT)
1388 val = xatoi_positive(opteq);
1399 case 3: // "retrans"
1402 case 4: // "acregmin"
1403 data.acregmin = val;
1405 case 5: // "acregmax"
1406 data.acregmax = val;
1408 case 6: // "acdirmin"
1409 data.acdirmin = val;
1411 case 7: // "acdirmax"
1412 data.acdirmax = val;
1414 case 8: // "actimeo"
1415 data.acregmin = val;
1416 data.acregmax = val;
1417 data.acdirmin = val;
1418 data.acdirmax = val;
1426 case 11: // "mountport"
1429 case 13: // "mountprog"
1432 case 14: // "mountvers"
1435 case 15: // "nfsprog"
1438 case 16: // "nfsvers"
1442 case 19: // "namlen"
1443 //if (nfs_mount_version >= 2)
1446 // bb_error_msg("warning: option namlen is not supported\n");
1449 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
1453 else { /* not of the form opt=val */
1454 static const char options[] ALIGN1 =
1469 if (is_prefixed_with(opt, "no")) {
1473 switch (index_in_strings(options, opt)) {
1509 if (nfs_mount_version >= 3)
1512 bb_error_msg("warning: option nolock is not supported");
1521 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1526 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1528 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1529 | (intr ? NFS_MOUNT_INTR : 0)
1530 | (posix ? NFS_MOUNT_POSIX : 0)
1531 | (nocto ? NFS_MOUNT_NOCTO : 0)
1532 | (noac ? NFS_MOUNT_NOAC : 0)
1533 | (nordirplus ? NFS_MOUNT_NORDIRPLUS : 0)
1534 | (noacl ? NFS_MOUNT_NOACL : 0);
1535 if (nfs_mount_version >= 2)
1536 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1537 if (nfs_mount_version >= 3)
1538 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1539 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1540 bb_error_msg("NFSv%d not supported", nfsvers);
1543 if (nfsvers && !mountvers)
1544 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1545 if (nfsvers && nfsvers < mountvers) {
1546 mountvers = nfsvers;
1549 /* Adjust options if none specified */
1551 data.timeo = tcp ? 70 : 7;
1553 data.version = nfs_mount_version;
1555 if (vfsflags & MS_REMOUNT)
1559 * If the previous mount operation on the same host was
1560 * backgrounded, and the "bg" for this mount is also set,
1561 * give up immediately, to avoid the initial timeout.
1563 if (bg && we_saw_this_host_before(hostname)) {
1564 daemonized = daemonize();
1565 if (daemonized <= 0) { /* parent or error */
1566 retval = -daemonized;
1571 /* Create mount daemon client */
1572 /* See if the nfs host = mount host. */
1574 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1575 mount_server_addr.sin_family = AF_INET;
1576 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1578 hp = gethostbyname(mounthost);
1580 bb_herror_msg("%s", mounthost);
1583 if (hp->h_length != (int)sizeof(struct in_addr)) {
1584 bb_error_msg_and_die("only IPv4 is supported");
1586 mount_server_addr.sin_family = AF_INET;
1587 memcpy(&mount_server_addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
1592 * The following loop implements the mount retries. When the mount
1593 * times out, and the "bg" option is set, we background ourself
1594 * and continue trying.
1596 * The case where the mount point is not present and the "bg"
1597 * option is set, is treated as a timeout. This is done to
1598 * support nested mounts.
1600 * The "retry" count specified by the user is the number of
1601 * minutes to retry before giving up.
1604 struct timeval total_timeout;
1605 struct timeval retry_timeout;
1611 retry_timeout.tv_sec = 3;
1612 retry_timeout.tv_usec = 0;
1613 total_timeout.tv_sec = 20;
1614 total_timeout.tv_usec = 0;
1615 /* FIXME: use monotonic()? */
1616 timeout = time(NULL) + 60 * retry;
1620 /* Be careful not to use too many CPU cycles */
1624 get_mountport(&pm_mnt, &mount_server_addr,
1629 nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers;
1631 /* contact the mount daemon via TCP */
1632 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1633 msock = RPC_ANYSOCK;
1635 switch (pm_mnt.pm_prot) {
1637 mclient = clntudp_create(&mount_server_addr,
1644 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1645 msock = RPC_ANYSOCK;
1647 mclient = clnttcp_create(&mount_server_addr,
1656 if (!daemonized && prevt == 0)
1657 error_msg_rpc(clnt_spcreateerror(" "));
1659 enum clnt_stat clnt_stat;
1661 /* Try to mount hostname:pathname */
1662 mclient->cl_auth = authunix_create_default();
1664 /* Make pointers in xdr_mountres3 NULL so
1665 * that xdr_array allocates memory for us
1667 memset(&status, 0, sizeof(status));
1669 if (pm_mnt.pm_vers == 3)
1670 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1671 (xdrproc_t) xdr_dirpath,
1672 (caddr_t) &pathname,
1673 (xdrproc_t) xdr_mountres3,
1677 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1678 (xdrproc_t) xdr_dirpath,
1679 (caddr_t) &pathname,
1680 (xdrproc_t) xdr_fhstatus,
1684 if (clnt_stat == RPC_SUCCESS)
1685 goto prepare_kernel_data; /* we're done */
1686 if (errno != ECONNREFUSED) {
1687 error_msg_rpc(clnt_sperror(mclient, " "));
1688 goto fail; /* don't retry */
1690 /* Connection refused */
1691 if (!daemonized && prevt == 0) /* print just once */
1692 error_msg_rpc(clnt_sperror(mclient, " "));
1693 auth_destroy(mclient->cl_auth);
1694 clnt_destroy(mclient);
1700 /* Timeout. We are going to retry... maybe */
1704 daemonized = daemonize();
1705 if (daemonized <= 0) { /* parent or error */
1706 retval = -daemonized;
1713 /* TODO error message */
1719 prepare_kernel_data:
1722 if (status.nfsv2.fhs_status != 0) {
1723 bb_error_msg("%s:%s failed, reason given by server: %s",
1725 nfs_strerror(status.nfsv2.fhs_status));
1728 memcpy(data.root.data,
1729 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1731 data.root.size = NFS_FHSIZE;
1732 memcpy(data.old_root.data,
1733 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1736 fhandle3 *my_fhandle;
1737 if (status.nfsv3.fhs_status != 0) {
1738 bb_error_msg("%s:%s failed, reason given by server: %s",
1740 nfs_strerror(status.nfsv3.fhs_status));
1743 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1744 memset(data.old_root.data, 0, NFS_FHSIZE);
1745 memset(&data.root, 0, sizeof(data.root));
1746 data.root.size = my_fhandle->fhandle3_len;
1747 memcpy(data.root.data,
1748 (char *) my_fhandle->fhandle3_val,
1749 my_fhandle->fhandle3_len);
1751 data.flags |= NFS_MOUNT_VER3;
1754 /* Create nfs socket for kernel */
1756 if (nfs_mount_version < 3) {
1757 bb_error_msg("NFS over TCP is not supported");
1760 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1762 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1764 bb_perror_msg("nfs socket");
1767 if (bindresvport(fsock, 0) < 0) {
1768 bb_perror_msg("nfs bindresvport");
1772 server_addr.sin_port = PMAPPORT;
1773 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1774 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1778 server_addr.sin_port = htons(port);
1780 /* Prepare data structure for kernel */
1782 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1783 strncpy(data.hostname, hostname, sizeof(data.hostname));
1786 auth_destroy(mclient->cl_auth);
1787 clnt_destroy(mclient);
1792 /* We must wait until mount directory is available */
1793 struct stat statbuf;
1795 while (stat(mp->mnt_dir, &statbuf) == -1) {
1797 daemonized = daemonize();
1798 if (daemonized <= 0) { /* parent or error */
1799 /* FIXME: parent doesn't close fsock - ??! */
1800 retval = -daemonized;
1804 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1811 /* Perform actual mount */
1813 retval = mount_it_now(mp, vfsflags, (char*)&data);
1820 auth_destroy(mclient->cl_auth);
1821 clnt_destroy(mclient);
1835 #else // !ENABLE_FEATURE_MOUNT_NFS
1837 /* Linux 2.6.23+ supports nfs mounts with options passed as a string.
1838 * For older kernels, you must build busybox with ENABLE_FEATURE_MOUNT_NFS.
1839 * (However, note that then you lose any chances that NFS over IPv6 would work).
1841 static int nfsmount(struct mntent *mp, unsigned long vfsflags, char *filteropts)
1843 len_and_sockaddr *lsa;
1849 # if ENABLE_FEATURE_IPV6
1850 end = strchr(mp->mnt_fsname, ']');
1851 if (end && end[1] == ':')
1855 /* mount_main() guarantees that ':' is there */
1856 end = strchr(mp->mnt_fsname, ':');
1859 lsa = xhost2sockaddr(mp->mnt_fsname, /*port:*/ 0);
1861 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
1862 if (ENABLE_FEATURE_CLEAN_UP) free(lsa);
1863 opts = xasprintf("%s%saddr=%s",
1864 filteropts ? filteropts : "",
1865 filteropts ? "," : "",
1868 if (ENABLE_FEATURE_CLEAN_UP) free(dotted);
1869 ret = mount_it_now(mp, vfsflags, opts);
1870 if (ENABLE_FEATURE_CLEAN_UP) free(opts);
1875 #endif // !ENABLE_FEATURE_MOUNT_NFS
1877 // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1878 // type detection. Returns 0 for success, nonzero for failure.
1879 // NB: mp->xxx fields may be trashed on exit
1880 static int singlemount(struct mntent *mp, int ignore_busy)
1883 unsigned long vfsflags;
1884 char *loopFile = NULL, *filteropts = NULL;
1890 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1892 // Treat fstype "auto" as unspecified
1893 if (mp->mnt_type && strcmp(mp->mnt_type, "auto") == 0)
1894 mp->mnt_type = NULL;
1896 // Might this be a virtual filesystem?
1897 if (ENABLE_FEATURE_MOUNT_HELPERS && strchr(mp->mnt_fsname, '#')) {
1901 // fsname: "cmd#arg1#arg2..."
1902 // WARNING: allows execution of arbitrary commands!
1903 // Try "mount 'sh#-c#sh' bogus_dir".
1904 // It is safe ONLY because non-root
1905 // cannot use two-argument mount command
1906 // and using one-argument "mount 'sh#-c#sh'" doesn't work:
1907 // "mount: can't find sh#-c#sh in /etc/fstab"
1908 // (if /etc/fstab has it, it's ok: root sets up /etc/fstab).
1913 while (*s && n < 35 - 2) {
1914 if (*s++ == '#' && *s != '#') {
1919 args[n++] = mp->mnt_dir;
1921 rc = spawn_and_wait(args);
1925 // Might this be an CIFS filesystem?
1926 if (ENABLE_FEATURE_MOUNT_CIFS
1927 && (!mp->mnt_type || strcmp(mp->mnt_type, "cifs") == 0)
1928 && (mp->mnt_fsname[0] == '/' || mp->mnt_fsname[0] == '\\')
1929 && mp->mnt_fsname[0] == mp->mnt_fsname[1]
1933 char *hostname, *share;
1935 len_and_sockaddr *lsa;
1937 // Parse mp->mnt_fsname of the form "//hostname/share[/dir1/dir2]"
1939 hostname = mp->mnt_fsname + 2;
1940 len = strcspn(hostname, "/\\");
1941 share = hostname + len + 1;
1942 if (len == 0 // 3rd char is a [back]slash (IOW: empty hostname)
1943 || share[-1] == '\0' // no [back]slash after hostname
1944 || share[0] == '\0' // empty share name
1950 len = strcspn(share, "/\\");
1952 // "unc=\\hostname\share" option is mandatory
1953 // after CIFS option parsing was rewritten in Linux 3.4.
1954 // Must use backslashes.
1955 // If /dir1/dir2 is present, also add "prefixpath=dir1/dir2"
1957 char *unc = xasprintf(
1958 share[len] != '\0' /* "/dir1/dir2" exists? */
1959 ? "unc=\\\\%s\\%.*s,prefixpath=%s"
1960 : "unc=\\\\%s\\%.*s",
1963 share + len + 1 /* "dir1/dir2" */
1965 parse_mount_options(unc, &filteropts);
1966 if (ENABLE_FEATURE_CLEAN_UP) free(unc);
1969 lsa = host2sockaddr(hostname, 0);
1974 // Insert "ip=..." option into options
1975 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
1976 if (ENABLE_FEATURE_CLEAN_UP) free(lsa);
1977 ip = xasprintf("ip=%s", dotted);
1978 if (ENABLE_FEATURE_CLEAN_UP) free(dotted);
1979 parse_mount_options(ip, &filteropts);
1980 if (ENABLE_FEATURE_CLEAN_UP) free(ip);
1982 mp->mnt_type = (char*)"cifs";
1983 rc = mount_it_now(mp, vfsflags, filteropts);
1988 // Might this be an NFS filesystem?
1989 if ((!mp->mnt_type || is_prefixed_with(mp->mnt_type, "nfs"))
1990 && strchr(mp->mnt_fsname, ':') != NULL
1993 mp->mnt_type = (char*)"nfs";
1994 rc = nfsmount(mp, vfsflags, filteropts);
1998 // Look at the file. (Not found isn't a failure for remount, or for
1999 // a synthetic filesystem like proc or sysfs.)
2000 // (We use stat, not lstat, in order to allow
2001 // mount symlink_to_file_or_blkdev dir)
2002 if (!stat(mp->mnt_fsname, &st)
2003 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
2005 // Do we need to allocate a loopback device for it?
2006 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
2007 loopFile = bb_simplify_path(mp->mnt_fsname);
2008 mp->mnt_fsname = NULL; // will receive malloced loop dev name
2009 if (set_loop(&mp->mnt_fsname, loopFile, 0, /*ro:*/ (vfsflags & MS_RDONLY)) < 0) {
2010 if (errno == EPERM || errno == EACCES)
2011 bb_error_msg(bb_msg_perm_denied_are_you_root);
2013 bb_perror_msg("can't setup loop device");
2017 // Autodetect bind mounts
2018 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
2019 vfsflags |= MS_BIND;
2022 // If we know the fstype (or don't need to), jump straight
2023 // to the actual mount.
2024 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {
2027 next = mp->mnt_type ? strchr(mp->mnt_type, ',') : NULL;
2030 rc = mount_it_now(mp, vfsflags, filteropts);
2031 if (rc == 0 || !next)
2033 mp->mnt_type = next + 1;
2036 // Loop through filesystem types until mount succeeds
2039 // Initialize list of block backed filesystems.
2040 // This has to be done here so that during "mount -a",
2041 // mounts after /proc shows up can autodetect.
2043 fslist = get_block_backed_filesystems();
2044 if (ENABLE_FEATURE_CLEAN_UP && fslist)
2045 atexit(delete_block_backed_filesystems);
2048 for (fl = fslist; fl; fl = fl->link) {
2049 mp->mnt_type = fl->data;
2050 rc = mount_it_now(mp, vfsflags, filteropts);
2056 // If mount failed, clean up loop file (if any).
2057 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
2058 del_loop(mp->mnt_fsname);
2059 if (ENABLE_FEATURE_CLEAN_UP) {
2061 free(mp->mnt_fsname);
2066 if (ENABLE_FEATURE_CLEAN_UP)
2069 if (errno == EBUSY && ignore_busy)
2071 if (errno == ENOENT && (vfsflags & MOUNT_NOFAIL))
2074 bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
2079 // -O interprets a list of filter options which select whether a mount
2080 // point will be mounted: only mounts with options matching *all* filtering
2081 // options will be selected.
2082 // By default each -O filter option must be present in the list of mount
2083 // options, but if it is prefixed by "no" then it must be absent.
2085 // -O a,nob,c matches -o a,c but fails to match -o a,b,c
2086 // (and also fails to match -o a because -o c is absent).
2088 // It is different from -t in that each option is matched exactly; a leading
2089 // "no" at the beginning of one option does not negate the rest.
2090 static int match_opt(const char *fs_opt_in, const char *O_opt)
2096 const char *fs_opt = fs_opt_in;
2100 // If option begins with "no" then treat as an inverted match:
2101 // matching is a failure
2103 if (O_opt[0] == 'n' && O_opt[1] == 'o') {
2107 // Isolate the current O option
2108 O_len = strchrnul(O_opt, ',') - O_opt;
2109 // Check for a match against existing options
2111 if (strncmp(fs_opt, O_opt, O_len) == 0
2112 && (fs_opt[O_len] == '\0' || fs_opt[O_len] == ',')
2115 return 0; // "no" prefix, but option found
2116 match = 1; // current O option found, go check next one
2119 fs_opt = strchr(fs_opt, ',');
2125 return 0; // match wanted but not found
2126 if (O_opt[O_len] == '\0') // end?
2128 // Step to the next O option
2131 // If we get here then everything matched
2135 // Parse options, if necessary parse fstab/mtab, and call singlemount for
2136 // each directory to be mounted.
2137 int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2138 int mount_main(int argc UNUSED_PARAM, char **argv)
2140 char *cmdopts = xzalloc(1);
2141 char *fstype = NULL;
2142 char *O_optmatch = NULL;
2144 llist_t *lst_o = NULL;
2145 const char *fstabname = "/etc/fstab";
2148 int rc = EXIT_SUCCESS;
2149 unsigned long cmdopt_flags;
2151 struct mntent mtpair[2], *mtcur = mtpair;
2152 IF_NOT_DESKTOP(const int nonroot = 0;)
2154 IF_DESKTOP(int nonroot = ) sanitize_env_if_suid();
2158 // Parse long options, like --bind and --move. Note that -o option
2159 // and --option are synonymous. Yes, this means --remount,rw works.
2160 for (i = j = 1; argv[i]; i++) {
2161 if (argv[i][0] == '-' && argv[i][1] == '-')
2162 append_mount_options(&cmdopts, argv[i] + 2);
2164 argv[j++] = argv[i];
2168 // Parse remaining options
2169 // Max 2 params; -o is a list, -v is a counter
2170 opt_complementary = "?2o::" IF_FEATURE_MOUNT_VERBOSE("vv");
2171 opt = getopt32(argv, OPTION_STR, &lst_o, &fstype, &O_optmatch
2172 IF_FEATURE_MOUNT_OTHERTAB(, &fstabname)
2173 IF_FEATURE_MOUNT_VERBOSE(, &verbose));
2174 while (lst_o) append_mount_options(&cmdopts, llist_pop(&lst_o)); // -o
2175 if (opt & OPT_r) append_mount_options(&cmdopts, "ro"); // -r
2176 if (opt & OPT_w) append_mount_options(&cmdopts, "rw"); // -w
2179 // If we have no arguments, show currently mounted filesystems
2181 if (!(opt & OPT_a)) {
2182 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
2185 bb_error_msg_and_die("no %s", bb_path_mtab_file);
2187 while (getmntent_r(mountTable, &mtpair[0], getmntent_buf,
2190 // Don't show rootfs. FIXME: why??
2191 // util-linux 2.12a happily shows rootfs...
2192 //if (strcmp(mtpair->mnt_fsname, "rootfs") == 0) continue;
2194 if (!fstype || strcmp(mtpair->mnt_type, fstype) == 0)
2195 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
2196 mtpair->mnt_dir, mtpair->mnt_type,
2199 if (ENABLE_FEATURE_CLEAN_UP)
2200 endmntent(mountTable);
2201 return EXIT_SUCCESS;
2203 storage_path = NULL;
2205 // When we have two arguments, the second is the directory and we can
2206 // skip looking at fstab entirely. We can always abspath() the directory
2207 // argument when we get it.
2210 bb_error_msg_and_die(bb_msg_you_must_be_root);
2211 mtpair->mnt_fsname = argv[0];
2212 mtpair->mnt_dir = argv[1];
2213 mtpair->mnt_type = fstype;
2214 mtpair->mnt_opts = cmdopts;
2215 resolve_mount_spec(&mtpair->mnt_fsname);
2216 rc = singlemount(mtpair, /*ignore_busy:*/ 0);
2219 storage_path = bb_simplify_path(argv[0]); // malloced
2222 // Past this point, we are handling either "mount -a [opts]"
2223 // or "mount [opts] single_param"
2225 cmdopt_flags = parse_mount_options(cmdopts, NULL);
2226 if (nonroot && (cmdopt_flags & ~MS_SILENT)) // Non-root users cannot specify flags
2227 bb_error_msg_and_die(bb_msg_you_must_be_root);
2229 // If we have a shared subtree flag, don't worry about fstab or mtab.
2230 if (ENABLE_FEATURE_MOUNT_FLAGS
2231 && (cmdopt_flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2233 // verbose_mount(source, target, type, flags, data)
2234 rc = verbose_mount("", argv[0], "", cmdopt_flags, "");
2236 bb_simple_perror_msg_and_die(argv[0]);
2240 // A malicious user could overmount /usr without this.
2241 if (ENABLE_FEATURE_MOUNT_OTHERTAB && nonroot)
2242 fstabname = "/etc/fstab";
2243 // Open either fstab or mtab
2244 if (cmdopt_flags & MS_REMOUNT) {
2245 // WARNING. I am not sure this matches util-linux's
2246 // behavior. It's possible util-linux does not
2247 // take -o opts from mtab (takes only mount source).
2248 fstabname = bb_path_mtab_file;
2250 fstab = setmntent(fstabname, "r");
2252 bb_perror_msg_and_die("can't read '%s'", fstabname);
2254 // Loop through entries until we find what we're looking for
2255 memset(mtpair, 0, sizeof(mtpair));
2257 struct mntent *mtother = (mtcur==mtpair ? mtpair+1 : mtpair);
2259 // Get next fstab entry
2260 if (!getmntent_r(fstab, mtcur, getmntent_buf
2261 + (mtcur==mtpair ? GETMNTENT_BUFSIZE/2 : 0),
2262 GETMNTENT_BUFSIZE/2)
2263 ) { // End of fstab/mtab is reached
2264 mtcur = mtother; // the thing we found last time
2268 // If we're trying to mount something specific and this isn't it,
2269 // skip it. Note we must match the exact text in fstab (ala
2270 // "proc") or a full path from root
2273 // Is this what we're looking for?
2274 if (strcmp(argv[0], mtcur->mnt_fsname) != 0
2275 && strcmp(storage_path, mtcur->mnt_fsname) != 0
2276 && strcmp(argv[0], mtcur->mnt_dir) != 0
2277 && strcmp(storage_path, mtcur->mnt_dir) != 0
2282 // Remember this entry. Something later may have
2283 // overmounted it, and we want the _last_ match.
2286 // If we're mounting all
2289 // No, mount -a won't mount anything,
2290 // even user mounts, for mere humans
2292 bb_error_msg_and_die(bb_msg_you_must_be_root);
2294 // Does type match? (NULL matches always)
2295 if (!match_fstype(mtcur, fstype))
2298 // Skip noauto and swap anyway
2299 if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP))
2300 // swap is bogus "fstype", parse_mount_options can't check fstypes
2301 || strcasecmp(mtcur->mnt_type, "swap") == 0
2306 // Does (at least one) option match?
2307 // (NULL matches always)
2308 if (!match_opt(mtcur->mnt_opts, O_optmatch))
2311 resolve_mount_spec(&mtcur->mnt_fsname);
2313 // NFS mounts want this to be xrealloc-able
2314 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
2316 // If nothing is mounted on this directory...
2317 // (otherwise repeated "mount -a" mounts everything again)
2318 mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0);
2319 // We do not check fsname match of found mount point -
2320 // "/" may have fsname of "/dev/root" while fstab
2321 // says "/dev/something_else".
2324 bb_error_msg("according to %s, "
2325 "%s is already mounted on %s",
2327 mp->mnt_fsname, mp->mnt_dir);
2330 // ...mount this thing
2331 if (singlemount(mtcur, /*ignore_busy:*/ 1)) {
2332 // Count number of failed mounts
2336 free(mtcur->mnt_opts);
2340 // End of fstab/mtab is reached.
2341 // Were we looking for something specific?
2342 if (argv[0]) { // yes
2345 // If we didn't find anything, complain
2346 if (!mtcur->mnt_fsname)
2347 bb_error_msg_and_die("can't find %s in %s",
2348 argv[0], fstabname);
2350 // What happens when we try to "mount swap_partition"?
2351 // (fstab containts "swap_partition swap swap defaults 0 0")
2352 // util-linux-ng 2.13.1 does this:
2353 // stat("/sbin/mount.swap", 0x7fff62a3a350) = -1 ENOENT (No such file or directory)
2354 // mount("swap_partition", "swap", "swap", MS_MGC_VAL, NULL) = -1 ENOENT (No such file or directory)
2355 // lstat("swap", 0x7fff62a3a640) = -1 ENOENT (No such file or directory)
2356 // write(2, "mount: mount point swap does not exist\n", 39) = 39
2357 // exit_group(32) = ?
2359 // In case we want to simply skip swap partitions:
2360 l = parse_mount_options(mtcur->mnt_opts, NULL);
2361 if ((l & MOUNT_SWAP)
2362 // swap is bogus "fstype", parse_mount_options can't check fstypes
2363 || strcasecmp(mtcur->mnt_type, "swap") == 0
2369 // fstab must have "users" or "user"
2370 l = parse_mount_options(mtcur->mnt_opts, NULL);
2371 if (!(l & MOUNT_USERS))
2372 bb_error_msg_and_die(bb_msg_you_must_be_root);
2375 //util-linux-2.12 does not do this check.
2376 //// If nothing is mounted on this directory...
2377 //// (otherwise repeated "mount FOO" mounts FOO again)
2378 //mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0);
2380 // bb_error_msg("according to %s, "
2381 // "%s is already mounted on %s",
2382 // bb_path_mtab_file,
2383 // mp->mnt_fsname, mp->mnt_dir);
2385 // ...mount the last thing we found
2386 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
2387 append_mount_options(&(mtcur->mnt_opts), cmdopts);
2388 resolve_mount_spec(&mtpair->mnt_fsname);
2389 rc = singlemount(mtcur, /*ignore_busy:*/ 0);
2390 if (ENABLE_FEATURE_CLEAN_UP)
2391 free(mtcur->mnt_opts);
2396 if (ENABLE_FEATURE_CLEAN_UP)
2398 if (ENABLE_FEATURE_CLEAN_UP) {
2403 //TODO: exitcode should be ORed mask of (from "man mount"):
2405 // 1 incorrect invocation or permissions
2406 // 2 system error (out of memory, cannot fork, no more loop devices)
2407 // 4 internal mount bug or missing nfs support in mount
2409 //16 problems writing or locking /etc/mtab
2411 //64 some mount succeeded