Whamcloud - gitweb
LU-708 utils: convert_hostnames() should not change its arg
[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                 *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 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
274                char **mountopts)
275 {
276         static struct option long_opt[] = {
277                 {"iam-dir", 0, 0, 'a'},
278                 {"backfstype", 1, 0, 'b'},
279                 {"stripe-count-hint", 1, 0, 'c'},
280                 {"comment", 1, 0, 'u'},
281                 {"configdev", 1, 0, 'C'},
282                 {"device-size", 1, 0, 'd'},
283                 {"dryrun", 0, 0, 'n'},
284                 {"erase-params", 0, 0, 'e'},
285                 {"failnode", 1, 0, 'f'},
286                 {"failover", 1, 0, 'f'},
287                 {"mgs", 0, 0, 'G'},
288                 {"help", 0, 0, 'h'},
289                 {"index", 1, 0, 'i'},
290                 {"mkfsoptions", 1, 0, 'k'},
291                 {"mgsnode", 1, 0, 'm'},
292                 {"mgsnid", 1, 0, 'm'},
293                 {"mdt", 0, 0, 'M'},
294                 {"fsname",1, 0, 'L'},
295                 {"noformat", 0, 0, 'n'},
296                 {"nomgs", 0, 0, 'N'},
297                 {"mountfsoptions", 1, 0, 'o'},
298                 {"ost", 0, 0, 'O'},
299                 {"param", 1, 0, 'p'},
300                 {"print", 0, 0, 'n'},
301                 {"quiet", 0, 0, 'q'},
302                 {"reformat", 0, 0, 'r'},
303                 {"servicenode", 1, 0, 's'},
304                 {"verbose", 0, 0, 'v'},
305                 {"writeconf", 0, 0, 'w'},
306                 {"upgrade_to_18", 0, 0, 'U'},
307                 {"network", 1, 0, 't'},
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 'a': {
319                         if (IS_MDT(&mop->mo_ldd))
320                                 mop->mo_ldd.ldd_flags |= LDD_F_IAM_DIR;
321                         break;
322                 }
323                 case 'b': {
324                         int i = 0;
325                         while (i < LDD_MT_LAST) {
326                                 if (strcmp(optarg, mt_str(i)) == 0) {
327                                         mop->mo_ldd.ldd_mount_type = i;
328                                         break;
329                                 }
330                                 i++;
331                         }
332                         if (i == LDD_MT_LAST) {
333                                 fprintf(stderr, "%s: invalid backend filesystem"
334                                         " type %s\n", progname, optarg);
335                                 return 1;
336                         }
337                         break;
338                 }
339                 case 'c':
340                         if (IS_MDT(&mop->mo_ldd)) {
341                                 int stripe_count = atol(optarg);
342                                 if (stripe_count <= 0) {
343                                         fprintf(stderr, "%s: bad stripe count "
344                                                 "%d\n", progname, stripe_count);
345                                         return 1;
346                                 }
347                                 mop->mo_stripe_count = stripe_count;
348                         } else {
349                                 badopt(long_opt[longidx].name, "MDT");
350                                 return 1;
351                         }
352                         break;
353                 case 'C': /* Configdev */
354                         //FIXME
355                         printf("Configdev not implemented\n");
356                         return 1;
357                 case 'd':
358                         mop->mo_device_sz = atol(optarg);
359                         break;
360                 case 'e':
361                         mop->mo_ldd.ldd_params[0] = '\0';
362                         /* Must update the mgs logs */
363                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
364                         break;
365                 case 'f':
366                 case 's': {
367                         char *nids;
368
369                         if ((opt == 'f' && servicenode_set)
370                             || (opt == 's' && failnode_set)) {
371                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
372                                         progname, long_opt[longidx].name,
373                                         opt == 'f' ? "servicenode" : "failnode");
374                                 return 1;
375                         }
376
377                         nids = convert_hostnames(optarg);
378                         if (!nids)
379                                 return 1;
380                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
381                                        nids);
382                         free(nids);
383                         if (rc)
384                                 return rc;
385                         /* Must update the mgs logs */
386                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
387                         if (opt == 'f') {
388                                 failnode_set = 1;
389                         } else {
390                                 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
391                                 servicenode_set = 1;
392                         }
393                         mop->mo_flags |= MO_FAILOVER;
394                         break;
395                 }
396                 case 'G':
397                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
398                         break;
399                 case 'h':
400                         usage(stdout);
401                         return 1;
402                 case 'i':
403                         if (!(mop->mo_ldd.ldd_flags &
404                               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
405                                LDD_F_WRITECONF))) {
406                                 fprintf(stderr, "%s: cannot change the index of"
407                                         " a registered target\n", progname);
408                                 return 1;
409                         }
410                         if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
411                                 mop->mo_ldd.ldd_svindex = atol(optarg);
412                                 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
413                         } else {
414                                 badopt(long_opt[longidx].name, "MDT,OST");
415                                 return 1;
416                         }
417                         break;
418                 case 'k':
419                         strscpy(mop->mo_mkfsopts, optarg,
420                                 sizeof(mop->mo_mkfsopts));
421                         break;
422                 case 'L': {
423                         char *tmp;
424                         if (!(mop->mo_flags & MO_FORCEFORMAT) &&
425                             (!(mop->mo_ldd.ldd_flags &
426                                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
427                                 LDD_F_WRITECONF)))) {
428                                 fprintf(stderr, "%s: cannot change the name of"
429                                         " a registered target\n", progname);
430                                 return 1;
431                         }
432                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
433                                 fprintf(stderr, "%s: filesystem name must be "
434                                         "1-8 chars\n", progname);
435                                 return 1;
436                         }
437                         if ((tmp = strpbrk(optarg, "/:"))) {
438                                 fprintf(stderr, "%s: char '%c' not allowed in "
439                                         "filesystem name\n", progname, *tmp);
440                                 return 1;
441                         }
442                         strscpy(mop->mo_ldd.ldd_fsname, optarg,
443                                 sizeof(mop->mo_ldd.ldd_fsname));
444                         break;
445                 }
446                 case 'm': {
447                         char *nids = convert_hostnames(optarg);
448                         if (!nids)
449                                 return 1;
450                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
451                                        nids);
452                         free(nids);
453                         if (rc)
454                                 return rc;
455                         mop->mo_mgs_failnodes++;
456                         break;
457                 }
458                 case 'M':
459                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
460                         break;
461                 case 'n':
462                         print_only++;
463                         break;
464                 case 'N':
465                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
466                         break;
467                 case 'o':
468                         *mountopts = optarg;
469                         break;
470                 case 'O':
471                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
472                         break;
473                 case 'p':
474                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
475                         if (rc)
476                                 return rc;
477                         /* Must update the mgs logs */
478                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
479                         break;
480                 case 'q':
481                         verbose--;
482                         break;
483                 case 'r':
484                         mop->mo_flags |= MO_FORCEFORMAT;
485                         break;
486                 case 't':
487                         if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
488                                 badopt(long_opt[longidx].name, "MDT,OST");
489                                 return 1;
490                         }
491
492                         if (!optarg)
493                                 return 1;
494
495                         rc = add_param(mop->mo_ldd.ldd_params,
496                                        PARAM_NETWORK, optarg);
497                         if (rc != 0)
498                                 return rc;
499                         /* Must update the mgs logs */
500                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
501                         break;
502                 case 'u':
503                         strscpy(mop->mo_ldd.ldd_userdata, optarg,
504                                 sizeof(mop->mo_ldd.ldd_userdata));
505                         break;
506                 case 'v':
507                         verbose++;
508                         break;
509                 case 'w':
510                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
511                         break;
512                 case 'U':
513                         upgrade_to_18 = 1;
514                         break;
515                 default:
516                         if (opt != '?') {
517                                 fatal();
518                                 fprintf(stderr, "Unknown option '%c'\n", opt);
519                         }
520                         return EINVAL;
521                 }
522         }//while
523
524         if (optind == argc) {
525                 /* The user didn't specify device name */
526                 fatal();
527                 fprintf(stderr, "Not enough arguments - device name or "
528                         "pool/dataset name not specified.\n");
529                 return EINVAL;
530         } else {
531                 /*  The device or pool/filesystem name */
532                 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
533
534                 /* Followed by optional vdevs */
535                 if (optind < argc - 1)
536                         mop->mo_pool_vdevs = (char **) &argv[optind + 1];
537         }
538
539         return 0;
540 }
541
542 int main(int argc, char *const argv[])
543 {
544         struct mkfs_opts mop;
545         struct lustre_disk_data *ldd;
546         char *mountopts = NULL;
547         char always_mountopts[512] = "";
548         char default_mountopts[512] = "";
549         unsigned mount_type;
550         int ret = 0;
551
552         if ((progname = strrchr(argv[0], '/')) != NULL)
553                 progname++;
554         else
555                 progname = argv[0];
556
557         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
558                 usage(stderr);
559                 return(EINVAL);
560         }
561
562         memset(&mop, 0, sizeof(mop));
563         set_defaults(&mop);
564
565         /* device is last arg */
566         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
567
568 #ifdef TUNEFS
569         /* For tunefs, we must read in the old values before parsing any
570            new ones. */
571
572         /* Check whether the disk has already been formatted by mkfs.lustre */
573         ret = osd_is_lustre(mop.mo_device, &mount_type);
574         if (ret == 0) {
575                 fatal();
576                 fprintf(stderr, "Device %s has not been formatted with "
577                         "mkfs.lustre\n", mop.mo_device);
578                 ret = ENODEV;
579                 goto out;
580         }
581
582         ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
583         if (ret) {
584                 fatal();
585                 fprintf(stderr, "Failed to read previous Lustre data from %s "
586                         "(%d)\n", mop.mo_device, ret);
587                 goto out;
588         }
589         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
590             mop.mo_mgs_failnodes++;
591
592         if (verbose > 0)
593                 print_ldd("Read previous values", &(mop.mo_ldd));
594 #endif
595
596         ret = osd_init();
597         if (ret)
598                 return ret;
599
600         ret = parse_opts(argc, argv, &mop, &mountopts);
601         if (ret)
602                 goto out;
603
604         ldd = &mop.mo_ldd;
605
606         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
607                 fatal();
608                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
609                 ret = EINVAL;
610                 goto out;
611         }
612
613         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
614                 fatal();
615                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
616                 ret = EINVAL;
617                 goto out;
618         }
619
620         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
621             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
622                 fatal();
623                 fprintf(stderr, "Can't find the target index, "
624                         "specify with --index\n");
625                 ret = EINVAL;
626                 goto out;
627         }
628 #if 0
629         /*
630          * Comment out these 2 checks temporarily, since for multi-MDSes
631          * in single node only 1 mds node could have mgs service
632          */
633         if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
634                 verrprint("No management node specified, adding MGS to this "
635                           "MDT\n");
636                 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
637         }
638         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
639                 fatal();
640                 if (IS_MDT(ldd))
641                         fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
642                 else
643                         fprintf(stderr, "Must specify --mgsnode=\n");
644                 ret = EINVAL;
645                 goto out;
646         }
647 #endif
648
649         /* These are the permanent mount options (always included) */
650         ret = osd_prepare_lustre(&mop,
651                                  default_mountopts, sizeof(default_mountopts),
652                                  always_mountopts, sizeof(always_mountopts));
653         if (ret) {
654                 fatal();
655                 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
656                 goto out;
657         }
658
659         if (mountopts) {
660                 trim_mountfsoptions(mountopts);
661                 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
662                 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
663                         ret = EINVAL;
664                         goto out;
665                 }
666                 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
667         } else {
668 #ifdef TUNEFS
669                 if (ldd->ldd_mount_opts[0] == 0)
670                         /* use the defaults unless old opts exist */
671 #endif
672                 {
673                         sprintf(ldd->ldd_mount_opts, "%s%s",
674                                 always_mountopts, default_mountopts);
675                         trim_mountfsoptions(ldd->ldd_mount_opts);
676                 }
677         }
678
679         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
680                          ldd->ldd_fsname, ldd->ldd_svname);
681
682         if (verbose >= 0)
683                 print_ldd("Permanent disk data", ldd);
684
685         if (print_only) {
686                 printf("exiting before disk write.\n");
687                 goto out;
688         }
689
690         if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
691                 return(EEXIST);
692
693         /* Create the loopback file */
694         if (mop.mo_flags & MO_IS_LOOP) {
695                 ret = access(mop.mo_device, F_OK);
696                 if (ret)
697                         ret = errno;
698 #ifndef TUNEFS /* mkfs.lustre */
699                 /* Reformat the loopback file */
700                 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
701                         ret = loop_format(&mop);
702                         if (ret)
703                                 goto out;
704                 }
705 #endif
706                 if (ret == 0)
707                         ret = loop_setup(&mop);
708                 if (ret) {
709                         fatal();
710                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
711                                 mop.mo_device, strerror(ret));
712                         goto out;
713                 }
714         }
715
716 #ifndef TUNEFS /* mkfs.lustre */
717         /* Check whether the disk has already been formatted by mkfs.lustre */
718         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
719                 ret = osd_is_lustre(mop.mo_device, &mount_type);
720                 if (ret) {
721                         fatal();
722                         fprintf(stderr, "Device %s was previously formatted "
723                                 "for lustre. Use --reformat to reformat it, "
724                                 "or tunefs.lustre to modify.\n",
725                                 mop.mo_device);
726                         goto out;
727                 }
728         }
729
730         /* Format the backing filesystem */
731         ret = osd_make_lustre(&mop);
732         if (ret != 0) {
733                 fatal();
734                 fprintf(stderr, "mkfs failed %d\n", ret);
735                 goto out;
736         }
737 #endif
738
739         /* Write our config files */
740         ret = osd_write_ldd(&mop);
741         if (ret != 0) {
742                 fatal();
743                 fprintf(stderr, "failed to write local files\n");
744                 goto out;
745         }
746
747 out:
748         loop_cleanup(&mop);
749         osd_fini();
750
751         /* Fix any crazy return values from system() */
752         if (ret && ((ret & 255) == 0))
753                 return (1);
754         if (ret)
755                 verrprint("%s: exiting with %d (%s)\n",
756                           progname, ret, strerror(ret));
757         return (ret);
758 }