blob: 93a94bc77e2ea774f38b2c7defc8a04b39eb611a [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 Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Rob Landleydc0955b2006-03-14 18:16:25 +00007 * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net>
Eric Andersen596e5461999-10-07 08:30:23 +00008 *
Rob Landley7b363fd2005-12-20 17:18:01 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersenb7cc49d2000-01-13 06:38:14 +000010 */
Eric Andersencc8ed391999-10-05 16:24:54 +000011
Rob Landleydc0955b2006-03-14 18:16:25 +000012/* todo:
13 * bb_getopt_ulflags();
14 */
15
Rob Landley22d26fc2006-06-15 15:49:36 +000016/* Design notes: There is no spec for mount. Remind me to write one.
Rob Landleydc0955b2006-03-14 18:16:25 +000017
18 mount_main() calls singlemount() which calls mount_it_now().
Eric Andersen9601a1c2006-03-20 18:07:50 +000019
Rob Landleydc0955b2006-03-14 18:16:25 +000020 mount_main() can loop through /etc/fstab for mount -a
21 singlemount() can loop through /etc/filesystems for fstype detection.
22 mount_it_now() does the actual mount.
23*/
24
Rob Landley22d26fc2006-06-15 15:49:36 +000025#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include <mntent.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000027
Rob Landleydc0955b2006-03-14 18:16:25 +000028// Not real flags, but we want to be able to check for this.
29#define MOUNT_NOAUTO (1<<29)
30#define MOUNT_SWAP (1<<30)
Rob Landley3ba7bd12006-08-09 19:51:13 +000031
Rob Landleydc0955b2006-03-14 18:16:25 +000032/* Standard mount options (from -o options or --options), with corresponding
33 * flags */
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Rob Landley6a6798b2005-08-10 20:35:54 +000035struct {
Rob Landleye3781b72006-08-08 01:39:49 +000036 char *name;
Rob Landley6a6798b2005-08-10 20:35:54 +000037 long flags;
Rob Landleye3781b72006-08-08 01:39:49 +000038} static mount_options[] = {
39 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
Rob Landleydc0955b2006-03-14 18:16:25 +000040
Rob Landleye3781b72006-08-08 01:39:49 +000041 USE_FEATURE_MOUNT_LOOP(
42 {"loop", 0},
43 )
Rob Landleydc0955b2006-03-14 18:16:25 +000044
Rob Landleye3781b72006-08-08 01:39:49 +000045 USE_FEATURE_MOUNT_FSTAB(
46 {"defaults", 0},
47 {"quiet", 0},
48 {"noauto",MOUNT_NOAUTO},
49 {"swap",MOUNT_SWAP},
50 )
Rob Landleydc0955b2006-03-14 18:16:25 +000051
Rob Landleye3781b72006-08-08 01:39:49 +000052 USE_FEATURE_MOUNT_FLAGS(
53 // vfs flags
54 {"nosuid", MS_NOSUID},
55 {"suid", ~MS_NOSUID},
56 {"dev", ~MS_NODEV},
57 {"nodev", MS_NODEV},
58 {"exec", ~MS_NOEXEC},
59 {"noexec", MS_NOEXEC},
60 {"sync", MS_SYNCHRONOUS},
61 {"async", ~MS_SYNCHRONOUS},
62 {"atime", ~MS_NOATIME},
63 {"noatime", MS_NOATIME},
64 {"diratime", ~MS_NODIRATIME},
65 {"nodiratime", MS_NODIRATIME},
66 {"loud", ~MS_SILENT},
Eric Andersen9601a1c2006-03-20 18:07:50 +000067
Rob Landleye3781b72006-08-08 01:39:49 +000068 // action flags
Rob Landleydc0955b2006-03-14 18:16:25 +000069
Rob Landleye3781b72006-08-08 01:39:49 +000070 {"bind", MS_BIND},
71 {"move", MS_MOVE},
72 {"shared", MS_SHARED},
73 {"slave", MS_SLAVE},
74 {"private", MS_PRIVATE},
75 {"unbindable", MS_UNBINDABLE},
76 {"rshared", MS_SHARED|MS_RECURSIVE},
77 {"rslave", MS_SLAVE|MS_RECURSIVE},
78 {"rprivate", MS_SLAVE|MS_RECURSIVE},
79 {"runbindable", MS_UNBINDABLE|MS_RECURSIVE},
80 )
81
82 // Always understood.
83
84 {"ro", MS_RDONLY}, // vfs flag
85 {"rw", ~MS_RDONLY}, // vfs flag
86 {"remount", MS_REMOUNT}, // action flag
87
88
Eric Andersencc8ed391999-10-05 16:24:54 +000089};
90
Rob Landleydc0955b2006-03-14 18:16:25 +000091/* Append mount options to string */
92static void append_mount_options(char **oldopts, char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +000093{
Rob Landleydc0955b2006-03-14 18:16:25 +000094 if(*oldopts && **oldopts) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000095 char *temp=xasprintf("%s,%s",*oldopts,newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +000096 free(*oldopts);
97 *oldopts=temp;
98 } else {
99 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000100 *oldopts = xstrdup(newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000101 }
102}
103
104/* Use the mount_options list to parse options into flags.
105 * Return list of unrecognized options in *strflags if strflags!=NULL */
106static int parse_mount_options(char *options, char **unrecognized)
107{
108 int flags = MS_SILENT;
109
Rob Landley6a6798b2005-08-10 20:35:54 +0000110 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000111 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000112 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000114
Rob Landleydc0955b2006-03-14 18:16:25 +0000115 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000116
Rob Landley6a6798b2005-08-10 20:35:54 +0000117 // Find this option in mount_options
Rob Landleydc0955b2006-03-14 18:16:25 +0000118 for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
119 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000120 long fl = mount_options[i].flags;
Rob Landleydc0955b2006-03-14 18:16:25 +0000121 if(fl < 0) flags &= fl;
122 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 break;
124 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000126 // If unrecognized not NULL, append unrecognized mount options */
127 if (unrecognized
128 && i == (sizeof(mount_options) / sizeof(*mount_options)))
Eric Andersen9601a1c2006-03-20 18:07:50 +0000129 {
Rob Landley6a6798b2005-08-10 20:35:54 +0000130 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000131 i = *unrecognized ? strlen(*unrecognized) : 0;
132 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000133
Rob Landley6a6798b2005-08-10 20:35:54 +0000134 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000135 if (i) (*unrecognized)[i++] = ',';
136 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000138
Rob Landley6a6798b2005-08-10 20:35:54 +0000139 // Advance to next option, or finish
140 if(comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 *comma = ',';
142 options = ++comma;
Rob Landley6a6798b2005-08-10 20:35:54 +0000143 } else break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000144 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000145
Rob Landleydc0955b2006-03-14 18:16:25 +0000146 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000147}
148
Rob Landleydc0955b2006-03-14 18:16:25 +0000149// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000150
Rob Landleydc0955b2006-03-14 18:16:25 +0000151static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000152{
Rob Landleydc0955b2006-03-14 18:16:25 +0000153 char *fs, *buf,
154 *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
155 llist_t *list = 0;
156 int i;
157 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000158
Rob Landleydc0955b2006-03-14 18:16:25 +0000159 for(i = 0; filesystems[i]; i++) {
160 if(!(f = fopen(filesystems[i], "r"))) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000161
Rob Landleydc0955b2006-03-14 18:16:25 +0000162 for(fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f));
163 free(buf))
164 {
165 if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000166
Rob Landleydc0955b2006-03-14 18:16:25 +0000167 while(isspace(*fs)) fs++;
168 if(*fs=='#' || *fs=='*') continue;
169 if(!*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000170
Rob Landleyd921b2e2006-08-03 15:41:12 +0000171 llist_add_to_end(&list,xstrdup(fs));
Rob Landleydc0955b2006-03-14 18:16:25 +0000172 }
173 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
174 }
175
176 return list;
177}
178
179llist_t *fslist = 0;
180
Rob Landleydc0955b2006-03-14 18:16:25 +0000181#if ENABLE_FEATURE_CLEAN_UP
182static void delete_block_backed_filesystems(void)
183{
Rob Landleya6b5b602006-05-08 19:03:07 +0000184 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000185}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000186#else
187void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000188#endif
189
190#if ENABLE_FEATURE_MTAB_SUPPORT
191static int useMtab;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000192static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000193#else
194#define useMtab 0
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000195#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000196#endif
197
198// Perform actual mount of specific filesystem at specific location.
199
Rob Landleyfe908fd2006-03-29 14:30:49 +0000200static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000201{
202 int rc;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000203
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000204 if (fakeIt) { return 0; }
205
Rob Landleydc0955b2006-03-14 18:16:25 +0000206 // Mount, with fallback to read-only if necessary.
207
208 for(;;) {
209 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
210 vfsflags, filteropts);
211 if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
212 break;
213 bb_error_msg("%s is write-protected, mounting read-only",
214 mp->mnt_fsname);
215 vfsflags |= MS_RDONLY;
216 }
217
Rob Landleydc0955b2006-03-14 18:16:25 +0000218 // Abort entirely if permission denied.
219
220 if (rc && errno == EPERM)
221 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
222
223 /* If the mount was successful, and we're maintaining an old-style
224 * mtab file by hand, add the new entry to it now. */
Eric Andersen9601a1c2006-03-20 18:07:50 +0000225
Rob Landleydc0955b2006-03-14 18:16:25 +0000226 if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
227 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
228 int i;
229
230 if(!mountTable)
231 bb_error_msg("No %s\n",bb_path_mtab_file);
232
233 // Add vfs string flags
234
235 for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
236 if (mount_options[i].flags > 0)
Rob Landleye3781b72006-08-08 01:39:49 +0000237 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000238
239 // Remove trailing / (if any) from directory we mounted on
240
241 i = strlen(mp->mnt_dir);
242 if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0;
243
244 // Write and close.
245
246 if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind";
247 addmntent(mountTable, mp);
248 endmntent(mountTable);
249 if (ENABLE_FEATURE_CLEAN_UP)
250 if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0;
251 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000252
Rob Landleydc0955b2006-03-14 18:16:25 +0000253 return rc;
254}
255
256
257// Mount one directory. Handles NFS, loopback, autobind, and filesystem type
258// detection. Returns 0 for success, nonzero for failure.
259
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000260static int singlemount(struct mntent *mp, int ignore_busy)
Rob Landleydc0955b2006-03-14 18:16:25 +0000261{
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000262 int rc = -1, vfsflags;
Rob Landleyfe908fd2006-03-29 14:30:49 +0000263 char *loopFile = 0, *filteropts = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000264 llist_t *fl = 0;
265 struct stat st;
266
Rob Landleyfe908fd2006-03-29 14:30:49 +0000267 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000268
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000269 // Treat fstype "auto" as unspecified.
270
271 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
272
Rob Landleydc0955b2006-03-14 18:16:25 +0000273 // Might this be an NFS filesystem?
274
275 if (ENABLE_FEATURE_MOUNT_NFS &&
276 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
277 strchr(mp->mnt_fsname, ':') != NULL)
278 {
Rob Landleyfe908fd2006-03-29 14:30:49 +0000279 if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &filteropts, 1)) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000280 bb_perror_msg("nfsmount failed");
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000281 goto report_error;
Rob Landleyfe908fd2006-03-29 14:30:49 +0000282 } else {
283 // Strangely enough, nfsmount() doesn't actually mount() anything.
Rob Landley19af2792006-04-05 01:43:39 +0000284 mp->mnt_type = "nfs";
Rob Landleyfe908fd2006-03-29 14:30:49 +0000285 rc = mount_it_now(mp, vfsflags, filteropts);
286 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
287
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000288 goto report_error;
Rob Landleydc0955b2006-03-14 18:16:25 +0000289 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000290 }
291
Rob Landleyab873602006-04-04 16:56:04 +0000292 // Look at the file. (Not found isn't a failure for remount, or for
293 // a synthetic filesystem like proc or sysfs.)
Rob Landleydc0955b2006-03-14 18:16:25 +0000294
Rob Landley3ba7bd12006-08-09 19:51:13 +0000295 if (!lstat(mp->mnt_fsname, &st) && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
Rob Landleye3781b72006-08-08 01:39:49 +0000296 {
Rob Landleydc0955b2006-03-14 18:16:25 +0000297 // Do we need to allocate a loopback device for it?
Eric Andersen9601a1c2006-03-20 18:07:50 +0000298
Rob Landleydc0955b2006-03-14 18:16:25 +0000299 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000300 loopFile = bb_simplify_path(mp->mnt_fsname);
Rob Landleydc0955b2006-03-14 18:16:25 +0000301 mp->mnt_fsname = 0;
302 switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) {
303 case 0:
304 case 1:
305 break;
306 default:
307 bb_error_msg( errno == EPERM || errno == EACCES
308 ? bb_msg_perm_denied_are_you_root
309 : "Couldn't setup loop device");
310 return errno;
311 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000312
Rob Landleydc0955b2006-03-14 18:16:25 +0000313 // Autodetect bind mounts
314
315 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) vfsflags |= MS_BIND;
316 }
317
318 /* If we know the fstype (or don't need to), jump straight
319 * to the actual mount. */
320
321 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
Rob Landleyfe908fd2006-03-29 14:30:49 +0000322 rc = mount_it_now(mp, vfsflags, filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000323
324 // Loop through filesystem types until mount succeeds or we run out
325
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000326 else {
Rob Landleydc0955b2006-03-14 18:16:25 +0000327
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000328 /* Initialize list of block backed filesystems. This has to be
329 * done here so that during "mount -a", mounts after /proc shows up
330 * can autodetect. */
Rob Landleydc0955b2006-03-14 18:16:25 +0000331
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000332 if (!fslist) {
333 fslist = get_block_backed_filesystems();
334 if (ENABLE_FEATURE_CLEAN_UP && fslist)
335 atexit(delete_block_backed_filesystems);
336 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000337
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000338 for (fl = fslist; fl; fl = fl->link) {
339 mp->mnt_type = fl->data;
340
Rob Landleyfe908fd2006-03-29 14:30:49 +0000341 if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000342
343 mp->mnt_type = 0;
344 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000345 }
346
Rob Landleyfe908fd2006-03-29 14:30:49 +0000347 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
348
349 // If mount failed, clean up loop file (if any).
350
Bernhard Reutner-Fischer08421e12006-05-26 14:05:48 +0000351 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000352 del_loop(mp->mnt_fsname);
Rob Landleyfe908fd2006-03-29 14:30:49 +0000353 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000354 free(loopFile);
355 free(mp->mnt_fsname);
356 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000357 }
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000358report_error:
359 if (rc && errno == EBUSY && ignore_busy) rc = 0;
360 if (rc < 0)
361 bb_perror_msg("Mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
362
Rob Landleydc0955b2006-03-14 18:16:25 +0000363 return rc;
364}
365
Rob Landleydc0955b2006-03-14 18:16:25 +0000366// Parse options, if necessary parse fstab/mtab, and call singlemount for
367// each directory to be mounted.
368
369int mount_main(int argc, char **argv)
370{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000371 char *cmdopts = xstrdup(""), *fstabname, *fstype=0, *storage_path=0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000372 FILE *fstab;
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000373 int i, opt, all = FALSE, rc = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000374 struct mntent mtpair[2], *mtcur = mtpair;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000375
Rob Landley6a6798b2005-08-10 20:35:54 +0000376 /* parse long options, like --bind and --move. Note that -o option
377 * and --option are synonymous. Yes, this means --remount,rw works. */
378
Rob Landleydc0955b2006-03-14 18:16:25 +0000379 for (i = opt = 0; i < argc; i++) {
380 if (argv[i][0] == '-' && argv[i][1] == '-') {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000381 append_mount_options(&cmdopts,argv[i]+2);
Rob Landleydc0955b2006-03-14 18:16:25 +0000382 } else argv[opt++] = argv[i];
Rob Landley6a6798b2005-08-10 20:35:54 +0000383 }
384 argc = opt;
385
386 // Parse remaining options
387
Rob Landleydc0955b2006-03-14 18:16:25 +0000388 while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) {
Matt Kraaia3045df2001-04-17 04:48:51 +0000389 switch (opt) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000390 case 'o':
391 append_mount_options(&cmdopts, optarg);
392 break;
393 case 't':
394 fstype = optarg;
395 break;
396 case 'r':
397 append_mount_options(&cmdopts, "ro");
398 break;
399 case 'w':
400 append_mount_options(&cmdopts, "rw");
401 break;
402 case 'a':
403 all = TRUE;
404 break;
405 case 'n':
406 USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;)
407 break;
408 case 'f':
409 USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;)
410 break;
411 case 'v':
412 break; // ignore -v
413 default:
414 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000415 }
Matt Kraaia3045df2001-04-17 04:48:51 +0000416 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000417
Rob Landleydc0955b2006-03-14 18:16:25 +0000418 // Three or more non-option arguments? Die with a usage message.
419
420 if (optind-argc>2) bb_show_usage();
Eric Andersen9601a1c2006-03-20 18:07:50 +0000421
Rob Landley6a6798b2005-08-10 20:35:54 +0000422 // If we have no arguments, show currently mounted filesystems
423
Rob Landleydc0955b2006-03-14 18:16:25 +0000424 if (optind == argc) {
425 if (!all) {
426 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
Rob Landley6a6798b2005-08-10 20:35:54 +0000427
Rob Landleydc0955b2006-03-14 18:16:25 +0000428 if(!mountTable) bb_error_msg_and_die("No %s",bb_path_mtab_file);
Rob Landley6a6798b2005-08-10 20:35:54 +0000429
Rob Landleydc0955b2006-03-14 18:16:25 +0000430 while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
431 sizeof(bb_common_bufsiz1)))
432 {
433 // Don't show rootfs.
434 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000435
Rob Landleydc0955b2006-03-14 18:16:25 +0000436 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
437 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
438 mtpair->mnt_dir, mtpair->mnt_type,
439 mtpair->mnt_opts);
440 }
441 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
442 return EXIT_SUCCESS;
Rob Landley6a6798b2005-08-10 20:35:54 +0000443 }
Glenn L McGrath8042f652002-08-23 06:17:46 +0000444 }
Matt Kraai12400822001-04-17 04:32:50 +0000445
Rob Landleydc0955b2006-03-14 18:16:25 +0000446 // When we have two arguments, the second is the directory and we can
447 // skip looking at fstab entirely. We can always abspath() the directory
448 // argument when we get it.
Rob Landley6a6798b2005-08-10 20:35:54 +0000449
Rob Landleydc0955b2006-03-14 18:16:25 +0000450 if (optind+2 == argc) {
451 mtpair->mnt_fsname = argv[optind];
452 mtpair->mnt_dir = argv[optind+1];
453 mtpair->mnt_type = fstype;
454 mtpair->mnt_opts = cmdopts;
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000455 rc = singlemount(mtpair, 0);
Rob Landleydc0955b2006-03-14 18:16:25 +0000456 goto clean_up;
457 }
Rob Landley6a6798b2005-08-10 20:35:54 +0000458
Rob Landley3ba7bd12006-08-09 19:51:13 +0000459 // If we have a shared subtree flag, don't worry about fstab or mtab.
460 i = parse_mount_options(cmdopts,0);
461 if (ENABLE_FEATURE_MOUNT_FLAGS &&
462 (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE )))
463 {
464 rc = mount("", argv[optind], "", i, "");
465 if (rc) bb_perror_msg_and_die("%s", argv[optind]);
466 goto clean_up;
467 }
468
Rob Landleydc0955b2006-03-14 18:16:25 +0000469 // Open either fstab or mtab
Rob Landley6a6798b2005-08-10 20:35:54 +0000470
Rob Landleydc0955b2006-03-14 18:16:25 +0000471 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
Rob Landley721b46e2006-08-08 12:54:02 +0000472 fstabname = bb_path_mtab_file;
Rob Landleydc0955b2006-03-14 18:16:25 +0000473 else fstabname="/etc/fstab";
Eric Andersen9601a1c2006-03-20 18:07:50 +0000474
Rob Landley3ba7bd12006-08-09 19:51:13 +0000475 storage_path = bb_simplify_path(argv[optind]);
476
Rob Landleydc0955b2006-03-14 18:16:25 +0000477 if (!(fstab=setmntent(fstabname,"r")))
478 bb_perror_msg_and_die("Cannot read %s",fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000479
Rob Landleydc0955b2006-03-14 18:16:25 +0000480 // Loop through entries until we find what we're looking for.
Rob Landley6a6798b2005-08-10 20:35:54 +0000481
Rob Landleydc0955b2006-03-14 18:16:25 +0000482 memset(mtpair,0,sizeof(mtpair));
483 for (;;) {
484 struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0);
Rob Landley6a6798b2005-08-10 20:35:54 +0000485
486 // Get next fstab entry
487
Rob Landley20fef962006-04-01 17:32:52 +0000488 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
489 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
490 sizeof(bb_common_bufsiz1)/2))
Rob Landleydc0955b2006-03-14 18:16:25 +0000491 {
492 // Were we looking for something specific?
493
494 if (optind != argc) {
495
496 // If we didn't find anything, complain.
497
498 if (!mtnext->mnt_fsname)
499 bb_error_msg_and_die("Can't find %s in %s",
500 argv[optind], fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000501
Rob Landleydc0955b2006-03-14 18:16:25 +0000502 // Mount the last thing we found.
503
504 mtcur = mtnext;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000505 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000506 append_mount_options(&(mtcur->mnt_opts),cmdopts);
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000507 rc = singlemount(mtcur, 0);
Rob Landleydc0955b2006-03-14 18:16:25 +0000508 free(mtcur->mnt_opts);
509 }
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000510 goto clean_up;
Rob Landley6a6798b2005-08-10 20:35:54 +0000511 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000512
Rob Landleydc0955b2006-03-14 18:16:25 +0000513 /* If we're trying to mount something specific and this isn't it,
514 * skip it. Note we must match both the exact text in fstab (ala
515 * "proc") or a full path from root */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000516
Rob Landleydc0955b2006-03-14 18:16:25 +0000517 if (optind != argc) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000518
Rob Landleydc0955b2006-03-14 18:16:25 +0000519 // Is this what we're looking for?
Rob Landley6a6798b2005-08-10 20:35:54 +0000520
Rob Landleydc0955b2006-03-14 18:16:25 +0000521 if(strcmp(argv[optind],mtcur->mnt_fsname) &&
522 strcmp(storage_path,mtcur->mnt_fsname) &&
523 strcmp(argv[optind],mtcur->mnt_dir) &&
524 strcmp(storage_path,mtcur->mnt_dir)) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000525
Rob Landleydc0955b2006-03-14 18:16:25 +0000526 // Remember this entry. Something later may have overmounted
527 // it, and we want the _last_ match.
Rob Landley6a6798b2005-08-10 20:35:54 +0000528
Rob Landleydc0955b2006-03-14 18:16:25 +0000529 mtcur = mtnext;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000530
Rob Landleydc0955b2006-03-14 18:16:25 +0000531 // If we're mounting all.
Rob Landley6a6798b2005-08-10 20:35:54 +0000532
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000533 } else {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000534
Rob Landleydc0955b2006-03-14 18:16:25 +0000535 // Do we need to match a filesystem type?
536 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000537
Rob Landleydc0955b2006-03-14 18:16:25 +0000538 // Skip noauto and swap anyway.
539
540 if (parse_mount_options(mtcur->mnt_opts,0)
541 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
542
543 // Mount this thing.
544
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000545 if (singlemount(mtcur, 1)) {
546 /* Count number of failed mounts */
547 rc++;
Rob Landleydc0955b2006-03-14 18:16:25 +0000548 }
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000549 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000550 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000551 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000552
Rob Landleydc0955b2006-03-14 18:16:25 +0000553clean_up:
554
555 if (ENABLE_FEATURE_CLEAN_UP) {
556 free(storage_path);
557 free(cmdopts);
Rob Landley7b363fd2005-12-20 17:18:01 +0000558 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000559
Rob Landley6e985212005-08-14 18:46:34 +0000560 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000561}