Whamcloud - gitweb
3d6baee55fcdf57b5a4f9fee36f6ce07f1a0d57b
[fs/lustre-release.git] / lustre / utils / mkfs_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, Intel Corporation.
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/mkfs_lustre.c
37  *
38  * Author: Nathan Rutman <nathan@clusterfs.com>
39 */
40
41 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
42
43 #if HAVE_CONFIG_H
44 #  include "config.h"
45 #endif /* HAVE_CONFIG_H */
46
47 #ifndef _GNU_SOURCE
48 #define _GNU_SOURCE
49 #endif
50 #include "mount_utils.h"
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55 #include <stdarg.h>
56 #include <mntent.h>
57
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <sys/mount.h>
61 #include <sys/utsname.h>
62
63 #include <string.h>
64 #include <getopt.h>
65 #include <limits.h>
66 #include <ctype.h>
67
68 #include <lustre_disk.h>
69 #include <lustre_param.h>
70 #include <lnet/lnetctl.h>
71 #include <lustre_ver.h>
72
73 #ifndef PATH_MAX
74 #define PATH_MAX 4096
75 #endif
76
77 char *progname;
78 int verbose = 1;
79 static int print_only = 0;
80 static int upgrade_to_18 = 0;
81
82 #ifdef HAVE_LDISKFS_OSD
83 #define FSLIST_LDISKFS "ldiskfs"
84 #define HAVE_FSLIST
85 #else
86  #define FSLIST_LDISKFS ""
87 #endif /* HAVE_LDISKFS_OSD */
88 #ifdef HAVE_ZFS_OSD
89  #ifdef HAVE_FSLIST
90    #define FSLIST_ZFS "|zfs"
91  #else
92   #define FSLIST_ZFS "zfs"
93   #define HAVE_FSLIST
94  #endif
95 #else
96  #define FSLIST_ZFS ""
97 #endif /* HAVE_ZFS_OSD */
98
99 #ifndef HAVE_FSLIST
100  #error "no backing OSD types (ldiskfs or ZFS) are configured"
101 #endif
102
103 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
104
105 void usage(FILE *out)
106 {
107         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
108         fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
109                 "--fsname=<filesystem name>\n"
110                 "\t--index=<target index> [options] <device>\n", progname);
111 #ifdef HAVE_ZFS_OSD
112         fprintf(out, "usage: %s <target type> --backfstype=zfs "
113                 "--fsname=<filesystem name> [options]\n"
114                 "\t<pool name>/<dataset name>\n"
115                 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
116                 progname);
117 #endif
118         fprintf(out,
119                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
120 #ifdef HAVE_ZFS_OSD
121                 "\t<pool name>: name of ZFS pool where target is created "
122                         "(e.g. tank)\n"
123                 "\t<dataset name>: name of new dataset, must be unique within "
124                         "pool (e.g. ost1)\n"
125                 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
126                         "cache, log)\n"
127 #endif
128                 "\n"
129                 "\ttarget types:\n"
130                 "\t\t--mgs: configuration management service\n"
131                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
132                 "\t\t--ost: object storage, mutually exclusive with mdt, mgs\n"
133                 "\toptions (in order of popularity):\n"
134                 "\t\t--index=#N: numerical target index (0..N)\n"
135                 "\t\t\trequired for all targets other than the MGS\n"
136                 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
137                 "\t\t\trequired for all targets other than MGS\n"
138                 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
139                 "\t\t\trequired for all targets other than MGS\n"
140                 "\t\t--mountfsoptions=<opts>: permanent mount options\n"
141                 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
142                 "\t\t\tmutually exclusive with --servicenode\n"
143                 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
144                 "\t\t\ttreat nodes as equal service node, mutually exclusive "
145                         "with --failnode\n"
146                 "\t\t--param <key>=<value>: set a permanent parameter\n"
147                 "\t\t\te.g. --param sys.timeout=40\n"
148                 "\t\t\t     --param lov.stripesize=2M\n"
149                 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
150 #ifndef TUNEFS
151                 "\t\t--backfstype=<fstype>: backing fs type (ext3, ldiskfs)\n"
152                 "\t\t--device-size=#N(KB): device size for loop devices\n"
153                 "\t\t--mkfsoptions=<opts>: format options\n"
154                 "\t\t--reformat: overwrite an existing disk\n"
155                 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
156 #else
157                 "\t\t--erase-params: erase all old parameter settings\n"
158                 "\t\t--nomgs: turn off MGS service on this MDT\n"
159                 "\t\t--writeconf: erase all config logs for this fs.\n"
160                 "\t\t--quota: enable space accounting on old 2.x device.\n"
161 #endif
162                 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
163                 "\t\t--dryrun: report what we would do; don't write to disk\n"
164                 "\t\t--verbose: e.g. show mkfs progress\n"
165                 "\t\t--quiet\n",
166                 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
167         return;
168 }
169
170 /* ==================== Lustre config functions =============*/
171
172 void print_ldd(char *str, struct lustre_disk_data *ldd)
173 {
174         printf("\n   %s:\n", str);
175         printf("Target:     %s\n", ldd->ldd_svname);
176         if (ldd->ldd_svindex == INDEX_UNASSIGNED)
177                 printf("Index:      unassigned\n");
178         else
179                 printf("Index:      %d\n", ldd->ldd_svindex);
180         if (ldd->ldd_uuid[0])
181                 printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
182         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
183         printf("Mount type: %s\n", MT_STR(ldd));
184         printf("Flags:      %#x\n", ldd->ldd_flags);
185         printf("              (%s%s%s%s%s%s%s%s%s)\n",
186                IS_MDT(ldd) ? "MDT ":"",
187                IS_OST(ldd) ? "OST ":"",
188                IS_MGS(ldd) ? "MGS ":"",
189                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
190                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time ":"",
191                ldd->ldd_flags & LDD_F_UPDATE     ? "update ":"",
192                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf ":"",
193                ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
194                ldd->ldd_flags & LDD_F_UPGRADE14  ? "upgrade1.4 ":"");
195         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
196         printf("Parameters:%s\n", ldd->ldd_params);
197         if (ldd->ldd_userdata[0])
198                 printf("Comment: %s\n", ldd->ldd_userdata);
199         printf("\n");
200 }
201
202 void set_defaults(struct mkfs_opts *mop)
203 {
204         mop->mo_ldd.ldd_magic = LDD_MAGIC;
205         mop->mo_ldd.ldd_config_ver = 1;
206         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
207 #ifdef HAVE_LDISKFS_OSD
208         mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
209 #else
210         mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
211 #endif
212         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
213         mop->mo_mgs_failnodes = 0;
214         mop->mo_stripe_count = 1;
215         mop->mo_pool_vdevs = NULL;
216 }
217
218 static inline void badopt(const char *opt, char *type)
219 {
220         fprintf(stderr, "%s: '--%s' only valid for %s\n",
221                 progname, opt, type);
222         usage(stderr);
223 }
224
225 /* from mount_lustre */
226 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
227 #define MAXNIDSTR 1024
228 static char *convert_hostnames(char *s1)
229 {
230         char *converted, *s2 = 0, *c, *end, sep;
231         int left = MAXNIDSTR;
232         lnet_nid_t nid;
233
234         converted = malloc(left);
235         if (converted == NULL) {
236                 return NULL;
237         }
238
239         end = s1 + strlen(s1);
240         c = converted;
241         while ((left > 0) && (s1 < end)) {
242                 s2 = strpbrk(s1, ",:");
243                 if (!s2)
244                         s2 = end;
245                 sep = *s2;
246                 *s2 = '\0';
247                 nid = libcfs_str2nid(s1);
248                 *s2 = sep;
249
250                 if (nid == LNET_NID_ANY) {
251                         fprintf(stderr, "%s: Can't parse NID '%s'\n",
252                                 progname, s1);
253                         free(converted);
254                         return NULL;
255                 }
256                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
257                             strlen("127.0.0.1")) == 0) {
258                         fprintf(stderr, "%s: The NID '%s' resolves to the "
259                                 "loopback address '%s'.  Lustre requires a "
260                                 "non-loopback address.\n",
261                                 progname, s1, libcfs_nid2str(nid));
262                         free(converted);
263                         return NULL;
264                 }
265
266                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
267                 left = converted + MAXNIDSTR - c;
268                 s1 = s2 + 1;
269         }
270         return converted;
271 }
272
273 static int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
274                char **mountopts)
275 {
276         static struct option long_opt[] = {
277                 {"backfstype", 1, 0, 'b'},
278                 {"stripe-count-hint", 1, 0, 'c'},
279                 {"comment", 1, 0, 'u'},
280                 {"configdev", 1, 0, 'C'},
281                 {"device-size", 1, 0, 'd'},
282                 {"dryrun", 0, 0, 'n'},
283                 {"erase-params", 0, 0, 'e'},
284                 {"failnode", 1, 0, 'f'},
285                 {"failover", 1, 0, 'f'},
286                 {"mgs", 0, 0, 'G'},
287                 {"help", 0, 0, 'h'},
288                 {"index", 1, 0, 'i'},
289                 {"mkfsoptions", 1, 0, 'k'},
290                 {"mgsnode", 1, 0, 'm'},
291                 {"mgsnid", 1, 0, 'm'},
292                 {"mdt", 0, 0, 'M'},
293                 {"fsname",1, 0, 'L'},
294                 {"noformat", 0, 0, 'n'},
295                 {"nomgs", 0, 0, 'N'},
296                 {"mountfsoptions", 1, 0, 'o'},
297                 {"ost", 0, 0, 'O'},
298                 {"param", 1, 0, 'p'},
299                 {"print", 0, 0, 'n'},
300                 {"quiet", 0, 0, 'q'},
301                 {"reformat", 0, 0, 'r'},
302                 {"servicenode", 1, 0, 's'},
303                 {"verbose", 0, 0, 'v'},
304                 {"writeconf", 0, 0, 'w'},
305                 {"upgrade_to_18", 0, 0, 'U'},
306                 {"network", 1, 0, 't'},
307                 {"quota", 0, 0, 'Q'},
308                 {0, 0, 0, 0}
309         };
310         char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrs:t:Uu:vw";
311         int opt;
312         int rc, longidx;
313         int failnode_set = 0, servicenode_set = 0;
314
315         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
316                EOF) {
317                 switch (opt) {
318                 case 'b': {
319                         int i = 0;
320                         while (i < LDD_MT_LAST) {
321                                 if (strcmp(optarg, mt_str(i)) == 0) {
322                                         mop->mo_ldd.ldd_mount_type = i;
323                                         break;
324                                 }
325                                 i++;
326                         }
327                         if (i == LDD_MT_LAST) {
328                                 fprintf(stderr, "%s: invalid backend filesystem"
329                                         " type %s\n", progname, optarg);
330                                 return 1;
331                         }
332                         break;
333                 }
334                 case 'c':
335                         if (IS_MDT(&mop->mo_ldd)) {
336                                 int stripe_count = atol(optarg);
337                                 if (stripe_count <= 0) {
338                                         fprintf(stderr, "%s: bad stripe count "
339                                                 "%d\n", progname, stripe_count);
340                                         return 1;
341                                 }
342                                 mop->mo_stripe_count = stripe_count;
343                         } else {
344                                 badopt(long_opt[longidx].name, "MDT");
345                                 return 1;
346                         }
347                         break;
348                 case 'C': /* Configdev */
349                         //FIXME
350                         printf("Configdev not implemented\n");
351                         return 1;
352                 case 'd':
353                         mop->mo_device_sz = atol(optarg);
354                         break;
355                 case 'e':
356                         mop->mo_ldd.ldd_params[0] = '\0';
357                         /* Must update the mgs logs */
358                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
359                         break;
360                 case 'f':
361                 case 's': {
362                         char *nids;
363
364                         if ((opt == 'f' && servicenode_set)
365                             || (opt == 's' && failnode_set)) {
366                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
367                                         progname, long_opt[longidx].name,
368                                         opt == 'f' ? "servicenode" : "failnode");
369                                 return 1;
370                         }
371
372                         nids = convert_hostnames(optarg);
373                         if (!nids)
374                                 return 1;
375                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
376                                        nids);
377                         free(nids);
378                         if (rc)
379                                 return rc;
380                         /* Must update the mgs logs */
381                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
382                         if (opt == 'f') {
383                                 failnode_set = 1;
384                         } else {
385                                 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
386                                 servicenode_set = 1;
387                         }
388                         mop->mo_flags |= MO_FAILOVER;
389                         break;
390                 }
391                 case 'G':
392                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
393                         break;
394                 case 'h':
395                         usage(stdout);
396                         return 1;
397                 case 'i':
398                         if (!(mop->mo_ldd.ldd_flags &
399                               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
400                                LDD_F_WRITECONF))) {
401                                 fprintf(stderr, "%s: cannot change the index of"
402                                         " a registered target\n", progname);
403                                 return 1;
404                         }
405                         /* LU-2374: check whether it is OST/MDT later */
406                         mop->mo_ldd.ldd_svindex = atol(optarg);
407                         mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
408                         break;
409                 case 'k':
410                         strscpy(mop->mo_mkfsopts, optarg,
411                                 sizeof(mop->mo_mkfsopts));
412                         break;
413                 case 'L': {
414                         char *tmp;
415                         if (!(mop->mo_flags & MO_FORCEFORMAT) &&
416                             (!(mop->mo_ldd.ldd_flags &
417                                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
418                                 LDD_F_WRITECONF)))) {
419                                 fprintf(stderr, "%s: cannot change the name of"
420                                         " a registered target\n", progname);
421                                 return 1;
422                         }
423                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
424                                 fprintf(stderr, "%s: filesystem name must be "
425                                         "1-8 chars\n", progname);
426                                 return 1;
427                         }
428                         if ((tmp = strpbrk(optarg, "/:"))) {
429                                 fprintf(stderr, "%s: char '%c' not allowed in "
430                                         "filesystem name\n", progname, *tmp);
431                                 return 1;
432                         }
433                         strscpy(mop->mo_ldd.ldd_fsname, optarg,
434                                 sizeof(mop->mo_ldd.ldd_fsname));
435                         break;
436                 }
437                 case 'm': {
438                         char *nids = convert_hostnames(optarg);
439                         if (!nids)
440                                 return 1;
441                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
442                                        nids);
443                         free(nids);
444                         if (rc)
445                                 return rc;
446                         mop->mo_mgs_failnodes++;
447                         break;
448                 }
449                 case 'M':
450                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
451                         break;
452                 case 'n':
453                         print_only++;
454                         break;
455                 case 'N':
456                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
457                         break;
458                 case 'o':
459                         *mountopts = optarg;
460                         break;
461                 case 'O':
462                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
463                         break;
464                 case 'p':
465                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
466                         if (rc)
467                                 return rc;
468                         /* Must update the mgs logs */
469                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
470                         break;
471                 case 'q':
472                         verbose--;
473                         break;
474                 case 'r':
475                         mop->mo_flags |= MO_FORCEFORMAT;
476                         break;
477                 case 't':
478                         if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
479                                 badopt(long_opt[longidx].name, "MDT,OST");
480                                 return 1;
481                         }
482
483                         if (!optarg)
484                                 return 1;
485
486                         rc = add_param(mop->mo_ldd.ldd_params,
487                                        PARAM_NETWORK, optarg);
488                         if (rc != 0)
489                                 return rc;
490                         /* Must update the mgs logs */
491                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
492                         break;
493                 case 'u':
494                         strscpy(mop->mo_ldd.ldd_userdata, optarg,
495                                 sizeof(mop->mo_ldd.ldd_userdata));
496                         break;
497                 case 'v':
498                         verbose++;
499                         break;
500                 case 'w':
501                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
502                         break;
503                 case 'U':
504                         upgrade_to_18 = 1;
505                         break;
506                 case 'Q':
507                         mop->mo_flags |= MO_QUOTA;
508                         break;
509                 default:
510                         if (opt != '?') {
511                                 fatal();
512                                 fprintf(stderr, "Unknown option '%c'\n", opt);
513                         }
514                         return EINVAL;
515                 }
516         }//while
517
518         if (optind == argc) {
519                 /* The user didn't specify device name */
520                 fatal();
521                 fprintf(stderr, "Not enough arguments - device name or "
522                         "pool/dataset name not specified.\n");
523                 return EINVAL;
524         } else {
525                 /* Followed by optional vdevs */
526                 if (optind < argc - 1)
527                         mop->mo_pool_vdevs = (char **) &argv[optind + 1];
528         }
529
530         return 0;
531 }
532
533 int main(int argc, char *const argv[])
534 {
535         struct mkfs_opts mop;
536         struct lustre_disk_data *ldd;
537         char *mountopts = NULL;
538         char always_mountopts[512] = "";
539         char default_mountopts[512] = "";
540         unsigned mount_type;
541         int ret = 0;
542
543         if ((progname = strrchr(argv[0], '/')) != NULL)
544                 progname++;
545         else
546                 progname = argv[0];
547
548         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
549                 usage(stderr);
550                 return(EINVAL);
551         }
552
553         memset(&mop, 0, sizeof(mop));
554         set_defaults(&mop);
555
556         /* Try to get the real path to the device */
557         ret = get_realpath(argv[argc - 1], &mop.mo_device);
558         if (ret != 0)
559                 return ret;
560
561         ret = osd_init();
562         if (ret)
563                 goto out;
564
565 #ifdef TUNEFS
566         /* For tunefs, we must read in the old values before parsing any
567            new ones. */
568
569         /* Check whether the disk has already been formatted by mkfs.lustre */
570         ret = osd_is_lustre(mop.mo_device, &mount_type);
571         if (ret == 0) {
572                 fatal();
573                 fprintf(stderr, "Device %s has not been formatted with "
574                         "mkfs.lustre\n", mop.mo_device);
575                 ret = ENODEV;
576                 goto out;
577         }
578         mop.mo_ldd.ldd_mount_type = mount_type;
579
580         ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
581         if (ret) {
582                 fatal();
583                 fprintf(stderr, "Failed to read previous Lustre data from %s "
584                         "(%d)\n", mop.mo_device, ret);
585                 goto out;
586         }
587         mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
588
589         /* svname of the form lustre:OST1234 means never registered */
590         ret = strlen(mop.mo_ldd.ldd_svname);
591         if (mop.mo_ldd.ldd_svname[ret - 8] == ':') {
592                 mop.mo_ldd.ldd_svname[ret - 8] = '-';
593                 mop.mo_ldd.ldd_flags |= LDD_F_VIRGIN;
594         } else if (mop.mo_ldd.ldd_svname[ret - 8] == '=') {
595                 mop.mo_ldd.ldd_svname[ret - 8] = '-';
596                 mop.mo_ldd.ldd_flags |= LDD_F_WRITECONF;
597         }
598
599         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
600             mop.mo_mgs_failnodes++;
601
602         if (verbose > 0)
603                 print_ldd("Read previous values", &(mop.mo_ldd));
604 #endif
605
606         ret = parse_opts(argc, argv, &mop, &mountopts);
607         if (ret)
608                 goto out;
609
610         ldd = &mop.mo_ldd;
611
612         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
613                 fatal();
614                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
615                 ret = EINVAL;
616                 goto out;
617         }
618
619         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
620                 fatal();
621                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
622                 ret = EINVAL;
623                 goto out;
624         }
625
626         /* Stand alone MGS doesn't need a index */
627         if (!IS_MDT(ldd) && IS_MGS(ldd)) {
628 #ifndef TUNEFS /* mkfs.lustre */
629                 /* But if --index was specified flag an error */
630                 if (!(mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)) {
631                         badopt("index", "MDT,OST");
632                         goto out;
633                 }
634 #endif
635                 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
636         }
637
638         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
639             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
640                 fatal();
641                 fprintf(stderr, "Can't find the target index, "
642                         "specify with --index\n");
643                 ret = EINVAL;
644                 goto out;
645         }
646
647         if (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)
648                 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
649                         "target index must be specified with --index\n",
650                         mop.mo_device);
651
652         /* If no index is supplied for MDT by default set index to zero */
653         if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
654                 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
655                 mop.mo_ldd.ldd_svindex = 0;
656         }
657
658 #if 0
659         /*
660          * Comment out these 2 checks temporarily, since for multi-MDSes
661          * in single node only 1 mds node could have mgs service
662          */
663         if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
664                 verrprint("No management node specified, adding MGS to this "
665                           "MDT\n");
666                 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
667         }
668         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
669                 fatal();
670                 if (IS_MDT(ldd))
671                         fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
672                 else
673                         fprintf(stderr, "Must specify --mgsnode=\n");
674                 ret = EINVAL;
675                 goto out;
676         }
677 #endif
678         if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
679                 fatal();
680                 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
681                 ret = EINVAL;
682                 goto out;
683         }
684
685         /* These are the permanent mount options (always included) */
686         ret = osd_prepare_lustre(&mop,
687                                  default_mountopts, sizeof(default_mountopts),
688                                  always_mountopts, sizeof(always_mountopts));
689         if (ret) {
690                 fatal();
691                 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
692                 goto out;
693         }
694
695         if (mountopts) {
696                 trim_mountfsoptions(mountopts);
697                 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
698                 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
699                         ret = EINVAL;
700                         goto out;
701                 }
702                 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
703         } else {
704 #ifdef TUNEFS
705                 if (ldd->ldd_mount_opts[0] == 0)
706                         /* use the defaults unless old opts exist */
707 #endif
708                 {
709                         sprintf(ldd->ldd_mount_opts, "%s%s",
710                                 always_mountopts, default_mountopts);
711                         trim_mountfsoptions(ldd->ldd_mount_opts);
712                 }
713         }
714
715         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
716                          ldd->ldd_fsname, ldd->ldd_svname);
717
718         if (verbose >= 0)
719                 print_ldd("Permanent disk data", ldd);
720
721         if (print_only) {
722                 printf("exiting before disk write.\n");
723                 goto out;
724         }
725
726         ret = check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL);
727         if (ret != 0) {
728                 fprintf(stderr, "%s: %s is already mounted\n",
729                         progname, mop.mo_device);
730                 goto out;
731         }
732
733         /* Create the loopback file */
734         if (mop.mo_flags & MO_IS_LOOP) {
735                 ret = access(mop.mo_device, F_OK);
736                 if (ret)
737                         ret = errno;
738 #ifndef TUNEFS /* mkfs.lustre */
739                 /* Reformat the loopback file */
740                 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
741                         ret = loop_format(&mop);
742                         if (ret)
743                                 goto out;
744                 }
745 #endif
746                 if (ret == 0)
747                         ret = loop_setup(&mop);
748                 if (ret) {
749                         fatal();
750                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
751                                 mop.mo_device, strerror(ret));
752                         goto out;
753                 }
754         }
755
756 #ifndef TUNEFS /* mkfs.lustre */
757         /* Check whether the disk has already been formatted by mkfs.lustre */
758         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
759                 ret = osd_is_lustre(mop.mo_device, &mount_type);
760                 if (ret) {
761                         fatal();
762                         fprintf(stderr, "Device %s was previously formatted "
763                                 "for lustre. Use --reformat to reformat it, "
764                                 "or tunefs.lustre to modify.\n",
765                                 mop.mo_device);
766                         goto out;
767                 }
768         }
769
770         /* Format the backing filesystem */
771         ret = osd_make_lustre(&mop);
772         if (ret != 0) {
773                 fatal();
774                 fprintf(stderr, "mkfs failed %d\n", ret);
775                 goto out;
776         }
777 #else
778         /* update svname with '=' to refresh config */
779         if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
780                 struct mount_opts opts;
781                 opts.mo_ldd = mop.mo_ldd;
782                 opts.mo_source = mop.mo_device;
783                 (void) osd_label_lustre(&opts);
784         }
785
786         /* Enable quota accounting */
787         if (mop.mo_flags & MO_QUOTA) {
788                 ret = osd_enable_quota(&mop);
789                 goto out;
790         }
791
792 #endif
793
794         /* Write our config files */
795         ret = osd_write_ldd(&mop);
796         if (ret != 0) {
797                 fatal();
798                 fprintf(stderr, "failed to write local files\n");
799                 goto out;
800         }
801
802 out:
803         loop_cleanup(&mop);
804         free(mop.mo_device);
805         osd_fini();
806
807         /* Fix any crazy return values from system() */
808         if (ret && ((ret & 255) == 0))
809                 return (1);
810         if (ret)
811                 verrprint("%s: exiting with %d (%s)\n",
812                           progname, ret, strerror(ret));
813         return (ret);
814 }