8777a3be495a12f9bafc190a29fc4d71b7c2d5ee
[oweals/busybox.git] / mount.c
1 /*
2  * Mini mount implementation for busybox
3  *
4  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
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.
25  *
26  * 1999-10-07   Erik Andersen <andersen@lineo.com>, <andersee@debian.org>.
27  *              Rewrote of a lot of code. Removed mtab usage (I plan on
28  *              putting it back as a compile-time option some time), 
29  *              major adjustments to option parsing, and some serious 
30  *              dieting all around.
31  *
32  * 2000-01-12   Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
33  *              mount to add loop support.
34  */
35
36 #include "internal.h"
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <mntent.h>
43 #include <sys/mount.h>
44 #include <ctype.h>
45 #include <fstab.h>
46
47 #if defined BB_FEATURE_MOUNT_LOOP
48 #include <fcntl.h>
49 #include <sys/ioctl.h>
50 #include <linux/loop.h>
51
52 static int set_loop(const char *device, const char *file, int offset, int *loopro);
53 static char *find_unused_loop_device (void);
54
55 static int use_loop = 0;
56 #endif
57
58 extern const char mtab_file[]; /* Defined in utility.c */
59
60 static const char mount_usage[] = "\tmount [flags]\n"
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"
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
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"
78 #if defined BB_FEATURE_MOUNT_LOOP
79     "\tloop: Mounts a file via loop device.\n"
80 #endif
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";
87
88
89 struct mount_options {
90     const char *name;
91     unsigned long and;
92     unsigned long or;
93 };
94
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}
109 };
110
111 static int
112 do_mount(char* specialfile, char* dir, char* filesystemtype, 
113         long flags, void* string_flags, int useMtab, int fakeIt, char* mtab_opts)
114 {
115     int status=0;
116
117 #if defined BB_MTAB
118     if (fakeIt==FALSE)
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");
128                 exit(1);
129             }
130             if (set_loop (specialfile, lofile, 0, &loro)) {
131                 fprintf(stderr, "Could not setup loop device\n");
132                 exit(1);
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
140         status=mount(specialfile, dir, filesystemtype, flags, string_flags);
141     }
142 #if defined BB_MTAB
143     if (status == 0) {
144         if (useMtab==TRUE)
145             write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts);
146         return 0;
147     }
148     else
149 #endif
150         return(status);
151 }
152
153
154
155 #if defined BB_MTAB
156 #define whine_if_fstab_is_missing() {} 
157 #else
158 extern void whine_if_fstab_is_missing()
159 {
160     struct stat statBuf;
161     if (stat("/etc/fstab", &statBuf) < 0) 
162         fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
163 }
164 #endif
165
166
167 /* Seperate standard mount options from the nonstandard string options */
168 static void
169 parse_mount_options ( char *options, unsigned long *flags, char *strflags)
170 {
171     while (options) {
172         int gotone=FALSE;
173         char *comma = strchr (options, ',');
174         const struct mount_options* f = mount_options;
175         if (comma)
176             *comma = '\0';
177
178         while (f->name != 0) {
179             if (strcasecmp (f->name, options) == 0) {
180
181                 *flags &= f->and;
182                 *flags |= f->or;
183                 gotone=TRUE;
184                 break;
185             }
186             f++;
187         }
188 #if defined BB_FEATURE_MOUNT_LOOP
189         if (gotone==FALSE && !strcasecmp ("loop", options)) { /* loop device support */
190             use_loop = 1;
191             gotone=TRUE;
192         }
193 #endif
194         if (*strflags && strflags!= '\0' && gotone==FALSE) {
195             char *temp=strflags;
196             temp += strlen (strflags);
197             *temp++ = ',';
198             *temp++ = '\0';
199         }
200         if (gotone==FALSE)
201             strcat (strflags, options);
202         if (comma) {
203             *comma = ',';
204             options = ++comma;
205         } else {
206             break;
207         }
208     }
209 }
210
211 int
212 mount_one(char *blockDevice, char *directory, char *filesystemType,
213            unsigned long flags, char *string_flags, int useMtab, int fakeIt, char *mtab_opts)
214 {
215     int status = 0;
216
217     char buf[255];
218
219 #if defined BB_FEATURE_USE_PROCFS
220     if (strcmp(filesystemType, "auto") == 0) {
221         FILE *f = fopen ("/proc/filesystems", "r");
222
223         if (f == NULL)
224             return( FALSE);
225
226         while (fgets (buf, sizeof (buf), f) != NULL) {
227             filesystemType = buf;
228             if (*filesystemType == '\t') {      // Not a nodev filesystem
229
230                 // Add NULL termination to each line
231                 while (*filesystemType && *filesystemType != '\n')
232                     filesystemType++;
233                 *filesystemType = '\0';
234
235                 filesystemType = buf;
236                 filesystemType++;       // hop past tab
237
238                 status = do_mount (blockDevice, directory, filesystemType,
239                                 flags | MS_MGC_VAL, string_flags, useMtab, 
240                                 fakeIt, mtab_opts);
241                 if (status == 0)
242                     break;
243             }
244         }
245         fclose (f);
246     } else
247 #endif
248     {
249         status = do_mount (blockDevice, directory, filesystemType,
250                         flags | MS_MGC_VAL, string_flags, useMtab, 
251                         fakeIt, mtab_opts);
252     }
253
254     if (status) {
255         fprintf (stderr, "Mounting %s on %s failed: %s\n",
256                  blockDevice, directory, strerror(errno));
257         return (FALSE);
258     }
259     return (TRUE);
260 }
261
262 extern int mount_main (int argc, char **argv)
263 {
264     char string_flags_buf[1024]="";
265     char *string_flags = string_flags_buf;
266     char *extra_opts = string_flags_buf;
267     unsigned long flags = 0;
268     char *filesystemType = "auto";
269     char *device = NULL;
270     char *directory = NULL;
271     int all = FALSE;
272     int fakeIt = FALSE;
273     int useMtab = TRUE;
274     int i;
275
276     /* Only compiled in if BB_MTAB is not defined */
277     whine_if_fstab_is_missing();
278
279     if (argc == 1) {
280         FILE *mountTable = setmntent (mtab_file, "r");
281         if (mountTable) {
282             struct mntent *m;
283             while ((m = getmntent (mountTable)) != 0) {
284                 struct fstab* fstabItem;
285                 char *blockDevice = m->mnt_fsname;
286                 /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */
287                 if (strcmp (blockDevice, "/dev/root") == 0) {
288                     fstabItem = getfsfile ("/");
289                     if (fstabItem != NULL)
290                         blockDevice = fstabItem->fs_spec;
291                 }
292                 printf ("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
293                         m->mnt_type, m->mnt_opts);
294             }
295             endmntent (mountTable);
296         } else {
297             perror(mtab_file);
298         }
299         exit( TRUE);
300     }
301
302
303     /* Parse options */
304     i = --argc;
305     argv++;
306     while (i > 0 && **argv) {
307         if (**argv == '-') {
308             char *opt = *argv;
309             while (i>0 && *++opt) switch (*opt) {
310             case 'o':
311                 if (--i == 0) {
312                     goto goodbye;
313                 }
314                 parse_mount_options (*(++argv), &flags, string_flags);
315                 break;
316             case 'r':
317                 flags |= MS_RDONLY;
318                 break;
319             case 't':
320                 if (--i == 0) {
321                     goto goodbye;
322                 }
323                 filesystemType = *(++argv);
324                 break;
325             case 'w':
326                 flags &= ~MS_RDONLY;
327                 break;
328             case 'a':
329                 all = TRUE;
330                 break;
331 #ifdef BB_MTAB
332             case 'f':
333                 fakeIt = TRUE;
334                 break;
335             case 'n':
336                 useMtab = FALSE;
337                 break;
338 #endif
339             case 'v':
340             case 'h':
341             case '-':
342                 goto goodbye;
343             }
344         } else {
345             if (device == NULL)
346                 device=*argv;
347             else if (directory == NULL)
348                 directory=*argv;
349             else {
350                 goto goodbye;
351             }
352         }
353         i--;
354         argv++;
355     }
356
357     if (all == TRUE) {
358         struct mntent *m;
359         FILE *f = setmntent ("/etc/fstab", "r");
360
361         if (f == NULL) {
362             perror("/etc/fstab");
363             exit( FALSE); 
364         }
365         while ((m = getmntent (f)) != NULL) {
366             // If the file system isn't noauto, and isn't mounted on /, 
367             // and isn't swap or nfs, then mount it
368             if ((!strstr (m->mnt_opts, "noauto")) &&
369                     (m->mnt_dir[1] != '\0') && 
370                     (!strstr (m->mnt_type, "swap")) && 
371                     (!strstr (m->mnt_type, "nfs"))) 
372             {
373                 flags = 0;
374                 *string_flags = '\0';
375                 parse_mount_options(m->mnt_opts, &flags, string_flags);
376                 mount_one (m->mnt_fsname, m->mnt_dir, m->mnt_type, 
377                         flags, string_flags, useMtab, fakeIt, extra_opts);
378             }
379         }
380         endmntent (f);
381     } else {
382         if (device && directory) {
383 #ifdef BB_NFSMOUNT
384             if (strcmp(filesystemType, "nfs") == 0) {
385                 if (nfsmount(device, directory, &flags, &extra_opts, &string_flags, 1) != 0)
386                 exit(FALSE);
387             }
388 #endif
389             exit (mount_one (device, directory, filesystemType, 
390                         flags, string_flags, useMtab, fakeIt, extra_opts));
391         } else {
392             goto goodbye;
393         }
394     }
395     exit( TRUE);
396
397 goodbye:
398     usage( mount_usage);
399 }
400
401 #if defined BB_FEATURE_MOUNT_LOOP
402 static int set_loop(const char *device, const char *file, int offset, int *loopro)
403 {
404         struct loop_info loopinfo;
405         int     fd, ffd, mode;
406         
407         mode = *loopro ? O_RDONLY : O_RDWR;
408         if ((ffd = open (file, mode)) < 0 && !*loopro
409             && (errno != EROFS || (ffd = open (file, mode = O_RDONLY)) < 0)) {
410           perror (file);
411           return 1;
412         }
413         if ((fd = open (device, mode)) < 0) {
414           close(ffd);
415           perror (device);
416           return 1;
417         }
418         *loopro = (mode == O_RDONLY);
419
420         memset(&loopinfo, 0, sizeof(loopinfo));
421         strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
422         loopinfo.lo_name[LO_NAME_SIZE-1] = 0;
423
424         loopinfo.lo_offset = offset;
425
426         loopinfo.lo_encrypt_key_size = 0;
427         if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
428                 perror("ioctl: LOOP_SET_FD");
429                 exit(1);
430         }
431         if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
432                 (void) ioctl(fd, LOOP_CLR_FD, 0);
433                 perror("ioctl: LOOP_SET_STATUS");
434                 exit(1);
435         }
436         close(fd);
437         close(ffd);
438         return 0;
439 }
440
441 static char *find_unused_loop_device (void)
442 {
443     char dev[20];
444     int i, fd, somedev = 0, someloop = 0;
445     struct stat statbuf;
446     struct loop_info loopinfo;
447
448     for(i = 0; i < 256; i++) {
449       sprintf(dev, "/dev/loop%d", i);
450       if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
451         somedev++;
452         fd = open (dev, O_RDONLY);
453         if (fd >= 0) {
454           if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
455             someloop++; /* in use */
456           else if (errno == ENXIO) {
457             close (fd);
458             return strdup(dev); /* probably free */
459           }
460           close (fd);
461         }
462         continue;
463       }
464       if (i >= 7)
465         break;
466     }
467     return NULL;
468 }
469 #endif /* BB_FEATURE_MOUNT_LOOP */