blob: 9225289b6767fae37a0b2b852281b1a24e66de98 [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 <unistd.h>
27#include <errno.h>
28#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000029#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000030#include <ctype.h>
Rob Landley6a6798b2005-08-10 20:35:54 +000031#include <fcntl.h> // for CONFIG_FEATURE_MOUNT_LOOP
32#include <sys/ioctl.h> // for CONFIG_FEATURE_MOUNT_LOOP
Eric Andersenbd22ed82000-07-08 18:55:24 +000033
Rob Landley6a6798b2005-08-10 20:35:54 +000034// These two aren't always defined in old headers
35#ifndef MS_BIND
36#define MS_BIND 4096
37#endif
38#ifndef MS_MOVE
39#define MS_MOVE 8192
Eric Andersend9fe9582003-07-22 08:25:37 +000040#endif
Rob Landleydc0955b2006-03-14 18:16:25 +000041#ifndef MS_SILENT
42#define MS_SILENT 32768
43#endif
Eric Andersend9fe9582003-07-22 08:25:37 +000044
Rob Landleydc0955b2006-03-14 18:16:25 +000045// Not real flags, but we want to be able to check for this.
46#define MOUNT_NOAUTO (1<<29)
47#define MOUNT_SWAP (1<<30)
48/* Standard mount options (from -o options or --options), with corresponding
49 * flags */
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Rob Landley6a6798b2005-08-10 20:35:54 +000051struct {
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 const char *name;
Rob Landley6a6798b2005-08-10 20:35:54 +000053 long flags;
Rob Landleydc0955b2006-03-14 18:16:25 +000054} static const mount_options[] = {
55 // NOP flags.
56
Rob Landley6a6798b2005-08-10 20:35:54 +000057 {"loop", 0},
58 {"defaults", 0},
Rob Landleydc0955b2006-03-14 18:16:25 +000059 {"quiet", 0},
60
Eric Andersen9601a1c2006-03-20 18:07:50 +000061 // vfs flags
Rob Landleydc0955b2006-03-14 18:16:25 +000062
Rob Landley6a6798b2005-08-10 20:35:54 +000063 {"ro", MS_RDONLY},
64 {"rw", ~MS_RDONLY},
65 {"nosuid", MS_NOSUID},
66 {"suid", ~MS_NOSUID},
67 {"dev", ~MS_NODEV},
68 {"nodev", MS_NODEV},
69 {"exec", ~MS_NOEXEC},
70 {"noexec", MS_NOEXEC},
71 {"sync", MS_SYNCHRONOUS},
72 {"async", ~MS_SYNCHRONOUS},
Rob Landley8b0efdb2006-01-10 02:37:20 +000073 {"atime", ~MS_NOATIME},
Rob Landley6a6798b2005-08-10 20:35:54 +000074 {"noatime", MS_NOATIME},
Rob Landley8b0efdb2006-01-10 02:37:20 +000075 {"diratime", ~MS_NODIRATIME},
Rob Landley6a6798b2005-08-10 20:35:54 +000076 {"nodiratime", MS_NODIRATIME},
Rob Landleydc0955b2006-03-14 18:16:25 +000077 {"loud", ~MS_SILENT},
Eric Andersen9601a1c2006-03-20 18:07:50 +000078
Rob Landleydc0955b2006-03-14 18:16:25 +000079 // action flags
80
81 {"remount", MS_REMOUNT},
Rob Landley6a6798b2005-08-10 20:35:54 +000082 {"bind", MS_BIND},
Rob Landleydc0955b2006-03-14 18:16:25 +000083 {"move", MS_MOVE},
84 {"noauto",MOUNT_NOAUTO},
85 {"swap",MOUNT_SWAP}
Eric Andersencc8ed391999-10-05 16:24:54 +000086};
87
Rob Landleydc0955b2006-03-14 18:16:25 +000088/* Append mount options to string */
89static void append_mount_options(char **oldopts, char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +000090{
Rob Landleydc0955b2006-03-14 18:16:25 +000091 if(*oldopts && **oldopts) {
92 char *temp=bb_xasprintf("%s,%s",*oldopts,newopts);
93 free(*oldopts);
94 *oldopts=temp;
95 } else {
96 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
97 *oldopts = bb_xstrdup(newopts);
98 }
99}
100
101/* Use the mount_options list to parse options into flags.
102 * Return list of unrecognized options in *strflags if strflags!=NULL */
103static int parse_mount_options(char *options, char **unrecognized)
104{
105 int flags = MS_SILENT;
106
Rob Landley6a6798b2005-08-10 20:35:54 +0000107 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000108 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000109 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000111
Rob Landleydc0955b2006-03-14 18:16:25 +0000112 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000113
Rob Landley6a6798b2005-08-10 20:35:54 +0000114 // Find this option in mount_options
Rob Landleydc0955b2006-03-14 18:16:25 +0000115 for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
116 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000117 long fl = mount_options[i].flags;
Rob Landleydc0955b2006-03-14 18:16:25 +0000118 if(fl < 0) flags &= fl;
119 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 break;
121 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000123 // If unrecognized not NULL, append unrecognized mount options */
124 if (unrecognized
125 && i == (sizeof(mount_options) / sizeof(*mount_options)))
Eric Andersen9601a1c2006-03-20 18:07:50 +0000126 {
Rob Landley6a6798b2005-08-10 20:35:54 +0000127 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000128 i = *unrecognized ? strlen(*unrecognized) : 0;
129 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000130
Rob Landley6a6798b2005-08-10 20:35:54 +0000131 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000132 if (i) (*unrecognized)[i++] = ',';
133 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000135
Rob Landley6a6798b2005-08-10 20:35:54 +0000136 // Advance to next option, or finish
137 if(comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 *comma = ',';
139 options = ++comma;
Rob Landley6a6798b2005-08-10 20:35:54 +0000140 } else break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000141 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000142
Rob Landleydc0955b2006-03-14 18:16:25 +0000143 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000144}
145
Rob Landleydc0955b2006-03-14 18:16:25 +0000146// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000147
Rob Landleydc0955b2006-03-14 18:16:25 +0000148static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000149{
Rob Landleydc0955b2006-03-14 18:16:25 +0000150 char *fs, *buf,
151 *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
152 llist_t *list = 0;
153 int i;
154 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000155
Rob Landleydc0955b2006-03-14 18:16:25 +0000156 for(i = 0; filesystems[i]; i++) {
157 if(!(f = fopen(filesystems[i], "r"))) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000158
Rob Landleydc0955b2006-03-14 18:16:25 +0000159 for(fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f));
160 free(buf))
161 {
162 if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000163
Rob Landleydc0955b2006-03-14 18:16:25 +0000164 while(isspace(*fs)) fs++;
165 if(*fs=='#' || *fs=='*') continue;
166 if(!*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000167
Rob Landley8bb50782006-05-26 23:44:51 +0000168 llist_add_to_end(&list,bb_xstrdup(fs));
Rob Landleydc0955b2006-03-14 18:16:25 +0000169 }
170 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
171 }
172
173 return list;
174}
175
176llist_t *fslist = 0;
177
Rob Landleydc0955b2006-03-14 18:16:25 +0000178#if ENABLE_FEATURE_CLEAN_UP
179static void delete_block_backed_filesystems(void)
180{
Rob Landleya6b5b602006-05-08 19:03:07 +0000181 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000182}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000183#else
184void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000185#endif
186
187#if ENABLE_FEATURE_MTAB_SUPPORT
188static int useMtab;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000189static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000190#else
191#define useMtab 0
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000192#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000193#endif
194
195// Perform actual mount of specific filesystem at specific location.
196
Rob Landleyfe908fd2006-03-29 14:30:49 +0000197static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000198{
199 int rc;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000200
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000201 if (fakeIt) { return 0; }
202
Rob Landleydc0955b2006-03-14 18:16:25 +0000203 // Mount, with fallback to read-only if necessary.
204
205 for(;;) {
206 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
207 vfsflags, filteropts);
208 if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
209 break;
210 bb_error_msg("%s is write-protected, mounting read-only",
211 mp->mnt_fsname);
212 vfsflags |= MS_RDONLY;
213 }
214
Rob Landleydc0955b2006-03-14 18:16:25 +0000215 // Abort entirely if permission denied.
216
217 if (rc && errno == EPERM)
218 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
219
220 /* If the mount was successful, and we're maintaining an old-style
221 * mtab file by hand, add the new entry to it now. */
Eric Andersen9601a1c2006-03-20 18:07:50 +0000222
Rob Landleydc0955b2006-03-14 18:16:25 +0000223 if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
224 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
225 int i;
226
227 if(!mountTable)
228 bb_error_msg("No %s\n",bb_path_mtab_file);
229
230 // Add vfs string flags
231
232 for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
233 if (mount_options[i].flags > 0)
234 append_mount_options(&(mp->mnt_opts),
235// Shut up about the darn const. It's not important. I don't care.
236 (char *)mount_options[i].name);
237
238 // Remove trailing / (if any) from directory we mounted on
239
240 i = strlen(mp->mnt_dir);
241 if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0;
242
243 // Write and close.
244
245 if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind";
246 addmntent(mountTable, mp);
247 endmntent(mountTable);
248 if (ENABLE_FEATURE_CLEAN_UP)
249 if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0;
250 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000251
Rob Landleydc0955b2006-03-14 18:16:25 +0000252 return rc;
253}
254
255
256// Mount one directory. Handles NFS, loopback, autobind, and filesystem type
257// detection. Returns 0 for success, nonzero for failure.
258
259static int singlemount(struct mntent *mp)
260{
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000261 int rc = 1, vfsflags;
Rob Landleyfe908fd2006-03-29 14:30:49 +0000262 char *loopFile = 0, *filteropts = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000263 llist_t *fl = 0;
264 struct stat st;
265
Rob Landleyfe908fd2006-03-29 14:30:49 +0000266 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000267
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000268 // Treat fstype "auto" as unspecified.
269
270 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
271
Rob Landleydc0955b2006-03-14 18:16:25 +0000272 // Might this be an NFS filesystem?
273
274 if (ENABLE_FEATURE_MOUNT_NFS &&
275 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
276 strchr(mp->mnt_fsname, ':') != NULL)
277 {
Rob Landleyfe908fd2006-03-29 14:30:49 +0000278 if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &filteropts, 1)) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000279 bb_perror_msg("nfsmount failed");
280 return 1;
Rob Landleyfe908fd2006-03-29 14:30:49 +0000281 } else {
282 // Strangely enough, nfsmount() doesn't actually mount() anything.
Rob Landley19af2792006-04-05 01:43:39 +0000283 mp->mnt_type = "nfs";
Rob Landleyfe908fd2006-03-29 14:30:49 +0000284 rc = mount_it_now(mp, vfsflags, filteropts);
285 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
286
287 return rc;
Rob Landleydc0955b2006-03-14 18:16:25 +0000288 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000289 }
290
Rob Landleyab873602006-04-04 16:56:04 +0000291 // Look at the file. (Not found isn't a failure for remount, or for
292 // a synthetic filesystem like proc or sysfs.)
Rob Landleydc0955b2006-03-14 18:16:25 +0000293
294 if (lstat(mp->mnt_fsname, &st));
Rob Landleyab873602006-04-04 16:56:04 +0000295 else if (!(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000296 // Do we need to allocate a loopback device for it?
Eric Andersen9601a1c2006-03-20 18:07:50 +0000297
Rob Landleydc0955b2006-03-14 18:16:25 +0000298 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000299 loopFile = bb_simplify_path(mp->mnt_fsname);
Rob Landleydc0955b2006-03-14 18:16:25 +0000300 mp->mnt_fsname = 0;
301 switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) {
302 case 0:
303 case 1:
304 break;
305 default:
306 bb_error_msg( errno == EPERM || errno == EACCES
307 ? bb_msg_perm_denied_are_you_root
308 : "Couldn't setup loop device");
309 return errno;
310 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000311
Rob Landleydc0955b2006-03-14 18:16:25 +0000312 // Autodetect bind mounts
313
314 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) vfsflags |= MS_BIND;
315 }
316
317 /* If we know the fstype (or don't need to), jump straight
318 * to the actual mount. */
319
320 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
Rob Landleyfe908fd2006-03-29 14:30:49 +0000321 rc = mount_it_now(mp, vfsflags, filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000322
323 // Loop through filesystem types until mount succeeds or we run out
324
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000325 else {
Rob Landleydc0955b2006-03-14 18:16:25 +0000326
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000327 /* Initialize list of block backed filesystems. This has to be
328 * done here so that during "mount -a", mounts after /proc shows up
329 * can autodetect. */
Rob Landleydc0955b2006-03-14 18:16:25 +0000330
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000331 if (!fslist) {
332 fslist = get_block_backed_filesystems();
333 if (ENABLE_FEATURE_CLEAN_UP && fslist)
334 atexit(delete_block_backed_filesystems);
335 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000336
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000337 for (fl = fslist; fl; fl = fl->link) {
338 mp->mnt_type = fl->data;
339
Rob Landleyfe908fd2006-03-29 14:30:49 +0000340 if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000341
342 mp->mnt_type = 0;
343 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000344 }
345
Rob Landleyfe908fd2006-03-29 14:30:49 +0000346 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
347
348 // If mount failed, clean up loop file (if any).
349
Bernhard Reutner-Fischer08421e12006-05-26 14:05:48 +0000350 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000351 del_loop(mp->mnt_fsname);
Rob Landleyfe908fd2006-03-29 14:30:49 +0000352 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000353 free(loopFile);
354 free(mp->mnt_fsname);
355 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000356 }
357 return rc;
358}
359
360
361// Parse options, if necessary parse fstab/mtab, and call singlemount for
362// each directory to be mounted.
363
364int mount_main(int argc, char **argv)
365{
366 char *cmdopts = bb_xstrdup(""), *fstabname, *fstype=0, *storage_path=0;
367 FILE *fstab;
368 int i, opt, all = FALSE, rc = 1;
369 struct mntent mtpair[2], *mtcur = mtpair;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000370
Rob Landley6a6798b2005-08-10 20:35:54 +0000371 /* parse long options, like --bind and --move. Note that -o option
372 * and --option are synonymous. Yes, this means --remount,rw works. */
373
Rob Landleydc0955b2006-03-14 18:16:25 +0000374 for (i = opt = 0; i < argc; i++) {
375 if (argv[i][0] == '-' && argv[i][1] == '-') {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000376 append_mount_options(&cmdopts,argv[i]+2);
Rob Landleydc0955b2006-03-14 18:16:25 +0000377 } else argv[opt++] = argv[i];
Rob Landley6a6798b2005-08-10 20:35:54 +0000378 }
379 argc = opt;
380
381 // Parse remaining options
382
Rob Landleydc0955b2006-03-14 18:16:25 +0000383 while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) {
Matt Kraaia3045df2001-04-17 04:48:51 +0000384 switch (opt) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000385 case 'o':
386 append_mount_options(&cmdopts, optarg);
387 break;
388 case 't':
389 fstype = optarg;
390 break;
391 case 'r':
392 append_mount_options(&cmdopts, "ro");
393 break;
394 case 'w':
395 append_mount_options(&cmdopts, "rw");
396 break;
397 case 'a':
398 all = TRUE;
399 break;
400 case 'n':
401 USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;)
402 break;
403 case 'f':
404 USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;)
405 break;
406 case 'v':
407 break; // ignore -v
408 default:
409 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 }
Matt Kraaia3045df2001-04-17 04:48:51 +0000411 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000412
Rob Landleydc0955b2006-03-14 18:16:25 +0000413 // Three or more non-option arguments? Die with a usage message.
414
415 if (optind-argc>2) bb_show_usage();
Eric Andersen9601a1c2006-03-20 18:07:50 +0000416
Rob Landley6a6798b2005-08-10 20:35:54 +0000417 // If we have no arguments, show currently mounted filesystems
418
Rob Landleydc0955b2006-03-14 18:16:25 +0000419 if (optind == argc) {
420 if (!all) {
421 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
Rob Landley6a6798b2005-08-10 20:35:54 +0000422
Rob Landleydc0955b2006-03-14 18:16:25 +0000423 if(!mountTable) bb_error_msg_and_die("No %s",bb_path_mtab_file);
Rob Landley6a6798b2005-08-10 20:35:54 +0000424
Rob Landleydc0955b2006-03-14 18:16:25 +0000425 while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
426 sizeof(bb_common_bufsiz1)))
427 {
428 // Don't show rootfs.
429 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000430
Rob Landleydc0955b2006-03-14 18:16:25 +0000431 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
432 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
433 mtpair->mnt_dir, mtpair->mnt_type,
434 mtpair->mnt_opts);
435 }
436 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
437 return EXIT_SUCCESS;
Rob Landley6a6798b2005-08-10 20:35:54 +0000438 }
Glenn L McGrath8042f652002-08-23 06:17:46 +0000439 }
Matt Kraai12400822001-04-17 04:32:50 +0000440
Rob Landleydc0955b2006-03-14 18:16:25 +0000441 // When we have two arguments, the second is the directory and we can
442 // skip looking at fstab entirely. We can always abspath() the directory
443 // argument when we get it.
Rob Landley6a6798b2005-08-10 20:35:54 +0000444
Rob Landleydc0955b2006-03-14 18:16:25 +0000445 if (optind+2 == argc) {
446 mtpair->mnt_fsname = argv[optind];
447 mtpair->mnt_dir = argv[optind+1];
448 mtpair->mnt_type = fstype;
449 mtpair->mnt_opts = cmdopts;
450 rc = singlemount(mtpair);
451 goto clean_up;
452 }
Rob Landley6a6798b2005-08-10 20:35:54 +0000453
Rob Landleydc0955b2006-03-14 18:16:25 +0000454 // If we have at least one argument, it's the storage location
Eric Andersen9601a1c2006-03-20 18:07:50 +0000455
Rob Landleydc0955b2006-03-14 18:16:25 +0000456 if (optind < argc) storage_path = bb_simplify_path(argv[optind]);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000457
Rob Landleydc0955b2006-03-14 18:16:25 +0000458 // Open either fstab or mtab
Rob Landley6a6798b2005-08-10 20:35:54 +0000459
Rob Landleydc0955b2006-03-14 18:16:25 +0000460 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
461 fstabname = (char *)bb_path_mtab_file; // Again with the evil const.
462 else fstabname="/etc/fstab";
Eric Andersen9601a1c2006-03-20 18:07:50 +0000463
Rob Landleydc0955b2006-03-14 18:16:25 +0000464 if (!(fstab=setmntent(fstabname,"r")))
465 bb_perror_msg_and_die("Cannot read %s",fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000466
Rob Landleydc0955b2006-03-14 18:16:25 +0000467 // Loop through entries until we find what we're looking for.
Rob Landley6a6798b2005-08-10 20:35:54 +0000468
Rob Landleydc0955b2006-03-14 18:16:25 +0000469 memset(mtpair,0,sizeof(mtpair));
470 for (;;) {
471 struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0);
Rob Landley6a6798b2005-08-10 20:35:54 +0000472
473 // Get next fstab entry
474
Rob Landley20fef962006-04-01 17:32:52 +0000475 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
476 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
477 sizeof(bb_common_bufsiz1)/2))
Rob Landleydc0955b2006-03-14 18:16:25 +0000478 {
479 // Were we looking for something specific?
480
481 if (optind != argc) {
482
483 // If we didn't find anything, complain.
484
485 if (!mtnext->mnt_fsname)
486 bb_error_msg_and_die("Can't find %s in %s",
487 argv[optind], fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000488
Rob Landleydc0955b2006-03-14 18:16:25 +0000489 // Mount the last thing we found.
490
491 mtcur = mtnext;
492 mtcur->mnt_opts=bb_xstrdup(mtcur->mnt_opts);
493 append_mount_options(&(mtcur->mnt_opts),cmdopts);
494 rc = singlemount(mtcur);
495 free(mtcur->mnt_opts);
496 }
Rob Landley6a6798b2005-08-10 20:35:54 +0000497 break;
498 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000499
Rob Landleydc0955b2006-03-14 18:16:25 +0000500 /* If we're trying to mount something specific and this isn't it,
501 * skip it. Note we must match both the exact text in fstab (ala
502 * "proc") or a full path from root */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000503
Rob Landleydc0955b2006-03-14 18:16:25 +0000504 if (optind != argc) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000505
Rob Landleydc0955b2006-03-14 18:16:25 +0000506 // Is this what we're looking for?
Rob Landley6a6798b2005-08-10 20:35:54 +0000507
Rob Landleydc0955b2006-03-14 18:16:25 +0000508 if(strcmp(argv[optind],mtcur->mnt_fsname) &&
509 strcmp(storage_path,mtcur->mnt_fsname) &&
510 strcmp(argv[optind],mtcur->mnt_dir) &&
511 strcmp(storage_path,mtcur->mnt_dir)) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000512
Rob Landleydc0955b2006-03-14 18:16:25 +0000513 // Remember this entry. Something later may have overmounted
514 // it, and we want the _last_ match.
Rob Landley6a6798b2005-08-10 20:35:54 +0000515
Rob Landleydc0955b2006-03-14 18:16:25 +0000516 mtcur = mtnext;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000517
Rob Landleydc0955b2006-03-14 18:16:25 +0000518 // If we're mounting all.
Rob Landley6a6798b2005-08-10 20:35:54 +0000519
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000520 } else {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000521
Rob Landleydc0955b2006-03-14 18:16:25 +0000522 // Do we need to match a filesystem type?
523 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000524
Rob Landleydc0955b2006-03-14 18:16:25 +0000525 // Skip noauto and swap anyway.
526
527 if (parse_mount_options(mtcur->mnt_opts,0)
528 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
529
530 // Mount this thing.
531
Rob Landley49159c72006-05-05 15:01:38 +0000532 if (singlemount(mtcur)) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000533 // Don't whine about already mounted fs when mounting all.
Rob Landley49159c72006-05-05 15:01:38 +0000534 // Note: we should probably change return value to indicate
535 // failure, without causing a duplicate error message.
536 if (errno != EBUSY) bb_perror_msg("Mounting %s on %s failed",
537 mtcur->mnt_fsname, mtcur->mnt_dir);
538 rc = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000539 }
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000540 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000541 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000542 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000543
Rob Landleydc0955b2006-03-14 18:16:25 +0000544clean_up:
545
546 if (ENABLE_FEATURE_CLEAN_UP) {
547 free(storage_path);
548 free(cmdopts);
Rob Landley7b363fd2005-12-20 17:18:01 +0000549 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000550
Rob Landleydc0955b2006-03-14 18:16:25 +0000551 if(rc)
552 bb_perror_msg("Mounting %s on %s failed",
553 mtcur->mnt_fsname, mtcur->mnt_dir);
Rob Landley6a6798b2005-08-10 20:35:54 +0000554
Rob Landley6e985212005-08-14 18:46:34 +0000555 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000556}