blob: 49d086c97a60acea440a858850f750ede18ac066 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersen596e5461999-10-07 08:30:23 +00003 * Mini mount implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersen1d1d2f92002-04-13 08:31:59 +00006 * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org>
Eric Andersen596e5461999-10-07 08:30:23 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * 3/21/1999 Charles P. Wright <cpwright@cpwright.com>
23 * searches through fstab when -a is passed
24 * will try mounting stuff with all fses when passed -t auto
25 *
26 * 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab.
Eric Andersenc4996011999-10-20 22:08:37 +000027 *
Eric Andersen3ece93b2002-06-22 17:23:45 +000028 * 1999-10-07 Erik Andersen <andersee@debian.org>.
Erik Andersen31638212000-01-15 22:28:50 +000029 * Rewrite of a lot of code. Removed mtab usage (I plan on
Eric Andersenc4996011999-10-20 22:08:37 +000030 * putting it back as a compile-time option some time),
31 * major adjustments to option parsing, and some serious
32 * dieting all around.
Erik Andersenb7cc49d2000-01-13 06:38:14 +000033 *
Erik Andersen31638212000-01-15 22:28:50 +000034 * 1999-11-06 mtab suppport is back - andersee
35 *
Erik Andersenb7cc49d2000-01-13 06:38:14 +000036 * 2000-01-12 Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
37 * mount to add loop support.
Eric Andersenfdd51032000-08-02 18:48:26 +000038 *
39 * 2000-04-30 Dave Cinege <dcinege@psychosis.com>
40 * Rewrote fstab while loop and lower mount section. Can now do
41 * single mounts from fstab. Can override fstab options for single
42 * mount. Common mount_one call for single mounts and 'all'. Fixed
43 * mtab updating and stale entries. Removed 'remount' default.
44 *
Erik Andersenb7cc49d2000-01-13 06:38:14 +000045 */
Eric Andersencc8ed391999-10-05 16:24:54 +000046
Matt Kraai34251112001-05-02 21:17:38 +000047#include <limits.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000048#include <stdlib.h>
49#include <unistd.h>
50#include <errno.h>
51#include <string.h>
52#include <stdio.h>
53#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000054#include <ctype.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000055#include "busybox.h"
Eric Andersenbd22ed82000-07-08 18:55:24 +000056
Mark Whitley59ab0252001-01-23 22:30:04 +000057enum {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000058 MS_MGC_VAL = 0xc0ed0000, /* Magic number indicatng "new" flags */
59 MS_RDONLY = 1, /* Mount read-only */
60 MS_NOSUID = 2, /* Ignore suid and sgid bits */
61 MS_NODEV = 4, /* Disallow access to device special files */
62 MS_NOEXEC = 8, /* Disallow program execution */
63 MS_SYNCHRONOUS = 16, /* Writes are synced at once */
64 MS_REMOUNT = 32, /* Alter flags of a mounted FS */
65 MS_MANDLOCK = 64, /* Allow mandatory locks on an FS */
66 S_QUOTA = 128, /* Quota initialized for file/directory/symlink */
67 S_APPEND = 256, /* Append-only file */
68 S_IMMUTABLE = 512, /* Immutable file */
69 MS_NOATIME = 1024, /* Do not update access times. */
70 MS_NODIRATIME = 2048, /* Do not update directory access times */
71 MS_BIND = 4096, /* Use the new linux 2.4.x "mount --bind" feature */
Mark Whitley59ab0252001-01-23 22:30:04 +000072};
Eric Andersencc8ed391999-10-05 16:24:54 +000073
Eric Andersenbd22ed82000-07-08 18:55:24 +000074
Eric Andersenbdfd0d72001-10-24 05:00:29 +000075#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersenb7cc49d2000-01-13 06:38:14 +000076#include <fcntl.h>
77#include <sys/ioctl.h>
Erik Andersene132f4b2000-02-09 04:16:43 +000078static int use_loop = FALSE;
Erik Andersenb7cc49d2000-01-13 06:38:14 +000079#endif
80
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000081extern int mount(__const char *__special_file, __const char *__dir,
82 __const char *__fstype, unsigned long int __rwflag,
83 __const void *__data);
84extern int umount(__const char *__special_file);
85extern int umount2(__const char *__special_file, int __flags);
Eric Andersende440672001-03-01 07:55:49 +000086
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000087extern int sysfs(int option, unsigned int fs_index, char *buf);
Eric Andersena57ba4d2000-07-08 19:20:49 +000088
Eric Andersencc8ed391999-10-05 16:24:54 +000089struct mount_options {
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 const char *name;
91 unsigned long and;
92 unsigned long or;
Eric Andersencc8ed391999-10-05 16:24:54 +000093};
94
Eric Andersen596e5461999-10-07 08:30:23 +000095static const struct mount_options mount_options[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 {"async", ~MS_SYNCHRONOUS, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +000097 {"atime", ~0, ~MS_NOATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 {"defaults", ~0, 0},
Robert Grieblaa385d42002-05-14 22:56:29 +000099 {"noauto", ~0, 0},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 {"dev", ~MS_NODEV, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000101 {"diratime", ~0, ~MS_NODIRATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 {"exec", ~MS_NOEXEC, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000103 {"noatime", ~0, MS_NOATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 {"nodev", ~0, MS_NODEV},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000105 {"nodiratime", ~0, MS_NODIRATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 {"noexec", ~0, MS_NOEXEC},
107 {"nosuid", ~0, MS_NOSUID},
108 {"remount", ~0, MS_REMOUNT},
109 {"ro", ~0, MS_RDONLY},
110 {"rw", ~MS_RDONLY, 0},
111 {"suid", ~MS_NOSUID, 0},
112 {"sync", ~0, MS_SYNCHRONOUS},
Eric Andersen2f6e1f82001-05-21 15:59:34 +0000113 {"bind", ~0, MS_BIND},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 {0, 0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000115};
116
Eric Andersend0246fb1999-11-04 21:18:07 +0000117static int
Glenn L McGrath8042f652002-08-23 06:17:46 +0000118do_mount(char *specialfile, char *dir, char *filesystemtype, long flags,
119 void *string_flags, int useMtab, int fakeIt, char *mtab_opts,
120 int mount_all)
Eric Andersend0246fb1999-11-04 21:18:07 +0000121{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 int status = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000123
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000124#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersene132f4b2000-02-09 04:16:43 +0000125 char *lofile = NULL;
Eric Andersen8847b9a2000-09-21 01:33:05 +0000126#endif
Eric Andersend0246fb1999-11-04 21:18:07 +0000127
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000128 if (!fakeIt) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000129#if defined CONFIG_FEATURE_MOUNT_LOOP
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000130 if (use_loop == TRUE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 int loro = flags & MS_RDONLY;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000132
Mark Whitleye677dfe2001-02-26 17:45:58 +0000133 lofile = specialfile;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134
135 specialfile = find_unused_loop_device();
136 if (specialfile == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 bb_error_msg_and_die("Could not find a spare loop device");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 }
139 if (set_loop(specialfile, lofile, 0, &loro)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 bb_error_msg_and_die("Could not setup loop device");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 }
142 if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_error_msg("WARNING: loop device is read-only");
Matt Kraai94f3a572001-07-05 14:46:07 +0000144 flags |= MS_RDONLY;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 }
146 }
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000147#endif
Eric Andersena42982e2000-06-07 17:28:53 +0000148 status = mount(specialfile, dir, filesystemtype, flags, string_flags);
Matt Kraai9344f752001-06-03 02:21:38 +0000149 if (status < 0 && errno == EROFS) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150 bb_error_msg("%s is write-protected, mounting read-only",
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000151 specialfile);
152 status = mount(specialfile, dir, filesystemtype, flags |=
153 MS_RDONLY, string_flags);
Eric Andersen0cccdfa2000-09-20 06:23:36 +0000154 }
Matt Kraai9344f752001-06-03 02:21:38 +0000155 /* Don't whine about already mounted filesystems when mounting all. */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000156 if (status < 0 && errno == EBUSY && mount_all) {
Matt Kraai9344f752001-06-03 02:21:38 +0000157 return TRUE;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000158 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 }
Erik Andersen5cbdd712000-01-26 20:06:48 +0000160
161
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 /* If the mount was sucessful, do anything needed, then return TRUE */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000163 if (status == 0 || fakeIt == TRUE) {
Erik Andersen5cbdd712000-01-26 20:06:48 +0000164
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000165#if defined CONFIG_FEATURE_MTAB_SUPPORT
Matt Kraai1f0c4362001-12-20 23:13:26 +0000166 if (useMtab) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000167 erase_mtab(specialfile); /* Clean any stale entries */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts);
169 }
170#endif
171 return (TRUE);
172 }
173
174 /* Bummer. mount failed. Clean up */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000175#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersene132f4b2000-02-09 04:16:43 +0000176 if (lofile != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 del_loop(specialfile);
Erik Andersen5cbdd712000-01-26 20:06:48 +0000178 }
Eric Andersend0246fb1999-11-04 21:18:07 +0000179#endif
Eric Andersena42982e2000-06-07 17:28:53 +0000180
181 if (errno == EPERM) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Eric Andersena42982e2000-06-07 17:28:53 +0000183 }
184
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 return (FALSE);
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000186}
187
Eric Andersend0246fb1999-11-04 21:18:07 +0000188
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000189static void paste_str(char **s1, const char *s2)
190{
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000191 *s1 = xrealloc(*s1, strlen(*s1) + strlen(s2) + 1);
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000192 strcat(*s1, s2);
193}
Eric Andersencc8ed391999-10-05 16:24:54 +0000194
Eric Andersen8341a151999-10-08 17:14:14 +0000195/* Seperate standard mount options from the nonstandard string options */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000196static void parse_mount_options(char *options, int *flags, char **strflags)
Eric Andersencc8ed391999-10-05 16:24:54 +0000197{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 while (options) {
199 int gotone = FALSE;
200 char *comma = strchr(options, ',');
201 const struct mount_options *f = mount_options;
Eric Andersencc8ed391999-10-05 16:24:54 +0000202
Glenn L McGrath8042f652002-08-23 06:17:46 +0000203 if (comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 *comma = '\0';
Glenn L McGrath8042f652002-08-23 06:17:46 +0000205 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000206
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 while (f->name != 0) {
208 if (strcasecmp(f->name, options) == 0) {
209
210 *flags &= f->and;
211 *flags |= f->or;
212 gotone = TRUE;
213 break;
214 }
215 f++;
216 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000217#if defined CONFIG_FEATURE_MOUNT_LOOP
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000218 if (!strcasecmp("loop", options)) { /* loop device support */
Erik Andersene132f4b2000-02-09 04:16:43 +0000219 use_loop = TRUE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 gotone = TRUE;
221 }
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000222#endif
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000223 if (!gotone) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000224 if (**strflags) {
225 /* have previous parsed options */
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000226 paste_str(strflags, ",");
Glenn L McGrath8042f652002-08-23 06:17:46 +0000227 }
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000228 paste_str(strflags, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 if (comma) {
231 *comma = ',';
232 options = ++comma;
233 } else {
234 break;
235 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000236 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000237}
238
Glenn L McGrath8042f652002-08-23 06:17:46 +0000239static int mount_one(char *blockDevice, char *directory, char *filesystemType,
240 unsigned long flags, char *string_flags, int useMtab,
241 int fakeIt, char *mtab_opts, int whineOnErrors,
242 int mount_all)
Eric Andersencc8ed391999-10-05 16:24:54 +0000243{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 int status = 0;
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000245 if (strcmp(filesystemType, "auto") == 0) {
246 char buf[255];
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000247 FILE *f;
248 int read_proc = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000249
250 f = fopen("/etc/filesystems", "r");
251
252 if (f) {
253 while (fgets(buf, sizeof(buf), f)) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000254 if (*buf == '*') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000255 read_proc = 1;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000256 } else if (*buf == '#') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000257 continue;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000258 } else {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000259 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000260
261 /* Add NULL termination to each line */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000262 while (*filesystemType && !isspace(*filesystemType)) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000263 filesystemType++;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000264 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000265 *filesystemType = '\0';
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000266
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000267 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000268
Manuel Novoa III cad53642003-03-19 09:13:01 +0000269 if (bb_strlen(filesystemType)) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000270 status =
271 do_mount(blockDevice, directory, filesystemType,
272 flags | MS_MGC_VAL, string_flags,
273 useMtab, fakeIt, mtab_opts, mount_all);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000274 if (status) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000275 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000276 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000277 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000278
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000279 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000280 }
281 fclose(f);
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000282 }
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000283
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000284 if ((!f || read_proc) && !status) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000285 f = bb_xfopen("/proc/filesystems", "r");
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000286
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000287 while (fgets(buf, sizeof(buf), f) != NULL) {
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000288 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000289 if (*filesystemType == '\t') { /* Not a nodev filesystem */
290
291 /* Add NULL termination to each line */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000292 while (*filesystemType && *filesystemType != '\n') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000293 filesystemType++;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000294 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000295 *filesystemType = '\0';
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000296
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000297 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000298 filesystemType++; /* hop past tab */
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000299
Glenn L McGrath8042f652002-08-23 06:17:46 +0000300 status =
301 do_mount(blockDevice, directory, filesystemType,
302 flags | MS_MGC_VAL, string_flags, useMtab,
303 fakeIt, mtab_opts, mount_all);
304 if (status) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000305 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000306 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000307 }
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000308 }
309 }
310 fclose(f);
Eric Andersendeca1062002-12-05 07:24:08 +0000311 } else {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000312 status =
313 do_mount(blockDevice, directory, filesystemType,
314 flags | MS_MGC_VAL, string_flags, useMtab, fakeIt,
315 mtab_opts, mount_all);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000317
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000318 if (!status) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000319 if (whineOnErrors) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000320 bb_perror_msg("Mounting %s on %s failed", blockDevice, directory);
Erik Andersene132f4b2000-02-09 04:16:43 +0000321 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000322 return (FALSE);
323 }
324 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000325}
326
Robert Griebld0dd3d32002-07-25 14:17:19 +0000327static void show_mounts(char *onlytype)
Matt Kraai12400822001-04-17 04:32:50 +0000328{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000329 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
Matt Kraai12400822001-04-17 04:32:50 +0000330
331 if (mountTable) {
332 struct mntent *m;
333
334 while ((m = getmntent(mountTable)) != 0) {
335 char *blockDevice = m->mnt_fsname;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000336
Eric Andersen9d7f0f02003-06-20 09:36:49 +0000337 if (strcmp(blockDevice, "rootfs") == 0) {
338 continue;
339 } else if (strcmp(blockDevice, "/dev/root") == 0) {
Eric Andersenc911a432001-05-15 17:42:16 +0000340 blockDevice = find_real_root_device_name(blockDevice);
Matt Kraai12400822001-04-17 04:32:50 +0000341 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000342 if (!onlytype || (strcmp(m->mnt_type, onlytype) == 0)) {
Robert Griebld0dd3d32002-07-25 14:17:19 +0000343 printf("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
344 m->mnt_type, m->mnt_opts);
345 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000346#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrath8042f652002-08-23 06:17:46 +0000347 if (blockDevice != m->mnt_fsname) {
Eric Andersenc911a432001-05-15 17:42:16 +0000348 free(blockDevice);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000349 }
Eric Andersenc911a432001-05-15 17:42:16 +0000350#endif
Matt Kraai12400822001-04-17 04:32:50 +0000351 }
352 endmntent(mountTable);
353 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000354 bb_perror_msg_and_die(bb_path_mtab_file);
Matt Kraai12400822001-04-17 04:32:50 +0000355 }
356 exit(EXIT_SUCCESS);
Matt Kraai12400822001-04-17 04:32:50 +0000357}
358
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359extern int mount_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000360{
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000361 struct stat statbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000362 char *string_flags = bb_xstrdup("");
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000363 char *extra_opts;
Eric Andersene7413a92000-07-14 06:19:41 +0000364 int flags = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 char *filesystemType = "auto";
Robert Griebld0dd3d32002-07-25 14:17:19 +0000366 int got_filesystemType = 0;
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000367 char *device = xmalloc(PATH_MAX);
368 char *directory = xmalloc(PATH_MAX);
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000369 struct mntent *m = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370 int all = FALSE;
371 int fakeIt = FALSE;
372 int useMtab = TRUE;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000373 int rc = EXIT_FAILURE;
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000374 FILE *f = 0;
Matt Kraaia3045df2001-04-17 04:48:51 +0000375 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000376
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377 /* Parse options */
Matt Kraaia3045df2001-04-17 04:48:51 +0000378 while ((opt = getopt(argc, argv, "o:rt:wafnv")) > 0) {
379 switch (opt) {
380 case 'o':
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000381 parse_mount_options(optarg, &flags, &string_flags);
Matt Kraaia3045df2001-04-17 04:48:51 +0000382 break;
383 case 'r':
384 flags |= MS_RDONLY;
385 break;
386 case 't':
387 filesystemType = optarg;
Robert Griebld0dd3d32002-07-25 14:17:19 +0000388 got_filesystemType = 1;
Matt Kraaia3045df2001-04-17 04:48:51 +0000389 break;
390 case 'w':
391 flags &= ~MS_RDONLY;
392 break;
393 case 'a':
394 all = TRUE;
395 break;
396 case 'f':
397 fakeIt = TRUE;
398 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000399#ifdef CONFIG_FEATURE_MTAB_SUPPORT
Matt Kraaia3045df2001-04-17 04:48:51 +0000400 case 'n':
401 useMtab = FALSE;
402 break;
Eric Andersena9c95ea1999-11-15 17:33:30 +0000403#endif
Matt Kraaia3045df2001-04-17 04:48:51 +0000404 case 'v':
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000405 break; /* ignore -v */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 }
Matt Kraaia3045df2001-04-17 04:48:51 +0000407 }
408
Glenn L McGrath8042f652002-08-23 06:17:46 +0000409 if (!all && (optind == argc)) {
410 show_mounts(got_filesystemType ? filesystemType : NULL);
411 }
Matt Kraai12400822001-04-17 04:32:50 +0000412
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000413 if (optind < argc) {
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000414 /* if device is a filename get its real path */
Glenn L McGrathcc0aa0f2001-05-07 01:51:24 +0000415 if (stat(argv[optind], &statbuf) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000416 char *tmp = bb_simplify_path(argv[optind]);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000417
Eric Andersendefd9982002-04-13 13:47:39 +0000418 safe_strncpy(device, tmp, PATH_MAX);
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000419 } else {
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000420 safe_strncpy(device, argv[optind], PATH_MAX);
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000421 }
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000422 }
Matt Kraai34251112001-05-02 21:17:38 +0000423
Matt Kraaia7cecbc2001-08-10 15:05:27 +0000424 if (optind + 1 < argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000425 directory = bb_simplify_path(argv[optind + 1]);
Matt Kraaia7cecbc2001-08-10 15:05:27 +0000426
Matt Kraai1f0c4362001-12-20 23:13:26 +0000427 if (all || optind + 1 == argc) {
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000428 f = setmntent("/etc/fstab", "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429
Erik Andersen246cc6d2000-03-07 07:41:42 +0000430 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000431 bb_perror_msg_and_die("\nCannot read /etc/fstab");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000432
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 while ((m = getmntent(f)) != NULL) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000434 if (!all && (optind + 1 == argc)
435 && ((strcmp(device, m->mnt_fsname) != 0)
436 && (strcmp(device, m->mnt_dir) != 0))) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000437 continue;
438 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000439
440 if (all && ( /* If we're mounting 'all' */
441 (strstr(m->mnt_opts, "noauto")) || /* and the file system isn't noauto, */
Eric Andersen5ef44822003-02-28 06:29:27 +0000442 (strstr(m->mnt_type, "swap")))) /* and isn't swap, then mount it */
443 {
Eric Andersenfdd51032000-08-02 18:48:26 +0000444 continue;
445 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000446
447 if (all || flags == 0) { /* Allow single mount to override fstab flags */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 flags = 0;
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000449 string_flags[0] = 0;
450 parse_mount_options(m->mnt_opts, &flags, &string_flags);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000452
Matt Kraai34251112001-05-02 21:17:38 +0000453 strcpy(device, m->mnt_fsname);
454 strcpy(directory, m->mnt_dir);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000455 filesystemType = bb_xstrdup(m->mnt_type);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000456 singlemount:
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000457 extra_opts = string_flags;
Eric Andersend9d03b82000-12-12 23:20:37 +0000458 rc = EXIT_SUCCESS;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000459#ifdef CONFIG_NFSMOUNT
Eric Andersenfcffa2c2002-04-06 05:17:57 +0000460 if (strchr(device, ':') != NULL) {
Pavel Roskin680d65a2000-06-06 17:03:55 +0000461 filesystemType = "nfs";
Glenn L McGrath8042f652002-08-23 06:17:46 +0000462 if (nfsmount
463 (device, directory, &flags, &extra_opts, &string_flags,
464 1)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000465 bb_perror_msg("nfsmount failed");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000466 rc = EXIT_FAILURE;
Eric Andersenfdd51032000-08-02 18:48:26 +0000467 }
Eric Andersen252bacc2000-09-19 01:21:13 +0000468 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469#endif
Glenn L McGrath8042f652002-08-23 06:17:46 +0000470 if (!mount_one
471 (device, directory, filesystemType, flags, string_flags,
472 useMtab, fakeIt, extra_opts, TRUE, all)) {
Matt Kraai92ed8a32000-12-06 15:55:23 +0000473 rc = EXIT_FAILURE;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000474 }
475 if (!all) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000476 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000477 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000478 }
Glenn L McGrath8042f652002-08-23 06:17:46 +0000479 if (f) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000480 endmntent(f);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000481 }
482 if (!all && f && m == NULL) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000483 fprintf(stderr, "Can't find %s in /etc/fstab\n", device);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000484 }
Matt Kraai93ba60f2001-02-28 15:33:12 +0000485 return rc;
Eric Andersenfdd51032000-08-02 18:48:26 +0000486 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000487
Eric Andersenfdd51032000-08-02 18:48:26 +0000488 goto singlemount;
Eric Andersencc8ed391999-10-05 16:24:54 +0000489}