Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 2 | * Mini mount implementation for busybox |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | * 3/21/1999 Charles P. Wright <cpwright@cpwright.com> |
| 21 | * searches through fstab when -a is passed |
| 22 | * will try mounting stuff with all fses when passed -t auto |
| 23 | * |
| 24 | * 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 25 | * |
| 26 | * 1999-10-07 Erik Andersen <andersen@lineo.com>, <andersee@debian.org>. |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame^] | 27 | * Rewrite of a lot of code. Removed mtab usage (I plan on |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 28 | * putting it back as a compile-time option some time), |
| 29 | * major adjustments to option parsing, and some serious |
| 30 | * dieting all around. |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 31 | * |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame^] | 32 | * 1999-11-06 mtab suppport is back - andersee |
| 33 | * |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 34 | * 2000-01-12 Ben Collins <bcollins@debian.org>, Borrowed utils-linux's |
| 35 | * mount to add loop support. |
| 36 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | |
| 38 | #include "internal.h" |
| 39 | #include <stdlib.h> |
| 40 | #include <unistd.h> |
| 41 | #include <errno.h> |
| 42 | #include <string.h> |
| 43 | #include <stdio.h> |
| 44 | #include <mntent.h> |
| 45 | #include <sys/mount.h> |
| 46 | #include <ctype.h> |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 47 | #include <fstab.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 49 | #if defined BB_FEATURE_MOUNT_LOOP |
| 50 | #include <fcntl.h> |
| 51 | #include <sys/ioctl.h> |
| 52 | #include <linux/loop.h> |
| 53 | |
| 54 | static int set_loop(const char *device, const char *file, int offset, int *loopro); |
| 55 | static char *find_unused_loop_device (void); |
| 56 | |
| 57 | static int use_loop = 0; |
| 58 | #endif |
| 59 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 60 | extern const char mtab_file[]; /* Defined in utility.c */ |
| 61 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 62 | static const char mount_usage[] = "\tmount [flags]\n" |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 63 | "\tmount [flags] device directory [-o options,more-options]\n" |
| 64 | "\n" |
| 65 | "Flags:\n" |
| 66 | "\t-a:\tMount all file systems in fstab.\n" |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 67 | #ifdef BB_MTAB |
| 68 | "\t-f:\t\"Fake\" mount. Add entry to mount table but don't mount it.\n" |
| 69 | "\t-n:\tDon't write a mount table entry.\n" |
| 70 | #endif |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 71 | "\t-o option:\tOne of many filesystem options, listed below.\n" |
| 72 | "\t-r:\tMount the filesystem read-only.\n" |
| 73 | "\t-t filesystem-type:\tSpecify the filesystem type.\n" |
| 74 | "\t-w:\tMount for reading and writing (default).\n" |
| 75 | "\n" |
| 76 | "Options for use with the \"-o\" flag:\n" |
| 77 | "\tasync / sync:\tWrites are asynchronous / synchronous.\n" |
| 78 | "\tdev / nodev:\tAllow use of special device files / disallow them.\n" |
| 79 | "\texec / noexec:\tAllow use of executable files / disallow them.\n" |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 80 | #if defined BB_FEATURE_MOUNT_LOOP |
| 81 | "\tloop: Mounts a file via loop device.\n" |
| 82 | #endif |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 83 | "\tsuid / nosuid:\tAllow set-user-id-root programs / disallow them.\n" |
| 84 | "\tremount: Re-mount a currently-mounted filesystem, changing its flags.\n" |
| 85 | "\tro / rw: Mount for read-only / read-write.\n" |
| 86 | "\t" |
| 87 | "There are EVEN MORE flags that are specific to each filesystem.\n" |
| 88 | "You'll have to see the written documentation for those.\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 90 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 91 | struct mount_options { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 92 | const char *name; |
| 93 | unsigned long and; |
| 94 | unsigned long or; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 97 | static const struct mount_options mount_options[] = { |
| 98 | {"async", ~MS_SYNCHRONOUS, 0}, |
| 99 | {"defaults", ~0, 0}, |
| 100 | {"dev", ~MS_NODEV, 0}, |
| 101 | {"exec", ~MS_NOEXEC, 0}, |
| 102 | {"nodev", ~0, MS_NODEV}, |
| 103 | {"noexec", ~0, MS_NOEXEC}, |
| 104 | {"nosuid", ~0, MS_NOSUID}, |
| 105 | {"remount", ~0, MS_REMOUNT}, |
| 106 | {"ro", ~0, MS_RDONLY}, |
| 107 | {"rw", ~MS_RDONLY, 0}, |
| 108 | {"suid", ~MS_NOSUID, 0}, |
| 109 | {"sync", ~0, MS_SYNCHRONOUS}, |
| 110 | {0, 0, 0} |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 113 | static int |
| 114 | do_mount(char* specialfile, char* dir, char* filesystemtype, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 115 | long flags, void* string_flags, int useMtab, int fakeIt, char* mtab_opts) |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 116 | { |
| 117 | int status=0; |
| 118 | |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 119 | #if defined BB_MTAB |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 120 | if (fakeIt==FALSE) |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 121 | #endif |
| 122 | { |
| 123 | #if defined BB_FEATURE_MOUNT_LOOP |
| 124 | if (use_loop) { |
| 125 | int loro = flags & MS_RDONLY; |
| 126 | char *lofile = specialfile; |
| 127 | specialfile = find_unused_loop_device(); |
| 128 | if (specialfile == NULL) { |
| 129 | fprintf(stderr, "Could not find a spare loop device\n"); |
| 130 | exit(1); |
| 131 | } |
| 132 | if (set_loop (specialfile, lofile, 0, &loro)) { |
| 133 | fprintf(stderr, "Could not setup loop device\n"); |
| 134 | exit(1); |
| 135 | } |
| 136 | if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */ |
| 137 | fprintf(stderr, "WARNING: loop device is read-only\n"); |
| 138 | flags &= ~MS_RDONLY; |
| 139 | } |
| 140 | } |
| 141 | #endif |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 142 | status=mount(specialfile, dir, filesystemtype, flags, string_flags); |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 143 | } |
| 144 | #if defined BB_MTAB |
| 145 | if (status == 0) { |
| 146 | if (useMtab==TRUE) |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 147 | write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts); |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 148 | return 0; |
| 149 | } |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 150 | else |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 151 | #endif |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 152 | return(status); |
| 153 | } |
| 154 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 155 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 156 | |
Erik Andersen | 0881de7 | 2000-01-05 09:34:26 +0000 | [diff] [blame] | 157 | #if defined BB_MTAB |
| 158 | #define whine_if_fstab_is_missing() {} |
| 159 | #else |
| 160 | extern void whine_if_fstab_is_missing() |
| 161 | { |
| 162 | struct stat statBuf; |
| 163 | if (stat("/etc/fstab", &statBuf) < 0) |
| 164 | fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n"); |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 169 | /* Seperate standard mount options from the nonstandard string options */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 170 | static void |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 171 | parse_mount_options ( char *options, unsigned long *flags, char *strflags) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 172 | { |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 173 | while (options) { |
| 174 | int gotone=FALSE; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 175 | char *comma = strchr (options, ','); |
| 176 | const struct mount_options* f = mount_options; |
| 177 | if (comma) |
| 178 | *comma = '\0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 179 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 180 | while (f->name != 0) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 181 | if (strcasecmp (f->name, options) == 0) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 182 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 183 | *flags &= f->and; |
| 184 | *flags |= f->or; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 185 | gotone=TRUE; |
| 186 | break; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 187 | } |
| 188 | f++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 189 | } |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 190 | #if defined BB_FEATURE_MOUNT_LOOP |
| 191 | if (gotone==FALSE && !strcasecmp ("loop", options)) { /* loop device support */ |
| 192 | use_loop = 1; |
| 193 | gotone=TRUE; |
| 194 | } |
| 195 | #endif |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 196 | if (*strflags && strflags!= '\0' && gotone==FALSE) { |
| 197 | char *temp=strflags; |
| 198 | temp += strlen (strflags); |
| 199 | *temp++ = ','; |
| 200 | *temp++ = '\0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 201 | } |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 202 | if (gotone==FALSE) |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 203 | strcat (strflags, options); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 204 | if (comma) { |
| 205 | *comma = ','; |
| 206 | options = ++comma; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 207 | } else { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 208 | break; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 209 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 210 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | int |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 214 | mount_one(char *blockDevice, char *directory, char *filesystemType, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 215 | unsigned long flags, char *string_flags, int useMtab, int fakeIt, char *mtab_opts) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 216 | { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 217 | int status = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 218 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 219 | char buf[255]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 220 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 221 | #if defined BB_FEATURE_USE_PROCFS |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 222 | if (strcmp(filesystemType, "auto") == 0) { |
| 223 | FILE *f = fopen ("/proc/filesystems", "r"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 224 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 225 | if (f == NULL) |
| 226 | return( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 227 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 228 | while (fgets (buf, sizeof (buf), f) != NULL) { |
| 229 | filesystemType = buf; |
| 230 | if (*filesystemType == '\t') { // Not a nodev filesystem |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 231 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 232 | // Add NULL termination to each line |
| 233 | while (*filesystemType && *filesystemType != '\n') |
| 234 | filesystemType++; |
| 235 | *filesystemType = '\0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 236 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 237 | filesystemType = buf; |
| 238 | filesystemType++; // hop past tab |
| 239 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 240 | status = do_mount (blockDevice, directory, filesystemType, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 241 | flags | MS_MGC_VAL, string_flags, useMtab, |
| 242 | fakeIt, mtab_opts); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 243 | if (status == 0) |
| 244 | break; |
| 245 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 246 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 247 | fclose (f); |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 248 | } else |
| 249 | #endif |
| 250 | { |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 251 | status = do_mount (blockDevice, directory, filesystemType, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 252 | flags | MS_MGC_VAL, string_flags, useMtab, |
| 253 | fakeIt, mtab_opts); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 254 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 255 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 256 | if (status) { |
| 257 | fprintf (stderr, "Mounting %s on %s failed: %s\n", |
| 258 | blockDevice, directory, strerror(errno)); |
| 259 | return (FALSE); |
| 260 | } |
| 261 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 264 | extern int mount_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 265 | { |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 266 | char string_flags_buf[1024]=""; |
| 267 | char *string_flags = string_flags_buf; |
| 268 | char *extra_opts = string_flags_buf; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 269 | unsigned long flags = 0; |
| 270 | char *filesystemType = "auto"; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 271 | char *device = NULL; |
| 272 | char *directory = NULL; |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 273 | int all = FALSE; |
| 274 | int fakeIt = FALSE; |
| 275 | int useMtab = TRUE; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 276 | int i; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 277 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 278 | /* Only compiled in if BB_MTAB is not defined */ |
| 279 | whine_if_fstab_is_missing(); |
Eric Andersen | cb6e256 | 1999-10-16 15:48:40 +0000 | [diff] [blame] | 280 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 281 | if (argc == 1) { |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 282 | FILE *mountTable = setmntent (mtab_file, "r"); |
| 283 | if (mountTable) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 284 | struct mntent *m; |
| 285 | while ((m = getmntent (mountTable)) != 0) { |
Eric Andersen | cb6e256 | 1999-10-16 15:48:40 +0000 | [diff] [blame] | 286 | struct fstab* fstabItem; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 287 | char *blockDevice = m->mnt_fsname; |
Eric Andersen | cb6e256 | 1999-10-16 15:48:40 +0000 | [diff] [blame] | 288 | /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */ |
| 289 | if (strcmp (blockDevice, "/dev/root") == 0) { |
| 290 | fstabItem = getfsfile ("/"); |
| 291 | if (fstabItem != NULL) |
| 292 | blockDevice = fstabItem->fs_spec; |
| 293 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 294 | printf ("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir, |
| 295 | m->mnt_type, m->mnt_opts); |
| 296 | } |
| 297 | endmntent (mountTable); |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 298 | } else { |
| 299 | perror(mtab_file); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 300 | } |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 301 | exit( TRUE); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 302 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 303 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 304 | |
| 305 | /* Parse options */ |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 306 | i = --argc; |
| 307 | argv++; |
| 308 | while (i > 0 && **argv) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 309 | if (**argv == '-') { |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 310 | char *opt = *argv; |
| 311 | while (i>0 && *++opt) switch (*opt) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 312 | case 'o': |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 313 | if (--i == 0) { |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 314 | goto goodbye; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 315 | } |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 316 | parse_mount_options (*(++argv), &flags, string_flags); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 317 | break; |
| 318 | case 'r': |
| 319 | flags |= MS_RDONLY; |
| 320 | break; |
| 321 | case 't': |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 322 | if (--i == 0) { |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 323 | goto goodbye; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 324 | } |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 325 | filesystemType = *(++argv); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 326 | break; |
| 327 | case 'w': |
| 328 | flags &= ~MS_RDONLY; |
| 329 | break; |
| 330 | case 'a': |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 331 | all = TRUE; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 332 | break; |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 333 | #ifdef BB_MTAB |
| 334 | case 'f': |
| 335 | fakeIt = TRUE; |
| 336 | break; |
| 337 | case 'n': |
| 338 | useMtab = FALSE; |
| 339 | break; |
| 340 | #endif |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 341 | case 'v': |
| 342 | case 'h': |
| 343 | case '-': |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 344 | goto goodbye; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 345 | } |
| 346 | } else { |
| 347 | if (device == NULL) |
| 348 | device=*argv; |
| 349 | else if (directory == NULL) |
| 350 | directory=*argv; |
| 351 | else { |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 352 | goto goodbye; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | i--; |
| 356 | argv++; |
| 357 | } |
| 358 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 359 | if (all == TRUE) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 360 | struct mntent *m; |
| 361 | FILE *f = setmntent ("/etc/fstab", "r"); |
| 362 | |
| 363 | if (f == NULL) { |
| 364 | perror("/etc/fstab"); |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 365 | exit( FALSE); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 366 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 367 | while ((m = getmntent (f)) != NULL) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 368 | // If the file system isn't noauto, and isn't mounted on /, |
| 369 | // and isn't swap or nfs, then mount it |
| 370 | if ((!strstr (m->mnt_opts, "noauto")) && |
| 371 | (m->mnt_dir[1] != '\0') && |
| 372 | (!strstr (m->mnt_type, "swap")) && |
| 373 | (!strstr (m->mnt_type, "nfs"))) |
| 374 | { |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 375 | flags = 0; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 376 | *string_flags = '\0'; |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 377 | parse_mount_options(m->mnt_opts, &flags, string_flags); |
| 378 | mount_one (m->mnt_fsname, m->mnt_dir, m->mnt_type, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 379 | flags, string_flags, useMtab, fakeIt, extra_opts); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 380 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 381 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 382 | endmntent (f); |
| 383 | } else { |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 384 | if (device && directory) { |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 385 | #ifdef BB_NFSMOUNT |
| 386 | if (strcmp(filesystemType, "nfs") == 0) { |
| 387 | if (nfsmount(device, directory, &flags, &extra_opts, &string_flags, 1) != 0) |
| 388 | exit(FALSE); |
| 389 | } |
| 390 | #endif |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 391 | exit (mount_one (device, directory, filesystemType, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 392 | flags, string_flags, useMtab, fakeIt, extra_opts)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 393 | } else { |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 394 | goto goodbye; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 395 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 396 | } |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 397 | exit( TRUE); |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 398 | |
| 399 | goodbye: |
| 400 | usage( mount_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 401 | } |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 402 | |
| 403 | #if defined BB_FEATURE_MOUNT_LOOP |
| 404 | static int set_loop(const char *device, const char *file, int offset, int *loopro) |
| 405 | { |
| 406 | struct loop_info loopinfo; |
| 407 | int fd, ffd, mode; |
| 408 | |
| 409 | mode = *loopro ? O_RDONLY : O_RDWR; |
| 410 | if ((ffd = open (file, mode)) < 0 && !*loopro |
| 411 | && (errno != EROFS || (ffd = open (file, mode = O_RDONLY)) < 0)) { |
| 412 | perror (file); |
| 413 | return 1; |
| 414 | } |
| 415 | if ((fd = open (device, mode)) < 0) { |
| 416 | close(ffd); |
| 417 | perror (device); |
| 418 | return 1; |
| 419 | } |
| 420 | *loopro = (mode == O_RDONLY); |
| 421 | |
| 422 | memset(&loopinfo, 0, sizeof(loopinfo)); |
| 423 | strncpy(loopinfo.lo_name, file, LO_NAME_SIZE); |
| 424 | loopinfo.lo_name[LO_NAME_SIZE-1] = 0; |
| 425 | |
| 426 | loopinfo.lo_offset = offset; |
| 427 | |
| 428 | loopinfo.lo_encrypt_key_size = 0; |
| 429 | if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { |
| 430 | perror("ioctl: LOOP_SET_FD"); |
| 431 | exit(1); |
| 432 | } |
| 433 | if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { |
| 434 | (void) ioctl(fd, LOOP_CLR_FD, 0); |
| 435 | perror("ioctl: LOOP_SET_STATUS"); |
| 436 | exit(1); |
| 437 | } |
| 438 | close(fd); |
| 439 | close(ffd); |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static char *find_unused_loop_device (void) |
| 444 | { |
| 445 | char dev[20]; |
| 446 | int i, fd, somedev = 0, someloop = 0; |
| 447 | struct stat statbuf; |
| 448 | struct loop_info loopinfo; |
| 449 | |
| 450 | for(i = 0; i < 256; i++) { |
| 451 | sprintf(dev, "/dev/loop%d", i); |
| 452 | if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) { |
| 453 | somedev++; |
| 454 | fd = open (dev, O_RDONLY); |
| 455 | if (fd >= 0) { |
| 456 | if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0) |
| 457 | someloop++; /* in use */ |
| 458 | else if (errno == ENXIO) { |
| 459 | close (fd); |
| 460 | return strdup(dev); /* probably free */ |
| 461 | } |
| 462 | close (fd); |
| 463 | } |
| 464 | continue; |
| 465 | } |
| 466 | if (i >= 7) |
| 467 | break; |
| 468 | } |
| 469 | return NULL; |
| 470 | } |
| 471 | #endif /* BB_FEATURE_MOUNT_LOOP */ |