blob: b1d9e287d283271f628f4048cd058adedfced998 [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.
105 * Return list of unrecognized options in *strflags if strflags!=NULL */
106static 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
191static int useMtab;
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.
199
Rob Landleyfe908fd2006-03-29 14:30:49 +0000200static int mount_it_now(struct mntent *mp, int vfsflags, char *filteropts)
Rob Landleydc0955b2006-03-14 18:16:25 +0000201{
202 int rc;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000203
Eric Andersen19b5b8f2006-03-20 18:07:13 +0000204 if (fakeIt) { return 0; }
205
Rob Landleydc0955b2006-03-14 18:16:25 +0000206 // Mount, with fallback to read-only if necessary.
207
208 for(;;) {
209 rc = mount(mp->mnt_fsname, mp->mnt_dir, mp->mnt_type,
210 vfsflags, filteropts);
211 if(!rc || (vfsflags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
212 break;
213 bb_error_msg("%s is write-protected, mounting read-only",
214 mp->mnt_fsname);
215 vfsflags |= MS_RDONLY;
216 }
217
Rob Landleydc0955b2006-03-14 18:16:25 +0000218 // Abort entirely if permission denied.
219
220 if (rc && errno == EPERM)
221 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
222
223 /* If the mount was successful, and we're maintaining an old-style
224 * mtab file by hand, add the new entry to it now. */
Eric Andersen9601a1c2006-03-20 18:07:50 +0000225
Rob Landleydc0955b2006-03-14 18:16:25 +0000226 if(ENABLE_FEATURE_MTAB_SUPPORT && useMtab && !rc) {
227 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
228 int i;
229
230 if(!mountTable)
Denis Vlasenko6d655be2006-09-06 19:02:46 +0000231 bb_error_msg("No %s",bb_path_mtab_file);
Rob Landleydc0955b2006-03-14 18:16:25 +0000232
233 // Add vfs string flags
234
235 for(i=0; mount_options[i].flags != MS_REMOUNT; i++)
236 if (mount_options[i].flags > 0)
Rob Landleye3781b72006-08-08 01:39:49 +0000237 append_mount_options(&(mp->mnt_opts), mount_options[i].name);
Rob Landleydc0955b2006-03-14 18:16:25 +0000238
239 // Remove trailing / (if any) from directory we mounted on
240
241 i = strlen(mp->mnt_dir);
242 if(i>1 && mp->mnt_dir[i-1] == '/') mp->mnt_dir[i-1] = 0;
243
244 // Write and close.
245
246 if(!mp->mnt_type || !*mp->mnt_type) mp->mnt_type="--bind";
247 addmntent(mountTable, mp);
248 endmntent(mountTable);
249 if (ENABLE_FEATURE_CLEAN_UP)
250 if(strcmp(mp->mnt_type,"--bind")) mp->mnt_type = 0;
251 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000252
Rob Landleydc0955b2006-03-14 18:16:25 +0000253 return rc;
254}
255
Rob Landley4cb035d2006-09-05 14:00:21 +0000256// Mount one directory. Handles CIFS, NFS, loopback, autobind, and filesystem
257// type detection. Returns 0 for success, nonzero for failure.
Rob Landleydc0955b2006-03-14 18:16:25 +0000258
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000259static int singlemount(struct mntent *mp, int ignore_busy)
Rob Landleydc0955b2006-03-14 18:16:25 +0000260{
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000261 int rc = -1, vfsflags;
Rob Landleyfe908fd2006-03-29 14:30:49 +0000262 char *loopFile = 0, *filteropts = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000263 llist_t *fl = 0;
264 struct stat st;
265
Rob Landleyfe908fd2006-03-29 14:30:49 +0000266 vfsflags = parse_mount_options(mp->mnt_opts, &filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000267
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000268 // Treat fstype "auto" as unspecified.
269
270 if (mp->mnt_type && !strcmp(mp->mnt_type,"auto")) mp->mnt_type = 0;
271
Rob Landley89d9d4d2006-09-01 08:10:44 +0000272 // Might this be an CIFS filesystem?
273
274 if(ENABLE_FEATURE_MOUNT_CIFS &&
275 (!mp->mnt_type || !strcmp(mp->mnt_type,"cifs")) &&
276 (mp->mnt_fsname[0]==mp->mnt_fsname[1] && (mp->mnt_fsname[0]=='/' || mp->mnt_fsname[0]=='\\')))
277 {
278 struct hostent *he;
279 char ip[32], *s;
280
281 rc = 1;
282 // Replace '/' with '\' and verify that unc points to "//server/share".
283
284 for (s = mp->mnt_fsname; *s; ++s)
285 if (*s == '/') *s = '\\';
286
287 // get server IP
288
289 s = strrchr(mp->mnt_fsname, '\\');
290 if (s == mp->mnt_fsname+1) goto report_error;
291 *s = 0;
292 he = gethostbyname(mp->mnt_fsname+2);
293 *s = '\\';
294 if (!he) goto report_error;
295
296 // Insert ip=... option into string flags. (NOTE: Add IPv6 support.)
297
298 sprintf(ip, "ip=%d.%d.%d.%d", he->h_addr[0], he->h_addr[1],
299 he->h_addr[2], he->h_addr[3]);
300 parse_mount_options(ip, &filteropts);
301
302 // compose new unc '\\server-ip\share'
303
Rob Landley4cb035d2006-09-05 14:00:21 +0000304 s = xasprintf("\\\\%s%s",ip+3,strchr(mp->mnt_fsname+2,'\\'));
Rob Landley89d9d4d2006-09-01 08:10:44 +0000305 if (ENABLE_FEATURE_CLEAN_UP) free(mp->mnt_fsname);
306 mp->mnt_fsname = s;
307
308 // lock is required
309 vfsflags |= MS_MANDLOCK;
310
311 mp->mnt_type = "cifs";
312 rc = mount_it_now(mp, vfsflags, filteropts);
313 goto report_error;
314 }
315
Rob Landleydc0955b2006-03-14 18:16:25 +0000316 // Might this be an NFS filesystem?
317
318 if (ENABLE_FEATURE_MOUNT_NFS &&
319 (!mp->mnt_type || !strcmp(mp->mnt_type,"nfs")) &&
320 strchr(mp->mnt_fsname, ':') != NULL)
321 {
Rob Landleyfe908fd2006-03-29 14:30:49 +0000322 if (nfsmount(mp->mnt_fsname, mp->mnt_dir, &vfsflags, &filteropts, 1)) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000323 bb_perror_msg("nfsmount failed");
Rob Landleyfe908fd2006-03-29 14:30:49 +0000324 } else {
325 // Strangely enough, nfsmount() doesn't actually mount() anything.
Rob Landley19af2792006-04-05 01:43:39 +0000326 mp->mnt_type = "nfs";
Rob Landleyfe908fd2006-03-29 14:30:49 +0000327 rc = mount_it_now(mp, vfsflags, filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000328 }
Rob Landley89d9d4d2006-09-01 08:10:44 +0000329 goto report_error;
Rob Landleydc0955b2006-03-14 18:16:25 +0000330 }
331
Rob Landleyab873602006-04-04 16:56:04 +0000332 // Look at the file. (Not found isn't a failure for remount, or for
333 // a synthetic filesystem like proc or sysfs.)
Rob Landleydc0955b2006-03-14 18:16:25 +0000334
Rob Landley3ba7bd12006-08-09 19:51:13 +0000335 if (!lstat(mp->mnt_fsname, &st) && !(vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
Rob Landleye3781b72006-08-08 01:39:49 +0000336 {
Rob Landleydc0955b2006-03-14 18:16:25 +0000337 // Do we need to allocate a loopback device for it?
Eric Andersen9601a1c2006-03-20 18:07:50 +0000338
Rob Landleydc0955b2006-03-14 18:16:25 +0000339 if (ENABLE_FEATURE_MOUNT_LOOP && S_ISREG(st.st_mode)) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000340 loopFile = bb_simplify_path(mp->mnt_fsname);
Rob Landleydc0955b2006-03-14 18:16:25 +0000341 mp->mnt_fsname = 0;
342 switch(set_loop(&(mp->mnt_fsname), loopFile, 0)) {
343 case 0:
344 case 1:
345 break;
346 default:
347 bb_error_msg( errno == EPERM || errno == EACCES
348 ? bb_msg_perm_denied_are_you_root
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000349 : "cannot setup loop device");
Rob Landleydc0955b2006-03-14 18:16:25 +0000350 return errno;
351 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000352
Rob Landleydc0955b2006-03-14 18:16:25 +0000353 // Autodetect bind mounts
354
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000355 } else if (S_ISDIR(st.st_mode) && !mp->mnt_type)
356 vfsflags |= MS_BIND;
Rob Landleydc0955b2006-03-14 18:16:25 +0000357 }
358
359 /* If we know the fstype (or don't need to), jump straight
360 * to the actual mount. */
361
362 if (mp->mnt_type || (vfsflags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
Rob Landleyfe908fd2006-03-29 14:30:49 +0000363 rc = mount_it_now(mp, vfsflags, filteropts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000364
365 // Loop through filesystem types until mount succeeds or we run out
366
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000367 else {
Rob Landleydc0955b2006-03-14 18:16:25 +0000368
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000369 /* Initialize list of block backed filesystems. This has to be
370 * done here so that during "mount -a", mounts after /proc shows up
371 * can autodetect. */
Rob Landleydc0955b2006-03-14 18:16:25 +0000372
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000373 if (!fslist) {
374 fslist = get_block_backed_filesystems();
375 if (ENABLE_FEATURE_CLEAN_UP && fslist)
376 atexit(delete_block_backed_filesystems);
377 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000378
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000379 for (fl = fslist; fl; fl = fl->link) {
380 mp->mnt_type = fl->data;
381
Rob Landleyfe908fd2006-03-29 14:30:49 +0000382 if (!(rc = mount_it_now(mp,vfsflags, filteropts))) break;
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000383
384 mp->mnt_type = 0;
385 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000386 }
387
Rob Landleyfe908fd2006-03-29 14:30:49 +0000388 // If mount failed, clean up loop file (if any).
389
Bernhard Reutner-Fischer08421e12006-05-26 14:05:48 +0000390 if (ENABLE_FEATURE_MOUNT_LOOP && rc && loopFile) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000391 del_loop(mp->mnt_fsname);
Rob Landleyfe908fd2006-03-29 14:30:49 +0000392 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000393 free(loopFile);
394 free(mp->mnt_fsname);
395 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000396 }
Rob Landley89d9d4d2006-09-01 08:10:44 +0000397
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000398report_error:
Rob Landley89d9d4d2006-09-01 08:10:44 +0000399 if (ENABLE_FEATURE_CLEAN_UP) free(filteropts);
400
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000401 if (rc && errno == EBUSY && ignore_busy) rc = 0;
402 if (rc < 0)
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000403 bb_perror_msg("mounting %s on %s failed", mp->mnt_fsname, mp->mnt_dir);
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000404
Rob Landleydc0955b2006-03-14 18:16:25 +0000405 return rc;
406}
407
Rob Landleydc0955b2006-03-14 18:16:25 +0000408// Parse options, if necessary parse fstab/mtab, and call singlemount for
409// each directory to be mounted.
410
411int mount_main(int argc, char **argv)
412{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000413 char *cmdopts = xstrdup(""), *fstabname, *fstype=0, *storage_path=0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000414 FILE *fstab;
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000415 int i, opt, all = FALSE, rc = 0;
Rob Landleydc0955b2006-03-14 18:16:25 +0000416 struct mntent mtpair[2], *mtcur = mtpair;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000417
Rob Landley6a6798b2005-08-10 20:35:54 +0000418 /* parse long options, like --bind and --move. Note that -o option
419 * and --option are synonymous. Yes, this means --remount,rw works. */
420
Rob Landleydc0955b2006-03-14 18:16:25 +0000421 for (i = opt = 0; i < argc; i++) {
422 if (argv[i][0] == '-' && argv[i][1] == '-') {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000423 append_mount_options(&cmdopts,argv[i]+2);
Rob Landleydc0955b2006-03-14 18:16:25 +0000424 } else argv[opt++] = argv[i];
Rob Landley6a6798b2005-08-10 20:35:54 +0000425 }
426 argc = opt;
427
428 // Parse remaining options
429
Rob Landleydc0955b2006-03-14 18:16:25 +0000430 while ((opt = getopt(argc, argv, "o:t:rwavnf")) > 0) {
Matt Kraaia3045df2001-04-17 04:48:51 +0000431 switch (opt) {
Rob Landleydc0955b2006-03-14 18:16:25 +0000432 case 'o':
433 append_mount_options(&cmdopts, optarg);
434 break;
435 case 't':
436 fstype = optarg;
437 break;
438 case 'r':
439 append_mount_options(&cmdopts, "ro");
440 break;
441 case 'w':
442 append_mount_options(&cmdopts, "rw");
443 break;
444 case 'a':
445 all = TRUE;
446 break;
447 case 'n':
448 USE_FEATURE_MTAB_SUPPORT(useMtab = FALSE;)
449 break;
450 case 'f':
451 USE_FEATURE_MTAB_SUPPORT(fakeIt = FALSE;)
452 break;
453 case 'v':
454 break; // ignore -v
455 default:
456 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 }
Matt Kraaia3045df2001-04-17 04:48:51 +0000458 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000459
Rob Landleydc0955b2006-03-14 18:16:25 +0000460 // Three or more non-option arguments? Die with a usage message.
461
462 if (optind-argc>2) bb_show_usage();
Eric Andersen9601a1c2006-03-20 18:07:50 +0000463
Rob Landley6a6798b2005-08-10 20:35:54 +0000464 // If we have no arguments, show currently mounted filesystems
465
Rob Landleydc0955b2006-03-14 18:16:25 +0000466 if (optind == argc) {
467 if (!all) {
468 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
Rob Landley6a6798b2005-08-10 20:35:54 +0000469
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000470 if(!mountTable) bb_error_msg_and_die("no %s",bb_path_mtab_file);
Rob Landley6a6798b2005-08-10 20:35:54 +0000471
Rob Landleydc0955b2006-03-14 18:16:25 +0000472 while (getmntent_r(mountTable,mtpair,bb_common_bufsiz1,
473 sizeof(bb_common_bufsiz1)))
474 {
475 // Don't show rootfs.
476 if (!strcmp(mtpair->mnt_fsname, "rootfs")) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000477
Rob Landleydc0955b2006-03-14 18:16:25 +0000478 if (!fstype || !strcmp(mtpair->mnt_type, fstype))
479 printf("%s on %s type %s (%s)\n", mtpair->mnt_fsname,
480 mtpair->mnt_dir, mtpair->mnt_type,
481 mtpair->mnt_opts);
482 }
483 if (ENABLE_FEATURE_CLEAN_UP) endmntent(mountTable);
484 return EXIT_SUCCESS;
Rob Landley6a6798b2005-08-10 20:35:54 +0000485 }
Rob Landley4d609cb2006-09-04 19:43:26 +0000486 } else storage_path = bb_simplify_path(argv[optind]);
Matt Kraai12400822001-04-17 04:32:50 +0000487
Rob Landleydc0955b2006-03-14 18:16:25 +0000488 // When we have two arguments, the second is the directory and we can
489 // skip looking at fstab entirely. We can always abspath() the directory
490 // argument when we get it.
Rob Landley6a6798b2005-08-10 20:35:54 +0000491
Rob Landleydc0955b2006-03-14 18:16:25 +0000492 if (optind+2 == argc) {
493 mtpair->mnt_fsname = argv[optind];
494 mtpair->mnt_dir = argv[optind+1];
495 mtpair->mnt_type = fstype;
496 mtpair->mnt_opts = cmdopts;
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000497 rc = singlemount(mtpair, 0);
Rob Landleydc0955b2006-03-14 18:16:25 +0000498 goto clean_up;
499 }
Rob Landley6a6798b2005-08-10 20:35:54 +0000500
Rob Landley3ba7bd12006-08-09 19:51:13 +0000501 // If we have a shared subtree flag, don't worry about fstab or mtab.
502 i = parse_mount_options(cmdopts,0);
503 if (ENABLE_FEATURE_MOUNT_FLAGS &&
504 (i & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE )))
505 {
506 rc = mount("", argv[optind], "", i, "");
507 if (rc) bb_perror_msg_and_die("%s", argv[optind]);
508 goto clean_up;
509 }
510
Rob Landleydc0955b2006-03-14 18:16:25 +0000511 // Open either fstab or mtab
Rob Landley6a6798b2005-08-10 20:35:54 +0000512
Rob Landleydc0955b2006-03-14 18:16:25 +0000513 if (parse_mount_options(cmdopts,0) & MS_REMOUNT)
Rob Landley721b46e2006-08-08 12:54:02 +0000514 fstabname = bb_path_mtab_file;
Rob Landleydc0955b2006-03-14 18:16:25 +0000515 else fstabname="/etc/fstab";
Eric Andersen9601a1c2006-03-20 18:07:50 +0000516
Rob Landleydc0955b2006-03-14 18:16:25 +0000517 if (!(fstab=setmntent(fstabname,"r")))
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000518 bb_perror_msg_and_die("cannot read %s",fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000519
Rob Landleydc0955b2006-03-14 18:16:25 +0000520 // Loop through entries until we find what we're looking for.
Rob Landley6a6798b2005-08-10 20:35:54 +0000521
Rob Landleydc0955b2006-03-14 18:16:25 +0000522 memset(mtpair,0,sizeof(mtpair));
523 for (;;) {
524 struct mntent *mtnext = mtpair + (mtcur==mtpair ? 1 : 0);
Rob Landley6a6798b2005-08-10 20:35:54 +0000525
526 // Get next fstab entry
527
Rob Landley20fef962006-04-01 17:32:52 +0000528 if (!getmntent_r(fstab, mtcur, bb_common_bufsiz1
529 + (mtcur==mtpair ? sizeof(bb_common_bufsiz1)/2 : 0),
530 sizeof(bb_common_bufsiz1)/2))
Rob Landleydc0955b2006-03-14 18:16:25 +0000531 {
532 // Were we looking for something specific?
533
534 if (optind != argc) {
535
536 // If we didn't find anything, complain.
537
538 if (!mtnext->mnt_fsname)
Denis Vlasenko39e93cc2006-09-10 18:38:17 +0000539 bb_error_msg_and_die("can't find %s in %s",
Rob Landleydc0955b2006-03-14 18:16:25 +0000540 argv[optind], fstabname);
Eric Andersen9601a1c2006-03-20 18:07:50 +0000541
Rob Landleydc0955b2006-03-14 18:16:25 +0000542 // Mount the last thing we found.
543
544 mtcur = mtnext;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000545 mtcur->mnt_opts = xstrdup(mtcur->mnt_opts);
Rob Landleydc0955b2006-03-14 18:16:25 +0000546 append_mount_options(&(mtcur->mnt_opts),cmdopts);
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000547 rc = singlemount(mtcur, 0);
Rob Landleydc0955b2006-03-14 18:16:25 +0000548 free(mtcur->mnt_opts);
549 }
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000550 goto clean_up;
Rob Landley6a6798b2005-08-10 20:35:54 +0000551 }
Rob Landleyeaa34ca2006-03-18 02:58:11 +0000552
Rob Landleydc0955b2006-03-14 18:16:25 +0000553 /* If we're trying to mount something specific and this isn't it,
554 * skip it. Note we must match both the exact text in fstab (ala
555 * "proc") or a full path from root */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000556
Rob Landleydc0955b2006-03-14 18:16:25 +0000557 if (optind != argc) {
Rob Landley6a6798b2005-08-10 20:35:54 +0000558
Rob Landleydc0955b2006-03-14 18:16:25 +0000559 // Is this what we're looking for?
Rob Landley6a6798b2005-08-10 20:35:54 +0000560
Rob Landleydc0955b2006-03-14 18:16:25 +0000561 if(strcmp(argv[optind],mtcur->mnt_fsname) &&
562 strcmp(storage_path,mtcur->mnt_fsname) &&
563 strcmp(argv[optind],mtcur->mnt_dir) &&
564 strcmp(storage_path,mtcur->mnt_dir)) continue;
Rob Landley6a6798b2005-08-10 20:35:54 +0000565
Rob Landleydc0955b2006-03-14 18:16:25 +0000566 // Remember this entry. Something later may have overmounted
567 // it, and we want the _last_ match.
Rob Landley6a6798b2005-08-10 20:35:54 +0000568
Rob Landleydc0955b2006-03-14 18:16:25 +0000569 mtcur = mtnext;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000570
Rob Landleydc0955b2006-03-14 18:16:25 +0000571 // If we're mounting all.
Rob Landley6a6798b2005-08-10 20:35:54 +0000572
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000573 } else {
Eric Andersen9601a1c2006-03-20 18:07:50 +0000574
Rob Landleydc0955b2006-03-14 18:16:25 +0000575 // Do we need to match a filesystem type?
576 if (fstype && strcmp(mtcur->mnt_type,fstype)) continue;
Eric Andersen9601a1c2006-03-20 18:07:50 +0000577
Rob Landleydc0955b2006-03-14 18:16:25 +0000578 // Skip noauto and swap anyway.
579
580 if (parse_mount_options(mtcur->mnt_opts,0)
581 & (MOUNT_NOAUTO | MOUNT_SWAP)) continue;
582
583 // Mount this thing.
584
Rob Landley0cc2c2c2006-06-21 03:53:33 +0000585 if (singlemount(mtcur, 1)) {
586 /* Count number of failed mounts */
587 rc++;
Rob Landleydc0955b2006-03-14 18:16:25 +0000588 }
Glenn L McGrath3aae1002001-05-07 01:38:03 +0000589 }
Eric Andersenfdd51032000-08-02 18:48:26 +0000590 }
Rob Landleydc0955b2006-03-14 18:16:25 +0000591 if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab);
Glenn L McGrath9fef17d2002-08-22 18:41:20 +0000592
Rob Landleydc0955b2006-03-14 18:16:25 +0000593clean_up:
594
595 if (ENABLE_FEATURE_CLEAN_UP) {
596 free(storage_path);
597 free(cmdopts);
Rob Landley7b363fd2005-12-20 17:18:01 +0000598 }
Eric Andersen9601a1c2006-03-20 18:07:50 +0000599
Rob Landley6e985212005-08-14 18:46:34 +0000600 return rc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000601}