Whamcloud - gitweb
042fac10c32980968067c20fb0eb221f70921746
[fs/lustre-release.git] / lustre / utils / mount_lustre.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/mount_lustre.c
37  *
38  * Author: Robert Read <rread@clusterfs.com>
39  * Author: Nathan Rutman <nathan@clusterfs.com>
40  */
41
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <string.h>
51 #include "obdctl.h"
52 #include <lustre_ver.h>
53 #include <ctype.h>
54 #include <limits.h>
55 #include "mount_utils.h"
56
57 #define MAXOPT 4096
58 #define MAX_RETRIES 99
59
60 int          verbose = 0;
61 char         *progname = NULL;
62
63 void usage(FILE *out)
64 {
65         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
66         fprintf(out, "\nThis mount helper should only be invoked via the "
67                 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
68         fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
69                 progname);
70         fprintf(out,
71                 "\t<device>: the disk device, or for a client:\n"
72                 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
73                 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
74                 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
75                 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
76                 "\t-o force|--force: force mount even if already in /etc/mtab\n"
77                 "\t-h|--help: print this usage message\n"
78                 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
79                 "\t-v|--verbose: print verbose config settings\n"
80                 "\t<mntopt>: one or more comma separated of:\n"
81                 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
82                 "\t\tabort_recov: abort server recovery handling\n"
83                 "\t\tnosvc: only start MGC/MGS obds\n"
84                 "\t\tnomgs: only start target obds, using existing MGS\n"
85                 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
86                 "inactive OSTs (e.g. lustre-OST0001)\n"
87                 "\t\tretry=<num>: number of times mount is retried by client\n"
88                 "\t\tmd_stripe_cache_size=<num>: set the raid stripe cache "
89                 "size for the underlying raid if present\n"
90                 );
91         exit((out != stdout) ? EINVAL : 0);
92 }
93
94 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
95 #define MAXNIDSTR 1024
96 static char *convert_hostnames(char *s1)
97 {
98         char *converted, *s2 = 0, *c;
99         char sep;
100         int left = MAXNIDSTR;
101         lnet_nid_t nid;
102
103         converted = malloc(left);
104         if (converted == NULL) {
105                 fprintf(stderr, "out of memory: needed %d bytes\n",
106                         MAXNIDSTR);
107                 return NULL;
108         }
109         c = converted;
110         while ((left > 0) && (*s1 != '/')) {
111                 s2 = strpbrk(s1, ",:");
112                 if (!s2)
113                         goto out_free;
114                 sep = *s2;
115                 *s2 = '\0';
116                 nid = libcfs_str2nid(s1);
117                 *s2 = sep;                      /* back to original string */
118                 if (nid == LNET_NID_ANY)
119                         goto out_free;
120                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
121                 left = converted + MAXNIDSTR - c;
122                 s1 = s2 + 1;
123         }
124         snprintf(c, left, "%s", s1);
125         return converted;
126 out_free:
127         fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
128         free(converted);
129         return NULL;
130 }
131
132 /*****************************************************************************
133  *
134  * This part was cribbed from util-linux/mount/mount.c.  There was no clear
135  * license information, but many other files in the package are identified as
136  * GNU GPL, so it's a pretty safe bet that was their intent.
137  *
138  ****************************************************************************/
139 struct opt_map {
140         const char *opt;        /* option name */
141         int inv;                /* true if flag value should be inverted */
142         int mask;               /* flag mask value */
143 };
144
145 static const struct opt_map opt_map[] = {
146   /*"optname", inv,ms_mask */
147   /* These flags are parsed by mount, not lustre */
148   { "defaults", 0, 0         },      /* default options */
149   { "remount",  0, MS_REMOUNT},      /* remount with different options */
150   { "rw",       1, MS_RDONLY },      /* read-write */
151   { "ro",       0, MS_RDONLY },      /* read-only */
152   { "exec",     1, MS_NOEXEC },      /* permit execution of binaries */
153   { "noexec",   0, MS_NOEXEC },      /* don't execute binaries */
154   { "suid",     1, MS_NOSUID },      /* honor suid executables */
155   { "nosuid",   0, MS_NOSUID },      /* don't honor suid executables */
156   { "dev",      1, MS_NODEV  },      /* interpret device files  */
157   { "nodev",    0, MS_NODEV  },      /* don't interpret devices */
158   { "sync",     0, MS_SYNCHRONOUS},  /* synchronous I/O */
159   { "async",    1, MS_SYNCHRONOUS},  /* asynchronous I/O */
160   { "atime",    1, MS_NOATIME  },    /* set file access time on read */
161   { "noatime",  0, MS_NOATIME  },    /* do not set file access time on read */
162 #ifdef MS_NODIRATIME
163   { "diratime", 1, MS_NODIRATIME },  /* set file access time on read */
164   { "nodiratime",0,MS_NODIRATIME },  /* do not set file access time on read */
165 #endif
166 #ifdef MS_RELATIME
167   { "relatime", 0, MS_RELATIME },  /* set file access time on read */
168   { "norelatime",1,MS_RELATIME },  /* do not set file access time on read */
169 #endif
170 #ifdef MS_STRICTATIME
171   { "strictatime",0,MS_STRICTATIME },  /* update access time strictly */
172 #endif
173   { "auto",     0, 0         },      /* Can be mounted using -a */
174   { "noauto",   0, 0         },      /* Can only be mounted explicitly */
175   { "nousers",  1, 0         },      /* Forbid ordinary user to mount */
176   { "nouser",   1, 0         },      /* Forbid ordinary user to mount */
177   { "noowner",  1, 0         },      /* Device owner has no special privs */
178   { "_netdev",  0, 0         },      /* Device accessible only via network */
179   { "loop",     0, 0         },
180   { NULL,       0, 0         }
181 };
182 /****************************************************************************/
183
184 /* 1  = don't pass on to lustre
185    0  = pass on to lustre */
186 static int parse_one_option(const char *check, int *flagp)
187 {
188         const struct opt_map *opt;
189
190         for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
191                 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
192                         if (opt->mask) {
193                                 if (opt->inv)
194                                         *flagp &= ~(opt->mask);
195                                 else
196                                         *flagp |= opt->mask;
197                         }
198                         return 1;
199                 }
200         }
201         /* Assume any unknown options are valid and pass them on.  The mount
202            will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
203         return 0;
204 }
205
206 static void append_option(char *options, const char *one)
207 {
208         if (*options)
209                 strcat(options, ",");
210         strcat(options, one);
211 }
212
213 /* Replace options with subset of Lustre-specific options, and
214    fill in mount flags */
215 int parse_options(struct mount_opts *mop, char *orig_options, int *flagp)
216 {
217         char *options, *opt, *nextopt, *arg, *val;
218
219         options = calloc(strlen(orig_options) + 1, 1);
220         *flagp = 0;
221         nextopt = orig_options;
222         while ((opt = strsep(&nextopt, ","))) {
223                 if (!*opt)
224                         /* empty option */
225                         continue;
226
227                 /* Handle retries in a slightly different
228                  * manner */
229                 arg = opt;
230                 val = strchr(opt, '=');
231                 /* please note that some ldiskfs mount options are also in the form
232                  * of param=value. We should pay attention not to remove those
233                  * mount options, see bug 22097. */
234                 if (val && strncmp(arg, "md_stripe_cache_size", 20) == 0) {
235                         mop->mo_md_stripe_cache_size = atoi(val + 1);
236                 } else if (val && strncmp(arg, "retry", 5) == 0) {
237                         mop->mo_retry = atoi(val + 1);
238                         if (mop->mo_retry > MAX_RETRIES)
239                                 mop->mo_retry = MAX_RETRIES;
240                         else if (mop->mo_retry < 0)
241                                 mop->mo_retry = 0;
242                 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
243                         append_option(options, opt);
244                 } else if (strncmp(arg, "nosvc", 5) == 0) {
245                         mop->mo_nosvc = 1;
246                         append_option(options, opt);
247                 } else if (strcmp(opt, "force") == 0) {
248                         //XXX special check for 'force' option
249                         ++mop->mo_force;
250                         printf("force: %d\n", mop->mo_force);
251                 } else if (parse_one_option(opt, flagp) == 0) {
252                         /* pass this on as an option */
253                         append_option(options, opt);
254                 }
255         }
256 #ifdef MS_STRICTATIME
257                 /* set strictatime to default if NOATIME or RELATIME
258                    not given explicit */
259         if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
260                 *flagp |= MS_STRICTATIME;
261 #endif
262         strcpy(orig_options, options);
263         free(options);
264         return 0;
265 }
266
267 /* Add mgsnids from ldd params */
268 static int add_mgsnids(struct mount_opts *mop, char *options,
269                        const char *params)
270 {
271         char *ptr = (char *)params;
272         char tmp, *sep;
273
274         while ((ptr = strstr(ptr, PARAM_MGSNODE)) != NULL) {
275                 sep = strchr(ptr, ' ');
276                 if (sep != NULL) {
277                         tmp = *sep;
278                         *sep = '\0';
279                 }
280                 append_option(options, ptr);
281                 mop->mo_have_mgsnid++;
282                 if (sep) {
283                         *sep = tmp;
284                         ptr = sep;
285                 } else {
286                         break;
287                 }
288         }
289
290         return 0;
291 }
292
293 static int parse_ldd(char *source, struct mount_opts *mop, char *options)
294 {
295         struct lustre_disk_data *ldd = &mop->mo_ldd;
296         char *cur, *start;
297         int rc;
298
299         rc = osd_is_lustre(source, &ldd->ldd_mount_type);
300         if (rc == 0) {
301                 fprintf(stderr, "%s: %s has not been formatted with mkfs.lustre"
302                         " or the backend filesystem type is not supported by "
303                         "this tool\n", progname, source);
304                 return ENODEV;
305         }
306
307         rc = osd_read_ldd(source, ldd);
308         if (rc) {
309                 fprintf(stderr, "%s: %s failed to read permanent mount"
310                         " data: %s\n", progname, source, strerror(rc));
311                 return rc;
312         }
313
314         if (ldd->ldd_flags & LDD_F_NEED_INDEX) {
315                 fprintf(stderr, "%s: %s has no index assigned "
316                         "(probably formatted with old mkfs)\n",
317                         progname, source);
318                 return EINVAL;
319         }
320
321         if (ldd->ldd_flags & LDD_F_UPGRADE14) {
322                 fprintf(stderr, "%s: we cannot upgrade %s from this (very old) "
323                         "Lustre version\n", progname, source);
324                 return EINVAL;
325         }
326
327         /* Since we never rewrite ldd, ignore temp flags */
328         ldd->ldd_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE | LDD_F_WRITECONF);
329
330         /* svname of the form lustre:OST1234 means never registered */
331         rc = strlen(ldd->ldd_svname);
332         if (ldd->ldd_svname[rc - 8] == ':') {
333                 ldd->ldd_svname[rc - 8] = '-';
334                 ldd->ldd_flags |= LDD_F_VIRGIN;
335         } else if (ldd->ldd_svname[rc - 8] == '=') {
336                 ldd->ldd_svname[rc - 8] = '-';
337                 ldd->ldd_flags |= LDD_F_WRITECONF;
338         }
339
340         /* backend osd type */
341         append_option(options, "osd=");
342         strcat(options, mt_type(ldd->ldd_mount_type));
343
344         append_option(options, ldd->ldd_mount_opts);
345
346         if (!mop->mo_have_mgsnid) {
347                 /* Only use disk data if mount -o mgsnode=nid wasn't
348                  * specified */
349                 if (ldd->ldd_flags & LDD_F_SV_TYPE_MGS) {
350                         append_option(options, "mgs");
351                         mop->mo_have_mgsnid++;
352                 } else {
353                         add_mgsnids(mop, options, ldd->ldd_params);
354                 }
355         }
356         /* Better have an mgsnid by now */
357         if (!mop->mo_have_mgsnid) {
358                 fprintf(stderr, "%s: missing option mgsnode=<nid>\n",
359                         progname);
360                 return EINVAL;
361         }
362
363         if (ldd->ldd_flags & LDD_F_VIRGIN)
364                 append_option(options, "virgin");
365         if (ldd->ldd_flags & LDD_F_WRITECONF)
366                 append_option(options, "writeconf");
367         if (ldd->ldd_flags & LDD_F_IAM_DIR)
368                 append_option(options, "iam");
369         if (ldd->ldd_flags & LDD_F_NO_PRIMNODE)
370                 append_option(options, "noprimnode");
371
372         /* prefix every lustre parameter with param= so that in-kernel
373          * mount can recognize them properly and send to MGS at registration */
374         start = ldd->ldd_params;
375         while (start && *start != '\0') {
376                 while (*start == ' ') start++;
377                 if (*start == '\0')
378                         break;
379                 cur = start;
380                 start = strchr(cur, ' ');
381                 if (start) {
382                         *start = '\0';
383                         start++;
384                 }
385                 append_option(options, "param=");
386                 strcat(options, cur);
387         }
388
389         /* svname must be last option */
390         append_option(options, "svname=");
391         strcat(options, ldd->ldd_svname);
392
393         return 0;
394 }
395
396 static void set_defaults(struct mount_opts *mop)
397 {
398         memset(mop, 0, sizeof(*mop));
399         mop->mo_usource = NULL;
400         mop->mo_source = NULL;
401         mop->mo_nomtab = 0;
402         mop->mo_fake = 0;
403         mop->mo_force = 0;
404         mop->mo_retry = 0;
405         mop->mo_have_mgsnid = 0;
406         mop->mo_md_stripe_cache_size = 16384;
407         mop->mo_orig_options = "";
408         mop->mo_nosvc = 0;
409 }
410
411 static int parse_opts(int argc, char *const argv[], struct mount_opts *mop)
412 {
413         static struct option long_opt[] = {
414                 {"fake", 0, 0, 'f'},
415                 {"force", 0, 0, 1},
416                 {"help", 0, 0, 'h'},
417                 {"nomtab", 0, 0, 'n'},
418                 {"options", 1, 0, 'o'},
419                 {"verbose", 0, 0, 'v'},
420                 {0, 0, 0, 0}
421         };
422         char real_path[PATH_MAX] = {'\0'};
423         FILE *f;
424         char path[256], name[256];
425         size_t sz;
426         char *ptr;
427         int opt, rc;
428
429         while ((opt = getopt_long(argc, argv, "fhno:v",
430                                   long_opt, NULL)) != EOF){
431                 switch (opt) {
432                 case 1:
433                         ++mop->mo_force;
434                         printf("force: %d\n", mop->mo_force);
435                         break;
436                 case 'f':
437                         ++mop->mo_fake;
438                         printf("fake: %d\n", mop->mo_fake);
439                         break;
440                 case 'h':
441                         usage(stdout);
442                         break;
443                 case 'n':
444                         ++mop->mo_nomtab;
445                         printf("nomtab: %d\n", mop->mo_nomtab);
446                         break;
447                 case 'o':
448                         mop->mo_orig_options = optarg;
449                         break;
450                 case 'v':
451                         ++verbose;
452                         break;
453                 default:
454                         fprintf(stderr, "%s: unknown option '%c'\n",
455                                         progname, opt);
456                         usage(stderr);
457                         break;
458                 }
459         }
460
461         if (optind + 2 > argc) {
462                 fprintf(stderr, "%s: too few arguments\n", progname);
463                 usage(stderr);
464         }
465
466         mop->mo_usource = argv[optind];
467         if (!mop->mo_usource) {
468                 usage(stderr);
469         }
470
471         /**
472          * Try to get the real path to the device, in case it is a
473          * symbolic link for instance
474          */
475         if (realpath(mop->mo_usource, real_path) != NULL) {
476                 ptr = strrchr(real_path, '/');
477                 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
478                         snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
479                         if ((f = fopen(path, "r"))) {
480                                 /* read "<name>\n" from sysfs */
481                                 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
482                                         name[sz - 1] = '\0';
483                                         snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
484                                 }
485                                 fclose(f);
486                         }
487                 }
488                 mop->mo_usource = strdup(real_path);
489         }
490
491         ptr = strstr(mop->mo_usource, ":/");
492         if (ptr != NULL) {
493                 mop->mo_source = convert_hostnames(mop->mo_usource);
494                 if (!mop->mo_source)
495                         usage(stderr);
496         } else {
497                 mop->mo_source = strdup(mop->mo_usource);
498         }
499
500         if (realpath(argv[optind + 1], mop->mo_target) == NULL) {
501                 rc = errno;
502                 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
503                                 argv[optind + 1], strerror(errno));
504                 return rc;
505         }
506
507         return 0;
508 }
509
510 int main(int argc, char *const argv[])
511 {
512         struct mount_opts mop;
513         char *options;
514         int i, rc, flags;
515
516         progname = strrchr(argv[0], '/');
517         progname = progname ? progname + 1 : argv[0];
518
519         set_defaults(&mop);
520
521         rc = osd_init();
522         if (rc)
523                 return rc;
524
525         rc = parse_opts(argc, argv, &mop);
526         if (rc)
527                 return rc;
528
529         if (verbose) {
530                 for (i = 0; i < argc; i++)
531                         printf("arg[%d] = %s\n", i, argv[i]);
532                 printf("source = %s (%s), target = %s\n", mop.mo_usource,
533                        mop.mo_source, mop.mo_target);
534                 printf("options = %s\n", mop.mo_orig_options);
535         }
536
537         options = malloc(MAXOPT);
538         if (options == NULL) {
539                 fprintf(stderr, "can't allocate memory for options\n");
540                 return -1;
541         }
542         strcpy(options, mop.mo_orig_options);
543         rc = parse_options(&mop, options, &flags);
544         if (rc) {
545                 fprintf(stderr, "%s: can't parse options: %s\n",
546                         progname, options);
547                 return(EINVAL);
548         }
549
550         if (!mop.mo_force) {
551                 rc = check_mtab_entry(mop.mo_usource, mop.mo_source,
552                                       mop.mo_target, "lustre");
553                 if (rc && !(flags & MS_REMOUNT)) {
554                         fprintf(stderr, "%s: according to %s %s is "
555                                 "already mounted on %s\n", progname, MOUNTED,
556                                 mop.mo_usource, mop.mo_target);
557                         return(EEXIST);
558                 }
559                 if (!rc && (flags & MS_REMOUNT)) {
560                         fprintf(stderr, "%s: according to %s %s is "
561                                 "not already mounted on %s\n", progname, MOUNTED,
562                                 mop.mo_usource, mop.mo_target);
563                         return(ENOENT);
564                 }
565         }
566         if (flags & MS_REMOUNT)
567                 mop.mo_nomtab++;
568
569         rc = access(mop.mo_target, F_OK);
570         if (rc) {
571                 rc = errno;
572                 fprintf(stderr, "%s: %s inaccessible: %s\n", progname,
573                         mop.mo_target, strerror(errno));
574                 return rc;
575         }
576
577         if (!strstr(mop.mo_usource, ":/")) {
578                 rc = parse_ldd(mop.mo_source, &mop, options);
579                 if (rc)
580                         return rc;
581         }
582
583         /* In Linux 2.4, the target device doesn't get passed to any of our
584            functions.  So we'll stick it on the end of the options. */
585         append_option(options, "device=");
586         strcat(options, mop.mo_source);
587
588         if (verbose)
589                 printf("mounting device %s at %s, flags=%#x options=%s\n",
590                        mop.mo_source, mop.mo_target, flags, options);
591
592         if (!strstr(mop.mo_usource, ":/") &&
593             osd_tune_lustre(mop.mo_source, &mop)) {
594                 if (verbose)
595                         fprintf(stderr, "%s: unable to set tunables for %s"
596                                         " (may cause reduced IO performance)\n",
597                                         argv[0], mop.mo_source);
598         }
599
600         if (!mop.mo_fake) {
601                 /* flags and target get to lustre_get_sb, but not
602                    lustre_fill_super.  Lustre ignores the flags, but mount
603                    does not. */
604                 for (i = 0, rc = -EAGAIN; i <= mop.mo_retry && rc != 0; i++) {
605                         rc = mount(mop.mo_source, mop.mo_target, "lustre",
606                                    flags, (void *)options);
607                         if (rc) {
608                                 if (verbose) {
609                                         fprintf(stderr, "%s: mount %s at %s "
610                                                 "failed: %s retries left: "
611                                                 "%d\n", basename(progname),
612                                                 mop.mo_usource, mop.mo_target,
613                                                 strerror(errno),
614                                                 mop.mo_retry - i);
615                                 }
616
617                                 if (mop.mo_retry) {
618                                         sleep(1 << max((i/2), 5));
619                                 }
620                                 else {
621                                         rc = errno;
622                                 }
623                         }
624                 }
625         }
626
627         if (rc) {
628                 char *cli;
629
630                 rc = errno;
631
632                 cli = strrchr(mop.mo_usource, ':');
633                 if (cli && (strlen(cli) > 2))
634                         cli += 2;
635                 else
636                         cli = NULL;
637
638                 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
639                         mop.mo_usource, mop.mo_target, strerror(errno));
640                 if (errno == EBUSY)
641                         fprintf(stderr, "Is the backend filesystem mounted?\n"
642                                         "Check /etc/mtab and /proc/mounts\n");
643                 if (errno == ENODEV)
644                         fprintf(stderr, "Are the lustre modules loaded?\n"
645                                 "Check /etc/modprobe.conf and "
646                                 "/proc/filesystems\n");
647                 if (errno == ENOTBLK)
648                         fprintf(stderr, "Do you need -o loop?\n");
649                 if (errno == ENOMEDIUM)
650                         fprintf(stderr,
651                                 "This filesystem needs at least 1 OST\n");
652                 if (errno == ENOENT) {
653                         fprintf(stderr, "Is the MGS specification correct?\n");
654                         fprintf(stderr, "Is the filesystem name correct?\n");
655                         fprintf(stderr, "If upgrading, is the copied client log"
656                                 " valid? (see upgrade docs)\n");
657                 }
658                 if (errno == EALREADY)
659                         fprintf(stderr, "The target service is already running."
660                                 " (%s)\n", mop.mo_usource);
661                 if (errno == ENXIO)
662                         fprintf(stderr, "The target service failed to start "
663                                 "(bad config log?) (%s).  "
664                                 "See /var/log/messages.\n", mop.mo_usource);
665                 if (errno == EIO)
666                         fprintf(stderr, "Is the MGS running?\n");
667                 if (errno == EADDRINUSE)
668                         fprintf(stderr, "The target service's index is already "
669                                 "in use. (%s)\n", mop.mo_usource);
670                 if (errno == EINVAL) {
671                         fprintf(stderr, "This may have multiple causes.\n");
672                         if (cli)
673                                 fprintf(stderr, "Is '%s' the correct filesystem"
674                                         " name?\n", cli);
675                         fprintf(stderr, "Are the mount options correct?\n");
676                         fprintf(stderr, "Check the syslog for more info.\n");
677                 }
678
679                 /* May as well try to clean up loop devs */
680                 if (strncmp(mop.mo_usource, "/dev/loop", 9) == 0) {
681                         char cmd[256];
682                         int ret;
683                         sprintf(cmd, "/sbin/losetup -d %s", mop.mo_usource);
684                         if ((ret = system(cmd)) < 0)
685                                 rc = errno;
686                         else if (ret > 0)
687                                 rc = WEXITSTATUS(ret);
688                 }
689
690         } else if (!mop.mo_nomtab) {
691                 rc = update_mtab_entry(mop.mo_usource, mop.mo_target, "lustre",
692                                        mop.mo_orig_options, 0,0,0);
693
694                 /* change label from <fsname>:<index> to <fsname>-<index>
695                  * to indicate the device has been registered.
696                  * only if the label is supposed to be changed and
697                  * target service is supposed to start */
698                 if (mop.mo_ldd.ldd_flags & (LDD_F_VIRGIN | LDD_F_WRITECONF)) {
699                         if (mop.mo_nosvc == 0 )
700                                 (void) osd_label_lustre(&mop);
701                 }
702         }
703
704         free(options);
705         /* mo_usource should be freed, but we can rely on the kernel */
706         free(mop.mo_source);
707
708         osd_fini();
709
710         return rc;
711 }