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"
170 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
171 "\t\t--dryrun: report what we would do; don't write to disk\n"
172 "\t\t--verbose: e.g. show mkfs progress\n"
174 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
178 /* ==================== Lustre config functions =============*/
180 void print_ldd(char *str, struct lustre_disk_data *ldd)
182 printf("\n %s:\n", str);
183 printf("Target: %s\n", ldd->ldd_svname);
184 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
185 printf("Index: unassigned\n");
187 printf("Index: %d\n", ldd->ldd_svindex);
188 if (ldd->ldd_uuid[0])
189 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
190 printf("Lustre FS: %s\n", ldd->ldd_fsname);
191 printf("Mount type: %s\n", MT_STR(ldd));
192 printf("Flags: %#x\n", ldd->ldd_flags);
193 printf(" (%s%s%s%s%s%s%s%s%s%s)\n",
194 IS_MDT(ldd) ? "MDT ":"",
195 IS_OST(ldd) ? "OST ":"",
196 IS_MGS(ldd) ? "MGS ":"",
197 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
198 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
199 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
200 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
201 ldd->ldd_flags & LDD_F_IAM_DIR ? "IAM_dir_format ":"",
202 ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
203 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
204 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
205 printf("Parameters:%s\n", ldd->ldd_params);
206 if (ldd->ldd_userdata[0])
207 printf("Comment: %s\n", ldd->ldd_userdata);
211 void set_defaults(struct mkfs_opts *mop)
213 mop->mo_ldd.ldd_magic = LDD_MAGIC;
214 mop->mo_ldd.ldd_config_ver = 1;
215 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
216 #ifdef HAVE_LDISKFS_OSD
217 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
219 mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
221 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
222 mop->mo_mgs_failnodes = 0;
223 mop->mo_stripe_count = 1;
224 mop->mo_pool_vdevs = NULL;
227 static inline void badopt(const char *opt, char *type)
229 fprintf(stderr, "%s: '--%s' only valid for %s\n",
230 progname, opt, type);
234 /* from mount_lustre */
235 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
236 #define MAXNIDSTR 1024
237 static char *convert_hostnames(char *s1)
239 char *converted, *s2 = 0, *c, *end, sep;
240 int left = MAXNIDSTR;
243 converted = malloc(left);
244 if (converted == NULL) {
248 end = s1 + strlen(s1);
250 while ((left > 0) && (s1 < end)) {
251 s2 = strpbrk(s1, ",:");
256 nid = libcfs_str2nid(s1);
259 if (nid == LNET_NID_ANY) {
260 fprintf(stderr, "%s: Can't parse NID '%s'\n",
265 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
266 strlen("127.0.0.1")) == 0) {
267 fprintf(stderr, "%s: The NID '%s' resolves to the "
268 "loopback address '%s'. Lustre requires a "
269 "non-loopback address.\n",
270 progname, s1, libcfs_nid2str(nid));
275 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
276 left = converted + MAXNIDSTR - c;
282 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
285 static struct option long_opt[] = {
286 {"iam-dir", 0, 0, 'a'},
287 {"backfstype", 1, 0, 'b'},
288 {"stripe-count-hint", 1, 0, 'c'},
289 {"comment", 1, 0, 'u'},
290 {"configdev", 1, 0, 'C'},
291 {"device-size", 1, 0, 'd'},
292 {"dryrun", 0, 0, 'n'},
293 {"erase-params", 0, 0, 'e'},
294 {"failnode", 1, 0, 'f'},
295 {"failover", 1, 0, 'f'},
298 {"index", 1, 0, 'i'},
299 {"mkfsoptions", 1, 0, 'k'},
300 {"mgsnode", 1, 0, 'm'},
301 {"mgsnid", 1, 0, 'm'},
303 {"fsname",1, 0, 'L'},
304 {"noformat", 0, 0, 'n'},
305 {"nomgs", 0, 0, 'N'},
306 {"mountfsoptions", 1, 0, 'o'},
308 {"param", 1, 0, 'p'},
309 {"print", 0, 0, 'n'},
310 {"quiet", 0, 0, 'q'},
311 {"reformat", 0, 0, 'r'},
312 {"servicenode", 1, 0, 's'},
313 {"verbose", 0, 0, 'v'},
314 {"writeconf", 0, 0, 'w'},
315 {"upgrade_to_18", 0, 0, 'U'},
316 {"network", 1, 0, 't'},
319 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrs:t:Uu:vw";
322 int failnode_set = 0, servicenode_set = 0;
324 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
328 if (IS_MDT(&mop->mo_ldd))
329 mop->mo_ldd.ldd_flags |= LDD_F_IAM_DIR;
334 while (i < LDD_MT_LAST) {
335 if (strcmp(optarg, mt_str(i)) == 0) {
336 mop->mo_ldd.ldd_mount_type = i;
341 if (i == LDD_MT_LAST) {
342 fprintf(stderr, "%s: invalid backend filesystem"
343 " type %s\n", progname, optarg);
349 if (IS_MDT(&mop->mo_ldd)) {
350 int stripe_count = atol(optarg);
351 if (stripe_count <= 0) {
352 fprintf(stderr, "%s: bad stripe count "
353 "%d\n", progname, stripe_count);
356 mop->mo_stripe_count = stripe_count;
358 badopt(long_opt[longidx].name, "MDT");
362 case 'C': /* Configdev */
364 printf("Configdev not implemented\n");
367 mop->mo_device_sz = atol(optarg);
370 mop->mo_ldd.ldd_params[0] = '\0';
371 /* Must update the mgs logs */
372 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
378 if ((opt == 'f' && servicenode_set)
379 || (opt == 's' && failnode_set)) {
380 fprintf(stderr, "%s: %s cannot use with --%s\n",
381 progname, long_opt[longidx].name,
382 opt == 'f' ? "servicenode" : "failnode");
386 nids = convert_hostnames(optarg);
389 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
394 /* Must update the mgs logs */
395 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
399 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
402 mop->mo_flags |= MO_FAILOVER;
406 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
412 if (!(mop->mo_ldd.ldd_flags &
413 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
415 fprintf(stderr, "%s: cannot change the index of"
416 " a registered target\n", progname);
419 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
420 mop->mo_ldd.ldd_svindex = atol(optarg);
421 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
423 badopt(long_opt[longidx].name, "MDT,OST");
428 strscpy(mop->mo_mkfsopts, optarg,
429 sizeof(mop->mo_mkfsopts));
433 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
434 (!(mop->mo_ldd.ldd_flags &
435 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
436 LDD_F_WRITECONF)))) {
437 fprintf(stderr, "%s: cannot change the name of"
438 " a registered target\n", progname);
441 if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
442 fprintf(stderr, "%s: filesystem name must be "
443 "1-8 chars\n", progname);
446 if ((tmp = strpbrk(optarg, "/:"))) {
447 fprintf(stderr, "%s: char '%c' not allowed in "
448 "filesystem name\n", progname, *tmp);
451 strscpy(mop->mo_ldd.ldd_fsname, optarg,
452 sizeof(mop->mo_ldd.ldd_fsname));
456 char *nids = convert_hostnames(optarg);
459 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
464 mop->mo_mgs_failnodes++;
468 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
474 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
480 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
483 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
486 /* Must update the mgs logs */
487 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
493 mop->mo_flags |= MO_FORCEFORMAT;
496 if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
497 badopt(long_opt[longidx].name, "MDT,OST");
504 rc = add_param(mop->mo_ldd.ldd_params,
505 PARAM_NETWORK, optarg);
508 /* Must update the mgs logs */
509 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
512 strscpy(mop->mo_ldd.ldd_userdata, optarg,
513 sizeof(mop->mo_ldd.ldd_userdata));
519 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
527 fprintf(stderr, "Unknown option '%c'\n", opt);
533 if (optind == argc) {
534 /* The user didn't specify device name */
536 fprintf(stderr, "Not enough arguments - device name or "
537 "pool/dataset name not specified.\n");
540 /* The device or pool/filesystem name */
541 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
543 /* Followed by optional vdevs */
544 if (optind < argc - 1)
545 mop->mo_pool_vdevs = (char **) &argv[optind + 1];
551 int main(int argc, char *const argv[])
553 struct mkfs_opts mop;
554 struct lustre_disk_data *ldd;
555 char *mountopts = NULL;
556 char always_mountopts[512] = "";
557 char default_mountopts[512] = "";
561 if ((progname = strrchr(argv[0], '/')) != NULL)
566 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
571 memset(&mop, 0, sizeof(mop));
574 /* device is last arg */
575 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
578 /* For tunefs, we must read in the old values before parsing any
581 /* Check whether the disk has already been formatted by mkfs.lustre */
582 ret = osd_is_lustre(mop.mo_device, &mount_type);
585 fprintf(stderr, "Device %s has not been formatted with "
586 "mkfs.lustre\n", mop.mo_device);
591 ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
594 fprintf(stderr, "Failed to read previous Lustre data from %s "
595 "(%d)\n", mop.mo_device, ret);
598 mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
600 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
601 mop.mo_mgs_failnodes++;
604 print_ldd("Read previous values", &(mop.mo_ldd));
611 ret = parse_opts(argc, argv, &mop, &mountopts);
617 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
619 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
624 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
626 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
631 /* Stand alone MGS doesn't need a index */
632 if (!IS_MDT(ldd) && IS_MGS(ldd))
633 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
635 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
636 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
638 fprintf(stderr, "Can't find the target index, "
639 "specify with --index\n");
644 if (IS_OST(ldd) && (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX))
645 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
646 "target index must be specified with --index\n",
651 * Comment out these 2 checks temporarily, since for multi-MDSes
652 * in single node only 1 mds node could have mgs service
654 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
655 verrprint("No management node specified, adding MGS to this "
657 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
659 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
662 fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
664 fprintf(stderr, "Must specify --mgsnode=\n");
669 if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
671 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
676 /* These are the permanent mount options (always included) */
677 ret = osd_prepare_lustre(&mop,
678 default_mountopts, sizeof(default_mountopts),
679 always_mountopts, sizeof(always_mountopts));
682 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
687 trim_mountfsoptions(mountopts);
688 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
689 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
693 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
696 if (ldd->ldd_mount_opts[0] == 0)
697 /* use the defaults unless old opts exist */
700 sprintf(ldd->ldd_mount_opts, "%s%s",
701 always_mountopts, default_mountopts);
702 trim_mountfsoptions(ldd->ldd_mount_opts);
706 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
707 ldd->ldd_fsname, ldd->ldd_svname);
710 print_ldd("Permanent disk data", ldd);
713 printf("exiting before disk write.\n");
717 if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
720 /* Create the loopback file */
721 if (mop.mo_flags & MO_IS_LOOP) {
722 ret = access(mop.mo_device, F_OK);
725 #ifndef TUNEFS /* mkfs.lustre */
726 /* Reformat the loopback file */
727 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
728 ret = loop_format(&mop);
734 ret = loop_setup(&mop);
737 fprintf(stderr, "Loop device setup for %s failed: %s\n",
738 mop.mo_device, strerror(ret));
743 #ifndef TUNEFS /* mkfs.lustre */
744 /* Check whether the disk has already been formatted by mkfs.lustre */
745 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
746 ret = osd_is_lustre(mop.mo_device, &mount_type);
749 fprintf(stderr, "Device %s was previously formatted "
750 "for lustre. Use --reformat to reformat it, "
751 "or tunefs.lustre to modify.\n",
757 /* Format the backing filesystem */
758 ret = osd_make_lustre(&mop);
761 fprintf(stderr, "mkfs failed %d\n", ret);
765 /* update svname with '=' to refresh config */
766 if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
767 struct mount_opts opts;
768 opts.mo_ldd = mop.mo_ldd;
769 opts.mo_source = mop.mo_device;
770 (void) osd_label_lustre(&opts);
774 /* Write our config files */
775 ret = osd_write_ldd(&mop);
778 fprintf(stderr, "failed to write local files\n");
786 /* Fix any crazy return values from system() */
787 if (ret && ((ret & 255) == 0))
790 verrprint("%s: exiting with %d (%s)\n",
791 progname, ret, strerror(ret));