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