2 3/21/1999 Charles P. Wright <cpwright@cpwright.com>
3 searches through fstab when -a is passed
4 will try mounting stuff with all fses when passed -t auto
6 1999-04-17 Dave Cinege...Rewrote -t auto. Fixed ro mtab.
16 #include <sys/mount.h>
19 const char mount_usage[] = "mount\n"
20 "\t\tmount [flags] special-device directory\n"
23 "\t-a:\tMount all file systems in fstab.\n"
24 "\t-f:\t\"Fake\" mount. Add entry to mount table but don't mount it.\n"
25 "\t-n:\tDon't write a mount table entry.\n"
26 "\t-o option:\tOne of many filesystem options, listed below.\n"
27 "\t-r:\tMount the filesystem read-only.\n"
28 "\t-t filesystem-type:\tSpecify the filesystem type.\n"
29 "\t-w:\tMount for reading and writing (default).\n"
31 "Options for use with the \"-o\" flag:\n"
32 "\tasync / sync:\tWrites are asynchronous / synchronous.\n"
33 "\tdev / nodev:\tAllow use of special device files / disallow them.\n"
34 "\texec / noexec:\tAllow use of executable files / disallow them.\n"
35 "\tsuid / nosuid:\tAllow set-user-id-root programs / disallow them.\n"
36 "\tremount: Re-mount a currently-mounted filesystem, changing its flags.\n"
37 "\tro / rw: Mount for read-only / read-write.\n"
39 "There are EVEN MORE flags that are specific to each filesystem.\n"
40 "You'll have to see the written documentation for those.\n";
42 struct mount_options {
48 static const struct mount_options mount_options[] = {
49 { "async", ~MS_SYNCHRONOUS,0 },
50 { "defaults", ~0, 0 },
51 { "dev", ~MS_NODEV, 0 },
52 { "exec", ~MS_NOEXEC, 0 },
53 { "nodev", ~0, MS_NODEV },
54 { "noexec", ~0, MS_NOEXEC },
55 { "nosuid", ~0, MS_NOSUID },
56 { "remount", ~0, MS_REMOUNT },
57 { "ro", ~0, MS_RDONLY },
58 { "rw", ~MS_RDONLY, 0 },
59 { "suid", ~MS_NOSUID, 0 },
60 { "sync", ~0, MS_SYNCHRONOUS },
65 show_flags(unsigned long flags, char * buffer)
67 const struct mount_options * f = mount_options;
69 if ( flags & f->and ) {
70 int length = strlen(f->name);
71 memcpy(buffer, f->name, length);
83 ,unsigned long * flags
86 const struct mount_options * f = mount_options;
88 while ( f->name != 0 ) {
89 if ( strcasecmp(f->name, option) == 0 ) {
100 strcpy(data, option);
106 ,unsigned long * flags
110 char * comma = strchr(options, ',');
113 one_option(options, flags, data);
127 ,char * filesystemType
139 if (*filesystemType == 'a') { //Will fail on real FS starting with 'a'
141 FILE *f = fopen("/proc/filesystems", "r");
143 if (f == NULL) return 1;
145 while (fgets(buf, sizeof(buf), f) != NULL) {
146 filesystemType = buf;
147 if (*filesystemType == '\t') { // Not a nodev filesystem
149 while (*filesystemType && *filesystemType != '\n') filesystemType++;
150 *filesystemType = '\0';
152 filesystemType = buf;
153 filesystemType++; //hop past tab
155 status = mount(blockDevice, directory, filesystemType,
156 flags|MS_MGC_VAL ,string_flags);
159 if (status == 0) break;
165 status = mount( blockDevice, directory, filesystemType,
166 flags|MS_MGC_VAL ,string_flags);
172 char * s = &string_flags[strlen(string_flags)];
174 if ( s != string_flags ) {
176 show_flags(flags, s);
178 if ( !noMtab && (mountTable = setmntent("/etc/mtab", "a+")) ) {
179 int length = strlen(directory);
182 if ( length > 1 && directory[length - 1] == '/' )
183 directory[length - 1] = '\0';
185 if ( filesystemType == 0 ) {
187 = findMountPoint(blockDevice, "/proc/mounts");
189 if ( p && p->mnt_type )
190 filesystemType = p->mnt_type;
192 m.mnt_fsname = blockDevice;
193 m.mnt_dir = directory;
194 m.mnt_type = filesystemType ? filesystemType : "default";
197 m.mnt_opts = string_flags;
199 if ( (flags | MS_RDONLY) == flags )
207 addmntent(mountTable, &m);
208 endmntent(mountTable);
212 fprintf(stderr, "Mount %s", blockDevice);
213 if ( filesystemType && *filesystemType )
214 fprintf(stderr, " (type %s)", filesystemType);
226 ,"mount point %s is not a directory"
231 ,"must be superuser to use mount");
236 ,"%s already mounted or %s busy"
243 if ( stat(directory, &statbuf) != 0 )
246 ,"directory %s does not exist"
248 else if ( stat(blockDevice, &statbuf) != 0 )
251 ,"block device %s does not exist"
256 ,"%s is not mounted on %s, but the mount table says it is."
264 ,"%s is not a directory"
270 ,"wrong filesystem type, or bad superblock on %s"
274 fprintf(stderr, "mount table full");
279 ,"I/O error reading %s"
284 FILE * f = fopen("/proc/filesystems", "r");
288 ,"filesystem type %s not in kernel.\n"
290 fprintf(stderr, "Do you need to load a module?\n");
296 ,"Here are the filesystem types the kernel"
298 while ( fgets(buf, sizeof(buf), f) != 0 )
299 fprintf(stderr, "\t%s", buf);
307 ,"%s is not a block device"
313 ,"%s is not a valid block device"
317 fputs(strerror(errno), stderr);
325 mount_main(struct FileInfo * i, int argc, char * * argv)
327 char string_flags[1024];
328 unsigned long flags = 0;
329 char * filesystemType = "auto";
334 *string_flags = '\0';
338 if ( (mountTable = setmntent("/etc/mtab", "r")) ) {
340 while ( (m = getmntent(mountTable)) != 0 ) {
342 "%s on %s type %s (%s)\n"
348 endmntent(mountTable);
353 while ( argc >= 2 && argv[1][0] == '-' ) {
354 switch ( argv[1][1] ) {
366 parse_mount_options(argv[2], &flags, string_flags);
378 filesystemType = argv[2];
400 FILE *f = setmntent("/etc/fstab", "r");
406 // FIXME: Combine read routine (make new function) with unmount_all to save space.
408 while ((m = getmntent(f)) != NULL) {
409 // If the file system isn't noauto, and isn't mounted on /, mount it
410 if ((!strstr(m->mnt_opts, "noauto")) && (m->mnt_dir[1] != '\0')
411 && !((m->mnt_type[0] == 's') && (m->mnt_type[1] == 'w'))
412 && !((m->mnt_type[0] == 'n') && (m->mnt_type[1] == 'f'))) {
413 mount_one(m->mnt_fsname, m->mnt_dir, m->mnt_type, flags, m->mnt_opts, noMtab, fake);
420 if ( mount_one( argv[1], argv[2], filesystemType, flags, string_flags, noMtab, fake) == 0 )