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"
48 static struct option longopts[] = {
49 { "fake", 0, 0, 'f' },
50 { "help", 0, 0, 'h' },
51 { "no-mtab", 0, 0, 'n' },
52 { "read-only", 0, 0, 'r' },
54 { "verbose", 0, 0, 'v' },
55 { "version", 0, 0, 'V' },
56 { "read-write", 0, 0, 'w' },
58 { "options", 1, 0, 'o' },
59 { "nfsvers", 1, 0, 't' },
60 { "bind", 0, 0, 128 },
61 { "replace", 0, 0, 129 },
62 { "after", 0, 0, 130 },
63 { "before", 0, 0, 131 },
64 { "over", 0, 0, 132 },
65 { "move", 0, 0, 133 },
66 { "rbind", 0, 0, 135 },
70 /* Map from -o and fstab option strings to the flag argument to mount(2). */
72 const char *opt; /* option name */
73 int skip; /* skip in mtab option string */
74 int inv; /* true if flag value should be inverted */
75 int mask; /* flag mask value */
78 /* Custom mount options for our own purposes. */
79 /* Maybe these should now be freed for kernel use again */
80 #define MS_DUMMY 0x00000000
81 #define MS_USERS 0x40000000
82 #define MS_USER 0x20000000
83 #define MS_OWNER 0x10000000
84 #define MS_GROUP 0x08000000
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", 0, 0, MS_USERS }, /* Allow ordinary user to mount */
105 { "nousers", 0, 0, MS_USERS }, /* Forbid ordinary user to mount */
106 { "user", 0, 0, MS_USER }, /* Allow ordinary user to mount */
107 { "nouser", 0, 0, MS_USER }, /* Forbid ordinary user to mount */
108 { "owner", 0, 0, MS_OWNER }, /* Let the owner of the device mount */
109 { "noowner", 0, 0, MS_OWNER }, /* Device owner has no special privs */
110 { "group", 0, 0, MS_GROUP }, /* Let the group of the device mount */
111 { "nogroup", 0, 0, MS_GROUP }, /* 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 struct passwd *pw = getpwuid(getuid());
149 new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
152 for (om = opt_map; om->opt != NULL; om++) {
155 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
157 new_opts = xstrconcat3(new_opts, ",", om->opt);
160 if (extra_opts && *extra_opts) {
161 new_opts = xstrconcat3(new_opts, ",", extra_opts);
167 void copy_mntent(struct mntent *ment, nfs_mntent_t *nment)
169 /* Not sure why nfs_mntent_t should exist */
170 strcpy(nment->mnt_fsname, ment->mnt_fsname);
171 strcpy(nment->mnt_dir, ment->mnt_dir);
172 strcpy(nment->mnt_type, ment->mnt_type);
173 strcpy(nment->mnt_opts, ment->mnt_opts);
174 nment->mnt_freq = ment->mnt_freq;
175 nment->mnt_passno = ment->mnt_passno;
178 int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opts, int freq, int passno)
184 ment.mnt_fsname = fsname;
185 ment.mnt_dir = mount_point;
186 ment.mnt_type = fstype;
187 ment.mnt_opts = fix_opts_string(flags, opts);
188 ment.mnt_freq = freq;
189 ment.mnt_passno= passno;
191 if(flags & MS_REMOUNT) {
194 copy_mntent(&ment, &nment);
195 update_mtab(nment.mnt_dir, &nment);
199 if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1) {
200 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
205 if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
206 fprintf(stderr, "Can't open " MOUNTED);
210 if (addmntent(mtab, &ment) == 1) {
211 fprintf(stderr, "Can't write mount entry");
215 if (fchmod(fileno(mtab), 0644) == -1) {
216 fprintf(stderr, "Can't set perms on " MOUNTED);
222 if (unlink(MOUNTED"~") == -1) {
223 fprintf(stderr, "Can't remove "MOUNTED"~");
230 int do_mount_syscall(char *spec, char *node, char *type, int flags, void *data)
232 return mount(spec, node, type, flags, data);
237 printf("usage: %s remotetarget dir [-rvVwfnh] [-t version] [-o nfsoptions]\n", progname);
238 printf("options:\n\t-r\t\tMount file system readonly\n");
239 printf("\t-v\t\tVerbose\n");
240 printf("\t-V\t\tPrint version\n");
241 printf("\t-w\t\tMount file system read-write\n");
242 printf("\t-f\t\tFake mount, don't actually mount\n");
243 printf("\t-n\t\tDo not update /etc/mtab\n");
244 printf("\t-h\t\tPrint this help\n");
245 printf("\tversion\t\tnfs4 - NFS version 4, nfs - older NFS version supported\n");
246 printf("\tnfsoptions\tRefer mount.nfs(8) or nfs(5)\n\n");
250 parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
251 const struct opt_map *om;
253 for (om = opt_map; om->opt != NULL; om++) {
254 if (!strcmp (opt, om->opt)) {
263 len -= strlen(extra_opts);
265 if (*extra_opts && --len > 0)
266 strcat(extra_opts, ",");
268 if ((len -= strlen(opt)) > 0)
269 strcat(extra_opts, opt);
272 /* Take -o options list and compute 4th and 5th args to mount(2). flags
273 gets the standard options (indicated by bits) and extra_opts all the rest */
274 static void parse_opts (const char *options, int *flags, char **extra_opts)
276 if (options != NULL) {
277 char *opts = xstrdup(options);
279 int len = strlen(opts) + 20;
281 *extra_opts = xmalloc(len);
284 for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ","))
285 parse_opt(opt, flags, *extra_opts, len);
292 static void mount_error(char *node)
296 fprintf(stderr, "%s: mount point %s is not a directory\n", progname, node);
299 fprintf(stderr, "%s: %s is already mounted or busy\n", progname, node);
302 fprintf(stderr, "%s: mount point %s does not exist\n", progname, node);
305 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
309 int main(int argc, char *argv[])
311 int c, flags = 0, nfs_mount_vers = 0, mnt_err = 1, fake = 0;
312 char *spec, *mount_point, *extra_opts = NULL;
313 char *mount_opts = NULL, *p;
316 if ((p = strrchr(progname, '/')) != NULL)
319 if(!strncmp(progname, "umount", strlen("umount"))) {
324 return(nfsumount(argc, argv));
332 if(argv[1][0] == '-') {
333 if(argv[1][1] == 'V')
334 printf("%s ("PACKAGE_STRING")\n", progname);
340 while ((c = getopt_long (argc - 2, argv + 2, "rt:vVwfno:h",
341 longopts, NULL)) != -1) {
347 nfs_mount_vers = (strncmp(optarg, "nfs4", 4)) ? 0 : 4;
353 printf("%s: ("PACKAGE_STRING")\n", progname);
364 case 'o': /* specify mount options */
366 mount_opts = xstrconcat3(mount_opts, ",", optarg);
368 mount_opts = xstrdup(optarg);
373 case 129: /* replace */
374 mounttype = MS_REPLACE;
376 case 130: /* after */
377 mounttype = MS_AFTER;
379 case 131: /* before */
380 mounttype = MS_BEFORE;
388 case 135: /* rbind */
389 mounttype = MS_BIND | MS_REC;
399 mount_point = canonicalize(argv[2]);
401 parse_opts(mount_opts, &flags, &extra_opts);
403 if (getuid() != 0 && !(flags & MS_USERS) && !(flags & MS_USER)) {
404 fprintf(stderr, "%s: permission denied.\n", progname);
408 if (!strcmp(progname, "mount.nfs4") || nfs_mount_vers == 4) {
410 mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0);
413 if (!strcmp(progname, "mount.nfs")) {
414 mnt_err = nfsmount(spec, mount_point, &flags,
415 &extra_opts, &mount_opts, &nfs_mount_vers, 0);
419 if (!mnt_err && !fake) {
420 if(!(flags & MS_REMOUNT)) {
421 mnt_err = do_mount_syscall(spec, mount_point,
422 nfs_mount_vers == 4 ? "nfs4" : "nfs", flags, mount_opts);
425 mount_error(mount_point);
430 add_mtab(spec, mount_point, nfs_mount_vers == 4 ? "nfs4" : "nfs",
431 flags, extra_opts, 0, 0);