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