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--force-nohostid: Ignore hostid requirement for ZFS "
170 "\t\t-V|--version: output build version of the utility and\n"
173 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
177 /* ==================== Lustre config functions =============*/
179 void print_ldd(char *str, struct lustre_disk_data *ldd)
181 printf("\n %s:\n", str);
182 printf("Target: %s\n", ldd->ldd_svname);
183 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
184 printf("Index: unassigned\n");
186 printf("Index: %d\n", ldd->ldd_svindex);
187 if (ldd->ldd_uuid[0])
188 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
189 printf("Lustre FS: %s\n", ldd->ldd_fsname);
190 printf("Mount type: %s\n", MT_STR(ldd));
191 printf("Flags: %#x\n", ldd->ldd_flags);
192 printf(" (%s%s%s%s%s%s%s%s%s)\n",
193 IS_MDT(ldd) ? "MDT ":"",
194 IS_OST(ldd) ? "OST ":"",
195 IS_MGS(ldd) ? "MGS ":"",
196 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
197 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
198 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
199 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
200 ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
201 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
202 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
203 printf("Parameters:%s\n", ldd->ldd_params);
204 if (ldd->ldd_userdata[0])
205 printf("Comment: %s\n", ldd->ldd_userdata);
209 void set_defaults(struct mkfs_opts *mop)
211 mop->mo_ldd.ldd_magic = LDD_MAGIC;
212 mop->mo_ldd.ldd_config_ver = 1;
213 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
214 #ifdef HAVE_LDISKFS_OSD
215 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
217 mop->mo_ldd.ldd_mount_type = LDD_MT_ZFS;
219 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
220 mop->mo_mgs_failnodes = 0;
221 mop->mo_stripe_count = 1;
222 mop->mo_pool_vdevs = NULL;
225 static inline void badopt(const char *opt, char *type)
227 fprintf(stderr, "%s: '--%s' only valid for %s\n",
228 progname, opt, type);
232 /* from mount_lustre */
233 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
234 #define MAXNIDSTR 1024
235 static char *convert_hostnames(char *s1)
237 char *converted, *s2 = 0, *c, *end, sep;
238 int left = MAXNIDSTR;
241 converted = malloc(left);
242 if (converted == NULL) {
246 end = s1 + strlen(s1);
248 while ((left > 0) && (s1 < end)) {
249 s2 = strpbrk(s1, ",:");
254 nid = libcfs_str2nid(s1);
257 if (nid == LNET_NID_ANY) {
258 fprintf(stderr, "%s: Cannot resolve hostname '%s'.\n",
263 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
264 strlen("127.0.0.1")) == 0) {
265 fprintf(stderr, "%s: The NID '%s' resolves to the "
266 "loopback address '%s'. Lustre requires a "
267 "non-loopback address.\n",
268 progname, s1, libcfs_nid2str(nid));
273 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
274 left = converted + MAXNIDSTR - c;
280 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
283 static struct option long_opt[] = {
284 { "backfs-mount-opts", required_argument, NULL, 'B' },
285 { "failnode", required_argument, NULL, 'f' },
286 { "failover", required_argument, NULL, 'f' },
287 { "help", no_argument, NULL, 'h' },
288 { "index", required_argument, NULL, 'i' },
289 { "fsname", required_argument, NULL, 'L' },
290 { "mgsnode", required_argument, NULL, 'm' },
291 { "mgsnid", required_argument, NULL, 'm' },
292 { "dryrun", no_argument, NULL, 'n' },
293 { "mountfsoptions", required_argument, NULL, 'o' },
294 { "param", required_argument, NULL, 'p' },
295 { "quiet", no_argument, NULL, 'q' },
296 { "servicenode", required_argument, NULL, 's' },
297 { "network", required_argument, NULL, 't' },
298 { "comment", required_argument, NULL, 'u' },
299 { "force-nohostid", no_argument, NULL, 'U' },
300 { "verbose", no_argument, NULL, 'v' },
301 { "version", no_argument, NULL, 'V' },
303 { "backfstype", required_argument, NULL, 'b' },
304 { "stripe-count-hint", required_argument, NULL, 'c' },
305 { "device-size", required_argument, NULL, 'd' },
306 { "mgs", no_argument, NULL, 'G' },
307 { "mkfsoptions", required_argument, NULL, 'k' },
308 { "mdt", no_argument, NULL, 'M' },
309 { "nomgs", no_argument, NULL, 'N' },
310 { "ost", no_argument, NULL, 'O' },
311 { "reformat", no_argument, NULL, 'r' },
312 { "replace", no_argument, NULL, 'R' },
314 { "erase-params", no_argument, NULL, 'e' },
315 { "quota", no_argument, NULL, 'Q' },
316 { "writeconf", no_argument, NULL, 'w' },
320 char *optstring = "B:f:hi:L:m:no:p:qs:t:u:vV"
326 struct lustre_disk_data *ldd = &mop->mo_ldd;
327 char new_fsname[16] = { 0 };
330 int failnode_set = 0, servicenode_set = 0;
332 bool index_option = false;
334 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
338 mop->mo_mountopts = optarg;
344 if ((opt == 'f' && servicenode_set) ||
345 (opt == 's' && failnode_set)) {
346 fprintf(stderr, "%s: %s cannot use with --%s\n",
347 progname, long_opt[longidx].name,
348 opt == 'f' ? "servicenode" :
353 nids = convert_hostnames(optarg);
357 rc = append_param(ldd->ldd_params, PARAM_FAILNODE,
363 /* Must update the mgs logs */
364 ldd->ldd_flags |= LDD_F_UPDATE;
368 ldd->ldd_flags |= LDD_F_NO_PRIMNODE;
371 mop->mo_flags |= MO_FAILOVER;
381 /* LU-2374: check whether it is OST/MDT later */
382 base = (strlen(optarg) > 1 &&
383 !strncmp(optarg, "0x", 2)) ? 16 : 10;
384 /* Allowed input are base 16 and base 10 numbers only */
385 mop->mo_ldd.ldd_svindex = strtoul(optarg,
387 if (*endptr != '\0') {
388 fprintf(stderr, "%s: wrong index %s. "
389 "Target index must be decimal or "
394 if (ldd->ldd_svindex >= INDEX_UNASSIGNED) {
395 fprintf(stderr, "%s: wrong index %u. "
396 "Target index must be less than %u.\n",
397 progname, ldd->ldd_svindex,
402 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
406 rc = lustre_is_fsname_valid(optarg, 1,
409 fprintf(stderr, "%s: filesystem name must be "
410 "1-%d chars\n", progname,
414 fprintf(stderr, "%s: char '%c' not allowed in "
415 "filesystem name\n", progname, rc);
419 strscpy(new_fsname, optarg, sizeof(new_fsname));
423 char *nids = convert_hostnames(optarg);
428 rc = append_param(ldd->ldd_params, PARAM_MGSNODE,
434 mop->mo_mgs_failnodes++;
444 rc = add_param(ldd->ldd_params, NULL, optarg);
448 /* Must update the mgs logs */
449 ldd->ldd_flags |= LDD_F_UPDATE;
455 if (!IS_MDT(ldd) && !IS_OST(ldd)) {
456 badopt(long_opt[longidx].name, "MDT,OST");
463 rc = add_param(ldd->ldd_params, PARAM_NETWORK, optarg);
467 /* Must update the mgs logs */
468 ldd->ldd_flags |= LDD_F_UPDATE;
471 strscpy(ldd->ldd_userdata, optarg,
472 sizeof(ldd->ldd_userdata));
475 mop->mo_flags |= MO_NOHOSTID_CHECK;
482 fprintf(stdout, "%s %s\n", progname,
483 LUSTRE_VERSION_STRING);
490 if (strcmp(optarg, mt_str(i)) == 0) {
491 ldd->ldd_mount_type = i;
494 } while (++i < LDD_MT_LAST);
496 if (i == LDD_MT_LAST) {
497 fprintf(stderr, "%s: invalid backend filesystem"
498 " type %s\n", progname, optarg);
505 int stripe_count = atol(optarg);
507 if (stripe_count <= 0) {
508 fprintf(stderr, "%s: bad stripe count "
509 "%d\n", progname, stripe_count);
512 mop->mo_stripe_count = stripe_count;
514 badopt(long_opt[longidx].name, "MDT");
519 mop->mo_device_kb = atol(optarg);
522 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
525 strscpy(mop->mo_mkfsopts, optarg,
526 sizeof(mop->mo_mkfsopts));
529 ldd->ldd_flags |= LDD_F_SV_TYPE_MDT;
532 ldd->ldd_flags &= ~LDD_F_SV_TYPE_MGS;
535 ldd->ldd_flags |= LDD_F_SV_TYPE_OST;
538 mop->mo_flags |= MO_FORCEFORMAT;
545 ldd->ldd_params[0] = '\0';
546 /* Must update the mgs logs */
547 ldd->ldd_flags |= LDD_F_UPDATE;
550 mop->mo_flags |= MO_QUOTA;
553 ldd->ldd_flags |= LDD_F_WRITECONF;
559 fprintf(stderr, "Unknown option '%c'\n", opt);
565 if (strlen(new_fsname) > 0) {
566 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
568 (LDD_F_UPGRADE14 | LDD_F_VIRGIN | LDD_F_WRITECONF)))) {
569 fprintf(stderr, "%s: cannot change the name "
570 "of a registered target\n", progname);
574 strscpy(ldd->ldd_fsname, new_fsname, sizeof(ldd->ldd_fsname));
577 if (index_option && !(mop->mo_ldd.ldd_flags &
578 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
580 fprintf(stderr, "%s: cannot change the index of"
581 " a registered target\n", progname);
585 /* Need to clear this flag after parsing 'L' and 'i' options. */
587 ldd->ldd_flags &= ~LDD_F_VIRGIN;
589 if (optind == argc) {
590 /* The user didn't specify device name */
592 fprintf(stderr, "Not enough arguments - device name or "
593 "pool/dataset name not specified.\n");
596 /* The device or pool/filesystem name */
597 strscpy(mop->mo_device, argv[optind], sizeof(mop->mo_device));
599 /* Followed by optional vdevs */
600 if (optind < argc - 1)
601 mop->mo_pool_vdevs = (char **) &argv[optind + 1];
607 int main(int argc, char *const argv[])
609 struct mkfs_opts mop;
610 struct lustre_disk_data *ldd = &mop.mo_ldd;
611 char *mountopts = NULL;
612 char wanted_mountopts[512] = "";
617 progname = strrchr(argv[0], '/');
618 if (progname != NULL)
623 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
628 memset(&mop, 0, sizeof(mop));
631 /* device is last arg */
632 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
639 /* For tunefs, we must read in the old values before parsing any
642 /* Check whether the disk has already been formatted by mkfs.lustre */
643 ret = osd_is_lustre(mop.mo_device, &mount_type);
646 fprintf(stderr, "Device %s has not been formatted with "
647 "mkfs.lustre\n", mop.mo_device);
651 ldd->ldd_mount_type = mount_type;
653 ret = osd_read_ldd(mop.mo_device, ldd);
656 fprintf(stderr, "Failed to read previous Lustre data from %s "
657 "(%d)\n", mop.mo_device, ret);
661 ldd->ldd_flags &= ~(LDD_F_WRITECONF | LDD_F_VIRGIN);
663 /* svname of the form lustre:OST1234 means never registered */
664 ret = strlen(ldd->ldd_svname);
665 if (ldd->ldd_svname[ret - 8] == ':') {
666 ldd->ldd_svname[ret - 8] = '-';
667 ldd->ldd_flags |= LDD_F_VIRGIN;
668 } else if (ldd->ldd_svname[ret - 8] == '=') {
669 ldd->ldd_svname[ret - 8] = '-';
670 ldd->ldd_flags |= LDD_F_WRITECONF;
673 if (strstr(ldd->ldd_params, PARAM_MGSNODE))
674 mop.mo_mgs_failnodes++;
677 print_ldd("Read previous values", ldd);
680 ret = parse_opts(argc, argv, &mop, &mountopts);
681 if (ret != 0 || version)
684 if (!IS_MDT(ldd) && !IS_OST(ldd) && !IS_MGS(ldd)) {
686 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
691 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
693 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
698 /* Stand alone MGS doesn't need an index */
699 if (!IS_MDT(ldd) && IS_MGS(ldd)) {
701 /* But if --index was specified flag an error */
702 if (!(ldd->ldd_flags & LDD_F_NEED_INDEX)) {
703 badopt("index", "MDT,OST");
707 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
710 if ((ldd->ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
711 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
713 fprintf(stderr, "Can't find the target index, "
714 "specify with --index\n");
719 if (ldd->ldd_flags & LDD_F_NEED_INDEX)
720 fprintf(stderr, "warning: %s: for Lustre 2.4 and later, the "
721 "target index must be specified with --index\n",
724 /* If no index is supplied for MDT by default set index to zero */
725 if (IS_MDT(ldd) && (ldd->ldd_svindex == INDEX_UNASSIGNED)) {
726 ldd->ldd_flags &= ~LDD_F_NEED_INDEX;
727 ldd->ldd_svindex = 0;
729 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
732 fprintf(stderr, "Must specify --mgs or --mgsnode\n");
734 fprintf(stderr, "Must specify --mgsnode\n");
738 if ((IS_MDT(ldd) || IS_OST(ldd)) && ldd->ldd_fsname[0] == '\0') {
740 fprintf(stderr, "Must specify --fsname for MDT/OST device\n");
745 /* These are the permanent mount options (always included) */
746 ret = osd_prepare_lustre(&mop,
747 wanted_mountopts, sizeof(wanted_mountopts));
750 fprintf(stderr, "unable to prepare backend (%d)\n", ret);
755 trim_mountfsoptions(mountopts);
756 if (check_mountfsoptions(mountopts, wanted_mountopts)) {
760 snprintf(ldd->ldd_mount_opts, sizeof(ldd->ldd_mount_opts),
764 if (ldd->ldd_mount_opts[0] == 0)
765 /* use the defaults unless old opts exist */
768 snprintf(ldd->ldd_mount_opts,
769 sizeof(ldd->ldd_mount_opts),
770 "%s", wanted_mountopts);
771 trim_mountfsoptions(ldd->ldd_mount_opts);
775 ret = osd_fix_mountopts(&mop, ldd->ldd_mount_opts,
776 sizeof(ldd->ldd_mount_opts));
779 fprintf(stderr, "unable to fix mountfsoptions (%d)\n", ret);
783 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
784 ldd->ldd_fsname, ldd->ldd_svname);
787 print_ldd("Permanent disk data", ldd);
790 printf("exiting before disk write.\n");
794 if (check_mtab_entry(mop.mo_device, mop.mo_device, NULL, NULL))
797 /* Create the loopback file */
798 if (mop.mo_flags & MO_IS_LOOP) {
799 ret = access(mop.mo_device, F_OK);
804 /* Reformat the loopback file */
805 if (ret != 0 || (mop.mo_flags & MO_FORCEFORMAT)) {
806 ret = loop_format(&mop);
812 ret = loop_setup(&mop);
815 fprintf(stderr, "Loop device setup for %s failed: %s\n",
816 mop.mo_device, strerror(ret));
822 /* Check whether the disk has already been formatted by mkfs.lustre */
823 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
824 ret = osd_is_lustre(mop.mo_device, &mount_type);
827 fprintf(stderr, "Device %s was previously formatted "
828 "for lustre. Use --reformat to reformat it, "
829 "or tunefs.lustre to modify.\n",
835 /* Format the backing filesystem */
836 ret = osd_make_lustre(&mop);
839 fprintf(stderr, "mkfs failed %d\n", ret);
843 /* update svname with '=' to refresh config */
844 if (ldd->ldd_flags & LDD_F_WRITECONF) {
845 struct mount_opts opts;
847 opts.mo_source = mop.mo_device;
848 (void) osd_label_lustre(&opts);
851 /* Enable quota accounting */
852 if (mop.mo_flags & MO_QUOTA) {
853 ret = osd_enable_quota(&mop);
858 /* Write our config files */
859 ret = osd_write_ldd(&mop);
862 fprintf(stderr, "failed to write local files\n");
868 ret2 = loop_cleanup(&mop);
872 /* Fix any crazy return values from system() */
873 if (ret != 0 && ((ret & 255) == 0))
877 verrprint("%s: exiting with %d (%s)\n",
878 progname, ret, strerror(ret));