2 * mount.c -- Linux NFS mount
4 * Copyright (C) 2006 Amit Gud <agud@redhat.com>
6 * - Basic code and wrapper around mount and umount code of NFS.
7 * Based on util-linux/mount/mount.c.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
23 #include <sys/types.h>
28 #include <sys/mount.h>
34 #include "mount_constants.h"
35 #include "nfs_paths.h"
37 #include "nfs_mount.h"
38 #include "nfs4_mount.h"
39 #include "nfsumount.h"
47 static struct option longopts[] = {
48 { "fake", 0, 0, 'f' },
49 { "help", 0, 0, 'h' },
50 { "no-mtab", 0, 0, 'n' },
51 { "read-only", 0, 0, 'r' },
53 { "verbose", 0, 0, 'v' },
54 { "version", 0, 0, 'V' },
55 { "read-write", 0, 0, 'w' },
57 { "options", 1, 0, 'o' },
58 { "nfsvers", 1, 0, 't' },
59 { "bind", 0, 0, 128 },
60 { "replace", 0, 0, 129 },
61 { "after", 0, 0, 130 },
62 { "before", 0, 0, 131 },
63 { "over", 0, 0, 132 },
64 { "move", 0, 0, 133 },
65 { "rbind", 0, 0, 135 },
69 /* Map from -o and fstab option strings to the flag argument to mount(2). */
71 const char *opt; /* option name */
72 int skip; /* skip in mtab option string */
73 int inv; /* true if flag value should be inverted */
74 int mask; /* flag mask value */
77 static const struct opt_map opt_map[] = {
78 { "defaults", 0, 0, 0 }, /* default options */
79 { "ro", 1, 0, MS_RDONLY }, /* read-only */
80 { "rw", 1, 1, MS_RDONLY }, /* read-write */
81 { "exec", 0, 1, MS_NOEXEC }, /* permit execution of binaries */
82 { "noexec", 0, 0, MS_NOEXEC }, /* don't execute binaries */
83 { "suid", 0, 1, MS_NOSUID }, /* honor suid executables */
84 { "nosuid", 0, 0, MS_NOSUID }, /* don't honor suid executables */
85 { "dev", 0, 1, MS_NODEV }, /* interpret device files */
86 { "nodev", 0, 0, MS_NODEV }, /* don't interpret devices */
87 { "sync", 0, 0, MS_SYNCHRONOUS}, /* synchronous I/O */
88 { "async", 0, 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
89 { "dirsync", 0, 0, MS_DIRSYNC}, /* synchronous directory modifications */
90 { "remount", 0, 0, MS_REMOUNT}, /* Alter flags of mounted FS */
91 { "bind", 0, 0, MS_BIND }, /* Remount part of tree elsewhere */
92 { "rbind", 0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
94 /* add new options here */
96 { "sub", 0, 1, MS_NOSUB }, /* allow submounts */
97 { "nosub", 0, 0, MS_NOSUB }, /* don't allow submounts */
100 { "quiet", 0, 0, MS_SILENT }, /* be quiet */
101 { "loud", 0, 1, MS_SILENT }, /* print out messages. */
104 { "mand", 0, 0, MS_MANDLOCK }, /* Allow mandatory locks on this FS */
105 { "nomand", 0, 1, MS_MANDLOCK }, /* Forbid mandatory locks on this FS */
108 { "atime", 0, 1, MS_NOATIME }, /* Update access time */
109 { "noatime", 0, 0, MS_NOATIME }, /* Do not update access time */
112 { "diratime", 0, 1, MS_NODIRATIME }, /* Update dir access times */
113 { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
118 /* Try to build a canonical options string. */
119 static char * fix_opts_string (int flags, const char *extra_opts) {
120 const struct opt_map *om;
123 new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
124 for (om = opt_map; om->opt != NULL; om++) {
127 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
129 new_opts = xstrconcat3(new_opts, ",", om->opt);
132 if (extra_opts && *extra_opts) {
133 new_opts = xstrconcat3(new_opts, ",", extra_opts);
139 int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opts, int freq, int passno)
145 ment.mnt_fsname = fsname;
146 ment.mnt_dir = mount_point;
147 ment.mnt_type = fstype;
148 ment.mnt_opts = fix_opts_string(flags, opts);
154 if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
155 fprintf(stderr, "Can't open " MOUNTED);
159 if (addmntent(mtab, &ment) == 1) {
162 fprintf(stderr, "Can't write mount entry");
166 if (fchmod(fileno(mtab), 0644) == -1) {
169 fprintf(stderr, "Can't set perms on " MOUNTED);
180 int do_mount_syscall(char *spec, char *node, char *type, int flags, void *data)
182 return mount(spec, node, type, flags, data);
187 printf("usage: %s remotetarget dir [-rvVwfnh] [-t version] [-o nfsoptions]\n", progname);
188 printf("options:\n\t-r\t\tMount file system readonly\n");
189 printf("\t-v\t\tVerbose\n");
190 printf("\t-V\t\tPrint version\n");
191 printf("\t-w\t\tMount file system read-write\n");
192 printf("\t-f\t\tFake mount, don't actually mount\n");
193 printf("\t-n\t\tDo not update /etc/mtab\n");
194 printf("\t-h\t\tPrint this help\n");
195 printf("\tversion\t\tnfs4 - NFS version 4, nfs - older NFS version supported\n");
196 printf("\tnfsoptions\tRefer mount.nfs(8) or nfs(5)\n\n");
200 parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
201 const struct opt_map *om;
203 for (om = opt_map; om->opt != NULL; om++) {
204 if (!strcmp (opt, om->opt)) {
213 len -= strlen(extra_opts);
215 if (*extra_opts && --len > 0)
216 strcat(extra_opts, ",");
218 if ((len -= strlen(opt)) > 0)
219 strcat(extra_opts, opt);
222 /* Take -o options list and compute 4th and 5th args to mount(2). flags
223 gets the standard options (indicated by bits) and extra_opts all the rest */
224 static void parse_opts (const char *options, int *flags, char **extra_opts)
226 if (options != NULL) {
227 char *opts = xstrdup(options);
229 int len = strlen(opts) + 20;
231 *extra_opts = xmalloc(len);
234 for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ","))
235 parse_opt(opt, flags, *extra_opts, len);
242 static void mount_error(char *node)
246 printf("%s: mount point %s is not a directory\n", progname, node);
249 printf("%s: %s is already mounted or busy\n", progname, node);
252 printf("%s: mount point %s does not exist\n", progname, node);
255 printf("%s: %s\n", progname, strerror(errno));
259 int main(int argc, char *argv[])
261 int c, flags = 0, nfs_mount_vers = 0, mnt_err = 1, fake = 0;
262 char *spec, *mount_point, *extra_opts = NULL;
263 char *mount_opts = NULL, *p;
266 if ((p = strrchr(progname, '/')) != NULL)
270 printf("%s: only root can do that.\n", progname);
274 if(!strncmp(progname, "umount", strlen("umount"))) {
279 return(nfsumount(argc, argv));
287 if(argv[1][0] == '-') {
288 if(argv[1][1] == 'V')
289 printf("%s ("PACKAGE_STRING")\n", progname);
295 while ((c = getopt_long (argc - 2, argv + 2, "rt:vVwfno:h",
296 longopts, NULL)) != -1) {
302 nfs_mount_vers = (strncmp(optarg, "nfs4", 4)) ? 0 : 4;
308 printf("%s: ("PACKAGE_STRING")\n", progname);
319 case 'o': /* specify mount options */
321 mount_opts = xstrconcat3(mount_opts, ",", optarg);
323 mount_opts = xstrdup(optarg);
328 case 129: /* replace */
329 mounttype = MS_REPLACE;
331 case 130: /* after */
332 mounttype = MS_AFTER;
334 case 131: /* before */
335 mounttype = MS_BEFORE;
343 case 135: /* rbind */
344 mounttype = MS_BIND | MS_REC;
354 mount_point = canonicalize(argv[2]);
356 parse_opts(mount_opts, &flags, &extra_opts);
358 if (!strcmp(progname, "mount.nfs4") || nfs_mount_vers == 4) {
360 mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0);
363 if (!strcmp(progname, "mount.nfs")) {
364 mnt_err = nfsmount(spec, mount_point, &flags,
365 &extra_opts, &mount_opts, &nfs_mount_vers, 0);
369 if (!mnt_err && !fake) {
370 mnt_err = do_mount_syscall(spec, mount_point, nfs_mount_vers == 4 ? "nfs4" : "nfs", flags, mount_opts);
373 mount_error(mount_point);
378 add_mtab(spec, mount_point, nfs_mount_vers == 4 ? "nfs4" : "nfs",
379 flags, extra_opts, 0, 0);