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"
47 int nfs_mount_data_version;
52 static struct option longopts[] = {
53 { "fake", 0, 0, 'f' },
54 { "help", 0, 0, 'h' },
55 { "no-mtab", 0, 0, 'n' },
56 { "read-only", 0, 0, 'r' },
58 { "verbose", 0, 0, 'v' },
59 { "version", 0, 0, 'V' },
60 { "read-write", 0, 0, 'w' },
62 { "options", 1, 0, 'o' },
67 * Map from -o and fstab option strings to the flag argument to mount(2).
70 const char *opt; /* option name */
71 int skip; /* skip in mtab option string */
72 int inv; /* true if flag value should be inverted */
73 int mask; /* flag mask value */
76 static const struct opt_map opt_map[] = {
77 { "defaults", 0, 0, 0 }, /* default options */
78 { "ro", 1, 0, MS_RDONLY }, /* read-only */
79 { "rw", 1, 1, MS_RDONLY }, /* read-write */
80 { "exec", 0, 1, MS_NOEXEC }, /* permit execution of binaries */
81 { "noexec", 0, 0, MS_NOEXEC }, /* don't execute binaries */
82 { "suid", 0, 1, MS_NOSUID }, /* honor suid executables */
83 { "nosuid", 0, 0, MS_NOSUID }, /* don't honor suid executables */
84 { "dev", 0, 1, MS_NODEV }, /* interpret device files */
85 { "nodev", 0, 0, MS_NODEV }, /* don't interpret devices */
86 { "sync", 0, 0, MS_SYNCHRONOUS}, /* synchronous I/O */
87 { "async", 0, 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
88 { "dirsync", 0, 0, MS_DIRSYNC}, /* synchronous directory modifications */
89 { "remount", 0, 0, MS_REMOUNT}, /* Alter flags of mounted FS */
90 { "bind", 0, 0, MS_BIND }, /* Remount part of tree elsewhere */
91 { "rbind", 0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
92 { "auto", 0, 0, MS_DUMMY }, /* Can be mounted using -a */
93 { "noauto", 0, 0, MS_DUMMY }, /* Can only be mounted explicitly */
94 { "users", 1, 0, MS_USERS }, /* Allow ordinary user to mount */
95 { "nousers", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
96 { "user", 1, 0, MS_USER }, /* Allow ordinary user to mount */
97 { "nouser", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
98 { "owner", 0, 0, MS_DUMMY }, /* Let the owner of the device mount */
99 { "noowner", 0, 0, MS_DUMMY }, /* Device owner has no special privs */
100 { "group", 0, 0, MS_DUMMY }, /* Let the group of the device mount */
101 { "nogroup", 0, 0, MS_DUMMY }, /* Device group has no special privs */
102 { "_netdev", 0, 0, MS_DUMMY}, /* Device requires network */
103 { "comment", 0, 0, MS_DUMMY}, /* fstab comment only (kudzu,_netdev)*/
105 /* add new options here */
107 { "sub", 0, 1, MS_NOSUB }, /* allow submounts */
108 { "nosub", 0, 0, MS_NOSUB }, /* don't allow submounts */
111 { "quiet", 0, 0, MS_SILENT }, /* be quiet */
112 { "loud", 0, 1, MS_SILENT }, /* print out messages. */
115 { "mand", 0, 0, MS_MANDLOCK }, /* Allow mandatory locks on this FS */
116 { "nomand", 0, 1, MS_MANDLOCK }, /* Forbid mandatory locks on this FS */
118 { "loop", 1, 0, MS_DUMMY }, /* use a loop device */
120 { "atime", 0, 1, MS_NOATIME }, /* Update access time */
121 { "noatime", 0, 0, MS_NOATIME }, /* Do not update access time */
124 { "diratime", 0, 1, MS_NODIRATIME }, /* Update dir access times */
125 { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
130 #define MAKE_VERSION(p,q,r) (65536 * (p) + 256 * (q) + (r))
132 int linux_version_code(void)
134 struct utsname my_utsname;
137 if (uname(&my_utsname) == 0) {
138 p = atoi(strtok(my_utsname.release, "."));
139 q = atoi(strtok(NULL, "."));
140 r = atoi(strtok(NULL, "."));
141 return MAKE_VERSION(p,q,r);
147 * Choose the version of the nfs_mount_data structure that is appropriate
148 * for the kernel that is doing the mount.
150 * NFS_MOUNT_VERSION: maximum version supported by these sources
151 * nfs_mount_data_version: maximum version supported by the running kernel
153 static void discover_nfs_mount_data_version(void)
155 int kernel_version = linux_version_code();
157 if (kernel_version) {
158 if (kernel_version < MAKE_VERSION(2, 1, 32))
159 nfs_mount_data_version = 1;
160 else if (kernel_version < MAKE_VERSION(2, 2, 18))
161 nfs_mount_data_version = 3;
162 else if (kernel_version < MAKE_VERSION(2, 3, 0))
163 nfs_mount_data_version = 4;
164 else if (kernel_version < MAKE_VERSION(2, 3, 99))
165 nfs_mount_data_version = 3;
166 else if (kernel_version < MAKE_VERSION(2, 6, 3))
167 nfs_mount_data_version = 4;
169 nfs_mount_data_version = 6;
171 if (nfs_mount_data_version > NFS_MOUNT_VERSION)
172 nfs_mount_data_version = NFS_MOUNT_VERSION;
175 static void print_one(char *spec, char *node, char *type, char *opts)
178 printf("%s on %s type %s", spec, node, type);
181 printf(" (%s)", opts);
188 * Build a canonical mount option string for /etc/mtab.
190 static char *fix_opts_string(int flags, const char *extra_opts)
192 const struct opt_map *om;
195 new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
196 if (flags & MS_USER) {
197 /* record who mounted this so they can unmount */
198 struct passwd *pw = getpwuid(getuid());
200 new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
202 if (flags & MS_USERS)
203 new_opts = xstrconcat3(new_opts, ",users", "");
205 for (om = opt_map; om->opt != NULL; om++) {
208 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
210 new_opts = xstrconcat3(new_opts, ",", om->opt);
213 if (extra_opts && *extra_opts) {
214 new_opts = xstrconcat3(new_opts, ",", extra_opts);
219 static int add_mtab(char *spec, char *mount_point, char *fstype,
220 int flags, char *opts, int freq, int pass)
224 int result = EX_FILEIO;
226 ment.mnt_fsname = spec;
227 ment.mnt_dir = mount_point;
228 ment.mnt_type = fstype;
229 ment.mnt_opts = fix_opts_string(flags, opts);
230 ment.mnt_freq = freq;
231 ment.mnt_passno = pass;
233 if (flags & MS_REMOUNT) {
234 update_mtab(ment.mnt_dir, &ment);
240 if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
242 nfs_error(_("Can't open mtab: %s"),
247 if (addmntent(mtab, &ment) == 1) {
248 nfs_error(_("Can't write mount entry to mtab: %s"),
253 if (fchmod(fileno(mtab), 0644) == -1) {
254 nfs_error(_("Can't set permissions on mtab: %s"),
269 void mount_usage(void)
271 printf("usage: %s remotetarget dir [-rvVwfnh] [-o nfsoptions]\n",
273 printf("options:\n");
274 printf("\t-r\t\tMount file system readonly\n");
275 printf("\t-v\t\tVerbose\n");
276 printf("\t-V\t\tPrint version\n");
277 printf("\t-w\t\tMount file system read-write\n");
278 printf("\t-f\t\tFake mount, do not actually mount\n");
279 printf("\t-n\t\tDo not update /etc/mtab\n");
280 printf("\t-s\t\tTolerate sloppy mount options rather than failing.\n");
281 printf("\t-h\t\tPrint this help\n");
282 printf("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n");
285 static void parse_opt(const char *opt, int *mask, char *extra_opts, int len)
287 const struct opt_map *om;
289 for (om = opt_map; om->opt != NULL; om++) {
290 if (!strcmp (opt, om->opt)) {
299 len -= strlen(extra_opts);
301 if (*extra_opts && --len > 0)
302 strcat(extra_opts, ",");
304 if ((len -= strlen(opt)) > 0)
305 strcat(extra_opts, opt);
309 * Convert the provided mount command-line options into the 4th &
310 * 5th arguments to mount(2). Output parameter "@flags" gets the
311 * standard options (indicated by MS_ bits), and output parameter
312 * "@extra_opts" gets all the filesystem-specific options.
314 static void parse_opts(const char *options, int *flags, char **extra_opts)
316 if (options != NULL) {
317 char *opts = xstrdup(options);
319 int len = strlen(opts) + 1; /* include room for a null */
322 *extra_opts = xmalloc(len);
325 for (p = opts, opt = NULL; p && *p; p++) {
327 opt = p; /* begin of the option item */
329 open_quote ^= 1; /* reverse the status */
331 continue; /* still in a quoted block */
333 *p = '\0'; /* terminate the option item */
335 /* end of option item or last item */
336 if (*p == '\0' || *(p + 1) == '\0') {
337 parse_opt(opt, flags, *extra_opts, len);
345 static int chk_mountpoint(char *mount_point)
349 if (stat(mount_point, &sb) < 0){
350 mount_error(NULL, mount_point, errno);
353 if (S_ISDIR(sb.st_mode) == 0){
354 mount_error(NULL, mount_point, ENOTDIR);
357 if (access(mount_point, X_OK) < 0) {
358 mount_error(NULL, mount_point, errno);
365 int main(int argc, char *argv[])
367 int c, flags = 0, mnt_err = 1, fake = 0;
368 char *spec, *mount_point, *fs_type = "nfs";
369 char *extra_opts = NULL, *mount_opts = NULL;
370 uid_t uid = getuid();
372 progname = basename(argv[0]);
374 discover_nfs_mount_data_version();
376 if(!strncmp(progname, "umount", strlen("umount")))
377 exit(nfsumount(argc, argv));
379 if (argv[1] && argv[1][0] == '-') {
380 if(argv[1][1] == 'V')
381 printf("%s ("PACKAGE_STRING")\n", progname);
393 mount_point = argv[2];
395 argv[2] = argv[0]; /* so that getopt error messages are correct */
396 while ((c = getopt_long(argc - 2, argv + 2, "rvVwfno:hs",
397 longopts, NULL)) != -1) {
406 printf("%s: ("PACKAGE_STRING")\n", progname);
417 case 'o': /* specify mount options */
419 mount_opts = xstrconcat3(mount_opts, ",", optarg);
421 mount_opts = xstrdup(optarg);
434 * Extra non-option words at the end are bogus...
436 if (optind != argc - 2) {
441 if (strcmp(progname, "mount.nfs4") == 0)
445 * If a non-root user is attempting to mount, make sure the
446 * user's requested options match the options specified in
447 * /etc/fstab; otherwise, don't allow the mount.
450 struct mntentchn *mc;
452 if ((mc = getfsfile(mount_point)) == NULL ||
453 strcmp(mc->m.mnt_fsname, spec) != 0 ||
454 strcmp(mc->m.mnt_type, fs_type) != 0) {
455 nfs_error(_("%s: permission denied: no match for %s "
456 "found in /etc/fstab"), progname, mount_point);
461 * 'mount' munges the options from fstab before passing them
462 * to us, so it is non-trivial to test that we have the correct
463 * set of options and we don't want to trust what the user
464 * gave us, so just take whatever is in /etc/fstab.
466 mount_opts = strdup(mc->m.mnt_opts);
469 mount_point = canonicalize(mount_point);
471 nfs_error(_("%s: no mount point provided"), progname);
474 if (mount_point[0] != '/') {
475 nfs_error(_("%s: unrecognized mount point %s"),
476 progname, mount_point);
481 parse_opts(mount_opts, &flags, &extra_opts);
484 if (!(flags & (MS_USERS|MS_USER))) {
485 nfs_error(_("%s: permission denied"), progname);
491 if (chk_mountpoint(mount_point)) {
496 if (strcmp(fs_type, "nfs4") == 0)
497 mnt_err = nfs4mount(spec, mount_point, flags, &extra_opts, fake);
499 mnt_err = nfsmount(spec, mount_point, flags, &extra_opts, fake);
505 print_one(spec, mount_point, fs_type, mount_opts);
508 mnt_err = add_mtab(spec, mount_point, fs_type, flags, extra_opts,
509 0, 0 /* these are always zero for NFS */ );