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;
81 #ifdef HAVE_LDISKFS_OSD
82 #define FSLIST_LDISKFS "ldiskfs"
85 #define FSLIST_LDISKFS ""
86 #endif /* HAVE_LDISKFS_OSD */
89 #define FSLIST_ZFS "|zfs"
91 #define FSLIST_ZFS "zfs"
96 #endif /* HAVE_ZFS_OSD */
99 #error "no backing OSD types (ldiskfs or ZFS) are configured"
102 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
104 void usage(FILE *out)
106 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
107 fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
108 "--fsname=<filesystem name>\n"
109 "\t--index=<target index> [options] <device>\n", progname);
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",
118 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
120 "\t<pool name>: name of ZFS pool where target is created "
122 "\t<dataset name>: name of new dataset, must be unique within "
124 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
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 "
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"
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"
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", required_argument, NULL, 'b' },
278 { "stripe-count-hint", required_argument, NULL, 'c' },
279 { "comment", required_argument, NULL, 'u' },
280 { "configdev", required_argument, NULL, 'C' },
281 { "device-size", required_argument, NULL, 'd' },
282 { "dryrun", no_argument, NULL, 'n' },
283 { "erase-params", no_argument, NULL, 'e' },
284 { "failnode", required_argument, NULL, 'f' },
285 { "failover", required_argument, NULL, 'f' },
286 { "mgs", no_argument, NULL, 'G' },
287 { "help", no_argument, NULL, 'h' },
288 { "index", required_argument, NULL, 'i' },
289 { "mkfsoptions", required_argument, NULL, 'k' },
290 { "mgsnode", required_argument, NULL, 'm' },
291 { "mgsnid", required_argument, NULL, 'm' },
292 { "mdt", no_argument, NULL, 'M' },
293 { "fsname", required_argument, NULL, 'L' },
294 { "noformat", no_argument, NULL, 'n' },
295 { "nomgs", no_argument, NULL, 'N' },
296 { "mountfsoptions", required_argument, NULL, 'o' },
297 { "ost", no_argument, NULL, 'O' },
298 { "param", required_argument, NULL, 'p' },
299 { "print", no_argument, NULL, 'n' },
300 { "quiet", no_argument, NULL, 'q' },
301 { "quota", no_argument, NULL, 'Q' },
302 { "reformat", no_argument, NULL, 'r' },
303 { "replace", no_argument, NULL, 'R' },
304 { "servicenode", required_argument, NULL, 's' },
305 { "network", required_argument, NULL, 't' },
306 { "verbose", no_argument, NULL, 'v' },
307 { "writeconf", no_argument, NULL, 'w' },
310 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:PqrRs:t:Uu:vw";
313 int failnode_set = 0, servicenode_set = 0;
316 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
321 while (i < LDD_MT_LAST) {
322 if (strcmp(optarg, mt_str(i)) == 0) {
323 mop->mo_ldd.ldd_mount_type = i;
328 if (i == LDD_MT_LAST) {
329 fprintf(stderr, "%s: invalid backend filesystem"
330 " type %s\n", progname, optarg);
336 if (IS_MDT(&mop->mo_ldd)) {
337 int stripe_count = atol(optarg);
338 if (stripe_count <= 0) {
339 fprintf(stderr, "%s: bad stripe count "
340 "%d\n", progname, stripe_count);
343 mop->mo_stripe_count = stripe_count;
345 badopt(long_opt[longidx].name, "MDT");
349 case 'C': /* Configdev */
351 printf("Configdev not implemented\n");
354 mop->mo_device_kb = atol(optarg);
357 mop->mo_ldd.ldd_params[0] = '\0';
358 /* Must update the mgs logs */
359 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
365 if ((opt == 'f' && servicenode_set)
366 || (opt == 's' && failnode_set)) {
367 fprintf(stderr, "%s: %s cannot use with --%s\n",
368 progname, long_opt[longidx].name,
369 opt == 'f' ? "servicenode" : "failnode");
373 nids = convert_hostnames(optarg);
376 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
381 /* Must update the mgs logs */
382 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
386 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
389 mop->mo_flags |= MO_FAILOVER;
393 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
399 if (!(mop->mo_ldd.ldd_flags &
400 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
402 fprintf(stderr, "%s: cannot change the index of"
403 " a registered target\n", progname);
406 /* LU-2374: check whether it is OST/MDT later */
407 mop->mo_ldd.ldd_svindex = atol(optarg);
408 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
411 strscpy(mop->mo_mkfsopts, optarg,
412 sizeof(mop->mo_mkfsopts));
416 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
417 (!(mop->mo_ldd.ldd_flags &
418 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
419 LDD_F_WRITECONF)))) {
420 fprintf(stderr, "%s: cannot change the name of"
421 " a registered target\n", progname);
424 if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
425 fprintf(stderr, "%s: filesystem name must be "
426 "1-8 chars\n", progname);
429 if ((tmp = strpbrk(optarg, "/:"))) {
430 fprintf(stderr, "%s: char '%c' not allowed in "
431 "filesystem name\n", progname, *tmp);
434 strscpy(mop->mo_ldd.ldd_fsname, optarg,
435 sizeof(mop->mo_ldd.ldd_fsname));
439 char *nids = convert_hostnames(optarg);
442 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
447 mop->mo_mgs_failnodes++;
451 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
457 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
463 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
466 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
469 /* Must update the mgs logs */
470 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
476 mop->mo_flags |= MO_FORCEFORMAT;
482 if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
483 badopt(long_opt[longidx].name, "MDT,OST");
490 rc = add_param(mop->mo_ldd.ldd_params,
491 PARAM_NETWORK, optarg);
494 /* Must update the mgs logs */
495 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
498 strscpy(mop->mo_ldd.ldd_userdata, optarg,
499 sizeof(mop->mo_ldd.ldd_userdata));
505 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
508 mop->mo_flags |= MO_QUOTA;
513 fprintf(stderr, "Unknown option '%c'\n", opt);
519 /* Need to clear this flag after parsing 'L' and 'i' options. */
521 mop->mo_ldd.ldd_flags &= ~LDD_F_VIRGIN;
523 if (optind == argc) {
524 /* The user didn't specify device name */
526 fprintf(stderr, "Not enough arguments - device name or "
527 "pool/dataset name not specified.\n");
530 /* The device or pool/filesystem name */
531 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
533 /* Followed by optional vdevs */
534 if (optind < argc - 1)
535 mop->mo_pool_vdevs = (char **) &argv[optind + 1];
541 int main(int argc, char *const argv[])
543 struct mkfs_opts mop;
544 struct lustre_disk_data *ldd;
545 char *mountopts = NULL;
546 char always_mountopts[512] = "";
547 char default_mountopts[512] = "";
552 if ((progname = strrchr(argv[0], '/')) != NULL)
557 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
562 memset(&mop, 0, sizeof(mop));
565 /* device is last arg */
566 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
573 /* For tunefs, we must read in the old values before parsing any
576 /* Check whether the disk has already been formatted by mkfs.lustre */
577 ret = osd_is_lustre(mop.mo_device, &mount_type);
580 fprintf(stderr, "Device %s has not been formatted with "
581 "mkfs.lustre\n", mop.mo_device);
585 mop.mo_ldd.ldd_mount_type = mount_type;
587 ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
590 fprintf(stderr, "Failed to read previous Lustre data from %s "
591 "(%d)\n", mop.mo_device, ret);
594 mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
596 /* svname of the form lustre:OST1234 means never registered */
597 ret = strlen(mop.mo_ldd.ldd_svname);
598 if (mop.mo_ldd.ldd_svname[ret - 8] == ':') {
599 mop.mo_ldd.ldd_svname[ret - 8] = '-';
600 mop.mo_ldd.ldd_flags |= LDD_F_VIRGIN;
601 } else if (mop.mo_ldd.ldd_svname[ret - 8] == '=') {
602 mop.mo_ldd.ldd_svname[ret - 8] = '-';
603 mop.mo_ldd.ldd_flags |= LDD_F_WRITECONF;
606 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
607 mop.mo_mgs_failnodes++;
610 print_ldd("Read previous values", &(mop.mo_ldd));
613 ret = parse_opts(argc, argv, &mop, &mountopts);
619 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
621 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
626 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
628 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
633 /* Stand alone MGS doesn't need a index */
634 if (!IS_MDT(ldd) && IS_MGS(ldd)) {
635 #ifndef TUNEFS /* mkfs.lustre */
636 /* But if --index was specified flag an error */
637 if (!(mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)) {
638 badopt("index", "MDT,OST");
642 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
645 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
646 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
648 fprintf(stderr, "Can't find the target index, "
649 "specify with --index\n");
654 if (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)
655 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
656 "target index must be specified with --index\n",
659 /* If no index is supplied for MDT by default set index to zero */
660 if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
661 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
662 mop.mo_ldd.ldd_svindex = 0;
667 * Comment out these 2 checks temporarily, since for multi-MDSes
668 * in single node only 1 mds node could have mgs service
670 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
671 verrprint("No management node specified, adding MGS to this "
673 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
675 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
678 fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
680 fprintf(stderr, "Must specify --mgsnode=\n");
685 if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
687 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
692 /* These are the permanent mount options (always included) */
693 ret = osd_prepare_lustre(&mop,
694 default_mountopts, sizeof(default_mountopts),
695 always_mountopts, sizeof(always_mountopts));
698 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
703 trim_mountfsoptions(mountopts);
704 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
705 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
709 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
712 if (ldd->ldd_mount_opts[0] == 0)
713 /* use the defaults unless old opts exist */
716 sprintf(ldd->ldd_mount_opts, "%s%s",
717 always_mountopts, default_mountopts);
718 trim_mountfsoptions(ldd->ldd_mount_opts);
722 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
723 ldd->ldd_fsname, ldd->ldd_svname);
726 print_ldd("Permanent disk data", ldd);
729 printf("exiting before disk write.\n");
733 if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
736 /* Create the loopback file */
737 if (mop.mo_flags & MO_IS_LOOP) {
738 ret = access(mop.mo_device, F_OK);
741 #ifndef TUNEFS /* mkfs.lustre */
742 /* Reformat the loopback file */
743 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
744 ret = loop_format(&mop);
750 ret = loop_setup(&mop);
753 fprintf(stderr, "Loop device setup for %s failed: %s\n",
754 mop.mo_device, strerror(ret));
759 #ifndef TUNEFS /* mkfs.lustre */
760 /* Check whether the disk has already been formatted by mkfs.lustre */
761 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
762 ret = osd_is_lustre(mop.mo_device, &mount_type);
765 fprintf(stderr, "Device %s was previously formatted "
766 "for lustre. Use --reformat to reformat it, "
767 "or tunefs.lustre to modify.\n",
773 /* Format the backing filesystem */
774 ret = osd_make_lustre(&mop);
777 fprintf(stderr, "mkfs failed %d\n", ret);
781 /* update svname with '=' to refresh config */
782 if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
783 struct mount_opts opts;
784 opts.mo_ldd = mop.mo_ldd;
785 opts.mo_source = mop.mo_device;
786 (void) osd_label_lustre(&opts);
789 /* Enable quota accounting */
790 if (mop.mo_flags & MO_QUOTA) {
791 ret = osd_enable_quota(&mop);
797 /* Write our config files */
798 ret = osd_write_ldd(&mop);
801 fprintf(stderr, "failed to write local files\n");
807 ret2 = loop_cleanup(&mop);
811 /* Fix any crazy return values from system() */
812 if (ret && ((ret & 255) == 0))
815 verrprint("%s: exiting with %d (%s)\n",
816 progname, ret, strerror(ret));