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