2 * Mini mount implementation for busybox
4 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * 3/21/1999 Charles P. Wright <cpwright@cpwright.com>
21 * searches through fstab when -a is passed
22 * will try mounting stuff with all fses when passed -t auto
24 * 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab.
26 * 1999-10-07 Erik Andersen <andersen@lineo.com>, <andersee@debian.org>.
27 * Rewrite of a lot of code. Removed mtab usage (I plan on
28 * putting it back as a compile-time option some time),
29 * major adjustments to option parsing, and some serious
32 * 1999-11-06 mtab suppport is back - andersee
34 * 2000-01-12 Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
35 * mount to add loop support.
45 #include <sys/mount.h>
49 #if defined BB_FEATURE_MOUNT_LOOP
51 #include <sys/ioctl.h>
52 #include <linux/loop.h>
55 static int use_loop = 0;
58 extern const char mtab_file[]; /* Defined in utility.c */
60 static const char mount_usage[] = "\tmount [flags]\n"
61 "\tmount [flags] device directory [-o options,more-options]\n"
64 "\t-a:\tMount all file systems in fstab.\n"
66 "\t-f:\t\"Fake\" mount. Add entry to mount table but don't mount it.\n"
67 "\t-n:\tDon't write a mount table entry.\n"
69 "\t-o option:\tOne of many filesystem options, listed below.\n"
70 "\t-r:\tMount the filesystem read-only.\n"
71 "\t-t filesystem-type:\tSpecify the filesystem type.\n"
72 "\t-w:\tMount for reading and writing (default).\n"
74 "Options for use with the \"-o\" flag:\n"
75 "\tasync / sync:\tWrites are asynchronous / synchronous.\n"
76 "\tdev / nodev:\tAllow use of special device files / disallow them.\n"
77 "\texec / noexec:\tAllow use of executable files / disallow them.\n"
78 #if defined BB_FEATURE_MOUNT_LOOP
79 "\tloop: Mounts a file via loop device.\n"
81 "\tsuid / nosuid:\tAllow set-user-id-root programs / disallow them.\n"
82 "\tremount: Re-mount a currently-mounted filesystem, changing its flags.\n"
83 "\tro / rw: Mount for read-only / read-write.\n"
85 "There are EVEN MORE flags that are specific to each filesystem.\n"
86 "You'll have to see the written documentation for those.\n";
89 struct mount_options {
95 static const struct mount_options mount_options[] = {
96 {"async", ~MS_SYNCHRONOUS, 0},
98 {"dev", ~MS_NODEV, 0},
99 {"exec", ~MS_NOEXEC, 0},
100 {"nodev", ~0, MS_NODEV},
101 {"noexec", ~0, MS_NOEXEC},
102 {"nosuid", ~0, MS_NOSUID},
103 {"remount", ~0, MS_REMOUNT},
104 {"ro", ~0, MS_RDONLY},
105 {"rw", ~MS_RDONLY, 0},
106 {"suid", ~MS_NOSUID, 0},
107 {"sync", ~0, MS_SYNCHRONOUS},
112 do_mount(char* specialfile, char* dir, char* filesystemtype,
113 long flags, void* string_flags, int useMtab, int fakeIt, char* mtab_opts)
121 #if defined BB_FEATURE_MOUNT_LOOP
123 int loro = flags & MS_RDONLY;
124 char *lofile = specialfile;
125 specialfile = find_unused_loop_device();
126 if (specialfile == NULL) {
127 fprintf(stderr, "Could not find a spare loop device\n");
130 if (set_loop (specialfile, lofile, 0, &loro)) {
131 fprintf(stderr, "Could not setup loop device\n");
134 if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */
135 fprintf(stderr, "WARNING: loop device is read-only\n");
140 status=mount(specialfile, dir, filesystemtype, flags, string_flags);
144 /* If the mount was sucessful, do anything needed, then return TRUE */
149 write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts);
155 /* Bummer. mount failed. Clean up */
156 #if defined BB_FEATURE_MOUNT_LOOP
157 if (specialfile != NULL) {
158 del_loop(specialfile);
167 #define whine_if_fstab_is_missing() {}
169 extern void whine_if_fstab_is_missing()
172 if (stat("/etc/fstab", &statBuf) < 0)
173 fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
178 /* Seperate standard mount options from the nonstandard string options */
180 parse_mount_options ( char *options, unsigned long *flags, char *strflags)
184 char *comma = strchr (options, ',');
185 const struct mount_options* f = mount_options;
189 while (f->name != 0) {
190 if (strcasecmp (f->name, options) == 0) {
199 #if defined BB_FEATURE_MOUNT_LOOP
200 if (gotone==FALSE && !strcasecmp ("loop", options)) { /* loop device support */
205 if (*strflags && strflags!= '\0' && gotone==FALSE) {
207 temp += strlen (strflags);
212 strcat (strflags, options);
223 mount_one(char *blockDevice, char *directory, char *filesystemType,
224 unsigned long flags, char *string_flags, int useMtab, int fakeIt, char *mtab_opts)
230 #if defined BB_FEATURE_USE_PROCFS
231 if (strcmp(filesystemType, "auto") == 0) {
232 FILE *f = fopen ("/proc/filesystems", "r");
237 while (fgets (buf, sizeof (buf), f) != NULL) {
238 filesystemType = buf;
239 if (*filesystemType == '\t') { // Not a nodev filesystem
241 // Add NULL termination to each line
242 while (*filesystemType && *filesystemType != '\n')
244 *filesystemType = '\0';
246 filesystemType = buf;
247 filesystemType++; // hop past tab
249 status = do_mount (blockDevice, directory, filesystemType,
250 flags | MS_MGC_VAL, string_flags, useMtab,
260 status = do_mount (blockDevice, directory, filesystemType,
261 flags | MS_MGC_VAL, string_flags, useMtab,
266 fprintf (stderr, "Mounting %s on %s failed: %s\n",
267 blockDevice, directory, strerror(errno));
273 extern int mount_main (int argc, char **argv)
275 char string_flags_buf[1024]="";
276 char *string_flags = string_flags_buf;
277 char *extra_opts = string_flags_buf;
278 unsigned long flags = 0;
279 char *filesystemType = "auto";
281 char *directory = NULL;
287 /* Only compiled in if BB_MTAB is not defined */
288 whine_if_fstab_is_missing();
291 FILE *mountTable = setmntent (mtab_file, "r");
294 while ((m = getmntent (mountTable)) != 0) {
295 struct fstab* fstabItem;
296 char *blockDevice = m->mnt_fsname;
297 /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */
298 if (strcmp (blockDevice, "/dev/root") == 0) {
299 fstabItem = getfsfile ("/");
300 if (fstabItem != NULL)
301 blockDevice = fstabItem->fs_spec;
303 printf ("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
304 m->mnt_type, m->mnt_opts);
306 endmntent (mountTable);
317 while (i > 0 && **argv) {
320 while (i>0 && *++opt) switch (*opt) {
325 parse_mount_options (*(++argv), &flags, string_flags);
334 filesystemType = *(++argv);
358 else if (directory == NULL)
370 FILE *f = setmntent ("/etc/fstab", "r");
373 perror("/etc/fstab");
376 while ((m = getmntent (f)) != NULL) {
377 // If the file system isn't noauto, and isn't mounted on /,
378 // and isn't swap or nfs, then mount it
379 if ((!strstr (m->mnt_opts, "noauto")) &&
380 (m->mnt_dir[1] != '\0') &&
381 (!strstr (m->mnt_type, "swap")) &&
382 (!strstr (m->mnt_type, "nfs")))
385 *string_flags = '\0';
386 parse_mount_options(m->mnt_opts, &flags, string_flags);
387 mount_one (m->mnt_fsname, m->mnt_dir, m->mnt_type,
388 flags, string_flags, useMtab, fakeIt, extra_opts);
393 if (device && directory) {
395 if (strcmp(filesystemType, "nfs") == 0) {
396 if (nfsmount(device, directory, &flags, &extra_opts, &string_flags, 1) != 0)
400 exit (mount_one (device, directory, filesystemType,
401 flags, string_flags, useMtab, fakeIt, extra_opts));