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