blob: 829d31666eec5b885548d99d1d1736025764e02c [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 Andersenbdfd0d72001-10-24 05:00:29 +000056#if defined CONFIG_FEATURE_USE_DEVPS_PATCH
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000057# include <linux/devmtab.h> /* For Erik's nifty devmtab device driver */
Mark Whitley4b541a82001-04-25 17:10:30 +000058#endif
Eric Andersenbd22ed82000-07-08 18:55:24 +000059
Mark Whitley59ab0252001-01-23 22:30:04 +000060enum {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000061 MS_MGC_VAL = 0xc0ed0000, /* Magic number indicatng "new" flags */
62 MS_RDONLY = 1, /* Mount read-only */
63 MS_NOSUID = 2, /* Ignore suid and sgid bits */
64 MS_NODEV = 4, /* Disallow access to device special files */
65 MS_NOEXEC = 8, /* Disallow program execution */
66 MS_SYNCHRONOUS = 16, /* Writes are synced at once */
67 MS_REMOUNT = 32, /* Alter flags of a mounted FS */
68 MS_MANDLOCK = 64, /* Allow mandatory locks on an FS */
69 S_QUOTA = 128, /* Quota initialized for file/directory/symlink */
70 S_APPEND = 256, /* Append-only file */
71 S_IMMUTABLE = 512, /* Immutable file */
72 MS_NOATIME = 1024, /* Do not update access times. */
73 MS_NODIRATIME = 2048, /* Do not update directory access times */
74 MS_BIND = 4096, /* Use the new linux 2.4.x "mount --bind" feature */
Mark Whitley59ab0252001-01-23 22:30:04 +000075};
Eric Andersencc8ed391999-10-05 16:24:54 +000076
Eric Andersenbd22ed82000-07-08 18:55:24 +000077
Eric Andersenbdfd0d72001-10-24 05:00:29 +000078#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersenb7cc49d2000-01-13 06:38:14 +000079#include <fcntl.h>
80#include <sys/ioctl.h>
Erik Andersene132f4b2000-02-09 04:16:43 +000081static int use_loop = FALSE;
Erik Andersenb7cc49d2000-01-13 06:38:14 +000082#endif
83
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000084extern int mount(__const char *__special_file, __const char *__dir,
85 __const char *__fstype, unsigned long int __rwflag,
86 __const void *__data);
87extern int umount(__const char *__special_file);
88extern int umount2(__const char *__special_file, int __flags);
Eric Andersende440672001-03-01 07:55:49 +000089
Glenn L McGrath9fef17d2002-08-22 18:41:20 +000090extern int sysfs(int option, unsigned int fs_index, char *buf);
Eric Andersena57ba4d2000-07-08 19:20:49 +000091
Erik Andersene49d5ec2000-02-08 19:58:47 +000092extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersend0246fb1999-11-04 21:18:07 +000093
Eric Andersencc8ed391999-10-05 16:24:54 +000094struct mount_options {
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 const char *name;
96 unsigned long and;
97 unsigned long or;
Eric Andersencc8ed391999-10-05 16:24:54 +000098};
99
Eric Andersen596e5461999-10-07 08:30:23 +0000100static const struct mount_options mount_options[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 {"async", ~MS_SYNCHRONOUS, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000102 {"atime", ~0, ~MS_NOATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 {"defaults", ~0, 0},
Robert Grieblaa385d42002-05-14 22:56:29 +0000104 {"noauto", ~0, 0},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 {"dev", ~MS_NODEV, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000106 {"diratime", ~0, ~MS_NODIRATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 {"exec", ~MS_NOEXEC, 0},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000108 {"noatime", ~0, MS_NOATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 {"nodev", ~0, MS_NODEV},
Erik Andersen6c5f2c62000-05-05 19:49:33 +0000110 {"nodiratime", ~0, MS_NODIRATIME},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 {"noexec", ~0, MS_NOEXEC},
112 {"nosuid", ~0, MS_NOSUID},
113 {"remount", ~0, MS_REMOUNT},
114 {"ro", ~0, MS_RDONLY},
115 {"rw", ~MS_RDONLY, 0},
116 {"suid", ~MS_NOSUID, 0},
117 {"sync", ~0, MS_SYNCHRONOUS},
Eric Andersen2f6e1f82001-05-21 15:59:34 +0000118 {"bind", ~0, MS_BIND},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 {0, 0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +0000120};
121
Eric Andersend0246fb1999-11-04 21:18:07 +0000122static int
Glenn L McGrath8042f652002-08-23 06:17:46 +0000123do_mount(char *specialfile, char *dir, char *filesystemtype, long flags,
124 void *string_flags, int useMtab, int fakeIt, char *mtab_opts,
125 int mount_all)
Eric Andersend0246fb1999-11-04 21:18:07 +0000126{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 int status = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000128
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000129#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersene132f4b2000-02-09 04:16:43 +0000130 char *lofile = NULL;
Eric Andersen8847b9a2000-09-21 01:33:05 +0000131#endif
Eric Andersend0246fb1999-11-04 21:18:07 +0000132
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000133 if (!fakeIt) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000134#if defined CONFIG_FEATURE_MOUNT_LOOP
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000135 if (use_loop == TRUE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 int loro = flags & MS_RDONLY;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000137
Mark Whitleye677dfe2001-02-26 17:45:58 +0000138 lofile = specialfile;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139
140 specialfile = find_unused_loop_device();
141 if (specialfile == NULL) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000142 error_msg_and_die("Could not find a spare loop device");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 }
144 if (set_loop(specialfile, lofile, 0, &loro)) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000145 error_msg_and_die("Could not setup loop device");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 }
147 if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */
Matt Kraaidd19c692001-01-31 19:00:21 +0000148 error_msg("WARNING: loop device is read-only");
Matt Kraai94f3a572001-07-05 14:46:07 +0000149 flags |= MS_RDONLY;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 }
151 }
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000152#endif
Eric Andersena42982e2000-06-07 17:28:53 +0000153 status = mount(specialfile, dir, filesystemtype, flags, string_flags);
Matt Kraai9344f752001-06-03 02:21:38 +0000154 if (status < 0 && errno == EROFS) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000155 error_msg("%s is write-protected, mounting read-only",
156 specialfile);
157 status = mount(specialfile, dir, filesystemtype, flags |=
158 MS_RDONLY, string_flags);
Eric Andersen0cccdfa2000-09-20 06:23:36 +0000159 }
Matt Kraai9344f752001-06-03 02:21:38 +0000160 /* Don't whine about already mounted filesystems when mounting all. */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000161 if (status < 0 && errno == EBUSY && mount_all) {
Matt Kraai9344f752001-06-03 02:21:38 +0000162 return TRUE;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000163 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000164 }
Erik Andersen5cbdd712000-01-26 20:06:48 +0000165
166
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 /* If the mount was sucessful, do anything needed, then return TRUE */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000168 if (status == 0 || fakeIt == TRUE) {
Erik Andersen5cbdd712000-01-26 20:06:48 +0000169
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000170#if defined CONFIG_FEATURE_MTAB_SUPPORT
Matt Kraai1f0c4362001-12-20 23:13:26 +0000171 if (useMtab) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000172 erase_mtab(specialfile); /* Clean any stale entries */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts);
174 }
175#endif
176 return (TRUE);
177 }
178
179 /* Bummer. mount failed. Clean up */
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000180#if defined CONFIG_FEATURE_MOUNT_LOOP
Erik Andersene132f4b2000-02-09 04:16:43 +0000181 if (lofile != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 del_loop(specialfile);
Erik Andersen5cbdd712000-01-26 20:06:48 +0000183 }
Eric Andersend0246fb1999-11-04 21:18:07 +0000184#endif
Eric Andersena42982e2000-06-07 17:28:53 +0000185
186 if (errno == EPERM) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000187 error_msg_and_die("permission denied. Are you root?");
Eric Andersena42982e2000-06-07 17:28:53 +0000188 }
189
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 return (FALSE);
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000191}
192
Eric Andersend0246fb1999-11-04 21:18:07 +0000193
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000194static void paste_str(char **s1, const char *s2)
195{
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000196 *s1 = xrealloc(*s1, strlen(*s1) + strlen(s2) + 1);
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000197 strcat(*s1, s2);
198}
Eric Andersencc8ed391999-10-05 16:24:54 +0000199
Eric Andersen8341a151999-10-08 17:14:14 +0000200/* Seperate standard mount options from the nonstandard string options */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000201static void parse_mount_options(char *options, int *flags, char **strflags)
Eric Andersencc8ed391999-10-05 16:24:54 +0000202{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 while (options) {
204 int gotone = FALSE;
205 char *comma = strchr(options, ',');
206 const struct mount_options *f = mount_options;
Eric Andersencc8ed391999-10-05 16:24:54 +0000207
Glenn L McGrath8042f652002-08-23 06:17:46 +0000208 if (comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 *comma = '\0';
Glenn L McGrath8042f652002-08-23 06:17:46 +0000210 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000211
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 while (f->name != 0) {
213 if (strcasecmp(f->name, options) == 0) {
214
215 *flags &= f->and;
216 *flags |= f->or;
217 gotone = TRUE;
218 break;
219 }
220 f++;
221 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000222#if defined CONFIG_FEATURE_MOUNT_LOOP
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000223 if (!strcasecmp("loop", options)) { /* loop device support */
Erik Andersene132f4b2000-02-09 04:16:43 +0000224 use_loop = TRUE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 gotone = TRUE;
226 }
Erik Andersenb7cc49d2000-01-13 06:38:14 +0000227#endif
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000228 if (!gotone) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000229 if (**strflags) {
230 /* have previous parsed options */
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000231 paste_str(strflags, ",");
Glenn L McGrath8042f652002-08-23 06:17:46 +0000232 }
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000233 paste_str(strflags, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 if (comma) {
236 *comma = ',';
237 options = ++comma;
238 } else {
239 break;
240 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000241 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000242}
243
Glenn L McGrath8042f652002-08-23 06:17:46 +0000244static int mount_one(char *blockDevice, char *directory, char *filesystemType,
245 unsigned long flags, char *string_flags, int useMtab,
246 int fakeIt, char *mtab_opts, int whineOnErrors,
247 int mount_all)
Eric Andersencc8ed391999-10-05 16:24:54 +0000248{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 int status = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000250
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000251#if defined CONFIG_FEATURE_USE_DEVPS_PATCH
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252 if (strcmp(filesystemType, "auto") == 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000253 static const char *noauto_array[] =
254 { "tmpfs", "shm", "proc", "ramfs", "devpts", "devfs", "usbdevfs",
2550 };
Glenn L McGrath1e117b42001-03-30 01:28:13 +0000256 const char **noauto_fstype;
Eric Andersen7b91f022001-03-01 07:50:04 +0000257 const int num_of_filesystems = sysfs(3, 0, 0);
258 char buf[255];
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000259 int i = 0;
Glenn L McGrath323434b2001-03-02 22:21:34 +0000260
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000261 filesystemType = buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000262
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000263 while (i < num_of_filesystems) {
Eric Andersen7b91f022001-03-01 07:50:04 +0000264 sysfs(2, i++, filesystemType);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000265 for (noauto_fstype = noauto_array; *noauto_fstype;
266 noauto_fstype++) {
Glenn L McGrath1e117b42001-03-30 01:28:13 +0000267 if (!strcmp(filesystemType, *noauto_fstype)) {
Glenn L McGrath323434b2001-03-02 22:21:34 +0000268 break;
269 }
270 }
Glenn L McGrath1e117b42001-03-30 01:28:13 +0000271 if (!*noauto_fstype) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000272 status =
273 do_mount(blockDevice, directory, filesystemType,
274 flags | MS_MGC_VAL, string_flags, useMtab,
275 fakeIt, mtab_opts, mount_all);
276 if (status) {
Glenn L McGrath323434b2001-03-02 22:21:34 +0000277 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000278 }
Glenn L McGrath323434b2001-03-02 22:21:34 +0000279 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000280 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000281 }
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000282#else
283 if (strcmp(filesystemType, "auto") == 0) {
284 char buf[255];
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000285 FILE *f;
286 int read_proc = 0;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000287
288 f = fopen("/etc/filesystems", "r");
289
290 if (f) {
291 while (fgets(buf, sizeof(buf), f)) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000292 if (*buf == '*') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000293 read_proc = 1;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000294 } else if (*buf == '#') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000295 continue;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000296 } else {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000297 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000298
299 /* Add NULL termination to each line */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000300 while (*filesystemType && !isspace(*filesystemType)) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000301 filesystemType++;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000302 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000303 *filesystemType = '\0';
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000304
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000305 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000306
307 if (xstrlen(filesystemType)) {
308 status =
309 do_mount(blockDevice, directory, filesystemType,
310 flags | MS_MGC_VAL, string_flags,
311 useMtab, fakeIt, mtab_opts, mount_all);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000312 if (status) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000313 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000314 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000315 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000316
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000317 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000318 }
319 fclose(f);
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000320 }
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000321
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000322 if ((!f || read_proc) && !status) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000323 f = xfopen("/proc/filesystems", "r");
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000324
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000325 while (fgets(buf, sizeof(buf), f) != NULL) {
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000326 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000327 if (*filesystemType == '\t') { /* Not a nodev filesystem */
328
329 /* Add NULL termination to each line */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000330 while (*filesystemType && *filesystemType != '\n') {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000331 filesystemType++;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000332 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000333 *filesystemType = '\0';
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000334
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000335 filesystemType = buf;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000336 filesystemType++; /* hop past tab */
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000337
Glenn L McGrath8042f652002-08-23 06:17:46 +0000338 status =
339 do_mount(blockDevice, directory, filesystemType,
340 flags | MS_MGC_VAL, string_flags, useMtab,
341 fakeIt, mtab_opts, mount_all);
342 if (status) {
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000343 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000344 }
Robert Griebl2a4a8d82002-07-24 01:41:30 +0000345 }
Eric Andersen8acbf1d2001-10-18 04:10:22 +0000346 }
347 }
348 fclose(f);
349 }
350#endif
351 else {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000352 status =
353 do_mount(blockDevice, directory, filesystemType,
354 flags | MS_MGC_VAL, string_flags, useMtab, fakeIt,
355 mtab_opts, mount_all);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000357
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000358 if (!status) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000359 if (whineOnErrors) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000360 perror_msg("Mounting %s on %s failed", blockDevice, directory);
Erik Andersene132f4b2000-02-09 04:16:43 +0000361 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000362 return (FALSE);
363 }
364 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000365}
366
Robert Griebld0dd3d32002-07-25 14:17:19 +0000367static void show_mounts(char *onlytype)
Matt Kraai12400822001-04-17 04:32:50 +0000368{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000369#if defined CONFIG_FEATURE_USE_DEVPS_PATCH
Matt Kraai12400822001-04-17 04:32:50 +0000370 int fd, i, numfilesystems;
371 char device[] = "/dev/mtab";
372 struct k_mntent *mntentlist;
373
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000374 /* open device */
Matt Kraai12400822001-04-17 04:32:50 +0000375 fd = open(device, O_RDONLY);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000376 if (fd < 0) {
Matt Kraai12400822001-04-17 04:32:50 +0000377 perror_msg_and_die("open failed for `%s'", device);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000378 }
Matt Kraai12400822001-04-17 04:32:50 +0000379
380 /* How many mounted filesystems? We need to know to
381 * allocate enough space for later... */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000382 numfilesystems = ioctl(fd, DEVMTAB_COUNT_MOUNTS);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000383 if (numfilesystems < 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000384 perror_msg_and_die("\nDEVMTAB_COUNT_MOUNTS");
Glenn L McGrath8042f652002-08-23 06:17:46 +0000385 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000386 mntentlist =
387 (struct k_mntent *) xcalloc(numfilesystems, sizeof(struct k_mntent));
Matt Kraai12400822001-04-17 04:32:50 +0000388
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000389 /* Grab the list of mounted filesystems */
Glenn L McGrath8042f652002-08-23 06:17:46 +0000390 if (ioctl(fd, DEVMTAB_GET_MOUNTS, mntentlist) < 0) {
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000391 perror_msg_and_die("\nDEVMTAB_GET_MOUNTS");
Glenn L McGrath8042f652002-08-23 06:17:46 +0000392 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000393
394 for (i = 0; i < numfilesystems; i++) {
395 if (!onlytype || (strcmp(mntentlist[i].mnt_type, onlytype) == 0)) {
396 printf("%s %s %s %s %d %d\n", mntentlist[i].mnt_fsname,
397 mntentlist[i].mnt_dir, mntentlist[i].mnt_type,
398 mntentlist[i].mnt_opts, mntentlist[i].mnt_freq,
399 mntentlist[i].mnt_passno);
Robert Griebld0dd3d32002-07-25 14:17:19 +0000400 }
Matt Kraai12400822001-04-17 04:32:50 +0000401 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000402#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraai12400822001-04-17 04:32:50 +0000403 /* Don't bother to close files or free memory. Exit
404 * does that automagically, so we can save a few bytes */
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000405 free(mntentlist);
Matt Kraai12400822001-04-17 04:32:50 +0000406 close(fd);
407#endif
408 exit(EXIT_SUCCESS);
409#else
410 FILE *mountTable = setmntent(mtab_file, "r");
411
412 if (mountTable) {
413 struct mntent *m;
414
415 while ((m = getmntent(mountTable)) != 0) {
416 char *blockDevice = m->mnt_fsname;
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000417
Matt Kraai12400822001-04-17 04:32:50 +0000418 if (strcmp(blockDevice, "/dev/root") == 0) {
Eric Andersenc911a432001-05-15 17:42:16 +0000419 blockDevice = find_real_root_device_name(blockDevice);
Matt Kraai12400822001-04-17 04:32:50 +0000420 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000421 if (!onlytype || (strcmp(m->mnt_type, onlytype) == 0)) {
Robert Griebld0dd3d32002-07-25 14:17:19 +0000422 printf("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
423 m->mnt_type, m->mnt_opts);
424 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000425#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrath8042f652002-08-23 06:17:46 +0000426 if (blockDevice != m->mnt_fsname) {
Eric Andersenc911a432001-05-15 17:42:16 +0000427 free(blockDevice);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000428 }
Eric Andersenc911a432001-05-15 17:42:16 +0000429#endif
Matt Kraai12400822001-04-17 04:32:50 +0000430 }
431 endmntent(mountTable);
432 } else {
433 perror_msg_and_die("%s", mtab_file);
434 }
435 exit(EXIT_SUCCESS);
436#endif
437}
438
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439extern int mount_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000440{
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000441 struct stat statbuf;
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000442 char *string_flags = xstrdup("");
443 char *extra_opts;
Eric Andersene7413a92000-07-14 06:19:41 +0000444 int flags = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 char *filesystemType = "auto";
Robert Griebld0dd3d32002-07-25 14:17:19 +0000446 int got_filesystemType = 0;
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000447 char *device = xmalloc(PATH_MAX);
448 char *directory = xmalloc(PATH_MAX);
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000449 struct mntent *m = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 int all = FALSE;
451 int fakeIt = FALSE;
452 int useMtab = TRUE;
Matt Kraai3e856ce2000-12-01 02:55:13 +0000453 int rc = EXIT_FAILURE;
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000454 FILE *f = 0;
Matt Kraaia3045df2001-04-17 04:48:51 +0000455 int opt;
Eric Andersencc8ed391999-10-05 16:24:54 +0000456
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 /* Parse options */
Matt Kraaia3045df2001-04-17 04:48:51 +0000458 while ((opt = getopt(argc, argv, "o:rt:wafnv")) > 0) {
459 switch (opt) {
460 case 'o':
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000461 parse_mount_options(optarg, &flags, &string_flags);
Matt Kraaia3045df2001-04-17 04:48:51 +0000462 break;
463 case 'r':
464 flags |= MS_RDONLY;
465 break;
466 case 't':
467 filesystemType = optarg;
Robert Griebld0dd3d32002-07-25 14:17:19 +0000468 got_filesystemType = 1;
Matt Kraaia3045df2001-04-17 04:48:51 +0000469 break;
470 case 'w':
471 flags &= ~MS_RDONLY;
472 break;
473 case 'a':
474 all = TRUE;
475 break;
476 case 'f':
477 fakeIt = TRUE;
478 break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000479#ifdef CONFIG_FEATURE_MTAB_SUPPORT
Matt Kraaia3045df2001-04-17 04:48:51 +0000480 case 'n':
481 useMtab = FALSE;
482 break;
Eric Andersena9c95ea1999-11-15 17:33:30 +0000483#endif
Matt Kraaia3045df2001-04-17 04:48:51 +0000484 case 'v':
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000485 break; /* ignore -v */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 }
Matt Kraaia3045df2001-04-17 04:48:51 +0000487 }
488
Glenn L McGrath8042f652002-08-23 06:17:46 +0000489 if (!all && (optind == argc)) {
490 show_mounts(got_filesystemType ? filesystemType : NULL);
491 }
Matt Kraai12400822001-04-17 04:32:50 +0000492
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000493 if (optind < argc) {
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000494 /* if device is a filename get its real path */
Glenn L McGrathcc0aa0f2001-05-07 01:51:24 +0000495 if (stat(argv[optind], &statbuf) == 0) {
Eric Andersendefd9982002-04-13 13:47:39 +0000496 char *tmp = simplify_path(argv[optind]);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000497
Eric Andersendefd9982002-04-13 13:47:39 +0000498 safe_strncpy(device, tmp, PATH_MAX);
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000499 } else {
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000500 safe_strncpy(device, argv[optind], PATH_MAX);
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000501 }
Matt Kraaie6bf66e2001-05-04 14:49:58 +0000502 }
Matt Kraai34251112001-05-02 21:17:38 +0000503
Matt Kraaia7cecbc2001-08-10 15:05:27 +0000504 if (optind + 1 < argc)
505 directory = simplify_path(argv[optind + 1]);
506
Matt Kraai1f0c4362001-12-20 23:13:26 +0000507 if (all || optind + 1 == argc) {
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000508 f = setmntent("/etc/fstab", "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509
Erik Andersen246cc6d2000-03-07 07:41:42 +0000510 if (f == NULL)
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000511 perror_msg_and_die("\nCannot read /etc/fstab");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000512
Erik Andersene49d5ec2000-02-08 19:58:47 +0000513 while ((m = getmntent(f)) != NULL) {
Glenn L McGrath8042f652002-08-23 06:17:46 +0000514 if (!all && (optind + 1 == argc)
515 && ((strcmp(device, m->mnt_fsname) != 0)
516 && (strcmp(device, m->mnt_dir) != 0))) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000517 continue;
518 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000519
520 if (all && ( /* If we're mounting 'all' */
521 (strstr(m->mnt_opts, "noauto")) || /* and the file system isn't noauto, */
522 (strstr(m->mnt_type, "swap")) || /* and isn't swap or nfs, then mount it */
523 (strstr(m->mnt_type, "nfs")))) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000524 continue;
525 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000526
527 if (all || flags == 0) { /* Allow single mount to override fstab flags */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 flags = 0;
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000529 string_flags[0] = 0;
530 parse_mount_options(m->mnt_opts, &flags, &string_flags);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000532
Matt Kraai34251112001-05-02 21:17:38 +0000533 strcpy(device, m->mnt_fsname);
534 strcpy(directory, m->mnt_dir);
Matt Kraaic8227632001-11-12 16:57:27 +0000535 filesystemType = xstrdup(m->mnt_type);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000536 singlemount:
Eric Andersen8b1aa4d2002-06-22 17:20:50 +0000537 extra_opts = string_flags;
Eric Andersend9d03b82000-12-12 23:20:37 +0000538 rc = EXIT_SUCCESS;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000539#ifdef CONFIG_NFSMOUNT
Eric Andersenfcffa2c2002-04-06 05:17:57 +0000540 if (strchr(device, ':') != NULL) {
Pavel Roskin680d65a2000-06-06 17:03:55 +0000541 filesystemType = "nfs";
Glenn L McGrath8042f652002-08-23 06:17:46 +0000542 if (nfsmount
543 (device, directory, &flags, &extra_opts, &string_flags,
544 1)) {
Matt Kraai93ba60f2001-02-28 15:33:12 +0000545 perror_msg("nfsmount failed");
Matt Kraai3e856ce2000-12-01 02:55:13 +0000546 rc = EXIT_FAILURE;
Eric Andersenfdd51032000-08-02 18:48:26 +0000547 }
Eric Andersen252bacc2000-09-19 01:21:13 +0000548 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549#endif
Glenn L McGrath8042f652002-08-23 06:17:46 +0000550 if (!mount_one
551 (device, directory, filesystemType, flags, string_flags,
552 useMtab, fakeIt, extra_opts, TRUE, all)) {
Matt Kraai92ed8a32000-12-06 15:55:23 +0000553 rc = EXIT_FAILURE;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000554 }
555 if (!all) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000556 break;
Glenn L McGrath8042f652002-08-23 06:17:46 +0000557 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000558 }
Glenn L McGrath8042f652002-08-23 06:17:46 +0000559 if (f) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000560 endmntent(f);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000561 }
562 if (!all && f && m == NULL) {
Eric Andersenfdd51032000-08-02 18:48:26 +0000563 fprintf(stderr, "Can't find %s in /etc/fstab\n", device);
Glenn L McGrath8042f652002-08-23 06:17:46 +0000564 }
Matt Kraai93ba60f2001-02-28 15:33:12 +0000565 return rc;
Eric Andersenfdd51032000-08-02 18:48:26 +0000566 }
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000567
Eric Andersenfdd51032000-08-02 18:48:26 +0000568 goto singlemount;
Eric Andersencc8ed391999-10-05 16:24:54 +0000569}