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>
35 #include "mount_constants.h"
36 #include "nfs_paths.h"
38 #include "nfs_mount.h"
39 #include "nfs4_mount.h"
40 #include "nfsumount.h"
49 static struct option longopts[] = {
50 { "fake", 0, 0, 'f' },
51 { "help", 0, 0, 'h' },
52 { "no-mtab", 0, 0, 'n' },
53 { "read-only", 0, 0, 'r' },
55 { "verbose", 0, 0, 'v' },
56 { "version", 0, 0, 'V' },
57 { "read-write", 0, 0, 'w' },
59 { "options", 1, 0, 'o' },
60 { "nfsvers", 1, 0, 't' },
61 { "bind", 0, 0, 128 },
62 { "replace", 0, 0, 129 },
63 { "after", 0, 0, 130 },
64 { "before", 0, 0, 131 },
65 { "over", 0, 0, 132 },
66 { "move", 0, 0, 133 },
67 { "rbind", 0, 0, 135 },
71 /* Map from -o and fstab option strings to the flag argument to mount(2). */
73 const char *opt; /* option name */
74 int skip; /* skip in mtab option string */
75 int inv; /* true if flag value should be inverted */
76 int mask; /* flag mask value */
79 /* Custom mount options for our own purposes. */
80 #define MS_DUMMY 0x00000000
81 #define MS_USERS 0x40000000
82 #define MS_USER 0x80000000
84 static const struct opt_map opt_map[] = {
85 { "defaults", 0, 0, 0 }, /* default options */
86 { "ro", 1, 0, MS_RDONLY }, /* read-only */
87 { "rw", 1, 1, MS_RDONLY }, /* read-write */
88 { "exec", 0, 1, MS_NOEXEC }, /* permit execution of binaries */
89 { "noexec", 0, 0, MS_NOEXEC }, /* don't execute binaries */
90 { "suid", 0, 1, MS_NOSUID }, /* honor suid executables */
91 { "nosuid", 0, 0, MS_NOSUID }, /* don't honor suid executables */
92 { "dev", 0, 1, MS_NODEV }, /* interpret device files */
93 { "nodev", 0, 0, MS_NODEV }, /* don't interpret devices */
94 { "sync", 0, 0, MS_SYNCHRONOUS}, /* synchronous I/O */
95 { "async", 0, 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
96 { "dirsync", 0, 0, MS_DIRSYNC}, /* synchronous directory modifications */
97 { "remount", 0, 0, MS_REMOUNT}, /* Alter flags of mounted FS */
98 { "bind", 0, 0, MS_BIND }, /* Remount part of tree elsewhere */
99 { "rbind", 0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
100 { "auto", 0, 0, MS_DUMMY }, /* Can be mounted using -a */
101 { "noauto", 0, 0, MS_DUMMY }, /* Can only be mounted explicitly */
102 { "users", 1, 0, MS_USERS|MS_NOEXEC|MS_NOSUID|MS_NODEV },
103 /* Allow ordinary user to mount */
104 { "nousers", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
105 { "user", 1, 0, MS_USER|MS_NOEXEC|MS_NOSUID|MS_NODEV },
106 /* Allow ordinary user to mount */
107 { "nouser", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
108 { "owner", 0, 0, MS_DUMMY }, /* Let the owner of the device mount */
109 { "noowner", 0, 0, MS_DUMMY }, /* Device owner has no special privs */
110 { "group", 0, 0, MS_DUMMY }, /* Let the group of the device mount */
111 { "nogroup", 0, 0, MS_DUMMY }, /* Device group has no special privs */
112 { "_netdev", 0, 0, MS_DUMMY}, /* Device requires network */
113 { "comment", 0, 0, MS_DUMMY}, /* fstab comment only (kudzu,_netdev)*/
115 /* add new options here */
117 { "sub", 0, 1, MS_NOSUB }, /* allow submounts */
118 { "nosub", 0, 0, MS_NOSUB }, /* don't allow submounts */
121 { "quiet", 0, 0, MS_SILENT }, /* be quiet */
122 { "loud", 0, 1, MS_SILENT }, /* print out messages. */
125 { "mand", 0, 0, MS_MANDLOCK }, /* Allow mandatory locks on this FS */
126 { "nomand", 0, 1, MS_MANDLOCK }, /* Forbid mandatory locks on this FS */
128 { "loop", 1, 0, MS_DUMMY }, /* use a loop device */
130 { "atime", 0, 1, MS_NOATIME }, /* Update access time */
131 { "noatime", 0, 0, MS_NOATIME }, /* Do not update access time */
134 { "diratime", 0, 1, MS_NODIRATIME }, /* Update dir access times */
135 { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
140 /* Try to build a canonical options string. */
141 static char * fix_opts_string (int flags, const char *extra_opts) {
142 const struct opt_map *om;
145 new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
146 if (flags & MS_USER) {
147 /* record who mounted this so they can unmount */
148 struct passwd *pw = getpwuid(getuid());
150 new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
152 if (flags & MS_USERS)
153 new_opts = xstrconcat3(new_opts, ",users", "");
155 for (om = opt_map; om->opt != NULL; om++) {
158 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
160 new_opts = xstrconcat3(new_opts, ",", om->opt);
163 if (extra_opts && *extra_opts) {
164 new_opts = xstrconcat3(new_opts, ",", extra_opts);
170 int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opts, int freq, int passno)
175 ment.mnt_fsname = fsname;
176 ment.mnt_dir = mount_point;
177 ment.mnt_type = fstype;
178 ment.mnt_opts = fix_opts_string(flags, opts);
184 if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
185 fprintf(stderr, "Can't open " MOUNTED);
189 if (addmntent(mtab, &ment) == 1) {
192 fprintf(stderr, "Can't write mount entry");
196 if (fchmod(fileno(mtab), 0644) == -1) {
199 fprintf(stderr, "Can't set perms on " MOUNTED);
210 int do_mount_syscall(char *spec, char *node, char *type, int flags, void *data)
212 return mount(spec, node, type, flags, data);
217 printf("usage: %s remotetarget dir [-rvVwfnh] [-t version] [-o nfsoptions]\n", progname);
218 printf("options:\n\t-r\t\tMount file system readonly\n");
219 printf("\t-v\t\tVerbose\n");
220 printf("\t-V\t\tPrint version\n");
221 printf("\t-w\t\tMount file system read-write\n");
222 printf("\t-f\t\tFake mount, don't actually mount\n");
223 printf("\t-n\t\tDo not update /etc/mtab\n");
224 printf("\t-s\t\tTolerate sloppy mount options rather than failing.\n");
225 printf("\t-h\t\tPrint this help\n");
226 printf("\tversion\t\tnfs4 - NFS version 4, nfs - older NFS version supported\n");
227 printf("\tnfsoptions\tRefer mount.nfs(8) or nfs(5)\n\n");
231 parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
232 const struct opt_map *om;
234 for (om = opt_map; om->opt != NULL; om++) {
235 if (!strcmp (opt, om->opt)) {
244 len -= strlen(extra_opts);
246 if (*extra_opts && --len > 0)
247 strcat(extra_opts, ",");
249 if ((len -= strlen(opt)) > 0)
250 strcat(extra_opts, opt);
253 /* Take -o options list and compute 4th and 5th args to mount(2). flags
254 gets the standard options (indicated by bits) and extra_opts all the rest */
255 static void parse_opts (const char *options, int *flags, char **extra_opts)
257 if (options != NULL) {
258 char *opts = xstrdup(options);
260 int len = strlen(opts) + 20;
262 *extra_opts = xmalloc(len);
265 for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ","))
266 parse_opt(opt, flags, *extra_opts, len);
273 static void mount_error(char *node)
277 fprintf(stderr, "%s: mount point %s is not a directory\n", progname, node);
280 fprintf(stderr, "%s: %s is already mounted or busy\n", progname, node);
283 fprintf(stderr, "%s: mount point %s does not exist\n", progname, node);
286 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
290 static void start_statd()
292 /* If /var/run/rpc.statd.pid exists and is non-empty,
293 * assume statd already running.
294 * If START_STATD not defined, or defined to a non-existent file,
296 * else run that file (typically a shell script)
299 if (stat("/var/run/rpc.statd.pid", &stb) == 0 &&
303 if (stat(START_STATD, &stb) ==0 &&
304 S_ISREG(stb.st_mode) &&
305 (stb.st_mode & S_IXUSR))
310 int main(int argc, char *argv[])
312 int c, flags = 0, nfs_mount_vers = 0, mnt_err = 1, fake = 0;
313 char *spec, *mount_point, *extra_opts = NULL;
314 char *mount_opts = NULL, *p;
315 uid_t uid = getuid();
318 if ((p = strrchr(progname, '/')) != NULL)
321 if(!strncmp(progname, "umount", strlen("umount"))) {
326 exit(nfsumount(argc, argv) ? 0 : 1);
334 if(argv[1][0] == '-') {
335 if(argv[1][1] == 'V')
336 printf("%s ("PACKAGE_STRING")\n", progname);
342 while ((c = getopt_long (argc - 2, argv + 2, "rt:vVwfno:hs",
343 longopts, NULL)) != -1) {
349 nfs_mount_vers = (strncmp(optarg, "nfs4", 4)) ? 0 : 4;
355 printf("%s: ("PACKAGE_STRING")\n", progname);
366 case 'o': /* specify mount options */
368 mount_opts = xstrconcat3(mount_opts, ",", optarg);
370 mount_opts = xstrdup(optarg);
378 case 129: /* replace */
379 mounttype = MS_REPLACE;
381 case 130: /* after */
382 mounttype = MS_AFTER;
384 case 131: /* before */
385 mounttype = MS_BEFORE;
393 case 135: /* rbind */
394 mounttype = MS_BIND | MS_REC;
404 mount_point = argv[2];
407 /* don't even think about it unless options exactly
410 struct mntentchn *mc;
412 if ((mc = getfsfile(mount_point)) == NULL ||
413 strcmp(mc->m.mnt_fsname, spec) != 0 ||
414 strcmp(mc->m.mnt_opts, mount_opts) != 0) {
415 fprintf(stderr, "%s: permission died - no match for fstab\n",
422 mount_point = canonicalize(mount_point);
424 parse_opts(mount_opts, &flags, &extra_opts);
427 if (! (flags & (MS_USERS | MS_USER))) {
428 fprintf(stderr, "%s: permission denied\n", progname);
433 if (!strcmp(progname, "mount.nfs4") || nfs_mount_vers == 4) {
435 mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0);
437 else if (!strcmp(progname, "mount.nfs")) {
439 mnt_err = nfsmount(spec, mount_point, &flags,
440 &extra_opts, &mount_opts,
442 if (!mnt_err && !fake && need_statd)
451 mnt_err = do_mount_syscall(spec, mount_point,
452 nfs_mount_vers == 4 ? "nfs4" : "nfs",
456 mount_error(mount_point);
461 add_mtab(spec, mount_point,
462 nfs_mount_vers == 4 ? "nfs4" : "nfs",
463 flags, extra_opts, 0, 0);