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"
86 #define MAX_LOOP_DEVICES 16
87 #define L_BLOCK_SIZE 4096
88 #define INDEX_UNASSIGNED 0xFFFF
89 #define MO_IS_LOOP 0x01
90 #define MO_FORCEFORMAT 0x02
92 /* used to describe the options to format the lustre disk, not persistent */
94 struct lustre_disk_data mo_ldd; /* to be written in MOUNT_DATA_FILE */
95 char mo_device[128]; /* disk device name */
96 char mo_loopdev[128]; /* in case a loop dev is needed */
97 char mo_mkfsopts[512]; /* options to the backing-store mkfs */
98 __u64 mo_device_sz; /* in KB */
101 int mo_mgs_failnodes;
106 static int print_only = 0;
107 static int failover = 0;
108 static int upgrade_to_18 = 0;
110 void usage(FILE *out)
112 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
113 fprintf(out, "usage: %s <target types> [options] <device>\n", progname);
115 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
117 "\t\t--ost: object storage, mutually exclusive with mdt,mgs\n"
118 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
119 "\t\t--mgs: configuration management service - one per site\n"
120 "\toptions (in order of popularity):\n"
121 "\t\t--mgsnode=<nid>[,<...>] : NID(s) of a remote mgs node\n"
122 "\t\t\trequired for all targets other than the mgs node\n"
123 "\t\t--fsname=<filesystem_name> : default is 'lustre'\n"
124 "\t\t--failnode=<nid>[,<...>] : NID(s) of a failover partner\n"
125 "\t\t\tcannot be used with --servicenode\n"
126 "\t\t--servicenode=<nid>[,<...>] : NID(s) of all service partners\n"
127 "\t\t\ttreat all nodes as equal service node, cannot be used with --failnode\n"
128 "\t\t--param <key>=<value> : set a permanent parameter\n"
129 "\t\t\te.g. --param sys.timeout=40\n"
130 "\t\t\t --param lov.stripesize=2M\n"
131 "\t\t--index=#N : target index (i.e. ost index within lov)\n"
132 "\t\t--comment=<user comment>: arbitrary string (%d bytes)\n"
133 "\t\t--mountfsoptions=<opts> : permanent mount options\n"
134 "\t\t--network=<net>[,<...>] : restrict OST/MDT to network(s)\n"
136 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
137 "\t\t--device-size=#N(KB) : device size for loop devices\n"
138 "\t\t--mkfsoptions=<opts> : format options\n"
139 "\t\t--reformat: overwrite an existing disk\n"
140 "\t\t--stripe-count-hint=#N : for optimizing MDT inode size\n"
141 "\t\t--iam-dir: use IAM directory format, not ext3 compatible\n"
143 "\t\t--erase-params : erase all old parameter settings\n"
144 "\t\t--nomgs: turn off MGS service on this MDT\n"
145 "\t\t--writeconf: erase all config logs for this fs.\n"
147 "\t\t--dryrun: just report what we would do; "
148 "don't write to disk\n"
149 "\t\t--verbose : e.g. show mkfs progress\n"
151 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
155 #define vprint if (verbose > 0) printf
156 #define verrprint if (verbose >= 0) printf
158 /*================ utility functions =====================*/
160 char *strscat(char *dst, char *src, int buflen) {
162 if (strlen(dst) + strlen(src) >= buflen) {
163 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
164 "\n", buflen, dst, src);
167 return strcat(dst, src);
171 char *strscpy(char *dst, char *src, int buflen) {
173 return strscat(dst, src, buflen);
177 dev_major (unsigned long long int __dev)
179 return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
183 dev_minor (unsigned long long int __dev)
185 return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
190 static int version = 0;
194 char release[4] = "";
196 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
198 fprintf(stderr, "%s: Warning: Can't resolve kernel "
199 "version, assuming 2.6\n", progname);
201 if (read(fd, release, 4) < 0) {
202 fprintf(stderr, "reading from /proc/sys/kernel"
203 "/osrelease: %s\n", strerror(errno));
209 if (strncmp(release, "2.4.", 4) == 0)
217 static int check_mtab_entry(char *spec)
222 fp = setmntent(MOUNTED, "r");
226 while ((mnt = getmntent(fp)) != NULL) {
227 if (strcmp(mnt->mnt_fsname, spec) == 0) {
229 fprintf(stderr, "%s: according to %s %s is "
230 "already mounted on %s\n",
231 progname, MOUNTED, spec, mnt->mnt_dir);
240 /*============ disk dev functions ===================*/
242 /* Setup a file in the first unused loop_device */
243 int loop_setup(struct mkfs_opts *mop)
249 /* Figure out the loop device names */
250 if (!access("/dev/loop0", F_OK | R_OK)) {
251 strcpy(loop_base, "/dev/loop\0");
252 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
253 strcpy(loop_base, "/dev/loop/\0");
255 fprintf(stderr, "%s: can't access loop devices\n", progname);
259 /* Find unused loop device */
260 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
262 int cmdsz = sizeof(cmd);
264 sprintf(l_device, "%s%d", loop_base, i);
265 if (access(l_device, F_OK | R_OK))
267 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
270 /* losetup gets 1 (ret=256) for non-set-up device */
272 /* Set up a loopback device to our file */
273 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
275 ret = run_command(cmd, cmdsz);
277 /* someone else picked up this loop device
281 fprintf(stderr, "%s: error %d on losetup: %s\n",
282 progname, ret, strerror(ret));
285 strscpy(mop->mo_loopdev, l_device,
286 sizeof(mop->mo_loopdev));
291 fprintf(stderr, "%s: out of loop devices!\n", progname);
295 int loop_cleanup(struct mkfs_opts *mop)
299 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
300 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
301 ret = run_command(cmd, sizeof(cmd));
306 /* return canonicalized absolute pathname, even if the target file does not
307 * exist, unlike realpath */
308 static char *absolute_path(char *devname)
310 char buf[PATH_MAX + 1];
314 path = malloc(PATH_MAX + 1);
318 if (devname[0] != '/') {
319 if (getcwd(buf, sizeof(buf) - 1) == NULL)
322 strcat(buf, devname);
324 strcpy(buf, devname);
326 /* truncate filename before calling realpath */
327 ptr = strrchr(buf, '/');
333 if (path != realpath(buf, path)) {
337 /* add the filename back */
339 strcat(path, ptr + 1);
343 /* Determine if a device is a block device (as opposed to a file) */
344 int is_block(char* devname)
350 devpath = absolute_path(devname);
351 if (devpath == NULL) {
352 fprintf(stderr, "%s: failed to resolve path to %s\n",
357 ret = access(devname, F_OK);
359 if (strncmp(devpath, "/dev/", 5) == 0) {
360 /* nobody sane wants to create a loopback file under
361 * /dev. Let's just report the device doesn't exist */
362 fprintf(stderr, "%s: %s apparently does not exist\n",
370 ret = stat(devpath, &st);
372 fprintf(stderr, "%s: cannot stat %s\n", progname, devpath);
375 ret = S_ISBLK(st.st_mode);
381 __u64 get_device_size(char* device)
386 fd = open(device, O_RDONLY);
388 fprintf(stderr, "%s: cannot open %s: %s\n",
389 progname, device, strerror(errno));
394 /* size in bytes. bz5831 */
395 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
400 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
401 size = (__u64)lsize * 512;
406 fprintf(stderr, "%s: size ioctl failed: %s\n",
407 progname, strerror(errno));
411 vprint("device size = "LPU64"MB\n", size >> 20);
412 /* return value in KB */
416 int loop_format(struct mkfs_opts *mop)
420 if (mop->mo_device_sz == 0) {
422 fprintf(stderr, "loop device requires a --device-size= "
427 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
430 fprintf(stderr, "%s: Unable to create backing store: %s\n",
431 progname, strerror(errno));
435 if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
438 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
439 progname, strerror(errno));
447 /* Display the need for the latest e2fsprogs to be installed. make_backfs
448 * indicates if the caller is make_lustre_backfs() or not. */
449 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs)
451 static int msg_displayed;
454 fprintf(stderr, "WARNING: %s does not support %s "
455 "feature.\n\n", E2FSPROGS, feature);
461 fprintf(stderr, "WARNING: The %s package currently installed on "
462 "your system does not support \"%s\" feature.\n",
464 #if !(HAVE_LDISKFSPROGS)
465 fprintf(stderr, "Please install the latest version of e2fsprogs from\n"
466 "http://downloads.whamcloud.com/public/e2fsprogs/latest/\n"
467 "to enable this feature.\n");
470 fprintf(stderr, "Feature will not be enabled until %s"
471 "is updated and '%s -O %s %%{device}' "
472 "is run.\n\n", E2FSPROGS, TUNE2FS, feature);
475 /* Check whether the file exists in the device */
476 static int file_in_dev(char *file_name, char *dev_name)
479 char debugfs_cmd[256];
480 unsigned int inode_num;
483 /* Construct debugfs command line. */
484 snprintf(debugfs_cmd, sizeof(debugfs_cmd),
485 "%s -c -R 'stat %s' '%s' 2>&1 | egrep '(Inode|unsupported)'",
486 DEBUGFS, file_name, dev_name);
488 fp = popen(debugfs_cmd, "r");
490 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
494 if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
498 i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
501 fprintf(stderr, "%s", debugfs_cmd);
502 if (strstr(debugfs_cmd, "unsupported feature")) {
503 disp_old_e2fsprogs_msg("an unknown", 0);
512 /* Check whether the device has already been used with lustre */
513 static int is_lustre_target(struct mkfs_opts *mop)
517 vprint("checking for existing Lustre data: ");
519 if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))) {
521 (rc == 1) ? MOUNT_DATA_FILE : "extents");
522 /* in the -1 case, 'extents' means this really IS a lustre
527 if ((rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
528 vprint("found %s\n", LAST_RCVD);
532 vprint("not found\n");
533 return 0; /* The device is not a lustre target. */
536 /* Check if a certain feature is supported by e2fsprogs.
537 * Firstly we try to use "debugfs supported_features" command to check if
538 * the feature is supported. If this fails we try to set this feature with
539 * mke2fs to check for its support. */
540 static int is_e2fsprogs_feature_supp(const char *feature)
542 static char supp_features[4096] = "";
545 char imgname[] = "/tmp/test-img-XXXXXX";
549 if (supp_features[0] == '\0') {
550 snprintf(cmd, sizeof(cmd), "%s -c -R supported_features 2>&1",
553 /* Using popen() instead of run_command() since debugfs does
554 * not return proper error code if command is not supported */
555 fp = popen(cmd, "r");
557 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
560 ret = fread(supp_features, 1, sizeof(supp_features), fp);
563 if (ret > 0 && strstr(supp_features,
564 strncmp(feature, "-O ", 3) ? feature : feature+3))
567 if ((fd = mkstemp(imgname)) < 0)
572 snprintf(cmd, sizeof(cmd), "%s -F %s %s 100 >/dev/null 2>&1",
573 MKE2FS, feature, imgname);
574 /* run_command() displays the output of mke2fs when it fails for
575 * some feature, so use system() directly */
583 * append_unique: append @key or @key=@val pair to @buf only if @key does not
585 * @buf: buffer to hold @key or @key=@val
586 * @prefix: prefix string before @key
588 * @val: value string if it's a @key=@val pair
590 static void append_unique(char *buf, char *prefix, char *key, char *val,
599 anchor = end = strstr(buf, key);
600 /* try to find exact match string in @buf */
601 while (end && *end != '\0' && *end != ',' && *end != ' ' && *end != '=')
604 if (anchor == NULL || strlen(key) != len ||
605 strncmp(anchor, key, len) != 0) {
607 strscat(buf, prefix, maxbuflen);
609 strscat(buf, key, maxbuflen);
611 strscat(buf, "=", maxbuflen);
612 strscat(buf, val, maxbuflen);
617 static void enable_default_ext4_features(struct mkfs_opts *mop, char *anchor,
618 size_t maxbuflen, int user_spec)
620 if (IS_OST(&mop->mo_ldd)) {
621 append_unique(anchor, user_spec ? "," : " -O ",
622 "extents", NULL, sizeof(mop->mo_mkfsopts));
623 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
624 } else if (IS_MDT(&mop->mo_ldd)) {
625 append_unique(anchor, user_spec ? "," : " -O ",
626 "dirdata", NULL, maxbuflen);
627 append_unique(anchor, ",", "uninit_bg", NULL, maxbuflen);
628 append_unique(anchor, ",", "^extents", NULL, maxbuflen);
630 append_unique(anchor, user_spec ? "," : " -O ",
631 "uninit_bg", NULL, maxbuflen);
634 /* Multiple mount protection enabled only if failover node specified */
636 if (is_e2fsprogs_feature_supp("-O mmp") == 0)
637 append_unique(anchor, ",", "mmp", NULL, maxbuflen);
639 disp_old_e2fsprogs_msg("mmp", 1);
642 /* Allow more than 65000 subdirectories */
643 if (is_e2fsprogs_feature_supp("-O dir_nlink") == 0)
644 append_unique(anchor, ",", "dir_nlink", NULL, maxbuflen);
646 #ifdef HAVE_EXT4_LDISKFS
647 /* The following options are only valid for ext4-based ldiskfs.
648 * If --backfstype=ext3 is specified, do not enable them. */
649 if (mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3)
652 /* Allow files larger than 2TB. Also needs LU-16, but not harmful. */
653 if (is_e2fsprogs_feature_supp("-O huge_file") == 0)
654 append_unique(anchor, ",", "huge_file", NULL, maxbuflen);
656 /* Enable large block addresses if the LUN is over 2^32 blocks. */
657 if (mop->mo_device_sz / (L_BLOCK_SIZE >> 10) >= 0x100002000ULL &&
658 is_e2fsprogs_feature_supp("-O 64bit") == 0)
659 append_unique(anchor, ",", "64bit", NULL, maxbuflen);
661 /* Cluster inode/block bitmaps and inode table for more efficient IO.
662 * Align the flex groups on a 1MB boundary for better performance. */
663 /* This -O feature needs to go last, since it adds the "-G" option. */
664 if (is_e2fsprogs_feature_supp("-O flex_bg") == 0) {
667 append_unique(anchor, ",", "flex_bg", NULL, maxbuflen);
669 if (IS_OST(&mop->mo_ldd)) {
670 snprintf(tmp_buf, sizeof(tmp_buf), " -G %u",
671 (1 << 20) / L_BLOCK_SIZE);
672 strscat(anchor, tmp_buf, maxbuflen);
675 /* Don't add any more "-O" options here, see last comment above */
680 * moveopts_to_end: find the option string, move remaining strings to
681 * where option string starts, and append the option
683 * @start: where the option string starts before the move
684 * RETURN: where the option string starts after the move
686 static char *moveopts_to_end(char *start)
691 /* skip whitespace before options */
696 /* find end of option characters */
697 while (*end != ' ' && *end != '\0')
701 strncpy(save, start, end - start);
702 save[end - start] = '\0';
704 /* move remaining options up front */
706 memmove(start, end, strlen(end));
707 *(start + strlen(end)) = '\0';
709 /* append the specified options */
710 if (*(start + strlen(start) - 1) != ' ')
712 idx = start + strlen(start);
718 /* Build fs according to type */
719 int make_lustre_backfs(struct mkfs_opts *mop)
721 __u64 device_sz = mop->mo_device_sz, block_count = 0;
722 char mkfs_cmd[PATH_MAX];
726 int ret = 0, ext_opts = 0;
729 if (!(mop->mo_flags & MO_IS_LOOP)) {
730 mop->mo_device_sz = get_device_size(mop->mo_device);
732 if (mop->mo_device_sz == 0)
735 /* Compare to real size */
736 if (device_sz == 0 || device_sz > mop->mo_device_sz)
737 device_sz = mop->mo_device_sz;
739 mop->mo_device_sz = device_sz;
742 if (mop->mo_device_sz != 0) {
743 if (mop->mo_device_sz < 8096){
744 fprintf(stderr, "%s: size of filesystem must be larger "
745 "than 8MB, but is set to %lldKB\n",
746 progname, (long long)mop->mo_device_sz);
749 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
750 /* If the LUN size is just over 2^32 blocks, limit the
751 * filesystem size to 2^32-1 blocks to avoid problems with
752 * ldiskfs/mkfs not handling this size. Bug 22906 */
753 if (block_count > 0xffffffffULL && block_count < 0x100002000ULL)
754 block_count = 0xffffffffULL;
757 if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
758 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS) ||
759 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS2)) {
762 /* Journal size in MB */
763 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
764 /* Choose our own default journal size */
765 long journal_sz = 0, max_sz;
766 if (device_sz > 1024 * 1024) /* 1GB */
767 journal_sz = (device_sz / 102400) * 4;
768 /* cap journal size at 1GB */
769 if (journal_sz > 1024L)
772 max_sz = (102400 * L_BLOCK_SIZE) >> 20; /* 400MB */
773 if (journal_sz > max_sz)
776 sprintf(buf, " -J size=%ld", journal_sz);
777 strscat(mop->mo_mkfsopts, buf,
778 sizeof(mop->mo_mkfsopts));
782 /* Inode size (for extended attributes). The LOV EA size is
783 * 32 (EA hdr) + 32 (lov_mds_md) + stripes * 24 (lov_ost_data),
784 * and we want some margin above that for ACLs, other EAs... */
785 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
786 if (IS_MDT(&mop->mo_ldd)) {
787 if (mop->mo_stripe_count > 72)
788 inode_size = 512; /* bz 7241 */
789 /* see also "-i" below for EA blocks */
790 else if (mop->mo_stripe_count > 32)
792 else if (mop->mo_stripe_count > 10)
796 } else if (IS_OST(&mop->mo_ldd)) {
797 /* We store MDS FID and OST objid in EA on OST
798 * we need to make inode bigger as well. */
802 if (inode_size > 0) {
803 sprintf(buf, " -I %ld", inode_size);
804 strscat(mop->mo_mkfsopts, buf,
805 sizeof(mop->mo_mkfsopts));
809 /* Bytes_per_inode: disk size / num inodes */
810 if (strstr(mop->mo_mkfsopts, "-i") == NULL &&
811 strstr(mop->mo_mkfsopts, "-N") == NULL) {
812 long bytes_per_inode = 0;
814 /* Allocate more inodes on MDT devices. There is
815 * no data stored on the MDT, and very little extra
816 * metadata beyond the inode. It could go down as
817 * low as 1024 bytes, but this is conservative.
818 * Account for external EA blocks for wide striping. */
819 if (IS_MDT(&mop->mo_ldd)) {
820 bytes_per_inode = inode_size + 1536;
822 if (mop->mo_stripe_count > 72) {
823 int extra = mop->mo_stripe_count * 24;
824 extra = ((extra - 1) | 4095) + 1;
825 bytes_per_inode += extra;
829 /* Allocate fewer inodes on large OST devices. Most
830 * filesystems can be much more aggressive than even
831 * this, but it is impossible to know in advance. */
832 if (IS_OST(&mop->mo_ldd)) {
833 /* OST > 16TB assume average file size 1MB */
834 if (device_sz > (16ULL << 30))
835 bytes_per_inode = 1024 * 1024;
836 /* OST > 4TB assume average file size 512kB */
837 else if (device_sz > (4ULL << 30))
838 bytes_per_inode = 512 * 1024;
839 /* OST > 1TB assume average file size 256kB */
840 else if (device_sz > (1ULL << 30))
841 bytes_per_inode = 256 * 1024;
842 /* OST > 10GB assume average file size 64kB,
843 * plus a bit so that inodes will fit into a
844 * 256x flex_bg without overflowing */
845 else if (device_sz > (10ULL << 20))
846 bytes_per_inode = 69905;
849 if (bytes_per_inode > 0) {
850 sprintf(buf, " -i %ld", bytes_per_inode);
851 strscat(mop->mo_mkfsopts, buf,
852 sizeof(mop->mo_mkfsopts));
857 strscat(mop->mo_mkfsopts, " -q",
858 sizeof(mop->mo_mkfsopts));
861 /* start handle -O mkfs options */
862 if ((start = strstr(mop->mo_mkfsopts, "-O")) != NULL) {
863 if (strstr(start + 2, "-O") != NULL) {
865 "%s: don't specify multiple -O options\n",
869 start = moveopts_to_end(start);
870 maxbuflen = sizeof(mop->mo_mkfsopts) -
871 (start - mop->mo_mkfsopts) - strlen(start);
872 enable_default_ext4_features(mop, start, maxbuflen, 1);
874 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts),
875 maxbuflen = sizeof(mop->mo_mkfsopts) -
876 strlen(mop->mo_mkfsopts);
877 enable_default_ext4_features(mop, start, maxbuflen, 0);
879 /* end handle -O mkfs options */
881 /* start handle -E mkfs options */
882 if ((start = strstr(mop->mo_mkfsopts, "-E")) != NULL) {
883 if (strstr(start + 2, "-E") != NULL) {
885 "%s: don't specify multiple -E options\n",
889 start = moveopts_to_end(start);
890 maxbuflen = sizeof(mop->mo_mkfsopts) -
891 (start - mop->mo_mkfsopts) - strlen(start);
894 start = mop->mo_mkfsopts + strlen(mop->mo_mkfsopts);
895 maxbuflen = sizeof(mop->mo_mkfsopts) -
896 strlen(mop->mo_mkfsopts);
899 /* In order to align the filesystem metadata on 1MB boundaries,
900 * give a resize value that will reserve a power-of-two group
901 * descriptor blocks, but leave one block for the superblock.
902 * Only useful for filesystems with < 2^32 blocks due to resize
904 if (IS_OST(&mop->mo_ldd) && mop->mo_device_sz > 100 * 1024 &&
905 mop->mo_device_sz * 1024 / L_BLOCK_SIZE <= 0xffffffffULL) {
906 unsigned group_blocks = L_BLOCK_SIZE * 8;
907 unsigned desc_per_block = L_BLOCK_SIZE / 32;
908 unsigned resize_blks;
910 resize_blks = (1ULL<<32) - desc_per_block*group_blocks;
911 snprintf(buf, sizeof(buf), "%u", resize_blks);
912 append_unique(start, ext_opts ? "," : " -E ",
913 "resize", buf, maxbuflen);
917 /* Avoid zeroing out the full journal - speeds up mkfs */
918 if (is_e2fsprogs_feature_supp("-E lazy_journal_init") == 0)
919 append_unique(start, ext_opts ? "," : " -E ",
920 "lazy_journal_init", NULL, maxbuflen);
921 /* end handle -E mkfs options */
923 /* Allow reformat of full devices (as opposed to
924 partitions.) We already checked for mounted dev. */
925 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
927 snprintf(mkfs_cmd, sizeof(mkfs_cmd),
928 "%s -j -b %d -L %s ", MKE2FS, L_BLOCK_SIZE,
929 mop->mo_ldd.ldd_svname);
931 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
932 progname, mop->mo_ldd.ldd_mount_type,
933 MT_STR(&mop->mo_ldd));
937 /* For loop device format the dev, not the filename */
938 dev = mop->mo_device;
939 if (mop->mo_flags & MO_IS_LOOP)
940 dev = mop->mo_loopdev;
942 vprint("formatting backing filesystem %s on %s\n",
943 MT_STR(&mop->mo_ldd), dev);
944 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
945 vprint("\t4k blocks "LPU64"\n", block_count);
946 vprint("\toptions %s\n", mop->mo_mkfsopts);
948 /* mkfs_cmd's trailing space is important! */
949 strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
950 strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
951 strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
952 if (block_count != 0) {
953 sprintf(buf, " "LPU64, block_count);
954 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
957 vprint("mkfs_cmd = %s\n", mkfs_cmd);
958 ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
961 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
966 /* ==================== Lustre config functions =============*/
968 void print_ldd(char *str, struct lustre_disk_data *ldd)
970 printf("\n %s:\n", str);
971 printf("Target: %s\n", ldd->ldd_svname);
972 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
973 printf("Index: unassigned\n");
975 printf("Index: %d\n", ldd->ldd_svindex);
976 if (ldd->ldd_uuid[0])
977 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
978 printf("Lustre FS: %s\n", ldd->ldd_fsname);
979 printf("Mount type: %s\n", MT_STR(ldd));
980 printf("Flags: %#x\n", ldd->ldd_flags);
981 printf(" (%s%s%s%s%s%s%s%s%s%s)\n",
982 IS_MDT(ldd) ? "MDT ":"",
983 IS_OST(ldd) ? "OST ":"",
984 IS_MGS(ldd) ? "MGS ":"",
985 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
986 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
987 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
988 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
989 ldd->ldd_flags & LDD_F_IAM_DIR ? "IAM_dir_format ":"",
990 ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
991 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
992 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
993 printf("Parameters:%s\n", ldd->ldd_params);
994 if (ldd->ldd_userdata[0])
995 printf("Comment: %s\n", ldd->ldd_userdata);
999 static int touch_file(char *filename)
1003 if (filename == NULL) {
1007 fd = open(filename, O_CREAT | O_TRUNC, 0600);
1016 /* keep it less than LL_FID_NAMELEN */
1017 #define DUMMY_FILE_NAME_LEN 25
1018 #define EXT3_DIRENT_SIZE DUMMY_FILE_NAME_LEN
1020 /* Need to add these many entries to this directory to make HTREE dir. */
1021 #define MIN_ENTRIES_REQ_FOR_HTREE ((L_BLOCK_SIZE / EXT3_DIRENT_SIZE))
1023 static int add_dummy_files(char *dir)
1025 char fpname[PATH_MAX];
1029 for (i = 0; i < MIN_ENTRIES_REQ_FOR_HTREE; i++) {
1030 snprintf(fpname, PATH_MAX, "%s/%0*d", dir,
1031 DUMMY_FILE_NAME_LEN, i);
1033 rc = touch_file(fpname);
1034 if (rc && rc != -EEXIST) {
1036 "%s: Can't create dummy file %s: %s\n",
1037 progname, fpname , strerror(errno));
1044 static int __l_mkdir(char * filepnm, int mode , struct mkfs_opts *mop)
1048 ret = mkdir(filepnm, mode);
1049 if (ret && ret != -EEXIST)
1052 /* IAM mode supports ext3 directories of HTREE type only. So add dummy
1053 * entries to new directory to create htree type of container for
1054 * this directory. */
1055 if (mop->mo_ldd.ldd_flags & LDD_F_IAM_DIR)
1056 return add_dummy_files(filepnm);
1060 /* Write the server config files */
1061 int write_local_files(struct mkfs_opts *mop)
1063 char mntpt[] = "/tmp/mntXXXXXX";
1070 /* Mount this device temporarily in order to write these files */
1071 if (!mkdtemp(mntpt)) {
1072 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
1073 progname, mntpt, strerror(errno));
1077 dev = mop->mo_device;
1078 if (mop->mo_flags & MO_IS_LOOP)
1079 dev = mop->mo_loopdev;
1081 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0,
1082 mop->mo_ldd.ldd_mount_opts);
1084 fprintf(stderr, "%s: Unable to mount %s: %s\n",
1085 progname, dev, strerror(errno));
1087 if (errno == ENODEV) {
1088 fprintf(stderr, "Is the %s module available?\n",
1089 MT_STR(&mop->mo_ldd));
1094 /* Set up initial directories */
1095 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
1096 ret = __l_mkdir(filepnm, 0777, mop);
1097 if ((ret != 0) && (errno != EEXIST)) {
1098 fprintf(stderr, "%s: Can't make configs dir %s (%s)\n",
1099 progname, filepnm, strerror(errno));
1101 } else if (errno == EEXIST) {
1105 /* Save the persistent mount data into a file. Lustre must pre-read
1106 this file to get the real mount options. */
1107 vprint("Writing %s\n", MOUNT_DATA_FILE);
1108 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
1109 filep = fopen(filepnm, "w");
1111 fprintf(stderr, "%s: Unable to create %s file: %s\n",
1112 progname, filepnm, strerror(errno));
1115 num = fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
1116 if (num < 1 && ferror(filep)) {
1117 fprintf(stderr, "%s: Unable to write to file (%s): %s\n",
1118 progname, filepnm, strerror(errno));
1124 /* Check for upgrade */
1125 if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS))
1126 == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
1129 int cmdsz = sizeof(cmd);
1130 vprint("Copying old logs\n");
1132 /* Copy the old client log to fsname-client */
1133 sprintf(filepnm, "%s/%s/%s-client",
1134 mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
1135 snprintf(cmd, cmdsz, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
1137 ret = run_command(cmd, cmdsz);
1139 fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
1140 "(%d)\n", progname, MDT_LOGS_DIR, ret);
1141 fprintf(stderr, "mount -t ldiskfs %s somewhere, "
1142 "find the client log for fs %s and "
1143 "copy it manually into %s/%s-client, "
1146 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
1147 mop->mo_ldd.ldd_fsname);
1151 /* We need to use the old mdt log because otherwise mdt won't
1152 have complete lov if old clients connect before all
1154 /* Copy the old mdt log to fsname-MDT0000 (get old
1155 name from mdt_UUID) */
1157 strscpy(filepnm, (char *)mop->mo_ldd.ldd_uuid, sizeof(filepnm));
1158 term = strstr(filepnm, "_UUID");
1161 snprintf(cmd, cmdsz, "cp %s/%s/%s %s/%s/%s",
1162 mntpt, MDT_LOGS_DIR, filepnm,
1163 mntpt, MOUNT_CONFIGS_DIR,
1164 mop->mo_ldd.ldd_svname);
1165 ret = run_command(cmd, cmdsz);
1168 fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
1169 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
1170 fprintf(stderr, "mount -t ext3 %s somewhere, "
1171 "find the MDT log for fs %s and "
1172 "copy it manually into %s/%s, "
1175 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
1176 mop->mo_ldd.ldd_svname);
1181 /* end COMPAT_146 */
1190 int read_local_files(struct mkfs_opts *mop)
1192 char tmpdir[] = "/tmp/dirXXXXXX";
1198 int cmdsz = sizeof(cmd);
1200 /* Make a temporary directory to hold Lustre data files. */
1201 if (!mkdtemp(tmpdir)) {
1202 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
1203 progname, tmpdir, strerror(errno));
1207 dev = mop->mo_device;
1209 /* TODO: it's worth observing the get_mountdata() function that is
1210 in mount_utils.c for getting the mountdata out of the
1213 /* Construct debugfs command line. */
1214 snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s/mountdata' '%s'",
1215 DEBUGFS, MOUNT_DATA_FILE, tmpdir, dev);
1217 ret = run_command(cmd, cmdsz);
1219 verrprint("%s: Unable to dump %s dir (%d)\n",
1220 progname, MOUNT_CONFIGS_DIR, ret);
1222 sprintf(filepnm, "%s/mountdata", tmpdir);
1223 filep = fopen(filepnm, "r");
1226 vprint("Reading %s\n", MOUNT_DATA_FILE);
1227 num_read = fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
1228 if (num_read < 1 && ferror(filep)) {
1229 fprintf(stderr, "%s: Unable to read from file %s: %s\n",
1230 progname, filepnm, strerror(errno));
1235 /* Try to read pre-1.6 config from last_rcvd */
1236 struct lr_server_data lsd;
1237 verrprint("%s: Unable to read %d.%d config %s.\n",
1238 progname, LUSTRE_MAJOR, LUSTRE_MINOR, filepnm);
1240 verrprint("Trying 1.4 config from last_rcvd\n");
1241 sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
1243 /* Construct debugfs command line. */
1244 snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s' %s",
1245 DEBUGFS, LAST_RCVD, filepnm, dev);
1247 ret = run_command(cmd, cmdsz);
1249 fprintf(stderr, "%s: Unable to dump %s file (%d)\n",
1250 progname, LAST_RCVD, ret);
1254 filep = fopen(filepnm, "r");
1256 fprintf(stderr, "%s: Unable to open %s: %s\n",
1257 progname, filepnm, strerror(errno));
1259 verrprint("Contents of %s:\n", tmpdir);
1261 snprintf(cmd, cmdsz, "ls -l %s/", tmpdir);
1262 run_command(cmd, cmdsz);
1263 verrprint("Contents of disk:\n");
1264 snprintf(cmd, cmdsz, "%s -c -R 'ls -l /' %s",
1266 run_command(cmd, cmdsz);
1270 vprint("Reading %s\n", LAST_RCVD);
1271 ret = fread(&lsd, 1, sizeof(lsd), filep);
1272 if (ret < sizeof(lsd)) {
1273 fprintf(stderr, "%s: Short read (%d of %d)\n",
1274 progname, ret, (int)sizeof(lsd));
1275 ret = ferror(filep);
1279 vprint("Feature compat=%x, incompat=%x\n",
1280 lsd.lsd_feature_compat, lsd.lsd_feature_incompat);
1282 if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
1283 (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
1284 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
1285 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
1286 } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
1287 (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
1288 /* We must co-locate so mgs can see old logs.
1289 If user doesn't want this, they can copy the old
1290 logs manually and re-tunefs. */
1291 mop->mo_ldd.ldd_flags =
1292 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
1293 mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
1295 /* If neither is set, we're pre-1.4.6, make a guess. */
1296 /* Construct debugfs command line. */
1297 snprintf(cmd, cmdsz, "%s -c -R 'rdump /%s %s' %s",
1298 DEBUGFS, MDT_LOGS_DIR, tmpdir, dev);
1299 run_command(cmd, cmdsz);
1301 sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
1302 if (lsd.lsd_ost_index > 0) {
1303 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
1304 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
1306 /* If there's a LOGS dir, it's an MDT */
1307 if ((ret = access(filepnm, F_OK)) == 0) {
1308 mop->mo_ldd.ldd_flags =
1311 /* Old MDT's are always index 0
1313 mop->mo_ldd.ldd_svindex = 0;
1315 /* The index may not be correct */
1316 mop->mo_ldd.ldd_flags =
1317 LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
1318 verrprint("OST with unknown index\n");
1324 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid,
1325 sizeof(mop->mo_ldd.ldd_uuid));
1326 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
1328 /* end COMPAT_146 */
1333 snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
1334 run_command(cmd, cmdsz);
1336 verrprint("Failed to read old data (%d)\n", ret);
1341 void set_defaults(struct mkfs_opts *mop)
1343 mop->mo_ldd.ldd_magic = LDD_MAGIC;
1344 mop->mo_ldd.ldd_config_ver = 1;
1345 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
1346 mop->mo_mgs_failnodes = 0;
1347 strcpy(mop->mo_ldd.ldd_fsname, "lustre");
1348 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
1350 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
1351 mop->mo_stripe_count = 1;
1354 static inline void badopt(const char *opt, char *type)
1356 fprintf(stderr, "%s: '--%s' only valid for %s\n",
1357 progname, opt, type);
1361 static int add_param(char *buf, char *key, char *val)
1363 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
1364 int start = strlen(buf);
1368 keylen = strlen(key);
1369 if (start + 1 + keylen + strlen(val) >= end) {
1370 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
1371 progname, buf, key ? key : "", val);
1375 sprintf(buf + start, " %s%s", key ? key : "", val);
1379 /* from mount_lustre */
1380 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
1381 #define MAXNIDSTR 1024
1382 static char *convert_hostnames(char *s1)
1384 char *converted, *s2 = 0, *c, *end, sep;
1385 int left = MAXNIDSTR;
1388 converted = malloc(left);
1389 if (converted == NULL) {
1393 end = s1 + strlen(s1);
1395 while ((left > 0) && (s1 < end)) {
1396 s2 = strpbrk(s1, ",:");
1401 nid = libcfs_str2nid(s1);
1403 if (nid == LNET_NID_ANY) {
1404 fprintf(stderr, "%s: Can't parse NID '%s'\n",
1409 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
1410 strlen("127.0.0.1")) == 0) {
1411 fprintf(stderr, "%s: The NID '%s' resolves to the "
1412 "loopback address '%s'. Lustre requires a "
1413 "non-loopback address.\n",
1414 progname, s1, libcfs_nid2str(nid));
1419 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
1420 left = converted + MAXNIDSTR - c;
1426 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
1429 static struct option long_opt[] = {
1430 {"iam-dir", 0, 0, 'a'},
1431 {"backfstype", 1, 0, 'b'},
1432 {"stripe-count-hint", 1, 0, 'c'},
1433 {"comment", 1, 0, 'u'},
1434 {"configdev", 1, 0, 'C'},
1435 {"device-size", 1, 0, 'd'},
1436 {"dryrun", 0, 0, 'n'},
1437 {"erase-params", 0, 0, 'e'},
1438 {"failnode", 1, 0, 'f'},
1439 {"failover", 1, 0, 'f'},
1441 {"help", 0, 0, 'h'},
1442 {"index", 1, 0, 'i'},
1443 {"mkfsoptions", 1, 0, 'k'},
1444 {"mgsnode", 1, 0, 'm'},
1445 {"mgsnid", 1, 0, 'm'},
1447 {"fsname",1, 0, 'L'},
1448 {"noformat", 0, 0, 'n'},
1449 {"nomgs", 0, 0, 'N'},
1450 {"mountfsoptions", 1, 0, 'o'},
1452 {"param", 1, 0, 'p'},
1453 {"print", 0, 0, 'n'},
1454 {"quiet", 0, 0, 'q'},
1455 {"reformat", 0, 0, 'r'},
1456 {"servicenode", 1, 0, 's'},
1457 {"verbose", 0, 0, 'v'},
1458 {"writeconf", 0, 0, 'w'},
1459 {"upgrade_to_18", 0, 0, 'U'},
1460 {"network", 1, 0, 't'},
1463 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrs:t:Uu:vw";
1466 int failnode_set = 0, servicenode_set = 0;
1468 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
1472 if (IS_MDT(&mop->mo_ldd))
1473 mop->mo_ldd.ldd_flags |= LDD_F_IAM_DIR;
1478 while (i < LDD_MT_LAST) {
1479 if (strcmp(optarg, mt_str(i)) == 0) {
1480 mop->mo_ldd.ldd_mount_type = i;
1488 if (IS_MDT(&mop->mo_ldd)) {
1489 int stripe_count = atol(optarg);
1490 if (stripe_count <= 0) {
1491 fprintf(stderr, "%s: bad stripe count "
1492 "%d\n", progname, stripe_count);
1495 mop->mo_stripe_count = stripe_count;
1497 badopt(long_opt[longidx].name, "MDT");
1501 case 'C': /* Configdev */
1503 printf("Configdev not implemented\n");
1506 mop->mo_device_sz = atol(optarg);
1509 mop->mo_ldd.ldd_params[0] = '\0';
1510 /* Must update the mgs logs */
1511 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1517 if ((opt == 'f' && servicenode_set)
1518 || (opt == 's' && failnode_set)) {
1519 fprintf(stderr, "%s: %s cannot use with --%s\n",
1520 progname, long_opt[longidx].name,
1521 opt == 'f' ? "servicenode" : "failnode");
1525 nids = convert_hostnames(optarg);
1528 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
1533 /* Must update the mgs logs */
1534 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1538 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
1539 servicenode_set = 1;
1545 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
1551 if (!(mop->mo_ldd.ldd_flags &
1552 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1553 LDD_F_WRITECONF))) {
1554 fprintf(stderr, "%s: cannot change the index of"
1555 " a registered target\n", progname);
1558 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
1559 mop->mo_ldd.ldd_svindex = atol(optarg);
1560 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
1562 badopt(long_opt[longidx].name, "MDT,OST");
1567 strscpy(mop->mo_mkfsopts, optarg,
1568 sizeof(mop->mo_mkfsopts));
1572 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
1573 (!(mop->mo_ldd.ldd_flags &
1574 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1575 LDD_F_WRITECONF)))) {
1576 fprintf(stderr, "%s: cannot change the name of"
1577 " a registered target\n", progname);
1580 if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
1581 fprintf(stderr, "%s: filesystem name must be "
1582 "1-8 chars\n", progname);
1585 if ((tmp = strpbrk(optarg, "/:"))) {
1586 fprintf(stderr, "%s: char '%c' not allowed in "
1587 "filesystem name\n", progname, *tmp);
1590 strscpy(mop->mo_ldd.ldd_fsname, optarg,
1591 sizeof(mop->mo_ldd.ldd_fsname));
1595 char *nids = convert_hostnames(optarg);
1598 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
1603 mop->mo_mgs_failnodes++;
1607 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1613 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1616 *mountopts = optarg;
1619 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1622 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1625 /* Must update the mgs logs */
1626 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1632 mop->mo_flags |= MO_FORCEFORMAT;
1635 if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
1636 badopt(long_opt[longidx].name, "MDT,OST");
1643 rc = add_param(mop->mo_ldd.ldd_params,
1644 PARAM_NETWORK, optarg);
1647 /* Must update the mgs logs */
1648 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1651 strscpy(mop->mo_ldd.ldd_userdata, optarg,
1652 sizeof(mop->mo_ldd.ldd_userdata));
1658 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1666 fprintf(stderr, "Unknown option '%c'\n", opt);
1672 /* Last arg is device */
1673 if (optind != argc - 1) {
1675 fprintf(stderr, "Bad argument: %s\n", argv[optind]);
1679 /* single argument: <device> */
1686 /* Search for opt in mntlist, returning true if found.
1688 static int in_mntlist(char *opt, char *mntlist)
1690 char *ml, *mlp, *item, *ctx = NULL;
1692 if (!(ml = strdup(mntlist))) {
1693 fprintf(stderr, "%s: out of memory\n", progname);
1697 while ((item = strtok_r(mlp, ",", &ctx))) {
1698 if (!strcmp(opt, item))
1703 return (item != NULL);
1706 /* Issue a message on stderr for every item in wanted_mountopts that is not
1707 * present in mountopts. The justwarn boolean toggles between error and
1708 * warning message. Return an error count.
1710 static int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
1713 char *ml, *mlp, *item, *ctx = NULL;
1716 if (!(ml = strdup(wanted_mountopts))) {
1717 fprintf(stderr, "%s: out of memory\n", progname);
1721 while ((item = strtok_r(mlp, ",", &ctx))) {
1722 if (!in_mntlist(item, mountopts)) {
1723 fprintf(stderr, "%s: %s mount option `%s' is missing\n",
1724 progname, justwarn ? "Warning: default"
1725 : "Error: mandatory", item);
1734 /* Trim embedded white space, leading and trailing commas from string s.
1736 static void trim_mountfsoptions(char *s)
1742 memmove(p, p + 1, strlen(p + 1) + 1);
1749 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
1751 p = s + strlen(s) - 1;
1752 while (p >= s && *p == ',')
1756 int main(int argc, char *const argv[])
1758 struct mkfs_opts mop;
1759 struct lustre_disk_data *ldd;
1760 char *mountopts = NULL;
1761 char always_mountopts[512] = "";
1762 char default_mountopts[512] = "";
1765 if ((progname = strrchr(argv[0], '/')) != NULL)
1770 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
1775 memset(&mop, 0, sizeof(mop));
1778 /* device is last arg */
1779 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
1781 /* Are we using a loop device? */
1782 ret = is_block(mop.mo_device);
1788 mop.mo_flags |= MO_IS_LOOP;
1791 /* For tunefs, we must read in the old values before parsing any
1794 /* Check whether the disk has already been formatted by mkfs.lustre */
1795 ret = is_lustre_target(&mop);
1798 fprintf(stderr, "Device %s has not been formatted with "
1799 "mkfs.lustre\n", mop.mo_device);
1804 ret = read_local_files(&mop);
1807 fprintf(stderr, "Failed to read previous Lustre data from %s "
1808 "(%d)\n", mop.mo_device, ret);
1811 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
1812 mop.mo_mgs_failnodes++;
1815 print_ldd("Read previous values", &(mop.mo_ldd));
1818 ret = parse_opts(argc, argv, &mop, &mountopts);
1824 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1826 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
1831 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
1833 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
1838 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
1839 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
1841 fprintf(stderr, "Can't find the target index, "
1842 "specify with --index\n");
1848 * Comment out these 2 checks temporarily, since for multi-MDSes
1849 * in single node only 1 mds node could have mgs service
1851 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1852 verrprint("No management node specified, adding MGS to this "
1854 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
1856 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1859 fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
1861 fprintf(stderr, "Must specify --mgsnode=\n");
1867 /* These are the permanent mount options (always included) */
1868 switch (ldd->ldd_mount_type) {
1870 case LDD_MT_LDISKFS:
1871 case LDD_MT_LDISKFS2:
1872 strscat(default_mountopts, ",errors=remount-ro",
1873 sizeof(default_mountopts));
1874 if (IS_MDT(ldd) || IS_MGS(ldd))
1875 strscat(always_mountopts, ",user_xattr",
1876 sizeof(always_mountopts));
1877 /* NB: Files created while extents are enabled can only be read
1878 * if mounted using the ext4 or ldiskfs filesystem type. */
1880 (ldd->ldd_mount_type == LDD_MT_LDISKFS ||
1881 ldd->ldd_mount_type == LDD_MT_LDISKFS2)) {
1882 strscat(default_mountopts, ",extents,mballoc",
1883 sizeof(default_mountopts));
1888 fprintf(stderr, "unknown fs type %d '%s'\n",
1889 ldd->ldd_mount_type,
1896 trim_mountfsoptions(mountopts);
1897 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
1898 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
1902 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
1905 if (ldd->ldd_mount_opts[0] == 0)
1906 /* use the defaults unless old opts exist */
1909 sprintf(ldd->ldd_mount_opts, "%s%s",
1910 always_mountopts, default_mountopts);
1911 trim_mountfsoptions(ldd->ldd_mount_opts);
1915 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1916 ldd->ldd_fsname, ldd->ldd_svname);
1919 print_ldd("Permanent disk data", ldd);
1922 printf("exiting before disk write.\n");
1926 if (check_mtab_entry(mop.mo_device))
1929 /* Create the loopback file */
1930 if (mop.mo_flags & MO_IS_LOOP) {
1931 ret = access(mop.mo_device, F_OK);
1934 #ifndef TUNEFS /* mkfs.lustre */
1935 /* Reformat the loopback file */
1936 if (ret || (mop.mo_flags & MO_FORCEFORMAT)) {
1937 ret = loop_format(&mop);
1943 ret = loop_setup(&mop);
1946 fprintf(stderr, "Loop device setup for %s failed: %s\n",
1947 mop.mo_device, strerror(ret));
1952 #ifndef TUNEFS /* mkfs.lustre */
1953 /* Check whether the disk has already been formatted by mkfs.lustre */
1954 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1955 ret = is_lustre_target(&mop);
1958 fprintf(stderr, "Device %s was previously formatted "
1959 "for lustre. Use --reformat to reformat it, "
1960 "or tunefs.lustre to modify.\n",
1966 /* Format the backing filesystem */
1967 ret = make_lustre_backfs(&mop);
1970 fprintf(stderr, "mkfs failed %d\n", ret);
1975 /* Write our config files */
1976 ret = write_local_files(&mop);
1979 fprintf(stderr, "failed to write local files\n");
1986 /* Fix any crazy return values from system() */
1987 if (ret && ((ret & 255) == 0))
1990 verrprint("%s: exiting with %d (%s)\n",
1991 progname, ret, strerror(ret));