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>
29 #include <sys/utsname.h>
37 #include "mount_constants.h"
38 #include "nfs_paths.h"
40 #include "nfs_mount.h"
41 #include "nfs4_mount.h"
42 #include "nfsumount.h"
48 int nfs_mount_data_version;
53 static struct option longopts[] = {
54 { "fake", 0, 0, 'f' },
55 { "help", 0, 0, 'h' },
56 { "no-mtab", 0, 0, 'n' },
57 { "read-only", 0, 0, 'r' },
59 { "verbose", 0, 0, 'v' },
60 { "version", 0, 0, 'V' },
61 { "read-write", 0, 0, 'w' },
63 { "options", 1, 0, 'o' },
68 * 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 */
93 { "auto", 0, 0, MS_DUMMY }, /* Can be mounted using -a */
94 { "noauto", 0, 0, MS_DUMMY }, /* Can only be mounted explicitly */
95 { "users", 1, 0, MS_USERS }, /* Allow ordinary user to mount */
96 { "nousers", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
97 { "user", 1, 0, MS_USER }, /* Allow ordinary user to mount */
98 { "nouser", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
99 { "owner", 0, 0, MS_DUMMY }, /* Let the owner of the device mount */
100 { "noowner", 0, 0, MS_DUMMY }, /* Device owner has no special privs */
101 { "group", 0, 0, MS_DUMMY }, /* Let the group of the device mount */
102 { "nogroup", 0, 0, MS_DUMMY }, /* Device group has no special privs */
103 { "_netdev", 0, 0, MS_DUMMY}, /* Device requires network */
104 { "comment", 0, 0, MS_DUMMY}, /* fstab comment only (kudzu,_netdev)*/
106 /* add new options here */
108 { "sub", 0, 1, MS_NOSUB }, /* allow submounts */
109 { "nosub", 0, 0, MS_NOSUB }, /* don't allow submounts */
112 { "quiet", 0, 0, MS_SILENT }, /* be quiet */
113 { "loud", 0, 1, MS_SILENT }, /* print out messages. */
116 { "mand", 0, 0, MS_MANDLOCK }, /* Allow mandatory locks on this FS */
117 { "nomand", 0, 1, MS_MANDLOCK }, /* Forbid mandatory locks on this FS */
119 { "loop", 1, 0, MS_DUMMY }, /* use a loop device */
121 { "atime", 0, 1, MS_NOATIME }, /* Update access time */
122 { "noatime", 0, 0, MS_NOATIME }, /* Do not update access time */
125 { "diratime", 0, 1, MS_NODIRATIME }, /* Update dir access times */
126 { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
131 #define MAKE_VERSION(p,q,r) (65536 * (p) + 256 * (q) + (r))
133 int linux_version_code(void)
135 struct utsname my_utsname;
138 if (uname(&my_utsname) == 0) {
139 p = atoi(strtok(my_utsname.release, "."));
140 q = atoi(strtok(NULL, "."));
141 r = atoi(strtok(NULL, "."));
142 return MAKE_VERSION(p,q,r);
148 * Choose the version of the nfs_mount_data structure that is appropriate
149 * for the kernel that is doing the mount.
151 * NFS_MOUNT_VERSION: maximum version supported by these sources
152 * nfs_mount_data_version: maximum version supported by the running kernel
154 static void discover_nfs_mount_data_version(void)
156 int kernel_version = linux_version_code();
158 if (kernel_version) {
159 if (kernel_version < MAKE_VERSION(2, 1, 32))
160 nfs_mount_data_version = 1;
161 else if (kernel_version < MAKE_VERSION(2, 2, 18))
162 nfs_mount_data_version = 3;
163 else if (kernel_version < MAKE_VERSION(2, 3, 0))
164 nfs_mount_data_version = 4;
165 else if (kernel_version < MAKE_VERSION(2, 3, 99))
166 nfs_mount_data_version = 3;
167 else if (kernel_version < MAKE_VERSION(2, 6, 3))
168 nfs_mount_data_version = 4;
170 nfs_mount_data_version = 6;
172 if (nfs_mount_data_version > NFS_MOUNT_VERSION)
173 nfs_mount_data_version = NFS_MOUNT_VERSION;
176 static void print_one(char *spec, char *node, char *type, char *opts)
179 printf("%s on %s type %s", spec, node, type);
182 printf(" (%s)", opts);
189 * Build a canonical mount option string for /etc/mtab.
191 static char *fix_opts_string(int flags, const char *extra_opts)
193 const struct opt_map *om;
196 new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
197 if (flags & MS_USER) {
198 /* record who mounted this so they can unmount */
199 struct passwd *pw = getpwuid(getuid());
201 new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
203 if (flags & MS_USERS)
204 new_opts = xstrconcat3(new_opts, ",users", "");
206 for (om = opt_map; om->opt != NULL; om++) {
209 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
211 new_opts = xstrconcat3(new_opts, ",", om->opt);
214 if (extra_opts && *extra_opts) {
215 new_opts = xstrconcat3(new_opts, ",", extra_opts);
220 static int add_mtab(char *spec, char *mount_point, char *fstype,
221 int flags, char *opts, int freq, int pass)
225 int result = EX_FILEIO;
227 ment.mnt_fsname = spec;
228 ment.mnt_dir = mount_point;
229 ment.mnt_type = fstype;
230 ment.mnt_opts = fix_opts_string(flags, opts);
231 ment.mnt_freq = freq;
232 ment.mnt_passno = pass;
234 if (flags & MS_REMOUNT) {
235 update_mtab(ment.mnt_dir, &ment);
241 if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
243 nfs_error(_("Can't open mtab: %s"),
248 if (addmntent(mtab, &ment) == 1) {
249 nfs_error(_("Can't write mount entry to mtab: %s"),
254 if (fchmod(fileno(mtab), 0644) == -1) {
255 nfs_error(_("Can't set permissions on mtab: %s"),
270 void mount_usage(void)
272 printf("usage: %s remotetarget dir [-rvVwfnh] [-o nfsoptions]\n",
274 printf("options:\n");
275 printf("\t-r\t\tMount file system readonly\n");
276 printf("\t-v\t\tVerbose\n");
277 printf("\t-V\t\tPrint version\n");
278 printf("\t-w\t\tMount file system read-write\n");
279 printf("\t-f\t\tFake mount, do not actually mount\n");
280 printf("\t-n\t\tDo not update /etc/mtab\n");
281 printf("\t-s\t\tTolerate sloppy mount options rather than failing.\n");
282 printf("\t-h\t\tPrint this help\n");
283 printf("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n");
286 static void parse_opt(const char *opt, int *mask, char *extra_opts, int len)
288 const struct opt_map *om;
290 for (om = opt_map; om->opt != NULL; om++) {
291 if (!strcmp (opt, om->opt)) {
300 len -= strlen(extra_opts);
302 if (*extra_opts && --len > 0)
303 strcat(extra_opts, ",");
305 if ((len -= strlen(opt)) > 0)
306 strcat(extra_opts, opt);
310 * Convert the provided mount command-line options into the 4th &
311 * 5th arguments to mount(2). Output parameter "@flags" gets the
312 * standard options (indicated by MS_ bits), and output parameter
313 * "@extra_opts" gets all the filesystem-specific options.
315 static void parse_opts(const char *options, int *flags, char **extra_opts)
317 if (options != NULL) {
318 char *opts = xstrdup(options);
320 int len = strlen(opts) + 1; /* include room for a null */
323 *extra_opts = xmalloc(len);
326 for (p = opts, opt = NULL; p && *p; p++) {
328 opt = p; /* begin of the option item */
330 open_quote ^= 1; /* reverse the status */
332 continue; /* still in a quoted block */
334 *p = '\0'; /* terminate the option item */
336 /* end of option item or last item */
337 if (*p == '\0' || *(p + 1) == '\0') {
338 parse_opt(opt, flags, *extra_opts, len);
346 static int chk_mountpoint(char *mount_point)
350 if (stat(mount_point, &sb) < 0){
351 mount_error(NULL, mount_point, errno);
354 if (S_ISDIR(sb.st_mode) == 0){
355 mount_error(NULL, mount_point, ENOTDIR);
358 if (access(mount_point, X_OK) < 0) {
359 mount_error(NULL, mount_point, errno);
366 int main(int argc, char *argv[])
368 int c, flags = 0, mnt_err = 1, fake = 0;
369 char *spec, *mount_point, *fs_type = "nfs";
370 char *extra_opts = NULL, *mount_opts = NULL;
371 uid_t uid = getuid();
373 progname = basename(argv[0]);
375 discover_nfs_mount_data_version();
377 if(!strncmp(progname, "umount", strlen("umount"))) {
382 exit(nfsumount(argc, argv));
385 if (argv[1] && argv[1][0] == '-') {
386 if(argv[1][1] == 'V')
387 printf("%s ("PACKAGE_STRING")\n", progname);
399 mount_point = argv[2];
401 argv[2] = argv[0]; /* so that getopt error messages are correct */
402 while ((c = getopt_long(argc - 2, argv + 2, "rvVwfno:hs",
403 longopts, NULL)) != -1) {
412 printf("%s: ("PACKAGE_STRING")\n", progname);
423 case 'o': /* specify mount options */
425 mount_opts = xstrconcat3(mount_opts, ",", optarg);
427 mount_opts = xstrdup(optarg);
440 * Extra non-option words at the end are bogus...
442 if (optind != argc - 2) {
447 if (strcmp(progname, "mount.nfs4") == 0)
451 * If a non-root user is attempting to mount, make sure the
452 * user's requested options match the options specified in
453 * /etc/fstab; otherwise, don't allow the mount.
456 struct mntentchn *mc;
458 if ((mc = getfsfile(mount_point)) == NULL ||
459 strcmp(mc->m.mnt_fsname, spec) != 0 ||
460 strcmp(mc->m.mnt_type, fs_type) != 0) {
461 nfs_error(_("%s: permission denied: no match for %s "
462 "found in /etc/fstab"), progname, mount_point);
467 * 'mount' munges the options from fstab before passing them
468 * to us, so it is non-trivial to test that we have the correct
469 * set of options and we don't want to trust what the user
470 * gave us, so just take whatever is in /etc/fstab.
472 mount_opts = strdup(mc->m.mnt_opts);
475 mount_point = canonicalize(mount_point);
477 nfs_error(_("%s: no mount point provided"), progname);
480 if (mount_point[0] != '/') {
481 nfs_error(_("%s: unrecognized mount point %s"),
482 progname, mount_point);
487 parse_opts(mount_opts, &flags, &extra_opts);
490 if (!(flags & (MS_USERS|MS_USER))) {
491 nfs_error(_("%s: permission denied"), progname);
497 if (chk_mountpoint(mount_point)) {
502 if (strcmp(fs_type, "nfs4") == 0)
503 mnt_err = nfs4mount(spec, mount_point, flags, &extra_opts, fake);
505 mnt_err = nfsmount(spec, mount_point, flags, &extra_opts, fake);
511 print_one(spec, mount_point, fs_type, mount_opts);
514 mnt_err = add_mtab(spec, mount_point, fs_type, flags, extra_opts,
515 0, 0 /* these are always zero for NFS */ );