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 by Rob Landley <rob@landley.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 /* This is just a warning of a common mistake. Possibly this should be a
39 * uclibc faq entry rather than in busybox... */
40 #if ENABLE_NFSMOUNT && defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
41 #error "You need to build uClibc with UCLIBC_HAS_RPC for busybox mount with NFS support to compile."
44 // These two aren't always defined in old headers
52 /* Consume standard mount options (from -o options or --options).
53 * Set appropriate flags and collect unrecognized ones as a comma separated
54 * string to pass to kernel */
59 } static const mount_options[] = {
65 {"nosuid", MS_NOSUID},
70 {"noexec", MS_NOEXEC},
71 {"sync", MS_SYNCHRONOUS},
72 {"async", ~MS_SYNCHRONOUS},
73 {"remount", MS_REMOUNT},
74 {"atime", MS_NOATIME},
75 {"noatime", MS_NOATIME},
76 {"diratime", MS_NODIRATIME},
77 {"nodiratime", MS_NODIRATIME},
82 /* Uses the mount_options list above */
83 static void parse_mount_options(char *options, int *flags, char **strflags)
85 // Loop through options
88 char *comma = strchr(options, ',');
92 // Find this option in mount_options
93 for(i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
94 if(!strcasecmp(mount_options[i].name, options)) {
95 long fl = mount_options[i].flags;
96 if(fl < 0) *flags &= fl;
101 // Unrecognized mount option?
102 if(i == (sizeof(mount_options) / sizeof(*mount_options))) {
103 // Add it to strflags, to pass on to kernel
104 i = *strflags ? strlen(*strflags) : 0;
105 *strflags = xrealloc(*strflags, i+strlen(options)+2);
106 // Comma separated if it's not the first one
107 if(i) (*strflags)[i++] = ',';
108 strcpy((*strflags)+i, options);
110 // Advance to next option, or finish
118 /* This does the work */
120 extern int mount_main(int argc, char **argv)
122 char *string_flags = 0, *fsType = 0, *blockDevice = 0, *directory = 0,
123 *loopFile = 0, *buf = 0,
124 *files[] = {"/etc/filesystems", "/proc/filesystems", 0};
125 int i, opt, all = FALSE, fakeIt = FALSE, allowWrite = FALSE,
126 rc = 1, useMtab = ENABLE_FEATURE_MTAB_SUPPORT;
127 int flags=0xc0ed0000; // Needed for linux 2.2, ignored by 2.4 and 2.6.
128 FILE *file = 0,*f = 0;
129 char path[PATH_MAX*2];
133 /* parse long options, like --bind and --move. Note that -o option
134 * and --option are synonymous. Yes, this means --remount,rw works. */
136 for(i = opt = 0; i < argc; i++) {
137 if(argv[i][0] == '-' && argv[i][1] == '-')
138 parse_mount_options(argv[i]+2, &flags, &string_flags);
139 else argv[opt++] = argv[i];
143 // Parse remaining options
145 while((opt = getopt(argc, argv, "o:t:rwafnv")) > 0) {
148 parse_mount_options(optarg, &flags, &string_flags);
175 // If we have no arguments, show currently mounted filesystems
177 if(!all && (optind == argc)) {
178 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
180 if(!mountTable) bb_perror_msg_and_die(bb_path_mtab_file);
182 while (getmntent_r(mountTable,&m,path,sizeof(path))) {
183 blockDevice = m.mnt_fsname;
185 // Clean up display a little bit regarding root devie
186 if(!strcmp(blockDevice, "rootfs")) continue;
187 if(!strcmp(blockDevice, "/dev/root"))
188 blockDevice = find_block_device("/");
190 if(!fsType || !strcmp(m.mnt_type, fsType))
191 printf("%s on %s type %s (%s)\n", blockDevice, m.mnt_dir,
192 m.mnt_type, m.mnt_opts);
193 if(ENABLE_FEATURE_CLEAN_UP && blockDevice != m.mnt_fsname)
196 endmntent(mountTable);
200 /* The next argument is what to mount. if there's an argument after that
201 * it's where to mount it. If we're not mounting all, and we have both
202 * of these arguments, jump straight to the actual mount. */
206 blockDevice = !stat(argv[optind], &statbuf) ?
207 bb_simplify_path(argv[optind]) :
208 (ENABLE_FEATURE_CLEAN_UP ? strdup(argv[optind]) : argv[optind]);
209 if(optind+1 < argc) directory = bb_simplify_path(argv[optind+1]);
211 // If we don't have to loop through fstab, skip ahead a bit.
213 if(!all && optind+1!=argc) goto singlemount;
215 // Loop through /etc/fstab entries to look up this entry.
217 if(!(file=setmntent("/etc/fstab","r")))
218 bb_perror_msg_and_die("\nCannot read /etc/fstab");
221 // Get next fstab entry
223 if(!getmntent_r(file,&m,path,sizeof(path))) {
225 bb_perror_msg("Can't find %s in /etc/fstab\n", blockDevice);
229 // If we're mounting all and all doesn't mount this one, skip it.
232 if(strstr(m.mnt_opts,"noauto") || strstr(m.mnt_type,"swap"))
236 /* If we're mounting something specific and this isn't it, skip it.
237 * Note we must match both the exact text in fstab (ala "proc") or
238 * a full path from root */
240 } else if(strcmp(blockDevice,m.mnt_fsname) &&
241 strcmp(argv[optind],m.mnt_fsname) &&
242 strcmp(blockDevice,m.mnt_dir) &&
243 strcmp(argv[optind],m.mnt_dir)) continue;
245 /* Parse flags from /etc/fstab (unless this is a single mount
246 * overriding fstab -- note the "all" test above zeroed the flags,
247 * to prevent flags from previous entries affecting this one, so
248 * the only way we could get here with nonzero flags is a single
252 if(ENABLE_FEATURE_CLEAN_UP) free(string_flags);
254 parse_mount_options(m.mnt_opts, &flags, &string_flags);
257 /* Fill out remaining fields with info from mtab */
259 if(ENABLE_FEATURE_CLEAN_UP) {
261 blockDevice=strdup(m.mnt_fsname);
263 directory=strdup(m.mnt_type);
265 blockDevice=m.mnt_fsname;
270 /* Ok, we're ready to actually mount a specific source on a specific
275 // If they said -w, override fstab
277 if(allowWrite) flags&=~MS_RDONLY;
279 // Might this be an NFS filesystem?
281 if(ENABLE_NFSMOUNT && (!fsType || !strcmp(fsType,"nfs")) &&
282 strchr(blockDevice, ':') != NULL)
284 if(nfsmount(blockDevice, directory, &flags, &string_flags, 1))
285 bb_perror_msg("nfsmount failed");
289 // Strangely enough, nfsmount() doesn't actually mount()
294 // Do we need to allocate a loopback device?
296 if(ENABLE_FEATURE_MOUNT_LOOP && !fakeIt && S_ISREG(statbuf.st_mode))
298 loopFile = blockDevice;
300 switch(set_loop(&blockDevice, loopFile, 0)) {
305 bb_error_msg_and_die(
306 errno == EPERM || errno == EACCES ?
307 bb_msg_perm_denied_are_you_root :
308 "Couldn't setup loop device");
313 /* If we know the fstype (or don't need to), jump straight
314 * to the actual mount. */
316 if(fsType || (flags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
320 // Loop through filesystem types until mount succeeds or we run out
322 for(i = 0; files[i] && rc; i++) {
323 f = fopen(files[i], "r");
325 // Get next block device backed filesystem
326 for(buf = 0; (buf = fsType = bb_get_chomped_line_from_file(f));
329 // Skip funky entries in /proc
330 if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
332 while(isspace(*fsType)) fsType++;
333 if(*buf=='#' || *buf=='*') continue;
334 if(!*fsType) continue;
336 // Okay, try to mount
340 rc = mount(blockDevice, directory, fsType, flags, string_flags);
341 if(!rc || (flags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
343 bb_error_msg("%s is write-protected, mounting read-only", blockDevice);
353 /* If the mount was sucessful, and we're maintaining an old-style
354 * mtab file by hand, add new entry to it now. */
355 if((!rc || fakeIt) && useMtab) {
356 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
358 if(!mountTable) bb_perror_msg(bb_path_mtab_file);
360 // Remove trailing / (if any) from directory we mounted on
361 int length=strlen(directory);
362 if(length>1 && directory[length-1] == '/')
363 directory[length-1]=0;
365 // Fill out structure (should be ok to re-use existing one).
366 m.mnt_fsname=blockDevice;
368 m.mnt_type=fsType ? : "--bind";
369 m.mnt_opts=string_flags ? :
370 ((flags & MS_RDONLY) ? "ro" : "rw");
375 addmntent(mountTable, &m);
376 endmntent(mountTable);
379 // Mount failed. Clean up
381 del_loop(blockDevice);
382 if(ENABLE_FEATURE_CLEAN_UP) free(loopFile);
384 // Don't whine about already mounted fs when mounting all.
385 if(rc<0 && errno == EBUSY && all) rc = 0;
386 else if (errno == EPERM)
387 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
389 // We couldn't free this earlier becase fsType could be in buf.
390 if(ENABLE_FEATURE_CLEAN_UP) {
398 if(file) endmntent(file);
399 if(rc) bb_perror_msg("Mounting %s on %s failed", blockDevice, directory);