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 #define FOREGROUND (0)
53 #define BACKGROUND (1)
55 static struct option longopts[] = {
56 { "fake", 0, 0, 'f' },
57 { "help", 0, 0, 'h' },
58 { "no-mtab", 0, 0, 'n' },
59 { "read-only", 0, 0, 'r' },
61 { "verbose", 0, 0, 'v' },
62 { "version", 0, 0, 'V' },
63 { "read-write", 0, 0, 'w' },
65 { "options", 1, 0, 'o' },
70 * 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 static const struct opt_map opt_map[] = {
80 { "defaults", 0, 0, 0 }, /* default options */
81 { "ro", 1, 0, MS_RDONLY }, /* read-only */
82 { "rw", 1, 1, MS_RDONLY }, /* read-write */
83 { "exec", 0, 1, MS_NOEXEC }, /* permit execution of binaries */
84 { "noexec", 0, 0, MS_NOEXEC }, /* don't execute binaries */
85 { "suid", 0, 1, MS_NOSUID }, /* honor suid executables */
86 { "nosuid", 0, 0, MS_NOSUID }, /* don't honor suid executables */
87 { "dev", 0, 1, MS_NODEV }, /* interpret device files */
88 { "nodev", 0, 0, MS_NODEV }, /* don't interpret devices */
89 { "sync", 0, 0, MS_SYNCHRONOUS}, /* synchronous I/O */
90 { "async", 0, 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
91 { "dirsync", 0, 0, MS_DIRSYNC}, /* synchronous directory modifications */
92 { "remount", 0, 0, MS_REMOUNT}, /* Alter flags of mounted FS */
93 { "bind", 0, 0, MS_BIND }, /* Remount part of tree elsewhere */
94 { "rbind", 0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
95 { "auto", 0, 0, MS_DUMMY }, /* Can be mounted using -a */
96 { "noauto", 0, 0, MS_DUMMY }, /* Can only be mounted explicitly */
97 { "users", 1, 0, MS_USERS }, /* Allow ordinary user to mount */
98 { "nousers", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
99 { "user", 1, 0, MS_USER }, /* Allow ordinary user to mount */
100 { "nouser", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
101 { "owner", 0, 0, MS_DUMMY }, /* Let the owner of the device mount */
102 { "noowner", 0, 0, MS_DUMMY }, /* Device owner has no special privs */
103 { "group", 0, 0, MS_DUMMY }, /* Let the group of the device mount */
104 { "nogroup", 0, 0, MS_DUMMY }, /* Device group has no special privs */
105 { "_netdev", 0, 0, MS_DUMMY}, /* Device requires network */
106 { "comment", 0, 0, MS_DUMMY}, /* fstab comment only (kudzu,_netdev)*/
108 /* add new options here */
110 { "sub", 0, 1, MS_NOSUB }, /* allow submounts */
111 { "nosub", 0, 0, MS_NOSUB }, /* don't allow submounts */
114 { "quiet", 0, 0, MS_SILENT }, /* be quiet */
115 { "loud", 0, 1, MS_SILENT }, /* print out messages. */
118 { "mand", 0, 0, MS_MANDLOCK }, /* Allow mandatory locks on this FS */
119 { "nomand", 0, 1, MS_MANDLOCK }, /* Forbid mandatory locks on this FS */
121 { "loop", 1, 0, MS_DUMMY }, /* use a loop device */
123 { "atime", 0, 1, MS_NOATIME }, /* Update access time */
124 { "noatime", 0, 0, MS_NOATIME }, /* Do not update access time */
127 { "diratime", 0, 1, MS_NODIRATIME }, /* Update dir access times */
128 { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
133 #define MAKE_VERSION(p,q,r) (65536 * (p) + 256 * (q) + (r))
135 int linux_version_code(void)
137 struct utsname my_utsname;
140 if (uname(&my_utsname) == 0) {
141 p = atoi(strtok(my_utsname.release, "."));
142 q = atoi(strtok(NULL, "."));
143 r = atoi(strtok(NULL, "."));
144 return MAKE_VERSION(p,q,r);
150 * Choose the version of the nfs_mount_data structure that is appropriate
151 * for the kernel that is doing the mount.
153 * NFS_MOUNT_VERSION: maximum version supported by these sources
154 * nfs_mount_data_version: maximum version supported by the running kernel
156 static void discover_nfs_mount_data_version(void)
158 int kernel_version = linux_version_code();
160 if (kernel_version) {
161 if (kernel_version < MAKE_VERSION(2, 1, 32))
162 nfs_mount_data_version = 1;
163 else if (kernel_version < MAKE_VERSION(2, 2, 18))
164 nfs_mount_data_version = 3;
165 else if (kernel_version < MAKE_VERSION(2, 3, 0))
166 nfs_mount_data_version = 4;
167 else if (kernel_version < MAKE_VERSION(2, 3, 99))
168 nfs_mount_data_version = 3;
169 else if (kernel_version < MAKE_VERSION(2, 6, 3))
170 nfs_mount_data_version = 4;
172 nfs_mount_data_version = 6;
174 if (nfs_mount_data_version > NFS_MOUNT_VERSION)
175 nfs_mount_data_version = NFS_MOUNT_VERSION;
178 static void print_one(char *spec, char *node, char *type, char *opts)
184 printf(_("%s on %s type %s (%s)\n"), spec, node, type, opts);
186 printf(_("%s on %s type %s\n"), spec, node, type);
190 * Build a canonical mount option string for /etc/mtab.
192 static char *fix_opts_string(int flags, const char *extra_opts)
194 const struct opt_map *om;
197 new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
198 if (flags & MS_USER) {
199 /* record who mounted this so they can unmount */
200 struct passwd *pw = getpwuid(getuid());
202 new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
204 if (flags & MS_USERS)
205 new_opts = xstrconcat3(new_opts, ",users", "");
207 for (om = opt_map; om->opt != NULL; om++) {
210 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
212 new_opts = xstrconcat3(new_opts, ",", om->opt);
215 if (extra_opts && *extra_opts) {
216 new_opts = xstrconcat3(new_opts, ",", extra_opts);
221 static int add_mtab(char *spec, char *mount_point, char *fstype,
222 int flags, char *opts, int freq, int pass)
226 int result = EX_FILEIO;
228 ment.mnt_fsname = spec;
229 ment.mnt_dir = mount_point;
230 ment.mnt_type = fstype;
231 ment.mnt_opts = fix_opts_string(flags, opts);
232 ment.mnt_freq = freq;
233 ment.mnt_passno = pass;
235 if (flags & MS_REMOUNT) {
236 update_mtab(ment.mnt_dir, &ment);
242 if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
244 nfs_error(_("Can't open mtab: %s"),
249 if (addmntent(mtab, &ment) == 1) {
250 nfs_error(_("Can't write mount entry to mtab: %s"),
255 if (fchmod(fileno(mtab), 0644) == -1) {
256 nfs_error(_("Can't set permissions on mtab: %s"),
271 void mount_usage(void)
273 printf(_("usage: %s remotetarget dir [-rvVwfnh] [-o nfsoptions]\n"),
275 printf(_("options:\n"));
276 printf(_("\t-r\t\tMount file system readonly\n"));
277 printf(_("\t-v\t\tVerbose\n"));
278 printf(_("\t-V\t\tPrint version\n"));
279 printf(_("\t-w\t\tMount file system read-write\n"));
280 printf(_("\t-f\t\tFake mount, do not actually mount\n"));
281 printf(_("\t-n\t\tDo not update /etc/mtab\n"));
282 printf(_("\t-s\t\tTolerate sloppy mount options rather than failing.\n"));
283 printf(_("\t-h\t\tPrint this help\n"));
284 printf(_("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n"));
287 static void parse_opt(const char *opt, int *mask, char *extra_opts, int len)
289 const struct opt_map *om;
291 for (om = opt_map; om->opt != NULL; om++) {
292 if (!strcmp (opt, om->opt)) {
301 len -= strlen(extra_opts);
303 if (*extra_opts && --len > 0)
304 strcat(extra_opts, ",");
306 if ((len -= strlen(opt)) > 0)
307 strcat(extra_opts, opt);
311 * Convert the provided mount command-line options into the 4th &
312 * 5th arguments to mount(2). Output parameter "@flags" gets the
313 * standard options (indicated by MS_ bits), and output parameter
314 * "@extra_opts" gets all the filesystem-specific options.
316 static void parse_opts(const char *options, int *flags, char **extra_opts)
318 if (options != NULL) {
319 char *opts = xstrdup(options);
321 int len = strlen(opts) + 1; /* include room for a null */
324 *extra_opts = xmalloc(len);
327 for (p = opts, opt = NULL; p && *p; p++) {
329 opt = p; /* begin of the option item */
331 open_quote ^= 1; /* reverse the status */
333 continue; /* still in a quoted block */
335 *p = '\0'; /* terminate the option item */
337 /* end of option item or last item */
338 if (*p == '\0' || *(p + 1) == '\0') {
339 parse_opt(opt, flags, *extra_opts, len);
347 static int chk_mountpoint(char *mount_point)
351 if (stat(mount_point, &sb) < 0){
352 mount_error(NULL, mount_point, errno);
355 if (S_ISDIR(sb.st_mode) == 0){
356 mount_error(NULL, mount_point, ENOTDIR);
359 if (access(mount_point, X_OK) < 0) {
360 mount_error(NULL, mount_point, errno);
367 static int try_mount(char *spec, char *mount_point, int flags,
368 char *fs_type, char **extra_opts, char *mount_opts,
369 int fake, int nomtab, int bg)
373 if (strcmp(fs_type, "nfs4") == 0)
374 ret = nfs4mount(spec, mount_point, flags,
375 extra_opts, fake, bg);
377 ret = nfsmount(spec, mount_point, flags,
378 extra_opts, fake, bg);
384 print_one(spec, mount_point, fs_type, mount_opts);
387 ret = add_mtab(spec, mount_point, fs_type, flags, *extra_opts,
388 0, 0 /* these are always zero for NFS */ );
392 int main(int argc, char *argv[])
394 int c, flags = 0, mnt_err = 1, fake = 0;
395 char *spec, *mount_point, *fs_type = "nfs";
396 char *extra_opts = NULL, *mount_opts = NULL;
397 uid_t uid = getuid();
399 progname = basename(argv[0]);
401 discover_nfs_mount_data_version();
403 if(!strncmp(progname, "umount", strlen("umount")))
404 exit(nfsumount(argc, argv));
406 if (argv[1] && argv[1][0] == '-') {
407 if(argv[1][1] == 'V')
408 printf("%s ("PACKAGE_STRING")\n", progname);
420 mount_point = argv[2];
422 argv[2] = argv[0]; /* so that getopt error messages are correct */
423 while ((c = getopt_long(argc - 2, argv + 2, "rvVwfno:hs",
424 longopts, NULL)) != -1) {
433 printf("%s: ("PACKAGE_STRING")\n", progname);
444 case 'o': /* specify mount options */
446 mount_opts = xstrconcat3(mount_opts, ",", optarg);
448 mount_opts = xstrdup(optarg);
461 * Extra non-option words at the end are bogus...
463 if (optind != argc - 2) {
468 if (strcmp(progname, "mount.nfs4") == 0)
472 * If a non-root user is attempting to mount, make sure the
473 * user's requested options match the options specified in
474 * /etc/fstab; otherwise, don't allow the mount.
477 struct mntentchn *mc;
479 if ((mc = getfsfile(mount_point)) == NULL ||
480 strcmp(mc->m.mnt_fsname, spec) != 0 ||
481 strcmp(mc->m.mnt_type, fs_type) != 0) {
482 nfs_error(_("%s: permission denied: no match for %s "
483 "found in /etc/fstab"), progname, mount_point);
488 * 'mount' munges the options from fstab before passing them
489 * to us, so it is non-trivial to test that we have the correct
490 * set of options and we don't want to trust what the user
491 * gave us, so just take whatever is in /etc/fstab.
493 mount_opts = strdup(mc->m.mnt_opts);
496 mount_point = canonicalize(mount_point);
498 nfs_error(_("%s: no mount point provided"), progname);
501 if (mount_point[0] != '/') {
502 nfs_error(_("%s: unrecognized mount point %s"),
503 progname, mount_point);
508 parse_opts(mount_opts, &flags, &extra_opts);
511 if (!(flags & (MS_USERS|MS_USER))) {
512 nfs_error(_("%s: permission denied"), progname);
518 if (chk_mountpoint(mount_point)) {
523 mnt_err = try_mount(spec, mount_point, flags, fs_type, &extra_opts,
524 mount_opts, fake, nomtab, FOREGROUND);
525 if (mnt_err == EX_BG) {
526 printf(_("mount: backgrounding \"%s\"\n"), spec);
530 * Parent exits immediately with success. Make
531 * sure not to free "mount_point"
536 mnt_err = try_mount(spec, mount_point, flags, fs_type,
537 &extra_opts, mount_opts, fake,
539 if (verbose && mnt_err)
540 printf(_("%s: giving up \"%s\"\n"),