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.
26 #include <sys/types.h>
32 #include <sys/mount.h>
40 #include "mount_constants.h"
41 #include "mount_config.h"
42 #include "nfs_paths.h"
43 #include "nfs_mntent.h"
45 #include "nfs_mount.h"
46 #include "nfs4_mount.h"
53 int nfs_mount_data_version;
59 #define FOREGROUND (0)
60 #define BACKGROUND (1)
62 static struct option longopts[] = {
63 { "fake", 0, 0, 'f' },
64 { "help", 0, 0, 'h' },
65 { "no-mtab", 0, 0, 'n' },
66 { "read-only", 0, 0, 'r' },
68 { "verbose", 0, 0, 'v' },
69 { "version", 0, 0, 'V' },
70 { "read-write", 0, 0, 'w' },
72 { "options", 1, 0, 'o' },
77 * Map from -o and fstab option strings to the flag argument to mount(2).
80 const char *opt; /* option name */
81 int skip; /* skip in mtab option string */
82 int inv; /* true if flag value should be inverted */
83 int mask; /* flag mask value */
86 static const struct opt_map opt_map[] = {
87 { "defaults", 0, 0, 0 }, /* default options */
88 { "ro", 1, 0, MS_RDONLY }, /* read-only */
89 { "rw", 1, 1, MS_RDONLY }, /* read-write */
90 { "exec", 0, 1, MS_NOEXEC }, /* permit execution of binaries */
91 { "noexec", 0, 0, MS_NOEXEC }, /* don't execute binaries */
92 { "suid", 0, 1, MS_NOSUID }, /* honor suid executables */
93 { "nosuid", 0, 0, MS_NOSUID }, /* don't honor suid executables */
94 { "dev", 0, 1, MS_NODEV }, /* interpret device files */
95 { "nodev", 0, 0, MS_NODEV }, /* don't interpret devices */
96 { "sync", 0, 0, MS_SYNCHRONOUS}, /* synchronous I/O */
97 { "async", 0, 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
98 { "dirsync", 0, 0, MS_DIRSYNC}, /* synchronous directory modifications */
99 { "remount", 0, 0, MS_REMOUNT}, /* Alter flags of mounted FS */
100 { "bind", 0, 0, MS_BIND }, /* Remount part of tree elsewhere */
101 { "rbind", 0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
102 { "auto", 0, 0, MS_DUMMY }, /* Can be mounted using -a */
103 { "noauto", 0, 0, MS_DUMMY }, /* Can only be mounted explicitly */
104 { "users", 1, 0, MS_USERS }, /* Allow ordinary user to mount */
105 { "nousers", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
106 { "user", 1, 0, MS_USER }, /* 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 */
138 { "relatime", 0, 0, MS_RELATIME }, /* Update access times relative to
140 { "norelatime", 0, 1, MS_RELATIME }, /* Update access time without regard
143 { "noquota", 0, 0, MS_DUMMY }, /* Don't enforce quota */
144 { "quota", 0, 0, MS_DUMMY }, /* Enforce user quota */
145 { "usrquota", 0, 0, MS_DUMMY }, /* Enforce user quota */
146 { "grpquota", 0, 0, MS_DUMMY }, /* Enforce group quota */
150 static void parse_opts(const char *options, int *flags, char **extra_opts);
153 * Build a canonical mount option string for /etc/mtab.
155 static char *fix_opts_string(int flags, const char *extra_opts)
157 const struct opt_map *om;
160 new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
161 if (flags & MS_USER) {
162 /* record who mounted this so they can unmount */
163 struct passwd *pw = getpwuid(getuid());
165 new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
167 if (flags & MS_USERS)
168 new_opts = xstrconcat3(new_opts, ",users", "");
170 for (om = opt_map; om->opt != NULL; om++) {
173 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
175 new_opts = xstrconcat3(new_opts, ",", om->opt);
178 if (extra_opts && *extra_opts) {
179 new_opts = xstrconcat3(new_opts, ",", extra_opts);
185 init_mntent(struct mntent *mnt, char *fsname, char *dir, char *type,
186 int flags, char *opts)
188 mnt->mnt_fsname = fsname;
190 mnt->mnt_type = type;
191 mnt->mnt_opts = fix_opts_string(flags & ~MS_NOMTAB, opts);
193 /* these are always zero for NFS */
198 /* Create mtab with a root entry. */
201 struct mntentchn *fstab;
208 mfp = nfs_setmntent (MOUNTED, "a+");
209 if (mfp == NULL || mfp->mntent_fp == NULL) {
211 die (EX_FILEIO, _("mount: can't open %s for writing: %s"),
212 MOUNTED, strerror (errsv));
215 /* Find the root entry by looking it up in fstab */
216 if ((fstab = getfsfile ("/")) || (fstab = getfsfile ("root"))) {
218 parse_opts (fstab->m.mnt_opts, &flags, &extra_opts);
219 init_mntent(&mnt, xstrdup(fstab->m.mnt_fsname), "/",
220 fstab->m.mnt_type, flags, extra_opts);
223 if (nfs_addmntent (mfp, &mnt) == 1) {
225 die (EX_FILEIO, _("mount: error writing %s: %s"),
226 _PATH_MOUNTED, strerror (errsv));
229 if (fchmod (fileno (mfp->mntent_fp), 0644) < 0)
230 if (errno != EROFS) {
233 _("mount: error changing mode of %s: %s"),
234 _PATH_MOUNTED, strerror (errsv));
243 static int add_mtab(char *spec, char *mount_point, char *fstype,
244 int flags, char *opts)
247 int result = EX_SUCCESS;
249 init_mntent(&ment, spec, mount_point, fstype, flags, opts);
251 if (!nomtab && mtab_does_not_exist()) {
253 printf(_("mount: no %s found - creating it..\n"),
258 if (!nomtab && mtab_is_writable()) {
259 if (flags & MS_REMOUNT)
260 update_mtab(ment.mnt_dir, &ment);
265 mtab = nfs_setmntent(MOUNTED, "a+");
266 if (mtab == NULL || mtab->mntent_fp == NULL) {
267 nfs_error(_("Can't open mtab: %s"),
271 if (nfs_addmntent(mtab, &ment) == 1) {
272 nfs_error(_("Can't write mount entry to mtab: %s"),
287 static void parse_opt(const char *opt, int *mask, char *extra_opts, size_t 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 size_t 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 try_mount(char *spec, char *mount_point, int flags,
348 char *fs_type, char **extra_opts, char *mount_opts,
354 ret = nfsmount_string(spec, mount_point, fs_type, flags,
355 extra_opts, fake, bg);
357 if (strcmp(fs_type, "nfs4") == 0)
358 ret = nfs4mount(spec, mount_point, flags,
359 extra_opts, fake, bg);
361 ret = nfsmount(spec, mount_point, flags,
362 extra_opts, fake, bg);
369 print_one(spec, mount_point, fs_type, mount_opts);
371 return add_mtab(spec, mount_point, fs_type, flags, *extra_opts);
374 int main(int argc, char *argv[])
376 int c, flags = 0, mnt_err = 1, fake = 0;
377 char *spec, *mount_point, *fs_type = "nfs";
378 char *extra_opts = NULL, *mount_opts = NULL;
379 uid_t uid = getuid();
381 progname = basename(argv[0]);
383 nfs_mount_data_version = discover_nfs_mount_data_version(&string);
385 if(!strncmp(progname, "umount", strlen("umount")))
386 exit(nfsumount(argc, argv));
388 if (argv[1] && argv[1][0] == '-') {
389 if(argv[1][1] == 'V')
390 printf("%s ("PACKAGE_STRING")\n", progname);
402 mount_point = argv[2];
404 mount_config_init(progname);
406 argv[2] = argv[0]; /* so that getopt error messages are correct */
407 while ((c = getopt_long(argc - 2, argv + 2, "rvVwfno:hs",
408 longopts, NULL)) != -1) {
417 printf("%s: ("PACKAGE_STRING")\n", progname);
428 case 'o': /* specify mount options */
430 mount_opts = xstrconcat3(mount_opts, ",", optarg);
432 mount_opts = xstrdup(optarg);
445 * Extra non-option words at the end are bogus...
447 if (optind != argc - 2) {
452 if (strcmp(progname, "mount.nfs4") == 0)
456 * If a non-root user is attempting to mount, make sure the
457 * user's requested options match the options specified in
458 * /etc/fstab; otherwise, don't allow the mount.
461 struct mntentchn *mc;
463 if ((mc = getfsfile(mount_point)) == NULL ||
464 strcmp(mc->m.mnt_fsname, spec) != 0 ||
465 strcmp(mc->m.mnt_type, fs_type) != 0) {
466 nfs_error(_("%s: permission denied: no match for %s "
467 "found in /etc/fstab"), progname, mount_point);
472 * 'mount' munges the options from fstab before passing them
473 * to us, so it is non-trivial to test that we have the correct
474 * set of options and we don't want to trust what the user
475 * gave us, so just take whatever is in /etc/fstab.
477 mount_opts = strdup(mc->m.mnt_opts);
480 mount_point = canonicalize(mount_point);
482 nfs_error(_("%s: no mount point provided"), progname);
485 if (mount_point[0] != '/') {
486 nfs_error(_("%s: unrecognized mount point %s"),
487 progname, mount_point);
492 * Concatenate mount options from the configuration file
494 mount_opts = mount_config_opts(spec, mount_point, mount_opts);
496 parse_opts(mount_opts, &flags, &extra_opts);
499 if (!(flags & (MS_USERS|MS_USER))) {
500 nfs_error(_("%s: permission denied"), progname);
505 if (geteuid() != 0) {
506 nfs_error(_("%s: not installed setuid - "
507 "\"user\" NFS mounts not supported."), progname);
512 if (chk_mountpoint(mount_point)) {
517 mnt_err = try_mount(spec, mount_point, flags, fs_type, &extra_opts,
518 mount_opts, fake, FOREGROUND);
519 if (mnt_err == EX_BG) {
520 printf(_("%s: backgrounding \"%s\"\n"),
522 printf(_("%s: mount options: \"%s\"\n"),
523 progname, extra_opts);
528 * Parent exits immediately with success.
531 nfs_error(_("%s: failed to start "
532 "background process: %s\n"),
533 progname, strerror(errno));
537 mnt_err = try_mount(spec, mount_point, flags, fs_type,
538 &extra_opts, mount_opts, fake,
540 if (verbose && mnt_err)
541 printf(_("%s: giving up \"%s\"\n"),