2005-04-21 16:53:53 +02:00
|
|
|
/* mount - mount a file system Author: Andy Tanenbaum */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-08-12 21:57:37 +02:00
|
|
|
#include <sys/mount.h>
|
2009-08-18 13:36:01 +02:00
|
|
|
#include <unistd.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
#include <minix/minlib.h>
|
|
|
|
#include <stdio.h>
|
2012-06-19 18:03:11 +02:00
|
|
|
#include <fstab.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2009-05-13 17:39:44 +02:00
|
|
|
#define MINIX_FS_TYPE "mfs"
|
|
|
|
|
2012-03-24 16:16:34 +01:00
|
|
|
int main(int argc, char **argv);
|
|
|
|
void list(void);
|
|
|
|
void usage(void);
|
2012-06-19 18:03:11 +02:00
|
|
|
void update_mtab(char *dev, char *mountpoint, char *fstype, int mountflags);
|
|
|
|
int mount_all(void);
|
|
|
|
|
|
|
|
static int write_mtab = 1;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
int main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
2012-06-19 18:03:11 +02:00
|
|
|
int all = 0, i, v = 0, mountflags;
|
|
|
|
char **ap, *opt, *err, *type, *args, *device;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
if (argc == 1) list(); /* just list /etc/mtab */
|
2009-08-18 13:36:01 +02:00
|
|
|
mountflags = 0;
|
2009-05-13 17:39:44 +02:00
|
|
|
type = NULL;
|
|
|
|
args = NULL;
|
2005-04-21 16:53:53 +02:00
|
|
|
ap = argv+1;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (argv[i][0] == '-') {
|
|
|
|
opt = argv[i]+1;
|
|
|
|
while (*opt != 0) switch (*opt++) {
|
2009-08-18 13:36:01 +02:00
|
|
|
case 'r': mountflags |= MS_RDONLY; break;
|
2009-05-13 17:39:44 +02:00
|
|
|
case 't': if (++i == argc) usage();
|
|
|
|
type = argv[i];
|
|
|
|
break;
|
2010-08-03 15:57:58 +02:00
|
|
|
case 'i': mountflags |= MS_REUSE; break;
|
2010-11-23 20:34:56 +01:00
|
|
|
case 'e': mountflags |= MS_EXISTING; break;
|
2010-08-03 15:57:58 +02:00
|
|
|
case 'n': write_mtab = 0; break;
|
2009-05-13 17:39:44 +02:00
|
|
|
case 'o': if (++i == argc) usage();
|
|
|
|
args = argv[i];
|
|
|
|
break;
|
2012-06-19 18:03:11 +02:00
|
|
|
case 'a': all = 1; break;
|
2005-04-21 16:53:53 +02:00
|
|
|
default: usage();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*ap++ = argv[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ap = NULL;
|
|
|
|
argc = (ap - argv);
|
|
|
|
|
2012-06-19 18:03:11 +02:00
|
|
|
if (!all && (argc != 3 || *argv[1] == 0)) usage();
|
|
|
|
if (all == 1) {
|
|
|
|
return mount_all();
|
|
|
|
}
|
2010-01-13 00:08:50 +01:00
|
|
|
|
|
|
|
device = argv[1];
|
|
|
|
if (!strcmp(device, "none")) device = NULL;
|
|
|
|
|
2010-08-03 15:46:00 +02:00
|
|
|
if ((type == NULL || !strcmp(type, MINIX_FS_TYPE)) && device != NULL) {
|
|
|
|
/* auto-detect type and/or version */
|
|
|
|
v = fsversion(device, "mount");
|
2010-08-03 08:28:58 +02:00
|
|
|
switch (v) {
|
|
|
|
case FSVERSION_MFS1:
|
|
|
|
case FSVERSION_MFS2:
|
2010-08-03 15:46:00 +02:00
|
|
|
case FSVERSION_MFS3: type = MINIX_FS_TYPE; break;
|
2010-08-03 08:28:58 +02:00
|
|
|
case FSVERSION_EXT2: type = "ext2"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 00:08:50 +01:00
|
|
|
if (mount(device, argv[2], mountflags, type, args) < 0) {
|
2010-01-05 20:39:27 +01:00
|
|
|
err = strerror(errno);
|
2010-01-13 00:08:50 +01:00
|
|
|
fprintf(stderr, "mount: Can't mount %s on %s: %s\n",
|
|
|
|
argv[1], argv[2], err);
|
2012-06-19 18:03:11 +02:00
|
|
|
return(EXIT_FAILURE);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2010-01-13 00:08:50 +01:00
|
|
|
|
2010-01-05 20:39:27 +01:00
|
|
|
/* The mount has completed successfully. Tell the user. */
|
2010-01-13 00:08:50 +01:00
|
|
|
printf("%s is read-%s mounted on %s\n",
|
|
|
|
argv[1], mountflags & MS_RDONLY ? "only" : "write", argv[2]);
|
2010-08-03 15:57:58 +02:00
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
/* Update /etc/mtab. */
|
2012-06-19 18:03:11 +02:00
|
|
|
update_mtab(argv[1], argv[2], type, mountflags);
|
|
|
|
return(EXIT_SUCCESS);
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2012-06-19 18:03:11 +02:00
|
|
|
void
|
|
|
|
update_mtab(char *dev, char *mountpoint, char *fstype, int mountflags)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char *vs;
|
|
|
|
char special[PATH_MAX], mounted_on[PATH_MAX], version[10], rw_flag[10];
|
|
|
|
|
|
|
|
if (!write_mtab) return;
|
|
|
|
n = load_mtab("mount");
|
|
|
|
if (n < 0) exit(1); /* something is wrong. */
|
|
|
|
|
|
|
|
/* Loop on all the /etc/mtab entries, copying each one to the output
|
|
|
|
* buf. */
|
|
|
|
while (1) {
|
|
|
|
n = get_mtab_entry(special, mounted_on, version, rw_flag);
|
|
|
|
if (n < 0) break;
|
|
|
|
n = put_mtab_entry(special, mounted_on, version, rw_flag);
|
|
|
|
if (n < 0) {
|
|
|
|
std_err("mount: /etc/mtab has grown too large\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* For MFS, use a version number. Otherwise, use the FS type name. */
|
|
|
|
if (!strcmp(fstype, MINIX_FS_TYPE)) {
|
|
|
|
vs = "MFSv3";
|
|
|
|
} else if (strlen(fstype) < sizeof(version)) {
|
|
|
|
vs = fstype;
|
|
|
|
} else {
|
|
|
|
vs = "-";
|
|
|
|
}
|
|
|
|
n = put_mtab_entry(dev, mountpoint, vs,
|
|
|
|
(mountflags & MS_RDONLY ? "ro" : "rw") );
|
2005-04-21 16:53:53 +02:00
|
|
|
if (n < 0) {
|
|
|
|
std_err("mount: /etc/mtab has grown too large\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:03:11 +02:00
|
|
|
n = rewrite_mtab("mount");
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void list()
|
|
|
|
{
|
|
|
|
int n;
|
2011-11-14 12:53:05 +01:00
|
|
|
char special[PATH_MAX], mounted_on[PATH_MAX], version[10], rw_flag[10];
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Read and print /etc/mtab. */
|
|
|
|
n = load_mtab("mount");
|
|
|
|
if (n < 0) exit(1);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
n = get_mtab_entry(special, mounted_on, version, rw_flag);
|
|
|
|
if (n < 0) break;
|
2010-01-13 00:08:50 +01:00
|
|
|
printf("%s is read-%s mounted on %s (type %s)\n",
|
|
|
|
special, strcmp(rw_flag, "rw") == 0 ? "write" : "only",
|
|
|
|
mounted_on, version);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:03:11 +02:00
|
|
|
int
|
|
|
|
has_opt(char *mntopts, char *option)
|
|
|
|
{
|
|
|
|
char *optbuf, *opt;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
optbuf = strdup(mntopts);
|
|
|
|
for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
|
|
|
|
if (!strcmp(opt, option)) found = 1;
|
|
|
|
}
|
|
|
|
free (optbuf);
|
|
|
|
return(found);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
mount_all()
|
|
|
|
{
|
|
|
|
struct fstab *fs;
|
2012-07-05 11:04:15 +02:00
|
|
|
int ro, mountflags;
|
2012-06-19 18:03:11 +02:00
|
|
|
char mountpoint[PATH_MAX];
|
2012-07-05 11:04:15 +02:00
|
|
|
char *device, *err;
|
2012-06-19 18:03:11 +02:00
|
|
|
|
|
|
|
while ((fs = getfsent()) != NULL) {
|
2012-07-05 11:04:15 +02:00
|
|
|
ro = 0;
|
|
|
|
mountflags = 0;
|
|
|
|
device = NULL;
|
2012-06-19 18:03:11 +02:00
|
|
|
if (realpath(fs->fs_file, mountpoint) == NULL) {
|
|
|
|
fprintf(stderr, "Can't mount on %s\n", fs->fs_file);
|
|
|
|
return(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (has_opt(fs->fs_mntops, "noauto"))
|
|
|
|
continue;
|
|
|
|
if (!strcmp(mountpoint, "/"))
|
|
|
|
continue; /* Not remounting root */
|
|
|
|
if (has_opt(fs->fs_mntops, "ro"))
|
|
|
|
ro = 1;
|
|
|
|
if (ro) {
|
|
|
|
mountflags |= MS_RDONLY;
|
|
|
|
}
|
|
|
|
|
2012-07-05 11:04:15 +02:00
|
|
|
device = fs->fs_spec;
|
|
|
|
/* passing a null string for block special device means don't
|
|
|
|
* use a device at all and this is what we need to do for
|
|
|
|
* entries starting with "none"
|
|
|
|
*/
|
|
|
|
if (!strcmp(device, "none"))
|
|
|
|
device = NULL;
|
|
|
|
|
|
|
|
if (mount(device, mountpoint, mountflags, fs->fs_vfstype,
|
|
|
|
fs->fs_mntops) == 0) {
|
2012-06-19 18:03:11 +02:00
|
|
|
update_mtab(fs->fs_spec, fs->fs_file, fs->fs_vfstype,
|
|
|
|
mountflags);
|
|
|
|
} else {
|
2012-07-05 11:04:15 +02:00
|
|
|
err = strerror(errno);
|
|
|
|
fprintf(stderr, "mount: Can't mount %s on %s: %s\n",
|
|
|
|
fs->fs_spec, fs->fs_file, err);
|
2012-06-19 18:03:11 +02:00
|
|
|
return(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(EXIT_SUCCESS);
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
2012-06-19 18:03:11 +02:00
|
|
|
std_err("Usage: mount [-a] [-r] [-e] [-t type] [-o options] special name\n");
|
2005-04-21 16:53:53 +02:00
|
|
|
exit(1);
|
|
|
|
}
|