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