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