1 /* mount - mount a file system Author: Andy Tanenbaum */
12 #include <minix/config.h>
13 #include <minix/const.h>
14 #include <minix/minlib.h>
15 #include <sys/svrctl.h>
17 #include "mfs/const.h"
20 #define MINIX_FS_TYPE "mfs"
22 int main(int argc
, char **argv
);
25 void update_mtab(char *dev
, char *mountpoint
, char *fstype
, int mountflags
);
28 static int write_mtab
= 1;
34 int all
= 0, i
, v
= 0, mountflags
;
35 char **ap
, *opt
, *err
, *type
, *args
, *device
;
37 if (argc
== 1) list(); /* just list /etc/mtab */
42 for (i
= 1; i
< argc
; i
++) {
43 if (argv
[i
][0] == '-') {
45 while (*opt
!= 0) switch (*opt
++) {
46 case 'r': mountflags
|= MS_RDONLY
; break;
47 case 't': if (++i
== argc
) usage();
50 case 'i': mountflags
|= MS_REUSE
; break;
51 case 'e': mountflags
|= MS_EXISTING
; break;
52 case 'n': write_mtab
= 0; break;
53 case 'o': if (++i
== argc
) usage();
56 case 'a': all
= 1; break;
66 if (!all
&& (argc
!= 3 || *argv
[1] == 0)) usage();
72 if (!strcmp(device
, "none")) device
= NULL
;
74 if ((type
== NULL
|| !strcmp(type
, MINIX_FS_TYPE
)) && device
!= NULL
) {
75 /* auto-detect type and/or version */
76 v
= fsversion(device
, "mount");
80 case FSVERSION_MFS3
: type
= MINIX_FS_TYPE
; break;
81 case FSVERSION_EXT2
: type
= "ext2"; break;
85 if (mount(device
, argv
[2], mountflags
, type
, args
) < 0) {
86 err
= strerror(errno
);
87 fprintf(stderr
, "mount: Can't mount %s on %s: %s\n",
88 argv
[1], argv
[2], err
);
92 /* The mount has completed successfully. Tell the user. */
93 printf("%s is read-%s mounted on %s\n",
94 argv
[1], mountflags
& MS_RDONLY
? "only" : "write", argv
[2]);
96 /* Update /etc/mtab. */
97 update_mtab(argv
[1], argv
[2], type
, mountflags
);
102 update_mtab(char *dev
, char *mountpoint
, char *fstype
, int mountflags
)
106 char special
[PATH_MAX
], mounted_on
[PATH_MAX
], version
[10], rw_flag
[10];
108 if (!write_mtab
) return;
109 n
= load_mtab("mount");
110 if (n
< 0) exit(1); /* something is wrong. */
112 /* Loop on all the /etc/mtab entries, copying each one to the output
115 n
= get_mtab_entry(special
, mounted_on
, version
, rw_flag
);
117 n
= put_mtab_entry(special
, mounted_on
, version
, rw_flag
);
119 std_err("mount: /etc/mtab has grown too large\n");
123 /* For MFS, use a version number. Otherwise, use the FS type name. */
124 if (!strcmp(fstype
, MINIX_FS_TYPE
)) {
126 } else if (strlen(fstype
) < sizeof(version
)) {
131 n
= put_mtab_entry(dev
, mountpoint
, vs
,
132 (mountflags
& MS_RDONLY
? "ro" : "rw") );
134 std_err("mount: /etc/mtab has grown too large\n");
138 n
= rewrite_mtab("mount");
144 char special
[PATH_MAX
], mounted_on
[PATH_MAX
], version
[10], rw_flag
[10];
146 /* Read and print /etc/mtab. */
147 n
= load_mtab("mount");
151 n
= get_mtab_entry(special
, mounted_on
, version
, rw_flag
);
153 printf("%s is read-%s mounted on %s (type %s)\n",
154 special
, strcmp(rw_flag
, "rw") == 0 ? "write" : "only",
155 mounted_on
, version
);
161 has_opt(char *mntopts
, char *option
)
166 optbuf
= strdup(mntopts
);
167 for (opt
= optbuf
; (opt
= strtok(opt
, ",")) != NULL
; opt
= NULL
) {
168 if (!strcmp(opt
, option
)) found
= 1;
180 char mountpoint
[PATH_MAX
];
183 while ((fs
= getfsent()) != NULL
) {
187 if (realpath(fs
->fs_file
, mountpoint
) == NULL
) {
188 fprintf(stderr
, "Can't mount on %s\n", fs
->fs_file
);
189 return(EXIT_FAILURE
);
191 if (has_opt(fs
->fs_mntops
, "noauto"))
193 if (!strcmp(mountpoint
, "/"))
194 continue; /* Not remounting root */
195 if (has_opt(fs
->fs_mntops
, "ro"))
198 mountflags
|= MS_RDONLY
;
201 device
= fs
->fs_spec
;
202 /* passing a null string for block special device means don't
203 * use a device at all and this is what we need to do for
204 * entries starting with "none"
206 if (!strcmp(device
, "none"))
209 if (mount(device
, mountpoint
, mountflags
, fs
->fs_vfstype
,
210 fs
->fs_mntops
) == 0) {
211 update_mtab(fs
->fs_spec
, fs
->fs_file
, fs
->fs_vfstype
,
214 err
= strerror(errno
);
215 fprintf(stderr
, "mount: Can't mount %s on %s: %s\n",
216 fs
->fs_spec
, fs
->fs_file
, err
);
217 return(EXIT_FAILURE
);
220 return(EXIT_SUCCESS
);
225 std_err("Usage: mount [-a] [-r] [-e] [-t type] [-o options] special name\n");