Whamcloud - gitweb
small fix
[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 "obdctl.h"
38 #include <lustre_ver.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                         endmntent(fp);
87                         fprintf(stderr, "%s: according to %s %s is "
88                                 "already mounted on %s\n",
89                                 progname, MOUNTED, spec, mtpt);
90                         return(EEXIST); 
91                 }
92         }
93         endmntent(fp);
94
95         return(0);
96 }
97
98 static int
99 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
100                   int flags, int freq, int pass)
101 {
102         FILE *fp;
103         struct mntent mnt;
104         int rc = 0;
105
106         mnt.mnt_fsname = spec;
107         mnt.mnt_dir = mtpt;
108         mnt.mnt_type = type;
109         mnt.mnt_opts = opts ? opts : "";
110         mnt.mnt_freq = freq;
111         mnt.mnt_passno = pass;
112
113         fp = setmntent(MOUNTED, "a+");
114         if (fp == NULL) {
115                 fprintf(stderr, "%s: setmntent(%s): %s:",
116                         progname, MOUNTED, strerror (errno));
117                 rc = 16;
118         } else {
119                 if ((addmntent(fp, &mnt)) == 1) {
120                         fprintf(stderr, "%s: addmntent: %s:",
121                                 progname, strerror (errno));
122                         rc = 16;
123                 }
124                 endmntent(fp);
125         }
126
127         return rc;
128 }
129
130 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
131 #define MAXNIDSTR 1024
132 static char *convert_hostnames(char *s1)
133 {
134         char *converted, *s2 = 0, *c;
135         char sep;
136         int left = MAXNIDSTR;
137         lnet_nid_t nid;
138         
139         converted = malloc(left);
140         c = converted;
141         while ((left > 0) && (*s1 != '/')) {
142                 s2 = strpbrk(s1, ",:");
143                 if (!s2)
144                         goto out_free;
145                 sep = *s2;
146                 *s2 = '\0';     
147                 nid = libcfs_str2nid(s1);
148                 if (nid == LNET_NID_ANY)
149                         goto out_free;
150                 if (LNET_NETTYP(LNET_NIDNET(nid)) == SOCKLND) {
151                         __u32 addr = LNET_NIDADDR(nid);
152                         c += snprintf(c, left, "%u.%u.%u.%u@%s%u%c",
153                                       (addr >> 24) & 0xff, (addr >> 16) & 0xff,
154                                       (addr >> 8) & 0xff, addr & 0xff,
155                                       libcfs_lnd2str(SOCKLND), 
156                                       LNET_NETNUM(LNET_NIDNET(nid)), sep);
157                 } else {
158                         c += snprintf(c, left, "%s%c", s1, sep);
159                 }
160                 left = converted + MAXNIDSTR - c;
161                 s1 = s2 + 1;
162         }
163         snprintf(c, left, "%s", s1);
164         return converted;
165 out_free:
166         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
167         free(converted);
168         return NULL;
169 }
170
171 /*****************************************************************************
172  *
173  * This part was cribbed from util-linux/mount/mount.c.  There was no clear
174  * license information, but many other files in the package are identified as
175  * GNU GPL, so it's a pretty safe bet that was their intent.
176  *
177  ****************************************************************************/
178 struct opt_map {
179         const char *opt;        /* option name */
180         int skip;               /* skip in mtab option string */
181         int inv;                /* true if flag value should be inverted */
182         int mask;               /* flag mask value */
183 };
184
185 static const struct opt_map opt_map[] = {
186   /* These flags are parsed by mount, not lustre */
187   { "defaults", 0, 0, 0         },      /* default options */
188   { "rw",       1, 1, MS_RDONLY },      /* read-write */
189   { "ro",       0, 0, MS_RDONLY },      /* read-only */
190   { "exec",     0, 1, MS_NOEXEC },      /* permit execution of binaries */
191   { "noexec",   0, 0, MS_NOEXEC },      /* don't execute binaries */
192   { "suid",     0, 1, MS_NOSUID },      /* honor suid executables */
193   { "nosuid",   0, 0, MS_NOSUID },      /* don't honor suid executables */
194   { "dev",      0, 1, MS_NODEV  },      /* interpret device files  */
195   { "nodev",    0, 0, MS_NODEV  },      /* don't interpret devices */
196   { "async",    0, 1, MS_SYNCHRONOUS},  /* asynchronous I/O */
197   { "auto",     0, 0, 0         },      /* Can be mounted using -a */
198   { "noauto",   0, 0, 0         },      /* Can only be mounted explicitly */
199   { "nousers",  0, 1, 0         },      /* Forbid ordinary user to mount */
200   { "nouser",   0, 1, 0         },      /* Forbid ordinary user to mount */
201   { "noowner",  0, 1, 0         },      /* Device owner has no special privs */
202   { "_netdev",  0, 0, 0         },      /* Device accessible only via network */
203   /* These strings are passed through and parsed in lustre ll_options */
204   { "flock",    0, 0, 0         },      /* Enable flock support */
205   { "noflock",  1, 1, 0         },      /* Disable flock support */
206   { "user_xattr",   0, 0, 0     },      /* Enable get/set user xattr */
207   { "nouser_xattr", 1, 1, 0     },      /* Disable user xattr */
208   { "acl",      0, 0, 0         },      /* Enable ACL support */
209   { "noacl",    1, 1, 0         },      /* Disable ACL support */
210   { "nosvc",    0, 0, 0         },      /* Only start MGS/MGC, nothing else */
211   { "exclude",  0, 0, 0         },      /* OST exclusion list */
212   { NULL,       0, 0, 0         }
213 };
214 /****************************************************************************/
215
216 /* 1  = found, flag set
217    0  = found, no flag set
218    -1 = not found in above list */
219 static int parse_one_option(const char *check, int *flagp)
220 {
221         const struct opt_map *opt;
222
223         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
224                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
225                         if (!opt->mask) 
226                                 return 0;
227                         if (opt->inv)
228                                 *flagp &= ~(opt->mask);
229                         else
230                                 *flagp |= opt->mask;
231                         return 1;
232                 }
233         }
234         fprintf(stderr, "%s: ignoring unknown option '%s'\n", progname,
235                 check);
236         return -1;
237 }
238
239 int parse_options(char *orig_options, int *flagp)
240 {
241         char *options, *opt, *nextopt;
242
243         options = calloc(strlen(orig_options) + 1, 1);
244         *flagp = 0;
245         nextopt = orig_options;
246         while ((opt = strsep(&nextopt, ","))) {
247                 if (!*opt) 
248                         /* empty option */
249                         continue;
250                 if (parse_one_option(opt, flagp) == 0) {
251                         /* no mount flags set, so pass this on as an option */
252                         if (*options)
253                                 strcat(options, ",");
254                         strcat(options, opt);
255                 }
256         }
257         /* options will always be <= orig_options */
258         strcpy(orig_options, options);
259         free(options);
260         return 0;
261 }
262
263
264 int main(int argc, char *const argv[])
265 {
266         char default_options[] = "";
267         char *source, *target, *options = default_options, *optcopy;
268         int i, nargs = 3, opt, rc, flags, optlen;
269         static struct option long_opt[] = {
270                 {"fake", 0, 0, 'f'},
271                 {"force", 0, 0, 1},
272                 {"help", 0, 0, 'h'},
273                 {"nomtab", 0, 0, 'n'},
274                 {"options", 1, 0, 'o'},
275                 {"verbose", 0, 0, 'v'},
276                 {0, 0, 0, 0}
277         };
278
279         progname = strrchr(argv[0], '/');
280         progname = progname ? progname + 1 : argv[0];
281
282         while ((opt = getopt_long(argc, argv, "fhno:v",
283                                   long_opt, NULL)) != EOF){
284                 switch (opt) {
285                 case 1:
286                         ++force;
287                         printf("force: %d\n", force);
288                         nargs++;
289                         break;
290                 case 'f':
291                         ++fake;
292                         printf("fake: %d\n", fake);
293                         nargs++;
294                         break;
295                 case 'h':
296                         usage(stdout);
297                         break;
298                 case 'n':
299                         ++nomtab;
300                         printf("nomtab: %d\n", nomtab);
301                         nargs++;
302                         break;
303                 case 'o':
304                         options = optarg;
305                         nargs++;
306                         break;
307                 case 'v':
308                         ++verbose;
309                         printf("verbose: %d\n", verbose);
310                         nargs++;
311                         break;
312                 default:
313                         fprintf(stderr, "%s: unknown option '%c'\n",
314                                 progname, opt);
315                         usage(stderr);
316                         break;
317                 }
318         }
319
320         if (optind + 2 > argc) {
321                 fprintf(stderr, "%s: too few arguments\n", progname);
322                 usage(stderr);
323         }
324
325         source = convert_hostnames(argv[optind]);
326         target = argv[optind + 1];
327
328         if (!source) {
329                 usage(stderr);
330         }
331
332         if (verbose > 1) {
333                 for (i = 0; i < argc; i++)
334                         printf("arg[%d] = %s\n", i, argv[i]);
335                 printf("source = %s, target = %s\n", source, target);
336         }
337
338         if (!force && check_mtab_entry(source, target, "lustre"))
339                 return(EEXIST);
340
341         rc = parse_options(options, &flags); 
342         if (rc) {
343                 fprintf(stderr, "%s: can't parse options: %s\n",
344                         progname, options);
345                 return(EINVAL);
346         }
347
348         rc = access(target, F_OK);
349         if (rc) {
350                 rc = errno;
351                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
352                         strerror(errno));
353                 return rc;
354         }
355
356         /* In Linux 2.4, the target device doesn't get passed to any of our
357            functions.  So we'll stick it on the end of the options. */
358         optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
359         optcopy = malloc(optlen);
360         strcpy(optcopy, options);
361         if (*optcopy)
362                 strcat(optcopy, ",");
363         strcat(optcopy, "device=");
364         strcat(optcopy, source);
365
366         if (verbose) 
367                 printf("mounting device %s at %s, flags=%#x options=%s\n",
368                        source, target, flags, optcopy);
369         
370         if (!fake)
371                 /* flags and target get to lustre_get_sb, but not 
372                    lustre_fill_super.  Lustre ignores the flags, but mount 
373                    does not. */
374                 rc = mount(source, target, "lustre", flags, (void *)optcopy);
375
376         if (rc) {
377                 fprintf(stderr, "%s: mount(%s, %s) failed: %s\n", progname, 
378                         source, target, strerror(errno));
379                 if (errno == ENODEV)
380                         fprintf(stderr, "Are the lustre modules loaded?\n"
381                              "Check /etc/modules.conf and /proc/filesystems\n");
382                 if (errno == ENOTBLK)
383                         fprintf(stderr,"Does this filesystem have any OSTs?\n");
384                 if (errno == ENOENT)
385                         fprintf(stderr,"Is the MGS specification correct? "
386                                 "(%s)\n", source);
387                 if (errno == EALREADY)
388                         fprintf(stderr,"The target service is already running. "
389                                 "(%s)\n", source);
390                 if (errno == ENXIO)
391                         fprintf(stderr,"The target service failed to start "
392                                 "(bad config log?) (%s)\n", source);
393                 if (errno == EIO)
394                         fprintf(stderr,"Is the MGS running? (%s)\n", source);
395                 if (errno == EADDRINUSE)
396                         fprintf(stderr,"The target service's index is already "
397                                 "in use. (%s)\n", source);
398                 rc = errno;
399         } else if (!nomtab) {
400                 rc = update_mtab_entry(source, target, "lustre", options,0,0,0);
401         }
402
403         free(optcopy);
404         free(source);
405         return rc;
406 }