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