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