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, 2012, Intel Corporation.
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 */
50 #include "mount_utils.h"
58 #include <sys/types.h>
60 #include <sys/mount.h>
61 #include <sys/utsname.h>
68 #include <lustre_disk.h>
69 #include <lustre_param.h>
70 #include <lnet/lnetctl.h>
71 #include <lustre_ver.h>
79 static int print_only = 0;
80 static int upgrade_to_18 = 0;
82 #ifdef HAVE_LDISKFS_OSD
83 #define FSLIST_LDISKFS "ldiskfs"
86 #define FSLIST_LDISKFS ""
87 #endif /* HAVE_LDISKFS_OSD */
90 #define FSLIST_ZFS "|zfs"
92 #define FSLIST_ZFS "zfs"
97 #endif /* HAVE_ZFS_OSD */
100 #error "no backing OSD types (ldiskfs or ZFS) are configured"
103 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
105 void usage(FILE *out)
107 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
108 fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
109 "--fsname=<filesystem name>\n"
110 "\t--index=<target index> [options] <device>\n", progname);
112 fprintf(out, "usage: %s <target type> --backfstype=zfs "
113 "--fsname=<filesystem name> [options]\n"
114 "\t<pool name>/<dataset name>\n"
115 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
119 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
121 "\t<pool name>: name of ZFS pool where target is created "
123 "\t<dataset name>: name of new dataset, must be unique within "
125 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
130 "\t\t--mgs: configuration management service\n"
131 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
132 "\t\t--ost: object storage, mutually exclusive with mdt, mgs\n"
133 "\toptions (in order of popularity):\n"
134 "\t\t--index=#N: numerical target index (0..N)\n"
135 "\t\t\trequired for all targets other than the MGS\n"
136 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
137 "\t\t\trequired for all targets other than MGS\n"
138 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
139 "\t\t\trequired for all targets other than MGS\n"
140 "\t\t--mountfsoptions=<opts>: permanent mount options\n"
141 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
142 "\t\t\tmutually exclusive with --servicenode\n"
143 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
144 "\t\t\ttreat nodes as equal service node, mutually exclusive "
146 "\t\t--param <key>=<value>: set a permanent parameter\n"
147 "\t\t\te.g. --param sys.timeout=40\n"
148 "\t\t\t --param lov.stripesize=2M\n"
149 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
151 "\t\t--backfstype=<fstype>: backing fs type (ext3, ldiskfs)\n"
152 "\t\t--device-size=#N(KB): device size for loop devices\n"
153 "\t\t--mkfsoptions=<opts>: format options\n"
154 "\t\t--reformat: overwrite an existing disk\n"
155 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
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"
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"
166 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
170 /* ==================== Lustre config functions =============*/
172 void print_ldd(char *str, struct lustre_disk_data *ldd)
174 printf("\n %s:\n", str);
175 printf("Target: %s\n", ldd->ldd_svname);
176 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
177 printf("Index: unassigned\n");
179 printf("Index: %d\n", ldd->ldd_svindex);
180 if (ldd->ldd_uuid[0])
181 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
182 printf("Lustre FS: %s\n", ldd->ldd_fsname);
183 printf("Mount type: %s\n", MT_STR(ldd));
184 printf("Flags: %#x\n", ldd->ldd_flags);
185 printf(" (%s%s%s%s%s%s%s%s%s)\n",
186 IS_MDT(ldd) ? "MDT ":"",
187 IS_OST(ldd) ? "OST ":"",
188 IS_MGS(ldd) ? "MGS ":"",
189 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
190 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
191 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
192 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
193 ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
194 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
195 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
196 printf("Parameters:%s\n", ldd->ldd_params);
197 if (ldd->ldd_userdata[0])
198 printf("Comment: %s\n", ldd->ldd_userdata);
202 void set_defaults(struct mkfs_opts *mop)
204 mop->mo_ldd.ldd_magic = LDD_MAGIC;
205 mop->mo_ldd.ldd_config_ver = 1;
206 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
207 #ifdef HAVE_LDISKFS_OSD
208 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
210 mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
212 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
213 mop->mo_mgs_failnodes = 0;
214 mop->mo_stripe_count = 1;
215 mop->mo_pool_vdevs = NULL;
218 static inline void badopt(const char *opt, char *type)
220 fprintf(stderr, "%s: '--%s' only valid for %s\n",
221 progname, opt, type);
225 /* from mount_lustre */
226 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
227 #define MAXNIDSTR 1024
228 static char *convert_hostnames(char *s1)
230 char *converted, *s2 = 0, *c, *end, sep;
231 int left = MAXNIDSTR;
234 converted = malloc(left);
235 if (converted == NULL) {
239 end = s1 + strlen(s1);
241 while ((left > 0) && (s1 < end)) {
242 s2 = strpbrk(s1, ",:");
247 nid = libcfs_str2nid(s1);
250 if (nid == LNET_NID_ANY) {
251 fprintf(stderr, "%s: Can't parse NID '%s'\n",
256 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
257 strlen("127.0.0.1")) == 0) {
258 fprintf(stderr, "%s: The NID '%s' resolves to the "
259 "loopback address '%s'. Lustre requires a "
260 "non-loopback address.\n",
261 progname, s1, libcfs_nid2str(nid));
266 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
267 left = converted + MAXNIDSTR - c;
273 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
276 static struct option long_opt[] = {
277 {"backfstype", 1, 0, 'b'},
278 {"stripe-count-hint", 1, 0, 'c'},
279 {"comment", 1, 0, 'u'},
280 {"configdev", 1, 0, 'C'},
281 {"device-size", 1, 0, 'd'},
282 {"dryrun", 0, 0, 'n'},
283 {"erase-params", 0, 0, 'e'},
284 {"failnode", 1, 0, 'f'},
285 {"failover", 1, 0, 'f'},
288 {"index", 1, 0, 'i'},
289 {"mkfsoptions", 1, 0, 'k'},
290 {"mgsnode", 1, 0, 'm'},
291 {"mgsnid", 1, 0, 'm'},
293 {"fsname",1, 0, 'L'},
294 {"noformat", 0, 0, 'n'},
295 {"nomgs", 0, 0, 'N'},
296 {"mountfsoptions", 1, 0, 'o'},
298 {"param", 1, 0, 'p'},
299 {"print", 0, 0, 'n'},
300 {"quiet", 0, 0, 'q'},
301 {"reformat", 0, 0, 'r'},
302 {"servicenode", 1, 0, 's'},
303 {"verbose", 0, 0, 'v'},
304 {"writeconf", 0, 0, 'w'},
305 {"upgrade_to_18", 0, 0, 'U'},
306 {"network", 1, 0, 't'},
307 {"quota", 0, 0, 'Q'},
310 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrs:t:Uu:vw";
313 int failnode_set = 0, servicenode_set = 0;
315 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
320 while (i < LDD_MT_LAST) {
321 if (strcmp(optarg, mt_str(i)) == 0) {
322 mop->mo_ldd.ldd_mount_type = i;
327 if (i == LDD_MT_LAST) {
328 fprintf(stderr, "%s: invalid backend filesystem"
329 " type %s\n", progname, optarg);
335 if (IS_MDT(&mop->mo_ldd)) {
336 int stripe_count = atol(optarg);
337 if (stripe_count <= 0) {
338 fprintf(stderr, "%s: bad stripe count "
339 "%d\n", progname, stripe_count);
342 mop->mo_stripe_count = stripe_count;
344 badopt(long_opt[longidx].name, "MDT");
348 case 'C': /* Configdev */
350 printf("Configdev not implemented\n");
353 mop->mo_device_sz = atol(optarg);
356 mop->mo_ldd.ldd_params[0] = '\0';
357 /* Must update the mgs logs */
358 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
364 if ((opt == 'f' && servicenode_set)
365 || (opt == 's' && failnode_set)) {
366 fprintf(stderr, "%s: %s cannot use with --%s\n",
367 progname, long_opt[longidx].name,
368 opt == 'f' ? "servicenode" : "failnode");
372 nids = convert_hostnames(optarg);
375 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
380 /* Must update the mgs logs */
381 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
385 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
388 mop->mo_flags |= MO_FAILOVER;
392 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
398 if (!(mop->mo_ldd.ldd_flags &
399 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
401 fprintf(stderr, "%s: cannot change the index of"
402 " a registered target\n", progname);
405 /* LU-2374: check whether it is OST/MDT later */
406 mop->mo_ldd.ldd_svindex = atol(optarg);
407 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
410 strscpy(mop->mo_mkfsopts, optarg,
411 sizeof(mop->mo_mkfsopts));
415 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
416 (!(mop->mo_ldd.ldd_flags &
417 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
418 LDD_F_WRITECONF)))) {
419 fprintf(stderr, "%s: cannot change the name of"
420 " a registered target\n", progname);
423 if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
424 fprintf(stderr, "%s: filesystem name must be "
425 "1-8 chars\n", progname);
428 if ((tmp = strpbrk(optarg, "/:"))) {
429 fprintf(stderr, "%s: char '%c' not allowed in "
430 "filesystem name\n", progname, *tmp);
433 strscpy(mop->mo_ldd.ldd_fsname, optarg,
434 sizeof(mop->mo_ldd.ldd_fsname));
438 char *nids = convert_hostnames(optarg);
441 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
446 mop->mo_mgs_failnodes++;
450 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
456 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
462 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
465 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
468 /* Must update the mgs logs */
469 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
475 mop->mo_flags |= MO_FORCEFORMAT;
478 if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
479 badopt(long_opt[longidx].name, "MDT,OST");
486 rc = add_param(mop->mo_ldd.ldd_params,
487 PARAM_NETWORK, optarg);
490 /* Must update the mgs logs */
491 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
494 strscpy(mop->mo_ldd.ldd_userdata, optarg,
495 sizeof(mop->mo_ldd.ldd_userdata));
501 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
507 mop->mo_flags |= MO_QUOTA;
512 fprintf(stderr, "Unknown option '%c'\n", opt);
518 if (optind == argc) {
519 /* The user didn't specify device name */
521 fprintf(stderr, "Not enough arguments - device name or "
522 "pool/dataset name not specified.\n");
525 /* The device or pool/filesystem name */
526 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
528 /* Followed by optional vdevs */
529 if (optind < argc - 1)
530 mop->mo_pool_vdevs = (char **) &argv[optind + 1];
536 int main(int argc, char *const argv[])
538 struct mkfs_opts mop;
539 struct lustre_disk_data *ldd;
540 char *mountopts = NULL;
541 char always_mountopts[512] = "";
542 char default_mountopts[512] = "";
546 if ((progname = strrchr(argv[0], '/')) != NULL)
551 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
556 memset(&mop, 0, sizeof(mop));
559 /* device is last arg */
560 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
567 /* For tunefs, we must read in the old values before parsing any
570 /* Check whether the disk has already been formatted by mkfs.lustre */
571 ret = osd_is_lustre(mop.mo_device, &mount_type);
574 fprintf(stderr, "Device %s has not been formatted with "
575 "mkfs.lustre\n", mop.mo_device);
579 mop.mo_ldd.ldd_mount_type = mount_type;
581 ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
584 fprintf(stderr, "Failed to read previous Lustre data from %s "
585 "(%d)\n", mop.mo_device, ret);
588 mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
590 /* svname of the form lustre:OST1234 means never registered */
591 ret = strlen(mop.mo_ldd.ldd_svname);
592 if (mop.mo_ldd.ldd_svname[ret - 8] == ':') {
593 mop.mo_ldd.ldd_svname[ret - 8] = '-';
594 mop.mo_ldd.ldd_flags |= LDD_F_VIRGIN;
595 } else if (mop.mo_ldd.ldd_svname[ret - 8] == '=') {
596 mop.mo_ldd.ldd_svname[ret - 8] = '-';
597 mop.mo_ldd.ldd_flags |= LDD_F_WRITECONF;
600 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
601 mop.mo_mgs_failnodes++;
604 print_ldd("Read previous values", &(mop.mo_ldd));
607 ret = parse_opts(argc, argv, &mop, &mountopts);
613 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
615 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
620 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
622 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
627 /* Stand alone MGS doesn't need a index */
628 if (!IS_MDT(ldd) && IS_MGS(ldd)) {
629 /* But if --index was specified flag an error */
630 if (!(mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)) {
631 badopt("--index", "MDT,OST");
634 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
637 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
638 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
640 fprintf(stderr, "Can't find the target index, "
641 "specify with --index\n");
646 if (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)
647 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
648 "target index must be specified with --index\n",
651 /* If no index is supplied for MDT by default set index to zero */
652 if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
653 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
654 mop.mo_ldd.ldd_svindex = 0;
659 * Comment out these 2 checks temporarily, since for multi-MDSes
660 * in single node only 1 mds node could have mgs service
662 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
663 verrprint("No management node specified, adding MGS to this "
665 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
667 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
670 fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
672 fprintf(stderr, "Must specify --mgsnode=\n");
677 if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
679 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
684 /* These are the permanent mount options (always included) */
685 ret = osd_prepare_lustre(&mop,
686 default_mountopts, sizeof(default_mountopts),
687 always_mountopts, sizeof(always_mountopts));
690 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
695 trim_mountfsoptions(mountopts);
696 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
697 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
701 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
704 if (ldd->ldd_mount_opts[0] == 0)
705 /* use the defaults unless old opts exist */
708 sprintf(ldd->ldd_mount_opts, "%s%s",
709 always_mountopts, default_mountopts);
710 trim_mountfsoptions(ldd->ldd_mount_opts);
714 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
715 ldd->ldd_fsname, ldd->ldd_svname);
718 print_ldd("Permanent disk data", ldd);
721 printf("exiting before disk write.\n");
725 if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
728 /* Create the loopback file */
729 if (mop.mo_flags & MO_IS_LOOP) {
730 ret = access(mop.mo_device, F_OK);
733 #ifndef TUNEFS /* mkfs.lustre */
734 /* Reformat the loopback file */
735 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
736 ret = loop_format(&mop);
742 ret = loop_setup(&mop);
745 fprintf(stderr, "Loop device setup for %s failed: %s\n",
746 mop.mo_device, strerror(ret));
751 #ifndef TUNEFS /* mkfs.lustre */
752 /* Check whether the disk has already been formatted by mkfs.lustre */
753 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
754 ret = osd_is_lustre(mop.mo_device, &mount_type);
757 fprintf(stderr, "Device %s was previously formatted "
758 "for lustre. Use --reformat to reformat it, "
759 "or tunefs.lustre to modify.\n",
765 /* Format the backing filesystem */
766 ret = osd_make_lustre(&mop);
769 fprintf(stderr, "mkfs failed %d\n", ret);
773 /* update svname with '=' to refresh config */
774 if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
775 struct mount_opts opts;
776 opts.mo_ldd = mop.mo_ldd;
777 opts.mo_source = mop.mo_device;
778 (void) osd_label_lustre(&opts);
781 /* Enable quota accounting */
782 if (mop.mo_flags & MO_QUOTA) {
783 ret = osd_enable_quota(&mop);
789 /* Write our config files */
790 ret = osd_write_ldd(&mop);
793 fprintf(stderr, "failed to write local files\n");
801 /* Fix any crazy return values from system() */
802 if (ret && ((ret & 255) == 0))
805 verrprint("%s: exiting with %d (%s)\n",
806 progname, ret, strerror(ret));