Whamcloud - gitweb
18ac16c0526c7343f379b7789e06f5497d898bc1
[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, 2015, 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 <stdlib.h>
51 #include <stdio.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <stdarg.h>
55 #include <mntent.h>
56 #include <stdbool.h>
57
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <sys/mount.h>
61 #include <sys/utsname.h>
62
63 #include <string.h>
64 #include <getopt.h>
65 #include <limits.h>
66 #include <ctype.h>
67 #include <lnet/nidstr.h>
68 #include <lustre_disk.h>
69 #include <lustre_param.h>
70 #include <lnet/lnetctl.h>
71 #include <lustre_ver.h>
72
73 #include "mount_utils.h"
74
75 char *progname;
76 int verbose = 1;
77 int version;
78 static int print_only = 0;
79
80 #ifdef HAVE_LDISKFS_OSD
81 #define FSLIST_LDISKFS "ldiskfs"
82 #define HAVE_FSLIST
83 #else
84  #define FSLIST_LDISKFS ""
85 #endif /* HAVE_LDISKFS_OSD */
86 #ifdef HAVE_ZFS_OSD
87  #ifdef HAVE_FSLIST
88    #define FSLIST_ZFS "|zfs"
89  #else
90   #define FSLIST_ZFS "zfs"
91   #define HAVE_FSLIST
92  #endif
93 #else
94  #define FSLIST_ZFS ""
95 #endif /* HAVE_ZFS_OSD */
96
97 #ifndef HAVE_FSLIST
98  #error "no backing OSD types (ldiskfs or ZFS) are configured"
99 #endif
100
101 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
102
103 void usage(FILE *out)
104 {
105         fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
106                 "--fsname=<filesystem name>\n"
107                 "\t--index=<target index> [options] <device>\n", progname);
108 #ifdef HAVE_ZFS_OSD
109         fprintf(out, "usage: %s <target type> --backfstype=zfs "
110                 "--fsname=<filesystem name> [options]\n"
111                 "\t<pool name>/<dataset name>\n"
112                 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
113                 progname);
114 #endif
115         fprintf(out,
116                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
117 #ifdef HAVE_ZFS_OSD
118                 "\t<pool name>: name of ZFS pool where target is created "
119                         "(e.g. tank)\n"
120                 "\t<dataset name>: name of new dataset, must be unique within "
121                         "pool (e.g. ost1)\n"
122                 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
123                         "cache, log)\n"
124 #endif
125                 "\n"
126                 "\ttarget types:\n"
127                 "\t\t--mgs: configuration management service\n"
128                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
129                 "\t\t--ost: object storage, mutually exclusive with mdt, mgs\n"
130                 "\toptions (in order of popularity):\n"
131                 "\t\t--index=#N: numerical target index (0..N)\n"
132                 "\t\t\trequired for all targets other than the MGS,\n"
133                 "\t\t\ttarget index may either be a decimal number or\n"
134                 "\t\t\thexadecimal number starting with '0x'\n"
135                 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
136                 "\t\t\trequired for all targets other than MGS\n"
137                 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
138                 "\t\t\trequired for all targets other than MGS\n"
139                 "\t\t--mountfsoptions=<opts>: permanent Lustre mount options\n"
140                 "\t\t--backfs-mount-opts=<opts>: backing fs mount options\n"
141                 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
142                 "\t\t\tmutually exclusive with --servicenode\n"
143                 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
144                 "\t\t\ttreat nodes as equal service node, mutually exclusive "
145                         "with --failnode\n"
146                 "\t\t--param <key>=<value>: set a permanent parameter\n"
147                 "\t\t\te.g. --param sys.timeout=40\n"
148                 "\t\t\t     --param lov.stripesize=2M\n"
149                 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
150 #ifndef TUNEFS
151                 "\t\t--backfstype=<fstype>: backing fs type (ext3, ldiskfs)\n"
152                 "\t\t--device-size=#N(KB): device size for loop devices\n"
153                 "\t\t--mkfsoptions=<opts>: format options\n"
154                 "\t\t--reformat: overwrite an existing disk\n"
155                 "\t\t--replace: replace an old target with the same index\n"
156                 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
157 #else
158                 "\t\t--erase-params: erase all old parameter settings\n"
159                 "\t\t--nomgs: turn off MGS service on this MDT\n"
160                 "\t\t--writeconf: erase all config logs for this fs.\n"
161                 "\t\t--quota: enable space accounting on old 2.x device.\n"
162 #endif
163                 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
164                 "\t\t--dryrun: report what we would do; don't write to disk\n"
165                 "\t\t--verbose: e.g. show mkfs progress\n"
166                 "\t\t-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                 { "backfstype",         required_argument,      NULL, 'b' },
281                 { "backfs-mount-opts",  required_argument,      NULL, 'B' },
282                 { "stripe-count-hint",  required_argument,      NULL, 'c' },
283                 { "comment",            required_argument,      NULL, 'u' },
284                 { "configdev",          required_argument,      NULL, 'C' },
285                 { "device-size",        required_argument,      NULL, 'd' },
286                 { "dryrun",             no_argument,            NULL, 'n' },
287                 { "erase-params",       no_argument,            NULL, 'e' },
288                 { "failnode",           required_argument,      NULL, 'f' },
289                 { "failover",           required_argument,      NULL, 'f' },
290                 { "mgs",                no_argument,            NULL, 'G' },
291                 { "help",               no_argument,            NULL, 'h' },
292                 { "index",              required_argument,      NULL, 'i' },
293                 { "mkfsoptions",        required_argument,      NULL, 'k' },
294                 { "mgsnode",            required_argument,      NULL, 'm' },
295                 { "mgsnid",             required_argument,      NULL, 'm' },
296                 { "mdt",                no_argument,            NULL, 'M' },
297                 { "fsname",             required_argument,      NULL, 'L' },
298                 { "noformat",           no_argument,            NULL, 'n' },
299                 { "nomgs",              no_argument,            NULL, 'N' },
300                 { "mountfsoptions",     required_argument,      NULL, 'o' },
301                 { "ost",                no_argument,            NULL, 'O' },
302                 { "param",              required_argument,      NULL, 'p' },
303                 { "print",              no_argument,            NULL, 'n' },
304                 { "quiet",              no_argument,            NULL, 'q' },
305                 { "quota",              no_argument,            NULL, 'Q' },
306                 { "reformat",           no_argument,            NULL, 'r' },
307                 { "replace",            no_argument,            NULL, 'R' },
308                 { "servicenode",        required_argument,      NULL, 's' },
309                 { "network",            required_argument,      NULL, 't' },
310                 { "verbose",            no_argument,            NULL, 'v' },
311                 { "version",            no_argument,            NULL, 'V' },
312                 { "writeconf",          no_argument,            NULL, 'w' },
313                 { 0,                    0,                      NULL,  0  }
314         };
315         char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:PqrRs:t:Uu:vVw";
316         int opt;
317         int rc, longidx;
318         int failnode_set = 0, servicenode_set = 0;
319         int replace = 0;
320         bool index_option = false;
321         bool fsname_option = false;
322
323         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
324                EOF) {
325                 switch (opt) {
326                 case 'b': {
327                         int i = 0;
328                         while (i < LDD_MT_LAST) {
329                                 if (strcmp(optarg, mt_str(i)) == 0) {
330                                         mop->mo_ldd.ldd_mount_type = i;
331                                         break;
332                                 }
333                                 i++;
334                         }
335                         if (i == LDD_MT_LAST) {
336                                 fprintf(stderr, "%s: invalid backend filesystem"
337                                         " type %s\n", progname, optarg);
338                                 return 1;
339                         }
340                         break;
341                 }
342                 case 'B':
343                         mop->mo_mountopts = optarg;
344                         break;
345                 case 'c':
346                         if (IS_MDT(&mop->mo_ldd)) {
347                                 int stripe_count = atol(optarg);
348                                 if (stripe_count <= 0) {
349                                         fprintf(stderr, "%s: bad stripe count "
350                                                 "%d\n", progname, stripe_count);
351                                         return 1;
352                                 }
353                                 mop->mo_stripe_count = stripe_count;
354                         } else {
355                                 badopt(long_opt[longidx].name, "MDT");
356                                 return 1;
357                         }
358                         break;
359                 case 'C': /* Configdev */
360                         //FIXME
361                         printf("Configdev not implemented\n");
362                         return 1;
363                 case 'd':
364                         mop->mo_device_kb = atol(optarg);
365                         break;
366                 case 'e':
367                         mop->mo_ldd.ldd_params[0] = '\0';
368                         /* Must update the mgs logs */
369                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
370                         break;
371                 case 'f':
372                 case 's': {
373                         char *nids;
374
375                         if ((opt == 'f' && servicenode_set)
376                             || (opt == 's' && failnode_set)) {
377                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
378                                         progname, long_opt[longidx].name,
379                                         opt == 'f' ? "servicenode" : "failnode");
380                                 return 1;
381                         }
382
383                         nids = convert_hostnames(optarg);
384                         if (!nids)
385                                 return 1;
386                         rc = append_param(mop->mo_ldd.ldd_params,
387                                           PARAM_FAILNODE, nids, ':');
388                         free(nids);
389                         if (rc)
390                                 return rc;
391                         /* Must update the mgs logs */
392                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
393                         if (opt == 'f') {
394                                 failnode_set = 1;
395                         } else {
396                                 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
397                                 servicenode_set = 1;
398                         }
399                         mop->mo_flags |= MO_FAILOVER;
400                         break;
401                 }
402                 case 'G':
403                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
404                         break;
405                 case 'h':
406                         usage(stdout);
407                         return 1;
408                 case 'i': {
409                         char *endptr = NULL;
410                         int base;
411                         index_option = true;
412                         /* LU-2374: check whether it is OST/MDT later */
413                         base = (strlen(optarg) > 1 &&
414                                 !strncmp(optarg, "0x", 2)) ? 16 : 10;
415                         /* Allowed input are base 16 and base 10 numbers only */
416                         mop->mo_ldd.ldd_svindex = strtoul(optarg,
417                                                           &endptr, base);
418                         if (*endptr != '\0') {
419                                 fprintf(stderr, "%s: wrong index %s. "
420                                         "Target index must be decimal or "
421                                         "hexadecimal.\n",
422                                         progname, optarg);
423                                 return 1;
424                         }
425                         if (mop->mo_ldd.ldd_svindex >= INDEX_UNASSIGNED) {
426                                 fprintf(stderr, "%s: wrong index %u. "
427                                         "Target index must be less than %u.\n",
428                                         progname, mop->mo_ldd.ldd_svindex,
429                                         INDEX_UNASSIGNED);
430                                 return 1;
431                         }
432                         mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
433                         break;
434                 }
435                 case 'k':
436                         strscpy(mop->mo_mkfsopts, optarg,
437                                 sizeof(mop->mo_mkfsopts));
438                         break;
439                 case 'L': {
440                         rc = lustre_is_fsname_valid(optarg, 1,
441                                                     LUSTRE_MAXFSNAME);
442                         if (rc < 0) {
443                                 fprintf(stderr, "%s: filesystem name must be "
444                                         "1-%d chars\n", progname,
445                                         LUSTRE_MAXFSNAME);
446                                 return 1;
447                         } else if (rc > 0) {
448                                 fprintf(stderr, "%s: char '%c' not allowed in "
449                                         "filesystem name\n", progname, rc);
450                                 return 1;
451                         }
452
453                         strscpy(mop->mo_ldd.ldd_fsname, optarg,
454                                 sizeof(mop->mo_ldd.ldd_fsname));
455                         fsname_option = true;
456                         break;
457                 }
458                 case 'm': {
459                         char *nids = convert_hostnames(optarg);
460                         if (!nids)
461                                 return 1;
462                         rc = append_param(mop->mo_ldd.ldd_params,
463                                           PARAM_MGSNODE, nids, ':');
464                         free(nids);
465                         if (rc)
466                                 return rc;
467                         mop->mo_mgs_failnodes++;
468                         break;
469                 }
470                 case 'M':
471                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
472                         break;
473                 case 'n':
474                         print_only++;
475                         break;
476                 case 'N':
477                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
478                         break;
479                 case 'o':
480                         *mountopts = optarg;
481                         break;
482                 case 'O':
483                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
484                         break;
485                 case 'p':
486                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
487                         if (rc)
488                                 return rc;
489                         /* Must update the mgs logs */
490                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
491                         break;
492                 case 'q':
493                         verbose--;
494                         break;
495                 case 'r':
496                         mop->mo_flags |= MO_FORCEFORMAT;
497                         break;
498                 case 'R':
499                         replace = 1;
500                         break;
501                 case 't':
502                         if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
503                                 badopt(long_opt[longidx].name, "MDT,OST");
504                                 return 1;
505                         }
506
507                         if (!optarg)
508                                 return 1;
509
510                         rc = add_param(mop->mo_ldd.ldd_params,
511                                        PARAM_NETWORK, optarg);
512                         if (rc != 0)
513                                 return rc;
514                         /* Must update the mgs logs */
515                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
516                         break;
517                 case 'u':
518                         strscpy(mop->mo_ldd.ldd_userdata, optarg,
519                                 sizeof(mop->mo_ldd.ldd_userdata));
520                         break;
521                 case 'v':
522                         verbose++;
523                         break;
524                 case 'V':
525                         ++version;
526                         fprintf(stdout, "%s %s\n", progname,
527                                 LUSTRE_VERSION_STRING);
528                         return 0;
529                 case 'w':
530                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
531                         break;
532                 case 'Q':
533                         mop->mo_flags |= MO_QUOTA;
534                         break;
535                 default:
536                         if (opt != '?') {
537                                 fatal();
538                                 fprintf(stderr, "Unknown option '%c'\n", opt);
539                         }
540                         return EINVAL;
541                 }
542         }//while
543
544         if (fsname_option &&
545             !(mop->mo_flags & MO_FORCEFORMAT) &&
546             (!(mop->mo_ldd.ldd_flags &
547                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
548                 LDD_F_WRITECONF)))) {
549                 fprintf(stderr, "%s: cannot change the name of"
550                         " a registered target\n", progname);
551                 return 1;
552         }
553         if (index_option && !(mop->mo_ldd.ldd_flags &
554               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
555                LDD_F_WRITECONF))) {
556                 fprintf(stderr, "%s: cannot change the index of"
557                         " a registered target\n", progname);
558                 return 1;
559         }
560
561         /* Need to clear this flag after parsing 'L' and 'i' options. */
562         if (replace)
563                 mop->mo_ldd.ldd_flags &= ~LDD_F_VIRGIN;
564
565         if (optind == argc) {
566                 /* The user didn't specify device name */
567                 fatal();
568                 fprintf(stderr, "Not enough arguments - device name or "
569                         "pool/dataset name not specified.\n");
570                 return EINVAL;
571         } else {
572                 /*  The device or pool/filesystem name */
573                 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
574
575                 /* Followed by optional vdevs */
576                 if (optind < argc - 1)
577                         mop->mo_pool_vdevs = (char **) &argv[optind + 1];
578         }
579
580         return 0;
581 }
582
583 int main(int argc, char *const argv[])
584 {
585         struct mkfs_opts mop;
586         struct lustre_disk_data *ldd;
587         char *mountopts = NULL;
588         char wanted_mountopts[512] = "";
589         unsigned mount_type;
590         int ret = 0;
591         int ret2 = 0;
592
593         if ((progname = strrchr(argv[0], '/')) != NULL)
594                 progname++;
595         else
596                 progname = argv[0];
597
598         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
599                 usage(stderr);
600                 return(EINVAL);
601         }
602
603         memset(&mop, 0, sizeof(mop));
604         set_defaults(&mop);
605
606         /* device is last arg */
607         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
608
609         ret = osd_init();
610         if (ret)
611                 return ret;
612
613 #ifdef TUNEFS
614         /* For tunefs, we must read in the old values before parsing any
615            new ones. */
616
617         /* Check whether the disk has already been formatted by mkfs.lustre */
618         ret = osd_is_lustre(mop.mo_device, &mount_type);
619         if (ret == 0) {
620                 fatal();
621                 fprintf(stderr, "Device %s has not been formatted with "
622                         "mkfs.lustre\n", mop.mo_device);
623                 ret = ENODEV;
624                 goto out;
625         }
626         mop.mo_ldd.ldd_mount_type = mount_type;
627
628         ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
629         if (ret) {
630                 fatal();
631                 fprintf(stderr, "Failed to read previous Lustre data from %s "
632                         "(%d)\n", mop.mo_device, ret);
633                 goto out;
634         }
635         mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
636
637         /* svname of the form lustre:OST1234 means never registered */
638         ret = strlen(mop.mo_ldd.ldd_svname);
639         if (mop.mo_ldd.ldd_svname[ret - 8] == ':') {
640                 mop.mo_ldd.ldd_svname[ret - 8] = '-';
641                 mop.mo_ldd.ldd_flags |= LDD_F_VIRGIN;
642         } else if (mop.mo_ldd.ldd_svname[ret - 8] == '=') {
643                 mop.mo_ldd.ldd_svname[ret - 8] = '-';
644                 mop.mo_ldd.ldd_flags |= LDD_F_WRITECONF;
645         }
646
647         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
648                 mop.mo_mgs_failnodes++;
649
650         if (verbose > 0)
651                 print_ldd("Read previous values", &(mop.mo_ldd));
652 #endif
653
654         ret = parse_opts(argc, argv, &mop, &mountopts);
655         if (ret || version)
656                 goto out;
657
658         ldd = &mop.mo_ldd;
659
660         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
661                 fatal();
662                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
663                 ret = EINVAL;
664                 goto out;
665         }
666
667         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
668                 fatal();
669                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
670                 ret = EINVAL;
671                 goto out;
672         }
673
674         /* Stand alone MGS doesn't need an index */
675         if (!IS_MDT(ldd) && IS_MGS(ldd)) {
676 #ifndef TUNEFS /* mkfs.lustre */
677                 /* But if --index was specified flag an error */
678                 if (!(mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)) {
679                         badopt("index", "MDT,OST");
680                         goto out;
681                 }
682 #endif
683                 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
684         }
685
686         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
687             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
688                 fatal();
689                 fprintf(stderr, "Can't find the target index, "
690                         "specify with --index\n");
691                 ret = EINVAL;
692                 goto out;
693         }
694
695         if (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)
696                 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
697                         "target index must be specified with --index\n",
698                         mop.mo_device);
699
700         /* If no index is supplied for MDT by default set index to zero */
701         if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
702                 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
703                 mop.mo_ldd.ldd_svindex = 0;
704         }
705         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
706                 fatal();
707                 if (IS_MDT(ldd))
708                         fprintf(stderr, "Must specify --mgs or --mgsnode\n");
709                 else
710                         fprintf(stderr, "Must specify --mgsnode\n");
711                 ret = EINVAL;
712                 goto out;
713         }
714         if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
715                 fatal();
716                 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
717                 ret = EINVAL;
718                 goto out;
719         }
720
721         /* These are the permanent mount options (always included) */
722         ret = osd_prepare_lustre(&mop,
723                                  wanted_mountopts, sizeof(wanted_mountopts));
724         if (ret) {
725                 fatal();
726                 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
727                 goto out;
728         }
729
730         if (mountopts) {
731                 trim_mountfsoptions(mountopts);
732                 if (check_mountfsoptions(mountopts, wanted_mountopts)) {
733                         ret = EINVAL;
734                         goto out;
735                 }
736                 snprintf(ldd->ldd_mount_opts, sizeof(ldd->ldd_mount_opts),
737                          "%s", mountopts);
738         } else {
739 #ifdef TUNEFS
740                 if (ldd->ldd_mount_opts[0] == 0)
741                 /* use the defaults unless old opts exist */
742 #endif
743                 {
744                         snprintf(ldd->ldd_mount_opts,
745                                  sizeof(ldd->ldd_mount_opts),
746                                  "%s", wanted_mountopts);
747                         trim_mountfsoptions(ldd->ldd_mount_opts);
748                 }
749         }
750
751         ret = osd_fix_mountopts(&mop, ldd->ldd_mount_opts,
752                                 sizeof(ldd->ldd_mount_opts));
753         if (ret) {
754                 fatal();
755                 fprintf(stderr, "unable to fix mountfsoptions (%d)\n", ret);
756                 goto out;
757         }
758
759         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
760                          ldd->ldd_fsname, ldd->ldd_svname);
761
762         if (verbose >= 0)
763                 print_ldd("Permanent disk data", ldd);
764
765         if (print_only) {
766                 printf("exiting before disk write.\n");
767                 goto out;
768         }
769
770         if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
771                 return(EEXIST);
772
773         /* Create the loopback file */
774         if (mop.mo_flags & MO_IS_LOOP) {
775                 ret = access(mop.mo_device, F_OK);
776                 if (ret)
777                         ret = errno;
778 #ifndef TUNEFS /* mkfs.lustre */
779                 /* Reformat the loopback file */
780                 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
781                         ret = loop_format(&mop);
782                         if (ret)
783                                 goto out;
784                 }
785 #endif
786                 if (ret == 0)
787                         ret = loop_setup(&mop);
788                 if (ret) {
789                         fatal();
790                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
791                                 mop.mo_device, strerror(ret));
792                         goto out;
793                 }
794         }
795
796 #ifndef TUNEFS /* mkfs.lustre */
797         /* Check whether the disk has already been formatted by mkfs.lustre */
798         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
799                 ret = osd_is_lustre(mop.mo_device, &mount_type);
800                 if (ret) {
801                         fatal();
802                         fprintf(stderr, "Device %s was previously formatted "
803                                 "for lustre. Use --reformat to reformat it, "
804                                 "or tunefs.lustre to modify.\n",
805                                 mop.mo_device);
806                         goto out;
807                 }
808         }
809
810         /* Format the backing filesystem */
811         ret = osd_make_lustre(&mop);
812         if (ret != 0) {
813                 fatal();
814                 fprintf(stderr, "mkfs failed %d\n", ret);
815                 goto out;
816         }
817 #else
818         /* update svname with '=' to refresh config */
819         if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
820                 struct mount_opts opts;
821                 opts.mo_ldd = mop.mo_ldd;
822                 opts.mo_source = mop.mo_device;
823                 (void) osd_label_lustre(&opts);
824         }
825
826         /* Enable quota accounting */
827         if (mop.mo_flags & MO_QUOTA) {
828                 ret = osd_enable_quota(&mop);
829                 goto out;
830         }
831
832 #endif
833
834         /* Write our config files */
835         ret = osd_write_ldd(&mop);
836         if (ret != 0) {
837                 fatal();
838                 fprintf(stderr, "failed to write local files\n");
839                 goto out;
840         }
841
842 out:
843         osd_fini();
844         ret2 = loop_cleanup(&mop);
845         if (ret == 0)
846                 ret = ret2;
847
848         /* Fix any crazy return values from system() */
849         if (ret && ((ret & 255) == 0))
850                 return (1);
851         if (ret)
852                 verrprint("%s: exiting with %d (%s)\n",
853                           progname, ret, strerror(ret));
854         return (ret);
855 }