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