mount.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. *
  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.
  27. *
  28. * 1999-10-07 Erik Andersen <andersen@codepoet.org>.
  29. * Rewrite of a lot of code. Removed mtab usage (I plan on
  30. * putting it back as a compile-time option some time),
  31. * major adjustments to option parsing, and some serious
  32. * dieting all around.
  33. *
  34. * 1999-11-06 mtab support is back - andersee
  35. *
  36. * 2000-01-12 Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
  37. * mount to add loop support.
  38. *
  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. *
  45. */
  46. #include <limits.h>
  47. #include <stdlib.h>
  48. #include <unistd.h>
  49. #include <errno.h>
  50. #include <string.h>
  51. #include <stdio.h>
  52. #include <mntent.h>
  53. #include <ctype.h>
  54. #include "busybox.h"
  55. #ifdef CONFIG_NFSMOUNT
  56. #if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_RPC__)
  57. #error "You need to build uClibc with UCLIBC_HAS_RPC for busybox mount with NFS support to compile."
  58. #endif
  59. #endif
  60. enum {
  61. 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 */
  75. MS_MOVE = 8192, /* Use the new linux 2.4.x "mount --move" feature */
  76. };
  77. #if defined CONFIG_FEATURE_MOUNT_LOOP
  78. #include <fcntl.h>
  79. #include <sys/ioctl.h>
  80. static int use_loop = FALSE;
  81. #endif
  82. extern int mount(__const char *__special_file, __const char *__dir,
  83. __const char *__fstype, unsigned long int __rwflag,
  84. __const void *__data);
  85. extern int umount(__const char *__special_file);
  86. extern int umount2(__const char *__special_file, int __flags);
  87. extern int sysfs(int option, unsigned int fs_index, char *buf);
  88. struct mount_options {
  89. const char *name;
  90. unsigned long and;
  91. unsigned long or;
  92. };
  93. static const struct mount_options mount_options[] = {
  94. {"async", ~MS_SYNCHRONOUS, 0},
  95. {"atime", ~0, ~MS_NOATIME},
  96. {"defaults", ~0, 0},
  97. {"noauto", ~0, 0},
  98. {"dev", ~MS_NODEV, 0},
  99. {"diratime", ~0, ~MS_NODIRATIME},
  100. {"exec", ~MS_NOEXEC, 0},
  101. {"noatime", ~0, MS_NOATIME},
  102. {"nodev", ~0, MS_NODEV},
  103. {"nodiratime", ~0, MS_NODIRATIME},
  104. {"noexec", ~0, MS_NOEXEC},
  105. {"nosuid", ~0, MS_NOSUID},
  106. {"remount", ~0, MS_REMOUNT},
  107. {"ro", ~0, MS_RDONLY},
  108. {"rw", ~MS_RDONLY, 0},
  109. {"suid", ~MS_NOSUID, 0},
  110. {"sync", ~0, MS_SYNCHRONOUS},
  111. {"bind", ~0, MS_BIND},
  112. {"move", ~0, MS_MOVE},
  113. {0, 0, 0}
  114. };
  115. static int
  116. do_mount(char *specialfile, char *dir, char *filesystemtype, long flags,
  117. void *string_flags, int useMtab, int fakeIt, char *mtab_opts,
  118. int mount_all)
  119. {
  120. int status = 0;
  121. #if defined CONFIG_FEATURE_MOUNT_LOOP
  122. char *lofile = NULL;
  123. #endif
  124. if (!fakeIt) {
  125. #if defined CONFIG_FEATURE_MOUNT_LOOP
  126. if (use_loop == TRUE) {
  127. int loro = flags & MS_RDONLY;
  128. lofile = specialfile;
  129. specialfile = find_unused_loop_device();
  130. if (specialfile == NULL) {
  131. bb_error_msg_and_die("Could not find a spare loop device");
  132. }
  133. if (set_loop(specialfile, lofile, 0, &loro)) {
  134. bb_error_msg_and_die("Could not setup loop device");
  135. }
  136. if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */
  137. bb_error_msg("WARNING: loop device is read-only");
  138. flags |= MS_RDONLY;
  139. }
  140. }
  141. #endif
  142. status = mount(specialfile, dir, filesystemtype, flags, string_flags);
  143. if (status < 0 && errno == EROFS) {
  144. bb_error_msg("%s is write-protected, mounting read-only",
  145. specialfile);
  146. status = mount(specialfile, dir, filesystemtype, flags |=
  147. MS_RDONLY, string_flags);
  148. }
  149. /* Don't whine about already mounted filesystems when mounting all. */
  150. if (status < 0 && errno == EBUSY && mount_all) {
  151. return TRUE;
  152. }
  153. }
  154. /* If the mount was sucessful, do anything needed, then return TRUE */
  155. if (status == 0 || fakeIt == TRUE) {
  156. #if defined CONFIG_FEATURE_MTAB_SUPPORT
  157. if (useMtab) {
  158. erase_mtab(specialfile); /* Clean any stale entries */
  159. write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts);
  160. }
  161. #endif
  162. return (TRUE);
  163. }
  164. /* Bummer. mount failed. Clean up */
  165. #if defined CONFIG_FEATURE_MOUNT_LOOP
  166. if (lofile != NULL) {
  167. del_loop(specialfile);
  168. }
  169. #endif
  170. if (errno == EPERM) {
  171. bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
  172. }
  173. return (FALSE);
  174. }
  175. static void paste_str(char **s1, const char *s2)
  176. {
  177. *s1 = xrealloc(*s1, strlen(*s1) + strlen(s2) + 1);
  178. strcat(*s1, s2);
  179. }
  180. /* Seperate standard mount options from the nonstandard string options */
  181. static void parse_mount_options(char *options, int *flags, char **strflags)
  182. {
  183. while (options) {
  184. int gotone = FALSE;
  185. char *comma = strchr(options, ',');
  186. const struct mount_options *f = mount_options;
  187. if (comma) {
  188. *comma = '\0';
  189. }
  190. while (f->name != 0) {
  191. if (strcasecmp(f->name, options) == 0) {
  192. *flags &= f->and;
  193. *flags |= f->or;
  194. gotone = TRUE;
  195. break;
  196. }
  197. f++;
  198. }
  199. #if defined CONFIG_FEATURE_MOUNT_LOOP
  200. if (!strcasecmp("loop", options)) { /* loop device support */
  201. use_loop = TRUE;
  202. gotone = TRUE;
  203. }
  204. #endif
  205. if (!gotone) {
  206. if (**strflags) {
  207. /* have previous parsed options */
  208. paste_str(strflags, ",");
  209. }
  210. paste_str(strflags, options);
  211. }
  212. if (comma) {
  213. *comma = ',';
  214. options = ++comma;
  215. } else {
  216. break;
  217. }
  218. }
  219. }
  220. static int mount_one(char *blockDevice, char *directory, char *filesystemType,
  221. unsigned long flags, char *string_flags, int useMtab,
  222. int fakeIt, char *mtab_opts, int whineOnErrors,
  223. int mount_all)
  224. {
  225. int status = 0;
  226. if (strcmp(filesystemType, "auto") == 0) {
  227. char buf[255];
  228. FILE *f;
  229. int read_proc = 0;
  230. f = fopen("/etc/filesystems", "r");
  231. if (f) {
  232. while (fgets(buf, sizeof(buf), f)) {
  233. if (*buf == '*') {
  234. read_proc = 1;
  235. } else if (*buf == '#') {
  236. continue;
  237. } else {
  238. filesystemType = buf;
  239. /* Add NULL termination to each line */
  240. while (*filesystemType && !isspace(*filesystemType)) {
  241. filesystemType++;
  242. }
  243. *filesystemType = '\0';
  244. filesystemType = buf;
  245. if (bb_strlen(filesystemType)) {
  246. status = do_mount(blockDevice, directory, filesystemType,
  247. flags | MS_MGC_VAL, string_flags,
  248. useMtab, fakeIt, mtab_opts, mount_all);
  249. if (status) {
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. fclose(f);
  256. } else {
  257. read_proc = 1;
  258. }
  259. if (read_proc && !status) {
  260. f = bb_xfopen("/proc/filesystems", "r");
  261. while (fgets(buf, sizeof(buf), f) != NULL) {
  262. filesystemType = buf;
  263. if (*filesystemType == '\t') { /* Not a nodev filesystem */
  264. /* Add NULL termination to each line */
  265. while (*filesystemType && *filesystemType != '\n') {
  266. filesystemType++;
  267. }
  268. *filesystemType = '\0';
  269. filesystemType = buf;
  270. filesystemType++; /* hop past tab */
  271. status = do_mount(blockDevice, directory, filesystemType,
  272. flags | MS_MGC_VAL, string_flags, useMtab,
  273. fakeIt, mtab_opts, mount_all);
  274. if (status) {
  275. break;
  276. }
  277. }
  278. }
  279. fclose(f);
  280. }
  281. } else {
  282. status = do_mount(blockDevice, directory, filesystemType,
  283. flags | MS_MGC_VAL, string_flags, useMtab, fakeIt,
  284. mtab_opts, mount_all);
  285. }
  286. if (!status) {
  287. if (whineOnErrors) {
  288. bb_perror_msg("Mounting %s on %s failed", blockDevice, directory);
  289. }
  290. return (FALSE);
  291. }
  292. return (TRUE);
  293. }
  294. static void show_mounts(char *onlytype)
  295. {
  296. FILE *mountTable = setmntent(bb_path_mtab_file, "r");
  297. if (mountTable) {
  298. struct mntent *m;
  299. while ((m = getmntent(mountTable)) != 0) {
  300. char *blockDevice = m->mnt_fsname;
  301. if (strcmp(blockDevice, "rootfs") == 0) {
  302. continue;
  303. } else if (strcmp(blockDevice, "/dev/root") == 0) {
  304. blockDevice = find_real_root_device_name();
  305. }
  306. if (!onlytype || (strcmp(m->mnt_type, onlytype) == 0)) {
  307. printf("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
  308. m->mnt_type, m->mnt_opts);
  309. }
  310. #ifdef CONFIG_FEATURE_CLEAN_UP
  311. if (blockDevice != m->mnt_fsname) {
  312. free(blockDevice);
  313. }
  314. #endif
  315. }
  316. endmntent(mountTable);
  317. } else {
  318. bb_perror_msg_and_die(bb_path_mtab_file);
  319. }
  320. exit(EXIT_SUCCESS);
  321. }
  322. extern int mount_main(int argc, char **argv)
  323. {
  324. struct stat statbuf;
  325. char *string_flags = bb_xstrdup("");
  326. char *extra_opts;
  327. int flags = 0;
  328. char *filesystemType = "auto";
  329. int got_filesystemType = 0;
  330. char *device = xmalloc(PATH_MAX);
  331. char *directory = xmalloc(PATH_MAX);
  332. struct mntent *m = NULL;
  333. int all = FALSE;
  334. int fakeIt = FALSE;
  335. int useMtab = TRUE;
  336. int rc = EXIT_FAILURE;
  337. FILE *f = 0;
  338. int opt;
  339. /* Parse options */
  340. while ((opt = getopt(argc, argv, "o:rt:wafnv")) > 0) {
  341. switch (opt) {
  342. case 'o':
  343. parse_mount_options(optarg, &flags, &string_flags);
  344. break;
  345. case 'r':
  346. flags |= MS_RDONLY;
  347. break;
  348. case 't':
  349. filesystemType = optarg;
  350. got_filesystemType = 1;
  351. break;
  352. case 'w':
  353. flags &= ~MS_RDONLY;
  354. break;
  355. case 'a':
  356. all = TRUE;
  357. break;
  358. case 'f':
  359. fakeIt = TRUE;
  360. break;
  361. case 'n':
  362. #ifdef CONFIG_FEATURE_MTAB_SUPPORT
  363. useMtab = FALSE;
  364. #endif
  365. break;
  366. case 'v':
  367. break; /* ignore -v */
  368. }
  369. }
  370. if (!all && (optind == argc)) {
  371. show_mounts(got_filesystemType ? filesystemType : NULL);
  372. }
  373. if (optind < argc) {
  374. /* if device is a filename get its real path */
  375. if (stat(argv[optind], &statbuf) == 0) {
  376. char *tmp = bb_simplify_path(argv[optind]);
  377. safe_strncpy(device, tmp, PATH_MAX);
  378. } else {
  379. safe_strncpy(device, argv[optind], PATH_MAX);
  380. }
  381. }
  382. if (optind + 1 < argc)
  383. directory = bb_simplify_path(argv[optind + 1]);
  384. if (all || optind + 1 == argc) {
  385. f = setmntent("/etc/fstab", "r");
  386. if (f == NULL)
  387. bb_perror_msg_and_die("\nCannot read /etc/fstab");
  388. while ((m = getmntent(f)) != NULL) {
  389. if (!all && (optind + 1 == argc)
  390. && ((strcmp(device, m->mnt_fsname) != 0)
  391. && (strcmp(device, m->mnt_dir) != 0))) {
  392. continue;
  393. }
  394. if (all && ( /* If we're mounting 'all' */
  395. (strstr(m->mnt_opts, "noauto")) || /* and the file system isn't noauto, */
  396. (strstr(m->mnt_type, "swap")))) /* and isn't swap, then mount it */
  397. {
  398. continue;
  399. }
  400. if (all || flags == 0) { /* Allow single mount to override fstab flags */
  401. flags = 0;
  402. string_flags[0] = 0;
  403. parse_mount_options(m->mnt_opts, &flags, &string_flags);
  404. }
  405. strcpy(device, m->mnt_fsname);
  406. strcpy(directory, m->mnt_dir);
  407. filesystemType = bb_xstrdup(m->mnt_type);
  408. singlemount:
  409. extra_opts = string_flags;
  410. rc = EXIT_SUCCESS;
  411. #ifdef CONFIG_NFSMOUNT
  412. if (strchr(device, ':') != NULL) {
  413. filesystemType = "nfs";
  414. if (nfsmount
  415. (device, directory, &flags, &extra_opts, &string_flags,
  416. 1)) {
  417. bb_perror_msg("nfsmount failed");
  418. rc = EXIT_FAILURE;
  419. }
  420. }
  421. #endif
  422. if (!mount_one
  423. (device, directory, filesystemType, flags, string_flags,
  424. useMtab, fakeIt, extra_opts, TRUE, all)) {
  425. rc = EXIT_FAILURE;
  426. }
  427. if (!all) {
  428. break;
  429. }
  430. }
  431. if (f) {
  432. endmntent(f);
  433. }
  434. if (!all && f && m == NULL) {
  435. fprintf(stderr, "Can't find %s in /etc/fstab\n", device);
  436. }
  437. return rc;
  438. }
  439. goto singlemount;
  440. }