blob: 15a0b5767d13806d23c4c7daff2855581ddb4618 [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 Andersencb81e642003-07-14 21:21:08 +00006 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.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 Andersencb81e642003-07-14 21:21:08 +000028 * 1999-10-07 Erik Andersen <andersen@codepoet.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
Eric Andersend9fe9582003-07-22 08:25:37 +000057#ifdef CONFIG_NFSMOUNT
58#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
59#error "You need to build uClibc with UCLIBC_HAS_RPC for busybox mount with NFS support to compile."
60#endif
61#endif
62
Mark Whitley59ab0252001-01-23 22:30:04 +000063enum {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000064 MS_MGC_VAL = 0xc0ed0000, /* Magic number indicatng "new" flags */
65 MS_RDONLY = 1, /* Mount read-only */
66 MS_NOSUID = 2, /* Ignore suid and sgid bits */
67 MS_NODEV = 4, /* Disallow access to device special files */
68 MS_NOEXEC = 8, /* Disallow program execution */
69 MS_SYNCHRONOUS = 16, /* Writes are synced at once */
70 MS_REMOUNT = 32, /* Alter flags of a mounted FS */
71 MS_MANDLOCK = 64, /* Allow mandatory locks on an FS */
72 S_QUOTA = 128, /* Quota initialized for file/directory/symlink */
73 S_APPEND = 256, /* Append-only file */
74 S_IMMUTABLE = 512, /* Immutable file */
75 MS_NOATIME = 1024, /* Do not update access times. */
76 MS_NODIRATIME = 2048, /* Do not update directory access times */
77 MS_BIND = 4096, /* Use the new linux 2.4.x "mount --bind" feature */
Mark Whitley59ab0252001-01-23 22:30:04 +000078};
Eric Andersencc8ed391999-10-05 16:24:54 +000079
Eric Andersenbd22ed82000-07-08 18:55:24 +000080
Eric Andersenbdfd0d72001-10-24 05:00:29 +000081#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersenb7cc49d2000-01-13 06:38:14 +000082#include <fcntl.h>
83#include <sys/ioctl.h>
Erik Andersene132f4b2000-02-09 04:16:43 +000084static int use_loop = FALSE;
Erik Andersenb7cc49d2000-01-13 06:38:14 +000085#endif
86
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000087extern int mount(__const char *__special_file, __const char *__dir,
88 __const char *__fstype, unsigned long int __rwflag,
89 __const void *__data);
90extern int umount(__const char *__special_file);
91extern int umount2(__const char *__special_file, int __flags);
Eric Andersende440672001-03-01 07:55:49 +000092
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000093extern int sysfs(int option, unsigned int fs_index, char *buf);
Eric Andersena57ba4d2000-07-08 19:20:49 +000094
Eric Andersencc8ed391999-10-05 16:24:54 +000095struct mount_options {
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 const char *name;
97 unsigned long and;
98 unsigned long or;
Eric Andersencc8ed391999-10-05 16:24:54 +000099};
100
Eric Andersen596e5461999-10-07 08:30:23 +0000101static const struct mount_options mount_options[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 {"async", ~MS_SYNCHRONOUS, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000103 {"atime", ~0, ~MS_NOATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 {"defaults", ~0, 0},
Robert Grieblaa385d42002-05-14 22:56:29 +0000105 {"noauto", ~0, 0},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 {"dev", ~MS_NODEV, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000107 {"diratime", ~0, ~MS_NODIRATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 {"exec", ~MS_NOEXEC, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000109 {"noatime", ~0, MS_NOATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 {"nodev", ~0, MS_NODEV},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000111 {"nodiratime", ~0, MS_NODIRATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 {"noexec", ~0, MS_NOEXEC},
113 {"nosuid", ~0, MS_NOSUID},
114 {"remount", ~0, MS_REMOUNT},
115 {"ro", ~0, MS_RDONLY},
116 {"rw", ~MS_RDONLY, 0},
117 {"suid", ~MS_NOSUID, 0},
118 {"sync", ~0, MS_SYNCHRONOUS},
Eric Andersen2f6e1f82001-05-21 15:59:34 +0000119 {"bind", ~0, MS_BIND},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 {0, 0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000121};
122
Eric Andersend0246fb1999-11-04 21:18:07 +0000123static int
Glenn L McGrath8042f652002-08-23 06:17:46 +0000124do_mount(char *specialfile, char *dir, char *filesystemtype, long flags,
125 void *string_flags, int useMtab, int fakeIt, char *mtab_opts,
126 int mount_all)
Eric Andersend0246fb1999-11-04 21:18:07 +0000127{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 int status = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000129
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000130#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersene132f4b2000-02-09 04:16:43 +0000131 char *lofile = NULL;
Eric Andersen8847b9a2000-09-21 01:33:05 +0000132#endif
Eric Andersend0246fb1999-11-04 21:18:07 +0000133
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000134 if (!fakeIt) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000135#if defined CONFIG_FEATURE_MOUNT_LOOP
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000136 if (use_loop == TRUE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 int loro = flags & MS_RDONLY;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000138
Mark Whitleye677dfe2001-02-26 17:45:58 +0000139 lofile = specialfile;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140
141 specialfile = find_unused_loop_device();
142 if (specialfile == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_error_msg_and_die("Could not find a spare loop device");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144 }
145 if (set_loop(specialfile, lofile, 0, &loro)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 bb_error_msg_and_die("Could not setup loop device");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 }
148 if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 bb_error_msg("WARNING: loop device is read-only");
Matt Kraai94f3a572001-07-05 14:46:07 +0000150 flags |= MS_RDONLY;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 }
152 }
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000153#endif
Eric Andersena42982e2000-06-07 17:28:53 +0000154 status = mount(specialfile, dir, filesystemtype, flags, string_flags);
Matt Kraai9344f752001-06-03 02:21:38 +0000155 if (status < 0 && errno == EROFS) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 bb_error_msg("%s is write-protected, mounting read-only",
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000157 specialfile);
158 status = mount(specialfile, dir, filesystemtype, flags |=
159 MS_RDONLY, string_flags);
Eric Andersen0cccdfa2000-09-20 06:23:36 +0000160 }
Matt Kraai9344f752001-06-03 02:21:38 +0000161 /* Don't whine about already mounted filesystems when mounting all. */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000162 if (status < 0 && errno == EBUSY && mount_all) {
Matt Kraai9344f752001-06-03 02:21:38 +0000163 return TRUE;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000164 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 }
Erik Andersen5cbdd712000-01-26 20:06:48 +0000166
167
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 /* If the mount was sucessful, do anything needed, then return TRUE */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000169 if (status == 0 || fakeIt == TRUE) {
Erik Andersen5cbdd712000-01-26 20:06:48 +0000170
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000171#if defined CONFIG_FEATURE_MTAB_SUPPORT
Matt Kraai1f0c4362001-12-20 23:13:26 +0000172 if (useMtab) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000173 erase_mtab(specialfile); /* Clean any stale entries */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174 write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts);
175 }
176#endif
177 return (TRUE);
178 }
179
180 /* Bummer. mount failed. Clean up */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000181#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersene132f4b2000-02-09 04:16:43 +0000182 if (lofile != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 del_loop(specialfile);
Erik Andersen5cbdd712000-01-26 20:06:48 +0000184 }
Eric Andersend0246fb1999-11-04 21:18:07 +0000185#endif
Eric Andersena42982e2000-06-07 17:28:53 +0000186
187 if (errno == EPERM) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Eric Andersena42982e2000-06-07 17:28:53 +0000189 }
190
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 return (FALSE);
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000192}
193
Eric Andersend0246fb1999-11-04 21:18:07 +0000194
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000195static void paste_str(char **s1, const char *s2)
196{
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000197 *s1 = xrealloc(*s1, strlen(*s1) + strlen(s2) + 1);
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000198 strcat(*s1, s2);
199}
Eric Andersencc8ed391999-10-05 16:24:54 +0000200
Eric Andersen8341a151999-10-08 17:14:14 +0000201/* Seperate standard mount options from the nonstandard string options */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000202static void parse_mount_options(char *options, int *flags, char **strflags)
Eric Andersencc8ed391999-10-05 16:24:54 +0000203{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 while (options) {
205 int gotone = FALSE;
206 char *comma = strchr(options, ',');
207 const struct mount_options *f = mount_options;
Eric Andersencc8ed391999-10-05 16:24:54 +0000208
Glenn L McGrath8042f652002-08-23 06:17:46 +0000209 if (comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 *comma = '\0';
Glenn L McGrath8042f652002-08-23 06:17:46 +0000211 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000212
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 while (f->name != 0) {
214 if (strcasecmp(f->name, options) == 0) {
215
216 *flags &= f->and;
217 *flags |= f->or;
218 gotone = TRUE;
219 break;
220 }
221 f++;
222 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000223#if defined CONFIG_FEATURE_MOUNT_LOOP
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000224 if (!strcasecmp("loop", options)) { /* loop device support */
Erik Andersene132f4b2000-02-09 04:16:43 +0000225 use_loop = TRUE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 gotone = TRUE;
227 }
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000228#endif
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000229 if (!gotone) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000230 if (**strflags) {
231 /* have previous parsed options */
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000232 paste_str(strflags, ",");
Glenn L McGrath8042f652002-08-23 06:17:46 +0000233 }
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000234 paste_str(strflags, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000236 if (comma) {
237 *comma = ',';
238 options = ++comma;
239 } else {
240 break;
241 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000242 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000243}
244
Glenn L McGrath8042f652002-08-23 06:17:46 +0000245static int mount_one(char *blockDevice, char *directory, char *filesystemType,
246 unsigned long flags, char *string_flags, int useMtab,
247 int fakeIt, char *mtab_opts, int whineOnErrors,
248 int mount_all)
Eric Andersencc8ed391999-10-05 16:24:54 +0000249{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 int status = 0;
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000251 if (strcmp(filesystemType, "auto") == 0) {
252 char buf[255];
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000253 FILE *f;
254 int read_proc = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000255
256 f = fopen("/etc/filesystems", "r");
257
258 if (f) {
259 while (fgets(buf, sizeof(buf), f)) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000260 if (*buf == '*') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000261 read_proc = 1;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000262 } else if (*buf == '#') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000263 continue;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000264 } else {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000265 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000266
267 /* Add NULL termination to each line */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000268 while (*filesystemType && !isspace(*filesystemType)) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000269 filesystemType++;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000270 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000271 *filesystemType = '\0';
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000272
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000273 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000274
Manuel Novoa III cad53642003-03-19 09:13:01 +0000275 if (bb_strlen(filesystemType)) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000276 status =
277 do_mount(blockDevice, directory, filesystemType,
278 flags | MS_MGC_VAL, string_flags,
279 useMtab, fakeIt, mtab_opts, mount_all);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000280 if (status) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000281 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000282 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000283 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000284
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000285 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000286 }
287 fclose(f);
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000288 }
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000289
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000290 if ((!f || read_proc) && !status) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000291 f = bb_xfopen("/proc/filesystems", "r");
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000292
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000293 while (fgets(buf, sizeof(buf), f) != NULL) {
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000294 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000295 if (*filesystemType == '\t') { /* Not a nodev filesystem */
296
297 /* Add NULL termination to each line */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000298 while (*filesystemType && *filesystemType != '\n') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000299 filesystemType++;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000300 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000301 *filesystemType = '\0';
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000302
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000303 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000304 filesystemType++; /* hop past tab */
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000305
Glenn L McGrath8042f652002-08-23 06:17:46 +0000306 status =
307 do_mount(blockDevice, directory, filesystemType,
308 flags | MS_MGC_VAL, string_flags, useMtab,
309 fakeIt, mtab_opts, mount_all);
310 if (status) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000311 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000312 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000313 }
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000314 }
315 }
316 fclose(f);
Eric Andersendeca1062002-12-05 07:24:08 +0000317 } else {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000318 status =
319 do_mount(blockDevice, directory, filesystemType,
320 flags | MS_MGC_VAL, string_flags, useMtab, fakeIt,
321 mtab_opts, mount_all);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000322 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000323
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000324 if (!status) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000325 if (whineOnErrors) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000326 bb_perror_msg("Mounting %s on %s failed", blockDevice, directory);
Erik Andersene132f4b2000-02-09 04:16:43 +0000327 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 return (FALSE);
329 }
330 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000331}
332
Robert Griebld0dd3d32002-07-25 14:17:19 +0000333static void show_mounts(char *onlytype)
Matt Kraai12400822001-04-17 04:32:50 +0000334{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000335 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
Matt Kraai12400822001-04-17 04:32:50 +0000336
337 if (mountTable) {
338 struct mntent *m;
339
340 while ((m = getmntent(mountTable)) != 0) {
341 char *blockDevice = m->mnt_fsname;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000342
Eric Andersen9d7f0f02003-06-20 09:36:49 +0000343 if (strcmp(blockDevice, "rootfs") == 0) {
344 continue;
345 } else if (strcmp(blockDevice, "/dev/root") == 0) {
Eric Andersenc911a432001-05-15 17:42:16 +0000346 blockDevice = find_real_root_device_name(blockDevice);
Matt Kraai12400822001-04-17 04:32:50 +0000347 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000348 if (!onlytype || (strcmp(m->mnt_type, onlytype) == 0)) {
Robert Griebld0dd3d32002-07-25 14:17:19 +0000349 printf("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
350 m->mnt_type, m->mnt_opts);
351 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000352#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrath8042f652002-08-23 06:17:46 +0000353 if (blockDevice != m->mnt_fsname) {
Eric Andersenc911a432001-05-15 17:42:16 +0000354 free(blockDevice);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000355 }
Eric Andersenc911a432001-05-15 17:42:16 +0000356#endif
Matt Kraai12400822001-04-17 04:32:50 +0000357 }
358 endmntent(mountTable);
359 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000360 bb_perror_msg_and_die(bb_path_mtab_file);
Matt Kraai12400822001-04-17 04:32:50 +0000361 }
362 exit(EXIT_SUCCESS);
Matt Kraai12400822001-04-17 04:32:50 +0000363}
364
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365extern int mount_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000366{
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000367 struct stat statbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000368 char *string_flags = bb_xstrdup("");
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000369 char *extra_opts;
Eric Andersene7413a92000-07-14 06:19:41 +0000370 int flags = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000371 char *filesystemType = "auto";
Robert Griebld0dd3d32002-07-25 14:17:19 +0000372 int got_filesystemType = 0;
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000373 char *device = xmalloc(PATH_MAX);
374 char *directory = xmalloc(PATH_MAX);
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000375 struct mntent *m = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 int all = FALSE;
377 int fakeIt = FALSE;
378 int useMtab = TRUE;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000379 int rc = EXIT_FAILURE;
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000380 FILE *f = 0;
Matt Kraaia3045df2001-04-17 04:48:51 +0000381 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000382
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383 /* Parse options */
Matt Kraaia3045df2001-04-17 04:48:51 +0000384 while ((opt = getopt(argc, argv, "o:rt:wafnv")) > 0) {
385 switch (opt) {
386 case 'o':
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000387 parse_mount_options(optarg, &flags, &string_flags);
Matt Kraaia3045df2001-04-17 04:48:51 +0000388 break;
389 case 'r':
390 flags |= MS_RDONLY;
391 break;
392 case 't':
393 filesystemType = optarg;
Robert Griebld0dd3d32002-07-25 14:17:19 +0000394 got_filesystemType = 1;
Matt Kraaia3045df2001-04-17 04:48:51 +0000395 break;
396 case 'w':
397 flags &= ~MS_RDONLY;
398 break;
399 case 'a':
400 all = TRUE;
401 break;
402 case 'f':
403 fakeIt = TRUE;
404 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000405#ifdef CONFIG_FEATURE_MTAB_SUPPORT
Matt Kraaia3045df2001-04-17 04:48:51 +0000406 case 'n':
407 useMtab = FALSE;
408 break;
Eric Andersena9c95ea1999-11-15 17:33:30 +0000409#endif
Matt Kraaia3045df2001-04-17 04:48:51 +0000410 case 'v':
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000411 break; /* ignore -v */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000412 }
Matt Kraaia3045df2001-04-17 04:48:51 +0000413 }
414
Glenn L McGrath8042f652002-08-23 06:17:46 +0000415 if (!all && (optind == argc)) {
416 show_mounts(got_filesystemType ? filesystemType : NULL);
417 }
Matt Kraai12400822001-04-17 04:32:50 +0000418
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000419 if (optind < argc) {
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000420 /* if device is a filename get its real path */
Glenn L McGrathcc0aa0f2001-05-07 01:51:24 +0000421 if (stat(argv[optind], &statbuf) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000422 char *tmp = bb_simplify_path(argv[optind]);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000423
Eric Andersendefd9982002-04-13 13:47:39 +0000424 safe_strncpy(device, tmp, PATH_MAX);
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000425 } else {
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000426 safe_strncpy(device, argv[optind], PATH_MAX);
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000427 }
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000428 }
Matt Kraai34251112001-05-02 21:17:38 +0000429
Matt Kraaia7cecbc2001-08-10 15:05:27 +0000430 if (optind + 1 < argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000431 directory = bb_simplify_path(argv[optind + 1]);
Matt Kraaia7cecbc2001-08-10 15:05:27 +0000432
Matt Kraai1f0c4362001-12-20 23:13:26 +0000433 if (all || optind + 1 == argc) {
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000434 f = setmntent("/etc/fstab", "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435
Erik Andersen246cc6d2000-03-07 07:41:42 +0000436 if (f == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000437 bb_perror_msg_and_die("\nCannot read /etc/fstab");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000438
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439 while ((m = getmntent(f)) != NULL) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000440 if (!all && (optind + 1 == argc)
441 && ((strcmp(device, m->mnt_fsname) != 0)
442 && (strcmp(device, m->mnt_dir) != 0))) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000443 continue;
444 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000445
446 if (all && ( /* If we're mounting 'all' */
447 (strstr(m->mnt_opts, "noauto")) || /* and the file system isn't noauto, */
Eric Andersen5ef44822003-02-28 06:29:27 +0000448 (strstr(m->mnt_type, "swap")))) /* and isn't swap, then mount it */
449 {
Eric Andersenfdd51032000-08-02 18:48:26 +0000450 continue;
451 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000452
453 if (all || flags == 0) { /* Allow single mount to override fstab flags */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454 flags = 0;
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000455 string_flags[0] = 0;
456 parse_mount_options(m->mnt_opts, &flags, &string_flags);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000458
Matt Kraai34251112001-05-02 21:17:38 +0000459 strcpy(device, m->mnt_fsname);
460 strcpy(directory, m->mnt_dir);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000461 filesystemType = bb_xstrdup(m->mnt_type);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000462 singlemount:
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000463 extra_opts = string_flags;
Eric Andersend9d03b82000-12-12 23:20:37 +0000464 rc = EXIT_SUCCESS;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000465#ifdef CONFIG_NFSMOUNT
Eric Andersenfcffa2c2002-04-06 05:17:57 +0000466 if (strchr(device, ':') != NULL) {
Pavel Roskin680d65a2000-06-06 17:03:55 +0000467 filesystemType = "nfs";
Glenn L McGrath8042f652002-08-23 06:17:46 +0000468 if (nfsmount
469 (device, directory, &flags, &extra_opts, &string_flags,
470 1)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000471 bb_perror_msg("nfsmount failed");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000472 rc = EXIT_FAILURE;
Eric Andersenfdd51032000-08-02 18:48:26 +0000473 }
Eric Andersen252bacc2000-09-19 01:21:13 +0000474 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475#endif
Glenn L McGrath8042f652002-08-23 06:17:46 +0000476 if (!mount_one
477 (device, directory, filesystemType, flags, string_flags,
478 useMtab, fakeIt, extra_opts, TRUE, all)) {
Matt Kraai92ed8a32000-12-06 15:55:23 +0000479 rc = EXIT_FAILURE;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000480 }
481 if (!all) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000482 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000483 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000484 }
Glenn L McGrath8042f652002-08-23 06:17:46 +0000485 if (f) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000486 endmntent(f);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000487 }
488 if (!all && f && m == NULL) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000489 fprintf(stderr, "Can't find %s in /etc/fstab\n", device);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000490 }
Matt Kraai93ba60f2001-02-28 15:33:12 +0000491 return rc;
Eric Andersenfdd51032000-08-02 18:48:26 +0000492 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000493
Eric Andersenfdd51032000-08-02 18:48:26 +0000494 goto singlemount;
Eric Andersencc8ed391999-10-05 16:24:54 +0000495}