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 tarball for details.
12 /* Design notes: There is no spec for mount. Remind me to write one.
14 mount_main() calls singlemount() which calls mount_it_now().
16 mount_main() can loop through /etc/fstab for mount -a
17 singlemount() can loop through /etc/filesystems for fstype detection.
18 mount_it_now() does the actual mount.
25 /* Needed for nfs support only... */
26 #include <sys/utsname.h>
30 #include <rpc/pmap_prot.h>
31 #include <rpc/pmap_clnt.h>
34 #define MS_SILENT (1 << 15)
37 #if defined(__dietlibc__)
38 /* 16.12.2006, Sampo Kellomaki (sampo@iki.fi)
39 * dietlibc-0.30 does not have implementation of getmntent_r() */
40 static struct mntent *getmntent_r(FILE* stream, struct mntent* result, char* buffer, int bufsize)
42 struct mntent* ment = getmntent(stream);
43 memcpy(result, ment, sizeof(struct mntent));
48 #define getmntent_buf bb_common_bufsiz1
51 // Not real flags, but we want to be able to check for this.
53 MOUNT_USERS = (1<<28)*ENABLE_DESKTOP,
54 MOUNT_NOAUTO = (1<<29),
57 // TODO: more "user" flag compatibility.
58 // "user" option (from mount manpage):
59 // Only the user that mounted a filesystem can unmount it again.
60 // If any user should be able to unmount, then use users instead of user
61 // in the fstab line. The owner option is similar to the user option,
62 // with the restriction that the user must be the owner of the special file.
63 // This may be useful e.g. for /dev/fd if a login script makes
64 // the console user owner of this device.
66 /* Standard mount options (from -o options or --options), with corresponding
69 static const int32_t mount_options[] = {
70 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
72 USE_FEATURE_MOUNT_LOOP(
76 USE_FEATURE_MOUNT_FSTAB(
78 /* "quiet" 0 - do not filter out, vfat wants to see it */
79 /* "noauto" */ MOUNT_NOAUTO,
80 /* "sw" */ MOUNT_SWAP,
81 /* "swap" */ MOUNT_SWAP,
82 USE_DESKTOP(/* "user" */ MOUNT_USERS,)
83 USE_DESKTOP(/* "users" */ MOUNT_USERS,)
87 USE_FEATURE_MOUNT_FLAGS(
89 /* "nosuid" */ MS_NOSUID,
90 /* "suid" */ ~MS_NOSUID,
91 /* "dev" */ ~MS_NODEV,
92 /* "nodev" */ MS_NODEV,
93 /* "exec" */ ~MS_NOEXEC,
94 /* "noexec" */ MS_NOEXEC,
95 /* "sync" */ MS_SYNCHRONOUS,
96 /* "async" */ ~MS_SYNCHRONOUS,
97 /* "atime" */ ~MS_NOATIME,
98 /* "noatime" */ MS_NOATIME,
99 /* "diratime" */ ~MS_NODIRATIME,
100 /* "nodiratime" */ MS_NODIRATIME,
101 /* "loud" */ ~MS_SILENT,
104 /* "bind" */ MS_BIND,
105 /* "move" */ MS_MOVE,
106 /* "shared" */ MS_SHARED,
107 /* "slave" */ MS_SLAVE,
108 /* "private" */ MS_PRIVATE,
109 /* "unbindable" */ MS_UNBINDABLE,
110 /* "rshared" */ MS_SHARED|MS_RECURSIVE,
111 /* "rslave" */ MS_SLAVE|MS_RECURSIVE,
112 /* "rprivate" */ MS_SLAVE|MS_RECURSIVE,
113 /* "runbindable" */ MS_UNBINDABLE|MS_RECURSIVE,
116 // Always understood.
117 /* "ro" */ MS_RDONLY, // vfs flag
118 /* "rw" */ ~MS_RDONLY, // vfs flag
119 /* "remount" */ MS_REMOUNT // action flag
122 static const char mount_option_str[] =
123 USE_FEATURE_MOUNT_LOOP(
126 USE_FEATURE_MOUNT_FSTAB(
128 /* "quiet" "\0" - do not filter out, vfat wants to see it */
132 USE_DESKTOP("user" "\0")
133 USE_DESKTOP("users" "\0")
136 USE_FEATURE_MOUNT_FLAGS(
165 // Always understood.
166 "ro" "\0" // vfs flag
167 "rw" "\0" // vfs flag
168 "remount" "\0" // action flag
171 /* Append mount options to string */
172 static void append_mount_options(char **oldopts, const char *newopts)
174 if (*oldopts && **oldopts) {
175 /* do not insert options which are already there */
178 int len = strlen(newopts);
179 p = strchr(newopts, ',');
180 if (p) len = p - newopts;
183 if (!strncmp(p, newopts, len)
184 && (p[len] == ',' || p[len] == '\0'))
190 p = xasprintf("%s,%.*s", *oldopts, len, newopts);
195 while (newopts[0] == ',') newopts++;
198 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
199 *oldopts = xstrdup(newopts);
203 /* Use the mount_options list to parse options into flags.
204 * Also return list of unrecognized options if unrecognized!=NULL */
205 static int parse_mount_options(char *options, char **unrecognized)
207 int flags = MS_SILENT;
209 // Loop through options
212 char *comma = strchr(options, ',');
213 const char *option_str = mount_option_str;
215 if (comma) *comma = '\0';
217 // Find this option in mount_options
218 for (i = 0; i < ARRAY_SIZE(mount_options); i++) {
219 if (!strcasecmp(option_str, options)) {
220 long fl = mount_options[i];
221 if (fl < 0) flags &= fl;
225 option_str += strlen(option_str) + 1;
227 // If unrecognized not NULL, append unrecognized mount options */
228 if (unrecognized && i == ARRAY_SIZE(mount_options)) {
229 // Add it to strflags, to pass on to kernel
230 i = *unrecognized ? strlen(*unrecognized) : 0;
231 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
233 // Comma separated if it's not the first one
234 if (i) (*unrecognized)[i++] = ',';
235 strcpy((*unrecognized)+i, options);
240 // Advance to next option
248 // Return a list of all block device backed filesystems
250 static llist_t *get_block_backed_filesystems(void)
252 static const char filesystems[2][sizeof("/proc/filesystems")] = {
261 for (i = 0; i < 2; i++) {
262 f = fopen(filesystems[i], "r");
265 while ((buf = xmalloc_getline(f)) != 0) {
266 if (!strncmp(buf, "nodev", 5) && isspace(buf[5]))
268 fs = skip_whitespace(buf);
269 if (*fs=='#' || *fs=='*' || !*fs) continue;
271 llist_add_to_end(&list, xstrdup(fs));
274 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
280 static llist_t *fslist;
282 #if ENABLE_FEATURE_CLEAN_UP
283 static void delete_block_backed_filesystems(void)
285 llist_free(fslist, free);
288 void delete_block_backed_filesystems(void);
291 #if ENABLE_FEATURE_MTAB_SUPPORT
292 static int useMtab = 1;
299 // Perform actual mount of specific filesystem at specific location.
300 // NB: mp->xxx fields may be trashed on exit
301 static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
305 if (fakeIt) goto mtab;
307 // Mount, with fallback to read-only if necessary.
310 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
311 vfsflags, filteropts);
313 // If mount failed, try
314 // helper program <mnt_type>
315 if (ENABLE_FEATURE_MOUNT_HELPERS && rc) {
317 int errno_save = errno;
318 args[0] = mp->mnt_type;
321 args[rc++] = (char *)"-o";
322 args[rc++] = filteropts;
324 args[rc++] = mp->mnt_fsname;
325 args[rc++] = mp->mnt_dir;
327 rc = wait4pid(spawn(args));
333 if (!rc || (vfsflags & MS_RDONLY) || (errno != EACCES && errno != EROFS))
335 if (!(vfsflags & MS_SILENT))
336 bb_error_msg("%s is write-protected, mounting read-only",
338 vfsflags |= MS_RDONLY;
341 // Abort entirely if permission denied.
343 if (rc && errno == EPERM)
344 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
346 /* If the mount was successful, and we're maintaining an old-style
347 * mtab file by hand, add the new entry to it now. */
349 if (ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc && !(vfsflags & MS_REMOUNT)) {
351 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
352 const char *option_str = mount_option_str;
356 bb_error_msg("no %s", bb_path_mtab_file);
360 // Add vfs string flags
362 for (i = 0; mount_options[i] != MS_REMOUNT; i++) {
363 if (mount_options[i] > 0 && (mount_options[i] & vfsflags))
364 append_mount_options(&(mp->mnt_opts), option_str);
365 option_str += strlen(option_str) + 1;
368 // Remove trailing / (if any) from directory we mounted on
370 i = strlen(mp->mnt_dir) - 1;
371 if (i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = '\0';
373 // Convert to canonical pathnames as needed
375 mp->mnt_dir = bb_simplify_path(mp->mnt_dir);
377 if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
378 mp->mnt_fsname = fsname = bb_simplify_path(mp->mnt_fsname);
379 mp->mnt_type = (char*)"bind";
381 mp->mnt_freq = mp->mnt_passno = 0;
385 addmntent(mountTable, mp);
386 endmntent(mountTable);
387 if (ENABLE_FEATURE_CLEAN_UP) {
396 #if ENABLE_FEATURE_MOUNT_NFS
400 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
402 * Licensed under GPLv2, see file LICENSE in this tarball for details.
404 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
405 * numbers to be specified on the command line.
407 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
408 * Omit the call to connect() for Linux version 1.3.11 or later.
410 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
411 * Implemented the "bg", "fg" and "retry" mount options for NFS.
413 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
414 * - added Native Language Support
416 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
420 /* This is just a warning of a common mistake. Possibly this should be a
421 * uclibc faq entry rather than in busybox... */
422 #if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
423 #error "You need to build uClibc with UCLIBC_HAS_RPC for NFS support."
426 #define MOUNTPORT 635
427 #define MNTPATHLEN 1024
428 #define MNTNAMLEN 255
432 typedef char fhandle[FHSIZE];
435 unsigned int fhandle3_len;
447 MNT3ERR_NAMETOOLONG = 63,
448 MNT3ERR_NOTSUPP = 10004,
449 MNT3ERR_SERVERFAULT = 10006,
451 typedef enum mountstat3 mountstat3;
454 unsigned int fhs_status;
459 typedef struct fhstatus fhstatus;
461 struct mountres3_ok {
464 unsigned int auth_flavours_len;
465 char *auth_flavours_val;
468 typedef struct mountres3_ok mountres3_ok;
471 mountstat3 fhs_status;
473 mountres3_ok mountinfo;
476 typedef struct mountres3 mountres3;
478 typedef char *dirpath;
482 typedef struct mountbody *mountlist;
486 dirpath ml_directory;
489 typedef struct mountbody mountbody;
491 typedef struct groupnode *groups;
497 typedef struct groupnode groupnode;
499 typedef struct exportnode *exports;
506 typedef struct exportnode exportnode;
519 typedef struct ppathcnf ppathcnf;
521 #define MOUNTPROG 100005
524 #define MOUNTPROC_NULL 0
525 #define MOUNTPROC_MNT 1
526 #define MOUNTPROC_DUMP 2
527 #define MOUNTPROC_UMNT 3
528 #define MOUNTPROC_UMNTALL 4
529 #define MOUNTPROC_EXPORT 5
530 #define MOUNTPROC_EXPORTALL 6
532 #define MOUNTVERS_POSIX 2
534 #define MOUNTPROC_PATHCONF 7
538 #define MOUNTPROC3_NULL 0
539 #define MOUNTPROC3_MNT 1
540 #define MOUNTPROC3_DUMP 2
541 #define MOUNTPROC3_UMNT 3
542 #define MOUNTPROC3_UMNTALL 4
543 #define MOUNTPROC3_EXPORT 5
555 * We want to be able to compile mount on old kernels in such a way
556 * that the binary will work well on more recent kernels.
557 * Thus, if necessary we teach nfsmount.c the structure of new fields
558 * that will come later.
560 * Moreover, the new kernel includes conflict with glibc includes
561 * so it is easiest to ignore the kernel altogether (at compile time).
569 unsigned char data[64];
572 struct nfs_mount_data {
575 struct nfs2_fh old_root; /* 1 */
581 int acregmin; /* 1 */
582 int acregmax; /* 1 */
583 int acdirmin; /* 1 */
584 int acdirmax; /* 1 */
585 struct sockaddr_in addr; /* 1 */
586 char hostname[256]; /* 1 */
588 unsigned int bsize; /* 3 */
589 struct nfs3_fh root; /* 4 */
592 /* bits in the flags field */
594 NFS_MOUNT_SOFT = 0x0001, /* 1 */
595 NFS_MOUNT_INTR = 0x0002, /* 1 */
596 NFS_MOUNT_SECURE = 0x0004, /* 1 */
597 NFS_MOUNT_POSIX = 0x0008, /* 1 */
598 NFS_MOUNT_NOCTO = 0x0010, /* 1 */
599 NFS_MOUNT_NOAC = 0x0020, /* 1 */
600 NFS_MOUNT_TCP = 0x0040, /* 2 */
601 NFS_MOUNT_VER3 = 0x0080, /* 3 */
602 NFS_MOUNT_KERBEROS = 0x0100, /* 3 */
603 NFS_MOUNT_NONLM = 0x0200 /* 3 */
608 * We need to translate between nfs status return values and
609 * the local errno values which may not be the same.
611 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
612 * "after #include <errno.h> the symbol errno is reserved for any use,
613 * it cannot even be used as a struct tag or field name".
617 #define EDQUOT ENOSPC
620 // Convert each NFSERR_BLAH into EBLAH
622 static const struct {
626 {0,0}, {1,EPERM}, {2,ENOENT}, {5,EIO}, {6,ENXIO}, {13,EACCES}, {17,EEXIST},
627 {19,ENODEV}, {20,ENOTDIR}, {21,EISDIR}, {22,EINVAL}, {27,EFBIG},
628 {28,ENOSPC}, {30,EROFS}, {63,ENAMETOOLONG}, {66,ENOTEMPTY}, {69,EDQUOT},
629 {70,ESTALE}, {71,EREMOTE}, {-1,EIO}
632 static char *nfs_strerror(int status)
636 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
637 if (nfs_errtbl[i].stat == status)
638 return strerror(nfs_errtbl[i].errnum);
640 return xasprintf("unknown nfs status return value: %d", status);
643 static bool_t xdr_fhandle(XDR *xdrs, fhandle objp)
645 if (!xdr_opaque(xdrs, objp, FHSIZE))
650 static bool_t xdr_fhstatus(XDR *xdrs, fhstatus *objp)
652 if (!xdr_u_int(xdrs, &objp->fhs_status))
654 switch (objp->fhs_status) {
656 if (!xdr_fhandle(xdrs, objp->fhstatus_u.fhs_fhandle))
665 static bool_t xdr_dirpath(XDR *xdrs, dirpath *objp)
667 if (!xdr_string(xdrs, objp, MNTPATHLEN))
672 static bool_t xdr_fhandle3(XDR *xdrs, fhandle3 *objp)
674 if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, (unsigned int *) &objp->fhandle3_len, FHSIZE3))
679 static bool_t xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp)
681 if (!xdr_fhandle3(xdrs, &objp->fhandle))
683 if (!xdr_array(xdrs, &(objp->auth_flavours.auth_flavours_val), &(objp->auth_flavours.auth_flavours_len), ~0,
684 sizeof (int), (xdrproc_t) xdr_int))
689 static bool_t xdr_mountstat3(XDR *xdrs, mountstat3 *objp)
691 if (!xdr_enum(xdrs, (enum_t *) objp))
696 static bool_t xdr_mountres3(XDR *xdrs, mountres3 *objp)
698 if (!xdr_mountstat3(xdrs, &objp->fhs_status))
700 switch (objp->fhs_status) {
702 if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo))
711 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
713 static smalluint nfs_mount_version;
716 * Unfortunately, the kernel prints annoying console messages
717 * in case of an unexpected nfs mount version (instead of
718 * just returning some error). Therefore we'll have to try
719 * and figure out what version the kernel expects.
722 * KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
723 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
724 * nfs_mount_version: version this source and running kernel can handle
727 find_kernel_nfs_mount_version(void)
731 if (nfs_mount_version)
734 nfs_mount_version = 4; /* default */
736 kernel_version = get_linux_version_code();
737 if (kernel_version) {
738 if (kernel_version < KERNEL_VERSION(2,1,32))
739 nfs_mount_version = 1;
740 else if (kernel_version < KERNEL_VERSION(2,2,18) ||
741 (kernel_version >= KERNEL_VERSION(2,3,0) &&
742 kernel_version < KERNEL_VERSION(2,3,99)))
743 nfs_mount_version = 3;
744 /* else v4 since 2.3.99pre4 */
749 get_mountport(struct pmap *pm_mnt,
750 struct sockaddr_in *server_addr,
752 long unsigned version,
756 struct pmaplist *pmap;
758 server_addr->sin_port = PMAPPORT;
759 /* glibc 2.4 (still) has pmap_getmaps(struct sockaddr_in *).
760 * I understand it like "IPv6 for this is not 100% ready" */
761 pmap = pmap_getmaps(server_addr);
763 if (version > MAX_NFSPROT)
764 version = MAX_NFSPROT;
767 pm_mnt->pm_prog = prog;
768 pm_mnt->pm_vers = version;
769 pm_mnt->pm_prot = proto;
770 pm_mnt->pm_port = port;
773 if (pmap->pml_map.pm_prog != prog)
775 if (!version && pm_mnt->pm_vers > pmap->pml_map.pm_vers)
777 if (version > 2 && pmap->pml_map.pm_vers != version)
779 if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
781 if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
782 (proto && pm_mnt->pm_prot && pmap->pml_map.pm_prot != proto) ||
783 (port && pmap->pml_map.pm_port != port))
785 memcpy(pm_mnt, &pmap->pml_map, sizeof(*pm_mnt));
787 pmap = pmap->pml_next;
789 if (!pm_mnt->pm_vers)
790 pm_mnt->pm_vers = MOUNTVERS;
791 if (!pm_mnt->pm_port)
792 pm_mnt->pm_port = MOUNTPORT;
793 if (!pm_mnt->pm_prot)
794 pm_mnt->pm_prot = IPPROTO_TCP;
798 static int daemonize(void)
802 if (pid < 0) /* error */
804 if (pid > 0) /* parent */
807 fd = xopen(bb_dev_null, O_RDWR);
811 while (fd > 2) close(fd--);
813 openlog(applet_name, LOG_PID, LOG_DAEMON);
814 logmode = LOGMODE_SYSLOG;
818 static inline int daemonize(void) { return -ENOSYS; }
822 static inline int we_saw_this_host_before(const char *hostname)
827 /* RPC strerror analogs are terminally idiotic:
828 * *mandatory* prefix and \n at end.
829 * This hopefully helps. Usage:
830 * error_msg_rpc(clnt_*error*(" ")) */
831 static void error_msg_rpc(const char *msg)
834 while (msg[0] == ' ' || msg[0] == ':') msg++;
836 while (len && msg[len-1] == '\n') len--;
837 bb_error_msg("%.*s", len, msg);
840 // NB: mp->xxx fields may be trashed on exit
841 static int nfsmount(struct mntent *mp, int vfsflags, char *filteropts)
847 struct nfs_mount_data data;
850 struct sockaddr_in server_addr;
851 struct sockaddr_in mount_server_addr;
854 struct fhstatus nfsv2;
855 struct mountres3 nfsv3;
881 find_kernel_nfs_mount_version();
889 /* NB: hostname, mounthost, filteropts must be free()d prior to return */
891 filteropts = xstrdup(filteropts); /* going to trash it later... */
893 hostname = xstrdup(mp->mnt_fsname);
894 /* mount_main() guarantees that ':' is there */
895 s = strchr(hostname, ':');
898 /* Ignore all but first hostname in replicated mounts
899 until they can be fully supported. (mack@sgi.com) */
900 s = strchr(hostname, ',');
903 bb_error_msg("warning: multiple hostnames not supported");
906 server_addr.sin_family = AF_INET;
907 if (!inet_aton(hostname, &server_addr.sin_addr)) {
908 hp = gethostbyname(hostname);
910 bb_herror_msg("%s", hostname);
913 if (hp->h_length > sizeof(struct in_addr)) {
914 bb_error_msg("got bad hp->h_length");
915 hp->h_length = sizeof(struct in_addr);
917 memcpy(&server_addr.sin_addr,
918 hp->h_addr, hp->h_length);
921 memcpy(&mount_server_addr, &server_addr, sizeof(mount_server_addr));
923 /* add IP address to mtab options for use when unmounting */
925 if (!mp->mnt_opts) { /* TODO: actually mp->mnt_opts is never NULL */
926 mp->mnt_opts = xasprintf("addr=%s", inet_ntoa(server_addr.sin_addr));
928 char *tmp = xasprintf("%s%saddr=%s", mp->mnt_opts,
929 mp->mnt_opts[0] ? "," : "",
930 inet_ntoa(server_addr.sin_addr));
935 /* Set default options.
936 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
937 * let the kernel decide.
938 * timeo is filled in after we know whether it'll be TCP or UDP. */
939 memset(&data, 0, sizeof(data));
945 data.namlen = NAME_MAX;
953 retry = 10000; /* 10000 minutes ~ 1 week */
956 mountprog = MOUNTPROG;
964 if (filteropts) for (opt = strtok(filteropts, ","); opt; opt = strtok(NULL, ",")) {
965 char *opteq = strchr(opt, '=');
967 static const char options[] ALIGN1 =
979 /* 11 */ "mountport\0"
980 /* 12 */ "mounthost\0"
981 /* 13 */ "mountprog\0"
982 /* 14 */ "mountvers\0"
989 int val = xatoi_u(opteq + 1);
991 switch (index_in_strings(options, opt)) {
1001 case 3: // "retrans"
1004 case 4: // "acregmin"
1005 data.acregmin = val;
1007 case 5: // "acregmax"
1008 data.acregmax = val;
1010 case 6: // "acdirmin"
1011 data.acdirmin = val;
1013 case 7: // "acdirmax"
1014 data.acdirmax = val;
1016 case 8: // "actimeo"
1017 data.acregmin = val;
1018 data.acregmax = val;
1019 data.acdirmin = val;
1020 data.acdirmax = val;
1028 case 11: // "mountport"
1031 case 12: // "mounthost"
1032 mounthost = xstrndup(opteq+1,
1033 strcspn(opteq+1," \t\n\r,"));
1035 case 13: // "mountprog"
1038 case 14: // "mountvers"
1041 case 15: // "nfsprog"
1044 case 16: // "nfsvers"
1049 if (!strncmp(opteq+1, "tcp", 3))
1051 else if (!strncmp(opteq+1, "udp", 3))
1054 bb_error_msg("warning: unrecognized proto= option");
1056 case 19: // "namlen"
1057 if (nfs_mount_version >= 2)
1060 bb_error_msg("warning: option namlen is not supported\n");
1062 case 20: // "addr" - ignore
1065 bb_error_msg("unknown nfs mount parameter: %s=%d", opt, val);
1070 static const char options[] ALIGN1 =
1083 if (!strncmp(opt, "no", 2)) {
1087 switch (index_in_strings(options, opt)) {
1123 if (nfs_mount_version >= 3)
1126 bb_error_msg("warning: option nolock is not supported");
1129 bb_error_msg("unknown nfs mount option: %s%s", val ? "" : "no", opt);
1134 proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
1136 data.flags = (soft ? NFS_MOUNT_SOFT : 0)
1137 | (intr ? NFS_MOUNT_INTR : 0)
1138 | (posix ? NFS_MOUNT_POSIX : 0)
1139 | (nocto ? NFS_MOUNT_NOCTO : 0)
1140 | (noac ? NFS_MOUNT_NOAC : 0);
1141 if (nfs_mount_version >= 2)
1142 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
1143 if (nfs_mount_version >= 3)
1144 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
1145 if (nfsvers > MAX_NFSPROT || mountvers > MAX_NFSPROT) {
1146 bb_error_msg("NFSv%d not supported", nfsvers);
1149 if (nfsvers && !mountvers)
1150 mountvers = (nfsvers < 3) ? 1 : nfsvers;
1151 if (nfsvers && nfsvers < mountvers) {
1152 mountvers = nfsvers;
1155 /* Adjust options if none specified */
1157 data.timeo = tcp ? 70 : 7;
1159 data.version = nfs_mount_version;
1161 if (vfsflags & MS_REMOUNT)
1165 * If the previous mount operation on the same host was
1166 * backgrounded, and the "bg" for this mount is also set,
1167 * give up immediately, to avoid the initial timeout.
1169 if (bg && we_saw_this_host_before(hostname)) {
1170 daemonized = daemonize();
1171 if (daemonized <= 0) { /* parent or error */
1172 retval = -daemonized;
1177 /* create mount daemon client */
1178 /* See if the nfs host = mount host. */
1180 if (mounthost[0] >= '0' && mounthost[0] <= '9') {
1181 mount_server_addr.sin_family = AF_INET;
1182 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
1184 hp = gethostbyname(mounthost);
1186 bb_herror_msg("%s", mounthost);
1189 if (hp->h_length > sizeof(struct in_addr)) {
1190 bb_error_msg("got bad hp->h_length?");
1191 hp->h_length = sizeof(struct in_addr);
1193 mount_server_addr.sin_family = AF_INET;
1194 memcpy(&mount_server_addr.sin_addr,
1195 hp->h_addr, hp->h_length);
1201 * The following loop implements the mount retries. When the mount
1202 * times out, and the "bg" option is set, we background ourself
1203 * and continue trying.
1205 * The case where the mount point is not present and the "bg"
1206 * option is set, is treated as a timeout. This is done to
1207 * support nested mounts.
1209 * The "retry" count specified by the user is the number of
1210 * minutes to retry before giving up.
1213 struct timeval total_timeout;
1214 struct timeval retry_timeout;
1220 retry_timeout.tv_sec = 3;
1221 retry_timeout.tv_usec = 0;
1222 total_timeout.tv_sec = 20;
1223 total_timeout.tv_usec = 0;
1224 timeout = time(NULL) + 60 * retry;
1228 /* be careful not to use too many CPU cycles */
1232 get_mountport(&pm_mnt, &mount_server_addr,
1237 nfsvers = (pm_mnt.pm_vers < 2) ? 2 : pm_mnt.pm_vers;
1239 /* contact the mount daemon via TCP */
1240 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1241 msock = RPC_ANYSOCK;
1243 switch (pm_mnt.pm_prot) {
1245 mclient = clntudp_create(&mount_server_addr,
1252 mount_server_addr.sin_port = htons(pm_mnt.pm_port);
1253 msock = RPC_ANYSOCK;
1255 mclient = clnttcp_create(&mount_server_addr,
1264 if (!daemonized && prevt == 0)
1265 error_msg_rpc(clnt_spcreateerror(" "));
1267 enum clnt_stat clnt_stat;
1268 /* try to mount hostname:pathname */
1269 mclient->cl_auth = authunix_create_default();
1271 /* make pointers in xdr_mountres3 NULL so
1272 * that xdr_array allocates memory for us
1274 memset(&status, 0, sizeof(status));
1276 if (pm_mnt.pm_vers == 3)
1277 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
1278 (xdrproc_t) xdr_dirpath,
1279 (caddr_t) &pathname,
1280 (xdrproc_t) xdr_mountres3,
1284 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
1285 (xdrproc_t) xdr_dirpath,
1286 (caddr_t) &pathname,
1287 (xdrproc_t) xdr_fhstatus,
1291 if (clnt_stat == RPC_SUCCESS)
1292 goto prepare_kernel_data; /* we're done */
1293 if (errno != ECONNREFUSED) {
1294 error_msg_rpc(clnt_sperror(mclient, " "));
1295 goto fail; /* don't retry */
1297 /* Connection refused */
1298 if (!daemonized && prevt == 0) /* print just once */
1299 error_msg_rpc(clnt_sperror(mclient, " "));
1300 auth_destroy(mclient->cl_auth);
1301 clnt_destroy(mclient);
1307 /* Timeout. We are going to retry... maybe */
1312 daemonized = daemonize();
1313 if (daemonized <= 0) { /* parent or error */
1314 retval = -daemonized;
1321 /* TODO error message */
1327 prepare_kernel_data:
1330 if (status.nfsv2.fhs_status != 0) {
1331 bb_error_msg("%s:%s failed, reason given by server: %s",
1333 nfs_strerror(status.nfsv2.fhs_status));
1336 memcpy(data.root.data,
1337 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1339 data.root.size = NFS_FHSIZE;
1340 memcpy(data.old_root.data,
1341 (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
1344 fhandle3 *my_fhandle;
1345 if (status.nfsv3.fhs_status != 0) {
1346 bb_error_msg("%s:%s failed, reason given by server: %s",
1348 nfs_strerror(status.nfsv3.fhs_status));
1351 my_fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
1352 memset(data.old_root.data, 0, NFS_FHSIZE);
1353 memset(&data.root, 0, sizeof(data.root));
1354 data.root.size = my_fhandle->fhandle3_len;
1355 memcpy(data.root.data,
1356 (char *) my_fhandle->fhandle3_val,
1357 my_fhandle->fhandle3_len);
1359 data.flags |= NFS_MOUNT_VER3;
1362 /* create nfs socket for kernel */
1365 if (nfs_mount_version < 3) {
1366 bb_error_msg("NFS over TCP is not supported");
1369 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1371 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1373 bb_perror_msg("nfs socket");
1376 if (bindresvport(fsock, 0) < 0) {
1377 bb_perror_msg("nfs bindresvport");
1381 server_addr.sin_port = PMAPPORT;
1382 port = pmap_getport(&server_addr, nfsprog, nfsvers,
1383 tcp ? IPPROTO_TCP : IPPROTO_UDP);
1387 server_addr.sin_port = htons(port);
1389 /* prepare data structure for kernel */
1392 memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
1393 strncpy(data.hostname, hostname, sizeof(data.hostname));
1397 auth_destroy(mclient->cl_auth);
1398 clnt_destroy(mclient);
1403 /* We must wait until mount directory is available */
1404 struct stat statbuf;
1406 while (stat(mp->mnt_dir, &statbuf) == -1) {
1408 daemonized = daemonize();
1409 if (daemonized <= 0) { /* parent or error */
1410 // FIXME: parent doesn't close fsock - ??!
1411 retval = -daemonized;
1415 sleep(delay); /* 1, 2, 4, 8, 16, 30, ... */
1422 do_mount: /* perform actual mount */
1424 mp->mnt_type = (char*)"nfs";
1425 retval = mount_it_now(mp, vfsflags, (char*)&data);
1432 auth_destroy(mclient->cl_auth);
1433 clnt_destroy(mclient);
1447 #else /* !ENABLE_FEATURE_MOUNT_NFS */
1449 /* Never called. Call should be optimized out. */
1450 int nfsmount(struct mntent *mp, int vfsflags, char *filteropts);
1452 #endif /* !ENABLE_FEATURE_MOUNT_NFS */
1454 // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
1455 // type detection. Returns 0 for success, nonzero for failure.
1456 // NB: mp->xxx fields may be trashed on exit
1457 static int singlemount(struct mntent *mp, int ignore_busy)
1459 int rc = -1, vfsflags;
1460 char *loopFile = 0, *filteropts = 0;
1464 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
1466 // Treat fstype "auto" as unspecified.
1468 if (mp->mnt_type && strcmp(mp->mnt_type,"auto") == 0)
1471 // Might this be a virtual filesystem?
1473 if (ENABLE_FEATURE_MOUNT_HELPERS
1474 && (strchr(mp->mnt_fsname, '#'))
1476 char *s, *p, *args[35];
1478 for (s = p = mp->mnt_fsname; *s && n < 35-3; ++s) {
1479 if (s[0] == '#' && s[1] != '#') {
1486 args[n++] = mp->mnt_dir;
1488 rc = wait4pid(xspawn(args));
1492 // Might this be an CIFS filesystem?
1494 if (ENABLE_FEATURE_MOUNT_CIFS
1495 && (!mp->mnt_type || strcmp(mp->mnt_type,"cifs") == 0)
1496 && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')
1497 && mp->mnt_fsname[0]==mp->mnt_fsname[1]
1499 len_and_sockaddr *lsa;
1504 // Replace '/' with '\' and verify that unc points to "//server/share".
1506 for (s = mp->mnt_fsname; *s; ++s)
1507 if (*s == '/') *s = '\\';
1511 s = strrchr(mp->mnt_fsname, '\\');
1512 if (s <= mp->mnt_fsname+1) goto report_error;
1514 lsa = host2sockaddr(mp->mnt_fsname+2, 0);
1516 if (!lsa) goto report_error;
1518 // insert ip=... option into string flags.
1520 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
1521 ip = xasprintf("ip=%s", dotted);
1522 parse_mount_options(ip, &filteropts);
1524 // compose new unc '\\server-ip\share'
1525 // (s => slash after hostname)
1527 mp->mnt_fsname = xasprintf("\\\\%s%s", dotted, s);
1530 vfsflags |= MS_MANDLOCK;
1532 mp->mnt_type = (char*)"cifs";
1533 rc = mount_it_now(mp, vfsflags, filteropts);
1534 if (ENABLE_FEATURE_CLEAN_UP) {
1535 free(mp->mnt_fsname);
1543 // Might this be an NFS filesystem?
1545 if (ENABLE_FEATURE_MOUNT_NFS
1546 && (!mp->mnt_type || !strcmp(mp->mnt_type, "nfs"))
1547 && strchr(mp->mnt_fsname, ':') != NULL
1549 rc = nfsmount(mp, vfsflags, filteropts);
1553 // Look at the file. (Not found isn't a failure for remount, or for
1554 // a synthetic filesystem like proc or sysfs.)
1555 // (We use stat, not lstat, in order to allow
1556 // mount symlink_to_file_or_blkdev dir)
1558 if (!stat(mp->mnt_fsname, &st)
1559 && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))
1561 // Do we need to allocate a loopback device for it?
1563 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
1564 loopFile = bb_simplify_path(mp->mnt_fsname);
1565 mp->mnt_fsname = NULL; /* will receive malloced loop dev name */
1566 if (set_loop(&(mp->mnt_fsname), loopFile, 0) < 0) {
1567 if (errno == EPERM || errno == EACCES)
1568 bb_error_msg(bb_msg_perm_denied_are_you_root);
1570 bb_perror_msg("cannot setup loop device");
1574 // Autodetect bind mounts
1576 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
1577 vfsflags |= MS_BIND;
1580 /* If we know the fstype (or don't need to), jump straight
1581 * to the actual mount. */
1583 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
1584 rc = mount_it_now(mp, vfsflags, filteropts);
1586 // Loop through filesystem types until mount succeeds
1589 /* Initialize list of block backed filesystems. This has to be
1590 * done here so that during "mount -a", mounts after /proc shows up
1591 * can autodetect. */
1594 fslist = get_block_backed_filesystems();
1595 if (ENABLE_FEATURE_CLEAN_UP && fslist)
1596 atexit(delete_block_backed_filesystems);
1599 for (fl = fslist; fl; fl = fl->link) {
1600 mp->mnt_type = fl->data;
1601 rc = mount_it_now(mp, vfsflags, filteropts);
1606 // If mount failed, clean up loop file (if any).
1608 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
1609 del_loop(mp->mnt_fsname);
1610 if (ENABLE_FEATURE_CLEAN_UP) {
1612 free(mp->mnt_fsname);
1617 if (ENABLE_FEATURE_CLEAN_UP)
1620 if (errno == EBUSY && ignore_busy)
1623 bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
1627 // Parse options, if necessary parse fstab/mtab, and call singlemount for
1628 // each directory to be mounted.
1630 static const char must_be_root[] ALIGN1 = "you must be root";
1632 int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1633 int mount_main(int argc, char **argv)
1635 enum { OPT_ALL = 0x10 };
1637 char *cmdopts = xstrdup(""), *fstype=0, *storage_path=0;
1639 const char *fstabname;
1643 struct mntent mtpair[2], *mtcur = mtpair;
1644 SKIP_DESKTOP(const int nonroot = 0;)
1645 USE_DESKTOP( int nonroot = (getuid() != 0);)
1647 /* parse long options, like --bind and --move. Note that -o option
1648 * and --option are synonymous. Yes, this means --remount,rw works. */
1650 for (i = j = 0; i < argc; i++) {
1651 if (argv[i][0] == '-' && argv[i][1] == '-') {
1652 append_mount_options(&cmdopts, argv[i]+2);
1653 } else argv[j++] = argv[i];
1658 // Parse remaining options
1660 opt = getopt32(argv, "o:t:rwanfvsi", &opt_o, &fstype);
1661 if (opt & 0x1) append_mount_options(&cmdopts, opt_o); // -o
1662 //if (opt & 0x2) // -t
1663 if (opt & 0x4) append_mount_options(&cmdopts, "ro"); // -r
1664 if (opt & 0x8) append_mount_options(&cmdopts, "rw"); // -w
1665 //if (opt & 0x10) // -a
1666 if (opt & 0x20) USE_FEATURE_MTAB_SUPPORT(useMtab = 0); // -n
1667 if (opt & 0x40) USE_FEATURE_MTAB_SUPPORT(fakeIt = 1); // -f
1668 //if (opt & 0x80) // -v: verbose (ignore)
1669 //if (opt & 0x100) // -s: sloppy (ignore)
1670 //if (opt & 0x200) // -i: don't call mount.<fstype> (ignore)
1674 // Three or more non-option arguments? Die with a usage message.
1676 if (argc > 2) bb_show_usage();
1678 // If we have no arguments, show currently mounted filesystems
1681 if (!(opt & OPT_ALL)) {
1682 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
1684 if (!mountTable) bb_error_msg_and_die("no %s", bb_path_mtab_file);
1686 while (getmntent_r(mountTable, &mtpair[0], getmntent_buf,
1687 sizeof(getmntent_buf)))
1689 // Don't show rootfs. FIXME: why??
1690 // util-linux 2.12a happily shows rootfs...
1691 //if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
1693 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
1694 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
1695 mtpair->mnt_dir, mtpair->mnt_type,
1698 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
1699 return EXIT_SUCCESS;
1701 } else storage_path = bb_simplify_path(argv[0]);
1703 // When we have two arguments, the second is the directory and we can
1704 // skip looking at fstab entirely. We can always abspath() the directory
1705 // argument when we get it.
1709 bb_error_msg_and_die(must_be_root);
1710 mtpair->mnt_fsname = argv[0];
1711 mtpair->mnt_dir = argv[1];
1712 mtpair->mnt_type = fstype;
1713 mtpair->mnt_opts = cmdopts;
1714 rc = singlemount(mtpair, 0);
1718 i = parse_mount_options(cmdopts, 0);
1719 if (nonroot && (i & ~MS_SILENT)) // Non-root users cannot specify flags
1720 bb_error_msg_and_die(must_be_root);
1722 // If we have a shared subtree flag, don't worry about fstab or mtab.
1724 if (ENABLE_FEATURE_MOUNT_FLAGS
1725 && (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1727 rc = mount("", argv[0], "", i, "");
1728 if (rc) bb_simple_perror_msg_and_die(argv[0]);
1732 // Open either fstab or mtab
1734 fstabname = "/etc/fstab";
1735 if (i & MS_REMOUNT) {
1736 fstabname = bb_path_mtab_file;
1738 fstab = setmntent(fstabname, "r");
1740 bb_perror_msg_and_die("cannot read %s", fstabname);
1742 // Loop through entries until we find what we're looking for.
1744 memset(mtpair, 0, sizeof(mtpair));
1746 struct mntent *mtnext = (mtcur==mtpair ? mtpair+1 : mtpair);
1748 // Get next fstab entry
1750 if (!getmntent_r(fstab, mtcur, getmntent_buf
1751 + (mtcur==mtpair ? sizeof(getmntent_buf)/2 : 0),
1752 sizeof(getmntent_buf)/2))
1754 // Were we looking for something specific?
1758 // If we didn't find anything, complain.
1760 if (!mtnext->mnt_fsname)
1761 bb_error_msg_and_die("can't find %s in %s",
1762 argv[0], fstabname);
1766 // fstab must have "users" or "user"
1767 if (!(parse_mount_options(mtcur->mnt_opts, 0) & MOUNT_USERS))
1768 bb_error_msg_and_die(must_be_root);
1771 // Mount the last thing we found.
1773 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
1774 append_mount_options(&(mtcur->mnt_opts), cmdopts);
1775 rc = singlemount(mtcur, 0);
1776 free(mtcur->mnt_opts);
1781 /* If we're trying to mount something specific and this isn't it,
1782 * skip it. Note we must match both the exact text in fstab (ala
1783 * "proc") or a full path from root */
1787 // Is this what we're looking for?
1789 if (strcmp(argv[0], mtcur->mnt_fsname) &&
1790 strcmp(storage_path, mtcur->mnt_fsname) &&
1791 strcmp(argv[0], mtcur->mnt_dir) &&
1792 strcmp(storage_path, mtcur->mnt_dir)) continue;
1794 // Remember this entry. Something later may have overmounted
1795 // it, and we want the _last_ match.
1799 // If we're mounting all.
1802 // Do we need to match a filesystem type?
1803 if (fstype && match_fstype(mtcur, fstype)) continue;
1805 // Skip noauto and swap anyway.
1807 if (parse_mount_options(mtcur->mnt_opts, 0)
1808 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
1810 // No, mount -a won't mount anything,
1811 // even user mounts, for mere humans.
1814 bb_error_msg_and_die(must_be_root);
1816 // Mount this thing.
1818 // NFS mounts want this to be xrealloc-able
1819 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
1820 if (singlemount(mtcur, 1)) {
1821 /* Count number of failed mounts */
1824 free(mtcur->mnt_opts);
1827 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
1831 if (ENABLE_FEATURE_CLEAN_UP) {