Whamcloud - gitweb
b=9853
[fs/lustre-release.git] / lustre / utils / mount_lustre.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Robert Read <rread@clusterfs.com>
6  *   Author: Nathan Rutman <nathan@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre 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.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25
26 #define _GNU_SOURCE
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/mount.h>
34 #include <mntent.h>
35 #include <getopt.h>
36 #include <sys/utsname.h>
37 #include <linux/lustre_ver.h>
38 #include "obdctl.h"
39
40 int          verbose = 0;
41 int          nomtab = 0;
42 int          fake = 0;
43 int          force = 0;
44 static char *progname = NULL;
45
46 void usage(FILE *out)
47 {
48         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
49         fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n", 
50                 progname);
51         fprintf(out, 
52                 "\t<device>: the disk device, or for a client:\n"
53                 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
54                 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
55                 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
56                 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
57                 "\t--force: force mount even if already in /etc/mtab\n"
58                 "\t-h|--help: print this usage message\n"
59                 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
60                 "\t-v|--verbose: print verbose config settings\n"
61                 "\t<mntopt>: one or more comma separated of:\n"
62                 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
63                 "\t\tnosvc: only start MGC/MGS obds\n"
64                 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
65                 "inactive OSTs (e.g. lustre-OST0001)\n"
66                 );
67         exit((out != stdout) ? EINVAL : 0);
68 }
69
70 static int check_mtab_entry(char *spec, char *mtpt, char *type)
71 {
72         FILE *fp;
73         struct mntent *mnt;
74
75         if (force)
76                 return (0);
77
78         fp = setmntent(MOUNTED, "r");
79         if (fp == NULL)
80                 return(0);
81
82         while ((mnt = getmntent(fp)) != NULL) {
83                 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
84                         strcmp(mnt->mnt_dir, mtpt) == 0 &&
85                         strcmp(mnt->mnt_type, type) == 0) {
86                         fprintf(stderr, "%s: according to %s %s is "
87                                 "already mounted on %s\n",
88                                 progname, MOUNTED, spec, mtpt);
89                         return(1); /* or should we return an error? */
90                 }
91         }
92         endmntent(fp);
93
94         return(0);
95 }
96
97 static int
98 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
99                   int flags, int freq, int pass)
100 {
101         FILE *fp;
102         struct mntent mnt;
103         int rc = 0;
104
105         mnt.mnt_fsname = spec;
106         mnt.mnt_dir = mtpt;
107         mnt.mnt_type = type;
108         mnt.mnt_opts = opts ? opts : "";
109         mnt.mnt_freq = freq;
110         mnt.mnt_passno = pass;
111
112         fp = setmntent(MOUNTED, "a+");
113         if (fp == NULL) {
114                 fprintf(stderr, "%s: setmntent(%s): %s:",
115                         progname, MOUNTED, strerror (errno));
116                 rc = 16;
117         } else {
118                 if ((addmntent(fp, &mnt)) == 1) {
119                         fprintf(stderr, "%s: addmntent: %s:",
120                                 progname, strerror (errno));
121                         rc = 16;
122                 }
123                 endmntent(fp);
124         }
125
126         return rc;
127 }
128
129 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
130 #define MAXNIDSTR 1024
131 static char *convert_hostnames(char *s1)
132 {
133         char *converted, *s2 = 0, *c;
134         char sep;
135         int left = MAXNIDSTR;
136         lnet_nid_t nid;
137         
138         converted = malloc(left);
139         c = converted;
140         while ((left > 0) && (*s1 != '/')) {
141                 s2 = strpbrk(s1, ",:");
142                 if (!s2)
143                         goto out_free;
144                 sep = *s2;
145                 *s2 = '\0';     
146                 nid = libcfs_str2nid(s1);
147                 if (nid == LNET_NID_ANY)
148                         goto out_free;
149                 if (LNET_NETTYP(LNET_NIDNET(nid)) == SOCKLND) {
150                         __u32 addr = LNET_NIDADDR(nid);
151                         c += snprintf(c, left, "%u.%u.%u.%u@%s%u%c",
152                                       (addr >> 24) & 0xff, (addr >> 16) & 0xff,
153                                       (addr >> 8) & 0xff, addr & 0xff,
154                                       libcfs_lnd2str(SOCKLND), 
155                                       LNET_NETNUM(LNET_NIDNET(nid)), sep);
156                 } else {
157                         c += snprintf(c, left, "%s%c", s1, sep);
158                 }
159                 left = converted + MAXNIDSTR - c;
160                 s1 = s2 + 1;
161         }
162         snprintf(c, left, "%s", s1);
163         return converted;
164 out_free:
165         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
166         free(converted);
167         return NULL;
168 }
169
170 /*****************************************************************************
171  *
172  * This part was cribbed from util-linux/mount/mount.c.  There was no clear
173  * license information, but many other files in the package are identified as
174  * GNU GPL, so it's a pretty safe bet that was their intent.
175  *
176  ****************************************************************************/
177 struct opt_map {
178         const char *opt;        /* option name */
179         int skip;               /* skip in mtab option string */
180         int inv;                /* true if flag value should be inverted */
181         int mask;               /* flag mask value */
182 };
183
184 static const struct opt_map opt_map[] = {
185   /* These flags are parsed by mount, not lustre */
186   { "defaults", 0, 0, 0         },      /* default options */
187   { "rw",       1, 1, MS_RDONLY },      /* read-write */
188   { "ro",       0, 0, MS_RDONLY },      /* read-only */
189   { "exec",     0, 1, MS_NOEXEC },      /* permit execution of binaries */
190   { "noexec",   0, 0, MS_NOEXEC },      /* don't execute binaries */
191   { "suid",     0, 1, MS_NOSUID },      /* honor suid executables */
192   { "nosuid",   0, 0, MS_NOSUID },      /* don't honor suid executables */
193   { "dev",      0, 1, MS_NODEV  },      /* interpret device files  */
194   { "nodev",    0, 0, MS_NODEV  },      /* don't interpret devices */
195   { "async",    0, 1, MS_SYNCHRONOUS},  /* asynchronous I/O */
196   { "auto",     0, 0, 0         },      /* Can be mounted using -a */
197   { "noauto",   0, 0, 0         },      /* Can only be mounted explicitly */
198   { "nousers",  0, 1, 0         },      /* Forbid ordinary user to mount */
199   { "nouser",   0, 1, 0         },      /* Forbid ordinary user to mount */
200   { "noowner",  0, 1, 0         },      /* Device owner has no special privs */
201   { "_netdev",  0, 0, 0         },      /* Device accessible only via network */
202   /* These strings are passed through and parsed in lustre ll_options */
203   { "flock",    0, 0, 0         },      /* Enable flock support */
204   { "noflock",  1, 1, 0         },      /* Disable flock support */
205   { "user_xattr",   0, 0, 0     },      /* Enable get/set user xattr */
206   { "nouser_xattr", 1, 1, 0     },      /* Disable user xattr */
207   { "acl",      0, 0, 0         },      /* Enable ACL support */
208   { "noacl",    1, 1, 0         },      /* Disable ACL support */
209   { "nosvc",    0, 0, 0         },      /* Only start MGS/MGC, nothing else */
210   { "exclude",  0, 0, 0         },      /* OST exclusion list */
211   { NULL,       0, 0, 0         }
212 };
213 /****************************************************************************/
214
215 /* 1  = found, flag set
216    0  = found, no flag set
217    -1 = not found in above list */
218 static int parse_one_option(const char *check, int *flagp)
219 {
220         const struct opt_map *opt;
221
222         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
223                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
224                         if (!opt->mask) 
225                                 return 0;
226                         if (opt->inv)
227                                 *flagp &= ~(opt->mask);
228                         else
229                                 *flagp |= opt->mask;
230                         return 1;
231                 }
232         }
233         fprintf(stderr, "%s: ignoring unknown option '%s'\n", progname,
234                 check);
235         return -1;
236 }
237
238 int parse_options(char *orig_options, int *flagp)
239 {
240         char *options, *opt, *nextopt;
241
242         options = calloc(strlen(orig_options) + 1, 1);
243         *flagp = 0;
244         nextopt = orig_options;
245         while ((opt = strsep(&nextopt, ","))) {
246                 if (!*opt) 
247                         /* empty option */
248                         continue;
249                 if (parse_one_option(opt, flagp) == 0) {
250                         /* no mount flags set, so pass this on as an option */
251                         if (*options)
252                                 strcat(options, ",");
253                         strcat(options, opt);
254                 }
255         }
256         /* options will always be <= orig_options */
257         strcpy(orig_options, options);
258         free(options);
259         return 0;
260 }
261
262
263 int main(int argc, char *const argv[])
264 {
265         char default_options[] = "";
266         char *source, *target, *options = default_options, *optcopy;
267         int i, nargs = 3, opt, rc, flags, optlen;
268         static struct option long_opt[] = {
269                 {"fake", 0, 0, 'f'},
270                 {"force", 0, 0, 1},
271                 {"help", 0, 0, 'h'},
272                 {"nomtab", 0, 0, 'n'},
273                 {"options", 1, 0, 'o'},
274                 {"verbose", 0, 0, 'v'},
275                 {0, 0, 0, 0}
276         };
277
278         progname = strrchr(argv[0], '/');
279         progname = progname ? progname + 1 : argv[0];
280
281         while ((opt = getopt_long(argc, argv, "fhno:v",
282                                   long_opt, NULL)) != EOF){
283                 switch (opt) {
284                 case 1:
285                         ++force;
286                         printf("force: %d\n", force);
287                         nargs++;
288                         break;
289                 case 'f':
290                         ++fake;
291                         printf("fake: %d\n", fake);
292                         nargs++;
293                         break;
294                 case 'h':
295                         usage(stdout);
296                         break;
297                 case 'n':
298                         ++nomtab;
299                         printf("nomtab: %d\n", nomtab);
300                         nargs++;
301                         break;
302                 case 'o':
303                         options = optarg;
304                         nargs++;
305                         break;
306                 case 'v':
307                         ++verbose;
308                         printf("verbose: %d\n", verbose);
309                         nargs++;
310                         break;
311                 default:
312                         fprintf(stderr, "%s: unknown option '%c'\n",
313                                 progname, opt);
314                         usage(stderr);
315                         break;
316                 }
317         }
318
319         if (optind + 2 > argc) {
320                 fprintf(stderr, "%s: too few arguments\n", progname);
321                 usage(stderr);
322         }
323
324         source = convert_hostnames(argv[optind]);
325         target = argv[optind + 1];
326
327         if (!source) {
328                 usage(stderr);
329         }
330
331         if (verbose > 1) {
332                 for (i = 0; i < argc; i++)
333                         printf("arg[%d] = %s\n", i, argv[i]);
334                 printf("source = %s, target = %s\n", source, target);
335         }
336
337         if (!force && check_mtab_entry(source, target, "lustre"))
338                 return(EEXIST);
339
340         rc = parse_options(options, &flags); 
341         if (rc) {
342                 fprintf(stderr, "%s: can't parse options: %s\n",
343                         progname, options);
344                 return(EINVAL);
345         }
346
347         rc = access(target, F_OK);
348         if (rc) {
349                 rc = errno;
350                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
351                         strerror(errno));
352                 return rc;
353         }
354
355         /* In Linux 2.4, the target device doesn't get passed to any of our
356            functions.  So we'll stick it on the end of the options. */
357         optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
358         optcopy = malloc(optlen);
359         strcpy(optcopy, options);
360         if (*optcopy)
361                 strcat(optcopy, ",");
362         strcat(optcopy, "device=");
363         strcat(optcopy, source);
364
365         if (verbose) 
366                 printf("mounting device %s at %s, flags=%#x options=%s\n",
367                        source, target, flags, optcopy);
368         
369         if (!fake)
370                 /* flags and target get to lustre_get_sb, but not 
371                    lustre_fill_super.  Lustre ignores the flags, but mount 
372                    does not. */
373                 rc = mount(source, target, "lustre", flags, (void *)optcopy);
374
375         if (rc) {
376                 fprintf(stderr, "%s: mount(%s, %s) failed: %s\n", progname, 
377                         source, target, strerror(errno));
378                 if (errno == ENODEV)
379                         fprintf(stderr, "Are the lustre modules loaded?\n"
380                              "Check /etc/modules.conf and /proc/filesystems\n");
381                 if (errno == ENOTBLK)
382                         fprintf(stderr,"Does this filesystem have any OSTs?\n");
383                 if (errno == ENOENT)
384                         fprintf(stderr,"Is the mgs specification correct? "
385                                 "(%s)\n", source);
386                 rc = errno;
387         } else if (!nomtab) {
388                 rc = update_mtab_entry(source, target, "lustre", options,0,0,0);
389         }
390
391         free(optcopy);
392         free(source);
393         return rc;
394 }