blob: 4ed000e8816c3650ae8b51a5043ee1b37275db5b [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersen596e5461999-10-07 08:30:23 +00003 * Mini mount implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Rob Landleydc0955b2006-03-14 18:16:25 +00007 * Copyright (C) 2005-2006 by Rob Landley <rob@landley.net>
Eric Andersen596e5461999-10-07 08:30:23 +00008 *
Rob Landley7b363fd2005-12-20 17:18:01 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Erik Andersenb7cc49d2000-01-13 06:38:14 +000010 */
Eric Andersencc8ed391999-10-05 16:24:54 +000011
Rob Landleydc0955b2006-03-14 18:16:25 +000012/* todo:
13 * bb_getopt_ulflags();
14 */
15
Rob Landley22d26fc2006-06-15 15:49:36 +000016/* Design notes: There is no spec for mount. Remind me to write one.
Rob Landleydc0955b2006-03-14 18:16:25 +000017
18 mount_main() calls singlemount() which calls mount_it_now().
Eric Andersen9601a1c2006-03-20 18:07:50 +000019
Rob Landleydc0955b2006-03-14 18:16:25 +000020 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 Landley22d26fc2006-06-15 15:49:36 +000025#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include <mntent.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000027
Rob Landleydc0955b2006-03-14 18:16:25 +000028// 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)
Rob Landley3ba7bd12006-08-09 19:51:13 +000031
Rob Landleydc0955b2006-03-14 18:16:25 +000032/* Standard mount options (from -o options or --options), with corresponding
33 * flags */
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Rob Landley6a6798b2005-08-10 20:35:54 +000035struct {
Rob Landleye3781b72006-08-08 01:39:49 +000036 char *name;
Rob Landley6a6798b2005-08-10 20:35:54 +000037 long flags;
Rob Landleye3781b72006-08-08 01:39:49 +000038} static mount_options[] = {
39 // MS_FLAGS set a bit. ~MS_FLAGS disable that bit. 0 flags are NOPs.
Rob Landleydc0955b2006-03-14 18:16:25 +000040
Rob Landleye3781b72006-08-08 01:39:49 +000041 USE_FEATURE_MOUNT_LOOP(
42 {"loop", 0},
43 )
Rob Landleydc0955b2006-03-14 18:16:25 +000044
Rob Landleye3781b72006-08-08 01:39:49 +000045 USE_FEATURE_MOUNT_FSTAB(
46 {"defaults", 0},
47 {"quiet", 0},
48 {"noauto",MOUNT_NOAUTO},
49 {"swap",MOUNT_SWAP},
50 )
Rob Landleydc0955b2006-03-14 18:16:25 +000051
Rob Landleye3781b72006-08-08 01:39:49 +000052 USE_FEATURE_MOUNT_FLAGS(
53 // vfs flags
54 {"nosuid", MS_NOSUID},
55 {"suid", ~MS_NOSUID},
56 {"dev", ~MS_NODEV},
57 {"nodev", MS_NODEV},
58 {"exec", ~MS_NOEXEC},
59 {"noexec", MS_NOEXEC},
60 {"sync", MS_SYNCHRONOUS},
61 {"async", ~MS_SYNCHRONOUS},
62 {"atime", ~MS_NOATIME},
63 {"noatime", MS_NOATIME},
64 {"diratime", ~MS_NODIRATIME},
65 {"nodiratime", MS_NODIRATIME},
66 {"loud", ~MS_SILENT},
Eric Andersen9601a1c2006-03-20 18:07:50 +000067
Rob Landleye3781b72006-08-08 01:39:49 +000068 // action flags
Rob Landleydc0955b2006-03-14 18:16:25 +000069
Rob Landleye3781b72006-08-08 01:39:49 +000070 {"bind", MS_BIND},
71 {"move", MS_MOVE},
72 {"shared", MS_SHARED},
73 {"slave", MS_SLAVE},
74 {"private", MS_PRIVATE},
75 {"unbindable", MS_UNBINDABLE},
76 {"rshared", MS_SHARED|MS_RECURSIVE},
77 {"rslave", MS_SLAVE|MS_RECURSIVE},
78 {"rprivate", MS_SLAVE|MS_RECURSIVE},
79 {"runbindable", MS_UNBINDABLE|MS_RECURSIVE},
80 )
81
82 // Always understood.
83
84 {"ro", MS_RDONLY}, // vfs flag
85 {"rw", ~MS_RDONLY}, // vfs flag
86 {"remount", MS_REMOUNT}, // action flag
87
88
Eric Andersencc8ed391999-10-05 16:24:54 +000089};
90
Rob Landleydc0955b2006-03-14 18:16:25 +000091/* Append mount options to string */
92static void append_mount_options(char **oldopts, char *newopts)
Eric Andersencc8ed391999-10-05 16:24:54 +000093{
Rob Landleydc0955b2006-03-14 18:16:25 +000094 if(*oldopts && **oldopts) {
Denis Vlasenko39e93cc2006-09-10 18:38:17 +000095 char *temp = xasprintf("%s,%s",*oldopts,newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +000096 free(*oldopts);
Denis Vlasenko39e93cc2006-09-10 18:38:17 +000097 *oldopts = temp;
Rob Landleydc0955b2006-03-14 18:16:25 +000098 } else {
99 if (ENABLE_FEATURE_CLEAN_UP) free(*oldopts);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000100 *oldopts = xstrdup(newopts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000101 }
102}
103
104/* Use the mount_options list to parse options into flags.
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000105 * Also return list of unrecognized options if unrecognized!=NULL */
Rob Landleydc0955b2006-03-14 18:16:25 +0000106static int parse_mount_options(char *options, char **unrecognized)
107{
108 int flags = MS_SILENT;
109
Rob Landley6a6798b2005-08-10 20:35:54 +0000110 // Loop through options
Rob Landleydc0955b2006-03-14 18:16:25 +0000111 for (;;) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000112 int i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 char *comma = strchr(options, ',');
Eric Andersencc8ed391999-10-05 16:24:54 +0000114
Rob Landleydc0955b2006-03-14 18:16:25 +0000115 if (comma) *comma = 0;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000116
Rob Landley6a6798b2005-08-10 20:35:54 +0000117 // Find this option in mount_options
Rob Landleydc0955b2006-03-14 18:16:25 +0000118 for (i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
119 if (!strcasecmp(mount_options[i].name, options)) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000120 long fl = mount_options[i].flags;
Rob Landleydc0955b2006-03-14 18:16:25 +0000121 if(fl < 0) flags &= fl;
122 else flags |= fl;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 break;
124 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000126 // If unrecognized not NULL, append unrecognized mount options */
127 if (unrecognized
128 && i == (sizeof(mount_options) / sizeof(*mount_options)))
Eric Andersen9601a1c2006-03-20 18:07:50 +0000129 {
Rob Landley6a6798b2005-08-10 20:35:54 +0000130 // Add it to strflags, to pass on to kernel
Rob Landleydc0955b2006-03-14 18:16:25 +0000131 i = *unrecognized ? strlen(*unrecognized) : 0;
132 *unrecognized = xrealloc(*unrecognized, i+strlen(options)+2);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000133
Rob Landley6a6798b2005-08-10 20:35:54 +0000134 // Comma separated if it's not the first one
Rob Landleydc0955b2006-03-14 18:16:25 +0000135 if (i) (*unrecognized)[i++] = ',';
136 strcpy((*unrecognized)+i, options);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000138
Rob Landley6a6798b2005-08-10 20:35:54 +0000139 // Advance to next option, or finish
140 if(comma) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 *comma = ',';
142 options = ++comma;
Rob Landley6a6798b2005-08-10 20:35:54 +0000143 } else break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000144 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000145
Rob Landleydc0955b2006-03-14 18:16:25 +0000146 return flags;
Eric Andersencc8ed391999-10-05 16:24:54 +0000147}
148
Rob Landleydc0955b2006-03-14 18:16:25 +0000149// Return a list of all block device backed filesystems
Matt Kraai12400822001-04-17 04:32:50 +0000150
Rob Landleydc0955b2006-03-14 18:16:25 +0000151static llist_t *get_block_backed_filesystems(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000152{
Rob Landleydc0955b2006-03-14 18:16:25 +0000153 char *fs, *buf,
154 *filesystems[] = {"/etc/filesystems", "/proc/filesystems", 0};
155 llist_t *list = 0;
156 int i;
157 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000158
Rob Landleydc0955b2006-03-14 18:16:25 +0000159 for(i = 0; filesystems[i]; i++) {
160 if(!(f = fopen(filesystems[i], "r"))) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000161
Rob Landleydc0955b2006-03-14 18:16:25 +0000162 for(fs = buf = 0; (fs = buf = bb_get_chomped_line_from_file(f));
163 free(buf))
164 {
165 if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000166
Rob Landleydc0955b2006-03-14 18:16:25 +0000167 while(isspace(*fs)) fs++;
168 if(*fs=='#' || *fs=='*') continue;
169 if(!*fs) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000170
Rob Landleyd921b2e2006-08-03 15:41:12 +0000171 llist_add_to_end(&list,xstrdup(fs));
Rob Landleydc0955b2006-03-14 18:16:25 +0000172 }
173 if (ENABLE_FEATURE_CLEAN_UP) fclose(f);
174 }
175
176 return list;
177}
178
179llist_t *fslist = 0;
180
Rob Landleydc0955b2006-03-14 18:16:25 +0000181#if ENABLE_FEATURE_CLEAN_UP
182static void delete_block_backed_filesystems(void)
183{
Rob Landleya6b5b602006-05-08 19:03:07 +0000184 llist_free(fslist, free);
Rob Landleydc0955b2006-03-14 18:16:25 +0000185}
Rob Landleyfe908fd2006-03-29 14:30:49 +0000186#else
187void delete_block_backed_filesystems(void);
Rob Landleydc0955b2006-03-14 18:16:25 +0000188#endif
189
190#if ENABLE_FEATURE_MTAB_SUPPORT
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000191static int useMtab = 1;
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000192static int fakeIt;
Rob Landleydc0955b2006-03-14 18:16:25 +0000193#else
194#define useMtab 0
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000195#define fakeIt 0
Rob Landleydc0955b2006-03-14 18:16:25 +0000196#endif
197
198// Perform actual mount of specific filesystem at specific location.
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000199int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000200{
201 int rc;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000202
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000203 if (fakeIt) { return 0; }
204
Rob Landleydc0955b2006-03-14 18:16:25 +0000205 // 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 Landleydc0955b2006-03-14 18:16:25 +0000217 // 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 Andersen9601a1c2006-03-20 18:07:50 +0000224
Rob Landleydc0955b2006-03-14 18:16:25 +0000225 if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
Denis Vlasenko727ef942006-09-14 13:19:19 +0000226 char dirbuf[PATH_MAX];
227 char srcbuf[PATH_MAX];
Rob Landleydc0955b2006-03-14 18:16:25 +0000228 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
229 int i;
230
231 if(!mountTable)
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000232 bb_error_msg("no %s",bb_path_mtab_file);
Rob Landleydc0955b2006-03-14 18:16:25 +0000233
234 // Add vfs string flags
235
236 for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
Denis Vlasenko727ef942006-09-14 13:19:19 +0000237 if (mount_options[i].flags > 0 && (mount_options[i].flags&vfsflags))
Rob Landleye3781b72006-08-08 01:39:49 +0000238 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000239
240 // Remove trailing / (if any) from directory we mounted on
241
Denis Vlasenko727ef942006-09-14 13:19:19 +0000242 i = strlen(mp->mnt_dir) - 1;
243 if(i > 0 && mp->mnt_dir[i] == '/') mp->mnt_dir[i] = 0;
244
245 // Add full pathnames as needed
246
247 if (mp->mnt_dir[0] != '/') {
248 getcwd(dirbuf, sizeof(dirbuf));
249 i = strlen(dirbuf);
250 /* strcat() would be unsafe here */
251 snprintf(dirbuf+i, sizeof(dirbuf)-i, "/%s", mp->mnt_dir);
252 mp->mnt_dir = dirbuf;
253 }
254 if (!mp->mnt_type || !*mp->mnt_type) { /* bind mount */
255 if (mp->mnt_fsname[0] != '/') {
256 getcwd(srcbuf, sizeof(srcbuf));
257 i = strlen(srcbuf);
258 snprintf(srcbuf+i, sizeof(srcbuf)-i, "/%s",
259 mp->mnt_fsname);
260 mp->mnt_fsname = srcbuf;
261 }
262 mp->mnt_type = "none";
263 mp->mnt_freq = mp->mnt_passno = 0;
264 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000265
266 // Write and close.
267
Denis Vlasenko727ef942006-09-14 13:19:19 +0000268 addmntent(mountTable, mp);
Rob Landleydc0955b2006-03-14 18:16:25 +0000269 endmntent(mountTable);
Rob Landleydc0955b2006-03-14 18:16:25 +0000270 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000271
Rob Landleydc0955b2006-03-14 18:16:25 +0000272 return rc;
273}
274
Rob Landley4cb035d2006-09-05 14:00:21 +0000275// Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
276// type detection. Returns 0 for success, nonzero for failure.
Rob Landleydc0955b2006-03-14 18:16:25 +0000277
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000278static int singlemount(struct mntent *mp, int ignore_busy)
Rob Landleydc0955b2006-03-14 18:16:25 +0000279{
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000280 int rc = -1, vfsflags;
Rob Landleyfe908fd2006-03-29 14:30:49 +0000281 char *loopFile = 0, *filteropts = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000282 llist_t *fl = 0;
283 struct stat st;
284
Rob Landleyfe908fd2006-03-29 14:30:49 +0000285 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000286
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000287 // Treat fstype "auto" as unspecified.
288
289 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
290
Rob Landley89d9d4d2006-09-01 08:10:44 +0000291 // Might this be an CIFS filesystem?
292
293 if(ENABLE_FEATURE_MOUNT_CIFS &&
294 (!mp->mnt_type || !strcmp(mp->mnt_type,"cifs")) &&
295 (mp->mnt_fsname[0]==mp->mnt_fsname[1] && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')))
296 {
297 struct hostent *he;
298 char ip[32], *s;
299
300 rc = 1;
301 // Replace '/' with '\' and verify that unc points to "//server/share".
302
303 for (s = mp->mnt_fsname; *s; ++s)
304 if (*s == '/') *s = '\\';
305
306 // get server IP
307
308 s = strrchr(mp->mnt_fsname, '\\');
309 if (s == mp->mnt_fsname+1) goto report_error;
310 *s = 0;
311 he = gethostbyname(mp->mnt_fsname+2);
312 *s = '\\';
313 if (!he) goto report_error;
314
315 // Insert ip=... option into string flags. (NOTE: Add IPv6 support.)
316
317 sprintf(ip, "ip=%d.%d.%d.%d", he->h_addr[0], he->h_addr[1],
318 he->h_addr[2], he->h_addr[3]);
319 parse_mount_options(ip, &filteropts);
320
321 // compose new unc '\\server-ip\share'
322
Rob Landley4cb035d2006-09-05 14:00:21 +0000323 s = xasprintf("\\\\%s%s",ip+3,strchr(mp->mnt_fsname+2,'\\'));
Rob Landley89d9d4d2006-09-01 08:10:44 +0000324 if (ENABLE_FEATURE_CLEAN_UP) free(mp->mnt_fsname);
325 mp->mnt_fsname = s;
326
327 // lock is required
328 vfsflags |= MS_MANDLOCK;
329
330 mp->mnt_type = "cifs";
331 rc = mount_it_now(mp, vfsflags, filteropts);
332 goto report_error;
333 }
334
Rob Landleydc0955b2006-03-14 18:16:25 +0000335 // Might this be an NFS filesystem?
336
337 if (ENABLE_FEATURE_MOUNT_NFS &&
338 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
339 strchr(mp->mnt_fsname, ':') != NULL)
340 {
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000341 rc = nfsmount(mp, vfsflags, filteropts);
Rob Landley89d9d4d2006-09-01 08:10:44 +0000342 goto report_error;
Rob Landleydc0955b2006-03-14 18:16:25 +0000343 }
344
Rob Landleyab873602006-04-04 16:56:04 +0000345 // Look at the file. (Not found isn't a failure for remount, or for
346 // a synthetic filesystem like proc or sysfs.)
Rob Landleydc0955b2006-03-14 18:16:25 +0000347
Rob Landley3ba7bd12006-08-09 19:51:13 +0000348 if (!lstat(mp->mnt_fsname, &st) && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
Rob Landleye3781b72006-08-08 01:39:49 +0000349 {
Rob Landleydc0955b2006-03-14 18:16:25 +0000350 // Do we need to allocate a loopback device for it?
Eric Andersen9601a1c2006-03-20 18:07:50 +0000351
Rob Landleydc0955b2006-03-14 18:16:25 +0000352 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000353 loopFile = bb_simplify_path(mp->mnt_fsname);
Rob Landleydc0955b2006-03-14 18:16:25 +0000354 mp->mnt_fsname = 0;
355 switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) {
356 case 0:
357 case 1:
358 break;
359 default:
360 bb_error_msg( errno == EPERM || errno == EACCES
361 ? bb_msg_perm_denied_are_you_root
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000362 : "cannot setup loop device");
Rob Landleydc0955b2006-03-14 18:16:25 +0000363 return errno;
364 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000365
Rob Landleydc0955b2006-03-14 18:16:25 +0000366 // Autodetect bind mounts
367
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000368 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
369 vfsflags |= MS_BIND;
Rob Landleydc0955b2006-03-14 18:16:25 +0000370 }
371
372 /* If we know the fstype (or don't need to), jump straight
373 * to the actual mount. */
374
375 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
Rob Landleyfe908fd2006-03-29 14:30:49 +0000376 rc = mount_it_now(mp, vfsflags, filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000377
378 // Loop through filesystem types until mount succeeds or we run out
379
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000380 else {
Rob Landleydc0955b2006-03-14 18:16:25 +0000381
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000382 /* Initialize list of block backed filesystems. This has to be
383 * done here so that during "mount -a", mounts after /proc shows up
384 * can autodetect. */
Rob Landleydc0955b2006-03-14 18:16:25 +0000385
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000386 if (!fslist) {
387 fslist = get_block_backed_filesystems();
388 if (ENABLE_FEATURE_CLEAN_UP && fslist)
389 atexit(delete_block_backed_filesystems);
390 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000391
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000392 for (fl = fslist; fl; fl = fl->link) {
393 mp->mnt_type = fl->data;
394
Rob Landleyfe908fd2006-03-29 14:30:49 +0000395 if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000396
397 mp->mnt_type = 0;
398 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000399 }
400
Rob Landleyfe908fd2006-03-29 14:30:49 +0000401 // If mount failed, clean up loop file (if any).
402
Bernhard Reutner-Fischer08421e12006-05-26 14:05:48 +0000403 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000404 del_loop(mp->mnt_fsname);
Rob Landleyfe908fd2006-03-29 14:30:49 +0000405 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000406 free(loopFile);
407 free(mp->mnt_fsname);
408 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000409 }
Rob Landley89d9d4d2006-09-01 08:10:44 +0000410
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000411report_error:
Rob Landley89d9d4d2006-09-01 08:10:44 +0000412 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
413
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000414 if (rc && errno == EBUSY && ignore_busy) rc = 0;
415 if (rc < 0)
Denis Vlasenko00d7d6c2006-09-11 17:42:44 +0000416 /* perror here sometimes says "mounting ... on ... failed: Success" */
417 bb_error_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000418
Rob Landleydc0955b2006-03-14 18:16:25 +0000419 return rc;
420}
421
Rob Landleydc0955b2006-03-14 18:16:25 +0000422// Parse options, if necessary parse fstab/mtab, and call singlemount for
423// each directory to be mounted.
424
425int mount_main(int argc, char **argv)
426{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000427 char *cmdopts = xstrdup(""), *fstabname, *fstype=0, *storage_path=0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000428 FILE *fstab;
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000429 int i, opt, all = FALSE, rc = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000430 struct mntent mtpair[2], *mtcur = mtpair;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000431
Rob Landley6a6798b2005-08-10 20:35:54 +0000432 /* parse long options, like --bind and --move. Note that -o option
433 * and --option are synonymous. Yes, this means --remount,rw works. */
434
Rob Landleydc0955b2006-03-14 18:16:25 +0000435 for (i = opt = 0; i < argc; i++) {
436 if (argv[i][0] == '-' && argv[i][1] == '-') {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000437 append_mount_options(&cmdopts,argv[i]+2);
Rob Landleydc0955b2006-03-14 18:16:25 +0000438 } else argv[opt++] = argv[i];
Rob Landley6a6798b2005-08-10 20:35:54 +0000439 }
440 argc = opt;
441
442 // Parse remaining options
443
Rob Landleydc0955b2006-03-14 18:16:25 +0000444 while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) {
Matt Kraaia3045df2001-04-17 04:48:51 +0000445 switch (opt) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000446 case 'o':
447 append_mount_options(&cmdopts, optarg);
448 break;
449 case 't':
450 fstype = optarg;
451 break;
452 case 'r':
453 append_mount_options(&cmdopts, "ro");
454 break;
455 case 'w':
456 append_mount_options(&cmdopts, "rw");
457 break;
458 case 'a':
459 all = TRUE;
460 break;
461 case 'n':
462 USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;)
463 break;
464 case 'f':
465 USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;)
466 break;
467 case 'v':
468 break; // ignore -v
469 default:
470 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 }
Matt Kraaia3045df2001-04-17 04:48:51 +0000472 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000473
Rob Landleydc0955b2006-03-14 18:16:25 +0000474 // Three or more non-option arguments? Die with a usage message.
475
476 if (optind-argc>2) bb_show_usage();
Eric Andersen9601a1c2006-03-20 18:07:50 +0000477
Rob Landley6a6798b2005-08-10 20:35:54 +0000478 // If we have no arguments, show currently mounted filesystems
479
Rob Landleydc0955b2006-03-14 18:16:25 +0000480 if (optind == argc) {
481 if (!all) {
482 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
Rob Landley6a6798b2005-08-10 20:35:54 +0000483
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000484 if(!mountTable) bb_error_msg_and_die("no %s",bb_path_mtab_file);
Rob Landley6a6798b2005-08-10 20:35:54 +0000485
Rob Landleydc0955b2006-03-14 18:16:25 +0000486 while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
487 sizeof(bb_common_bufsiz1)))
488 {
489 // Don't show rootfs.
490 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000491
Rob Landleydc0955b2006-03-14 18:16:25 +0000492 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
493 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
494 mtpair->mnt_dir, mtpair->mnt_type,
495 mtpair->mnt_opts);
496 }
497 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
498 return EXIT_SUCCESS;
Rob Landley6a6798b2005-08-10 20:35:54 +0000499 }
Rob Landley4d609cb2006-09-04 19:43:26 +0000500 } else storage_path = bb_simplify_path(argv[optind]);
Matt Kraai12400822001-04-17 04:32:50 +0000501
Rob Landleydc0955b2006-03-14 18:16:25 +0000502 // When we have two arguments, the second is the directory and we can
503 // skip looking at fstab entirely. We can always abspath() the directory
504 // argument when we get it.
Rob Landley6a6798b2005-08-10 20:35:54 +0000505
Rob Landleydc0955b2006-03-14 18:16:25 +0000506 if (optind+2 == argc) {
507 mtpair->mnt_fsname = argv[optind];
508 mtpair->mnt_dir = argv[optind+1];
509 mtpair->mnt_type = fstype;
510 mtpair->mnt_opts = cmdopts;
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000511 rc = singlemount(mtpair, 0);
Rob Landleydc0955b2006-03-14 18:16:25 +0000512 goto clean_up;
513 }
Rob Landley6a6798b2005-08-10 20:35:54 +0000514
Rob Landley3ba7bd12006-08-09 19:51:13 +0000515 // If we have a shared subtree flag, don't worry about fstab or mtab.
516 i = parse_mount_options(cmdopts,0);
517 if (ENABLE_FEATURE_MOUNT_FLAGS &&
518 (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE )))
519 {
520 rc = mount("", argv[optind], "", i, "");
521 if (rc) bb_perror_msg_and_die("%s", argv[optind]);
522 goto clean_up;
523 }
524
Rob Landleydc0955b2006-03-14 18:16:25 +0000525 // Open either fstab or mtab
Rob Landley6a6798b2005-08-10 20:35:54 +0000526
Rob Landleydc0955b2006-03-14 18:16:25 +0000527 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
Rob Landley721b46e2006-08-08 12:54:02 +0000528 fstabname = bb_path_mtab_file;
Rob Landleydc0955b2006-03-14 18:16:25 +0000529 else fstabname="/etc/fstab";
Eric Andersen9601a1c2006-03-20 18:07:50 +0000530
Rob Landleydc0955b2006-03-14 18:16:25 +0000531 if (!(fstab=setmntent(fstabname,"r")))
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000532 bb_perror_msg_and_die("cannot read %s",fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000533
Rob Landleydc0955b2006-03-14 18:16:25 +0000534 // Loop through entries until we find what we're looking for.
Rob Landley6a6798b2005-08-10 20:35:54 +0000535
Rob Landleydc0955b2006-03-14 18:16:25 +0000536 memset(mtpair,0,sizeof(mtpair));
537 for (;;) {
538 struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0);
Rob Landley6a6798b2005-08-10 20:35:54 +0000539
540 // Get next fstab entry
541
Rob Landley20fef962006-04-01 17:32:52 +0000542 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
543 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
544 sizeof(bb_common_bufsiz1)/2))
Rob Landleydc0955b2006-03-14 18:16:25 +0000545 {
546 // Were we looking for something specific?
547
548 if (optind != argc) {
549
550 // If we didn't find anything, complain.
551
552 if (!mtnext->mnt_fsname)
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000553 bb_error_msg_and_die("can't find %s in %s",
Rob Landleydc0955b2006-03-14 18:16:25 +0000554 argv[optind], fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000555
Rob Landleydc0955b2006-03-14 18:16:25 +0000556 // Mount the last thing we found.
557
558 mtcur = mtnext;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000559 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000560 append_mount_options(&(mtcur->mnt_opts),cmdopts);
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000561 rc = singlemount(mtcur, 0);
Rob Landleydc0955b2006-03-14 18:16:25 +0000562 free(mtcur->mnt_opts);
563 }
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000564 goto clean_up;
Rob Landley6a6798b2005-08-10 20:35:54 +0000565 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000566
Rob Landleydc0955b2006-03-14 18:16:25 +0000567 /* If we're trying to mount something specific and this isn't it,
568 * skip it. Note we must match both the exact text in fstab (ala
569 * "proc") or a full path from root */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000570
Rob Landleydc0955b2006-03-14 18:16:25 +0000571 if (optind != argc) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000572
Rob Landleydc0955b2006-03-14 18:16:25 +0000573 // Is this what we're looking for?
Rob Landley6a6798b2005-08-10 20:35:54 +0000574
Rob Landleydc0955b2006-03-14 18:16:25 +0000575 if(strcmp(argv[optind],mtcur->mnt_fsname) &&
576 strcmp(storage_path,mtcur->mnt_fsname) &&
577 strcmp(argv[optind],mtcur->mnt_dir) &&
578 strcmp(storage_path,mtcur->mnt_dir)) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000579
Rob Landleydc0955b2006-03-14 18:16:25 +0000580 // Remember this entry. Something later may have overmounted
581 // it, and we want the _last_ match.
Rob Landley6a6798b2005-08-10 20:35:54 +0000582
Rob Landleydc0955b2006-03-14 18:16:25 +0000583 mtcur = mtnext;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000584
Rob Landleydc0955b2006-03-14 18:16:25 +0000585 // If we're mounting all.
Rob Landley6a6798b2005-08-10 20:35:54 +0000586
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000587 } else {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000588
Rob Landleydc0955b2006-03-14 18:16:25 +0000589 // Do we need to match a filesystem type?
590 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000591
Rob Landleydc0955b2006-03-14 18:16:25 +0000592 // Skip noauto and swap anyway.
593
594 if (parse_mount_options(mtcur->mnt_opts,0)
595 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
596
597 // Mount this thing.
598
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000599 if (singlemount(mtcur, 1)) {
600 /* Count number of failed mounts */
601 rc++;
Rob Landleydc0955b2006-03-14 18:16:25 +0000602 }
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000603 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000604 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000605 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000606
Rob Landleydc0955b2006-03-14 18:16:25 +0000607clean_up:
608
609 if (ENABLE_FEATURE_CLEAN_UP) {
610 free(storage_path);
611 free(cmdopts);
Rob Landley7b363fd2005-12-20 17:18:01 +0000612 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000613
Rob Landley6e985212005-08-14 18:46:34 +0000614 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000615}