blob: 8b5efe14f93e4f45b1c95553ad17610410fa86e2 [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
Eric Andersen596e5461999-10-07 08:30:23 +00002 * Mini mount implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersen596e5461999-10-07 08:30:23 +00005 *
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.
10 *
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.
15 *
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
19 *
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
23 *
24 * 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab.
Eric Andersenc4996011999-10-20 22:08:37 +000025 *
26 * 1999-10-07 Erik Andersen <andersen@lineo.com>, <andersee@debian.org>.
27 * Rewrote 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
30 * dieting all around.
Eric Andersencc8ed391999-10-05 16:24:54 +000031*/
32
33#include "internal.h"
34#include <stdlib.h>
35#include <unistd.h>
36#include <errno.h>
37#include <string.h>
38#include <stdio.h>
39#include <mntent.h>
40#include <sys/mount.h>
41#include <ctype.h>
Eric Andersen596e5461999-10-07 08:30:23 +000042#include <fstab.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersend0246fb1999-11-04 21:18:07 +000044extern const char mtab_file[]; /* Defined in utility.c */
45
Eric Andersene77ae3a1999-10-19 20:03:34 +000046static const char mount_usage[] = "Usage:\tmount [flags]\n"
Eric Andersen596e5461999-10-07 08:30:23 +000047 "\tmount [flags] device directory [-o options,more-options]\n"
48 "\n"
49 "Flags:\n"
50 "\t-a:\tMount all file systems in fstab.\n"
Eric Andersend0246fb1999-11-04 21:18:07 +000051#ifdef BB_MTAB
52 "\t-f:\t\"Fake\" mount. Add entry to mount table but don't mount it.\n"
53 "\t-n:\tDon't write a mount table entry.\n"
54#endif
Eric Andersen596e5461999-10-07 08:30:23 +000055 "\t-o option:\tOne of many filesystem options, listed below.\n"
56 "\t-r:\tMount the filesystem read-only.\n"
57 "\t-t filesystem-type:\tSpecify the filesystem type.\n"
58 "\t-w:\tMount for reading and writing (default).\n"
59 "\n"
60 "Options for use with the \"-o\" flag:\n"
61 "\tasync / sync:\tWrites are asynchronous / synchronous.\n"
62 "\tdev / nodev:\tAllow use of special device files / disallow them.\n"
63 "\texec / noexec:\tAllow use of executable files / disallow them.\n"
64 "\tsuid / nosuid:\tAllow set-user-id-root programs / disallow them.\n"
65 "\tremount: Re-mount a currently-mounted filesystem, changing its flags.\n"
66 "\tro / rw: Mount for read-only / read-write.\n"
67 "\t"
68 "There are EVEN MORE flags that are specific to each filesystem.\n"
69 "You'll have to see the written documentation for those.\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000070
Eric Andersend0246fb1999-11-04 21:18:07 +000071
Eric Andersencc8ed391999-10-05 16:24:54 +000072struct mount_options {
Eric Andersen596e5461999-10-07 08:30:23 +000073 const char *name;
74 unsigned long and;
75 unsigned long or;
Eric Andersencc8ed391999-10-05 16:24:54 +000076};
77
Eric Andersen596e5461999-10-07 08:30:23 +000078static const struct mount_options mount_options[] = {
79 {"async", ~MS_SYNCHRONOUS, 0},
80 {"defaults", ~0, 0},
81 {"dev", ~MS_NODEV, 0},
82 {"exec", ~MS_NOEXEC, 0},
83 {"nodev", ~0, MS_NODEV},
84 {"noexec", ~0, MS_NOEXEC},
85 {"nosuid", ~0, MS_NOSUID},
86 {"remount", ~0, MS_REMOUNT},
87 {"ro", ~0, MS_RDONLY},
88 {"rw", ~MS_RDONLY, 0},
89 {"suid", ~MS_NOSUID, 0},
90 {"sync", ~0, MS_SYNCHRONOUS},
91 {0, 0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +000092};
93
Eric Andersend0246fb1999-11-04 21:18:07 +000094#if ! defined BB_MTAB
95#define do_mount(specialfile, dir, filesystemtype, flags, string_flags, useMtab, fakeIt) \
96 mount(specialfile, dir, filesystemtype, flags, string_flags)
97#else
98static int
99do_mount(char* specialfile, char* dir, char* filesystemtype,
100 long flags, void* string_flags, int useMtab, int fakeIt)
101{
102 int status=0;
103
104 if (fakeIt==FALSE)
105 status=mount(specialfile, dir, filesystemtype, flags, string_flags);
106
107 if ( status == 0 ) {
108 if ( useMtab==TRUE )
109 write_mtab(specialfile, dir, filesystemtype, flags, string_flags);
110 return 0;
111 }
112 else
113 return( status);
114}
115#endif
116
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Eric Andersen8341a151999-10-08 17:14:14 +0000118/* Seperate standard mount options from the nonstandard string options */
Eric Andersencc8ed391999-10-05 16:24:54 +0000119static void
Eric Andersen8341a151999-10-08 17:14:14 +0000120parse_mount_options ( char *options, unsigned long *flags, char *strflags)
Eric Andersencc8ed391999-10-05 16:24:54 +0000121{
Eric Andersen8341a151999-10-08 17:14:14 +0000122 while (options) {
123 int gotone=FALSE;
Eric Andersen596e5461999-10-07 08:30:23 +0000124 char *comma = strchr (options, ',');
125 const struct mount_options* f = mount_options;
126 if (comma)
127 *comma = '\0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000128
Eric Andersen596e5461999-10-07 08:30:23 +0000129 while (f->name != 0) {
Eric Andersen596e5461999-10-07 08:30:23 +0000130 if (strcasecmp (f->name, options) == 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000131
Eric Andersen596e5461999-10-07 08:30:23 +0000132 *flags &= f->and;
133 *flags |= f->or;
Eric Andersen8341a151999-10-08 17:14:14 +0000134 gotone=TRUE;
135 break;
Eric Andersen596e5461999-10-07 08:30:23 +0000136 }
137 f++;
Eric Andersencc8ed391999-10-05 16:24:54 +0000138 }
Eric Andersen8341a151999-10-08 17:14:14 +0000139 if (*strflags && strflags!= '\0' && gotone==FALSE) {
140 char *temp=strflags;
141 temp += strlen (strflags);
142 *temp++ = ',';
143 *temp++ = '\0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000144 }
Eric Andersen8341a151999-10-08 17:14:14 +0000145 if (gotone==FALSE) {
146 strcat (strflags, options);
147 gotone=FALSE;
148 }
Eric Andersen596e5461999-10-07 08:30:23 +0000149 if (comma) {
150 *comma = ',';
151 options = ++comma;
Eric Andersen8341a151999-10-08 17:14:14 +0000152 } else {
Eric Andersen596e5461999-10-07 08:30:23 +0000153 break;
Eric Andersen8341a151999-10-08 17:14:14 +0000154 }
Eric Andersen596e5461999-10-07 08:30:23 +0000155 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000156}
157
158int
Eric Andersend0246fb1999-11-04 21:18:07 +0000159mount_one(char *blockDevice, char *directory, char *filesystemType,
160 unsigned long flags, char *string_flags, int useMtab, int fakeIt)
Eric Andersencc8ed391999-10-05 16:24:54 +0000161{
Eric Andersen596e5461999-10-07 08:30:23 +0000162 int status = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000163
Eric Andersen596e5461999-10-07 08:30:23 +0000164 char buf[255];
Eric Andersencc8ed391999-10-05 16:24:54 +0000165
Eric Andersen596e5461999-10-07 08:30:23 +0000166 if (strcmp(filesystemType, "auto") == 0) {
167 FILE *f = fopen ("/proc/filesystems", "r");
Eric Andersencc8ed391999-10-05 16:24:54 +0000168
Eric Andersen596e5461999-10-07 08:30:23 +0000169 if (f == NULL)
170 return( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000171
Eric Andersen596e5461999-10-07 08:30:23 +0000172 while (fgets (buf, sizeof (buf), f) != NULL) {
173 filesystemType = buf;
174 if (*filesystemType == '\t') { // Not a nodev filesystem
Eric Andersencc8ed391999-10-05 16:24:54 +0000175
Eric Andersen596e5461999-10-07 08:30:23 +0000176 // Add NULL termination to each line
177 while (*filesystemType && *filesystemType != '\n')
178 filesystemType++;
179 *filesystemType = '\0';
Eric Andersencc8ed391999-10-05 16:24:54 +0000180
Eric Andersen596e5461999-10-07 08:30:23 +0000181 filesystemType = buf;
182 filesystemType++; // hop past tab
183
Eric Andersend0246fb1999-11-04 21:18:07 +0000184 status = do_mount (blockDevice, directory, filesystemType,
185 flags | MS_MGC_VAL, string_flags, useMtab, fakeIt);
Eric Andersen596e5461999-10-07 08:30:23 +0000186 if (status == 0)
187 break;
188 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000189 }
Eric Andersen596e5461999-10-07 08:30:23 +0000190 fclose (f);
191 } else {
Eric Andersend0246fb1999-11-04 21:18:07 +0000192 status = do_mount (blockDevice, directory, filesystemType,
193 flags | MS_MGC_VAL, string_flags, useMtab, fakeIt);
Eric Andersen596e5461999-10-07 08:30:23 +0000194 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000195
Eric Andersen596e5461999-10-07 08:30:23 +0000196 if (status) {
197 fprintf (stderr, "Mounting %s on %s failed: %s\n",
198 blockDevice, directory, strerror(errno));
199 return (FALSE);
200 }
201 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000202}
203
Eric Andersen596e5461999-10-07 08:30:23 +0000204extern int mount_main (int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000205{
Eric Andersen8341a151999-10-08 17:14:14 +0000206 char string_flags[1024]="";
Eric Andersen596e5461999-10-07 08:30:23 +0000207 unsigned long flags = 0;
208 char *filesystemType = "auto";
Eric Andersen8341a151999-10-08 17:14:14 +0000209 char *device = NULL;
210 char *directory = NULL;
Eric Andersencb6e2561999-10-16 15:48:40 +0000211 struct stat statBuf;
Eric Andersend0246fb1999-11-04 21:18:07 +0000212 int all = FALSE;
213 int fakeIt = FALSE;
214 int useMtab = TRUE;
Eric Andersen8341a151999-10-08 17:14:14 +0000215 int i;
Eric Andersencc8ed391999-10-05 16:24:54 +0000216
Eric Andersencb6e2561999-10-16 15:48:40 +0000217 if (stat("/etc/fstab", &statBuf) < 0)
218 fprintf(stderr, "/etc/fstab file missing -- Please install one.\n\n");
219
Eric Andersen596e5461999-10-07 08:30:23 +0000220 if (argc == 1) {
Eric Andersend0246fb1999-11-04 21:18:07 +0000221 FILE *mountTable = setmntent (mtab_file, "r");
222 if (mountTable) {
Eric Andersen596e5461999-10-07 08:30:23 +0000223 struct mntent *m;
224 while ((m = getmntent (mountTable)) != 0) {
Eric Andersencb6e2561999-10-16 15:48:40 +0000225 struct fstab* fstabItem;
Eric Andersen596e5461999-10-07 08:30:23 +0000226 char *blockDevice = m->mnt_fsname;
Eric Andersencb6e2561999-10-16 15:48:40 +0000227 /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */
228 if (strcmp (blockDevice, "/dev/root") == 0) {
229 fstabItem = getfsfile ("/");
230 if (fstabItem != NULL)
231 blockDevice = fstabItem->fs_spec;
232 }
Eric Andersen596e5461999-10-07 08:30:23 +0000233 printf ("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
234 m->mnt_type, m->mnt_opts);
235 }
236 endmntent (mountTable);
Eric Andersend0246fb1999-11-04 21:18:07 +0000237 } else {
238 perror(mtab_file);
Eric Andersencc8ed391999-10-05 16:24:54 +0000239 }
Eric Andersen3c163821999-10-14 22:16:57 +0000240 exit( TRUE);
Eric Andersen596e5461999-10-07 08:30:23 +0000241 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000242
Eric Andersen596e5461999-10-07 08:30:23 +0000243
244 /* Parse options */
Eric Andersen8341a151999-10-08 17:14:14 +0000245 i = --argc;
246 argv++;
247 while (i > 0 && **argv) {
Eric Andersen596e5461999-10-07 08:30:23 +0000248 if (**argv == '-') {
Eric Andersen8341a151999-10-08 17:14:14 +0000249 while (i>0 && *++(*argv)) switch (**argv) {
Eric Andersen596e5461999-10-07 08:30:23 +0000250 case 'o':
Eric Andersen8341a151999-10-08 17:14:14 +0000251 if (--i == 0) {
Eric Andersen596e5461999-10-07 08:30:23 +0000252 fprintf (stderr, "%s\n", mount_usage);
Eric Andersen3c163821999-10-14 22:16:57 +0000253 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000254 }
Eric Andersen8341a151999-10-08 17:14:14 +0000255 parse_mount_options (*(++argv), &flags, string_flags);
256 --i;
257 ++argv;
Eric Andersen596e5461999-10-07 08:30:23 +0000258 break;
259 case 'r':
260 flags |= MS_RDONLY;
261 break;
262 case 't':
Eric Andersen8341a151999-10-08 17:14:14 +0000263 if (--i == 0) {
Eric Andersen596e5461999-10-07 08:30:23 +0000264 fprintf (stderr, "%s\n", mount_usage);
Eric Andersen3c163821999-10-14 22:16:57 +0000265 exit( FALSE);
Eric Andersen596e5461999-10-07 08:30:23 +0000266 }
Eric Andersen8341a151999-10-08 17:14:14 +0000267 filesystemType = *(++argv);
268 --i;
269 ++argv;
Eric Andersen596e5461999-10-07 08:30:23 +0000270 break;
271 case 'w':
272 flags &= ~MS_RDONLY;
273 break;
274 case 'a':
Eric Andersen3ae0c781999-11-04 01:13:21 +0000275 all = TRUE;
Eric Andersen596e5461999-10-07 08:30:23 +0000276 break;
Eric Andersend0246fb1999-11-04 21:18:07 +0000277#ifdef BB_MTAB
278 case 'f':
279 fakeIt = TRUE;
280 break;
281 case 'n':
282 useMtab = FALSE;
283 break;
284#endif
Eric Andersen596e5461999-10-07 08:30:23 +0000285 case 'v':
286 case 'h':
287 case '-':
288 fprintf (stderr, "%s\n", mount_usage);
Eric Andersen3c163821999-10-14 22:16:57 +0000289 exit( TRUE);
Eric Andersen8341a151999-10-08 17:14:14 +0000290 break;
291 }
292 } else {
293 if (device == NULL)
294 device=*argv;
295 else if (directory == NULL)
296 directory=*argv;
297 else {
298 fprintf (stderr, "%s\n", mount_usage);
Eric Andersen3c163821999-10-14 22:16:57 +0000299 exit( TRUE);
Eric Andersen596e5461999-10-07 08:30:23 +0000300 }
301 }
302 i--;
303 argv++;
304 }
305
Eric Andersen3ae0c781999-11-04 01:13:21 +0000306 if (all == TRUE) {
Eric Andersen596e5461999-10-07 08:30:23 +0000307 struct mntent *m;
308 FILE *f = setmntent ("/etc/fstab", "r");
309
310 if (f == NULL) {
311 perror("/etc/fstab");
Eric Andersen3c163821999-10-14 22:16:57 +0000312 exit( FALSE);
Eric Andersen596e5461999-10-07 08:30:23 +0000313 }
Eric Andersen596e5461999-10-07 08:30:23 +0000314 while ((m = getmntent (f)) != NULL) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000315 // If the file system isn't noauto, and isn't mounted on /,
316 // and isn't swap or nfs, then mount it
317 if ((!strstr (m->mnt_opts, "noauto")) &&
318 (m->mnt_dir[1] != '\0') &&
319 (!strstr (m->mnt_type, "swap")) &&
320 (!strstr (m->mnt_type, "nfs")))
321 {
Eric Andersend0246fb1999-11-04 21:18:07 +0000322 flags = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000323 *string_flags = '\0';
Eric Andersend0246fb1999-11-04 21:18:07 +0000324 parse_mount_options(m->mnt_opts, &flags, string_flags);
325 mount_one (m->mnt_fsname, m->mnt_dir, m->mnt_type,
326 flags, string_flags, useMtab, fakeIt);
Eric Andersen596e5461999-10-07 08:30:23 +0000327 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000328 }
Eric Andersen596e5461999-10-07 08:30:23 +0000329 endmntent (f);
330 } else {
Eric Andersen8341a151999-10-08 17:14:14 +0000331 if (device && directory) {
Eric Andersen3c163821999-10-14 22:16:57 +0000332 exit (mount_one (device, directory, filesystemType,
Eric Andersend0246fb1999-11-04 21:18:07 +0000333 flags, string_flags, useMtab, fakeIt));
Eric Andersencc8ed391999-10-05 16:24:54 +0000334 } else {
Eric Andersen596e5461999-10-07 08:30:23 +0000335 fprintf (stderr, "%s\n", mount_usage);
Eric Andersen3c163821999-10-14 22:16:57 +0000336 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000337 }
Eric Andersen596e5461999-10-07 08:30:23 +0000338 }
Eric Andersen3c163821999-10-14 22:16:57 +0000339 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000340}