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