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.
13 * bb_getopt_ulflags();
16 /* Design notes: There is no spec for mount. Remind me to write one.
18 mount_main() calls singlemount() which calls mount_it_now().
20 mount_main() can loop through /etc/fstab for mount -a
21 singlemount() can loop through /etc/filesystems for fstype detection.
22 mount_it_now() does the actual mount.
28 // Not real flags, but we want to be able to check for this.
29 #define MOUNT_NOAUTO (1<<29)
30 #define MOUNT_SWAP (1<<30)
32 /* Standard mount options (from -o options or --options), with corresponding
38 } static mount_options[] = {
39 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
41 USE_FEATURE_MOUNT_LOOP(
45 USE_FEATURE_MOUNT_FSTAB(
48 {"noauto",MOUNT_NOAUTO},
52 USE_FEATURE_MOUNT_FLAGS(
54 {"nosuid", MS_NOSUID},
59 {"noexec", MS_NOEXEC},
60 {"sync", MS_SYNCHRONOUS},
61 {"async", ~MS_SYNCHRONOUS},
62 {"atime", ~MS_NOATIME},
63 {"noatime", MS_NOATIME},
64 {"diratime", ~MS_NODIRATIME},
65 {"nodiratime", MS_NODIRATIME},
72 {"shared", MS_SHARED},
74 {"private", MS_PRIVATE},
75 {"unbindable", MS_UNBINDABLE},
76 {"rshared", MS_SHARED|MS_RECURSIVE},
77 {"rslave", MS_SLAVE|MS_RECURSIVE},
78 {"rprivate", MS_SLAVE|MS_RECURSIVE},
79 {"runbindable", MS_UNBINDABLE|MS_RECURSIVE},
84 {"ro", MS_RDONLY}, // vfs flag
85 {"rw", ~MS_RDONLY}, // vfs flag
86 {"remount", MS_REMOUNT}, // action flag
91 /* Append mount options to string */
92 static void append_mount_options(char **oldopts, char *newopts)
94 if(*oldopts && **oldopts) {
95 char *temp = xasprintf("%s,%s",*oldopts,newopts);
99 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
100 *oldopts = xstrdup(newopts);
104 /* Use the mount_options list to parse options into flags.
105 * Also return list of unrecognized options if unrecognized!=NULL */
106 static int parse_mount_options(char *options, char **unrecognized)
108 int flags = MS_SILENT;
110 // Loop through options
113 char *comma = strchr(options, ',');
115 if (comma) *comma = 0;
117 // Find this option in mount_options
118 for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
119 if (!strcasecmp(mount_options[i].name, options)) {
120 long fl = mount_options[i].flags;
121 if(fl < 0) flags &= fl;
126 // If unrecognized not NULL, append unrecognized mount options */
128 && i == (sizeof(mount_options) / sizeof(*mount_options)))
130 // Add it to strflags, to pass on to kernel
131 i = *unrecognized ? strlen(*unrecognized) : 0;
132 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
134 // Comma separated if it's not the first one
135 if (i) (*unrecognized)[i++] = ',';
136 strcpy((*unrecognized)+i, options);
139 // Advance to next option, or finish
149 // Return a list of all block device backed filesystems
151 static llist_t *get_block_backed_filesystems(void)
154 *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
159 for(i = 0; filesystems[i]; i++) {
160 if(!(f = fopen(filesystems[i], "r"))) continue;
162 for(fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f));
165 if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
167 while(isspace(*fs)) fs++;
168 if(*fs=='#' || *fs=='*') continue;
171 llist_add_to_end(&list,xstrdup(fs));
173 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
181 #if ENABLE_FEATURE_CLEAN_UP
182 static void delete_block_backed_filesystems(void)
184 llist_free(fslist, free);
187 void delete_block_backed_filesystems(void);
190 #if ENABLE_FEATURE_MTAB_SUPPORT
191 static int useMtab = 1;
198 // Perform actual mount of specific filesystem at specific location.
199 int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
203 if (fakeIt) { return 0; }
205 // Mount, with fallback to read-only if necessary.
208 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
209 vfsflags, filteropts);
210 if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
212 bb_error_msg("%s is write-protected, mounting read-only",
214 vfsflags |= MS_RDONLY;
217 // Abort entirely if permission denied.
219 if (rc && errno == EPERM)
220 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
222 /* If the mount was successful, and we're maintaining an old-style
223 * mtab file by hand, add the new entry to it now. */
225 if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
226 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
230 bb_error_msg("no %s",bb_path_mtab_file);
232 // Add vfs string flags
234 for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
235 if (mount_options[i].flags > 0)
236 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
238 // Remove trailing / (if any) from directory we mounted on
240 i = strlen(mp->mnt_dir);
241 if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0;
245 if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind";
246 // addmntent(mountTable, mp);
247 if(0) bb_error_msg("buggy: addmntent(fsname='%s' dir='%s' type='%s' opts='%s')",
253 endmntent(mountTable);
254 if (ENABLE_FEATURE_CLEAN_UP)
255 if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0;
261 // Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
262 // type detection. Returns 0 for success, nonzero for failure.
264 static int singlemount(struct mntent *mp, int ignore_busy)
266 int rc = -1, vfsflags;
267 char *loopFile = 0, *filteropts = 0;
271 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
273 // Treat fstype "auto" as unspecified.
275 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
277 // Might this be an CIFS filesystem?
279 if(ENABLE_FEATURE_MOUNT_CIFS &&
280 (!mp->mnt_type || !strcmp(mp->mnt_type,"cifs")) &&
281 (mp->mnt_fsname[0]==mp->mnt_fsname[1] && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')))
287 // Replace '/' with '\' and verify that unc points to "//server/share".
289 for (s = mp->mnt_fsname; *s; ++s)
290 if (*s == '/') *s = '\\';
294 s = strrchr(mp->mnt_fsname, '\\');
295 if (s == mp->mnt_fsname+1) goto report_error;
297 he = gethostbyname(mp->mnt_fsname+2);
299 if (!he) goto report_error;
301 // Insert ip=... option into string flags. (NOTE: Add IPv6 support.)
303 sprintf(ip, "ip=%d.%d.%d.%d", he->h_addr[0], he->h_addr[1],
304 he->h_addr[2], he->h_addr[3]);
305 parse_mount_options(ip, &filteropts);
307 // compose new unc '\\server-ip\share'
309 s = xasprintf("\\\\%s%s",ip+3,strchr(mp->mnt_fsname+2,'\\'));
310 if (ENABLE_FEATURE_CLEAN_UP) free(mp->mnt_fsname);
314 vfsflags |= MS_MANDLOCK;
316 mp->mnt_type = "cifs";
317 rc = mount_it_now(mp, vfsflags, filteropts);
321 // Might this be an NFS filesystem?
323 if (ENABLE_FEATURE_MOUNT_NFS &&
324 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
325 strchr(mp->mnt_fsname, ':') != NULL)
327 rc = nfsmount(mp, vfsflags, filteropts);
331 // Look at the file. (Not found isn't a failure for remount, or for
332 // a synthetic filesystem like proc or sysfs.)
334 if (!lstat(mp->mnt_fsname, &st) && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
336 // Do we need to allocate a loopback device for it?
338 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
339 loopFile = bb_simplify_path(mp->mnt_fsname);
341 switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) {
346 bb_error_msg( errno == EPERM || errno == EACCES
347 ? bb_msg_perm_denied_are_you_root
348 : "cannot setup loop device");
352 // Autodetect bind mounts
354 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
358 /* If we know the fstype (or don't need to), jump straight
359 * to the actual mount. */
361 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
362 rc = mount_it_now(mp, vfsflags, filteropts);
364 // Loop through filesystem types until mount succeeds or we run out
368 /* Initialize list of block backed filesystems. This has to be
369 * done here so that during "mount -a", mounts after /proc shows up
373 fslist = get_block_backed_filesystems();
374 if (ENABLE_FEATURE_CLEAN_UP && fslist)
375 atexit(delete_block_backed_filesystems);
378 for (fl = fslist; fl; fl = fl->link) {
379 mp->mnt_type = fl->data;
381 if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break;
387 // If mount failed, clean up loop file (if any).
389 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
390 del_loop(mp->mnt_fsname);
391 if (ENABLE_FEATURE_CLEAN_UP) {
393 free(mp->mnt_fsname);
398 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
400 if (rc && errno == EBUSY && ignore_busy) rc = 0;
402 /* perror here sometimes says "mounting ... on ... failed: Success" */
403 bb_error_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
408 // Parse options, if necessary parse fstab/mtab, and call singlemount for
409 // each directory to be mounted.
411 int mount_main(int argc, char **argv)
413 char *cmdopts = xstrdup(""), *fstabname, *fstype=0, *storage_path=0;
415 int i, opt, all = FALSE, rc = 0;
416 struct mntent mtpair[2], *mtcur = mtpair;
418 /* parse long options, like --bind and --move. Note that -o option
419 * and --option are synonymous. Yes, this means --remount,rw works. */
421 for (i = opt = 0; i < argc; i++) {
422 if (argv[i][0] == '-' && argv[i][1] == '-') {
423 append_mount_options(&cmdopts,argv[i]+2);
424 } else argv[opt++] = argv[i];
428 // Parse remaining options
430 while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) {
433 append_mount_options(&cmdopts, optarg);
439 append_mount_options(&cmdopts, "ro");
442 append_mount_options(&cmdopts, "rw");
448 USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;)
451 USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;)
460 // Three or more non-option arguments? Die with a usage message.
462 if (optind-argc>2) bb_show_usage();
464 // If we have no arguments, show currently mounted filesystems
466 if (optind == argc) {
468 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
470 if(!mountTable) bb_error_msg_and_die("no %s",bb_path_mtab_file);
472 while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
473 sizeof(bb_common_bufsiz1)))
475 // Don't show rootfs.
476 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
478 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
479 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
480 mtpair->mnt_dir, mtpair->mnt_type,
483 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
486 } else storage_path = bb_simplify_path(argv[optind]);
488 // When we have two arguments, the second is the directory and we can
489 // skip looking at fstab entirely. We can always abspath() the directory
490 // argument when we get it.
492 if (optind+2 == argc) {
493 mtpair->mnt_fsname = argv[optind];
494 mtpair->mnt_dir = argv[optind+1];
495 mtpair->mnt_type = fstype;
496 mtpair->mnt_opts = cmdopts;
497 rc = singlemount(mtpair, 0);
501 // If we have a shared subtree flag, don't worry about fstab or mtab.
502 i = parse_mount_options(cmdopts,0);
503 if (ENABLE_FEATURE_MOUNT_FLAGS &&
504 (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE )))
506 rc = mount("", argv[optind], "", i, "");
507 if (rc) bb_perror_msg_and_die("%s", argv[optind]);
511 // Open either fstab or mtab
513 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
514 fstabname = bb_path_mtab_file;
515 else fstabname="/etc/fstab";
517 if (!(fstab=setmntent(fstabname,"r")))
518 bb_perror_msg_and_die("cannot read %s",fstabname);
520 // Loop through entries until we find what we're looking for.
522 memset(mtpair,0,sizeof(mtpair));
524 struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0);
526 // Get next fstab entry
528 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
529 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
530 sizeof(bb_common_bufsiz1)/2))
532 // Were we looking for something specific?
534 if (optind != argc) {
536 // If we didn't find anything, complain.
538 if (!mtnext->mnt_fsname)
539 bb_error_msg_and_die("can't find %s in %s",
540 argv[optind], fstabname);
542 // Mount the last thing we found.
545 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
546 append_mount_options(&(mtcur->mnt_opts),cmdopts);
547 rc = singlemount(mtcur, 0);
548 free(mtcur->mnt_opts);
553 /* If we're trying to mount something specific and this isn't it,
554 * skip it. Note we must match both the exact text in fstab (ala
555 * "proc") or a full path from root */
557 if (optind != argc) {
559 // Is this what we're looking for?
561 if(strcmp(argv[optind],mtcur->mnt_fsname) &&
562 strcmp(storage_path,mtcur->mnt_fsname) &&
563 strcmp(argv[optind],mtcur->mnt_dir) &&
564 strcmp(storage_path,mtcur->mnt_dir)) continue;
566 // Remember this entry. Something later may have overmounted
567 // it, and we want the _last_ match.
571 // If we're mounting all.
575 // Do we need to match a filesystem type?
576 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
578 // Skip noauto and swap anyway.
580 if (parse_mount_options(mtcur->mnt_opts,0)
581 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
585 if (singlemount(mtcur, 1)) {
586 /* Count number of failed mounts */
591 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
595 if (ENABLE_FEATURE_CLEAN_UP) {