Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 3 | * Mini mount implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 7 | * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net> |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 8 | * |
Rob Landley | 7b363fd | 2005-12-20 17:18:01 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 10 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 11 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 12 | /* todo: |
| 13 | * bb_getopt_ulflags(); |
| 14 | */ |
| 15 | |
| 16 | /* Design notes: There is no spec for this. Remind me to write one. |
| 17 | |
| 18 | mount_main() calls singlemount() which calls mount_it_now(). |
| 19 | |
| 20 | mount_main() can loop through /etc/fstab for mount -a |
| 21 | singlemount() can loop through /etc/filesystems for fstype detection. |
| 22 | mount_it_now() does the actual mount. |
| 23 | */ |
| 24 | |
Matt Kraai | 3425111 | 2001-05-02 21:17:38 +0000 | [diff] [blame] | 25 | #include <limits.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | #include <errno.h> |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | #include <mntent.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | #include <ctype.h> |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 33 | #include <sys/mount.h> |
| 34 | #include <fcntl.h> // for CONFIG_FEATURE_MOUNT_LOOP |
| 35 | #include <sys/ioctl.h> // for CONFIG_FEATURE_MOUNT_LOOP |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 36 | #include "busybox.h" |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 37 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 38 | // These two aren't always defined in old headers |
| 39 | #ifndef MS_BIND |
| 40 | #define MS_BIND 4096 |
| 41 | #endif |
| 42 | #ifndef MS_MOVE |
| 43 | #define MS_MOVE 8192 |
Eric Andersen | d9fe958 | 2003-07-22 08:25:37 +0000 | [diff] [blame] | 44 | #endif |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 45 | #ifndef MS_SILENT |
| 46 | #define MS_SILENT 32768 |
| 47 | #endif |
Eric Andersen | d9fe958 | 2003-07-22 08:25:37 +0000 | [diff] [blame] | 48 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 49 | // Not real flags, but we want to be able to check for this. |
| 50 | #define MOUNT_NOAUTO (1<<29) |
| 51 | #define MOUNT_SWAP (1<<30) |
| 52 | /* Standard mount options (from -o options or --options), with corresponding |
| 53 | * flags */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 55 | struct { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | const char *name; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 57 | long flags; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 58 | } static const mount_options[] = { |
| 59 | // NOP flags. |
| 60 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 61 | {"loop", 0}, |
| 62 | {"defaults", 0}, |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 63 | {"quiet", 0}, |
| 64 | |
| 65 | // vfs flags |
| 66 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 67 | {"ro", MS_RDONLY}, |
| 68 | {"rw", ~MS_RDONLY}, |
| 69 | {"nosuid", MS_NOSUID}, |
| 70 | {"suid", ~MS_NOSUID}, |
| 71 | {"dev", ~MS_NODEV}, |
| 72 | {"nodev", MS_NODEV}, |
| 73 | {"exec", ~MS_NOEXEC}, |
| 74 | {"noexec", MS_NOEXEC}, |
| 75 | {"sync", MS_SYNCHRONOUS}, |
| 76 | {"async", ~MS_SYNCHRONOUS}, |
Rob Landley | 8b0efdb | 2006-01-10 02:37:20 +0000 | [diff] [blame] | 77 | {"atime", ~MS_NOATIME}, |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 78 | {"noatime", MS_NOATIME}, |
Rob Landley | 8b0efdb | 2006-01-10 02:37:20 +0000 | [diff] [blame] | 79 | {"diratime", ~MS_NODIRATIME}, |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 80 | {"nodiratime", MS_NODIRATIME}, |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 81 | {"loud", ~MS_SILENT}, |
| 82 | |
| 83 | // action flags |
| 84 | |
| 85 | {"remount", MS_REMOUNT}, |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 86 | {"bind", MS_BIND}, |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 87 | {"move", MS_MOVE}, |
| 88 | {"noauto",MOUNT_NOAUTO}, |
| 89 | {"swap",MOUNT_SWAP} |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 92 | /* Append mount options to string */ |
| 93 | static void append_mount_options(char **oldopts, char *newopts) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 95 | if(*oldopts && **oldopts) { |
| 96 | char *temp=bb_xasprintf("%s,%s",*oldopts,newopts); |
| 97 | free(*oldopts); |
| 98 | *oldopts=temp; |
| 99 | } else { |
| 100 | if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts); |
| 101 | *oldopts = bb_xstrdup(newopts); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* Use the mount_options list to parse options into flags. |
| 106 | * Return list of unrecognized options in *strflags if strflags!=NULL */ |
| 107 | static int parse_mount_options(char *options, char **unrecognized) |
| 108 | { |
| 109 | int flags = MS_SILENT; |
| 110 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 111 | // Loop through options |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 112 | for (;;) { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 113 | int i; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 114 | char *comma = strchr(options, ','); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 116 | if (comma) *comma = 0; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 117 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 118 | // Find this option in mount_options |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 119 | for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) { |
| 120 | if (!strcasecmp(mount_options[i].name, options)) { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 121 | long fl = mount_options[i].flags; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 122 | if(fl < 0) flags &= fl; |
| 123 | else flags |= fl; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | break; |
| 125 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 127 | // If unrecognized not NULL, append unrecognized mount options */ |
| 128 | if (unrecognized |
| 129 | && i == (sizeof(mount_options) / sizeof(*mount_options))) |
| 130 | { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 131 | // Add it to strflags, to pass on to kernel |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 132 | i = *unrecognized ? strlen(*unrecognized) : 0; |
| 133 | *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2); |
| 134 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 135 | // Comma separated if it's not the first one |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 136 | if (i) (*unrecognized)[i++] = ','; |
| 137 | strcpy((*unrecognized)+i, options); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 139 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 140 | // Advance to next option, or finish |
| 141 | if(comma) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | *comma = ','; |
| 143 | options = ++comma; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 144 | } else break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 146 | |
| 147 | return flags; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 150 | // Return a list of all block device backed filesystems |
Matt Kraai | 1240082 | 2001-04-17 04:32:50 +0000 | [diff] [blame] | 151 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 152 | static llist_t *get_block_backed_filesystems(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 153 | { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 154 | char *fs, *buf, |
| 155 | *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0}; |
| 156 | llist_t *list = 0; |
| 157 | int i; |
| 158 | FILE *f; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 159 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 160 | for(i = 0; filesystems[i]; i++) { |
| 161 | if(!(f = fopen(filesystems[i], "r"))) continue; |
| 162 | |
| 163 | for(fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f)); |
| 164 | free(buf)) |
| 165 | { |
| 166 | if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue; |
| 167 | |
| 168 | while(isspace(*fs)) fs++; |
| 169 | if(*fs=='#' || *fs=='*') continue; |
| 170 | if(!*fs) continue; |
| 171 | |
| 172 | list=llist_add_to_end(list,bb_xstrdup(fs)); |
| 173 | } |
| 174 | if (ENABLE_FEATURE_CLEAN_UP) fclose(f); |
| 175 | } |
| 176 | |
| 177 | return list; |
| 178 | } |
| 179 | |
| 180 | llist_t *fslist = 0; |
| 181 | |
| 182 | void delete_block_backed_filesystems(void); |
| 183 | #if ENABLE_FEATURE_CLEAN_UP |
| 184 | static void delete_block_backed_filesystems(void) |
| 185 | { |
| 186 | llist_free(fslist); |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | #if ENABLE_FEATURE_MTAB_SUPPORT |
| 191 | static int useMtab; |
| 192 | #else |
| 193 | #define useMtab 0 |
| 194 | #endif |
| 195 | |
| 196 | // Perform actual mount of specific filesystem at specific location. |
| 197 | |
| 198 | static int mount_it_now(struct mntent *mp, int vfsflags) |
| 199 | { |
| 200 | int rc; |
| 201 | char *filteropts = 0; |
| 202 | |
| 203 | parse_mount_options(mp->mnt_opts, &filteropts); |
| 204 | // Mount, with fallback to read-only if necessary. |
| 205 | |
| 206 | for(;;) { |
| 207 | rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type, |
| 208 | vfsflags, filteropts); |
| 209 | if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS)) |
| 210 | break; |
| 211 | bb_error_msg("%s is write-protected, mounting read-only", |
| 212 | mp->mnt_fsname); |
| 213 | vfsflags |= MS_RDONLY; |
| 214 | } |
| 215 | |
| 216 | free(filteropts); |
| 217 | |
| 218 | // Abort entirely if permission denied. |
| 219 | |
| 220 | if (rc && errno == EPERM) |
| 221 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
| 222 | |
| 223 | /* If the mount was successful, and we're maintaining an old-style |
| 224 | * mtab file by hand, add the new entry to it now. */ |
| 225 | |
| 226 | if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) { |
| 227 | FILE *mountTable = setmntent(bb_path_mtab_file, "a+"); |
| 228 | int i; |
| 229 | |
| 230 | if(!mountTable) |
| 231 | bb_error_msg("No %s\n",bb_path_mtab_file); |
| 232 | |
| 233 | // Add vfs string flags |
| 234 | |
| 235 | for(i=0; mount_options[i].flags != MS_REMOUNT; i++) |
| 236 | if (mount_options[i].flags > 0) |
| 237 | append_mount_options(&(mp->mnt_opts), |
| 238 | // Shut up about the darn const. It's not important. I don't care. |
| 239 | (char *)mount_options[i].name); |
| 240 | |
| 241 | // Remove trailing / (if any) from directory we mounted on |
| 242 | |
| 243 | i = strlen(mp->mnt_dir); |
| 244 | if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0; |
| 245 | |
| 246 | // Write and close. |
| 247 | |
| 248 | if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind"; |
| 249 | addmntent(mountTable, mp); |
| 250 | endmntent(mountTable); |
| 251 | if (ENABLE_FEATURE_CLEAN_UP) |
| 252 | if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0; |
| 253 | } |
| 254 | |
| 255 | return rc; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | // Mount one directory. Handles NFS, loopback, autobind, and filesystem type |
| 260 | // detection. Returns 0 for success, nonzero for failure. |
| 261 | |
| 262 | static int singlemount(struct mntent *mp) |
| 263 | { |
| 264 | int rc = 0, vfsflags; |
| 265 | char *loopFile = 0; |
| 266 | llist_t *fl = 0; |
| 267 | struct stat st; |
| 268 | |
| 269 | vfsflags = parse_mount_options(mp->mnt_opts, 0); |
| 270 | |
| 271 | // Might this be an NFS filesystem? |
| 272 | |
| 273 | if (ENABLE_FEATURE_MOUNT_NFS && |
| 274 | (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) && |
| 275 | strchr(mp->mnt_fsname, ':') != NULL) |
| 276 | { |
| 277 | char *options=0; |
| 278 | parse_mount_options(mp->mnt_opts, &options); |
| 279 | if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &options, 1)) { |
| 280 | bb_perror_msg("nfsmount failed"); |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | // Strangely enough, nfsmount() doesn't actually mount() anything. |
| 285 | |
| 286 | else return mount_it_now(mp, vfsflags); |
| 287 | } |
| 288 | |
| 289 | // Look at the file. (Not found isn't a failure for remount.) |
| 290 | |
| 291 | if (lstat(mp->mnt_fsname, &st)); |
| 292 | |
| 293 | if (!(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) { |
| 294 | // Do we need to allocate a loopback device for it? |
| 295 | |
| 296 | if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) { |
| 297 | loopFile = mp->mnt_fsname; |
| 298 | mp->mnt_fsname = 0; |
| 299 | switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) { |
| 300 | case 0: |
| 301 | case 1: |
| 302 | break; |
| 303 | default: |
| 304 | bb_error_msg( errno == EPERM || errno == EACCES |
| 305 | ? bb_msg_perm_denied_are_you_root |
| 306 | : "Couldn't setup loop device"); |
| 307 | return errno; |
| 308 | } |
| 309 | |
| 310 | // Autodetect bind mounts |
| 311 | |
| 312 | } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) vfsflags |= MS_BIND; |
| 313 | } |
| 314 | |
| 315 | /* If we know the fstype (or don't need to), jump straight |
| 316 | * to the actual mount. */ |
| 317 | |
| 318 | if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) |
| 319 | rc = mount_it_now(mp, vfsflags); |
| 320 | |
| 321 | // Loop through filesystem types until mount succeeds or we run out |
| 322 | |
| 323 | else for (fl = fslist; fl; fl = fl->link) { |
| 324 | mp->mnt_type = fl->data; |
| 325 | |
| 326 | if (!(rc = mount_it_now(mp,vfsflags))) break; |
| 327 | |
| 328 | mp->mnt_type = 0; |
| 329 | } |
| 330 | |
| 331 | // Mount failed. Clean up |
| 332 | if (rc && loopFile) { |
| 333 | del_loop(mp->mnt_fsname); |
| 334 | if(ENABLE_FEATURE_CLEAN_UP) free(loopFile); |
| 335 | } |
| 336 | return rc; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // Parse options, if necessary parse fstab/mtab, and call singlemount for |
| 341 | // each directory to be mounted. |
| 342 | |
| 343 | int mount_main(int argc, char **argv) |
| 344 | { |
| 345 | char *cmdopts = bb_xstrdup(""), *fstabname, *fstype=0, *storage_path=0; |
| 346 | FILE *fstab; |
| 347 | int i, opt, all = FALSE, rc = 1; |
| 348 | struct mntent mtpair[2], *mtcur = mtpair; |
| 349 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 350 | /* parse long options, like --bind and --move. Note that -o option |
| 351 | * and --option are synonymous. Yes, this means --remount,rw works. */ |
| 352 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 353 | for (i = opt = 0; i < argc; i++) { |
| 354 | if (argv[i][0] == '-' && argv[i][1] == '-') { |
| 355 | append_mount_options(&cmdopts,argv[i]+2); |
| 356 | } else argv[opt++] = argv[i]; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 357 | } |
| 358 | argc = opt; |
| 359 | |
| 360 | // Parse remaining options |
| 361 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 362 | while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) { |
Matt Kraai | a3045df | 2001-04-17 04:48:51 +0000 | [diff] [blame] | 363 | switch (opt) { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 364 | case 'o': |
| 365 | append_mount_options(&cmdopts, optarg); |
| 366 | break; |
| 367 | case 't': |
| 368 | fstype = optarg; |
| 369 | break; |
| 370 | case 'r': |
| 371 | append_mount_options(&cmdopts, "ro"); |
| 372 | break; |
| 373 | case 'w': |
| 374 | append_mount_options(&cmdopts, "rw"); |
| 375 | break; |
| 376 | case 'a': |
| 377 | all = TRUE; |
| 378 | break; |
| 379 | case 'n': |
| 380 | USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;) |
| 381 | break; |
| 382 | case 'f': |
| 383 | USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;) |
| 384 | break; |
| 385 | case 'v': |
| 386 | break; // ignore -v |
| 387 | default: |
| 388 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 389 | } |
Matt Kraai | a3045df | 2001-04-17 04:48:51 +0000 | [diff] [blame] | 390 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 391 | |
| 392 | // Ignore type "auto". |
Matt Kraai | a3045df | 2001-04-17 04:48:51 +0000 | [diff] [blame] | 393 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 394 | if (fstype && !strcmp(fstype, "auto")) fstype = NULL; |
| 395 | |
| 396 | // Three or more non-option arguments? Die with a usage message. |
| 397 | |
| 398 | if (optind-argc>2) bb_show_usage(); |
| 399 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 400 | // If we have no arguments, show currently mounted filesystems |
| 401 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 402 | if (optind == argc) { |
| 403 | if (!all) { |
| 404 | FILE *mountTable = setmntent(bb_path_mtab_file, "r"); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 405 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 406 | if(!mountTable) bb_error_msg_and_die("No %s",bb_path_mtab_file); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 407 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 408 | while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1, |
| 409 | sizeof(bb_common_bufsiz1))) |
| 410 | { |
| 411 | // Don't show rootfs. |
| 412 | if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 413 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 414 | if (!fstype || !strcmp(mtpair->mnt_type, fstype)) |
| 415 | printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname, |
| 416 | mtpair->mnt_dir, mtpair->mnt_type, |
| 417 | mtpair->mnt_opts); |
| 418 | } |
| 419 | if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable); |
| 420 | return EXIT_SUCCESS; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 421 | } |
Glenn L McGrath | 8042f65 | 2002-08-23 06:17:46 +0000 | [diff] [blame] | 422 | } |
Matt Kraai | 1240082 | 2001-04-17 04:32:50 +0000 | [diff] [blame] | 423 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 424 | /* Initialize list of block backed filesystems */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 425 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 426 | fslist = get_block_backed_filesystems(); |
| 427 | if (ENABLE_FEATURE_CLEAN_UP) atexit(delete_block_backed_filesystems); |
| 428 | |
| 429 | // When we have two arguments, the second is the directory and we can |
| 430 | // skip looking at fstab entirely. We can always abspath() the directory |
| 431 | // argument when we get it. |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 432 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 433 | if (optind+2 == argc) { |
| 434 | mtpair->mnt_fsname = argv[optind]; |
| 435 | mtpair->mnt_dir = argv[optind+1]; |
| 436 | mtpair->mnt_type = fstype; |
| 437 | mtpair->mnt_opts = cmdopts; |
| 438 | rc = singlemount(mtpair); |
| 439 | goto clean_up; |
| 440 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 441 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 442 | // If we have at least one argument, it's the storage location |
| 443 | |
| 444 | if (optind < argc) storage_path = bb_simplify_path(argv[optind]); |
| 445 | |
| 446 | // Open either fstab or mtab |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 447 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 448 | if (parse_mount_options(cmdopts,0) & MS_REMOUNT) |
| 449 | fstabname = (char *)bb_path_mtab_file; // Again with the evil const. |
| 450 | else fstabname="/etc/fstab"; |
| 451 | |
| 452 | if (!(fstab=setmntent(fstabname,"r"))) |
| 453 | bb_perror_msg_and_die("Cannot read %s",fstabname); |
| 454 | |
| 455 | // Loop through entries until we find what we're looking for. |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 456 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 457 | memset(mtpair,0,sizeof(mtpair)); |
| 458 | for (;;) { |
| 459 | struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 460 | |
| 461 | // Get next fstab entry |
| 462 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 463 | if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1, |
| 464 | sizeof(bb_common_bufsiz1))) |
| 465 | { |
| 466 | // Were we looking for something specific? |
| 467 | |
| 468 | if (optind != argc) { |
| 469 | |
| 470 | // If we didn't find anything, complain. |
| 471 | |
| 472 | if (!mtnext->mnt_fsname) |
| 473 | bb_error_msg_and_die("Can't find %s in %s", |
| 474 | argv[optind], fstabname); |
| 475 | |
| 476 | // Mount the last thing we found. |
| 477 | |
| 478 | mtcur = mtnext; |
| 479 | mtcur->mnt_opts=bb_xstrdup(mtcur->mnt_opts); |
| 480 | append_mount_options(&(mtcur->mnt_opts),cmdopts); |
| 481 | rc = singlemount(mtcur); |
| 482 | free(mtcur->mnt_opts); |
| 483 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 484 | break; |
| 485 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 486 | |
| 487 | /* If we're trying to mount something specific and this isn't it, |
| 488 | * skip it. Note we must match both the exact text in fstab (ala |
| 489 | * "proc") or a full path from root */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 490 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 491 | if (optind != argc) { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 492 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 493 | // Is this what we're looking for? |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 494 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 495 | if(strcmp(argv[optind],mtcur->mnt_fsname) && |
| 496 | strcmp(storage_path,mtcur->mnt_fsname) && |
| 497 | strcmp(argv[optind],mtcur->mnt_dir) && |
| 498 | strcmp(storage_path,mtcur->mnt_dir)) continue; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 499 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 500 | // Remember this entry. Something later may have overmounted |
| 501 | // it, and we want the _last_ match. |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 502 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 503 | mtcur = mtnext; |
| 504 | |
| 505 | // If we're mounting all. |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 506 | |
Glenn L McGrath | 3aae100 | 2001-05-07 01:38:03 +0000 | [diff] [blame] | 507 | } else { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 508 | |
| 509 | // Do we need to match a filesystem type? |
| 510 | if (fstype && strcmp(mtcur->mnt_type,fstype)) continue; |
| 511 | |
| 512 | // Skip noauto and swap anyway. |
| 513 | |
| 514 | if (parse_mount_options(mtcur->mnt_opts,0) |
| 515 | & (MOUNT_NOAUTO | MOUNT_SWAP)) continue; |
| 516 | |
| 517 | // Mount this thing. |
| 518 | |
| 519 | rc = singlemount(mtcur); |
| 520 | if (rc) { |
| 521 | // Don't whine about already mounted fs when mounting all. |
| 522 | if (errno == EBUSY) rc = 0; |
| 523 | else break; |
| 524 | } |
Glenn L McGrath | 3aae100 | 2001-05-07 01:38:03 +0000 | [diff] [blame] | 525 | } |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 526 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 527 | if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 528 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 529 | clean_up: |
| 530 | |
| 531 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 532 | free(storage_path); |
| 533 | free(cmdopts); |
| 534 | free(fstype); |
Rob Landley | 7b363fd | 2005-12-20 17:18:01 +0000 | [diff] [blame] | 535 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 536 | |
| 537 | if(rc) |
| 538 | bb_perror_msg("Mounting %s on %s failed", |
| 539 | mtcur->mnt_fsname, mtcur->mnt_dir); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 540 | |
Rob Landley | 6e98521 | 2005-08-14 18:46:34 +0000 | [diff] [blame] | 541 | return rc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 542 | } |