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