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 | |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 54 | |
| 55 | static int use_loop = 0; |
| 56 | #endif |
| 57 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 58 | extern const char mtab_file[]; /* Defined in utility.c */ |
| 59 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 60 | static const char mount_usage[] = "\tmount [flags]\n" |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 61 | "\tmount [flags] device directory [-o options,more-options]\n" |
| 62 | "\n" |
| 63 | "Flags:\n" |
| 64 | "\t-a:\tMount all file systems in fstab.\n" |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 65 | #ifdef BB_MTAB |
| 66 | "\t-f:\t\"Fake\" mount. Add entry to mount table but don't mount it.\n" |
| 67 | "\t-n:\tDon't write a mount table entry.\n" |
| 68 | #endif |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 69 | "\t-o option:\tOne of many filesystem options, listed below.\n" |
| 70 | "\t-r:\tMount the filesystem read-only.\n" |
| 71 | "\t-t filesystem-type:\tSpecify the filesystem type.\n" |
| 72 | "\t-w:\tMount for reading and writing (default).\n" |
| 73 | "\n" |
| 74 | "Options for use with the \"-o\" flag:\n" |
| 75 | "\tasync / sync:\tWrites are asynchronous / synchronous.\n" |
| 76 | "\tdev / nodev:\tAllow use of special device files / disallow them.\n" |
| 77 | "\texec / noexec:\tAllow use of executable files / disallow them.\n" |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 78 | #if defined BB_FEATURE_MOUNT_LOOP |
| 79 | "\tloop: Mounts a file via loop device.\n" |
| 80 | #endif |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 81 | "\tsuid / nosuid:\tAllow set-user-id-root programs / disallow them.\n" |
| 82 | "\tremount: Re-mount a currently-mounted filesystem, changing its flags.\n" |
| 83 | "\tro / rw: Mount for read-only / read-write.\n" |
| 84 | "\t" |
| 85 | "There are EVEN MORE flags that are specific to each filesystem.\n" |
| 86 | "You'll have to see the written documentation for those.\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 88 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | struct mount_options { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 90 | const char *name; |
| 91 | unsigned long and; |
| 92 | unsigned long or; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 95 | static const struct mount_options mount_options[] = { |
| 96 | {"async", ~MS_SYNCHRONOUS, 0}, |
| 97 | {"defaults", ~0, 0}, |
| 98 | {"dev", ~MS_NODEV, 0}, |
| 99 | {"exec", ~MS_NOEXEC, 0}, |
| 100 | {"nodev", ~0, MS_NODEV}, |
| 101 | {"noexec", ~0, MS_NOEXEC}, |
| 102 | {"nosuid", ~0, MS_NOSUID}, |
| 103 | {"remount", ~0, MS_REMOUNT}, |
| 104 | {"ro", ~0, MS_RDONLY}, |
| 105 | {"rw", ~MS_RDONLY, 0}, |
| 106 | {"suid", ~MS_NOSUID, 0}, |
| 107 | {"sync", ~0, MS_SYNCHRONOUS}, |
| 108 | {0, 0, 0} |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 111 | static int |
| 112 | do_mount(char* specialfile, char* dir, char* filesystemtype, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 113 | long flags, void* string_flags, int useMtab, int fakeIt, char* mtab_opts) |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 114 | { |
| 115 | int status=0; |
| 116 | |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 117 | #if defined BB_MTAB |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 118 | if (fakeIt==FALSE) |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 119 | #endif |
| 120 | { |
| 121 | #if defined BB_FEATURE_MOUNT_LOOP |
| 122 | if (use_loop) { |
| 123 | int loro = flags & MS_RDONLY; |
| 124 | char *lofile = specialfile; |
| 125 | specialfile = find_unused_loop_device(); |
| 126 | if (specialfile == NULL) { |
| 127 | fprintf(stderr, "Could not find a spare loop device\n"); |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 128 | return( FALSE); |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 129 | } |
| 130 | if (set_loop (specialfile, lofile, 0, &loro)) { |
| 131 | fprintf(stderr, "Could not setup loop device\n"); |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 132 | return( FALSE); |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 133 | } |
| 134 | if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */ |
| 135 | fprintf(stderr, "WARNING: loop device is read-only\n"); |
| 136 | flags &= ~MS_RDONLY; |
| 137 | } |
| 138 | } |
| 139 | #endif |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 140 | status=mount(specialfile, dir, filesystemtype, flags, string_flags); |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 141 | } |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | /* If the mount was sucessful, do anything needed, then return TRUE */ |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 145 | if (status == 0) { |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 146 | |
| 147 | #if defined BB_MTAB |
| 148 | if (useMtab==TRUE) { |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 149 | write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts); |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 150 | } |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 151 | #endif |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 152 | return( TRUE); |
| 153 | } |
| 154 | |
| 155 | /* Bummer. mount failed. Clean up */ |
| 156 | #if defined BB_FEATURE_MOUNT_LOOP |
| 157 | if (specialfile != NULL) { |
| 158 | del_loop(specialfile); |
| 159 | } |
| 160 | #endif |
| 161 | return( FALSE); |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 164 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 165 | |
Erik Andersen | 0881de7 | 2000-01-05 09:34:26 +0000 | [diff] [blame] | 166 | #if defined BB_MTAB |
| 167 | #define whine_if_fstab_is_missing() {} |
| 168 | #else |
| 169 | extern void whine_if_fstab_is_missing() |
| 170 | { |
| 171 | struct stat statBuf; |
| 172 | if (stat("/etc/fstab", &statBuf) < 0) |
| 173 | fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n"); |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 178 | /* Seperate standard mount options from the nonstandard string options */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 179 | static void |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 180 | parse_mount_options ( char *options, unsigned long *flags, char *strflags) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 181 | { |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 182 | while (options) { |
| 183 | int gotone=FALSE; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 184 | char *comma = strchr (options, ','); |
| 185 | const struct mount_options* f = mount_options; |
| 186 | if (comma) |
| 187 | *comma = '\0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 188 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 189 | while (f->name != 0) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 190 | if (strcasecmp (f->name, options) == 0) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 191 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 192 | *flags &= f->and; |
| 193 | *flags |= f->or; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 194 | gotone=TRUE; |
| 195 | break; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 196 | } |
| 197 | f++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 198 | } |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 199 | #if defined BB_FEATURE_MOUNT_LOOP |
| 200 | if (gotone==FALSE && !strcasecmp ("loop", options)) { /* loop device support */ |
| 201 | use_loop = 1; |
| 202 | gotone=TRUE; |
| 203 | } |
| 204 | #endif |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 205 | if (*strflags && strflags!= '\0' && gotone==FALSE) { |
| 206 | char *temp=strflags; |
| 207 | temp += strlen (strflags); |
| 208 | *temp++ = ','; |
| 209 | *temp++ = '\0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 210 | } |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 211 | if (gotone==FALSE) |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 212 | strcat (strflags, options); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 213 | if (comma) { |
| 214 | *comma = ','; |
| 215 | options = ++comma; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 216 | } else { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 217 | break; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 218 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 219 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | int |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 223 | mount_one(char *blockDevice, char *directory, char *filesystemType, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 224 | 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] | 225 | { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 226 | int status = 0; |
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 | char buf[255]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 229 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 230 | #if defined BB_FEATURE_USE_PROCFS |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 231 | if (strcmp(filesystemType, "auto") == 0) { |
| 232 | FILE *f = fopen ("/proc/filesystems", "r"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 233 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 234 | if (f == NULL) |
| 235 | return( FALSE); |
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 | while (fgets (buf, sizeof (buf), f) != NULL) { |
| 238 | filesystemType = buf; |
| 239 | if (*filesystemType == '\t') { // Not a nodev filesystem |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 240 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 241 | // Add NULL termination to each line |
| 242 | while (*filesystemType && *filesystemType != '\n') |
| 243 | filesystemType++; |
| 244 | *filesystemType = '\0'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 245 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 246 | filesystemType = buf; |
| 247 | filesystemType++; // hop past tab |
| 248 | |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 249 | status = do_mount (blockDevice, directory, filesystemType, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 250 | flags | MS_MGC_VAL, string_flags, useMtab, |
| 251 | fakeIt, mtab_opts); |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 252 | if (status == TRUE) |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 253 | break; |
| 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 | fclose (f); |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 257 | } else |
| 258 | #endif |
| 259 | { |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 260 | status = do_mount (blockDevice, directory, filesystemType, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 261 | flags | MS_MGC_VAL, string_flags, useMtab, |
| 262 | fakeIt, mtab_opts); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 263 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 264 | |
Erik Andersen | 5cbdd71 | 2000-01-26 20:06:48 +0000 | [diff] [blame] | 265 | if (status==FALSE) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 266 | fprintf (stderr, "Mounting %s on %s failed: %s\n", |
| 267 | blockDevice, directory, strerror(errno)); |
| 268 | return (FALSE); |
| 269 | } |
| 270 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 273 | extern int mount_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 274 | { |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 275 | char string_flags_buf[1024]=""; |
| 276 | char *string_flags = string_flags_buf; |
| 277 | char *extra_opts = string_flags_buf; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 278 | unsigned long flags = 0; |
| 279 | char *filesystemType = "auto"; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 280 | char *device = NULL; |
| 281 | char *directory = NULL; |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 282 | int all = FALSE; |
| 283 | int fakeIt = FALSE; |
| 284 | int useMtab = TRUE; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 285 | int i; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 286 | |
Eric Andersen | 29d2e36 | 1999-11-06 06:07:27 +0000 | [diff] [blame] | 287 | /* Only compiled in if BB_MTAB is not defined */ |
| 288 | whine_if_fstab_is_missing(); |
Eric Andersen | cb6e256 | 1999-10-16 15:48:40 +0000 | [diff] [blame] | 289 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 290 | if (argc == 1) { |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 291 | FILE *mountTable = setmntent (mtab_file, "r"); |
| 292 | if (mountTable) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 293 | struct mntent *m; |
| 294 | while ((m = getmntent (mountTable)) != 0) { |
Eric Andersen | cb6e256 | 1999-10-16 15:48:40 +0000 | [diff] [blame] | 295 | struct fstab* fstabItem; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 296 | char *blockDevice = m->mnt_fsname; |
Eric Andersen | cb6e256 | 1999-10-16 15:48:40 +0000 | [diff] [blame] | 297 | /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */ |
| 298 | if (strcmp (blockDevice, "/dev/root") == 0) { |
| 299 | fstabItem = getfsfile ("/"); |
| 300 | if (fstabItem != NULL) |
| 301 | blockDevice = fstabItem->fs_spec; |
| 302 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 303 | printf ("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir, |
| 304 | m->mnt_type, m->mnt_opts); |
| 305 | } |
| 306 | endmntent (mountTable); |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 307 | } else { |
| 308 | perror(mtab_file); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 309 | } |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 310 | exit( TRUE); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 311 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 312 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 313 | |
| 314 | /* Parse options */ |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 315 | i = --argc; |
| 316 | argv++; |
| 317 | while (i > 0 && **argv) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 318 | if (**argv == '-') { |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 319 | char *opt = *argv; |
| 320 | while (i>0 && *++opt) switch (*opt) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 321 | case 'o': |
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 | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 324 | } |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 325 | parse_mount_options (*(++argv), &flags, string_flags); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 326 | break; |
| 327 | case 'r': |
| 328 | flags |= MS_RDONLY; |
| 329 | break; |
| 330 | case 't': |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 331 | if (--i == 0) { |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 332 | goto goodbye; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 333 | } |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 334 | filesystemType = *(++argv); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 335 | break; |
| 336 | case 'w': |
| 337 | flags &= ~MS_RDONLY; |
| 338 | break; |
| 339 | case 'a': |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 340 | all = TRUE; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 341 | break; |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 342 | #ifdef BB_MTAB |
| 343 | case 'f': |
| 344 | fakeIt = TRUE; |
| 345 | break; |
| 346 | case 'n': |
| 347 | useMtab = FALSE; |
| 348 | break; |
| 349 | #endif |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 350 | case 'v': |
| 351 | case 'h': |
| 352 | case '-': |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 353 | goto goodbye; |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 354 | } |
| 355 | } else { |
| 356 | if (device == NULL) |
| 357 | device=*argv; |
| 358 | else if (directory == NULL) |
| 359 | directory=*argv; |
| 360 | else { |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 361 | goto goodbye; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | i--; |
| 365 | argv++; |
| 366 | } |
| 367 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 368 | if (all == TRUE) { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 369 | struct mntent *m; |
| 370 | FILE *f = setmntent ("/etc/fstab", "r"); |
| 371 | |
| 372 | if (f == NULL) { |
| 373 | perror("/etc/fstab"); |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 374 | exit( FALSE); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 375 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 376 | while ((m = getmntent (f)) != NULL) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 377 | // If the file system isn't noauto, and isn't mounted on /, |
| 378 | // and isn't swap or nfs, then mount it |
| 379 | if ((!strstr (m->mnt_opts, "noauto")) && |
| 380 | (m->mnt_dir[1] != '\0') && |
| 381 | (!strstr (m->mnt_type, "swap")) && |
| 382 | (!strstr (m->mnt_type, "nfs"))) |
| 383 | { |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 384 | flags = 0; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 385 | *string_flags = '\0'; |
Eric Andersen | d0246fb | 1999-11-04 21:18:07 +0000 | [diff] [blame] | 386 | parse_mount_options(m->mnt_opts, &flags, string_flags); |
| 387 | mount_one (m->mnt_fsname, m->mnt_dir, m->mnt_type, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 388 | flags, string_flags, useMtab, fakeIt, extra_opts); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 389 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 390 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 391 | endmntent (f); |
| 392 | } else { |
Eric Andersen | 8341a15 | 1999-10-08 17:14:14 +0000 | [diff] [blame] | 393 | if (device && directory) { |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 394 | #ifdef BB_NFSMOUNT |
| 395 | if (strcmp(filesystemType, "nfs") == 0) { |
| 396 | if (nfsmount(device, directory, &flags, &extra_opts, &string_flags, 1) != 0) |
| 397 | exit(FALSE); |
| 398 | } |
| 399 | #endif |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 400 | exit (mount_one (device, directory, filesystemType, |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 401 | flags, string_flags, useMtab, fakeIt, extra_opts)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 402 | } else { |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 403 | goto goodbye; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 404 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 405 | } |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 406 | exit( TRUE); |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 407 | |
| 408 | goodbye: |
| 409 | usage( mount_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 410 | } |
Erik Andersen | b7cc49d | 2000-01-13 06:38:14 +0000 | [diff] [blame] | 411 | |