Whamcloud - gitweb
LU-6978 utils: mkfs.lustre to recognise non ldiskfs opts
[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--fsname=<8_char_filesystem_name>: fs targets belong to\n"
137                 "\t\t\trequired for all targets other than MGS\n"
138                 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
139                 "\t\t\trequired for all targets other than MGS\n"
140                 "\t\t--mountfsoptions=<opts>: permanent Lustre mount options\n"
141                 "\t\t--backfs-mount-opts=<opts>: backing fs mount options\n"
142                 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
143                 "\t\t\tmutually exclusive with --servicenode\n"
144                 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
145                 "\t\t\ttreat nodes as equal service node, mutually exclusive "
146                         "with --failnode\n"
147                 "\t\t--param <key>=<value>: set a permanent parameter\n"
148                 "\t\t\te.g. --param sys.timeout=40\n"
149                 "\t\t\t     --param lov.stripesize=2M\n"
150                 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
151 #ifndef TUNEFS
152                 "\t\t--backfstype=<fstype>: backing fs type (ext3, ldiskfs)\n"
153                 "\t\t--device-size=#N(KB): device size for loop devices\n"
154                 "\t\t--mkfsoptions=<opts>: format options\n"
155                 "\t\t--reformat: overwrite an existing disk\n"
156                 "\t\t--replace: replace an old target with the same index\n"
157                 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
158 #else
159                 "\t\t--erase-params: erase all old parameter settings\n"
160                 "\t\t--nomgs: turn off MGS service on this MDT\n"
161                 "\t\t--writeconf: erase all config logs for this fs.\n"
162                 "\t\t--quota: enable space accounting on old 2.x device.\n"
163 #endif
164                 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
165                 "\t\t--dryrun: report what we would do; don't write to disk\n"
166                 "\t\t--verbose: e.g. show mkfs progress\n"
167                 "\t\t-V|--version: output build version of the utility and\n"
168                 "\t\t\texit\n"
169                 "\t\t--quiet\n",
170                 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
171         return;
172 }
173
174 /* ==================== Lustre config functions =============*/
175
176 void print_ldd(char *str, struct lustre_disk_data *ldd)
177 {
178         printf("\n   %s:\n", str);
179         printf("Target:     %s\n", ldd->ldd_svname);
180         if (ldd->ldd_svindex == INDEX_UNASSIGNED)
181                 printf("Index:      unassigned\n");
182         else
183                 printf("Index:      %d\n", ldd->ldd_svindex);
184         if (ldd->ldd_uuid[0])
185                 printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
186         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
187         printf("Mount type: %s\n", MT_STR(ldd));
188         printf("Flags:      %#x\n", ldd->ldd_flags);
189         printf("              (%s%s%s%s%s%s%s%s%s)\n",
190                IS_MDT(ldd) ? "MDT ":"",
191                IS_OST(ldd) ? "OST ":"",
192                IS_MGS(ldd) ? "MGS ":"",
193                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
194                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time ":"",
195                ldd->ldd_flags & LDD_F_UPDATE     ? "update ":"",
196                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf ":"",
197                ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
198                ldd->ldd_flags & LDD_F_UPGRADE14  ? "upgrade1.4 ":"");
199         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
200         printf("Parameters:%s\n", ldd->ldd_params);
201         if (ldd->ldd_userdata[0])
202                 printf("Comment: %s\n", ldd->ldd_userdata);
203         printf("\n");
204 }
205
206 void set_defaults(struct mkfs_opts *mop)
207 {
208         mop->mo_ldd.ldd_magic = LDD_MAGIC;
209         mop->mo_ldd.ldd_config_ver = 1;
210         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
211 #ifdef HAVE_LDISKFS_OSD
212         mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
213 #else
214         mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
215 #endif
216         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
217         mop->mo_mgs_failnodes = 0;
218         mop->mo_stripe_count = 1;
219         mop->mo_pool_vdevs = NULL;
220 }
221
222 static inline void badopt(const char *opt, char *type)
223 {
224         fprintf(stderr, "%s: '--%s' only valid for %s\n",
225                 progname, opt, type);
226         usage(stderr);
227 }
228
229 /* from mount_lustre */
230 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
231 #define MAXNIDSTR 1024
232 static char *convert_hostnames(char *s1)
233 {
234         char *converted, *s2 = 0, *c, *end, sep;
235         int left = MAXNIDSTR;
236         lnet_nid_t nid;
237
238         converted = malloc(left);
239         if (converted == NULL) {
240                 return NULL;
241         }
242
243         end = s1 + strlen(s1);
244         c = converted;
245         while ((left > 0) && (s1 < end)) {
246                 s2 = strpbrk(s1, ",:");
247                 if (!s2)
248                         s2 = end;
249                 sep = *s2;
250                 *s2 = '\0';
251                 nid = libcfs_str2nid(s1);
252                 *s2 = sep;
253
254                 if (nid == LNET_NID_ANY) {
255                         fprintf(stderr, "%s: Cannot resolve hostname '%s'.\n",
256                                 progname, s1);
257                         free(converted);
258                         return NULL;
259                 }
260                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
261                             strlen("127.0.0.1")) == 0) {
262                         fprintf(stderr, "%s: The NID '%s' resolves to the "
263                                 "loopback address '%s'.  Lustre requires a "
264                                 "non-loopback address.\n",
265                                 progname, s1, libcfs_nid2str(nid));
266                         free(converted);
267                         return NULL;
268                 }
269
270                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
271                 left = converted + MAXNIDSTR - c;
272                 s1 = s2 + 1;
273         }
274         return converted;
275 }
276
277 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
278                char **mountopts)
279 {
280         static struct option long_opt[] = {
281                 { "backfstype",         required_argument,      NULL, 'b' },
282                 { "backfs-mount-opts",  required_argument,      NULL, 'B' },
283                 { "stripe-count-hint",  required_argument,      NULL, 'c' },
284                 { "comment",            required_argument,      NULL, 'u' },
285                 { "configdev",          required_argument,      NULL, 'C' },
286                 { "device-size",        required_argument,      NULL, 'd' },
287                 { "dryrun",             no_argument,            NULL, 'n' },
288                 { "erase-params",       no_argument,            NULL, 'e' },
289                 { "failnode",           required_argument,      NULL, 'f' },
290                 { "failover",           required_argument,      NULL, 'f' },
291                 { "mgs",                no_argument,            NULL, 'G' },
292                 { "help",               no_argument,            NULL, 'h' },
293                 { "index",              required_argument,      NULL, 'i' },
294                 { "mkfsoptions",        required_argument,      NULL, 'k' },
295                 { "mgsnode",            required_argument,      NULL, 'm' },
296                 { "mgsnid",             required_argument,      NULL, 'm' },
297                 { "mdt",                no_argument,            NULL, 'M' },
298                 { "fsname",             required_argument,      NULL, 'L' },
299                 { "noformat",           no_argument,            NULL, 'n' },
300                 { "nomgs",              no_argument,            NULL, 'N' },
301                 { "mountfsoptions",     required_argument,      NULL, 'o' },
302                 { "ost",                no_argument,            NULL, 'O' },
303                 { "param",              required_argument,      NULL, 'p' },
304                 { "print",              no_argument,            NULL, 'n' },
305                 { "quiet",              no_argument,            NULL, 'q' },
306                 { "quota",              no_argument,            NULL, 'Q' },
307                 { "reformat",           no_argument,            NULL, 'r' },
308                 { "replace",            no_argument,            NULL, 'R' },
309                 { "servicenode",        required_argument,      NULL, 's' },
310                 { "network",            required_argument,      NULL, 't' },
311                 { "verbose",            no_argument,            NULL, 'v' },
312                 { "version",            no_argument,            NULL, 'V' },
313                 { "writeconf",          no_argument,            NULL, 'w' },
314                 { 0,                    0,                      NULL,  0  }
315         };
316         char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:PqrRs:t:Uu:vVw";
317         int opt;
318         int rc, longidx;
319         int failnode_set = 0, servicenode_set = 0;
320         int replace = 0;
321         bool index_option = false;
322         bool fsname_option = false;
323
324         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
325                EOF) {
326                 switch (opt) {
327                 case 'b': {
328                         int i = 0;
329                         while (i < LDD_MT_LAST) {
330                                 if (strcmp(optarg, mt_str(i)) == 0) {
331                                         mop->mo_ldd.ldd_mount_type = i;
332                                         break;
333                                 }
334                                 i++;
335                         }
336                         if (i == LDD_MT_LAST) {
337                                 fprintf(stderr, "%s: invalid backend filesystem"
338                                         " type %s\n", progname, optarg);
339                                 return 1;
340                         }
341                         break;
342                 }
343                 case 'B':
344                         mop->mo_mountopts = optarg;
345                         break;
346                 case 'c':
347                         if (IS_MDT(&mop->mo_ldd)) {
348                                 int stripe_count = atol(optarg);
349                                 if (stripe_count <= 0) {
350                                         fprintf(stderr, "%s: bad stripe count "
351                                                 "%d\n", progname, stripe_count);
352                                         return 1;
353                                 }
354                                 mop->mo_stripe_count = stripe_count;
355                         } else {
356                                 badopt(long_opt[longidx].name, "MDT");
357                                 return 1;
358                         }
359                         break;
360                 case 'C': /* Configdev */
361                         //FIXME
362                         printf("Configdev not implemented\n");
363                         return 1;
364                 case 'd':
365                         mop->mo_device_kb = atol(optarg);
366                         break;
367                 case 'e':
368                         mop->mo_ldd.ldd_params[0] = '\0';
369                         /* Must update the mgs logs */
370                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
371                         break;
372                 case 'f':
373                 case 's': {
374                         char *nids;
375
376                         if ((opt == 'f' && servicenode_set)
377                             || (opt == 's' && failnode_set)) {
378                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
379                                         progname, long_opt[longidx].name,
380                                         opt == 'f' ? "servicenode" : "failnode");
381                                 return 1;
382                         }
383
384                         nids = convert_hostnames(optarg);
385                         if (!nids)
386                                 return 1;
387                         rc = append_param(mop->mo_ldd.ldd_params,
388                                           PARAM_FAILNODE, nids, ':');
389                         free(nids);
390                         if (rc)
391                                 return rc;
392                         /* Must update the mgs logs */
393                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
394                         if (opt == 'f') {
395                                 failnode_set = 1;
396                         } else {
397                                 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
398                                 servicenode_set = 1;
399                         }
400                         mop->mo_flags |= MO_FAILOVER;
401                         break;
402                 }
403                 case 'G':
404                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
405                         break;
406                 case 'h':
407                         usage(stdout);
408                         return 1;
409                 case 'i':
410                         index_option = true;
411                         /* LU-2374: check whether it is OST/MDT later */
412                         mop->mo_ldd.ldd_svindex = atol(optarg);
413                         if (mop->mo_ldd.ldd_svindex >= INDEX_UNASSIGNED) {
414                                 fprintf(stderr, "%s: wrong index %u. "
415                                         "Target index must be less than %u.\n",
416                                         progname, mop->mo_ldd.ldd_svindex,
417                                         INDEX_UNASSIGNED);
418                                 return 1;
419                         }
420                         mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
421                         break;
422                 case 'k':
423                         strscpy(mop->mo_mkfsopts, optarg,
424                                 sizeof(mop->mo_mkfsopts));
425                         break;
426                 case 'L': {
427                         rc = lustre_is_fsname_valid(optarg, 1,
428                                                     LUSTRE_MAXFSNAME);
429                         if (rc < 0) {
430                                 fprintf(stderr, "%s: filesystem name must be "
431                                         "1-%d chars\n", progname,
432                                         LUSTRE_MAXFSNAME);
433                                 return 1;
434                         } else if (rc > 0) {
435                                 fprintf(stderr, "%s: char '%c' not allowed in "
436                                         "filesystem name\n", progname, rc);
437                                 return 1;
438                         }
439
440                         strscpy(mop->mo_ldd.ldd_fsname, optarg,
441                                 sizeof(mop->mo_ldd.ldd_fsname));
442                         fsname_option = true;
443                         break;
444                 }
445                 case 'm': {
446                         char *nids = convert_hostnames(optarg);
447                         if (!nids)
448                                 return 1;
449                         rc = append_param(mop->mo_ldd.ldd_params,
450                                           PARAM_MGSNODE, nids, ':');
451                         free(nids);
452                         if (rc)
453                                 return rc;
454                         mop->mo_mgs_failnodes++;
455                         break;
456                 }
457                 case 'M':
458                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
459                         break;
460                 case 'n':
461                         print_only++;
462                         break;
463                 case 'N':
464                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
465                         break;
466                 case 'o':
467                         *mountopts = optarg;
468                         break;
469                 case 'O':
470                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
471                         break;
472                 case 'p':
473                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
474                         if (rc)
475                                 return rc;
476                         /* Must update the mgs logs */
477                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
478                         break;
479                 case 'q':
480                         verbose--;
481                         break;
482                 case 'r':
483                         mop->mo_flags |= MO_FORCEFORMAT;
484                         break;
485                 case 'R':
486                         replace = 1;
487                         break;
488                 case 't':
489                         if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
490                                 badopt(long_opt[longidx].name, "MDT,OST");
491                                 return 1;
492                         }
493
494                         if (!optarg)
495                                 return 1;
496
497                         rc = add_param(mop->mo_ldd.ldd_params,
498                                        PARAM_NETWORK, optarg);
499                         if (rc != 0)
500                                 return rc;
501                         /* Must update the mgs logs */
502                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
503                         break;
504                 case 'u':
505                         strscpy(mop->mo_ldd.ldd_userdata, optarg,
506                                 sizeof(mop->mo_ldd.ldd_userdata));
507                         break;
508                 case 'v':
509                         verbose++;
510                         break;
511                 case 'V':
512                         ++version;
513                         fprintf(stdout, "%s %s\n", progname,
514                                 LUSTRE_VERSION_STRING);
515                         return 0;
516                 case 'w':
517                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
518                         break;
519                 case 'Q':
520                         mop->mo_flags |= MO_QUOTA;
521                         break;
522                 default:
523                         if (opt != '?') {
524                                 fatal();
525                                 fprintf(stderr, "Unknown option '%c'\n", opt);
526                         }
527                         return EINVAL;
528                 }
529         }//while
530
531         if (fsname_option &&
532             !(mop->mo_flags & MO_FORCEFORMAT) &&
533             (!(mop->mo_ldd.ldd_flags &
534                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
535                 LDD_F_WRITECONF)))) {
536                 fprintf(stderr, "%s: cannot change the name of"
537                         " a registered target\n", progname);
538                 return 1;
539         }
540         if (index_option && !(mop->mo_ldd.ldd_flags &
541               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
542                LDD_F_WRITECONF))) {
543                 fprintf(stderr, "%s: cannot change the index of"
544                         " a registered target\n", progname);
545                 return 1;
546         }
547
548         /* Need to clear this flag after parsing 'L' and 'i' options. */
549         if (replace)
550                 mop->mo_ldd.ldd_flags &= ~LDD_F_VIRGIN;
551
552         if (optind == argc) {
553                 /* The user didn't specify device name */
554                 fatal();
555                 fprintf(stderr, "Not enough arguments - device name or "
556                         "pool/dataset name not specified.\n");
557                 return EINVAL;
558         } else {
559                 /*  The device or pool/filesystem name */
560                 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
561
562                 /* Followed by optional vdevs */
563                 if (optind < argc - 1)
564                         mop->mo_pool_vdevs = (char **) &argv[optind + 1];
565         }
566
567         return 0;
568 }
569
570 int main(int argc, char *const argv[])
571 {
572         struct mkfs_opts mop;
573         struct lustre_disk_data *ldd;
574         char *mountopts = NULL;
575         char always_mountopts[512] = "";
576         char default_mountopts[512] = "";
577         unsigned mount_type;
578         int ret = 0;
579         int ret2 = 0;
580
581         if ((progname = strrchr(argv[0], '/')) != NULL)
582                 progname++;
583         else
584                 progname = argv[0];
585
586         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
587                 usage(stderr);
588                 return(EINVAL);
589         }
590
591         memset(&mop, 0, sizeof(mop));
592         set_defaults(&mop);
593
594         /* device is last arg */
595         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
596
597         ret = osd_init();
598         if (ret)
599                 return ret;
600
601 #ifdef TUNEFS
602         /* For tunefs, we must read in the old values before parsing any
603            new ones. */
604
605         /* Check whether the disk has already been formatted by mkfs.lustre */
606         ret = osd_is_lustre(mop.mo_device, &mount_type);
607         if (ret == 0) {
608                 fatal();
609                 fprintf(stderr, "Device %s has not been formatted with "
610                         "mkfs.lustre\n", mop.mo_device);
611                 ret = ENODEV;
612                 goto out;
613         }
614         mop.mo_ldd.ldd_mount_type = mount_type;
615
616         ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
617         if (ret) {
618                 fatal();
619                 fprintf(stderr, "Failed to read previous Lustre data from %s "
620                         "(%d)\n", mop.mo_device, ret);
621                 goto out;
622         }
623         mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
624
625         /* svname of the form lustre:OST1234 means never registered */
626         ret = strlen(mop.mo_ldd.ldd_svname);
627         if (mop.mo_ldd.ldd_svname[ret - 8] == ':') {
628                 mop.mo_ldd.ldd_svname[ret - 8] = '-';
629                 mop.mo_ldd.ldd_flags |= LDD_F_VIRGIN;
630         } else if (mop.mo_ldd.ldd_svname[ret - 8] == '=') {
631                 mop.mo_ldd.ldd_svname[ret - 8] = '-';
632                 mop.mo_ldd.ldd_flags |= LDD_F_WRITECONF;
633         }
634
635         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
636                 mop.mo_mgs_failnodes++;
637
638         if (verbose > 0)
639                 print_ldd("Read previous values", &(mop.mo_ldd));
640 #endif
641
642         ret = parse_opts(argc, argv, &mop, &mountopts);
643         if (ret || version)
644                 goto out;
645
646         ldd = &mop.mo_ldd;
647
648         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
649                 fatal();
650                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
651                 ret = EINVAL;
652                 goto out;
653         }
654
655         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
656                 fatal();
657                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
658                 ret = EINVAL;
659                 goto out;
660         }
661
662         /* Stand alone MGS doesn't need an index */
663         if (!IS_MDT(ldd) && IS_MGS(ldd)) {
664 #ifndef TUNEFS /* mkfs.lustre */
665                 /* But if --index was specified flag an error */
666                 if (!(mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)) {
667                         badopt("index", "MDT,OST");
668                         goto out;
669                 }
670 #endif
671                 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
672         }
673
674         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
675             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
676                 fatal();
677                 fprintf(stderr, "Can't find the target index, "
678                         "specify with --index\n");
679                 ret = EINVAL;
680                 goto out;
681         }
682
683         if (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)
684                 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
685                         "target index must be specified with --index\n",
686                         mop.mo_device);
687
688         /* If no index is supplied for MDT by default set index to zero */
689         if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
690                 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
691                 mop.mo_ldd.ldd_svindex = 0;
692         }
693         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
694                 fatal();
695                 if (IS_MDT(ldd))
696                         fprintf(stderr, "Must specify --mgs or --mgsnode\n");
697                 else
698                         fprintf(stderr, "Must specify --mgsnode\n");
699                 ret = EINVAL;
700                 goto out;
701         }
702         if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
703                 fatal();
704                 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
705                 ret = EINVAL;
706                 goto out;
707         }
708
709         /* These are the permanent mount options (always included) */
710         ret = osd_prepare_lustre(&mop,
711                                  default_mountopts, sizeof(default_mountopts),
712                                  always_mountopts, sizeof(always_mountopts));
713         if (ret) {
714                 fatal();
715                 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
716                 goto out;
717         }
718
719         if (mountopts) {
720                 trim_mountfsoptions(mountopts);
721                 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
722                 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
723                         ret = EINVAL;
724                         goto out;
725                 }
726                 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
727         } else {
728 #ifdef TUNEFS
729                 if (ldd->ldd_mount_opts[0] == 0)
730                         /* use the defaults unless old opts exist */
731 #endif
732                 {
733                         sprintf(ldd->ldd_mount_opts, "%s%s",
734                                 always_mountopts, default_mountopts);
735                         trim_mountfsoptions(ldd->ldd_mount_opts);
736                 }
737         }
738
739         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
740                          ldd->ldd_fsname, ldd->ldd_svname);
741
742         if (verbose >= 0)
743                 print_ldd("Permanent disk data", ldd);
744
745         if (print_only) {
746                 printf("exiting before disk write.\n");
747                 goto out;
748         }
749
750         if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
751                 return(EEXIST);
752
753         /* Create the loopback file */
754         if (mop.mo_flags & MO_IS_LOOP) {
755                 ret = access(mop.mo_device, F_OK);
756                 if (ret)
757                         ret = errno;
758 #ifndef TUNEFS /* mkfs.lustre */
759                 /* Reformat the loopback file */
760                 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
761                         ret = loop_format(&mop);
762                         if (ret)
763                                 goto out;
764                 }
765 #endif
766                 if (ret == 0)
767                         ret = loop_setup(&mop);
768                 if (ret) {
769                         fatal();
770                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
771                                 mop.mo_device, strerror(ret));
772                         goto out;
773                 }
774         }
775
776 #ifndef TUNEFS /* mkfs.lustre */
777         /* Check whether the disk has already been formatted by mkfs.lustre */
778         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
779                 ret = osd_is_lustre(mop.mo_device, &mount_type);
780                 if (ret) {
781                         fatal();
782                         fprintf(stderr, "Device %s was previously formatted "
783                                 "for lustre. Use --reformat to reformat it, "
784                                 "or tunefs.lustre to modify.\n",
785                                 mop.mo_device);
786                         goto out;
787                 }
788         }
789
790         /* Format the backing filesystem */
791         ret = osd_make_lustre(&mop);
792         if (ret != 0) {
793                 fatal();
794                 fprintf(stderr, "mkfs failed %d\n", ret);
795                 goto out;
796         }
797 #else
798         /* update svname with '=' to refresh config */
799         if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
800                 struct mount_opts opts;
801                 opts.mo_ldd = mop.mo_ldd;
802                 opts.mo_source = mop.mo_device;
803                 (void) osd_label_lustre(&opts);
804         }
805
806         /* Enable quota accounting */
807         if (mop.mo_flags & MO_QUOTA) {
808                 ret = osd_enable_quota(&mop);
809                 goto out;
810         }
811
812 #endif
813
814         /* Write our config files */
815         ret = osd_write_ldd(&mop);
816         if (ret != 0) {
817                 fatal();
818                 fprintf(stderr, "failed to write local files\n");
819                 goto out;
820         }
821
822 out:
823         osd_fini();
824         ret2 = loop_cleanup(&mop);
825         if (ret == 0)
826                 ret = ret2;
827
828         /* Fix any crazy return values from system() */
829         if (ret && ((ret & 255) == 0))
830                 return (1);
831         if (ret)
832                 verrprint("%s: exiting with %d (%s)\n",
833                           progname, ret, strerror(ret));
834         return (ret);
835 }