1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
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 /* Determine if a device is a block device (as opposed to a file) */
307 int is_block(char* devname)
312 ret = access(devname, F_OK);
315 ret = stat(devname, &st);
317 fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
320 return S_ISBLK(st.st_mode);
323 __u64 get_device_size(char* device)
328 fd = open(device, O_RDONLY);
330 fprintf(stderr, "%s: cannot open %s: %s\n",
331 progname, device, strerror(errno));
336 /* size in bytes. bz5831 */
337 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
342 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
343 size = (__u64)lsize * 512;
348 fprintf(stderr, "%s: size ioctl failed: %s\n",
349 progname, strerror(errno));
353 vprint("device size = "LPU64"MB\n", size >> 20);
354 /* return value in KB */
358 int loop_format(struct mkfs_opts *mop)
362 if (mop->mo_device_sz == 0) {
364 fprintf(stderr, "loop device requires a --device-size= "
369 ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
372 fprintf(stderr, "%s: Unable to create backing store: %d\n",
378 ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
381 fprintf(stderr, "%s: Unable to truncate backing store: %d\n",
388 /* Display the need for the latest e2fsprogs to be installed. make_backfs
389 * indicates if the caller is make_lustre_backfs() or not. */
390 static void disp_old_e2fsprogs_msg(const char *feature, int make_backfs)
392 static int msg_displayed;
395 fprintf(stderr, "WARNING: %s does not support %s "
396 "feature.\n\n", E2FSPROGS, feature);
402 fprintf(stderr, "WARNING: The %s package currently installed on "
403 "your system does not support \"%s\" feature.\n",
405 #if !(HAVE_LDISKFSPROGS)
406 fprintf(stderr, "Please install the latest version of e2fsprogs from\n"
407 "http://downloads.lustre.org/public/tools/e2fsprogs/\n"
408 "to enable this feature.\n");
411 fprintf(stderr, "Feature will not be enabled until %s"
412 "is updated and '%s -O %s %%{device}' "
413 "is run.\n\n", E2FSPROGS, TUNE2FS, feature);
416 /* Check whether the file exists in the device */
417 static int file_in_dev(char *file_name, char *dev_name)
420 char debugfs_cmd[256];
421 unsigned int inode_num;
424 /* Construct debugfs command line. */
425 snprintf(debugfs_cmd, sizeof(debugfs_cmd),
426 "%s -c -R 'stat %s' '%s' 2>&1 | egrep '(Inode|unsupported)'",
427 DEBUGFS, file_name, dev_name);
429 fp = popen(debugfs_cmd, "r");
431 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
435 if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
439 i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
442 fprintf(stderr, "%s", debugfs_cmd);
443 if (strstr(debugfs_cmd, "unsupported feature")) {
444 disp_old_e2fsprogs_msg("an unknown", 0);
453 /* Check whether the device has already been used with lustre */
454 static int is_lustre_target(struct mkfs_opts *mop)
458 vprint("checking for existing Lustre data: ");
460 if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))) {
462 (rc == 1) ? MOUNT_DATA_FILE : "extents");
463 /* in the -1 case, 'extents' means this really IS a lustre
468 if ((rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
469 vprint("found %s\n", LAST_RCVD);
473 vprint("not found\n");
474 return 0; /* The device is not a lustre target. */
477 /* Check if a certain feature is supported by e2fsprogs.
478 * Firstly we try to use "debugfs supported_features" command to check if
479 * the feature is supported. If this fails we try to set this feature with
480 * mke2fs to check for its support. */
481 static int is_e2fsprogs_feature_supp(const char *feature)
483 static char supp_features[4096] = "";
486 char imgname[] = "/tmp/test-img-XXXXXX";
490 if (supp_features[0] == '\0') {
491 snprintf(cmd, sizeof(cmd), "%s -c -R supported_features 2>&1",
494 /* Using popen() instead of run_command() since debugfs does
495 * not return proper error code if command is not supported */
496 fp = popen(cmd, "r");
498 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
501 ret = fread(supp_features, 1, sizeof(supp_features), fp);
504 if (ret > 0 && strstr(supp_features,
505 strncmp(feature, "-O ", 3) ? feature : feature+3))
508 if ((fd = mkstemp(imgname)) < 0)
513 snprintf(cmd, sizeof(cmd), "%s -F %s %s 100 >/dev/null 2>&1",
514 MKE2FS, feature, imgname);
515 /* run_command() displays the output of mke2fs when it fails for
516 * some feature, so use system() directly */
523 static void enable_default_ext4_features(struct mkfs_opts *mop)
525 if (IS_OST(&mop->mo_ldd))
526 strscat(mop->mo_mkfsopts, " -O extents,uninit_bg",
527 sizeof(mop->mo_mkfsopts));
528 else if (IS_MDT(&mop->mo_ldd))
529 strscat(mop->mo_mkfsopts, " -O dirdata,uninit_bg",
530 sizeof(mop->mo_mkfsopts));
532 strscat(mop->mo_mkfsopts, " -O uninit_bg",
533 sizeof(mop->mo_mkfsopts));
535 /* Multiple mount protection enabled only if failover node specified */
537 if (is_e2fsprogs_feature_supp("-O mmp") == 0)
538 strscat(mop->mo_mkfsopts, ",mmp",
539 sizeof(mop->mo_mkfsopts));
541 disp_old_e2fsprogs_msg("mmp", 1);
544 /* Allow more than 65000 subdirectories */
545 if (is_e2fsprogs_feature_supp("-O dir_nlink") == 0)
546 strscat(mop->mo_mkfsopts,",dir_nlink",sizeof(mop->mo_mkfsopts));
548 #ifdef HAVE_EXT4_LDISKFS
549 /* The following options are only valid for ext4-based ldiskfs.
550 * If --backfstype=ext3 is specified, do not enable them. */
551 if (mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3)
554 /* Allow files larger than 2TB. Also needs LU-16, but not harmful. */
555 if (is_e2fsprogs_feature_supp("-O huge_file") == 0)
556 strscat(mop->mo_mkfsopts,",huge_file",sizeof(mop->mo_mkfsopts));
558 /* Cluster inode/block bitmaps and inode table for more efficient IO.
559 * Align the flex groups on a 1MB boundary for better performance.
560 * This -O feature needs to go last, since it adds an extra option. */
561 if (is_e2fsprogs_feature_supp("-O flex_bg") == 0) {
564 strscat(mop->mo_mkfsopts, ",flex_bg", sizeof(mop->mo_mkfsopts));
566 if (IS_OST(&mop->mo_ldd)) {
567 snprintf(tmp_buf, sizeof(tmp_buf), " -G %u",
568 (1 << 20) / L_BLOCK_SIZE);
569 strscat(mop->mo_mkfsopts, tmp_buf,
570 sizeof(mop->mo_mkfsopts));
576 /* Build fs according to type */
577 int make_lustre_backfs(struct mkfs_opts *mop)
579 __u64 device_sz = mop->mo_device_sz, block_count = 0;
580 char mkfs_cmd[PATH_MAX];
585 if (!(mop->mo_flags & MO_IS_LOOP)) {
586 mop->mo_device_sz = get_device_size(mop->mo_device);
588 if (mop->mo_device_sz == 0)
591 /* Compare to real size */
592 if (device_sz == 0 || device_sz > mop->mo_device_sz)
593 device_sz = mop->mo_device_sz;
595 mop->mo_device_sz = device_sz;
598 if (mop->mo_device_sz != 0) {
599 if (mop->mo_device_sz < 8096){
600 fprintf(stderr, "%s: size of filesystem must be larger "
601 "than 8MB, but is set to %lldKB\n",
602 progname, (long long)mop->mo_device_sz);
605 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
606 /* If the LUN size is just over 2^32 blocks, limit the
607 * filesystem size to 2^32-1 blocks to avoid problems with
608 * ldiskfs/mkfs not handling this size. Bug 22906 */
609 if (block_count > 0xffffffffULL && block_count < 0x100002000ULL)
610 block_count = 0xffffffffULL;
613 if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
614 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS) ||
615 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS2)) {
618 /* Journal size in MB */
619 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
620 /* Choose our own default journal size */
621 long journal_sz = 0, max_sz;
622 if (device_sz > 1024 * 1024) /* 1GB */
623 journal_sz = (device_sz / 102400) * 4;
624 /* cap journal size at 1GB */
625 if (journal_sz > 1024L)
628 max_sz = (102400 * L_BLOCK_SIZE) >> 20; /* 400MB */
629 if (journal_sz > max_sz)
632 sprintf(buf, " -J size=%ld", journal_sz);
633 strscat(mop->mo_mkfsopts, buf,
634 sizeof(mop->mo_mkfsopts));
638 /* Inode size (for extended attributes). The LOV EA size is
639 * 32 (EA hdr) + 32 (lov_mds_md) + stripes * 24 (lov_ost_data),
640 * and we want some margin above that for ACLs, other EAs... */
641 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
642 if (IS_MDT(&mop->mo_ldd)) {
643 if (mop->mo_stripe_count > 72)
644 inode_size = 512; /* bz 7241 */
645 /* see also "-i" below for EA blocks */
646 else if (mop->mo_stripe_count > 32)
648 else if (mop->mo_stripe_count > 10)
652 } else if (IS_OST(&mop->mo_ldd)) {
653 /* We store MDS FID and OST objid in EA on OST
654 * we need to make inode bigger as well. */
658 if (inode_size > 0) {
659 sprintf(buf, " -I %ld", inode_size);
660 strscat(mop->mo_mkfsopts, buf,
661 sizeof(mop->mo_mkfsopts));
665 /* Bytes_per_inode: disk size / num inodes */
666 if (strstr(mop->mo_mkfsopts, "-i") == NULL &&
667 strstr(mop->mo_mkfsopts, "-N") == NULL) {
668 long bytes_per_inode = 0;
670 /* Allocate more inodes on MDT devices. There is
671 * no data stored on the MDT, and very little extra
672 * metadata beyond the inode. It could go down as
673 * low as 1024 bytes, but this is conservative.
674 * Account for external EA blocks for wide striping. */
675 if (IS_MDT(&mop->mo_ldd)) {
676 bytes_per_inode = inode_size + 1536;
678 if (mop->mo_stripe_count > 72) {
679 int extra = mop->mo_stripe_count * 24;
680 extra = ((extra - 1) | 4095) + 1;
681 bytes_per_inode += extra;
685 /* Allocate fewer inodes on large OST devices. Most
686 * filesystems can be much more aggressive than even
687 * this, but it is impossible to know in advance. */
688 if (IS_OST(&mop->mo_ldd)) {
689 /* OST > 8TB assume average file size 1MB */
690 if (device_sz >= (8ULL << 30))
691 bytes_per_inode = 1024 * 1024;
692 /* OST > 1TB assume average file size 256kB */
693 else if (device_sz >= (1ULL << 30))
694 bytes_per_inode = 256 * 1024;
695 /* OST > 100GB assume average file size 64kB,
696 * plus a bit so that inodes will fit into a
697 * 256x flex_bg without overflowing */
698 else if (device_sz >= (10ULL << 20))
699 bytes_per_inode = 69905;
703 if (bytes_per_inode > 0) {
704 sprintf(buf, " -i %ld", bytes_per_inode);
705 strscat(mop->mo_mkfsopts, buf,
706 sizeof(mop->mo_mkfsopts));
711 strscat(mop->mo_mkfsopts, " -q",
712 sizeof(mop->mo_mkfsopts));
715 if (strstr(mop->mo_mkfsopts, "-O") == NULL)
716 enable_default_ext4_features(mop);
718 /* In order to align the filesystem metadata on 1MB boundaries,
719 * give a resize value that will reserve a power-of-two group
720 * descriptor blocks, but leave one block for the superblock.
721 * Only useful for filesystems with < 2^32 blocks due to resize
723 if (IS_OST(&mop->mo_ldd) && mop->mo_device_sz > 100 * 1024 &&
724 mop->mo_device_sz / L_BLOCK_SIZE <= 0xffffffff) {
725 unsigned group_blocks = L_BLOCK_SIZE * 8;
726 unsigned desc_per_block = L_BLOCK_SIZE / 32;
727 unsigned resize_blks;
729 resize_blks = (1ULL<<32) - desc_per_block*group_blocks;
730 snprintf(buf, sizeof(buf)," -E resize=%u,",resize_blks);
732 strncpy(buf, " -E ", sizeof(buf));
735 /* Avoid zeroing out the full journal - speeds up mkfs */
736 if (is_e2fsprogs_feature_supp("-E lazy_journal_init") == 0)
737 strscat(buf, "lazy_journal_init,", sizeof(buf));
739 if (strlen(buf) > strlen(" -E "))
740 strscat(mop->mo_mkfsopts, buf,sizeof(mop->mo_mkfsopts));
742 /* Allow reformat of full devices (as opposed to
743 partitions.) We already checked for mounted dev. */
744 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
746 snprintf(mkfs_cmd, sizeof(mkfs_cmd),
747 "%s -j -b %d -L %s ", MKE2FS, L_BLOCK_SIZE,
748 mop->mo_ldd.ldd_svname);
750 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
751 progname, mop->mo_ldd.ldd_mount_type,
752 MT_STR(&mop->mo_ldd));
756 /* For loop device format the dev, not the filename */
757 dev = mop->mo_device;
758 if (mop->mo_flags & MO_IS_LOOP)
759 dev = mop->mo_loopdev;
761 vprint("formatting backing filesystem %s on %s\n",
762 MT_STR(&mop->mo_ldd), dev);
763 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
764 vprint("\t4k blocks "LPU64"\n", block_count);
765 vprint("\toptions %s\n", mop->mo_mkfsopts);
767 /* mkfs_cmd's trailing space is important! */
768 strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
769 strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
770 strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
771 if (block_count != 0) {
772 sprintf(buf, " "LPU64, block_count);
773 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
776 vprint("mkfs_cmd = %s\n", mkfs_cmd);
777 ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
780 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
785 /* ==================== Lustre config functions =============*/
787 void print_ldd(char *str, struct lustre_disk_data *ldd)
789 printf("\n %s:\n", str);
790 printf("Target: %s\n", ldd->ldd_svname);
791 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
792 printf("Index: unassigned\n");
794 printf("Index: %d\n", ldd->ldd_svindex);
795 if (ldd->ldd_uuid[0])
796 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
797 printf("Lustre FS: %s\n", ldd->ldd_fsname);
798 printf("Mount type: %s\n", MT_STR(ldd));
799 printf("Flags: %#x\n", ldd->ldd_flags);
800 printf(" (%s%s%s%s%s%s%s%s%s%s)\n",
801 IS_MDT(ldd) ? "MDT ":"",
802 IS_OST(ldd) ? "OST ":"",
803 IS_MGS(ldd) ? "MGS ":"",
804 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
805 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
806 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
807 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
808 ldd->ldd_flags & LDD_F_IAM_DIR ? "IAM_dir_format ":"",
809 ldd->ldd_flags & LDD_F_NO_PRIMNODE? "no_primnode ":"",
810 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
811 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
812 printf("Parameters:%s\n", ldd->ldd_params);
813 if (ldd->ldd_userdata[0])
814 printf("Comment: %s\n", ldd->ldd_userdata);
818 static int touch_file(char *filename)
822 if (filename == NULL) {
826 fd = open(filename, O_CREAT | O_TRUNC, 0600);
835 /* keep it less than LL_FID_NAMELEN */
836 #define DUMMY_FILE_NAME_LEN 25
837 #define EXT3_DIRENT_SIZE DUMMY_FILE_NAME_LEN
839 /* Need to add these many entries to this directory to make HTREE dir. */
840 #define MIN_ENTRIES_REQ_FOR_HTREE ((L_BLOCK_SIZE / EXT3_DIRENT_SIZE))
842 static int add_dummy_files(char *dir)
844 char fpname[PATH_MAX];
848 for (i = 0; i < MIN_ENTRIES_REQ_FOR_HTREE; i++) {
849 snprintf(fpname, PATH_MAX, "%s/%0*d", dir,
850 DUMMY_FILE_NAME_LEN, i);
852 rc = touch_file(fpname);
853 if (rc && rc != -EEXIST) {
855 "%s: Can't create dummy file %s: %s\n",
856 progname, fpname , strerror(errno));
863 static int __l_mkdir(char * filepnm, int mode , struct mkfs_opts *mop)
867 ret = mkdir(filepnm, mode);
868 if (ret && ret != -EEXIST)
871 /* IAM mode supports ext3 directories of HTREE type only. So add dummy
872 * entries to new directory to create htree type of container for
874 if (mop->mo_ldd.ldd_flags & LDD_F_IAM_DIR)
875 return add_dummy_files(filepnm);
879 /* Write the server config files */
880 int write_local_files(struct mkfs_opts *mop)
882 char mntpt[] = "/tmp/mntXXXXXX";
889 /* Mount this device temporarily in order to write these files */
890 if (!mkdtemp(mntpt)) {
891 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
892 progname, mntpt, strerror(errno));
896 dev = mop->mo_device;
897 if (mop->mo_flags & MO_IS_LOOP)
898 dev = mop->mo_loopdev;
900 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0,
901 mop->mo_ldd.ldd_mount_opts);
903 fprintf(stderr, "%s: Unable to mount %s: %s\n",
904 progname, dev, strerror(errno));
906 if (errno == ENODEV) {
907 fprintf(stderr, "Is the %s module available?\n",
908 MT_STR(&mop->mo_ldd));
913 /* Set up initial directories */
914 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
915 ret = __l_mkdir(filepnm, 0777, mop);
916 if ((ret != 0) && (errno != EEXIST)) {
917 fprintf(stderr, "%s: Can't make configs dir %s (%s)\n",
918 progname, filepnm, strerror(errno));
920 } else if (errno == EEXIST) {
924 /* Save the persistent mount data into a file. Lustre must pre-read
925 this file to get the real mount options. */
926 vprint("Writing %s\n", MOUNT_DATA_FILE);
927 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
928 filep = fopen(filepnm, "w");
930 fprintf(stderr, "%s: Unable to create %s file: %s\n",
931 progname, filepnm, strerror(errno));
934 num = fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
935 if (num < 1 && ferror(filep)) {
936 fprintf(stderr, "%s: Unable to write to file (%s): %s\n",
937 progname, filepnm, strerror(errno));
943 /* Check for upgrade */
944 if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS))
945 == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
948 int cmdsz = sizeof(cmd);
949 vprint("Copying old logs\n");
951 /* Copy the old client log to fsname-client */
952 sprintf(filepnm, "%s/%s/%s-client",
953 mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
954 snprintf(cmd, cmdsz, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
956 ret = run_command(cmd, cmdsz);
958 fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
959 "(%d)\n", progname, MDT_LOGS_DIR, ret);
960 fprintf(stderr, "mount -t ldiskfs %s somewhere, "
961 "find the client log for fs %s and "
962 "copy it manually into %s/%s-client, "
965 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
966 mop->mo_ldd.ldd_fsname);
970 /* We need to use the old mdt log because otherwise mdt won't
971 have complete lov if old clients connect before all
973 /* Copy the old mdt log to fsname-MDT0000 (get old
974 name from mdt_UUID) */
976 strscpy(filepnm, (char *)mop->mo_ldd.ldd_uuid, sizeof(filepnm));
977 term = strstr(filepnm, "_UUID");
980 snprintf(cmd, cmdsz, "cp %s/%s/%s %s/%s/%s",
981 mntpt, MDT_LOGS_DIR, filepnm,
982 mntpt, MOUNT_CONFIGS_DIR,
983 mop->mo_ldd.ldd_svname);
984 ret = run_command(cmd, cmdsz);
987 fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
988 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
989 fprintf(stderr, "mount -t ext3 %s somewhere, "
990 "find the MDT log for fs %s and "
991 "copy it manually into %s/%s, "
994 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
995 mop->mo_ldd.ldd_svname);
1000 /* end COMPAT_146 */
1009 int read_local_files(struct mkfs_opts *mop)
1011 char tmpdir[] = "/tmp/dirXXXXXX";
1017 int cmdsz = sizeof(cmd);
1019 /* Make a temporary directory to hold Lustre data files. */
1020 if (!mkdtemp(tmpdir)) {
1021 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
1022 progname, tmpdir, strerror(errno));
1026 dev = mop->mo_device;
1028 /* TODO: it's worth observing the get_mountdata() function that is
1029 in mount_utils.c for getting the mountdata out of the
1032 /* Construct debugfs command line. */
1033 snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s/mountdata' '%s'",
1034 DEBUGFS, MOUNT_DATA_FILE, tmpdir, dev);
1036 ret = run_command(cmd, cmdsz);
1038 verrprint("%s: Unable to dump %s dir (%d)\n",
1039 progname, MOUNT_CONFIGS_DIR, ret);
1041 sprintf(filepnm, "%s/mountdata", tmpdir);
1042 filep = fopen(filepnm, "r");
1045 vprint("Reading %s\n", MOUNT_DATA_FILE);
1046 num_read = fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
1047 if (num_read < 1 && ferror(filep)) {
1048 fprintf(stderr, "%s: Unable to read from file %s: %s\n",
1049 progname, filepnm, strerror(errno));
1054 /* Try to read pre-1.6 config from last_rcvd */
1055 struct lr_server_data lsd;
1056 verrprint("%s: Unable to read %d.%d config %s.\n",
1057 progname, LUSTRE_MAJOR, LUSTRE_MINOR, filepnm);
1059 verrprint("Trying 1.4 config from last_rcvd\n");
1060 sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
1062 /* Construct debugfs command line. */
1063 snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s' %s",
1064 DEBUGFS, LAST_RCVD, filepnm, dev);
1066 ret = run_command(cmd, cmdsz);
1068 fprintf(stderr, "%s: Unable to dump %s file (%d)\n",
1069 progname, LAST_RCVD, ret);
1073 filep = fopen(filepnm, "r");
1075 fprintf(stderr, "%s: Unable to open %s: %s\n",
1076 progname, filepnm, strerror(errno));
1078 verrprint("Contents of %s:\n", tmpdir);
1080 snprintf(cmd, cmdsz, "ls -l %s/", tmpdir);
1081 run_command(cmd, cmdsz);
1082 verrprint("Contents of disk:\n");
1083 snprintf(cmd, cmdsz, "%s -c -R 'ls -l /' %s",
1085 run_command(cmd, cmdsz);
1089 vprint("Reading %s\n", LAST_RCVD);
1090 ret = fread(&lsd, 1, sizeof(lsd), filep);
1091 if (ret < sizeof(lsd)) {
1092 fprintf(stderr, "%s: Short read (%d of %d)\n",
1093 progname, ret, (int)sizeof(lsd));
1094 ret = ferror(filep);
1098 vprint("Feature compat=%x, incompat=%x\n",
1099 lsd.lsd_feature_compat, lsd.lsd_feature_incompat);
1101 if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
1102 (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
1103 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
1104 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
1105 } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
1106 (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
1107 /* We must co-locate so mgs can see old logs.
1108 If user doesn't want this, they can copy the old
1109 logs manually and re-tunefs. */
1110 mop->mo_ldd.ldd_flags =
1111 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
1112 mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
1114 /* If neither is set, we're pre-1.4.6, make a guess. */
1115 /* Construct debugfs command line. */
1116 snprintf(cmd, cmdsz, "%s -c -R 'rdump /%s %s' %s",
1117 DEBUGFS, MDT_LOGS_DIR, tmpdir, dev);
1118 run_command(cmd, cmdsz);
1120 sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
1121 if (lsd.lsd_ost_index > 0) {
1122 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
1123 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
1125 /* If there's a LOGS dir, it's an MDT */
1126 if ((ret = access(filepnm, F_OK)) == 0) {
1127 mop->mo_ldd.ldd_flags =
1130 /* Old MDT's are always index 0
1132 mop->mo_ldd.ldd_svindex = 0;
1134 /* The index may not be correct */
1135 mop->mo_ldd.ldd_flags =
1136 LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
1137 verrprint("OST with unknown index\n");
1143 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid,
1144 sizeof(mop->mo_ldd.ldd_uuid));
1145 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
1147 /* end COMPAT_146 */
1152 snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
1153 run_command(cmd, cmdsz);
1155 verrprint("Failed to read old data (%d)\n", ret);
1160 void set_defaults(struct mkfs_opts *mop)
1162 mop->mo_ldd.ldd_magic = LDD_MAGIC;
1163 mop->mo_ldd.ldd_config_ver = 1;
1164 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
1165 mop->mo_mgs_failnodes = 0;
1166 strcpy(mop->mo_ldd.ldd_fsname, "lustre");
1167 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
1169 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
1170 mop->mo_stripe_count = 1;
1173 static inline void badopt(const char *opt, char *type)
1175 fprintf(stderr, "%s: '--%s' only valid for %s\n",
1176 progname, opt, type);
1180 static int add_param(char *buf, char *key, char *val)
1182 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
1183 int start = strlen(buf);
1187 keylen = strlen(key);
1188 if (start + 1 + keylen + strlen(val) >= end) {
1189 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
1190 progname, buf, key ? key : "", val);
1194 sprintf(buf + start, " %s%s", key ? key : "", val);
1198 /* from mount_lustre */
1199 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
1200 #define MAXNIDSTR 1024
1201 static char *convert_hostnames(char *s1)
1203 char *converted, *s2 = 0, *c, *end, sep;
1204 int left = MAXNIDSTR;
1207 converted = malloc(left);
1208 if (converted == NULL) {
1212 end = s1 + strlen(s1);
1214 while ((left > 0) && (s1 < end)) {
1215 s2 = strpbrk(s1, ",:");
1220 nid = libcfs_str2nid(s1);
1222 if (nid == LNET_NID_ANY) {
1223 fprintf(stderr, "%s: Can't parse NID '%s'\n",
1228 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
1229 strlen("127.0.0.1")) == 0) {
1230 fprintf(stderr, "%s: The NID '%s' resolves to the "
1231 "loopback address '%s'. Lustre requires a "
1232 "non-loopback address.\n",
1233 progname, s1, libcfs_nid2str(nid));
1238 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
1239 left = converted + MAXNIDSTR - c;
1245 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
1248 static struct option long_opt[] = {
1249 {"iam-dir", 0, 0, 'a'},
1250 {"backfstype", 1, 0, 'b'},
1251 {"stripe-count-hint", 1, 0, 'c'},
1252 {"comment", 1, 0, 'u'},
1253 {"configdev", 1, 0, 'C'},
1254 {"device-size", 1, 0, 'd'},
1255 {"dryrun", 0, 0, 'n'},
1256 {"erase-params", 0, 0, 'e'},
1257 {"failnode", 1, 0, 'f'},
1258 {"failover", 1, 0, 'f'},
1260 {"help", 0, 0, 'h'},
1261 {"index", 1, 0, 'i'},
1262 {"mkfsoptions", 1, 0, 'k'},
1263 {"mgsnode", 1, 0, 'm'},
1264 {"mgsnid", 1, 0, 'm'},
1266 {"fsname",1, 0, 'L'},
1267 {"noformat", 0, 0, 'n'},
1268 {"nomgs", 0, 0, 'N'},
1269 {"mountfsoptions", 1, 0, 'o'},
1271 {"param", 1, 0, 'p'},
1272 {"print", 0, 0, 'n'},
1273 {"quiet", 0, 0, 'q'},
1274 {"reformat", 0, 0, 'r'},
1275 {"servicenode", 1, 0, 's'},
1276 {"verbose", 0, 0, 'v'},
1277 {"writeconf", 0, 0, 'w'},
1278 {"upgrade_to_18", 0, 0, 'U'},
1279 {"network", 1, 0, 't'},
1282 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrs:t:Uu:vw";
1285 int failnode_set = 0, servicenode_set = 0;
1287 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
1291 if (IS_MDT(&mop->mo_ldd))
1292 mop->mo_ldd.ldd_flags |= LDD_F_IAM_DIR;
1297 while (i < LDD_MT_LAST) {
1298 if (strcmp(optarg, mt_str(i)) == 0) {
1299 mop->mo_ldd.ldd_mount_type = i;
1307 if (IS_MDT(&mop->mo_ldd)) {
1308 int stripe_count = atol(optarg);
1309 if (stripe_count <= 0) {
1310 fprintf(stderr, "%s: bad stripe count "
1311 "%d\n", progname, stripe_count);
1314 mop->mo_stripe_count = stripe_count;
1316 badopt(long_opt[longidx].name, "MDT");
1320 case 'C': /* Configdev */
1322 printf("Configdev not implemented\n");
1325 mop->mo_device_sz = atol(optarg);
1328 mop->mo_ldd.ldd_params[0] = '\0';
1329 /* Must update the mgs logs */
1330 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1336 if ((opt == 'f' && servicenode_set)
1337 || (opt == 's' && failnode_set)) {
1338 fprintf(stderr, "%s: %s cannot use with --%s\n",
1339 progname, long_opt[longidx].name,
1340 opt == 'f' ? "servicenode" : "failnode");
1344 nids = convert_hostnames(optarg);
1347 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
1352 /* Must update the mgs logs */
1353 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1357 mop->mo_ldd.ldd_flags |= LDD_F_NO_PRIMNODE;
1358 servicenode_set = 1;
1364 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
1370 if (!(mop->mo_ldd.ldd_flags &
1371 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1372 LDD_F_WRITECONF))) {
1373 fprintf(stderr, "%s: cannot change the index of"
1374 " a registered target\n", progname);
1377 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
1378 mop->mo_ldd.ldd_svindex = atol(optarg);
1379 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
1381 badopt(long_opt[longidx].name, "MDT,OST");
1386 strscpy(mop->mo_mkfsopts, optarg,
1387 sizeof(mop->mo_mkfsopts));
1391 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
1392 (!(mop->mo_ldd.ldd_flags &
1393 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1394 LDD_F_WRITECONF)))) {
1395 fprintf(stderr, "%s: cannot change the name of"
1396 " a registered target\n", progname);
1399 if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
1400 fprintf(stderr, "%s: filesystem name must be "
1401 "1-8 chars\n", progname);
1404 if ((tmp = strpbrk(optarg, "/:"))) {
1405 fprintf(stderr, "%s: char '%c' not allowed in "
1406 "filesystem name\n", progname, *tmp);
1409 strscpy(mop->mo_ldd.ldd_fsname, optarg,
1410 sizeof(mop->mo_ldd.ldd_fsname));
1414 char *nids = convert_hostnames(optarg);
1417 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
1422 mop->mo_mgs_failnodes++;
1426 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1432 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1435 *mountopts = optarg;
1438 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1441 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1444 /* Must update the mgs logs */
1445 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1451 mop->mo_flags |= MO_FORCEFORMAT;
1454 if (!IS_MDT(&mop->mo_ldd) && !IS_OST(&mop->mo_ldd)) {
1455 badopt(long_opt[longidx].name, "MDT,OST");
1462 rc = add_param(mop->mo_ldd.ldd_params,
1463 PARAM_NETWORK, optarg);
1466 /* Must update the mgs logs */
1467 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1470 strscpy(mop->mo_ldd.ldd_userdata, optarg,
1471 sizeof(mop->mo_ldd.ldd_userdata));
1477 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1485 fprintf(stderr, "Unknown option '%c'\n", opt);
1491 /* Last arg is device */
1492 if (optind != argc - 1) {
1494 fprintf(stderr, "Bad argument: %s\n", argv[optind]);
1498 /* single argument: <device> */
1505 /* Search for opt in mntlist, returning true if found.
1507 static int in_mntlist(char *opt, char *mntlist)
1509 char *ml, *mlp, *item, *ctx = NULL;
1511 if (!(ml = strdup(mntlist))) {
1512 fprintf(stderr, "%s: out of memory\n", progname);
1516 while ((item = strtok_r(mlp, ",", &ctx))) {
1517 if (!strcmp(opt, item))
1522 return (item != NULL);
1525 /* Issue a message on stderr for every item in wanted_mountopts that is not
1526 * present in mountopts. The justwarn boolean toggles between error and
1527 * warning message. Return an error count.
1529 static int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
1532 char *ml, *mlp, *item, *ctx = NULL;
1535 if (!(ml = strdup(wanted_mountopts))) {
1536 fprintf(stderr, "%s: out of memory\n", progname);
1540 while ((item = strtok_r(mlp, ",", &ctx))) {
1541 if (!in_mntlist(item, mountopts)) {
1542 fprintf(stderr, "%s: %s mount option `%s' is missing\n",
1543 progname, justwarn ? "Warning: default"
1544 : "Error: mandatory", item);
1553 /* Trim embedded white space, leading and trailing commas from string s.
1555 static void trim_mountfsoptions(char *s)
1561 memmove(p, p + 1, strlen(p + 1) + 1);
1568 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
1570 p = s + strlen(s) - 1;
1571 while (p >= s && *p == ',')
1575 int main(int argc, char *const argv[])
1577 struct mkfs_opts mop;
1578 struct lustre_disk_data *ldd;
1579 char *mountopts = NULL;
1580 char always_mountopts[512] = "";
1581 char default_mountopts[512] = "";
1584 if ((progname = strrchr(argv[0], '/')) != NULL)
1589 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
1594 memset(&mop, 0, sizeof(mop));
1597 /* device is last arg */
1598 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
1600 /* Are we using a loop device? */
1601 ret = is_block(mop.mo_device);
1605 mop.mo_flags |= MO_IS_LOOP;
1608 /* For tunefs, we must read in the old values before parsing any
1611 /* Check whether the disk has already been formatted by mkfs.lustre */
1612 ret = is_lustre_target(&mop);
1615 fprintf(stderr, "Device %s has not been formatted with "
1616 "mkfs.lustre\n", mop.mo_device);
1621 ret = read_local_files(&mop);
1624 fprintf(stderr, "Failed to read previous Lustre data from %s "
1625 "(%d)\n", mop.mo_device, ret);
1628 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
1629 mop.mo_mgs_failnodes++;
1632 print_ldd("Read previous values", &(mop.mo_ldd));
1635 ret = parse_opts(argc, argv, &mop, &mountopts);
1641 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1643 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
1648 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
1650 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
1655 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
1656 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
1658 fprintf(stderr, "Can't find the target index, "
1659 "specify with --index\n");
1665 * Comment out these 2 checks temporarily, since for multi-MDSes
1666 * in single node only 1 mds node could have mgs service
1668 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1669 verrprint("No management node specified, adding MGS to this "
1671 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
1673 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1676 fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
1678 fprintf(stderr, "Must specify --mgsnode=\n");
1684 /* These are the permanent mount options (always included) */
1685 switch (ldd->ldd_mount_type) {
1687 case LDD_MT_LDISKFS:
1688 case LDD_MT_LDISKFS2:
1689 strscat(default_mountopts, ",errors=remount-ro",
1690 sizeof(default_mountopts));
1691 if (IS_MDT(ldd) || IS_MGS(ldd))
1692 strscat(always_mountopts, ",user_xattr",
1693 sizeof(always_mountopts));
1694 /* NB: Files created while extents are enabled can only be read
1695 * if mounted using the ext4 or ldiskfs filesystem type. */
1697 (ldd->ldd_mount_type == LDD_MT_LDISKFS ||
1698 ldd->ldd_mount_type == LDD_MT_LDISKFS2)) {
1699 strscat(default_mountopts, ",extents,mballoc",
1700 sizeof(default_mountopts));
1705 fprintf(stderr, "unknown fs type %d '%s'\n",
1706 ldd->ldd_mount_type,
1713 trim_mountfsoptions(mountopts);
1714 (void)check_mountfsoptions(mountopts, default_mountopts, 1);
1715 if (check_mountfsoptions(mountopts, always_mountopts, 0)) {
1719 sprintf(ldd->ldd_mount_opts, "%s", mountopts);
1722 if (ldd->ldd_mount_opts[0] == 0)
1723 /* use the defaults unless old opts exist */
1726 sprintf(ldd->ldd_mount_opts, "%s%s",
1727 always_mountopts, default_mountopts);
1728 trim_mountfsoptions(ldd->ldd_mount_opts);
1732 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1733 ldd->ldd_fsname, ldd->ldd_svname);
1736 print_ldd("Permanent disk data", ldd);
1739 printf("exiting before disk write.\n");
1743 if (check_mtab_entry(mop.mo_device))
1746 /* Create the loopback file */
1747 if (mop.mo_flags & MO_IS_LOOP) {
1748 ret = access(mop.mo_device, F_OK);
1751 #ifndef TUNEFS /* mkfs.lustre */
1752 /* Reformat the loopback file */
1753 if (ret || (mop.mo_flags & MO_FORCEFORMAT))
1754 ret = loop_format(&mop);
1757 ret = loop_setup(&mop);
1760 fprintf(stderr, "Loop device setup for %s failed: %s\n",
1761 mop.mo_device, strerror(ret));
1766 #ifndef TUNEFS /* mkfs.lustre */
1767 /* Check whether the disk has already been formatted by mkfs.lustre */
1768 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1769 ret = is_lustre_target(&mop);
1772 fprintf(stderr, "Device %s was previously formatted "
1773 "for lustre. Use --reformat to reformat it, "
1774 "or tunefs.lustre to modify.\n",
1780 /* Format the backing filesystem */
1781 ret = make_lustre_backfs(&mop);
1784 fprintf(stderr, "mkfs failed %d\n", ret);
1789 /* Write our config files */
1790 ret = write_local_files(&mop);
1793 fprintf(stderr, "failed to write local files\n");
1800 /* Fix any crazy return values from system() */
1801 if (ret && ((ret & 255) == 0))
1804 verrprint("%s: exiting with %d (%s)\n",
1805 progname, ret, strerror(ret));