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