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