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>
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>
80 static int print_only = 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, "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"
165 "\t\t-V|--version: output build version of the utility and\n"
168 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
172 /* ==================== Lustre config functions =============*/
174 void print_ldd(char *str, struct lustre_disk_data *ldd)
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");
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);
204 void set_defaults(struct mkfs_opts *mop)
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;
212 mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
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;
220 static inline void badopt(const char *opt, char *type)
222 fprintf(stderr, "%s: '--%s' only valid for %s\n",
223 progname, opt, type);
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)
232 char *converted, *s2 = 0, *c, *end, sep;
233 int left = MAXNIDSTR;
236 converted = malloc(left);
237 if (converted == NULL) {
241 end = s1 + strlen(s1);
243 while ((left > 0) && (s1 < end)) {
244 s2 = strpbrk(s1, ",:");
249 nid = libcfs_str2nid(s1);
252 if (nid == LNET_NID_ANY) {
253 fprintf(stderr, "%s: Cannot resolve hostname '%s'.\n",
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));
268 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
269 left = converted + MAXNIDSTR - c;
275 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
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' },
313 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:PqrRs:t:Uu:vVw";
316 int failnode_set = 0, servicenode_set = 0;
319 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
324 while (i < LDD_MT_LAST) {
325 if (strcmp(optarg, mt_str(i)) == 0) {
326 mop->mo_ldd.ldd_mount_type = i;
331 if (i == LDD_MT_LAST) {
332 fprintf(stderr, "%s: invalid backend filesystem"
333 " type %s\n", progname, optarg);
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);
346 mop->mo_stripe_count = stripe_count;
348 badopt(long_opt[longidx].name, "MDT");
352 case 'C': /* Configdev */
354 printf("Configdev not implemented\n");
357 mop->mo_device_kb = atol(optarg);
360 mop->mo_ldd.ldd_params[0] = '\0';
361 /* Must update the mgs logs */
362 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
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");
376 nids = convert_hostnames(optarg);
379 rc = append_param(mop->mo_ldd.ldd_params,
380 PARAM_FAILNODE, nids, ':');
384 /* Must update the mgs logs */
385 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
389 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
392 mop->mo_flags |= MO_FAILOVER;
396 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
402 if (!(mop->mo_ldd.ldd_flags &
403 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
405 fprintf(stderr, "%s: cannot change the index of"
406 " a registered target\n", progname);
409 /* LU-2374: check whether it is OST/MDT later */
410 mop->mo_ldd.ldd_svindex = atol(optarg);
411 if (mop->mo_ldd.ldd_svindex >= INDEX_UNASSIGNED) {
412 fprintf(stderr, "%s: wrong index %u. "
413 "Target index must be less than %u.\n",
414 progname, mop->mo_ldd.ldd_svindex,
418 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
421 strscpy(mop->mo_mkfsopts, optarg,
422 sizeof(mop->mo_mkfsopts));
426 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
427 (!(mop->mo_ldd.ldd_flags &
428 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
429 LDD_F_WRITECONF)))) {
430 fprintf(stderr, "%s: cannot change the name of"
431 " a registered target\n", progname);
434 if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
435 fprintf(stderr, "%s: filesystem name must be "
436 "1-8 chars\n", progname);
439 if ((tmp = strpbrk(optarg, "/:"))) {
440 fprintf(stderr, "%s: char '%c' not allowed in "
441 "filesystem name\n", progname, *tmp);
444 strscpy(mop->mo_ldd.ldd_fsname, optarg,
445 sizeof(mop->mo_ldd.ldd_fsname));
449 char *nids = convert_hostnames(optarg);
452 rc = append_param(mop->mo_ldd.ldd_params,
453 PARAM_MGSNODE, nids, ':');
457 mop->mo_mgs_failnodes++;
461 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
467 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
473 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
476 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
479 /* Must update the mgs logs */
480 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
486 mop->mo_flags |= MO_FORCEFORMAT;
492 if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
493 badopt(long_opt[longidx].name, "MDT,OST");
500 rc = add_param(mop->mo_ldd.ldd_params,
501 PARAM_NETWORK, optarg);
504 /* Must update the mgs logs */
505 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
508 strscpy(mop->mo_ldd.ldd_userdata, optarg,
509 sizeof(mop->mo_ldd.ldd_userdata));
516 fprintf(stdout, "%s %s\n", progname,
517 LUSTRE_VERSION_STRING);
520 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
523 mop->mo_flags |= MO_QUOTA;
528 fprintf(stderr, "Unknown option '%c'\n", opt);
534 /* Need to clear this flag after parsing 'L' and 'i' options. */
536 mop->mo_ldd.ldd_flags &= ~LDD_F_VIRGIN;
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] = "";
567 if ((progname = strrchr(argv[0], '/')) != NULL)
572 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
577 memset(&mop, 0, sizeof(mop));
580 /* device is last arg */
581 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
588 /* For tunefs, we must read in the old values before parsing any
591 /* Check whether the disk has already been formatted by mkfs.lustre */
592 ret = osd_is_lustre(mop.mo_device, &mount_type);
595 fprintf(stderr, "Device %s has not been formatted with "
596 "mkfs.lustre\n", mop.mo_device);
600 mop.mo_ldd.ldd_mount_type = mount_type;
602 ret = osd_read_ldd(mop.mo_device, &mop.mo_ldd);
605 fprintf(stderr, "Failed to read previous Lustre data from %s "
606 "(%d)\n", mop.mo_device, ret);
609 mop.mo_ldd.ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
611 /* svname of the form lustre:OST1234 means never registered */
612 ret = strlen(mop.mo_ldd.ldd_svname);
613 if (mop.mo_ldd.ldd_svname[ret - 8] == ':') {
614 mop.mo_ldd.ldd_svname[ret - 8] = '-';
615 mop.mo_ldd.ldd_flags |= LDD_F_VIRGIN;
616 } else if (mop.mo_ldd.ldd_svname[ret - 8] == '=') {
617 mop.mo_ldd.ldd_svname[ret - 8] = '-';
618 mop.mo_ldd.ldd_flags |= LDD_F_WRITECONF;
621 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
622 mop.mo_mgs_failnodes++;
625 print_ldd("Read previous values", &(mop.mo_ldd));
628 ret = parse_opts(argc, argv, &mop, &mountopts);
634 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
636 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
641 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
643 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
648 /* Stand alone MGS doesn't need a index */
649 if (!IS_MDT(ldd) && IS_MGS(ldd)) {
650 #ifndef TUNEFS /* mkfs.lustre */
651 /* But if --index was specified flag an error */
652 if (!(mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)) {
653 badopt("index", "MDT,OST");
657 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
660 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
661 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
663 fprintf(stderr, "Can't find the target index, "
664 "specify with --index\n");
669 if (mop.mo_ldd.ldd_flags & LDD_F_NEED_INDEX)
670 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
671 "target index must be specified with --index\n",
674 /* If no index is supplied for MDT by default set index to zero */
675 if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
676 mop.mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
677 mop.mo_ldd.ldd_svindex = 0;
679 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
682 fprintf(stderr, "Must specify --mgs or --mgsnode\n");
684 fprintf(stderr, "Must specify --mgsnode\n");
688 if ((IS_MDT(ldd) || IS_OST(ldd)) && mop.mo_ldd.ldd_fsname[0] == '\0') {
690 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
695 /* These are the permanent mount options (always included) */
696 ret = osd_prepare_lustre(&mop,
697 default_mountopts, sizeof(default_mountopts),
698 always_mountopts, sizeof(always_mountopts));
701 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
706 trim_mountfsoptions(mountopts);
707 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
708 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
712 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
715 if (ldd->ldd_mount_opts[0] == 0)
716 /* use the defaults unless old opts exist */
719 sprintf(ldd->ldd_mount_opts, "%s%s",
720 always_mountopts, default_mountopts);
721 trim_mountfsoptions(ldd->ldd_mount_opts);
725 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
726 ldd->ldd_fsname, ldd->ldd_svname);
729 print_ldd("Permanent disk data", ldd);
732 printf("exiting before disk write.\n");
736 if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
739 /* Create the loopback file */
740 if (mop.mo_flags & MO_IS_LOOP) {
741 ret = access(mop.mo_device, F_OK);
744 #ifndef TUNEFS /* mkfs.lustre */
745 /* Reformat the loopback file */
746 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
747 ret = loop_format(&mop);
753 ret = loop_setup(&mop);
756 fprintf(stderr, "Loop device setup for %s failed: %s\n",
757 mop.mo_device, strerror(ret));
762 #ifndef TUNEFS /* mkfs.lustre */
763 /* Check whether the disk has already been formatted by mkfs.lustre */
764 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
765 ret = osd_is_lustre(mop.mo_device, &mount_type);
768 fprintf(stderr, "Device %s was previously formatted "
769 "for lustre. Use --reformat to reformat it, "
770 "or tunefs.lustre to modify.\n",
776 /* Format the backing filesystem */
777 ret = osd_make_lustre(&mop);
780 fprintf(stderr, "mkfs failed %d\n", ret);
784 /* update svname with '=' to refresh config */
785 if (mop.mo_ldd.ldd_flags & LDD_F_WRITECONF) {
786 struct mount_opts opts;
787 opts.mo_ldd = mop.mo_ldd;
788 opts.mo_source = mop.mo_device;
789 (void) osd_label_lustre(&opts);
792 /* Enable quota accounting */
793 if (mop.mo_flags & MO_QUOTA) {
794 ret = osd_enable_quota(&mop);
800 /* Write our config files */
801 ret = osd_write_ldd(&mop);
804 fprintf(stderr, "failed to write local files\n");
810 ret2 = loop_cleanup(&mop);
814 /* Fix any crazy return values from system() */
815 if (ret && ((ret & 255) == 0))
818 verrprint("%s: exiting with %d (%s)\n",
819 progname, ret, strerror(ret));