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(). |
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 | |
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 | |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 65 | // vfs flags |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 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}, |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 82 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 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))) |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 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); |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 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 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +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 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 146 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 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; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 162 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 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; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 167 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 168 | while(isspace(*fs)) fs++; |
| 169 | if(*fs=='#' || *fs=='*') continue; |
| 170 | if(!*fs) continue; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 171 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 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 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 182 | #if ENABLE_FEATURE_CLEAN_UP |
| 183 | static void delete_block_backed_filesystems(void) |
| 184 | { |
| 185 | llist_free(fslist); |
| 186 | } |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 187 | #else |
| 188 | void delete_block_backed_filesystems(void); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 189 | #endif |
| 190 | |
| 191 | #if ENABLE_FEATURE_MTAB_SUPPORT |
| 192 | static int useMtab; |
Eric Andersen | 19b5b8f | 2006-03-20 18:07:13 +0000 | [diff] [blame] | 193 | static int fakeIt; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 194 | #else |
| 195 | #define useMtab 0 |
Eric Andersen | 19b5b8f | 2006-03-20 18:07:13 +0000 | [diff] [blame] | 196 | #define fakeIt 0 |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 197 | #endif |
| 198 | |
| 199 | // Perform actual mount of specific filesystem at specific location. |
| 200 | |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 201 | static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 202 | { |
| 203 | int rc; |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 204 | |
Eric Andersen | 19b5b8f | 2006-03-20 18:07:13 +0000 | [diff] [blame] | 205 | if (fakeIt) { return 0; } |
| 206 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 207 | // Mount, with fallback to read-only if necessary. |
| 208 | |
| 209 | for(;;) { |
| 210 | rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type, |
| 211 | vfsflags, filteropts); |
| 212 | if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS)) |
| 213 | break; |
| 214 | bb_error_msg("%s is write-protected, mounting read-only", |
| 215 | mp->mnt_fsname); |
| 216 | vfsflags |= MS_RDONLY; |
| 217 | } |
| 218 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 219 | // Abort entirely if permission denied. |
| 220 | |
| 221 | if (rc && errno == EPERM) |
| 222 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
| 223 | |
| 224 | /* If the mount was successful, and we're maintaining an old-style |
| 225 | * mtab file by hand, add the new entry to it now. */ |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 226 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 227 | if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) { |
| 228 | FILE *mountTable = setmntent(bb_path_mtab_file, "a+"); |
| 229 | int i; |
| 230 | |
| 231 | if(!mountTable) |
| 232 | bb_error_msg("No %s\n",bb_path_mtab_file); |
| 233 | |
| 234 | // Add vfs string flags |
| 235 | |
| 236 | for(i=0; mount_options[i].flags != MS_REMOUNT; i++) |
| 237 | if (mount_options[i].flags > 0) |
| 238 | append_mount_options(&(mp->mnt_opts), |
| 239 | // Shut up about the darn const. It's not important. I don't care. |
| 240 | (char *)mount_options[i].name); |
| 241 | |
| 242 | // Remove trailing / (if any) from directory we mounted on |
| 243 | |
| 244 | i = strlen(mp->mnt_dir); |
| 245 | if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0; |
| 246 | |
| 247 | // Write and close. |
| 248 | |
| 249 | if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind"; |
| 250 | addmntent(mountTable, mp); |
| 251 | endmntent(mountTable); |
| 252 | if (ENABLE_FEATURE_CLEAN_UP) |
| 253 | if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0; |
| 254 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 255 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 256 | return rc; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | // Mount one directory. Handles NFS, loopback, autobind, and filesystem type |
| 261 | // detection. Returns 0 for success, nonzero for failure. |
| 262 | |
| 263 | static int singlemount(struct mntent *mp) |
| 264 | { |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 265 | int rc = 1, vfsflags; |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 266 | char *loopFile = 0, *filteropts = 0; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 267 | llist_t *fl = 0; |
| 268 | struct stat st; |
| 269 | |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 270 | vfsflags = parse_mount_options(mp->mnt_opts, &filteropts); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 271 | |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 272 | // Treat fstype "auto" as unspecified. |
| 273 | |
| 274 | if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0; |
| 275 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 276 | // Might this be an NFS filesystem? |
| 277 | |
| 278 | if (ENABLE_FEATURE_MOUNT_NFS && |
| 279 | (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) && |
| 280 | strchr(mp->mnt_fsname, ':') != NULL) |
| 281 | { |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 282 | if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &filteropts, 1)) { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 283 | bb_perror_msg("nfsmount failed"); |
| 284 | return 1; |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 285 | } else { |
| 286 | // Strangely enough, nfsmount() doesn't actually mount() anything. |
| 287 | rc = mount_it_now(mp, vfsflags, filteropts); |
| 288 | if (ENABLE_FEATURE_CLEAN_UP) free(filteropts); |
| 289 | |
| 290 | return rc; |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 291 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Rob Landley | ab87360 | 2006-04-04 16:56:04 +0000 | [diff] [blame^] | 294 | // Look at the file. (Not found isn't a failure for remount, or for |
| 295 | // a synthetic filesystem like proc or sysfs.) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 296 | |
| 297 | if (lstat(mp->mnt_fsname, &st)); |
Rob Landley | ab87360 | 2006-04-04 16:56:04 +0000 | [diff] [blame^] | 298 | else if (!(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 299 | // Do we need to allocate a loopback device for it? |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 300 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 301 | if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) { |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 302 | loopFile = bb_simplify_path(mp->mnt_fsname); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 303 | mp->mnt_fsname = 0; |
| 304 | switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) { |
| 305 | case 0: |
| 306 | case 1: |
| 307 | break; |
| 308 | default: |
| 309 | bb_error_msg( errno == EPERM || errno == EACCES |
| 310 | ? bb_msg_perm_denied_are_you_root |
| 311 | : "Couldn't setup loop device"); |
| 312 | return errno; |
| 313 | } |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 314 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 315 | // Autodetect bind mounts |
| 316 | |
| 317 | } else if (S_ISDIR(st.st_mode) && !mp->mnt_type) vfsflags |= MS_BIND; |
| 318 | } |
| 319 | |
| 320 | /* If we know the fstype (or don't need to), jump straight |
| 321 | * to the actual mount. */ |
| 322 | |
| 323 | if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE))) |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 324 | rc = mount_it_now(mp, vfsflags, filteropts); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 325 | |
| 326 | // Loop through filesystem types until mount succeeds or we run out |
| 327 | |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 328 | else { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 329 | |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 330 | /* Initialize list of block backed filesystems. This has to be |
| 331 | * done here so that during "mount -a", mounts after /proc shows up |
| 332 | * can autodetect. */ |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 333 | |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 334 | if (!fslist) { |
| 335 | fslist = get_block_backed_filesystems(); |
| 336 | if (ENABLE_FEATURE_CLEAN_UP && fslist) |
| 337 | atexit(delete_block_backed_filesystems); |
| 338 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 339 | |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 340 | for (fl = fslist; fl; fl = fl->link) { |
| 341 | mp->mnt_type = fl->data; |
| 342 | |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 343 | if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break; |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 344 | |
| 345 | mp->mnt_type = 0; |
| 346 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 349 | if (ENABLE_FEATURE_CLEAN_UP) free(filteropts); |
| 350 | |
| 351 | // If mount failed, clean up loop file (if any). |
| 352 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 353 | if (rc && loopFile) { |
| 354 | del_loop(mp->mnt_fsname); |
Rob Landley | fe908fd | 2006-03-29 14:30:49 +0000 | [diff] [blame] | 355 | if (ENABLE_FEATURE_CLEAN_UP) { |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 356 | free(loopFile); |
| 357 | free(mp->mnt_fsname); |
| 358 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 359 | } |
| 360 | return rc; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | // Parse options, if necessary parse fstab/mtab, and call singlemount for |
| 365 | // each directory to be mounted. |
| 366 | |
| 367 | int mount_main(int argc, char **argv) |
| 368 | { |
| 369 | char *cmdopts = bb_xstrdup(""), *fstabname, *fstype=0, *storage_path=0; |
| 370 | FILE *fstab; |
| 371 | int i, opt, all = FALSE, rc = 1; |
| 372 | struct mntent mtpair[2], *mtcur = mtpair; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 373 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 374 | /* parse long options, like --bind and --move. Note that -o option |
| 375 | * and --option are synonymous. Yes, this means --remount,rw works. */ |
| 376 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 377 | for (i = opt = 0; i < argc; i++) { |
| 378 | if (argv[i][0] == '-' && argv[i][1] == '-') { |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 379 | append_mount_options(&cmdopts,argv[i]+2); |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 380 | } else argv[opt++] = argv[i]; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 381 | } |
| 382 | argc = opt; |
| 383 | |
| 384 | // Parse remaining options |
| 385 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 386 | while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) { |
Matt Kraai | a3045df | 2001-04-17 04:48:51 +0000 | [diff] [blame] | 387 | switch (opt) { |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 388 | case 'o': |
| 389 | append_mount_options(&cmdopts, optarg); |
| 390 | break; |
| 391 | case 't': |
| 392 | fstype = optarg; |
| 393 | break; |
| 394 | case 'r': |
| 395 | append_mount_options(&cmdopts, "ro"); |
| 396 | break; |
| 397 | case 'w': |
| 398 | append_mount_options(&cmdopts, "rw"); |
| 399 | break; |
| 400 | case 'a': |
| 401 | all = TRUE; |
| 402 | break; |
| 403 | case 'n': |
| 404 | USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;) |
| 405 | break; |
| 406 | case 'f': |
| 407 | USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;) |
| 408 | break; |
| 409 | case 'v': |
| 410 | break; // ignore -v |
| 411 | default: |
| 412 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 413 | } |
Matt Kraai | a3045df | 2001-04-17 04:48:51 +0000 | [diff] [blame] | 414 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 415 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 416 | // Three or more non-option arguments? Die with a usage message. |
| 417 | |
| 418 | if (optind-argc>2) bb_show_usage(); |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 419 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 420 | // If we have no arguments, show currently mounted filesystems |
| 421 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 422 | if (optind == argc) { |
| 423 | if (!all) { |
| 424 | FILE *mountTable = setmntent(bb_path_mtab_file, "r"); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 425 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 426 | 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] | 427 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 428 | while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1, |
| 429 | sizeof(bb_common_bufsiz1))) |
| 430 | { |
| 431 | // Don't show rootfs. |
| 432 | if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 433 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 434 | if (!fstype || !strcmp(mtpair->mnt_type, fstype)) |
| 435 | printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname, |
| 436 | mtpair->mnt_dir, mtpair->mnt_type, |
| 437 | mtpair->mnt_opts); |
| 438 | } |
| 439 | if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable); |
| 440 | return EXIT_SUCCESS; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 441 | } |
Glenn L McGrath | 8042f65 | 2002-08-23 06:17:46 +0000 | [diff] [blame] | 442 | } |
Matt Kraai | 1240082 | 2001-04-17 04:32:50 +0000 | [diff] [blame] | 443 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 444 | // When we have two arguments, the second is the directory and we can |
| 445 | // skip looking at fstab entirely. We can always abspath() the directory |
| 446 | // argument when we get it. |
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 (optind+2 == argc) { |
| 449 | mtpair->mnt_fsname = argv[optind]; |
| 450 | mtpair->mnt_dir = argv[optind+1]; |
| 451 | mtpair->mnt_type = fstype; |
| 452 | mtpair->mnt_opts = cmdopts; |
| 453 | rc = singlemount(mtpair); |
| 454 | goto clean_up; |
| 455 | } |
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 | // If we have at least one argument, it's the storage location |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 458 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 459 | if (optind < argc) storage_path = bb_simplify_path(argv[optind]); |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 460 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 461 | // Open either fstab or mtab |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 462 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 463 | if (parse_mount_options(cmdopts,0) & MS_REMOUNT) |
| 464 | fstabname = (char *)bb_path_mtab_file; // Again with the evil const. |
| 465 | else fstabname="/etc/fstab"; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 466 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 467 | if (!(fstab=setmntent(fstabname,"r"))) |
| 468 | bb_perror_msg_and_die("Cannot read %s",fstabname); |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 469 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 470 | // Loop through entries until we find what we're looking for. |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 471 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 472 | memset(mtpair,0,sizeof(mtpair)); |
| 473 | for (;;) { |
| 474 | struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 475 | |
| 476 | // Get next fstab entry |
| 477 | |
Rob Landley | 20fef96 | 2006-04-01 17:32:52 +0000 | [diff] [blame] | 478 | if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1 |
| 479 | + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0), |
| 480 | sizeof(bb_common_bufsiz1)/2)) |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 481 | { |
| 482 | // Were we looking for something specific? |
| 483 | |
| 484 | if (optind != argc) { |
| 485 | |
| 486 | // If we didn't find anything, complain. |
| 487 | |
| 488 | if (!mtnext->mnt_fsname) |
| 489 | bb_error_msg_and_die("Can't find %s in %s", |
| 490 | argv[optind], fstabname); |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 491 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 492 | // Mount the last thing we found. |
| 493 | |
| 494 | mtcur = mtnext; |
| 495 | mtcur->mnt_opts=bb_xstrdup(mtcur->mnt_opts); |
| 496 | append_mount_options(&(mtcur->mnt_opts),cmdopts); |
| 497 | rc = singlemount(mtcur); |
| 498 | free(mtcur->mnt_opts); |
| 499 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 500 | break; |
| 501 | } |
Rob Landley | eaa34ca | 2006-03-18 02:58:11 +0000 | [diff] [blame] | 502 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 503 | /* If we're trying to mount something specific and this isn't it, |
| 504 | * skip it. Note we must match both the exact text in fstab (ala |
| 505 | * "proc") or a full path from root */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 506 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 507 | if (optind != argc) { |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 508 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 509 | // Is this what we're looking for? |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 510 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 511 | if(strcmp(argv[optind],mtcur->mnt_fsname) && |
| 512 | strcmp(storage_path,mtcur->mnt_fsname) && |
| 513 | strcmp(argv[optind],mtcur->mnt_dir) && |
| 514 | strcmp(storage_path,mtcur->mnt_dir)) continue; |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 515 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 516 | // Remember this entry. Something later may have overmounted |
| 517 | // it, and we want the _last_ match. |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 518 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 519 | mtcur = mtnext; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 520 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 521 | // If we're mounting all. |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 522 | |
Glenn L McGrath | 3aae100 | 2001-05-07 01:38:03 +0000 | [diff] [blame] | 523 | } else { |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 524 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 525 | // Do we need to match a filesystem type? |
| 526 | if (fstype && strcmp(mtcur->mnt_type,fstype)) continue; |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 527 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 528 | // Skip noauto and swap anyway. |
| 529 | |
| 530 | if (parse_mount_options(mtcur->mnt_opts,0) |
| 531 | & (MOUNT_NOAUTO | MOUNT_SWAP)) continue; |
| 532 | |
| 533 | // Mount this thing. |
| 534 | |
| 535 | rc = singlemount(mtcur); |
| 536 | if (rc) { |
| 537 | // Don't whine about already mounted fs when mounting all. |
| 538 | if (errno == EBUSY) rc = 0; |
| 539 | else break; |
| 540 | } |
Glenn L McGrath | 3aae100 | 2001-05-07 01:38:03 +0000 | [diff] [blame] | 541 | } |
Eric Andersen | fdd5103 | 2000-08-02 18:48:26 +0000 | [diff] [blame] | 542 | } |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 543 | if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 544 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 545 | clean_up: |
| 546 | |
| 547 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 548 | free(storage_path); |
| 549 | free(cmdopts); |
Rob Landley | 7b363fd | 2005-12-20 17:18:01 +0000 | [diff] [blame] | 550 | } |
Eric Andersen | 9601a1c | 2006-03-20 18:07:50 +0000 | [diff] [blame] | 551 | |
Rob Landley | dc0955b | 2006-03-14 18:16:25 +0000 | [diff] [blame] | 552 | if(rc) |
| 553 | bb_perror_msg("Mounting %s on %s failed", |
| 554 | mtcur->mnt_fsname, mtcur->mnt_dir); |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 555 | |
Rob Landley | 6e98521 | 2005-08-14 18:46:34 +0000 | [diff] [blame] | 556 | return rc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 557 | } |