source: MondoRescue/trunk/mindi-busybox/util-linux/mount.c@ 929

Last change on this file since 929 was 904, checked in by Bruno Cornec, 17 years ago

merge -r890:902 $SVN_M/branches/stable

File size: 13.5 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini mount implementation for busybox
4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net>
8 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 */
11
12/* todo:
13 * bb_getopt_ulflags();
14 */
15
16/* Design notes: There is no spec for mount. Remind me to write one.
17
18 mount_main() calls singlemount() which calls mount_it_now().
19
20 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
25#include "busybox.h"
26#include <unistd.h>
27#include <errno.h>
28#include <string.h>
29#include <mntent.h>
30#include <ctype.h>
31#include <fcntl.h> // for CONFIG_FEATURE_MOUNT_LOOP
32#include <sys/ioctl.h> // for CONFIG_FEATURE_MOUNT_LOOP
33
34// 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
40#endif
41#ifndef MS_SILENT
42#define MS_SILENT 32768
43#endif
44
45// 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 */
50
51struct {
52 const char *name;
53 long flags;
54} static const mount_options[] = {
55 // NOP flags.
56
57 {"loop", 0},
58 {"defaults", 0},
59 {"quiet", 0},
60
61 // vfs flags
62
63 {"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},
73 {"atime", ~MS_NOATIME},
74 {"noatime", MS_NOATIME},
75 {"diratime", ~MS_NODIRATIME},
76 {"nodiratime", MS_NODIRATIME},
77 {"loud", ~MS_SILENT},
78
79 // action flags
80
81 {"remount", MS_REMOUNT},
82 {"bind", MS_BIND},
83 {"move", MS_MOVE},
84 {"noauto",MOUNT_NOAUTO},
85 {"swap",MOUNT_SWAP}
86};
87
88/* Append mount options to string */
89static void append_mount_options(char **oldopts, char *newopts)
90{
91 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
107 // Loop through options
108 for (;;) {
109 int i;
110 char *comma = strchr(options, ',');
111
112 if (comma) *comma = 0;
113
114 // Find this option in mount_options
115 for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
116 if (!strcasecmp(mount_options[i].name, options)) {
117 long fl = mount_options[i].flags;
118 if(fl < 0) flags &= fl;
119 else flags |= fl;
120 break;
121 }
122 }
123 // If unrecognized not NULL, append unrecognized mount options */
124 if (unrecognized
125 && i == (sizeof(mount_options) / sizeof(*mount_options)))
126 {
127 // Add it to strflags, to pass on to kernel
128 i = *unrecognized ? strlen(*unrecognized) : 0;
129 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
130
131 // Comma separated if it's not the first one
132 if (i) (*unrecognized)[i++] = ',';
133 strcpy((*unrecognized)+i, options);
134 }
135
136 // Advance to next option, or finish
137 if(comma) {
138 *comma = ',';
139 options = ++comma;
140 } else break;
141 }
142
143 return flags;
144}
145
146// Return a list of all block device backed filesystems
147
148static llist_t *get_block_backed_filesystems(void)
149{
150 char *fs, *buf,
151 *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
152 llist_t *list = 0;
153 int i;
154 FILE *f;
155
156 for(i = 0; filesystems[i]; i++) {
157 if(!(f = fopen(filesystems[i], "r"))) continue;
158
159 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;
163
164 while(isspace(*fs)) fs++;
165 if(*fs=='#' || *fs=='*') continue;
166 if(!*fs) continue;
167
168 llist_add_to_end(&list,bb_xstrdup(fs));
169 }
170 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
171 }
172
173 return list;
174}
175
176llist_t *fslist = 0;
177
178#if ENABLE_FEATURE_CLEAN_UP
179static void delete_block_backed_filesystems(void)
180{
181 llist_free(fslist, free);
182}
183#else
184void delete_block_backed_filesystems(void);
185#endif
186
187#if ENABLE_FEATURE_MTAB_SUPPORT
188static int useMtab;
189static int fakeIt;
190#else
191#define useMtab 0
192#define fakeIt 0
193#endif
194
195// Perform actual mount of specific filesystem at specific location.
196
197static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
198{
199 int rc;
200
201 if (fakeIt) { return 0; }
202
203 // 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
215 // 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. */
222
223 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 }
251
252 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, int ignore_busy)
260{
261 int rc = -1, vfsflags;
262 char *loopFile = 0, *filteropts = 0;
263 llist_t *fl = 0;
264 struct stat st;
265
266 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
267
268 // Treat fstype "auto" as unspecified.
269
270 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
271
272 // 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 {
278 if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &filteropts, 1)) {
279 bb_perror_msg("nfsmount failed");
280 goto report_error;
281 } else {
282 // Strangely enough, nfsmount() doesn't actually mount() anything.
283 mp->mnt_type = "nfs";
284 rc = mount_it_now(mp, vfsflags, filteropts);
285 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
286
287 goto report_error;
288 }
289 }
290
291 // Look at the file. (Not found isn't a failure for remount, or for
292 // a synthetic filesystem like proc or sysfs.)
293
294 if (stat(mp->mnt_fsname, &st));
295 else if (!(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) {
296 // Do we need to allocate a loopback device for it?
297
298 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
299 loopFile = bb_simplify_path(mp->mnt_fsname);
300 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 }
311
312 // 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)))
321 rc = mount_it_now(mp, vfsflags, filteropts);
322
323 // Loop through filesystem types until mount succeeds or we run out
324
325 else {
326
327 /* 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. */
330
331 if (!fslist) {
332 fslist = get_block_backed_filesystems();
333 if (ENABLE_FEATURE_CLEAN_UP && fslist)
334 atexit(delete_block_backed_filesystems);
335 }
336
337 for (fl = fslist; fl; fl = fl->link) {
338 mp->mnt_type = fl->data;
339
340 if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break;
341
342 mp->mnt_type = 0;
343 }
344 }
345
346 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
347
348 // If mount failed, clean up loop file (if any).
349
350 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
351 del_loop(mp->mnt_fsname);
352 if (ENABLE_FEATURE_CLEAN_UP) {
353 free(loopFile);
354 free(mp->mnt_fsname);
355 }
356 }
357report_error:
358 if (rc && errno == EBUSY && ignore_busy) rc = 0;
359 if (rc < 0)
360 bb_perror_msg("Mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
361
362 return rc;
363}
364
365// Parse options, if necessary parse fstab/mtab, and call singlemount for
366// each directory to be mounted.
367
368int mount_main(int argc, char **argv)
369{
370 char *cmdopts = bb_xstrdup(""), *fstabname, *fstype=0, *storage_path=0;
371 FILE *fstab;
372 int i, opt, all = FALSE, rc = 0;
373 struct mntent mtpair[2], *mtcur = mtpair;
374
375 /* parse long options, like --bind and --move. Note that -o option
376 * and --option are synonymous. Yes, this means --remount,rw works. */
377
378 for (i = opt = 0; i < argc; i++) {
379 if (argv[i][0] == '-' && argv[i][1] == '-') {
380 append_mount_options(&cmdopts,argv[i]+2);
381 } else argv[opt++] = argv[i];
382 }
383 argc = opt;
384
385 // Parse remaining options
386
387 while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) {
388 switch (opt) {
389 case 'o':
390 append_mount_options(&cmdopts, optarg);
391 break;
392 case 't':
393 fstype = optarg;
394 break;
395 case 'r':
396 append_mount_options(&cmdopts, "ro");
397 break;
398 case 'w':
399 append_mount_options(&cmdopts, "rw");
400 break;
401 case 'a':
402 all = TRUE;
403 break;
404 case 'n':
405 USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;)
406 break;
407 case 'f':
408 USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;)
409 break;
410 case 'v':
411 break; // ignore -v
412 default:
413 bb_show_usage();
414 }
415 }
416
417 // Three or more non-option arguments? Die with a usage message.
418
419 if (optind-argc>2) bb_show_usage();
420
421 // If we have no arguments, show currently mounted filesystems
422
423 if (optind == argc) {
424 if (!all) {
425 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
426
427 if(!mountTable) bb_error_msg_and_die("No %s",bb_path_mtab_file);
428
429 while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
430 sizeof(bb_common_bufsiz1)))
431 {
432 // Don't show rootfs.
433 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
434
435 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
436 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
437 mtpair->mnt_dir, mtpair->mnt_type,
438 mtpair->mnt_opts);
439 }
440 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
441 return EXIT_SUCCESS;
442 }
443 }
444
445 // When we have two arguments, the second is the directory and we can
446 // skip looking at fstab entirely. We can always abspath() the directory
447 // argument when we get it.
448
449 if (optind+2 == argc) {
450 mtpair->mnt_fsname = argv[optind];
451 mtpair->mnt_dir = argv[optind+1];
452 mtpair->mnt_type = fstype;
453 mtpair->mnt_opts = cmdopts;
454 rc = singlemount(mtpair, 0);
455 goto clean_up;
456 }
457
458 // If we have at least one argument, it's the storage location
459
460 if (optind < argc) storage_path = bb_simplify_path(argv[optind]);
461
462 // Open either fstab or mtab
463
464 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
465 fstabname = (char *)bb_path_mtab_file; // Again with the evil const.
466 else fstabname="/etc/fstab";
467
468 if (!(fstab=setmntent(fstabname,"r")))
469 bb_perror_msg_and_die("Cannot read %s",fstabname);
470
471 // Loop through entries until we find what we're looking for.
472
473 memset(mtpair,0,sizeof(mtpair));
474 for (;;) {
475 struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0);
476
477 // Get next fstab entry
478
479 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
480 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
481 sizeof(bb_common_bufsiz1)/2))
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);
492
493 // 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, 0);
499 free(mtcur->mnt_opts);
500 }
501 goto clean_up;
502 }
503
504 /* 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 */
507
508 if (optind != argc) {
509
510 // Is this what we're looking for?
511
512 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;
516
517 // Remember this entry. Something later may have overmounted
518 // it, and we want the _last_ match.
519
520 mtcur = mtnext;
521
522 // If we're mounting all.
523
524 } else {
525
526 // Do we need to match a filesystem type?
527 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
528
529 // 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 if (singlemount(mtcur, 1)) {
537 /* Count number of failed mounts */
538 rc++;
539 }
540 }
541 }
542 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
543
544clean_up:
545
546 if (ENABLE_FEATURE_CLEAN_UP) {
547 free(storage_path);
548 free(cmdopts);
549 }
550
551 return rc;
552}
Note: See TracBrowser for help on using the repository browser.