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