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, 2015, 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 */
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>
73 #include "mount_utils.h"
78 static int print_only = 0;
80 #ifdef HAVE_LDISKFS_OSD
81 #define FSLIST_LDISKFS "ldiskfs"
84 #define FSLIST_LDISKFS ""
85 #endif /* HAVE_LDISKFS_OSD */
88 #define FSLIST_ZFS "|zfs"
90 #define FSLIST_ZFS "zfs"
95 #endif /* HAVE_ZFS_OSD */
98 #error "no backing OSD types (ldiskfs or ZFS) are configured"
101 #define FSLIST FSLIST_LDISKFS FSLIST_ZFS
103 void usage(FILE *out)
105 fprintf(out, "usage: %s <target type> [--backfstype="FSLIST"] "
106 "--fsname=<filesystem name>\n"
107 "\t--index=<target index> [options] <device>\n", progname);
109 fprintf(out, "usage: %s <target type> --backfstype=zfs "
110 "--fsname=<filesystem name> [options]\n"
111 "\t<pool name>/<dataset name>\n"
112 "\t[[<vdev type>] <device> [<device> ...] [vdev type>] ...]\n",
116 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
118 "\t<pool name>: name of ZFS pool where target is created "
120 "\t<dataset name>: name of new dataset, must be unique within "
122 "\t<vdev type>: type of vdev (mirror, raidz, raidz2, spare, "
128 "\t\t--mgs: configuration management service\n"
129 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
130 "\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\ttarget index may either be a decimal number or\n"
136 "\t\t\thexadecimal number starting with '0x'\n"
137 "\t\t--fsname=<8_char_filesystem_name>: fs targets belong to\n"
138 "\t\t\trequired for all targets other than MGS\n"
139 "\t\t--mgsnode=<nid>[,<...>]: NID(s) of remote MGS\n"
140 "\t\t\trequired for all targets other than MGS\n"
141 "\t\t--mountfsoptions=<opts>: permanent Lustre mount options\n"
142 "\t\t--backfs-mount-opts=<opts>: backing fs mount options\n"
143 "\t\t--failnode=<nid>[,<...>]: NID(s) of backup failover node\n"
144 "\t\t\tmutually exclusive with --servicenode\n"
145 "\t\t--servicenode=<nid>[,<...>]: NID(s) of service partners\n"
146 "\t\t\ttreat nodes as equal service node, mutually exclusive "
148 "\t\t--param <key>=<value>: set a permanent parameter\n"
149 "\t\t\te.g. --param sys.timeout=40\n"
150 "\t\t\t --param lov.stripesize=2M\n"
151 "\t\t--network=<net>[,<...>]: restrict OST/MDT to network(s)\n"
153 "\t\t--backfstype=<fstype>: backing fs type (ldiskfs, zfs)\n"
154 "\t\t--device-size=#N(KB): device size for loop devices\n"
155 "\t\t--mkfsoptions=<opts>: format options\n"
156 "\t\t--reformat: overwrite an existing disk\n"
157 "\t\t--replace: replace an old target with the same index\n"
158 "\t\t--stripe-count-hint=#N: for optimizing MDT inode size\n"
160 "\t\t--erase-params: erase all old parameter settings\n"
161 "\t\t--nomgs: turn off MGS service on this MDT\n"
162 "\t\t--writeconf: erase all config logs for this fs.\n"
163 "\t\t--quota: enable space accounting on old 2.x device.\n"
165 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
166 "\t\t--dryrun: report what we would do; don't write to disk\n"
167 "\t\t--verbose: e.g. show mkfs progress\n"
168 "\t\t-V|--version: output build version of the utility and\n"
171 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
175 /* ==================== Lustre config functions =============*/
177 void print_ldd(char *str, struct lustre_disk_data *ldd)
179 printf("\n %s:\n", str);
180 printf("Target: %s\n", ldd->ldd_svname);
181 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
182 printf("Index: unassigned\n");
184 printf("Index: %d\n", ldd->ldd_svindex);
185 if (ldd->ldd_uuid[0])
186 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
187 printf("Lustre FS: %s\n", ldd->ldd_fsname);
188 printf("Mount type: %s\n", MT_STR(ldd));
189 printf("Flags: %#x\n", ldd->ldd_flags);
190 printf(" (%s%s%s%s%s%s%s%s%s)\n",
191 IS_MDT(ldd) ? "MDT ":"",
192 IS_OST(ldd) ? "OST ":"",
193 IS_MGS(ldd) ? "MGS ":"",
194 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
195 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
196 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
197 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
198 ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
199 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
200 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
201 printf("Parameters:%s\n", ldd->ldd_params);
202 if (ldd->ldd_userdata[0])
203 printf("Comment: %s\n", ldd->ldd_userdata);
207 void set_defaults(struct mkfs_opts *mop)
209 mop->mo_ldd.ldd_magic = LDD_MAGIC;
210 mop->mo_ldd.ldd_config_ver = 1;
211 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
212 #ifdef HAVE_LDISKFS_OSD
213 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
215 mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
217 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
218 mop->mo_mgs_failnodes = 0;
219 mop->mo_stripe_count = 1;
220 mop->mo_pool_vdevs = NULL;
223 static inline void badopt(const char *opt, char *type)
225 fprintf(stderr, "%s: '--%s' only valid for %s\n",
226 progname, opt, type);
230 /* from mount_lustre */
231 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
232 #define MAXNIDSTR 1024
233 static char *convert_hostnames(char *s1)
235 char *converted, *s2 = 0, *c, *end, sep;
236 int left = MAXNIDSTR;
239 converted = malloc(left);
240 if (converted == NULL) {
244 end = s1 + strlen(s1);
246 while ((left > 0) && (s1 < end)) {
247 s2 = strpbrk(s1, ",:");
252 nid = libcfs_str2nid(s1);
255 if (nid == LNET_NID_ANY) {
256 fprintf(stderr, "%s: Cannot resolve hostname '%s'.\n",
261 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
262 strlen("127.0.0.1")) == 0) {
263 fprintf(stderr, "%s: The NID '%s' resolves to the "
264 "loopback address '%s'. Lustre requires a "
265 "non-loopback address.\n",
266 progname, s1, libcfs_nid2str(nid));
271 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
272 left = converted + MAXNIDSTR - c;
278 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
281 static struct option long_opt[] = {
282 { "backfs-mount-opts", required_argument, NULL, 'B' },
283 { "failnode", required_argument, NULL, 'f' },
284 { "failover", required_argument, NULL, 'f' },
285 { "help", no_argument, NULL, 'h' },
286 { "index", required_argument, NULL, 'i' },
287 { "fsname", required_argument, NULL, 'L' },
288 { "mgsnode", required_argument, NULL, 'm' },
289 { "mgsnid", required_argument, NULL, 'm' },
290 { "dryrun", no_argument, NULL, 'n' },
291 { "mountfsoptions", required_argument, NULL, 'o' },
292 { "param", required_argument, NULL, 'p' },
293 { "quiet", no_argument, NULL, 'q' },
294 { "servicenode", required_argument, NULL, 's' },
295 { "network", required_argument, NULL, 't' },
296 { "comment", required_argument, NULL, 'u' },
297 { "verbose", no_argument, NULL, 'v' },
298 { "version", no_argument, NULL, 'V' },
300 { "backfstype", required_argument, NULL, 'b' },
301 { "stripe-count-hint", required_argument, NULL, 'c' },
302 { "device-size", required_argument, NULL, 'd' },
303 { "mgs", no_argument, NULL, 'G' },
304 { "mkfsoptions", required_argument, NULL, 'k' },
305 { "mdt", no_argument, NULL, 'M' },
306 { "nomgs", no_argument, NULL, 'N' },
307 { "ost", no_argument, NULL, 'O' },
308 { "reformat", no_argument, NULL, 'r' },
309 { "replace", no_argument, NULL, 'R' },
311 { "erase-params", no_argument, NULL, 'e' },
312 { "quota", no_argument, NULL, 'Q' },
313 { "writeconf", no_argument, NULL, 'w' },
317 char *optstring = "B:f:hi:L:m:no:p:qs:t:u:vV"
323 struct lustre_disk_data *ldd = &mop->mo_ldd;
324 char new_fsname[16] = { 0 };
327 int failnode_set = 0, servicenode_set = 0;
329 bool index_option = false;
331 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
335 mop->mo_mountopts = optarg;
341 if ((opt == 'f' && servicenode_set) ||
342 (opt == 's' && failnode_set)) {
343 fprintf(stderr, "%s: %s cannot use with --%s\n",
344 progname, long_opt[longidx].name,
345 opt == 'f' ? "servicenode" :
350 nids = convert_hostnames(optarg);
354 rc = append_param(ldd->ldd_params, PARAM_FAILNODE,
360 /* Must update the mgs logs */
361 ldd->ldd_flags |= LDD_F_UPDATE;
365 ldd->ldd_flags |= LDD_F_NO_PRIMNODE;
368 mop->mo_flags |= MO_FAILOVER;
378 /* LU-2374: check whether it is OST/MDT later */
379 base = (strlen(optarg) > 1 &&
380 !strncmp(optarg, "0x", 2)) ? 16 : 10;
381 /* Allowed input are base 16 and base 10 numbers only */
382 mop->mo_ldd.ldd_svindex = strtoul(optarg,
384 if (*endptr != '\0') {
385 fprintf(stderr, "%s: wrong index %s. "
386 "Target index must be decimal or "
391 if (ldd->ldd_svindex >= INDEX_UNASSIGNED) {
392 fprintf(stderr, "%s: wrong index %u. "
393 "Target index must be less than %u.\n",
394 progname, ldd->ldd_svindex,
399 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
403 rc = lustre_is_fsname_valid(optarg, 1,
406 fprintf(stderr, "%s: filesystem name must be "
407 "1-%d chars\n", progname,
411 fprintf(stderr, "%s: char '%c' not allowed in "
412 "filesystem name\n", progname, rc);
416 strscpy(new_fsname, optarg, sizeof(new_fsname));
420 char *nids = convert_hostnames(optarg);
425 rc = append_param(ldd->ldd_params, PARAM_MGSNODE,
431 mop->mo_mgs_failnodes++;
441 rc = add_param(ldd->ldd_params, NULL, optarg);
445 /* Must update the mgs logs */
446 ldd->ldd_flags |= LDD_F_UPDATE;
452 if (!IS_MDT(ldd) && !IS_OST(ldd)) {
453 badopt(long_opt[longidx].name, "MDT,OST");
460 rc = add_param(ldd->ldd_params, PARAM_NETWORK, optarg);
464 /* Must update the mgs logs */
465 ldd->ldd_flags |= LDD_F_UPDATE;
468 strscpy(ldd->ldd_userdata, optarg,
469 sizeof(ldd->ldd_userdata));
476 fprintf(stdout, "%s %s\n", progname,
477 LUSTRE_VERSION_STRING);
484 if (strcmp(optarg, mt_str(i)) == 0) {
485 ldd->ldd_mount_type = i;
488 } while (++i < LDD_MT_LAST);
490 if (i == LDD_MT_LAST) {
491 fprintf(stderr, "%s: invalid backend filesystem"
492 " type %s\n", progname, optarg);
499 int stripe_count = atol(optarg);
501 if (stripe_count <= 0) {
502 fprintf(stderr, "%s: bad stripe count "
503 "%d\n", progname, stripe_count);
506 mop->mo_stripe_count = stripe_count;
508 badopt(long_opt[longidx].name, "MDT");
513 mop->mo_device_kb = atol(optarg);
516 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
519 strscpy(mop->mo_mkfsopts, optarg,
520 sizeof(mop->mo_mkfsopts));
523 ldd->ldd_flags |= LDD_F_SV_TYPE_MDT;
526 ldd->ldd_flags &= ~LDD_F_SV_TYPE_MGS;
529 ldd->ldd_flags |= LDD_F_SV_TYPE_OST;
532 mop->mo_flags |= MO_FORCEFORMAT;
539 ldd->ldd_params[0] = '\0';
540 /* Must update the mgs logs */
541 ldd->ldd_flags |= LDD_F_UPDATE;
544 mop->mo_flags |= MO_QUOTA;
547 ldd->ldd_flags |= LDD_F_WRITECONF;
553 fprintf(stderr, "Unknown option '%c'\n", opt);
559 if (strlen(new_fsname) > 0) {
560 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
562 (LDD_F_UPGRADE14 | LDD_F_VIRGIN | LDD_F_WRITECONF)))) {
563 fprintf(stderr, "%s: cannot change the name "
564 "of a registered target\n", progname);
568 strscpy(ldd->ldd_fsname, new_fsname, sizeof(ldd->ldd_fsname));
571 if (index_option && !(mop->mo_ldd.ldd_flags &
572 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
574 fprintf(stderr, "%s: cannot change the index of"
575 " a registered target\n", progname);
579 /* Need to clear this flag after parsing 'L' and 'i' options. */
581 ldd->ldd_flags &= ~LDD_F_VIRGIN;
583 if (optind == argc) {
584 /* The user didn't specify device name */
586 fprintf(stderr, "Not enough arguments - device name or "
587 "pool/dataset name not specified.\n");
590 /* The device or pool/filesystem name */
591 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
593 /* Followed by optional vdevs */
594 if (optind < argc - 1)
595 mop->mo_pool_vdevs = (char **) &argv[optind + 1];
601 int main(int argc, char *const argv[])
603 struct mkfs_opts mop;
604 struct lustre_disk_data *ldd = &mop.mo_ldd;
605 char *mountopts = NULL;
606 char wanted_mountopts[512] = "";
611 progname = strrchr(argv[0], '/');
612 if (progname != NULL)
617 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
622 memset(&mop, 0, sizeof(mop));
625 /* device is last arg */
626 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
633 /* For tunefs, we must read in the old values before parsing any
636 /* Check whether the disk has already been formatted by mkfs.lustre */
637 ret = osd_is_lustre(mop.mo_device, &mount_type);
640 fprintf(stderr, "Device %s has not been formatted with "
641 "mkfs.lustre\n", mop.mo_device);
645 ldd->ldd_mount_type = mount_type;
647 ret = osd_read_ldd(mop.mo_device, ldd);
650 fprintf(stderr, "Failed to read previous Lustre data from %s "
651 "(%d)\n", mop.mo_device, ret);
655 ldd->ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
657 /* svname of the form lustre:OST1234 means never registered */
658 ret = strlen(ldd->ldd_svname);
659 if (ldd->ldd_svname[ret - 8] == ':') {
660 ldd->ldd_svname[ret - 8] = '-';
661 ldd->ldd_flags |= LDD_F_VIRGIN;
662 } else if (ldd->ldd_svname[ret - 8] == '=') {
663 ldd->ldd_svname[ret - 8] = '-';
664 ldd->ldd_flags |= LDD_F_WRITECONF;
667 if (strstr(ldd->ldd_params, PARAM_MGSNODE))
668 mop.mo_mgs_failnodes++;
671 print_ldd("Read previous values", ldd);
674 ret = parse_opts(argc, argv, &mop, &mountopts);
675 if (ret != 0 || version)
678 if (!IS_MDT(ldd) && !IS_OST(ldd) && !IS_MGS(ldd)) {
680 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
685 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
687 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
692 /* Stand alone MGS doesn't need an index */
693 if (!IS_MDT(ldd) && IS_MGS(ldd)) {
695 /* But if --index was specified flag an error */
696 if (!(ldd->ldd_flags & LDD_F_NEED_INDEX)) {
697 badopt("index", "MDT,OST");
701 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
704 if ((ldd->ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
705 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
707 fprintf(stderr, "Can't find the target index, "
708 "specify with --index\n");
713 if (ldd->ldd_flags & LDD_F_NEED_INDEX)
714 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
715 "target index must be specified with --index\n",
718 /* If no index is supplied for MDT by default set index to zero */
719 if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
720 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
721 ldd->ldd_svindex = 0;
723 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
726 fprintf(stderr, "Must specify --mgs or --mgsnode\n");
728 fprintf(stderr, "Must specify --mgsnode\n");
732 if ((IS_MDT(ldd) || IS_OST(ldd)) && ldd->ldd_fsname[0] == '\0') {
734 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
739 /* These are the permanent mount options (always included) */
740 ret = osd_prepare_lustre(&mop,
741 wanted_mountopts, sizeof(wanted_mountopts));
744 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
749 trim_mountfsoptions(mountopts);
750 if (check_mountfsoptions(mountopts, wanted_mountopts)) {
754 snprintf(ldd->ldd_mount_opts, sizeof(ldd->ldd_mount_opts),
758 if (ldd->ldd_mount_opts[0] == 0)
759 /* use the defaults unless old opts exist */
762 snprintf(ldd->ldd_mount_opts,
763 sizeof(ldd->ldd_mount_opts),
764 "%s", wanted_mountopts);
765 trim_mountfsoptions(ldd->ldd_mount_opts);
769 ret = osd_fix_mountopts(&mop, ldd->ldd_mount_opts,
770 sizeof(ldd->ldd_mount_opts));
773 fprintf(stderr, "unable to fix mountfsoptions (%d)\n", ret);
777 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
778 ldd->ldd_fsname, ldd->ldd_svname);
781 print_ldd("Permanent disk data", ldd);
784 printf("exiting before disk write.\n");
788 if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
791 /* Create the loopback file */
792 if (mop.mo_flags & MO_IS_LOOP) {
793 ret = access(mop.mo_device, F_OK);
798 /* Reformat the loopback file */
799 if (ret != 0 || (mop.mo_flags & MO_FORCEFORMAT)) {
800 ret = loop_format(&mop);
806 ret = loop_setup(&mop);
809 fprintf(stderr, "Loop device setup for %s failed: %s\n",
810 mop.mo_device, strerror(ret));
816 /* Check whether the disk has already been formatted by mkfs.lustre */
817 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
818 ret = osd_is_lustre(mop.mo_device, &mount_type);
821 fprintf(stderr, "Device %s was previously formatted "
822 "for lustre. Use --reformat to reformat it, "
823 "or tunefs.lustre to modify.\n",
829 /* Format the backing filesystem */
830 ret = osd_make_lustre(&mop);
833 fprintf(stderr, "mkfs failed %d\n", ret);
837 /* update svname with '=' to refresh config */
838 if (ldd->ldd_flags & LDD_F_WRITECONF) {
839 struct mount_opts opts;
841 opts.mo_source = mop.mo_device;
842 (void) osd_label_lustre(&opts);
845 /* Enable quota accounting */
846 if (mop.mo_flags & MO_QUOTA) {
847 ret = osd_enable_quota(&mop);
852 /* Write our config files */
853 ret = osd_write_ldd(&mop);
856 fprintf(stderr, "failed to write local files\n");
862 ret2 = loop_cleanup(&mop);
866 /* Fix any crazy return values from system() */
867 if (ret != 0 && ((ret & 255) == 0))
871 verrprint("%s: exiting with %d (%s)\n",
872 progname, ret, strerror(ret));