Whamcloud - gitweb
ab813fa6dc3e8442a4488adb2167f0e93e9a1db6
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/utils/mkfs_lustre.c
33  *
34  * Author: Nathan Rutman <nathan@clusterfs.com>
35 */
36
37 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
38
39 #if HAVE_CONFIG_H
40 #  include "config.h"
41 #endif /* HAVE_CONFIG_H */
42
43 #ifndef _GNU_SOURCE
44 #define _GNU_SOURCE
45 #endif
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <stdarg.h>
51 #include <mntent.h>
52 #include <stdbool.h>
53
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <sys/mount.h>
57 #include <sys/utsname.h>
58
59 #include <string.h>
60 #include <getopt.h>
61 #include <limits.h>
62 #include <ctype.h>
63 #include <lnet/nidstr.h>
64 #include <lustre_disk.h>
65 #include <lustre_param.h>
66 #include <lnet/lnetctl.h>
67 #include <lustre_ver.h>
68
69 #include "mount_utils.h"
70
71 char *progname;
72 int verbose = 1;
73 int version;
74 static int print_only = 0;
75
76 #ifdef HAVE_LDISKFS_OSD
77 #define FSLIST_LDISKFS "ldiskfs"
78 #define HAVE_FSLIST
79 #else
80  #define FSLIST_LDISKFS ""
81 #endif /* HAVE_LDISKFS_OSD */
82 #ifdef HAVE_ZFS_OSD
83  #ifdef HAVE_FSLIST
84    #define FSLIST_ZFS "|zfs"
85  #else
86   #define FSLIST_ZFS "zfs"
87   #define HAVE_FSLIST
88  #endif
89 #else
90  #define FSLIST_ZFS ""
91 #endif /* HAVE_ZFS_OSD */
92
93 #ifndef HAVE_FSLIST
94  #error "no backing OSD types (ldiskfs or ZFS) are configured"
95 #endif
96
97 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
98
99 void usage(FILE *out)
100 {
101         fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
102                 "--fsname=<filesystem name>\n"
103                 "\t--index=<target index> [options] <device>\n", progname);
104 #ifdef HAVE_ZFS_OSD
105         fprintf(out, "usage: %s <target type> --backfstype=zfs "
106                 "--fsname=<filesystem name> [options]\n"
107                 "\t<pool name>/<dataset name>\n"
108                 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
109                 progname);
110 #endif
111         fprintf(out,
112                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
113 #ifdef HAVE_ZFS_OSD
114                 "\t<pool name>: name of ZFS pool where target is created "
115                         "(e.g. tank)\n"
116                 "\t<dataset name>: name of new dataset, must be unique within "
117                         "pool (e.g. ost1)\n"
118                 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
119                         "cache, log)\n"
120 #endif
121                 "\n"
122                 "\ttarget types:\n"
123                 "\t\t--mgs: configuration management service\n"
124                 "\t\t--nomgs: turn off MGS service on this MDT\n"
125 #ifndef TUNEFS
126                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
127                 "\t\t--ost: object storage, mutually exclusive with mdt, mgs\n"
128 #endif
129                 "\toptions (in order of popularity):\n"
130                 "\t\t--index=#N: numerical target index (0..N)\n"
131                 "\t\t\trequired for all targets other than the MGS,\n"
132                 "\t\t\ttarget index may either be a decimal number or\n"
133                 "\t\t\thexadecimal number starting with '0x'\n"
134                 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
135                 "\t\t\trequired for all targets other than MGS\n"
136                 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
137                 "\t\t\trequired for all targets other than MGS\n"
138                 "\t\t--mountfsoptions=<opts>: permanent Lustre mount options\n"
139                 "\t\t--backfs-mount-opts=<opts>: backing fs mount options\n"
140                 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
141                 "\t\t\tmutually exclusive with --servicenode\n"
142                 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
143                 "\t\t\ttreat nodes as equal service node, mutually exclusive "
144                         "with --failnode\n"
145                 "\t\t--param <key>=<value>: set a permanent parameter\n"
146                 "\t\t\te.g. --param sys.timeout=40\n"
147                 "\t\t\t     --param lov.stripesize=2M\n"
148                 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
149 #ifndef TUNEFS
150                 "\t\t--backfstype=<fstype>: backing fs type (ldiskfs, zfs)\n"
151                 "\t\t--device-size=#N(KB): device size for loop devices\n"
152                 "\t\t--mkfsoptions=<opts>: format options\n"
153                 "\t\t--reformat: overwrite an existing disk\n"
154                 "\t\t--replace: replace an old target with the same index\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--writeconf: erase all config logs for this fs.\n"
159                 "\t\t--quota: enable space accounting on old 2.x device.\n"
160                 "\t\t--rename: rename the filesystem name\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--force-nohostid: Ignore hostid requirement for ZFS "
166                         "import\n"
167                 "\t\t-V|--version: output build version of the utility and\n"
168                 "\t\t\texit\n"
169                 "\t\t--quiet\n",
170                 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
171         return;
172 }
173
174 /* ==================== Lustre config functions =============*/
175
176 void print_ldd(char *str, struct lustre_disk_data *ldd)
177 {
178         printf("\n   %s:\n", str);
179         printf("Target:     %s\n", ldd->ldd_svname);
180         if (ldd->ldd_svindex == INDEX_UNASSIGNED)
181                 printf("Index:      unassigned\n");
182         else
183                 printf("Index:      %d\n", ldd->ldd_svindex);
184         if (ldd->ldd_uuid[0])
185                 printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
186         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
187         printf("Mount type: %s\n", MT_STR(ldd));
188         printf("Flags:      %#x\n", ldd->ldd_flags);
189         printf("              (%s%s%s%s%s%s%s%s%s)\n",
190                IS_MDT(ldd) ? "MDT ":"",
191                IS_OST(ldd) ? "OST ":"",
192                IS_MGS(ldd) ? "MGS ":"",
193                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
194                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time ":"",
195                ldd->ldd_flags & LDD_F_UPDATE     ? "update ":"",
196                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf ":"",
197                ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
198                ldd->ldd_flags & LDD_F_UPGRADE14  ? "upgrade1.4 ":"");
199         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
200         printf("Parameters:%s\n", ldd->ldd_params);
201         if (ldd->ldd_userdata[0])
202                 printf("Comment: %s\n", ldd->ldd_userdata);
203         printf("\n");
204 }
205
206 void set_defaults(struct mkfs_opts *mop)
207 {
208         mop->mo_ldd.ldd_magic = LDD_MAGIC;
209         mop->mo_ldd.ldd_config_ver = 1;
210         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
211 #ifdef HAVE_LDISKFS_OSD
212         mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
213 #else
214         mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
215 #endif
216         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
217         mop->mo_mgs_failnodes = 0;
218         mop->mo_stripe_count = 1;
219         mop->mo_pool_vdevs = NULL;
220 }
221
222 static inline void badopt(const char *opt, char *type)
223 {
224         fprintf(stderr, "%s: '--%s' only valid for %s\n",
225                 progname, opt, type);
226         usage(stderr);
227 }
228
229 /* from mount_lustre */
230 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
231 #define MAXNIDSTR 1024
232 static char *convert_hostnames(char *s1)
233 {
234         char *converted, *s2 = 0, *c, *end, sep;
235         int left = MAXNIDSTR;
236         lnet_nid_t nid;
237
238         converted = malloc(left);
239         if (converted == NULL) {
240                 return NULL;
241         }
242
243         end = s1 + strlen(s1);
244         c = converted;
245         while ((left > 0) && (s1 < end)) {
246                 s2 = strpbrk(s1, ",:");
247                 if (!s2)
248                         s2 = end;
249                 sep = *s2;
250                 *s2 = '\0';
251                 nid = libcfs_str2nid(s1);
252                 *s2 = sep;
253
254                 if (nid == LNET_NID_ANY) {
255                         fprintf(stderr, "%s: Cannot resolve hostname '%s'.\n",
256                                 progname, s1);
257                         free(converted);
258                         return NULL;
259                 }
260                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
261                             strlen("127.0.0.1")) == 0) {
262                         fprintf(stderr, "%s: The NID '%s' resolves to the "
263                                 "loopback address '%s'.  Lustre requires a "
264                                 "non-loopback address.\n",
265                                 progname, s1, libcfs_nid2str(nid));
266                         free(converted);
267                         return NULL;
268                 }
269
270                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
271                 left = converted + MAXNIDSTR - c;
272                 s1 = s2 + 1;
273         }
274         return converted;
275 }
276
277 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
278                char **mountopts, char *old_fsname)
279 {
280         static struct option long_opt[] = {
281                 { "backfs-mount-opts",  required_argument,      NULL, 'B' },
282                 { "failnode",           required_argument,      NULL, 'f' },
283                 { "failover",           required_argument,      NULL, 'f' },
284                 { "mgs",                no_argument,            NULL, 'G' },
285                 { "help",               no_argument,            NULL, 'h' },
286                 { "index",              required_argument,      NULL, 'i' },
287                 { "fsname",             required_argument,      NULL, 'L' },
288                 { "mgsnode",            required_argument,      NULL, 'm' },
289                 { "mgsnid",             required_argument,      NULL, 'm' },
290                 { "dryrun",             no_argument,            NULL, 'n' },
291                 { "nomgs",              no_argument,            NULL, 'N' },
292                 { "mountfsoptions",     required_argument,      NULL, 'o' },
293                 { "param",              required_argument,      NULL, 'p' },
294                 { "quiet",              no_argument,            NULL, 'q' },
295                 { "servicenode",        required_argument,      NULL, 's' },
296                 { "network",            required_argument,      NULL, 't' },
297                 { "comment",            required_argument,      NULL, 'u' },
298                 { "force-nohostid",     no_argument,            NULL, 'U' },
299                 { "verbose",            no_argument,            NULL, 'v' },
300                 { "version",            no_argument,            NULL, 'V' },
301 #ifndef TUNEFS
302                 { "backfstype",         required_argument,      NULL, 'b' },
303                 { "stripe-count-hint",  required_argument,      NULL, 'c' },
304                 { "device-size",        required_argument,      NULL, 'd' },
305                 { "mkfsoptions",        required_argument,      NULL, 'k' },
306                 { "mdt",                no_argument,            NULL, 'M' },
307                 { "ost",                no_argument,            NULL, 'O' },
308                 { "reformat",           no_argument,            NULL, 'r' },
309                 { "replace",            no_argument,            NULL, 'R' },
310 #else
311                 { "erase-params",       no_argument,            NULL, 'e' },
312                 { "quota",              no_argument,            NULL, 'Q' },
313                 { "rename",             optional_argument,      NULL, 'R' },
314                 { "writeconf",          no_argument,            NULL, 'w' },
315 #endif
316                 { 0,                    0,                      NULL,  0  }
317         };
318         char *optstring = "B:f:Ghi:L:m:nNo:p:qs:t:u:vV"
319 #ifndef TUNEFS
320                           "b:c:d:k:MOrR";
321 #else
322                           "eQR::w";
323 #endif
324         struct lustre_disk_data *ldd = &mop->mo_ldd;
325         char new_fsname[16] = { 0 };
326         int opt;
327         int rc, longidx;
328         int failnode_set = 0, servicenode_set = 0;
329         int replace = 0;
330         bool index_option = false;
331
332         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
333                EOF) {
334                 switch (opt) {
335                 case 'B':
336                         mop->mo_mountopts = optarg;
337                         break;
338                 case 'f':
339                 case 's': {
340                         char *nids;
341
342                         if ((opt == 'f' && servicenode_set) ||
343                             (opt == 's' && failnode_set)) {
344                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
345                                         progname, long_opt[longidx].name,
346                                         opt == 'f' ? "servicenode" :
347                                         "failnode");
348                                 return 1;
349                         }
350
351                         nids = convert_hostnames(optarg);
352                         if (nids == NULL)
353                                 return 1;
354
355                         rc = append_param(ldd->ldd_params, PARAM_FAILNODE,
356                                           nids, ':');
357                         free(nids);
358                         if (rc != 0)
359                                 return rc;
360
361                         /* Must update the mgs logs */
362                         ldd->ldd_flags |= LDD_F_UPDATE;
363                         if (opt == 'f') {
364                                 ldd->ldd_flags &= ~LDD_F_NO_PRIMNODE;
365                                 failnode_set = 1;
366                         } else {
367                                 ldd->ldd_flags |= LDD_F_NO_PRIMNODE;
368                                 servicenode_set = 1;
369                         }
370                         mop->mo_flags |= MO_FAILOVER;
371                         break;
372                 }
373                 case 'G':
374                         ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
375                         break;
376                 case 'h':
377                         usage(stdout);
378                         return 1;
379                 case 'i': {
380                         char *endptr = NULL;
381                         int base;
382                         index_option = true;
383                         /* LU-2374: check whether it is OST/MDT later */
384                         base = (strlen(optarg) > 1 &&
385                                 !strncmp(optarg, "0x", 2)) ? 16 : 10;
386                         /* Allowed input are base 16 and base 10 numbers only */
387                         mop->mo_ldd.ldd_svindex = strtoul(optarg,
388                                                           &endptr, base);
389                         if (*endptr != '\0') {
390                                 fprintf(stderr, "%s: wrong index %s. "
391                                         "Target index must be decimal or "
392                                         "hexadecimal.\n",
393                                         progname, optarg);
394                                 return 1;
395                         }
396                         if (ldd->ldd_svindex >= INDEX_UNASSIGNED) {
397                                 fprintf(stderr, "%s: wrong index %u. "
398                                         "Target index must be less than %u.\n",
399                                         progname, ldd->ldd_svindex,
400                                         INDEX_UNASSIGNED);
401                                 return 1;
402                         }
403
404                         ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
405                         break;
406                 }
407                 case 'L': {
408                         rc = lustre_is_fsname_valid(optarg, 1,
409                                                     LUSTRE_MAXFSNAME);
410                         if (rc < 0) {
411                                 fprintf(stderr, "%s: filesystem name must be "
412                                         "1-%d chars\n", progname,
413                                         LUSTRE_MAXFSNAME);
414                                 return 1;
415                         } else if (rc > 0) {
416                                 fprintf(stderr, "%s: char '%c' not allowed in "
417                                         "filesystem name\n", progname, rc);
418                                 return 1;
419                         }
420
421                         strscpy(new_fsname, optarg, sizeof(new_fsname));
422                         break;
423                 }
424                 case 'm': {
425                         char *nids = convert_hostnames(optarg);
426
427                         if (nids == NULL)
428                                 return 1;
429
430                         rc = append_param(ldd->ldd_params, PARAM_MGSNODE,
431                                           nids, ':');
432                         free(nids);
433                         if (rc != 0)
434                                 return rc;
435
436                         mop->mo_mgs_failnodes++;
437                         break;
438                 }
439                 case 'n':
440                         print_only++;
441                         break;
442                 case 'N':
443                         ldd->ldd_flags &= ~LDD_F_SV_TYPE_MGS;
444                         break;
445                 case 'o':
446                         *mountopts = optarg;
447                         break;
448                 case 'p':
449                         rc = add_param(ldd->ldd_params, NULL, optarg);
450                         if (rc != 0)
451                                 return rc;
452
453                         /* Must update the mgs logs */
454                         ldd->ldd_flags |= LDD_F_UPDATE;
455                         break;
456                 case 'q':
457                         verbose--;
458                         break;
459                 case 't':
460                         if (!IS_MDT(ldd) && !IS_OST(ldd)) {
461                                 badopt(long_opt[longidx].name, "MDT,OST");
462                                 return 1;
463                         }
464
465                         if (optarg == NULL)
466                                 return 1;
467
468                         rc = add_param(ldd->ldd_params, PARAM_NETWORK, optarg);
469                         if (rc != 0)
470                                 return rc;
471
472                         /* Must update the mgs logs */
473                         ldd->ldd_flags |= LDD_F_UPDATE;
474                         break;
475                 case 'u':
476                         strscpy(ldd->ldd_userdata, optarg,
477                                 sizeof(ldd->ldd_userdata));
478                         break;
479                 case 'U':
480                         mop->mo_flags |= MO_NOHOSTID_CHECK;
481                         break;
482                 case 'v':
483                         verbose++;
484                         break;
485                 case 'V':
486                         ++version;
487                         fprintf(stdout, "%s %s\n", progname,
488                                 LUSTRE_VERSION_STRING);
489                         return 0;
490 #ifndef TUNEFS
491                 case 'b': {
492                         int i = 0;
493
494                         do {
495                                 if (strcmp(optarg, mt_str(i)) == 0) {
496                                         ldd->ldd_mount_type = i;
497                                         break;
498                                 }
499                         } while (++i < LDD_MT_LAST);
500
501                         if (i == LDD_MT_LAST) {
502                                 fprintf(stderr, "%s: invalid backend filesystem"
503                                         " type %s\n", progname, optarg);
504                                 return 1;
505                         }
506                         break;
507                 }
508                 case 'c':
509                         if (IS_MDT(ldd)) {
510                                 int stripe_count = atol(optarg);
511
512                                 if (stripe_count <= 0) {
513                                         fprintf(stderr, "%s: bad stripe count "
514                                                 "%d\n", progname, stripe_count);
515                                         return 1;
516                                 }
517                                 mop->mo_stripe_count = stripe_count;
518                         } else {
519                                 badopt(long_opt[longidx].name, "MDT");
520                                 return 1;
521                         }
522                         break;
523                 case 'd':
524                         mop->mo_device_kb = atol(optarg);
525                         break;
526                 case 'k':
527                         strscpy(mop->mo_mkfsopts, optarg,
528                                 sizeof(mop->mo_mkfsopts));
529                         break;
530                 case 'M':
531                         ldd->ldd_flags |= LDD_F_SV_TYPE_MDT;
532                         break;
533                 case 'O':
534                         ldd->ldd_flags |= LDD_F_SV_TYPE_OST;
535                         break;
536                 case 'r':
537                         mop->mo_flags |= MO_FORCEFORMAT;
538                         break;
539                 case 'R':
540                         replace = 1;
541                         break;
542 #else /* !TUNEFS */
543                 case 'e':
544                         ldd->ldd_params[0] = '\0';
545                         /* Must update the mgs logs */
546                         ldd->ldd_flags |= LDD_F_UPDATE;
547                         break;
548                 case 'Q':
549                         mop->mo_flags |= MO_QUOTA;
550                         break;
551                 case 'R': {
552                         char *tmp;
553
554                         mop->mo_flags |= MO_RENAME;
555                         if (!optarg) {
556                                 if (IS_SEPARATED_MGS(ldd)) {
557                                         fprintf(stderr, "%s: must specify the "
558                                                 "old fsname to be renamed for "
559                                                 "separated MGS\n", progname);
560                                         return 1;
561                                 }
562                                 break;
563                         }
564
565                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
566                                 fprintf(stderr, "%s: filesystem name must be "
567                                         "1-8 chars\n", progname);
568                                 return 1;
569                         }
570
571                         tmp = strpbrk(optarg, "/:");
572                         if (tmp) {
573                                 fprintf(stderr, "%s: char '%c' not allowed in "
574                                         "filesystem name\n", progname, *tmp);
575                                 return 1;
576                         }
577
578                         if (IS_SEPARATED_MGS(ldd)) {
579                                 strscpy(old_fsname, optarg,
580                                         sizeof(ldd->ldd_fsname));
581                         } else if (strlen(old_fsname) != strlen(optarg) ||
582                                    strcmp(old_fsname, optarg) != 0) {
583                                 fprintf(stderr, "%s: the given fsname '%s' "
584                                         "to be renamed does not exist\n",
585                                         progname, optarg);
586                                 return 1;
587                         }
588                         break;
589                 }
590                 case 'w':
591                         ldd->ldd_flags |= LDD_F_WRITECONF;
592                         break;
593 #endif /* !TUNEFS */
594                 default:
595                         if (opt != '?') {
596                                 fatal();
597                                 fprintf(stderr, "Unknown option '%c'\n", opt);
598                         }
599                         return EINVAL;
600                 }
601         }
602
603         if (strlen(new_fsname) > 0) {
604                 if (!(mop->mo_flags & (MO_FORCEFORMAT | MO_RENAME)) &&
605                      (!(ldd->ldd_flags &
606                         (LDD_F_UPGRADE14 | LDD_F_VIRGIN | LDD_F_WRITECONF)))) {
607                         fprintf(stderr, "%s: cannot change the name "
608                                 "of a registered target\n", progname);
609                         return 1;
610                 }
611
612                 strscpy(ldd->ldd_fsname, new_fsname, sizeof(ldd->ldd_fsname));
613         }
614
615         if (index_option && !(mop->mo_ldd.ldd_flags &
616               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
617                LDD_F_WRITECONF))) {
618                 fprintf(stderr, "%s: cannot change the index of"
619                         " a registered target\n", progname);
620                 return 1;
621         }
622
623 #ifdef TUNEFS
624         if (mop->mo_flags & MO_RENAME) {
625                 if (new_fsname[0] == '\0') {
626                         fprintf(stderr, "%s: need to specify new fsname for "
627                                 "renaming case\n", progname);
628                         return 1;
629                 }
630
631                 if (strcmp(old_fsname, new_fsname) == 0) {
632                         fprintf(stderr, "%s: cannot rename fsname '%s' to "
633                                 "the same name\n", progname, old_fsname);
634                         return 1;
635                 }
636         }
637 #endif
638
639         /* Need to clear this flag after parsing 'L' and 'i' options. */
640         if (replace)
641                 ldd->ldd_flags &= ~LDD_F_VIRGIN;
642
643         if (optind == argc) {
644                 /* The user didn't specify device name */
645                 fatal();
646                 fprintf(stderr, "Not enough arguments - device name or "
647                         "pool/dataset name not specified.\n");
648                 return EINVAL;
649         } else {
650                 /*  The device or pool/filesystem name */
651                 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
652
653                 /* Followed by optional vdevs */
654                 if (optind < argc - 1)
655                         mop->mo_pool_vdevs = (char **) &argv[optind + 1];
656         }
657
658         return 0;
659 }
660
661 int main(int argc, char *const argv[])
662 {
663         struct mkfs_opts mop;
664         struct lustre_disk_data *ldd = &mop.mo_ldd;
665         char *mountopts = NULL;
666         char wanted_mountopts[512] = "";
667         char old_fsname[16] = "";
668         unsigned mount_type;
669         int ret = 0;
670         int ret2 = 0;
671
672         progname = strrchr(argv[0], '/');
673         if (progname != NULL)
674                 progname++;
675         else
676                 progname = argv[0];
677
678         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
679                 usage(stderr);
680                 return EINVAL;
681         }
682
683         memset(&mop, 0, sizeof(mop));
684         set_defaults(&mop);
685
686         /* device is last arg */
687         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
688
689         ret = osd_init();
690         if (ret != 0)
691                 return ret;
692
693 #ifdef TUNEFS
694         /* For tunefs, we must read in the old values before parsing any
695            new ones. */
696
697         /* Check whether the disk has already been formatted by mkfs.lustre */
698         ret = osd_is_lustre(mop.mo_device, &mount_type);
699         if (ret == 0) {
700                 fatal();
701                 fprintf(stderr, "Device %s has not been formatted with "
702                         "mkfs.lustre\n", mop.mo_device);
703                 ret = ENODEV;
704                 goto out;
705         }
706         ldd->ldd_mount_type = mount_type;
707
708         ret = osd_read_ldd(mop.mo_device, ldd);
709         if (ret != 0) {
710                 fatal();
711                 fprintf(stderr, "Failed to read previous Lustre data from %s "
712                         "(%d)\n", mop.mo_device, ret);
713                 goto out;
714         }
715
716         strscpy(old_fsname, ldd->ldd_fsname, sizeof(ldd->ldd_fsname));
717         ldd->ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
718
719         /* svname of the form lustre:OST1234 means never registered */
720         ret = strlen(ldd->ldd_svname);
721         if (ldd->ldd_svname[ret - 8] == ':') {
722                 ldd->ldd_svname[ret - 8] = '-';
723                 ldd->ldd_flags |= LDD_F_VIRGIN;
724         } else if (ldd->ldd_svname[ret - 8] == '=') {
725                 ldd->ldd_svname[ret - 8] = '-';
726                 ldd->ldd_flags |= LDD_F_WRITECONF;
727         }
728
729         if (strstr(ldd->ldd_params, PARAM_MGSNODE))
730                 mop.mo_mgs_failnodes++;
731
732         if (verbose > 0)
733                 print_ldd("Read previous values", ldd);
734 #endif /* TUNEFS */
735
736         ret = parse_opts(argc, argv, &mop, &mountopts, old_fsname);
737         if (ret != 0 || version)
738                 goto out;
739
740         if (!IS_MDT(ldd) && !IS_OST(ldd) && !IS_MGS(ldd)) {
741                 fatal();
742                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
743                 ret = EINVAL;
744                 goto out;
745         }
746
747         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
748                 fatal();
749                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
750                 ret = EINVAL;
751                 goto out;
752         }
753
754         /* Stand alone MGS doesn't need an index */
755         if (!IS_MDT(ldd) && IS_MGS(ldd)) {
756 #ifndef TUNEFS
757                 /* But if --index was specified flag an error */
758                 if (!(ldd->ldd_flags & LDD_F_NEED_INDEX)) {
759                         badopt("index", "MDT,OST");
760                         goto out;
761                 }
762 #endif
763                 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
764         }
765
766         if ((ldd->ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
767             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
768                 fatal();
769                 fprintf(stderr, "Can't find the target index, "
770                 "specify with --index\n");
771                 ret = EINVAL;
772                 goto out;
773         }
774
775         if (ldd->ldd_flags & LDD_F_NEED_INDEX)
776                 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
777                         "target index must be specified with --index\n",
778                         mop.mo_device);
779
780         /* If no index is supplied for MDT by default set index to zero */
781         if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
782                 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
783                 ldd->ldd_svindex = 0;
784         }
785         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
786                 fatal();
787                 if (IS_MDT(ldd))
788                         fprintf(stderr, "Must specify --mgs or --mgsnode\n");
789                 else
790                         fprintf(stderr, "Must specify --mgsnode\n");
791                 ret = EINVAL;
792                 goto out;
793         }
794         if ((IS_MDT(ldd) || IS_OST(ldd)) && ldd->ldd_fsname[0] == '\0') {
795                 fatal();
796                 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
797                 ret = EINVAL;
798                 goto out;
799         }
800
801         /* These are the permanent mount options (always included) */
802         ret = osd_prepare_lustre(&mop,
803                                  wanted_mountopts, sizeof(wanted_mountopts));
804         if (ret != 0) {
805                 fatal();
806                 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
807                 goto out;
808         }
809
810         if (mountopts) {
811                 trim_mountfsoptions(mountopts);
812                 if (check_mountfsoptions(mountopts, wanted_mountopts)) {
813                         ret = EINVAL;
814                         goto out;
815                 }
816                 snprintf(ldd->ldd_mount_opts, sizeof(ldd->ldd_mount_opts),
817                          "%s", mountopts);
818         } else {
819 #ifdef TUNEFS
820                 if (ldd->ldd_mount_opts[0] == 0)
821                 /* use the defaults unless old opts exist */
822 #endif
823                 {
824                         snprintf(ldd->ldd_mount_opts,
825                                  sizeof(ldd->ldd_mount_opts),
826                                  "%s", wanted_mountopts);
827                         trim_mountfsoptions(ldd->ldd_mount_opts);
828                 }
829         }
830
831         ret = osd_fix_mountopts(&mop, ldd->ldd_mount_opts,
832                                 sizeof(ldd->ldd_mount_opts));
833         if (ret != 0) {
834                 fatal();
835                 fprintf(stderr, "unable to fix mountfsoptions (%d)\n", ret);
836                 goto out;
837         }
838
839         if (server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
840                              ldd->ldd_fsname, ldd->ldd_svname)) {
841                 printf("unknown server type %#x\n", ldd->ldd_flags);
842                 goto out;
843         }
844
845         if (verbose >= 0)
846                 print_ldd("Permanent disk data", ldd);
847
848         if (print_only) {
849                 printf("exiting before disk write.\n");
850                 goto out;
851         }
852
853         if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
854                 return(EEXIST);
855
856         /* Create the loopback file */
857         if (mop.mo_flags & MO_IS_LOOP) {
858                 ret = access(mop.mo_device, F_OK);
859                 if (ret != 0)
860                         ret = errno;
861
862 #ifndef TUNEFS
863                 /* Reformat the loopback file */
864                 if (ret != 0 || (mop.mo_flags & MO_FORCEFORMAT)) {
865                         ret = loop_format(&mop);
866                         if (ret != 0)
867                                 goto out;
868                 }
869 #endif
870                 if (ret == 0)
871                         ret = loop_setup(&mop);
872                 if (ret != 0) {
873                         fatal();
874                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
875                                         mop.mo_device, strerror(ret));
876                         goto out;
877                 }
878         }
879
880 #ifndef TUNEFS
881         /* Check whether the disk has already been formatted by mkfs.lustre */
882         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
883                 ret = osd_is_lustre(mop.mo_device, &mount_type);
884                 if (ret != 0) {
885                         fatal();
886                         fprintf(stderr, "Device %s was previously formatted "
887                                 "for lustre. Use --reformat to reformat it, "
888                                 "or tunefs.lustre to modify.\n",
889                                 mop.mo_device);
890                         goto out;
891                 }
892         }
893
894         /* Format the backing filesystem */
895         ret = osd_make_lustre(&mop);
896         if (ret != 0) {
897                 fatal();
898                 fprintf(stderr, "mkfs failed %d\n", ret);
899                 goto out;
900         }
901 #else /* TUNEFS */
902         /* update svname with '=' to refresh config */
903         if (ldd->ldd_flags & LDD_F_WRITECONF) {
904                 struct mount_opts opts;
905                 opts.mo_ldd = *ldd;
906                 opts.mo_source = mop.mo_device;
907                 (void) osd_label_lustre(&opts);
908         }
909
910         /* Rename filesystem fsname */
911         if (mop.mo_flags & MO_RENAME) {
912                 ret = osd_rename_fsname(&mop, old_fsname);
913                 if (ret)
914                         goto out;
915         }
916
917         /* Enable quota accounting */
918         if (mop.mo_flags & MO_QUOTA) {
919                 ret = osd_enable_quota(&mop);
920                 goto out;
921         }
922 #endif /* !TUNEFS */
923
924         /* Write our config files */
925         ret = osd_write_ldd(&mop);
926         if (ret != 0) {
927                 fatal();
928                 fprintf(stderr, "failed to write local files\n");
929                 goto out;
930         }
931
932 out:
933         osd_fini();
934         ret2 = loop_cleanup(&mop);
935         if (ret == 0)
936                 ret = ret2;
937
938         /* Fix any crazy return values from system() */
939         if (ret != 0 && ((ret & 255) == 0))
940                 return 1;
941
942         if (ret != 0)
943                 verrprint("%s: exiting with %d (%s)\n",
944                           progname, ret, strerror(ret));
945         return ret;
946 }