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