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