Whamcloud - gitweb
- make HEAD from b_post_cmd3
[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, "\nThis mount helper should only be invoked via the "
50                 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
51         fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n", 
52                 progname);
53         fprintf(out, 
54                 "\t<device>: the disk device, or for a client:\n"
55                 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
56                 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
57                 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
58                 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
59                 "\t--force: force mount even if already in /etc/mtab\n"
60                 "\t-h|--help: print this usage message\n"
61                 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
62                 "\t-v|--verbose: print verbose config settings\n"
63                 "\t<mntopt>: one or more comma separated of:\n"
64                 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
65                 "\t\tnosvc: only start MGC/MGS obds\n"
66                 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
67                 "inactive OSTs (e.g. lustre-OST0001)\n"
68                 );
69         exit((out != stdout) ? EINVAL : 0);
70 }
71
72 static int check_mtab_entry(char *spec, char *mtpt, char *type)
73 {
74         FILE *fp;
75         struct mntent *mnt;
76
77         fp = setmntent(MOUNTED, "r");
78         if (fp == NULL)
79                 return(0);
80
81         while ((mnt = getmntent(fp)) != NULL) {
82                 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
83                         strcmp(mnt->mnt_dir, mtpt) == 0 &&
84                         strcmp(mnt->mnt_type, type) == 0) {
85                         endmntent(fp);
86                         return(EEXIST); 
87                 }
88         }
89         endmntent(fp);
90
91         return(0);
92 }
93
94 static int
95 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
96                   int flags, int freq, int pass)
97 {
98         FILE *fp;
99         struct mntent mnt;
100         int rc = 0;
101
102         mnt.mnt_fsname = spec;
103         mnt.mnt_dir = mtpt;
104         mnt.mnt_type = type;
105         mnt.mnt_opts = opts ? opts : "";
106         mnt.mnt_freq = freq;
107         mnt.mnt_passno = pass;
108
109         fp = setmntent(MOUNTED, "a+");
110         if (fp == NULL) {
111                 fprintf(stderr, "%s: setmntent(%s): %s:",
112                         progname, MOUNTED, strerror (errno));
113                 rc = 16;
114         } else {
115                 if ((addmntent(fp, &mnt)) == 1) {
116                         fprintf(stderr, "%s: addmntent: %s:",
117                                 progname, strerror (errno));
118                         rc = 16;
119                 }
120                 endmntent(fp);
121         }
122
123         return rc;
124 }
125
126 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
127 #define MAXNIDSTR 1024
128 static char *convert_hostnames(char *s1)
129 {
130         char *converted, *s2 = 0, *c;
131         char sep;
132         int left = MAXNIDSTR;
133         lnet_nid_t nid;
134         
135         converted = malloc(left);
136         c = converted;
137         while ((left > 0) && (*s1 != '/')) {
138                 s2 = strpbrk(s1, ",:");
139                 if (!s2)
140                         goto out_free;
141                 sep = *s2;
142                 *s2 = '\0';     
143                 nid = libcfs_str2nid(s1);
144                 *s2 = sep;                      /* back to original string */
145                 if (nid == LNET_NID_ANY)
146                         goto out_free;
147                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
148                 left = converted + MAXNIDSTR - c;
149                 s1 = s2 + 1;
150         }
151         snprintf(c, left, "%s", s1);
152         return converted;
153 out_free:
154         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
155         free(converted);
156         return NULL;
157 }
158
159 /*****************************************************************************
160  *
161  * This part was cribbed from util-linux/mount/mount.c.  There was no clear
162  * license information, but many other files in the package are identified as
163  * GNU GPL, so it's a pretty safe bet that was their intent.
164  *
165  ****************************************************************************/
166 struct opt_map {
167         const char *opt;        /* option name */
168         int inv;                /* true if flag value should be inverted */
169         int mask;               /* flag mask value */
170 };
171
172 static const struct opt_map opt_map[] = {
173   /*"optname", inv,ms_mask */
174   /* These flags are parsed by mount, not lustre */
175   { "defaults", 0, 0         },      /* default options */
176   { "remount",  0, MS_REMOUNT},      /* remount with different options */
177   { "rw",       1, MS_RDONLY },      /* read-write */
178   { "ro",       0, MS_RDONLY },      /* read-only */
179   { "exec",     1, MS_NOEXEC },      /* permit execution of binaries */
180   { "noexec",   0, MS_NOEXEC },      /* don't execute binaries */
181   { "suid",     1, MS_NOSUID },      /* honor suid executables */
182   { "nosuid",   0, MS_NOSUID },      /* don't honor suid executables */
183   { "dev",      1, MS_NODEV  },      /* interpret device files  */
184   { "nodev",    0, MS_NODEV  },      /* don't interpret devices */
185   { "async",    1, MS_SYNCHRONOUS},  /* asynchronous I/O */
186   { "auto",     0, 0         },      /* Can be mounted using -a */
187   { "noauto",   0, 0         },      /* Can only be mounted explicitly */
188   { "nousers",  1, 0         },      /* Forbid ordinary user to mount */
189   { "nouser",   1, 0         },      /* Forbid ordinary user to mount */
190   { "noowner",  1, 0         },      /* Device owner has no special privs */
191   { "_netdev",  0, 0         },      /* Device accessible only via network */
192   { NULL,       0, 0         }
193 };
194 /****************************************************************************/
195
196 /* 1  = don't pass on to lustre
197    0  = pass on to lustre */
198 static int parse_one_option(const char *check, int *flagp)
199 {
200         const struct opt_map *opt;
201
202         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
203                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
204                         if (opt->mask) {
205                                 if (opt->inv)
206                                         *flagp &= ~(opt->mask);
207                                 else
208                                         *flagp |= opt->mask;
209                         }
210                         return 1;
211                 }
212         }
213         /* Assume any unknown options are valid and pass them on.  The mount
214            will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
215         return 0;
216 }
217
218 /* Replace options with subset of Lustre-specific options, and
219    fill in mount flags */
220 int parse_options(char *orig_options, int *flagp)
221 {
222         char *options, *opt, *nextopt;
223
224         options = calloc(strlen(orig_options) + 1, 1);
225         *flagp = 0;
226         nextopt = orig_options;
227         while ((opt = strsep(&nextopt, ","))) {
228                 if (!*opt) 
229                         /* empty option */
230                         continue;
231                 if (parse_one_option(opt, flagp) == 0) {
232                         /* pass this on as an option */
233                         if (*options)
234                                 strcat(options, ",");
235                         strcat(options, opt);
236                 }
237         }
238         strcpy(orig_options, options);
239         free(options);
240         return 0;
241 }
242
243
244 int main(int argc, char *const argv[])
245 {
246         char default_options[] = "";
247         char *usource, *source, *target, *ptr;
248         char *options, *optcopy, *orig_options = default_options;
249         int i, nargs = 3, opt, rc, flags, optlen;
250         static struct option long_opt[] = {
251                 {"fake", 0, 0, 'f'},
252                 {"force", 0, 0, 1},
253                 {"help", 0, 0, 'h'},
254                 {"nomtab", 0, 0, 'n'},
255                 {"options", 1, 0, 'o'},
256                 {"verbose", 0, 0, 'v'},
257                 {0, 0, 0, 0}
258         };
259
260         progname = strrchr(argv[0], '/');
261         progname = progname ? progname + 1 : argv[0];
262
263         while ((opt = getopt_long(argc, argv, "fhno:v",
264                                   long_opt, NULL)) != EOF){
265                 switch (opt) {
266                 case 1:
267                         ++force;
268                         printf("force: %d\n", force);
269                         nargs++;
270                         break;
271                 case 'f':
272                         ++fake;
273                         printf("fake: %d\n", fake);
274                         nargs++;
275                         break;
276                 case 'h':
277                         usage(stdout);
278                         break;
279                 case 'n':
280                         ++nomtab;
281                         printf("nomtab: %d\n", nomtab);
282                         nargs++;
283                         break;
284                 case 'o':
285                         orig_options = optarg;
286                         nargs++;
287                         break;
288                 case 'v':
289                         ++verbose;
290                         nargs++;
291                         break;
292                 default:
293                         fprintf(stderr, "%s: unknown option '%c'\n",
294                                 progname, opt);
295                         usage(stderr);
296                         break;
297                 }
298         }
299
300         if (optind + 2 > argc) {
301                 fprintf(stderr, "%s: too few arguments\n", progname);
302                 usage(stderr);
303         }
304
305         usource = argv[optind];
306         source = convert_hostnames(usource);
307         target = argv[optind + 1];
308         ptr = target + strlen(target) - 1;
309         while ((ptr > target) && (*ptr == '/')) {
310                 *ptr = 0;
311                 ptr--;
312         }
313
314         if (!usource || !source) {
315                 usage(stderr);
316         }
317
318         if (verbose) {
319                 for (i = 0; i < argc; i++)
320                         printf("arg[%d] = %s\n", i, argv[i]);
321                 printf("source = %s (%s), target = %s\n", usource, source, target);
322                 printf("options = %s\n", orig_options);
323         }
324
325         options = malloc(strlen(orig_options) + 1);
326         strcpy(options, orig_options);
327         rc = parse_options(options, &flags); 
328         if (rc) {
329                 fprintf(stderr, "%s: can't parse options: %s\n",
330                         progname, options);
331                 return(EINVAL);
332         }
333
334         if (!force) {
335                 rc = check_mtab_entry(usource, target, "lustre");
336                 if (rc && !(flags & MS_REMOUNT)) {
337                         fprintf(stderr, "%s: according to %s %s is "
338                                 "already mounted on %s\n",
339                                 progname, MOUNTED, usource, target);
340                         return(EEXIST);
341                 }
342                 if (!rc && (flags & MS_REMOUNT)) {
343                         fprintf(stderr, "%s: according to %s %s is "
344                                 "not already mounted on %s\n",
345                                 progname, MOUNTED, usource, target);
346                         return(ENOENT);
347                 }
348         }
349         if (flags & MS_REMOUNT) 
350                 nomtab++;
351
352         rc = access(target, F_OK);
353         if (rc) {
354                 rc = errno;
355                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
356                         strerror(errno));
357                 return rc;
358         }
359
360         /* In Linux 2.4, the target device doesn't get passed to any of our
361            functions.  So we'll stick it on the end of the options. */
362         optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
363         optcopy = malloc(optlen);
364         strcpy(optcopy, options);
365         if (*optcopy)
366                 strcat(optcopy, ",");
367         strcat(optcopy, "device=");
368         strcat(optcopy, source);
369
370         if (verbose) 
371                 printf("mounting device %s at %s, flags=%#x options=%s\n",
372                        source, target, flags, optcopy);
373         
374         if (!fake)
375                 /* flags and target get to lustre_get_sb, but not 
376                    lustre_fill_super.  Lustre ignores the flags, but mount 
377                    does not. */
378                 rc = mount(source, target, "lustre", flags, (void *)optcopy);
379
380         if (rc) {
381                 char *cli;
382
383                 rc = errno;
384
385                 cli = strrchr(usource, ':');
386                 if (cli && (strlen(cli) > 2)) 
387                         cli += 2;
388                 else
389                         cli = NULL;
390
391                 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname, 
392                         usource, target, strerror(errno));
393                 if (errno == ENODEV)
394                         fprintf(stderr, "Are the lustre modules loaded?\n"
395                                 "Check /etc/modprobe.conf and /proc/filesystems"
396                                 "\nNote 'alias lustre llite' should be removed"
397                                 " from modprobe.conf\n");
398                 if (errno == ENOTBLK)
399                         fprintf(stderr, "Do you need -o loop?\n");
400                 if (errno == ENOMEDIUM)
401                         fprintf(stderr, 
402                                 "This filesystem needs at least 1 OST\n");
403                 if (errno == ENOENT) {
404                         fprintf(stderr, "Is the MGS specification correct?\n");
405                         fprintf(stderr, "Is the filesystem name correct?\n");
406                         fprintf(stderr, "If upgrading, is the copied client log"
407                                 " valid? (see upgrade docs)\n");
408                 }
409                 if (errno == EALREADY)
410                         fprintf(stderr, "The target service is already running."
411                                 " (%s)\n", usource);
412                 if (errno == ENXIO)
413                         fprintf(stderr, "The target service failed to start "
414                                 "(bad config log?) (%s).  "
415                                 "See /var/log/messages.\n", usource);
416                 if (errno == EIO)
417                         fprintf(stderr, "Is the MGS running?\n");
418                 if (errno == EADDRINUSE)
419                         fprintf(stderr, "The target service's index is already "
420                                 "in use. (%s)\n", usource);
421                 if (errno == EINVAL) {
422                         fprintf(stderr, "This may have multiple causes.\n");
423                         if (cli) 
424                                 fprintf(stderr, "Is '%s' the correct filesystem"
425                                         " name?\n", cli);
426                         fprintf(stderr, "Are the mount options correct?\n");
427                         fprintf(stderr, "Check the syslog for more info.\n");
428                 }
429
430                 /* May as well try to clean up loop devs */
431                 if (strncmp(usource, "/dev/loop", 9) == 0) {
432                         char cmd[256];
433                         sprintf(cmd, "/sbin/losetup -d %s", usource);
434                         system(cmd);
435                 }
436
437         } else if (!nomtab) {
438                 rc = update_mtab_entry(usource, target, "lustre", orig_options,
439                                        0,0,0);
440         }
441
442         free(optcopy);
443         free(source);
444         return rc;
445 }