Whamcloud - gitweb
LU-1608 utils: require --fsname for mkfs.lustre
[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, Whamcloud, Inc.
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
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/mount.h>
60 #include <sys/utsname.h>
61
62 #include <string.h>
63 #include <getopt.h>
64 #include <limits.h>
65 #include <ctype.h>
66
67 #ifdef __linux__
68 /* libcfs.h is not really needed here, but on SLES10/PPC, fs.h includes idr.h
69  * which requires BITS_PER_LONG to be defined */
70 #include <libcfs/libcfs.h>
71 #ifndef BLKGETSIZE64
72 #include <linux/fs.h> /* for BLKGETSIZE64 */
73 #endif
74 #include <linux/version.h>
75 #endif
76 #include <lustre_disk.h>
77 #include <lustre_param.h>
78 #include <lnet/lnetctl.h>
79 #include <lustre_ver.h>
80 #include "mount_utils.h"
81
82 #ifndef PATH_MAX
83 #define PATH_MAX 4096
84 #endif
85
86 char *progname;
87 int verbose = 1;
88 static int print_only = 0;
89 static int upgrade_to_18 = 0;
90
91 #define FSLIST_LDISKFS "ldiskfs"
92 #define HAVE_FSLIST
93 #ifdef HAVE_ZFS_OSD
94  #ifdef HAVE_FSLIST
95    #define FSLIST_ZFS "|zfs"
96  #else
97   #define FSLIST_ZFS "zfs"
98   #define HAVE_FSLIST
99  #endif
100 #else
101  #define FSLIST_ZFS ""
102 #endif /* HAVE_ZFS_OSD */
103
104 #ifndef HAVE_FSLIST
105  #error "no backing OSD types (ldiskfs or ZFS) are configured"
106 #endif
107
108 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
109
110 void usage(FILE *out)
111 {
112         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
113         fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
114                 "--fsname=<filesystem name>\n"
115                 "\t--index=<target index> [options] <device>\n", progname);
116 #ifdef HAVE_ZFS_OSD
117         fprintf(out, "usage: %s <target type> --backfstype=zfs "
118                 "--fsname=<filesystem name> [options]\n"
119                 "\t<pool name>/<dataset name>\n"
120                 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
121                 progname);
122 #endif
123         fprintf(out,
124                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
125 #ifdef HAVE_ZFS_OSD
126                 "\t<pool name>: name of ZFS pool where target is created "
127                         "(e.g. tank)\n"
128                 "\t<dataset name>: name of new dataset, must be unique within "
129                         "pool (e.g. ost1)\n"
130                 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
131                         "cache, log)\n"
132 #endif
133                 "\n"
134                 "\ttarget types:\n"
135                 "\t\t--mgs: configuration management service\n"
136                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
137                 "\t\t--ost: object storage, mutually exclusive with mdt, mgs\n"
138                 "\toptions (in order of popularity):\n"
139                 "\t\t--index=#N: numerical target index (0..N)\n"
140                 "\t\t\trequired for all targets other than the MGS\n"
141                 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
142                 "\t\t\trequired for all targets other than MGS\n"
143                 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
144                 "\t\t\trequired for all targets other than MGS\n"
145                 "\t\t--mountfsoptions=<opts>: permanent mount options\n"
146                 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
147                 "\t\t\tmutually exclusive with --servicenode\n"
148                 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
149                 "\t\t\ttreat nodes as equal service node, mutually exclusive "
150                         "with --failnode\n"
151                 "\t\t--param <key>=<value>: set a permanent parameter\n"
152                 "\t\t\te.g. --param sys.timeout=40\n"
153                 "\t\t\t     --param lov.stripesize=2M\n"
154                 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
155 #ifndef TUNEFS
156                 "\t\t--backfstype=<fstype>: backing fs type (ext3, ldiskfs)\n"
157                 "\t\t--device-size=#N(KB): device size for loop devices\n"
158                 "\t\t--mkfsoptions=<opts>: format options\n"
159                 "\t\t--reformat: overwrite an existing disk\n"
160                 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
161 #else
162                 "\t\t--erase-params: erase all old parameter settings\n"
163                 "\t\t--nomgs: turn off MGS service on this MDT\n"
164                 "\t\t--writeconf: erase all config logs for this fs.\n"
165 #endif
166                 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
167                 "\t\t--dryrun: report what we would do; don't write to disk\n"
168                 "\t\t--verbose: e.g. show mkfs progress\n"
169                 "\t\t--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%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_IAM_DIR  ? "IAM_dir_format ":"",
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         mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
213         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
214         mop->mo_mgs_failnodes = 0;
215         mop->mo_stripe_count = 1;
216         mop->mo_pool_vdevs = NULL;
217 }
218
219 static inline void badopt(const char *opt, char *type)
220 {
221         fprintf(stderr, "%s: '--%s' only valid for %s\n",
222                 progname, opt, type);
223         usage(stderr);
224 }
225
226 /* from mount_lustre */
227 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
228 #define MAXNIDSTR 1024
229 static char *convert_hostnames(char *s1)
230 {
231         char *converted, *s2 = 0, *c, *end, sep;
232         int left = MAXNIDSTR;
233         lnet_nid_t nid;
234
235         converted = malloc(left);
236         if (converted == NULL) {
237                 return NULL;
238         }
239
240         end = s1 + strlen(s1);
241         c = converted;
242         while ((left > 0) && (s1 < end)) {
243                 s2 = strpbrk(s1, ",:");
244                 if (!s2)
245                         s2 = end;
246                 sep = *s2;
247                 *s2 = '\0';
248                 nid = libcfs_str2nid(s1);
249                 *s2 = sep;
250
251                 if (nid == LNET_NID_ANY) {
252                         fprintf(stderr, "%s: Can't parse NID '%s'\n",
253                                 progname, s1);
254                         free(converted);
255                         return NULL;
256                 }
257                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
258                             strlen("127.0.0.1")) == 0) {
259                         fprintf(stderr, "%s: The NID '%s' resolves to the "
260                                 "loopback address '%s'.  Lustre requires a "
261                                 "non-loopback address.\n",
262                                 progname, s1, libcfs_nid2str(nid));
263                         free(converted);
264                         return NULL;
265                 }
266
267                 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
268                 left = converted + MAXNIDSTR - c;
269                 s1 = s2 + 1;
270         }
271         return converted;
272 }
273
274 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
275                char **mountopts)
276 {
277         static struct option long_opt[] = {
278                 {"iam-dir", 0, 0, 'a'},
279                 {"backfstype", 1, 0, 'b'},
280                 {"stripe-count-hint", 1, 0, 'c'},
281                 {"comment", 1, 0, 'u'},
282                 {"configdev", 1, 0, 'C'},
283                 {"device-size", 1, 0, 'd'},
284                 {"dryrun", 0, 0, 'n'},
285                 {"erase-params", 0, 0, 'e'},
286                 {"failnode", 1, 0, 'f'},
287                 {"failover", 1, 0, 'f'},
288                 {"mgs", 0, 0, 'G'},
289                 {"help", 0, 0, 'h'},
290                 {"index", 1, 0, 'i'},
291                 {"mkfsoptions", 1, 0, 'k'},
292                 {"mgsnode", 1, 0, 'm'},
293                 {"mgsnid", 1, 0, 'm'},
294                 {"mdt", 0, 0, 'M'},
295                 {"fsname",1, 0, 'L'},
296                 {"noformat", 0, 0, 'n'},
297                 {"nomgs", 0, 0, 'N'},
298                 {"mountfsoptions", 1, 0, 'o'},
299                 {"ost", 0, 0, 'O'},
300                 {"param", 1, 0, 'p'},
301                 {"print", 0, 0, 'n'},
302                 {"quiet", 0, 0, 'q'},
303                 {"reformat", 0, 0, 'r'},
304                 {"servicenode", 1, 0, 's'},
305                 {"verbose", 0, 0, 'v'},
306                 {"writeconf", 0, 0, 'w'},
307                 {"upgrade_to_18", 0, 0, 'U'},
308                 {"network", 1, 0, 't'},
309                 {0, 0, 0, 0}
310         };
311         char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrs:t:Uu:vw";
312         int opt;
313         int rc, longidx;
314         int failnode_set = 0, servicenode_set = 0;
315
316         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
317                EOF) {
318                 switch (opt) {
319                 case 'a': {
320                         if (IS_MDT(&mop->mo_ldd))
321                                 mop->mo_ldd.ldd_flags |= LDD_F_IAM_DIR;
322                         break;
323                 }
324                 case 'b': {
325                         int i = 0;
326                         while (i < LDD_MT_LAST) {
327                                 if (strcmp(optarg, mt_str(i)) == 0) {
328                                         mop->mo_ldd.ldd_mount_type = i;
329                                         break;
330                                 }
331                                 i++;
332                         }
333                         if (i == LDD_MT_LAST) {
334                                 fprintf(stderr, "%s: invalid backend filesystem"
335                                         " type %s\n", progname, optarg);
336                                 return 1;
337                         }
338                         break;
339                 }
340                 case 'c':
341                         if (IS_MDT(&mop->mo_ldd)) {
342                                 int stripe_count = atol(optarg);
343                                 if (stripe_count <= 0) {
344                                         fprintf(stderr, "%s: bad stripe count "
345                                                 "%d\n", progname, stripe_count);
346                                         return 1;
347                                 }
348                                 mop->mo_stripe_count = stripe_count;
349                         } else {
350                                 badopt(long_opt[longidx].name, "MDT");
351                                 return 1;
352                         }
353                         break;
354                 case 'C': /* Configdev */
355                         //FIXME
356                         printf("Configdev not implemented\n");
357                         return 1;
358                 case 'd':
359                         mop->mo_device_sz = atol(optarg);
360                         break;
361                 case 'e':
362                         mop->mo_ldd.ldd_params[0] = '\0';
363                         /* Must update the mgs logs */
364                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
365                         break;
366                 case 'f':
367                 case 's': {
368                         char *nids;
369
370                         if ((opt == 'f' && servicenode_set)
371                             || (opt == 's' && failnode_set)) {
372                                 fprintf(stderr, "%s: %s cannot use with --%s\n",
373                                         progname, long_opt[longidx].name,
374                                         opt == 'f' ? "servicenode" : "failnode");
375                                 return 1;
376                         }
377
378                         nids = convert_hostnames(optarg);
379                         if (!nids)
380                                 return 1;
381                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
382                                        nids);
383                         free(nids);
384                         if (rc)
385                                 return rc;
386                         /* Must update the mgs logs */
387                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
388                         if (opt == 'f') {
389                                 failnode_set = 1;
390                         } else {
391                                 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
392                                 servicenode_set = 1;
393                         }
394                         mop->mo_flags |= MO_FAILOVER;
395                         break;
396                 }
397                 case 'G':
398                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
399                         break;
400                 case 'h':
401                         usage(stdout);
402                         return 1;
403                 case 'i':
404                         if (!(mop->mo_ldd.ldd_flags &
405                               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
406                                LDD_F_WRITECONF))) {
407                                 fprintf(stderr, "%s: cannot change the index of"
408                                         " a registered target\n", progname);
409                                 return 1;
410                         }
411                         if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
412                                 mop->mo_ldd.ldd_svindex = atol(optarg);
413                                 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
414                         } else {
415                                 badopt(long_opt[longidx].name, "MDT,OST");
416                                 return 1;
417                         }
418                         break;
419                 case 'k':
420                         strscpy(mop->mo_mkfsopts, optarg,
421                                 sizeof(mop->mo_mkfsopts));
422                         break;
423                 case 'L': {
424                         char *tmp;
425                         if (!(mop->mo_flags & MO_FORCEFORMAT) &&
426                             (!(mop->mo_ldd.ldd_flags &
427                                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
428                                 LDD_F_WRITECONF)))) {
429                                 fprintf(stderr, "%s: cannot change the name of"
430                                         " a registered target\n", progname);
431                                 return 1;
432                         }
433                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
434                                 fprintf(stderr, "%s: filesystem name must be "
435                                         "1-8 chars\n", progname);
436                                 return 1;
437                         }
438                         if ((tmp = strpbrk(optarg, "/:"))) {
439                                 fprintf(stderr, "%s: char '%c' not allowed in "
440                                         "filesystem name\n", progname, *tmp);
441                                 return 1;
442                         }
443                         strscpy(mop->mo_ldd.ldd_fsname, optarg,
444                                 sizeof(mop->mo_ldd.ldd_fsname));
445                         break;
446                 }
447                 case 'm': {
448                         char *nids = convert_hostnames(optarg);
449                         if (!nids)
450                                 return 1;
451                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
452                                        nids);
453                         free(nids);
454                         if (rc)
455                                 return rc;
456                         mop->mo_mgs_failnodes++;
457                         break;
458                 }
459                 case 'M':
460                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
461                         break;
462                 case 'n':
463                         print_only++;
464                         break;
465                 case 'N':
466                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
467                         break;
468                 case 'o':
469                         *mountopts = optarg;
470                         break;
471                 case 'O':
472                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
473                         break;
474                 case 'p':
475                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
476                         if (rc)
477                                 return rc;
478                         /* Must update the mgs logs */
479                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
480                         break;
481                 case 'q':
482                         verbose--;
483                         break;
484                 case 'r':
485                         mop->mo_flags |= MO_FORCEFORMAT;
486                         break;
487                 case 't':
488                         if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
489                                 badopt(long_opt[longidx].name, "MDT,OST");
490                                 return 1;
491                         }
492
493                         if (!optarg)
494                                 return 1;
495
496                         rc = add_param(mop->mo_ldd.ldd_params,
497                                        PARAM_NETWORK, optarg);
498                         if (rc != 0)
499                                 return rc;
500                         /* Must update the mgs logs */
501                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
502                         break;
503                 case 'u':
504                         strscpy(mop->mo_ldd.ldd_userdata, optarg,
505                                 sizeof(mop->mo_ldd.ldd_userdata));
506                         break;
507                 case 'v':
508                         verbose++;
509                         break;
510                 case 'w':
511                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
512                         break;
513                 case 'U':
514                         upgrade_to_18 = 1;
515                         break;
516                 default:
517                         if (opt != '?') {
518                                 fatal();
519                                 fprintf(stderr, "Unknown option '%c'\n", opt);
520                         }
521                         return EINVAL;
522                 }
523         }//while
524
525         if (optind == argc) {
526                 /* The user didn't specify device name */
527                 fatal();
528                 fprintf(stderr, "Not enough arguments - device name or "
529                         "pool/dataset name not specified.\n");
530                 return EINVAL;
531         } else {
532                 /*  The device or pool/filesystem name */
533                 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
534
535                 /* Followed by optional vdevs */
536                 if (optind < argc - 1)
537                         mop->mo_pool_vdevs = (char **) &argv[optind + 1];
538         }
539
540         return 0;
541 }
542
543 int main(int argc, char *const argv[])
544 {
545         struct mkfs_opts mop;
546         struct lustre_disk_data *ldd;
547         char *mountopts = NULL;
548         char always_mountopts[512] = "";
549         char default_mountopts[512] = "";
550         unsigned mount_type;
551         int ret = 0;
552
553         if ((progname = strrchr(argv[0], '/')) != NULL)
554                 progname++;
555         else
556                 progname = argv[0];
557
558         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
559                 usage(stderr);
560                 return(EINVAL);
561         }
562
563         memset(&mop, 0, sizeof(mop));
564         set_defaults(&mop);
565
566         /* device is last arg */
567         strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
568
569 #ifdef TUNEFS
570         /* For tunefs, we must read in the old values before parsing any
571            new ones. */
572
573         /* Check whether the disk has already been formatted by mkfs.lustre */
574         ret = osd_is_lustre(mop.mo_device, &mount_type);
575         if (ret == 0) {
576                 fatal();
577                 fprintf(stderr, "Device %s has not been formatted with "
578                         "mkfs.lustre\n", mop.mo_device);
579                 ret = ENODEV;
580                 goto out;
581         }
582
583         ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
584         if (ret) {
585                 fatal();
586                 fprintf(stderr, "Failed to read previous Lustre data from %s "
587                         "(%d)\n", mop.mo_device, ret);
588                 goto out;
589         }
590         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
591             mop.mo_mgs_failnodes++;
592
593         if (verbose > 0)
594                 print_ldd("Read previous values", &(mop.mo_ldd));
595 #endif
596
597         ret = osd_init();
598         if (ret)
599                 return ret;
600
601         ret = parse_opts(argc, argv, &mop, &mountopts);
602         if (ret)
603                 goto out;
604
605         ldd = &mop.mo_ldd;
606
607         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
608                 fatal();
609                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
610                 ret = EINVAL;
611                 goto out;
612         }
613
614         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
615                 fatal();
616                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
617                 ret = EINVAL;
618                 goto out;
619         }
620
621         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
622             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
623                 fatal();
624                 fprintf(stderr, "Can't find the target index, "
625                         "specify with --index\n");
626                 ret = EINVAL;
627                 goto out;
628         }
629 #if 0
630         /*
631          * Comment out these 2 checks temporarily, since for multi-MDSes
632          * in single node only 1 mds node could have mgs service
633          */
634         if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
635                 verrprint("No management node specified, adding MGS to this "
636                           "MDT\n");
637                 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
638         }
639         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
640                 fatal();
641                 if (IS_MDT(ldd))
642                         fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
643                 else
644                         fprintf(stderr, "Must specify --mgsnode=\n");
645                 ret = EINVAL;
646                 goto out;
647         }
648 #endif
649         if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
650                 fatal();
651                 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
652                 ret = EINVAL;
653                 goto out;
654         }
655
656         /* These are the permanent mount options (always included) */
657         ret = osd_prepare_lustre(&mop,
658                                  default_mountopts, sizeof(default_mountopts),
659                                  always_mountopts, sizeof(always_mountopts));
660         if (ret) {
661                 fatal();
662                 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
663                 goto out;
664         }
665
666         if (mountopts) {
667                 trim_mountfsoptions(mountopts);
668                 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
669                 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
670                         ret = EINVAL;
671                         goto out;
672                 }
673                 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
674         } else {
675 #ifdef TUNEFS
676                 if (ldd->ldd_mount_opts[0] == 0)
677                         /* use the defaults unless old opts exist */
678 #endif
679                 {
680                         sprintf(ldd->ldd_mount_opts, "%s%s",
681                                 always_mountopts, default_mountopts);
682                         trim_mountfsoptions(ldd->ldd_mount_opts);
683                 }
684         }
685
686         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
687                          ldd->ldd_fsname, ldd->ldd_svname);
688
689         if (verbose >= 0)
690                 print_ldd("Permanent disk data", ldd);
691
692         if (print_only) {
693                 printf("exiting before disk write.\n");
694                 goto out;
695         }
696
697         if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
698                 return(EEXIST);
699
700         /* Create the loopback file */
701         if (mop.mo_flags & MO_IS_LOOP) {
702                 ret = access(mop.mo_device, F_OK);
703                 if (ret)
704                         ret = errno;
705 #ifndef TUNEFS /* mkfs.lustre */
706                 /* Reformat the loopback file */
707                 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
708                         ret = loop_format(&mop);
709                         if (ret)
710                                 goto out;
711                 }
712 #endif
713                 if (ret == 0)
714                         ret = loop_setup(&mop);
715                 if (ret) {
716                         fatal();
717                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
718                                 mop.mo_device, strerror(ret));
719                         goto out;
720                 }
721         }
722
723 #ifndef TUNEFS /* mkfs.lustre */
724         /* Check whether the disk has already been formatted by mkfs.lustre */
725         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
726                 ret = osd_is_lustre(mop.mo_device, &mount_type);
727                 if (ret) {
728                         fatal();
729                         fprintf(stderr, "Device %s was previously formatted "
730                                 "for lustre. Use --reformat to reformat it, "
731                                 "or tunefs.lustre to modify.\n",
732                                 mop.mo_device);
733                         goto out;
734                 }
735         }
736
737         /* Format the backing filesystem */
738         ret = osd_make_lustre(&mop);
739         if (ret != 0) {
740                 fatal();
741                 fprintf(stderr, "mkfs failed %d\n", ret);
742                 goto out;
743         }
744 #endif
745
746         /* Write our config files */
747         ret = osd_write_ldd(&mop);
748         if (ret != 0) {
749                 fatal();
750                 fprintf(stderr, "failed to write local files\n");
751                 goto out;
752         }
753
754 out:
755         loop_cleanup(&mop);
756         osd_fini();
757
758         /* Fix any crazy return values from system() */
759         if (ret && ((ret & 255) == 0))
760                 return (1);
761         if (ret)
762                 verrprint("%s: exiting with %d (%s)\n",
763                           progname, ret, strerror(ret));
764         return (ret);
765 }