4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/mkfs_lustre.c
38 * Author: Nathan Rutman <nathan@clusterfs.com>
41 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
45 #endif /* HAVE_CONFIG_H */
57 #include <sys/types.h>
59 #include <sys/mount.h>
60 #include <sys/utsname.h>
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>
72 #include <linux/fs.h> /* for BLKGETSIZE64 */
74 #include <linux/version.h>
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"
88 static int print_only = 0;
89 static int upgrade_to_18 = 0;
91 #ifdef HAVE_LDISKFS_OSD
92 #define FSLIST_LDISKFS "ldiskfs"
95 #define FSLIST_LDISKFS ""
96 #endif /* HAVE_LDISKFS_OSD */
99 #define FSLIST_ZFS "|zfs"
101 #define FSLIST_ZFS "zfs"
105 #define FSLIST_ZFS ""
106 #endif /* HAVE_ZFS_OSD */
109 #error "no backing OSD types (ldiskfs or ZFS) are configured"
112 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
114 void usage(FILE *out)
116 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
117 fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
118 "--fsname=<filesystem name>\n"
119 "\t--index=<target index> [options] <device>\n", progname);
121 fprintf(out, "usage: %s <target type> --backfstype=zfs "
122 "--fsname=<filesystem name> [options]\n"
123 "\t<pool name>/<dataset name>\n"
124 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
128 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
130 "\t<pool name>: name of ZFS pool where target is created "
132 "\t<dataset name>: name of new dataset, must be unique within "
134 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
139 "\t\t--mgs: configuration management service\n"
140 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
141 "\t\t--ost: object storage, mutually exclusive with mdt, mgs\n"
142 "\toptions (in order of popularity):\n"
143 "\t\t--index=#N: numerical target index (0..N)\n"
144 "\t\t\trequired for all targets other than the MGS\n"
145 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
146 "\t\t\trequired for all targets other than MGS\n"
147 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
148 "\t\t\trequired for all targets other than MGS\n"
149 "\t\t--mountfsoptions=<opts>: permanent mount options\n"
150 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
151 "\t\t\tmutually exclusive with --servicenode\n"
152 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
153 "\t\t\ttreat nodes as equal service node, mutually exclusive "
155 "\t\t--param <key>=<value>: set a permanent parameter\n"
156 "\t\t\te.g. --param sys.timeout=40\n"
157 "\t\t\t --param lov.stripesize=2M\n"
158 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
160 "\t\t--backfstype=<fstype>: backing fs type (ext3, ldiskfs)\n"
161 "\t\t--device-size=#N(KB): device size for loop devices\n"
162 "\t\t--mkfsoptions=<opts>: format options\n"
163 "\t\t--reformat: overwrite an existing disk\n"
164 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
166 "\t\t--erase-params: erase all old parameter settings\n"
167 "\t\t--nomgs: turn off MGS service on this MDT\n"
168 "\t\t--writeconf: erase all config logs for this fs.\n"
169 "\t\t--quota: enable space accounting on old 2.x device.\n"
171 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
172 "\t\t--dryrun: report what we would do; don't write to disk\n"
173 "\t\t--verbose: e.g. show mkfs progress\n"
175 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
179 /* ==================== Lustre config functions =============*/
181 void print_ldd(char *str, struct lustre_disk_data *ldd)
183 printf("\n %s:\n", str);
184 printf("Target: %s\n", ldd->ldd_svname);
185 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
186 printf("Index: unassigned\n");
188 printf("Index: %d\n", ldd->ldd_svindex);
189 if (ldd->ldd_uuid[0])
190 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
191 printf("Lustre FS: %s\n", ldd->ldd_fsname);
192 printf("Mount type: %s\n", MT_STR(ldd));
193 printf("Flags: %#x\n", ldd->ldd_flags);
194 printf(" (%s%s%s%s%s%s%s%s%s%s)\n",
195 IS_MDT(ldd) ? "MDT ":"",
196 IS_OST(ldd) ? "OST ":"",
197 IS_MGS(ldd) ? "MGS ":"",
198 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
199 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
200 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
201 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
202 ldd->ldd_flags & LDD_F_IAM_DIR ? "IAM_dir_format ":"",
203 ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
204 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
205 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
206 printf("Parameters:%s\n", ldd->ldd_params);
207 if (ldd->ldd_userdata[0])
208 printf("Comment: %s\n", ldd->ldd_userdata);
212 void set_defaults(struct mkfs_opts *mop)
214 mop->mo_ldd.ldd_magic = LDD_MAGIC;
215 mop->mo_ldd.ldd_config_ver = 1;
216 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
217 #ifdef HAVE_LDISKFS_OSD
218 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
220 mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
222 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
223 mop->mo_mgs_failnodes = 0;
224 mop->mo_stripe_count = 1;
225 mop->mo_pool_vdevs = NULL;
228 static inline void badopt(const char *opt, char *type)
230 fprintf(stderr, "%s: '--%s' only valid for %s\n",
231 progname, opt, type);
235 /* from mount_lustre */
236 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
237 #define MAXNIDSTR 1024
238 static char *convert_hostnames(char *s1)
240 char *converted, *s2 = 0, *c, *end, sep;
241 int left = MAXNIDSTR;
244 converted = malloc(left);
245 if (converted == NULL) {
249 end = s1 + strlen(s1);
251 while ((left > 0) && (s1 < end)) {
252 s2 = strpbrk(s1, ",:");
257 nid = libcfs_str2nid(s1);
260 if (nid == LNET_NID_ANY) {
261 fprintf(stderr, "%s: Can't parse NID '%s'\n",
266 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
267 strlen("127.0.0.1")) == 0) {
268 fprintf(stderr, "%s: The NID '%s' resolves to the "
269 "loopback address '%s'. Lustre requires a "
270 "non-loopback address.\n",
271 progname, s1, libcfs_nid2str(nid));
276 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
277 left = converted + MAXNIDSTR - c;
283 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
286 static struct option long_opt[] = {
287 {"iam-dir", 0, 0, 'a'},
288 {"backfstype", 1, 0, 'b'},
289 {"stripe-count-hint", 1, 0, 'c'},
290 {"comment", 1, 0, 'u'},
291 {"configdev", 1, 0, 'C'},
292 {"device-size", 1, 0, 'd'},
293 {"dryrun", 0, 0, 'n'},
294 {"erase-params", 0, 0, 'e'},
295 {"failnode", 1, 0, 'f'},
296 {"failover", 1, 0, 'f'},
299 {"index", 1, 0, 'i'},
300 {"mkfsoptions", 1, 0, 'k'},
301 {"mgsnode", 1, 0, 'm'},
302 {"mgsnid", 1, 0, 'm'},
304 {"fsname",1, 0, 'L'},
305 {"noformat", 0, 0, 'n'},
306 {"nomgs", 0, 0, 'N'},
307 {"mountfsoptions", 1, 0, 'o'},
309 {"param", 1, 0, 'p'},
310 {"print", 0, 0, 'n'},
311 {"quiet", 0, 0, 'q'},
312 {"reformat", 0, 0, 'r'},
313 {"servicenode", 1, 0, 's'},
314 {"verbose", 0, 0, 'v'},
315 {"writeconf", 0, 0, 'w'},
316 {"upgrade_to_18", 0, 0, 'U'},
317 {"network", 1, 0, 't'},
318 {"quota", 0, 0, 'Q'},
321 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrs:t:Uu:vw";
324 int failnode_set = 0, servicenode_set = 0;
326 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
330 if (IS_MDT(&mop->mo_ldd))
331 mop->mo_ldd.ldd_flags |= LDD_F_IAM_DIR;
336 while (i < LDD_MT_LAST) {
337 if (strcmp(optarg, mt_str(i)) == 0) {
338 mop->mo_ldd.ldd_mount_type = i;
343 if (i == LDD_MT_LAST) {
344 fprintf(stderr, "%s: invalid backend filesystem"
345 " type %s\n", progname, optarg);
351 if (IS_MDT(&mop->mo_ldd)) {
352 int stripe_count = atol(optarg);
353 if (stripe_count <= 0) {
354 fprintf(stderr, "%s: bad stripe count "
355 "%d\n", progname, stripe_count);
358 mop->mo_stripe_count = stripe_count;
360 badopt(long_opt[longidx].name, "MDT");
364 case 'C': /* Configdev */
366 printf("Configdev not implemented\n");
369 mop->mo_device_sz = atol(optarg);
372 mop->mo_ldd.ldd_params[0] = '\0';
373 /* Must update the mgs logs */
374 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
380 if ((opt == 'f' && servicenode_set)
381 || (opt == 's' && failnode_set)) {
382 fprintf(stderr, "%s: %s cannot use with --%s\n",
383 progname, long_opt[longidx].name,
384 opt == 'f' ? "servicenode" : "failnode");
388 nids = convert_hostnames(optarg);
391 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
396 /* Must update the mgs logs */
397 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
401 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
404 mop->mo_flags |= MO_FAILOVER;
408 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
414 if (!(mop->mo_ldd.ldd_flags &
415 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
417 fprintf(stderr, "%s: cannot change the index of"
418 " a registered target\n", progname);
421 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
422 mop->mo_ldd.ldd_svindex = atol(optarg);
423 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
425 badopt(long_opt[longidx].name, "MDT,OST");
430 strscpy(mop->mo_mkfsopts, optarg,
431 sizeof(mop->mo_mkfsopts));
435 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
436 (!(mop->mo_ldd.ldd_flags &
437 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
438 LDD_F_WRITECONF)))) {
439 fprintf(stderr, "%s: cannot change the name of"
440 " a registered target\n", progname);
443 if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
444 fprintf(stderr, "%s: filesystem name must be "
445 "1-8 chars\n", progname);
448 if ((tmp = strpbrk(optarg, "/:"))) {
449 fprintf(stderr, "%s: char '%c' not allowed in "
450 "filesystem name\n", progname, *tmp);
453 strscpy(mop->mo_ldd.ldd_fsname, optarg,
454 sizeof(mop->mo_ldd.ldd_fsname));
458 char *nids = convert_hostnames(optarg);
461 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
466 mop->mo_mgs_failnodes++;
470 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
476 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
482 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
485 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
488 /* Must update the mgs logs */
489 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
495 mop->mo_flags |= MO_FORCEFORMAT;
498 if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
499 badopt(long_opt[longidx].name, "MDT,OST");
506 rc = add_param(mop->mo_ldd.ldd_params,
507 PARAM_NETWORK, optarg);
510 /* Must update the mgs logs */
511 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
514 strscpy(mop->mo_ldd.ldd_userdata, optarg,
515 sizeof(mop->mo_ldd.ldd_userdata));
521 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
527 mop->mo_flags |= MO_QUOTA;
532 fprintf(stderr, "Unknown option '%c'\n", opt);
538 if (optind == argc) {
539 /* The user didn't specify device name */
541 fprintf(stderr, "Not enough arguments - device name or "
542 "pool/dataset name not specified.\n");
545 /* The device or pool/filesystem name */
546 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
548 /* Followed by optional vdevs */
549 if (optind < argc - 1)
550 mop->mo_pool_vdevs = (char **) &argv[optind + 1];
556 int main(int argc, char *const argv[])
558 struct mkfs_opts mop;
559 struct lustre_disk_data *ldd;
560 char *mountopts = NULL;
561 char always_mountopts[512] = "";
562 char default_mountopts[512] = "";
566 if ((progname = strrchr(argv[0], '/')) != NULL)
571 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
576 memset(&mop, 0, sizeof(mop));
579 /* device is last arg */
580 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
583 /* For tunefs, we must read in the old values before parsing any
586 /* Check whether the disk has already been formatted by mkfs.lustre */
587 ret = osd_is_lustre(mop.mo_device, &mount_type);
590 fprintf(stderr, "Device %s has not been formatted with "
591 "mkfs.lustre\n", mop.mo_device);
596 ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
599 fprintf(stderr, "Failed to read previous Lustre data from %s "
600 "(%d)\n", mop.mo_device, ret);
603 mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
605 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
606 mop.mo_mgs_failnodes++;
609 print_ldd("Read previous values", &(mop.mo_ldd));
616 ret = parse_opts(argc, argv, &mop, &mountopts);
622 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
624 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
629 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
631 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
636 /* Stand alone MGS doesn't need a index */
637 if (!IS_MDT(ldd) && IS_MGS(ldd))
638 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
640 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
641 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
643 fprintf(stderr, "Can't find the target index, "
644 "specify with --index\n");
649 if (IS_OST(ldd) && (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX))
650 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
651 "target index must be specified with --index\n",
656 * Comment out these 2 checks temporarily, since for multi-MDSes
657 * in single node only 1 mds node could have mgs service
659 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
660 verrprint("No management node specified, adding MGS to this "
662 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
664 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
667 fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
669 fprintf(stderr, "Must specify --mgsnode=\n");
674 if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
676 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
681 /* These are the permanent mount options (always included) */
682 ret = osd_prepare_lustre(&mop,
683 default_mountopts, sizeof(default_mountopts),
684 always_mountopts, sizeof(always_mountopts));
687 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
692 trim_mountfsoptions(mountopts);
693 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
694 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
698 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
701 if (ldd->ldd_mount_opts[0] == 0)
702 /* use the defaults unless old opts exist */
705 sprintf(ldd->ldd_mount_opts, "%s%s",
706 always_mountopts, default_mountopts);
707 trim_mountfsoptions(ldd->ldd_mount_opts);
711 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
712 ldd->ldd_fsname, ldd->ldd_svname);
715 print_ldd("Permanent disk data", ldd);
718 printf("exiting before disk write.\n");
722 if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
725 /* Create the loopback file */
726 if (mop.mo_flags & MO_IS_LOOP) {
727 ret = access(mop.mo_device, F_OK);
730 #ifndef TUNEFS /* mkfs.lustre */
731 /* Reformat the loopback file */
732 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
733 ret = loop_format(&mop);
739 ret = loop_setup(&mop);
742 fprintf(stderr, "Loop device setup for %s failed: %s\n",
743 mop.mo_device, strerror(ret));
748 #ifndef TUNEFS /* mkfs.lustre */
749 /* Check whether the disk has already been formatted by mkfs.lustre */
750 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
751 ret = osd_is_lustre(mop.mo_device, &mount_type);
754 fprintf(stderr, "Device %s was previously formatted "
755 "for lustre. Use --reformat to reformat it, "
756 "or tunefs.lustre to modify.\n",
762 /* Format the backing filesystem */
763 ret = osd_make_lustre(&mop);
766 fprintf(stderr, "mkfs failed %d\n", ret);
770 /* update svname with '=' to refresh config */
771 if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
772 struct mount_opts opts;
773 opts.mo_ldd = mop.mo_ldd;
774 opts.mo_source = mop.mo_device;
775 (void) osd_label_lustre(&opts);
778 /* Enable quota accounting */
779 if (mop.mo_flags & MO_QUOTA) {
780 ret = osd_enable_quota(&mop);
786 /* Write our config files */
787 ret = osd_write_ldd(&mop);
790 fprintf(stderr, "failed to write local files\n");
798 /* Fix any crazy return values from system() */
799 if (ret && ((ret & 255) == 0))
802 verrprint("%s: exiting with %d (%s)\n",
803 progname, ret, strerror(ret));