mount.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* mount.c - mount filesystems
  2. *
  3. * Copyright 2014 Rob Landley <rob@landley.net>
  4. *
  5. * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mount.html
  6. *
  7. * Note: -hV is bad spec, haven't implemented -FsLU yet
  8. * no mtab (/proc/mounts does it) so -n is NOP.
  9. * TODO mount -o loop,autoclear (linux git 96c5865559ce)
  10. USE_MOUNT(NEWTOY(mount, "?O:afnrvwt:o*[-rw]", TOYFLAG_BIN|TOYFLAG_STAYROOT))
  11. //USE_NFSMOUNT(NEWTOY(nfsmount, "?<2>2", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
  12. config MOUNT
  13. bool "mount"
  14. default y
  15. help
  16. usage: mount [-afFrsvw] [-t TYPE] [-o OPTION,] [[DEVICE] DIR]
  17. Mount new filesystem(s) on directories. With no arguments, display existing
  18. mounts.
  19. -a Mount all entries in /etc/fstab (with -t, only entries of that TYPE)
  20. -O Only mount -a entries that have this option
  21. -f Fake it (don't actually mount)
  22. -r Read only (same as -o ro)
  23. -w Read/write (default, same as -o rw)
  24. -t Specify filesystem type
  25. -v Verbose
  26. OPTIONS is a comma separated list of options, which can also be supplied
  27. as --longopts.
  28. Autodetects loopback mounts (a file on a directory) and bind mounts (file
  29. on file, directory on directory), so you don't need to say --bind or --loop.
  30. You can also "mount -a /path" to mount everything in /etc/fstab under /path,
  31. even if it's noauto. DEVICE starting with UUID= is identified by blkid -U.
  32. #config SMBMOUNT
  33. # bool "smbmount"
  34. # default n
  35. # helo
  36. # usage: smbmount SHARE DIR
  37. #
  38. # Mount smb share with user/pasword prompt as necessary.
  39. #
  40. #config NFSMOUNT
  41. # bool "nfsmount"
  42. # default n
  43. # help
  44. # usage: nfsmount SHARE DIR
  45. #
  46. # Invoke an eldrich horror from the dawn of time.
  47. */
  48. #define FOR_mount
  49. #include "toys.h"
  50. GLOBALS(
  51. struct arg_list *optlist;
  52. char *type;
  53. char *bigO;
  54. unsigned long flags;
  55. char *opts;
  56. int okuser;
  57. )
  58. // mount.tests should check for all of this:
  59. // TODO detect existing identical mount (procfs with different dev name?)
  60. // TODO user, users, owner, group, nofail
  61. // TODO -p (passfd)
  62. // TODO -a -t notype,type2
  63. // TODO --subtree
  64. // TODO --rbind, -R
  65. // TODO make "mount --bind,ro old new" work (implicit -o remount)
  66. // TODO mount -a
  67. // TODO mount -o remount
  68. // TODO fstab: lookup default options for mount
  69. // TODO implement -v
  70. // TODO "mount -a -o remount,ro" should detect overmounts
  71. // TODO work out how that differs from "mount -ar"
  72. // TODO what if you --bind mount a block device somewhere (file, dir, dev)
  73. // TODO "touch servername; mount -t cifs servername path"
  74. // TODO mount -o remount a user mount
  75. // TODO mount image.img sub (auto-loopback) then umount image.img
  76. // TODO mount UUID=blah
  77. // Strip flags out of comma separated list of options, return flags,.
  78. static long flag_opts(char *new, long flags, char **more)
  79. {
  80. struct {
  81. char *name;
  82. long flags;
  83. } opts[] = {
  84. // NOPs (we autodetect --loop and --bind)
  85. {"loop", 0}, {"bind", 0}, {"defaults", 0}, {"quiet", 0},
  86. {"user", 0}, {"nouser", 0}, // checked in fstab, ignored in -o
  87. {"ro", MS_RDONLY}, {"rw", ~MS_RDONLY},
  88. {"nosuid", MS_NOSUID}, {"suid", ~MS_NOSUID},
  89. {"nodev", MS_NODEV}, {"dev", ~MS_NODEV},
  90. {"noexec", MS_NOEXEC}, {"exec", ~MS_NOEXEC},
  91. {"sync", MS_SYNCHRONOUS}, {"async", ~MS_SYNCHRONOUS},
  92. {"noatime", MS_NOATIME}, {"atime", ~MS_NOATIME},
  93. {"norelatime", ~MS_RELATIME}, {"relatime", MS_RELATIME},
  94. {"nodiratime", MS_NODIRATIME}, {"diratime", ~MS_NODIRATIME},
  95. {"loud", ~MS_SILENT},
  96. {"shared", MS_SHARED}, {"rshared", MS_SHARED|MS_REC},
  97. {"slave", MS_SLAVE}, {"rslave", MS_SLAVE|MS_REC},
  98. {"private", MS_PRIVATE}, {"rprivate", MS_SLAVE|MS_REC},
  99. {"unbindable", MS_UNBINDABLE}, {"runbindable", MS_UNBINDABLE|MS_REC},
  100. {"remount", MS_REMOUNT}, {"move", MS_MOVE},
  101. // mand dirsync rec iversion strictatime
  102. };
  103. if (new) for (;;) {
  104. char *comma = strchr(new, ',');
  105. int i;
  106. if (comma) *comma = 0;
  107. // If we recognize an option, apply flags
  108. for (i = 0; i < ARRAY_LEN(opts); i++) if (!strcasecmp(opts[i].name, new)) {
  109. long ll = opts[i].flags;
  110. if (ll < 0) flags &= ll;
  111. else flags |= ll;
  112. break;
  113. }
  114. // If we didn't recognize it, keep string version
  115. if (more && i == ARRAY_LEN(opts)) {
  116. i = *more ? strlen(*more) : 0;
  117. *more = xrealloc(*more, i + strlen(new) + 2);
  118. if (i) (*more)[i++] = ',';
  119. strcpy(i+*more, new);
  120. }
  121. if (!comma) break;
  122. *comma = ',';
  123. new = comma + 1;
  124. }
  125. return flags;
  126. }
  127. // Shell out to a program, returning the output string or NULL on error
  128. static char *tortoise(int loud, char **cmd)
  129. {
  130. int rc, pipe, len;
  131. pid_t pid;
  132. pid = xpopen(cmd, &pipe, 1);
  133. len = readall(pipe, toybuf, sizeof(toybuf)-1);
  134. rc = xpclose(pid, pipe);
  135. if (!rc && len > 1) {
  136. if (toybuf[len-1] == '\n') --len;
  137. toybuf[len] = 0;
  138. return toybuf;
  139. }
  140. if (loud) error_msg("%s failed %d", *cmd, rc);
  141. return 0;
  142. }
  143. static void mount_filesystem(char *dev, char *dir, char *type,
  144. unsigned long flags, char *opts)
  145. {
  146. FILE *fp = 0;
  147. int rc = EINVAL;
  148. char *buf = 0;
  149. if (FLAG(f)) return;
  150. if (getuid()) {
  151. if (TT.okuser) TT.okuser = 0;
  152. else {
  153. error_msg("'%s' not user mountable in fstab", dev);
  154. return;
  155. }
  156. }
  157. if (strstart(&dev, "UUID=")) {
  158. char *s = tortoise(0, (char *[]){"blkid", "-U", dev, 0});
  159. if (!s) return error_msg("No uuid %s", dev);
  160. dev = s;
  161. }
  162. // Autodetect bind mount or filesystem type
  163. if (type && !strcmp(type, "auto")) type = 0;
  164. if (flags & MS_MOVE) {
  165. if (type) error_exit("--move with -t");
  166. } else if (!type) {
  167. struct stat stdev, stdir;
  168. // file on file or dir on dir is a --bind mount.
  169. if (!stat(dev, &stdev) && !stat(dir, &stdir)
  170. && ((S_ISREG(stdev.st_mode) && S_ISREG(stdir.st_mode))
  171. || (S_ISDIR(stdev.st_mode) && S_ISDIR(stdir.st_mode))))
  172. {
  173. flags |= MS_BIND;
  174. } else fp = xfopen("/proc/filesystems", "r");
  175. } else if (!strcmp(type, "ignore")) return;
  176. else if (!strcmp(type, "swap"))
  177. toys.exitval |= xrun((char *[]){"swapon", "--", dev, 0});
  178. for (;;) {
  179. int fd = -1, ro = 0;
  180. // If type wasn't specified, try all of them in order.
  181. if (fp && !buf) {
  182. size_t i;
  183. if (getline(&buf, &i, fp)<1) {
  184. error_msg("%s: need -t", dev);
  185. break;
  186. }
  187. type = buf;
  188. // skip nodev devices
  189. if (!isspace(*type)) {
  190. free(buf);
  191. buf = 0;
  192. continue;
  193. }
  194. // trim whitespace
  195. while (isspace(*type)) type++;
  196. i = strlen(type);
  197. if (i) type[i-1] = 0;
  198. }
  199. if (FLAG(v)) printf("try '%s' type '%s' on '%s'\n", dev, type, dir);
  200. for (;;) {
  201. rc = mount(dev, dir, type, flags, opts);
  202. // Did we succeed, fail unrecoverably, or already try read-only?
  203. if (!rc || (errno != EACCES && errno != EROFS) || (flags&MS_RDONLY))
  204. break;
  205. // If we haven't already tried it, use the BLKROSET ioctl to ensure
  206. // that the underlying device isn't read-only.
  207. if (fd == -1) {
  208. if (FLAG(v))
  209. printf("trying BLKROSET ioctl on '%s'\n", dev);
  210. if (-1 != (fd = open(dev, O_RDONLY))) {
  211. rc = ioctl(fd, BLKROSET, &ro);
  212. close(fd);
  213. if (!rc) continue;
  214. }
  215. }
  216. fprintf(stderr, "'%s' is read-only\n", dev);
  217. flags |= MS_RDONLY;
  218. }
  219. // Trying to autodetect loop mounts like bind mounts above (file on dir)
  220. // isn't good enough because "mount -t ext2 fs.img dir" is valid, but if
  221. // you _do_ accept loop mounts with -t how do you tell "-t cifs" isn't
  222. // looking for a block device if it's not in /proc/filesystems yet
  223. // because the fs module won't be loaded until you try the mount, and
  224. // if you can't then DEVICE existing as a file would cause a false
  225. // positive loopback mount (so "touch servername" becomes a potential
  226. // denial of service attack...)
  227. //
  228. // Solution: try the mount, let the kernel tell us it wanted a block
  229. // device, then do the loopback setup and retry the mount.
  230. if (rc && errno == ENOTBLK) {
  231. dev = tortoise(1, (char *[]){"losetup",
  232. (flags&MS_RDONLY) ? "-fsr" : "-fs", dev, 0});
  233. if (!dev) break;
  234. continue;
  235. }
  236. free(buf);
  237. buf = 0;
  238. if (!rc) break;
  239. if (fp && (errno == EINVAL || errno == EBUSY)) continue;
  240. perror_msg("'%s'->'%s'", dev, dir);
  241. break;
  242. }
  243. if (fp) fclose(fp);
  244. }
  245. void mount_main(void)
  246. {
  247. char *opts = 0, *dev = 0, *dir = 0, **ss;
  248. long flags = MS_SILENT;
  249. struct arg_list *o;
  250. struct mtab_list *mtl, *mtl2 = 0, *mm, *remount;
  251. // TODO
  252. // remount
  253. // - overmounts
  254. // shared subtree
  255. // -o parsed after fstab options
  256. // test if mountpoint already exists (-o noremount?)
  257. // First pass; just accumulate string, don't parse flags yet. (This is so
  258. // we can modify fstab entries with -a, or mtab with remount.)
  259. for (o = TT.optlist; o; o = o->next) comma_collate(&opts, o->arg);
  260. if (FLAG(r)) comma_collate(&opts, "ro");
  261. if (FLAG(w)) comma_collate(&opts, "rw");
  262. // Treat each --option as -o option
  263. for (ss = toys.optargs; *ss; ss++) {
  264. char *sss = *ss;
  265. // If you realy, really want to mount a file named "--", we support it.
  266. if (sss[0]=='-' && sss[1]=='-' && sss[2]) comma_collate(&opts, sss+2);
  267. else if (!dev) dev = sss;
  268. else if (!dir) dir = sss;
  269. // same message as lib/args.c ">2" which we can't use because --opts count
  270. else error_exit("Max 2 arguments\n");
  271. }
  272. if (FLAG(a) && dir) error_exit("-a with >1 arg");
  273. // For remount we need _last_ match (in case of overmounts), so traverse
  274. // in reverse order. (Yes I'm using remount as a boolean for a bit here,
  275. // the double cast is to get gcc to shut up about it.)
  276. remount = (void *)(long)comma_scan(opts, "remount", 0);
  277. if ((FLAG(a) && !access("/proc/mounts", R_OK)) || remount) {
  278. mm = dlist_terminate(mtl = mtl2 = xgetmountlist(0));
  279. if (remount) remount = mm;
  280. }
  281. // Do we need to do an /etc/fstab trawl?
  282. // This covers -a, -o remount, one argument, all user mounts
  283. if (FLAG(a) || (dev && (!dir || getuid() || remount))) {
  284. if (!remount) dlist_terminate(mtl = xgetmountlist("/etc/fstab"));
  285. for (mm = remount ? remount : mtl; mm; mm = (remount ? mm->prev : mm->next))
  286. {
  287. char *aopts = 0;
  288. struct mtab_list *mmm = 0;
  289. int aflags, noauto, len;
  290. // Check for noauto and get it out of the option list. (Unknown options
  291. // that make it to the kernel give filesystem drivers indigestion.)
  292. noauto = comma_scan(mm->opts, "noauto", 1);
  293. if (FLAG(a)) {
  294. // "mount -a /path" to mount all entries under /path
  295. if (dev) {
  296. len = strlen(dev);
  297. if (strncmp(dev, mm->dir, len)
  298. || (mm->dir[len] && mm->dir[len] != '/')) continue;
  299. } else if (noauto) continue; // never present in the remount case
  300. if (!mountlist_istype(mm,TT.type) || !comma_scanall(mm->opts,TT.bigO))
  301. continue;
  302. } else {
  303. if (dir && strcmp(dir, mm->dir)) continue;
  304. if (strcmp(dev, mm->device) && (dir || strcmp(dev, mm->dir))) continue;
  305. }
  306. // Don't overmount the same dev on the same directory
  307. // (Unless root explicitly says to in non -a mode.)
  308. if (mtl2 && !remount)
  309. for (mmm = mtl2; mmm; mmm = mmm->next)
  310. if (!strcmp(mm->dir, mmm->dir) && !strcmp(mm->device, mmm->device))
  311. break;
  312. // user only counts from fstab, not opts.
  313. if (!mmm) {
  314. TT.okuser = comma_scan(mm->opts, "user", 1);
  315. aflags = flag_opts(mm->opts, flags, &aopts);
  316. aflags = flag_opts(opts, aflags, &aopts);
  317. mount_filesystem(mm->device, mm->dir, mm->type, aflags, aopts);
  318. } // TODO else if (getuid()) error_msg("already there") ?
  319. free(aopts);
  320. if (!FLAG(a)) break;
  321. }
  322. if (CFG_TOYBOX_FREE) {
  323. llist_traverse(mtl, free);
  324. llist_traverse(mtl2, free);
  325. }
  326. if (!mm && !FLAG(a))
  327. error_exit("'%s' not in %s", dir ? dir : dev,
  328. remount ? "/proc/mounts" : "fstab");
  329. // show mounts from /proc/mounts
  330. } else if (!dev) {
  331. for (mtl = xgetmountlist(0); mtl && (mm = dlist_pop(&mtl)); free(mm)) {
  332. char *s = 0;
  333. if (TT.type && strcmp(TT.type, mm->type)) continue;
  334. if (*mm->device == '/') s = xabspath(mm->device, 0);
  335. xprintf("%s on %s type %s (%s)\n",
  336. s ? s : mm->device, mm->dir, mm->type, mm->opts);
  337. free(s);
  338. }
  339. // two arguments
  340. } else {
  341. char *more = 0;
  342. flags = flag_opts(opts, flags, &more);
  343. mount_filesystem(dev, dir, TT.type, flags, more);
  344. if (CFG_TOYBOX_FREE) free(more);
  345. }
  346. }