1 /* mount.c - mount filesystems
3 * Copyright 2014 Rob Landley <rob@landley.net>
5 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mount.html
6 * Note: -hV is bad spec, haven't implemented -FsLU yet
7 * no mtab (/proc/mounts does it) so -n is NOP.
9 USE_MOUNT(NEWTOY(mount, "?O:afnrvwt:o*[-rw]", TOYFLAG_BIN|TOYFLAG_STAYROOT))
10 //USE_NFSMOUNT(NEWTOY(nfsmount, "?<2>2", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
16 usage: mount [-afFrsvw] [-t TYPE] [-o OPTIONS...] [[DEVICE] DIR]
18 Mount new filesystem(s) on directories. With no arguments, display existing
21 -a mount all entries in /etc/fstab (with -t, only entries of that TYPE)
22 -O only mount -a entries that have this option
23 -f fake it (don't actually mount)
24 -r read only (same as -o ro)
25 -w read/write (default, same as -o rw)
26 -t specify filesystem type
29 OPTIONS is a comma separated list of options, which can also be supplied
32 This mount autodetects loopback mounts (a file on a directory) and
33 bind mounts (file on file, directory on directory), so you don't need
34 to say --bind or --loop. You can also "mount -a /path" to mount everything
35 in /etc/fstab under /path, even if it's noauto.
41 # usage: nfsmount SHARE DIR
43 # Invoke an eldrich horror from the dawn of time.
50 struct arg_list *optlist;
59 // mount.tests should check for all of this:
60 // TODO detect existing identical mount (procfs with different dev name?)
61 // TODO user, users, owner, group, nofail
63 // TODO -a -t notype,type2
66 // TODO make "mount --bind,ro old new" work (implicit -o remount)
68 // TODO mount -o remount
69 // TODO fstab: lookup default options for mount
71 // TODO "mount -a -o remount,ro" should detect overmounts
72 // TODO work out how that differs from "mount -ar"
73 // TODO what if you --bind mount a block device somewhere (file, dir, dev)
74 // TODO "touch servername; mount -t cifs servername path"
75 // TODO mount -o remount a user mount
76 // TODO mount image.img sub (auto-loopback) then umount image.img
78 // Strip flags out of comma separated list of options, return flags,.
79 static long flag_opts(char *new, long flags, char **more)
85 // NOPs (we autodetect --loop and --bind)
86 {"loop", 0}, {"bind", 0}, {"defaults", 0}, {"quiet", 0},
87 {"user", 0}, {"nouser", 0}, // checked in fstab, ignored in -o
88 {"ro", MS_RDONLY}, {"rw", ~MS_RDONLY},
89 {"nosuid", MS_NOSUID}, {"suid", ~MS_NOSUID},
90 {"nodev", MS_NODEV}, {"dev", ~MS_NODEV},
91 {"noexec", MS_NOEXEC}, {"exec", ~MS_NOEXEC},
92 {"sync", MS_SYNCHRONOUS}, {"async", ~MS_SYNCHRONOUS},
93 {"noatime", MS_NOATIME}, {"atime", ~MS_NOATIME},
94 {"nodiratime", MS_NODIRATIME}, {"diratime", ~MS_NODIRATIME},
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
105 char *comma = strchr(new, ',');
108 if (comma) *comma = 0;
110 // If we recognize an option, apply flags
111 for (i = 0; i < ARRAY_LEN(opts); i++) if (!strcasecmp(opts[i].name, new)) {
112 long ll = opts[i].flags;
114 if (ll < 0) flags &= ll;
120 // If we didn't recognize it, keep string version
121 if (more && i == ARRAY_LEN(opts)) {
122 i = *more ? strlen(*more) : 0;
123 *more = xrealloc(*more, i + strlen(new) + 2);
124 if (i) (*more)[i++] = ',';
125 strcpy(i+*more, new);
136 static void mount_filesystem(char *dev, char *dir, char *type,
137 unsigned long flags, char *opts)
143 if (toys.optflags & FLAG_f) return;
146 if (TT.okuser) TT.okuser = 0;
148 error_msg("'%s' not user mountable in fstab", dev);
154 // Autodetect bind mount or filesystem type
156 if (type && !strcmp(type, "auto")) type = 0;
157 if (flags & MS_MOVE) {
158 if (type) error_exit("--move with -t");
160 struct stat stdev, stdir;
162 // file on file or dir on dir is a --bind mount.
163 if (!stat(dev, &stdev) && !stat(dir, &stdir)
164 && ((S_ISREG(stdev.st_mode) && S_ISREG(stdir.st_mode))
165 || (S_ISDIR(stdev.st_mode) && S_ISDIR(stdir.st_mode))))
168 } else fp = xfopen("/proc/filesystems", "r");
169 } else if (!strcmp(type, "ignore")) return;
170 else if (!strcmp(type, "swap"))
171 toys.exitval |= xrun((char *[]){"swapon", "--", dev, 0});
176 // If type wasn't specified, try all of them in order.
180 if (getline(&buf, &i, fp)<0) break;
182 // skip nodev devices
183 if (!isspace(*type)) {
190 while (isspace(*type)) type++;
192 if (i) type[i-1] = 0;
194 if (toys.optflags & FLAG_v)
195 printf("try '%s' type '%s' on '%s'\n", dev, type, dir);
197 rc = mount(dev, dir, type, flags, opts);
198 if ((rc != EACCES && rc != EROFS) || (flags & MS_RDONLY)) break;
199 if (rc == EROFS && fd == -1) {
200 if (-1 != (fd = open(dev, O_RDONLY))) {
201 ioctl(fd, BLKROSET, &ro);
207 fprintf(stderr, "'%s' is read-only", dev);
211 // Trying to autodetect loop mounts like bind mounts above (file on dir)
212 // isn't good enough because "mount -t ext2 fs.img dir" is valid, but if
213 // you _do_ accept loop mounts with -t how do you tell "-t cifs" isn't
214 // looking for a block device if it's not in /proc/filesystems yet
215 // because the module that won't be loaded until you try the mount, and
216 // if you can't then DEVICE existing as a file would cause a false
217 // positive loopback mount (so "touch servername" becomes a potential
218 // denial of service attack...)
220 // Solution: try the mount, let the kernel tell us it wanted a block
221 // device, then do the loopback setup and retry the mount.
223 if (rc && errno == ENOTBLK) {
224 char *losetup[] = {"losetup", "-fs", dev, 0};
228 if (flags & MS_RDONLY) losetup[1] = "-fsr";
229 pid = xpopen(losetup, &pipe, 1);
230 len = readall(pipe, toybuf, sizeof(toybuf)-1);
231 rc = xpclose(pid, pipe);
232 if (!rc && len > 1) {
233 if (toybuf[len-1] == '\n') --len;
239 error_msg("losetup failed %d", rc);
247 if (fp && (errno == EINVAL || errno == EBUSY)) continue;
249 perror_msg("'%s'->'%s'", dev, dir);
256 void mount_main(void)
258 char *opts = 0, *dev = 0, *dir = 0, **ss;
259 long flags = MS_SILENT;
261 struct mtab_list *mtl, *mtl2 = 0, *mm, *remount;
267 // -o parsed after fstab options
268 // test if mountpoint already exists (-o noremount?)
270 // First pass; just accumulate string, don't parse flags yet. (This is so
271 // we can modify fstab entries with -a, or mtab with remount.)
272 for (o = TT.optlist; o; o = o->next) comma_collate(&opts, o->arg);
273 if (toys.optflags & FLAG_r) comma_collate(&opts, "ro");
274 if (toys.optflags & FLAG_w) comma_collate(&opts, "rw");
276 // Treat each --option as -o option
277 for (ss = toys.optargs; *ss; ss++) {
280 // If you realy, really want to mount a file named "--", we support it.
281 if (sss[0]=='-' && sss[1]=='-' && sss[2]) comma_collate(&opts, sss+2);
282 else if (!dev) dev = sss;
283 else if (!dir) dir = sss;
284 // same message as lib/args.c ">2" which we can't use because --opts count
285 else error_exit("Max 2 arguments\n");
288 if ((toys.optflags & FLAG_a) && dir) error_exit("-a with >1 arg");
290 // For remount we need _last_ match (in case of overmounts), so traverse
291 // in reverse order. (Yes I'm using remount as a boolean for a bit here,
292 // the double cast is to get gcc to shut up about it.)
293 remount = (void *)(long)comma_scan(opts, "remount", 1);
294 if (((toys.optflags & FLAG_a) && !access("/proc/mounts", R_OK)) || remount) {
295 mm = dlist_terminate(mtl = mtl2 = xgetmountlist(0));
296 if (remount) remount = mm;
299 // Do we need to do an /etc/fstab trawl?
300 // This covers -a, -o remount, one argument, all user mounts
301 if ((toys.optflags & FLAG_a) || (dev && (!dir || getuid() || remount))) {
302 if (!remount) dlist_terminate(mtl = xgetmountlist("/etc/fstab"));
304 for (mm = remount ? remount : mtl; mm; mm = (remount ? mm->prev : mm->next))
307 struct mtab_list *mmm = 0;
308 int aflags, noauto, len;
310 // Check for noauto and get it out of the option list. (Unknown options
311 // that make it to the kernel give filesystem drivers indigestion.)
312 noauto = comma_scan(mm->opts, "noauto", 1);
314 if (toys.optflags & FLAG_a) {
315 // "mount -a /path" to mount all entries under /path
318 if (strncmp(dev, mm->dir, len)
319 || (mm->dir[len] && mm->dir[len] != '/')) continue;
320 } else if (noauto) continue; // never present in the remount case
321 if (!mountlist_istype(mm,TT.type) || !comma_scanall(mm->opts,TT.bigO))
324 if (dir && strcmp(dir, mm->dir)) continue;
325 if (dev && strcmp(dev, mm->device) && (dir || strcmp(dev, mm->dir)))
329 // Don't overmount the same dev on the same directory
330 // (Unless root explicitly says to in non -a mode.)
331 if (mtl2 && !remount)
332 for (mmm = mtl2; mmm; mmm = mmm->next)
333 if (!strcmp(mm->dir, mmm->dir) && !strcmp(mm->device, mmm->device))
336 // user only counts from fstab, not opts.
338 TT.okuser = comma_scan(mm->opts, "user", 1);
339 aflags = flag_opts(mm->opts, flags, &aopts);
340 aflags = flag_opts(opts, aflags, &aopts);
342 mount_filesystem(mm->device, mm->dir, mm->type, aflags, aopts);
343 } // TODO else if (getuid()) error_msg("already there") ?
346 if (!(toys.optflags & FLAG_a)) break;
348 if (CFG_TOYBOX_FREE) {
349 llist_traverse(mtl, free);
350 llist_traverse(mtl2, free);
352 if (!mm && !(toys.optflags & FLAG_a))
353 error_exit("'%s' not in %s", dir ? dir : dev,
354 remount ? "/proc/mounts" : "fstab");
356 // show mounts from /proc/mounts
358 for (mtl = xgetmountlist(0); mtl && (mm = dlist_pop(&mtl)); free(mm)) {
361 if (TT.type && strcmp(TT.type, mm->type)) continue;
362 if (*mm->device == '/') s = xabspath(mm->device, 0);
363 xprintf("%s on %s type %s (%s)\n",
364 s ? s : mm->device, mm->dir, mm->type, mm->opts);
372 mount_filesystem(dev, dir, TT.type, flag_opts(opts, flags, &more), more);
373 if (CFG_TOYBOX_FREE) free(more);