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