blob: f0b4d3831ba962e6655269e9f5ddbecdfc8c514d [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
16/* Design notes: There is no spec for this. Remind me to write one.
17
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
Matt Kraai34251112001-05-02 21:17:38 +000025#include <limits.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include <stdlib.h>
27#include <unistd.h>
28#include <errno.h>
29#include <string.h>
30#include <stdio.h>
31#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000032#include <ctype.h>
Rob Landley6a6798b2005-08-10 20:35:54 +000033#include <sys/mount.h>
34#include <fcntl.h> // for CONFIG_FEATURE_MOUNT_LOOP
35#include <sys/ioctl.h> // for CONFIG_FEATURE_MOUNT_LOOP
Eric Andersencbe31da2001-02-20 06:14:08 +000036#include "busybox.h"
Eric Andersenbd22ed82000-07-08 18:55:24 +000037
Rob Landley6a6798b2005-08-10 20:35:54 +000038// These two aren't always defined in old headers
39#ifndef MS_BIND
40#define MS_BIND 4096
41#endif
42#ifndef MS_MOVE
43#define MS_MOVE 8192
Eric Andersend9fe9582003-07-22 08:25:37 +000044#endif
Rob Landleydc0955b2006-03-14 18:16:25 +000045#ifndef MS_SILENT
46#define MS_SILENT 32768
47#endif
Eric Andersend9fe9582003-07-22 08:25:37 +000048
Rob Landleydc0955b2006-03-14 18:16:25 +000049// Not real flags, but we want to be able to check for this.
50#define MOUNT_NOAUTO (1<<29)
51#define MOUNT_SWAP (1<<30)
52/* Standard mount options (from -o options or --options), with corresponding
53 * flags */
Eric Andersencc8ed391999-10-05 16:24:54 +000054
Rob Landley6a6798b2005-08-10 20:35:54 +000055struct {
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 const char *name;
Rob Landley6a6798b2005-08-10 20:35:54 +000057 long flags;
Rob Landleydc0955b2006-03-14 18:16:25 +000058} static const mount_options[] = {
59 // NOP flags.
60
Rob Landley6a6798b2005-08-10 20:35:54 +000061 {"loop", 0},
62 {"defaults", 0},
Rob Landleydc0955b2006-03-14 18:16:25 +000063 {"quiet", 0},
64
Eric Andersen9601a1c2006-03-20 18:07:50 +000065 // vfs flags
Rob Landleydc0955b2006-03-14 18:16:25 +000066
Rob Landley6a6798b2005-08-10 20:35:54 +000067 {"ro", MS_RDONLY},
68 {"rw", ~MS_RDONLY},
69 {"nosuid", MS_NOSUID},
70 {"suid", ~MS_NOSUID},
71 {"dev", ~MS_NODEV},
72 {"nodev", MS_NODEV},
73 {"exec", ~MS_NOEXEC},
74 {"noexec", MS_NOEXEC},
75 {"sync", MS_SYNCHRONOUS},
76 {"async", ~MS_SYNCHRONOUS},
Rob Landley8b0efdb2006-01-10 02:37:20 +000077 {"atime", ~MS_NOATIME},
Rob Landley6a6798b2005-08-10 20:35:54 +000078 {"noatime", MS_NOATIME},
Rob Landley8b0efdb2006-01-10 02:37:20 +000079 {"diratime", ~MS_NODIRATIME},
Rob Landley6a6798b2005-08-10 20:35:54 +000080 {"nodiratime", MS_NODIRATIME},
Rob Landleydc0955b2006-03-14 18:16:25 +000081 {"loud", ~MS_SILENT},
Eric Andersen9601a1c2006-03-20 18:07:50 +000082
Rob Landleydc0955b2006-03-14 18:16:25 +000083 // action flags
84
85 {"remount", MS_REMOUNT},
Rob Landley6a6798b2005-08-10 20:35:54 +000086 {"bind", MS_BIND},
Rob Landleydc0955b2006-03-14 18:16:25 +000087 {"move", MS_MOVE},
88 {"noauto",MOUNT_NOAUTO},
89 {"swap",MOUNT_SWAP}
Eric Andersencc8ed391999-10-05 16:24:54 +000090};
91
Rob Landleydc0955b2006-03-14 18:16:25 +000092/* Append mount options to string */
93static void append_mount_options(char **oldopts, char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +000094{
Rob Landleydc0955b2006-03-14 18:16:25 +000095 if(*oldopts && **oldopts) {
96 char *temp=bb_xasprintf("%s,%s",*oldopts,newopts);
97 free(*oldopts);
98 *oldopts=temp;
99 } else {
100 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
101 *oldopts = bb_xstrdup(newopts);
102 }
103}
104
105/* Use the mount_options list to parse options into flags.
106 * Return list of unrecognized options in *strflags if strflags!=NULL */
107static int parse_mount_options(char *options, char **unrecognized)
108{
109 int flags = MS_SILENT;
110
Rob Landley6a6798b2005-08-10 20:35:54 +0000111 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000112 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000113 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000115
Rob Landleydc0955b2006-03-14 18:16:25 +0000116 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000117
Rob Landley6a6798b2005-08-10 20:35:54 +0000118 // Find this option in mount_options
Rob Landleydc0955b2006-03-14 18:16:25 +0000119 for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
120 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000121 long fl = mount_options[i].flags;
Rob Landleydc0955b2006-03-14 18:16:25 +0000122 if(fl < 0) flags &= fl;
123 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 break;
125 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000127 // If unrecognized not NULL, append unrecognized mount options */
128 if (unrecognized
129 && i == (sizeof(mount_options) / sizeof(*mount_options)))
Eric Andersen9601a1c2006-03-20 18:07:50 +0000130 {
Rob Landley6a6798b2005-08-10 20:35:54 +0000131 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000132 i = *unrecognized ? strlen(*unrecognized) : 0;
133 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000134
Rob Landley6a6798b2005-08-10 20:35:54 +0000135 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000136 if (i) (*unrecognized)[i++] = ',';
137 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000139
Rob Landley6a6798b2005-08-10 20:35:54 +0000140 // Advance to next option, or finish
141 if(comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 *comma = ',';
143 options = ++comma;
Rob Landley6a6798b2005-08-10 20:35:54 +0000144 } else break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000145 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000146
Rob Landleydc0955b2006-03-14 18:16:25 +0000147 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000148}
149
Rob Landleydc0955b2006-03-14 18:16:25 +0000150// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000151
Rob Landleydc0955b2006-03-14 18:16:25 +0000152static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000153{
Rob Landleydc0955b2006-03-14 18:16:25 +0000154 char *fs, *buf,
155 *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
156 llist_t *list = 0;
157 int i;
158 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000159
Rob Landleydc0955b2006-03-14 18:16:25 +0000160 for(i = 0; filesystems[i]; i++) {
161 if(!(f = fopen(filesystems[i], "r"))) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000162
Rob Landleydc0955b2006-03-14 18:16:25 +0000163 for(fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f));
164 free(buf))
165 {
166 if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000167
Rob Landleydc0955b2006-03-14 18:16:25 +0000168 while(isspace(*fs)) fs++;
169 if(*fs=='#' || *fs=='*') continue;
170 if(!*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000171
Rob Landleydc0955b2006-03-14 18:16:25 +0000172 list=llist_add_to_end(list,bb_xstrdup(fs));
173 }
174 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
175 }
176
177 return list;
178}
179
180llist_t *fslist = 0;
181
Rob Landleydc0955b2006-03-14 18:16:25 +0000182#if ENABLE_FEATURE_CLEAN_UP
183static void delete_block_backed_filesystems(void)
184{
185 llist_free(fslist);
186}
187#endif
188
189#if ENABLE_FEATURE_MTAB_SUPPORT
190static int useMtab;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000191static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000192#else
193#define useMtab 0
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000194#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000195#endif
196
197// Perform actual mount of specific filesystem at specific location.
198
199static int mount_it_now(struct mntent *mp, int vfsflags)
200{
201 int rc;
202 char *filteropts = 0;
203
204 parse_mount_options(mp->mnt_opts, &filteropts);
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000205
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000206 if (fakeIt) { return 0; }
207
Rob Landleydc0955b2006-03-14 18:16:25 +0000208 // Mount, with fallback to read-only if necessary.
209
210 for(;;) {
211 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
212 vfsflags, filteropts);
213 if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
214 break;
215 bb_error_msg("%s is write-protected, mounting read-only",
216 mp->mnt_fsname);
217 vfsflags |= MS_RDONLY;
218 }
219
220 free(filteropts);
221
222 // Abort entirely if permission denied.
223
224 if (rc && errno == EPERM)
225 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
226
227 /* If the mount was successful, and we're maintaining an old-style
228 * mtab file by hand, add the new entry to it now. */
Eric Andersen9601a1c2006-03-20 18:07:50 +0000229
Rob Landleydc0955b2006-03-14 18:16:25 +0000230 if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
231 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
232 int i;
233
234 if(!mountTable)
235 bb_error_msg("No %s\n",bb_path_mtab_file);
236
237 // Add vfs string flags
238
239 for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
240 if (mount_options[i].flags > 0)
241 append_mount_options(&(mp->mnt_opts),
242// Shut up about the darn const. It's not important. I don't care.
243 (char *)mount_options[i].name);
244
245 // Remove trailing / (if any) from directory we mounted on
246
247 i = strlen(mp->mnt_dir);
248 if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0;
249
250 // Write and close.
251
252 if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind";
253 addmntent(mountTable, mp);
254 endmntent(mountTable);
255 if (ENABLE_FEATURE_CLEAN_UP)
256 if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0;
257 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000258
Rob Landleydc0955b2006-03-14 18:16:25 +0000259 return rc;
260}
261
262
263// Mount one directory. Handles NFS, loopback, autobind, and filesystem type
264// detection. Returns 0 for success, nonzero for failure.
265
266static int singlemount(struct mntent *mp)
267{
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000268 int rc = 1, vfsflags;
Rob Landleydc0955b2006-03-14 18:16:25 +0000269 char *loopFile = 0;
270 llist_t *fl = 0;
271 struct stat st;
272
273 vfsflags = parse_mount_options(mp->mnt_opts, 0);
274
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000275 // Treat fstype "auto" as unspecified.
276
277 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
278
Rob Landleydc0955b2006-03-14 18:16:25 +0000279 // Might this be an NFS filesystem?
280
281 if (ENABLE_FEATURE_MOUNT_NFS &&
282 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
283 strchr(mp->mnt_fsname, ':') != NULL)
284 {
285 char *options=0;
286 parse_mount_options(mp->mnt_opts, &options);
287 if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &options, 1)) {
288 bb_perror_msg("nfsmount failed");
289 return 1;
290 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000291
Rob Landleydc0955b2006-03-14 18:16:25 +0000292 // Strangely enough, nfsmount() doesn't actually mount() anything.
293
294 else return mount_it_now(mp, vfsflags);
295 }
296
297 // Look at the file. (Not found isn't a failure for remount.)
298
299 if (lstat(mp->mnt_fsname, &st));
300
Eric Andersen9601a1c2006-03-20 18:07:50 +0000301 if (!(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000302 // Do we need to allocate a loopback device for it?
Eric Andersen9601a1c2006-03-20 18:07:50 +0000303
Rob Landleydc0955b2006-03-14 18:16:25 +0000304 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000305 loopFile = bb_simplify_path(mp->mnt_fsname);
Rob Landleydc0955b2006-03-14 18:16:25 +0000306 mp->mnt_fsname = 0;
307 switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) {
308 case 0:
309 case 1:
310 break;
311 default:
312 bb_error_msg( errno == EPERM || errno == EACCES
313 ? bb_msg_perm_denied_are_you_root
314 : "Couldn't setup loop device");
315 return errno;
316 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000317
Rob Landleydc0955b2006-03-14 18:16:25 +0000318 // Autodetect bind mounts
319
320 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) vfsflags |= MS_BIND;
321 }
322
323 /* If we know the fstype (or don't need to), jump straight
324 * to the actual mount. */
325
326 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
327 rc = mount_it_now(mp, vfsflags);
328
329 // Loop through filesystem types until mount succeeds or we run out
330
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000331 else {
Rob Landleydc0955b2006-03-14 18:16:25 +0000332
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000333 /* Initialize list of block backed filesystems. This has to be
334 * done here so that during "mount -a", mounts after /proc shows up
335 * can autodetect. */
Rob Landleydc0955b2006-03-14 18:16:25 +0000336
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000337 if (!fslist) {
338 fslist = get_block_backed_filesystems();
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000339#if ENABLE_FEATURE_CLEAN_UP
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000340 if (ENABLE_FEATURE_CLEAN_UP && fslist)
341 atexit(delete_block_backed_filesystems);
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000342#endif
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000343 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000344
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000345 for (fl = fslist; fl; fl = fl->link) {
346 mp->mnt_type = fl->data;
347
348 if (!(rc = mount_it_now(mp,vfsflags))) break;
349
350 mp->mnt_type = 0;
351 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000352 }
353
354 // Mount failed. Clean up
355 if (rc && loopFile) {
356 del_loop(mp->mnt_fsname);
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000357 if(ENABLE_FEATURE_CLEAN_UP) {
358 free(loopFile);
359 free(mp->mnt_fsname);
360 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000361 }
362 return rc;
363}
364
365
366// 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{
371 char *cmdopts = bb_xstrdup(""), *fstabname, *fstype=0, *storage_path=0;
372 FILE *fstab;
373 int i, opt, all = FALSE, rc = 1;
374 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;
455 rc = singlemount(mtpair);
456 goto clean_up;
457 }
Rob Landley6a6798b2005-08-10 20:35:54 +0000458
Rob Landleydc0955b2006-03-14 18:16:25 +0000459 // If we have at least one argument, it's the storage location
Eric Andersen9601a1c2006-03-20 18:07:50 +0000460
Rob Landleydc0955b2006-03-14 18:16:25 +0000461 if (optind < argc) storage_path = bb_simplify_path(argv[optind]);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000462
Rob Landleydc0955b2006-03-14 18:16:25 +0000463 // Open either fstab or mtab
Rob Landley6a6798b2005-08-10 20:35:54 +0000464
Rob Landleydc0955b2006-03-14 18:16:25 +0000465 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
466 fstabname = (char *)bb_path_mtab_file; // Again with the evil const.
467 else fstabname="/etc/fstab";
Eric Andersen9601a1c2006-03-20 18:07:50 +0000468
Rob Landleydc0955b2006-03-14 18:16:25 +0000469 if (!(fstab=setmntent(fstabname,"r")))
470 bb_perror_msg_and_die("Cannot read %s",fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000471
Rob Landleydc0955b2006-03-14 18:16:25 +0000472 // Loop through entries until we find what we're looking for.
Rob Landley6a6798b2005-08-10 20:35:54 +0000473
Rob Landleydc0955b2006-03-14 18:16:25 +0000474 memset(mtpair,0,sizeof(mtpair));
475 for (;;) {
476 struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0);
Rob Landley6a6798b2005-08-10 20:35:54 +0000477
478 // Get next fstab entry
479
Rob Landleydc0955b2006-03-14 18:16:25 +0000480 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1,
481 sizeof(bb_common_bufsiz1)))
482 {
483 // Were we looking for something specific?
484
485 if (optind != argc) {
486
487 // If we didn't find anything, complain.
488
489 if (!mtnext->mnt_fsname)
490 bb_error_msg_and_die("Can't find %s in %s",
491 argv[optind], fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000492
Rob Landleydc0955b2006-03-14 18:16:25 +0000493 // Mount the last thing we found.
494
495 mtcur = mtnext;
496 mtcur->mnt_opts=bb_xstrdup(mtcur->mnt_opts);
497 append_mount_options(&(mtcur->mnt_opts),cmdopts);
498 rc = singlemount(mtcur);
499 free(mtcur->mnt_opts);
500 }
Rob Landley6a6798b2005-08-10 20:35:54 +0000501 break;
502 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000503
Rob Landleydc0955b2006-03-14 18:16:25 +0000504 /* If we're trying to mount something specific and this isn't it,
505 * skip it. Note we must match both the exact text in fstab (ala
506 * "proc") or a full path from root */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000507
Rob Landleydc0955b2006-03-14 18:16:25 +0000508 if (optind != argc) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000509
Rob Landleydc0955b2006-03-14 18:16:25 +0000510 // Is this what we're looking for?
Rob Landley6a6798b2005-08-10 20:35:54 +0000511
Rob Landleydc0955b2006-03-14 18:16:25 +0000512 if(strcmp(argv[optind],mtcur->mnt_fsname) &&
513 strcmp(storage_path,mtcur->mnt_fsname) &&
514 strcmp(argv[optind],mtcur->mnt_dir) &&
515 strcmp(storage_path,mtcur->mnt_dir)) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000516
Rob Landleydc0955b2006-03-14 18:16:25 +0000517 // Remember this entry. Something later may have overmounted
518 // it, and we want the _last_ match.
Rob Landley6a6798b2005-08-10 20:35:54 +0000519
Rob Landleydc0955b2006-03-14 18:16:25 +0000520 mtcur = mtnext;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000521
Rob Landleydc0955b2006-03-14 18:16:25 +0000522 // If we're mounting all.
Rob Landley6a6798b2005-08-10 20:35:54 +0000523
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000524 } else {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000525
Rob Landleydc0955b2006-03-14 18:16:25 +0000526 // Do we need to match a filesystem type?
527 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000528
Rob Landleydc0955b2006-03-14 18:16:25 +0000529 // Skip noauto and swap anyway.
530
531 if (parse_mount_options(mtcur->mnt_opts,0)
532 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
533
534 // Mount this thing.
535
536 rc = singlemount(mtcur);
537 if (rc) {
538 // Don't whine about already mounted fs when mounting all.
539 if (errno == EBUSY) rc = 0;
540 else break;
541 }
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000542 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000543 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000544 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000545
Rob Landleydc0955b2006-03-14 18:16:25 +0000546clean_up:
547
548 if (ENABLE_FEATURE_CLEAN_UP) {
549 free(storage_path);
550 free(cmdopts);
551 free(fstype);
Rob Landley7b363fd2005-12-20 17:18:01 +0000552 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000553
Rob Landleydc0955b2006-03-14 18:16:25 +0000554 if(rc)
555 bb_perror_msg("Mounting %s on %s failed",
556 mtcur->mnt_fsname, mtcur->mnt_dir);
Rob Landley6a6798b2005-08-10 20:35:54 +0000557
Rob Landley6e985212005-08-14 18:46:34 +0000558 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000559}