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