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