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 this. 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.
33 #include <sys/mount.h>
34 #include <fcntl.h> // for CONFIG_FEATURE_MOUNT_LOOP
35 #include <sys/ioctl.h> // for CONFIG_FEATURE_MOUNT_LOOP
38 // These two aren't always defined in old headers
46 #define MS_SILENT 32768
49 // Not real flags, but we want to be able to check for this.
50 #define MOUNT_NOAUTO (1<<29)
51 #define MOUNT_SWAP (1<<30)
52 /* Standard mount options (from -o options or --options), with corresponding
58 } static const mount_options[] = {
69 {"nosuid", MS_NOSUID},
74 {"noexec", MS_NOEXEC},
75 {"sync", MS_SYNCHRONOUS},
76 {"async", ~MS_SYNCHRONOUS},
77 {"atime", ~MS_NOATIME},
78 {"noatime", MS_NOATIME},
79 {"diratime", ~MS_NODIRATIME},
80 {"nodiratime", MS_NODIRATIME},
85 {"remount", MS_REMOUNT},
88 {"noauto",MOUNT_NOAUTO},
92 /* Append mount options to string */
93 static void append_mount_options(char **oldopts, char *newopts)
95 if(*oldopts && **oldopts) {
96 char *temp=bb_xasprintf("%s,%s",*oldopts,newopts);
100 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
101 *oldopts = bb_xstrdup(newopts);
105 /* Use the mount_options list to parse options into flags.
106 * Return list of unrecognized options in *strflags if strflags!=NULL */
107 static int parse_mount_options(char *options, char **unrecognized)
109 int flags = MS_SILENT;
111 // Loop through options
114 char *comma = strchr(options, ',');
116 if (comma) *comma = 0;
118 // Find this option in mount_options
119 for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
120 if (!strcasecmp(mount_options[i].name, options)) {
121 long fl = mount_options[i].flags;
122 if(fl < 0) flags &= fl;
127 // If unrecognized not NULL, append unrecognized mount options */
129 && i == (sizeof(mount_options) / sizeof(*mount_options)))
131 // Add it to strflags, to pass on to kernel
132 i = *unrecognized ? strlen(*unrecognized) : 0;
133 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
135 // Comma separated if it's not the first one
136 if (i) (*unrecognized)[i++] = ',';
137 strcpy((*unrecognized)+i, options);
140 // Advance to next option, or finish
150 // Return a list of all block device backed filesystems
152 static llist_t *get_block_backed_filesystems(void)
155 *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
160 for(i = 0; filesystems[i]; i++) {
161 if(!(f = fopen(filesystems[i], "r"))) continue;
163 for(fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f));
166 if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
168 while(isspace(*fs)) fs++;
169 if(*fs=='#' || *fs=='*') continue;
172 llist_add_to_end(&list,bb_xstrdup(fs));
174 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
182 #if ENABLE_FEATURE_CLEAN_UP
183 static void delete_block_backed_filesystems(void)
185 llist_free(fslist, free);
188 void delete_block_backed_filesystems(void);
191 #if ENABLE_FEATURE_MTAB_SUPPORT
199 // Perform actual mount of specific filesystem at specific location.
201 static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
205 if (fakeIt) { return 0; }
207 // Mount, with fallback to read-only if necessary.
210 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
211 vfsflags, filteropts);
212 if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
214 bb_error_msg("%s is write-protected, mounting read-only",
216 vfsflags |= MS_RDONLY;
219 // Abort entirely if permission denied.
221 if (rc && errno == EPERM)
222 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
224 /* If the mount was successful, and we're maintaining an old-style
225 * mtab file by hand, add the new entry to it now. */
227 if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
228 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
232 bb_error_msg("No %s\n",bb_path_mtab_file);
234 // Add vfs string flags
236 for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
237 if (mount_options[i].flags > 0)
238 append_mount_options(&(mp->mnt_opts),
239 // Shut up about the darn const. It's not important. I don't care.
240 (char *)mount_options[i].name);
242 // Remove trailing / (if any) from directory we mounted on
244 i = strlen(mp->mnt_dir);
245 if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0;
249 if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind";
250 addmntent(mountTable, mp);
251 endmntent(mountTable);
252 if (ENABLE_FEATURE_CLEAN_UP)
253 if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0;
260 // Mount one directory. Handles NFS, loopback, autobind, and filesystem type
261 // detection. Returns 0 for success, nonzero for failure.
263 static int singlemount(struct mntent *mp)
265 int rc = 1, vfsflags;
266 char *loopFile = 0, *filteropts = 0;
270 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
272 // Treat fstype "auto" as unspecified.
274 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
276 // Might this be an NFS filesystem?
278 if (ENABLE_FEATURE_MOUNT_NFS &&
279 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
280 strchr(mp->mnt_fsname, ':') != NULL)
282 if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &filteropts, 1)) {
283 bb_perror_msg("nfsmount failed");
286 // Strangely enough, nfsmount() doesn't actually mount() anything.
287 mp->mnt_type = "nfs";
288 rc = mount_it_now(mp, vfsflags, filteropts);
289 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
295 // Look at the file. (Not found isn't a failure for remount, or for
296 // a synthetic filesystem like proc or sysfs.)
298 if (lstat(mp->mnt_fsname, &st));
299 else if (!(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {
300 // Do we need to allocate a loopback device for it?
302 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
303 loopFile = bb_simplify_path(mp->mnt_fsname);
305 switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) {
310 bb_error_msg( errno == EPERM || errno == EACCES
311 ? bb_msg_perm_denied_are_you_root
312 : "Couldn't setup loop device");
316 // Autodetect bind mounts
318 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) vfsflags |= MS_BIND;
321 /* If we know the fstype (or don't need to), jump straight
322 * to the actual mount. */
324 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
325 rc = mount_it_now(mp, vfsflags, filteropts);
327 // Loop through filesystem types until mount succeeds or we run out
331 /* Initialize list of block backed filesystems. This has to be
332 * done here so that during "mount -a", mounts after /proc shows up
336 fslist = get_block_backed_filesystems();
337 if (ENABLE_FEATURE_CLEAN_UP && fslist)
338 atexit(delete_block_backed_filesystems);
341 for (fl = fslist; fl; fl = fl->link) {
342 mp->mnt_type = fl->data;
344 if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break;
350 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
352 // If mount failed, clean up loop file (if any).
354 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
355 del_loop(mp->mnt_fsname);
356 if (ENABLE_FEATURE_CLEAN_UP) {
358 free(mp->mnt_fsname);
365 // Parse options, if necessary parse fstab/mtab, and call singlemount for
366 // each directory to be mounted.
368 int mount_main(int argc, char **argv)
370 char *cmdopts = bb_xstrdup(""), *fstabname, *fstype=0, *storage_path=0;
372 int i, opt, all = FALSE, rc = 1;
373 struct mntent mtpair[2], *mtcur = mtpair;
375 /* parse long options, like --bind and --move. Note that -o option
376 * and --option are synonymous. Yes, this means --remount,rw works. */
378 for (i = opt = 0; i < argc; i++) {
379 if (argv[i][0] == '-' && argv[i][1] == '-') {
380 append_mount_options(&cmdopts,argv[i]+2);
381 } else argv[opt++] = argv[i];
385 // Parse remaining options
387 while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) {
390 append_mount_options(&cmdopts, optarg);
396 append_mount_options(&cmdopts, "ro");
399 append_mount_options(&cmdopts, "rw");
405 USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;)
408 USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;)
417 // Three or more non-option arguments? Die with a usage message.
419 if (optind-argc>2) bb_show_usage();
421 // If we have no arguments, show currently mounted filesystems
423 if (optind == argc) {
425 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
427 if(!mountTable) bb_error_msg_and_die("No %s",bb_path_mtab_file);
429 while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
430 sizeof(bb_common_bufsiz1)))
432 // Don't show rootfs.
433 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
435 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
436 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
437 mtpair->mnt_dir, mtpair->mnt_type,
440 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
445 // When we have two arguments, the second is the directory and we can
446 // skip looking at fstab entirely. We can always abspath() the directory
447 // argument when we get it.
449 if (optind+2 == argc) {
450 mtpair->mnt_fsname = argv[optind];
451 mtpair->mnt_dir = argv[optind+1];
452 mtpair->mnt_type = fstype;
453 mtpair->mnt_opts = cmdopts;
454 rc = singlemount(mtpair);
458 // If we have at least one argument, it's the storage location
460 if (optind < argc) storage_path = bb_simplify_path(argv[optind]);
462 // Open either fstab or mtab
464 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
465 fstabname = (char *)bb_path_mtab_file; // Again with the evil const.
466 else fstabname="/etc/fstab";
468 if (!(fstab=setmntent(fstabname,"r")))
469 bb_perror_msg_and_die("Cannot read %s",fstabname);
471 // Loop through entries until we find what we're looking for.
473 memset(mtpair,0,sizeof(mtpair));
475 struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0);
477 // Get next fstab entry
479 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
480 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
481 sizeof(bb_common_bufsiz1)/2))
483 // Were we looking for something specific?
485 if (optind != argc) {
487 // If we didn't find anything, complain.
489 if (!mtnext->mnt_fsname)
490 bb_error_msg_and_die("Can't find %s in %s",
491 argv[optind], fstabname);
493 // Mount the last thing we found.
496 mtcur->mnt_opts=bb_xstrdup(mtcur->mnt_opts);
497 append_mount_options(&(mtcur->mnt_opts),cmdopts);
498 rc = singlemount(mtcur);
499 free(mtcur->mnt_opts);
504 /* If we're trying to mount something specific and this isn't it,
505 * skip it. Note we must match both the exact text in fstab (ala
506 * "proc") or a full path from root */
508 if (optind != argc) {
510 // Is this what we're looking for?
512 if(strcmp(argv[optind],mtcur->mnt_fsname) &&
513 strcmp(storage_path,mtcur->mnt_fsname) &&
514 strcmp(argv[optind],mtcur->mnt_dir) &&
515 strcmp(storage_path,mtcur->mnt_dir)) continue;
517 // Remember this entry. Something later may have overmounted
518 // it, and we want the _last_ match.
522 // If we're mounting all.
526 // Do we need to match a filesystem type?
527 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
529 // Skip noauto and swap anyway.
531 if (parse_mount_options(mtcur->mnt_opts,0)
532 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
536 if (singlemount(mtcur)) {
537 // Don't whine about already mounted fs when mounting all.
538 // Note: we should probably change return value to indicate
539 // failure, without causing a duplicate error message.
540 if (errno != EBUSY) bb_perror_msg("Mounting %s on %s failed",
541 mtcur->mnt_fsname, mtcur->mnt_dir);
546 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
550 if (ENABLE_FEATURE_CLEAN_UP) {
556 bb_perror_msg("Mounting %s on %s failed",
557 mtcur->mnt_fsname, mtcur->mnt_dir);