1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2006 Cluster File Systems, Inc.
5 * Author: Nathan Rutman <nathan@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
33 #include <sys/types.h>
35 #include <sys/mount.h>
41 # include <linux/fs.h> /* for BLKGETSIZE64 */
43 #include <lustre_disk.h>
44 #include <lustre_param.h>
45 #include <lnet/lnetctl.h>
46 #include <lustre_ver.h>
49 #define MAX_LOOP_DEVICES 16
50 #define L_BLOCK_SIZE 4096
51 #define INDEX_UNASSIGNED 0xFFFF
52 #define MO_IS_LOOP 0x01
53 #define MO_FORCEFORMAT 0x02
55 /* used to describe the options to format the lustre disk, not persistent */
57 struct lustre_disk_data mo_ldd; /* to be written in MOUNT_DATA_FILE */
58 char mo_mount_type_string[20]; /* "ext3", "ldiskfs", ... */
59 char mo_device[128]; /* disk device name */
60 char mo_mkfsopts[128]; /* options to the backing-store mkfs */
61 char mo_loopdev[128]; /* in case a loop dev is needed */
62 __u64 mo_device_sz; /* in KB */
68 static char *progname;
69 static int verbose = 0;
70 static int print_only = 0;
75 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
76 fprintf(out, "usage: %s <target types> [options] <device>\n", progname);
78 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
80 "\t\t--ost: object storage, mutually exclusive with mdt,mgs\n"
81 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
82 "\t\t--mgs: configuration management service - one per site\n"
83 "\toptions (in order of popularity):\n"
84 "\t\t--mgsnode=<nid>[,<...>] : NID(s) of a remote mgs node\n"
85 "\t\t\trequired for all targets other than the mgs node\n"
86 "\t\t--fsname=<filesystem_name> : default is 'lustre'\n"
87 "\t\t--failnode=<nid>[,<...>] : NID(s) of a failover partner\n"
88 "\t\t--param <key>=<value> : set a permanent parameter\n"
89 "\t\t\te.g. --param sys.timeout=40\n"
90 "\t\t\t --param lov.stripesize=2M\n"
91 "\t\t--index=#N : target index (i.e. ost index within the lov)\n"
92 /* FIXME implement 1.6.x
93 "\t\t--configdev=<altdevice|file>: store configuration info\n"
94 "\t\t\tfor this device on an alternate device\n"
96 "\t\t--comment=<user comment>: arbitrary user string (%d bytes)\n"
97 "\t\t--mountfsoptions=<opts> : permanent mount options\n"
99 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
100 "\t\t--device-size=#N(KB) : device size for loop devices\n"
101 "\t\t--mkfsoptions=<opts> : format options\n"
102 "\t\t--reformat: overwrite an existing disk\n"
103 "\t\t--stripe-count-hint=#N : used for optimizing MDT inode size\n"
105 "\t\t--erase-params : erase all old parameter settings\n"
106 "\t\t--nomgs: turn off MGS service on this MDT\n"
107 "\t\t--writeconf: erase all config logs for this fs.\n"
109 "\t\t--noformat: just report what we would do; "
110 "don't write to disk\n"
113 sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
117 #define vprint if (verbose > 0) printf
118 #define verrprint if (verbose >= 0) printf
120 static void fatal(void)
123 fprintf(stderr, "\n%s FATAL: ", progname);
126 /*================ utility functions =====================*/
129 dev_major (unsigned long long int __dev)
131 return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
135 dev_minor (unsigned long long int __dev)
137 return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
142 static int version = 0;
146 char release[4] = "";
148 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
150 fprintf(stderr, "%s: Warning: Can't resolve kernel "
151 "version, assuming 2.6\n", progname);
153 read(fd, release, 4);
156 if (strncmp(release, "2.4.", 4) == 0)
164 int run_command(char *cmd)
166 char log[] = "/tmp/mkfs_logXXXXXX";
169 vprint("cmd: %s\n", cmd);
170 if ((fd = mkstemp(log)) >= 0) {
175 strcat(cmd, " 2>&1");
177 /* Can't use popen because we need the rv of the command */
179 if ((rc || (verbose > 2)) && (fd >= 0)) {
182 fp = fopen(log, "r");
184 while (fgets(buf, sizeof(buf), fp) != NULL) {
195 static int check_mtab_entry(char *spec)
200 fp = setmntent(MOUNTED, "r");
204 while ((mnt = getmntent(fp)) != NULL) {
205 if (strcmp(mnt->mnt_fsname, spec) == 0) {
207 fprintf(stderr, "%s: according to %s %s is "
208 "already mounted on %s\n",
209 progname, MOUNTED, spec, mnt->mnt_dir);
218 /*============ disk dev functions ===================*/
220 /* Setup a file in the first unused loop_device */
221 int loop_setup(struct mkfs_opts *mop)
228 /* Figure out the loop device names */
229 if (!access("/dev/loop0", F_OK | R_OK)) {
230 strcpy(loop_base, "/dev/loop\0");
231 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
232 strcpy(loop_base, "/dev/loop/\0");
234 fprintf(stderr, "%s: can't access loop devices\n", progname);
238 /* Find unused loop device */
239 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
241 sprintf(l_device, "%s%d", loop_base, i);
242 if (access(l_device, F_OK | R_OK))
244 sprintf(cmd, "losetup %s > /dev/null 2>&1", l_device);
247 /* losetup gets 1 (ret=256) for non-set-up device */
249 /* Set up a loopback device to our file */
250 sprintf(cmd, "losetup %s %s", l_device, mop->mo_device);
251 ret = run_command(cmd);
253 fprintf(stderr, "%s: error %d on losetup: %s\n",
254 progname, ret, strerror(ret));
257 strcpy(mop->mo_loopdev, l_device);
262 fprintf(stderr, "%s: out of loop devices!\n", progname);
266 int loop_cleanup(struct mkfs_opts *mop)
270 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
271 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
272 ret = run_command(cmd);
277 /* Determine if a device is a block device (as opposed to a file) */
278 int is_block(char* devname)
283 ret = access(devname, F_OK);
286 ret = stat(devname, &st);
288 fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
291 return S_ISBLK(st.st_mode);
294 __u64 get_device_size(char* device)
299 fd = open(device, O_RDONLY);
301 fprintf(stderr, "%s: cannot open %s: %s\n",
302 progname, device, strerror(errno));
307 /* size in bytes. bz5831 */
308 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
313 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
314 size = (__u64)lsize * 512;
319 fprintf(stderr, "%s: size ioctl failed: %s\n",
320 progname, strerror(errno));
324 vprint("device size = "LPU64"MB\n", size >> 20);
325 /* return value in KB */
329 int loop_format(struct mkfs_opts *mop)
333 if (mop->mo_device_sz == 0) {
335 fprintf(stderr, "loop device requires a --device-size= "
340 ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
343 fprintf(stderr, "%s: Unable to create backing store: %d\n",
349 ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
352 fprintf(stderr, "%s: Unable to truncate backing store: %d\n",
359 /* Check whether the file exists in the device */
360 static int file_in_dev(char *file_name, char *dev_name)
363 char debugfs_cmd[256];
364 unsigned int inode_num;
367 /* Construct debugfs command line. */
368 memset(debugfs_cmd, 0, sizeof(debugfs_cmd));
370 "debugfs -c -R 'stat %s' %s 2>&1 | egrep '(Inode|unsupported)'",
371 file_name, dev_name);
373 fp = popen(debugfs_cmd, "r");
375 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
379 if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
383 i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
386 fprintf(stderr, "%s", debugfs_cmd);
387 if (strstr(debugfs_cmd, "unsupported feature")) {
388 fprintf(stderr, "In all likelihood, the "
389 "'unsupported feature' is 'extents', which "
390 "older debugfs does not understand.\n"
391 "Use e2fsprogs-1.38-cfs1 or later, available "
392 "from ftp://ftp.lustre.org/pub/lustre/other/"
401 /* Check whether the device has already been used with lustre */
402 static int is_lustre_target(struct mkfs_opts *mop)
406 vprint("checking for existing Lustre data\n");
407 if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))
408 || (rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
409 vprint("found Lustre data\n");
410 /* in the -1 case, 'extents' means this really IS a lustre
415 return 0; /* The device is not a lustre target. */
418 /* Build fs according to type */
419 int make_lustre_backfs(struct mkfs_opts *mop)
427 if (mop->mo_device_sz != 0) {
428 if (mop->mo_device_sz < 8096){
429 fprintf(stderr, "%s: size of filesystem must be larger "
430 "than 8MB, but is set to %lldKB\n",
431 progname, mop->mo_device_sz);
434 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
437 if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
438 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS)) {
439 __u64 device_sz = mop->mo_device_sz;
441 /* we really need the size */
442 if (device_sz == 0) {
443 device_sz = get_device_size(mop->mo_device);
448 /* Journal size in MB */
449 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
450 /* Choose our own default journal size */
452 if (device_sz > 1024 * 1024) /* 1GB */
453 journal_sz = (device_sz / 102400) * 4;
455 sprintf(buf, " -J size=%ld", journal_sz);
456 strcat(mop->mo_mkfsopts, buf);
460 /* Default bytes_per_inode is block size */
461 if (strstr(mop->mo_mkfsopts, "-i") == NULL) {
462 long bytes_per_inode = 0;
464 if (IS_MDT(&mop->mo_ldd))
465 bytes_per_inode = 4096;
467 /* Allocate fewer inodes on large OST devices. Most
468 filesystems can be much more aggressive than even
470 if (IS_OST(&mop->mo_ldd)) {
471 if (device_sz > 100000000) /* 100 Gb */
472 bytes_per_inode = 16384;
474 bytes_per_inode = 4096;
477 if (bytes_per_inode > 0) {
478 sprintf(buf, " -i %ld", bytes_per_inode);
479 strcat(mop->mo_mkfsopts, buf);
483 /* This is an undocumented mke2fs option. Default is 128. */
484 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
486 if (IS_MDT(&mop->mo_ldd)) {
487 if (mop->mo_stripe_count > 77)
488 inode_size = 512; /* bz 7241 */
489 /* cray stripes across all osts (>60) */
490 else if (mop->mo_stripe_count > 34)
492 else if (mop->mo_stripe_count > 13)
496 } else if (IS_OST(&mop->mo_ldd)) {
497 /* now as we store fids in EA on OST we need
498 to make inode bigger */
502 if (inode_size > 0) {
503 sprintf(buf, " -I %ld", inode_size);
504 strcat(mop->mo_mkfsopts, buf);
510 strcat(mop->mo_mkfsopts, " -q");
513 /* Enable hashed b-tree directory lookup in large dirs bz6224 */
514 if (strstr(mop->mo_mkfsopts, "-O") == NULL) {
515 strcat(mop->mo_mkfsopts, " -O dir_index");
518 /* Allow reformat of full devices (as opposed to
519 partitions.) We already checked for mounted dev. */
520 strcat(mop->mo_mkfsopts, " -F");
522 sprintf(mkfs_cmd, "mkfs.ext2 -j -b %d -L %s ", L_BLOCK_SIZE,
523 mop->mo_ldd.ldd_svname);
525 } else if (mop->mo_ldd.ldd_mount_type == LDD_MT_REISERFS) {
526 long journal_sz = 0; /* FIXME default journal size */
527 if (journal_sz > 0) {
528 sprintf(buf, " --journal_size %ld", journal_sz);
529 strcat(mop->mo_mkfsopts, buf);
531 sprintf(mkfs_cmd, "mkreiserfs -ff ");
534 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
535 progname, mop->mo_ldd.ldd_mount_type,
536 MT_STR(&mop->mo_ldd));
540 /* For loop device format the dev, not the filename */
541 dev = mop->mo_device;
542 if (mop->mo_flags & MO_IS_LOOP)
543 dev = mop->mo_loopdev;
545 vprint("formatting backing filesystem %s on %s\n",
546 MT_STR(&mop->mo_ldd), dev);
547 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
548 vprint("\t4k blocks %d\n", block_count);
549 vprint("\toptions %s\n", mop->mo_mkfsopts);
551 /* mkfs_cmd's trailing space is important! */
552 strcat(mkfs_cmd, mop->mo_mkfsopts);
553 strcat(mkfs_cmd, " ");
554 strcat(mkfs_cmd, dev);
555 if (block_count != 0) {
556 sprintf(buf, " %d", block_count);
557 strcat(mkfs_cmd, buf);
560 vprint("mkfs_cmd = %s\n", mkfs_cmd);
561 ret = run_command(mkfs_cmd);
564 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
572 /* ==================== Lustre config functions =============*/
574 void print_ldd(char *str, struct lustre_disk_data *ldd)
576 printf("\n %s:\n", str);
577 printf("Target: %s\n", ldd->ldd_svname);
578 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
579 printf("Index: unassigned\n");
581 printf("Index: %d\n", ldd->ldd_svindex);
582 if (ldd->ldd_uuid[0])
583 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
584 printf("Lustre FS: %s\n", ldd->ldd_fsname);
585 printf("Mount type: %s\n", MT_STR(ldd));
586 printf("Flags: %#x\n", ldd->ldd_flags);
587 printf(" (%s%s%s%s%s%s%s%s)\n",
588 IS_MDT(ldd) ? "MDT ":"",
589 IS_OST(ldd) ? "OST ":"",
590 IS_MGS(ldd) ? "MGS ":"",
591 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
592 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
593 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
594 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
595 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
596 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
597 printf("Parameters:%s\n", ldd->ldd_params);
598 if (ldd->ldd_userdata[0])
599 printf("Comment: %s\n", ldd->ldd_userdata);
603 /* Write the server config files */
604 int write_local_files(struct mkfs_opts *mop)
606 char mntpt[] = "/tmp/mntXXXXXX";
612 /* Mount this device temporarily in order to write these files */
613 if (!mkdtemp(mntpt)) {
614 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
615 progname, mntpt, strerror(errno));
619 dev = mop->mo_device;
620 if (mop->mo_flags & MO_IS_LOOP)
621 dev = mop->mo_loopdev;
623 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
625 fprintf(stderr, "%s: Unable to mount %s: %s\n",
626 progname, dev, strerror(errno));
627 if (errno == ENODEV) {
628 fprintf(stderr, "Is the %s module available?\n",
629 MT_STR(&mop->mo_ldd));
634 /* Set up initial directories */
635 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
636 ret = mkdir(filepnm, 0777);
637 if ((ret != 0) && (errno != EEXIST)) {
638 fprintf(stderr, "%s: Can't make configs dir %s (%d)\n",
639 progname, filepnm, ret);
641 } else if (errno == EEXIST) {
645 sprintf(filepnm, "%s/%s", mntpt, "ROOT");
646 ret = mkdir(filepnm, 0777);
647 if ((ret != 0) && (errno != EEXIST)) {
648 fprintf(stderr, "%s: Can't make ROOT dir %s (%d)\n",
649 progname, filepnm, ret);
651 } else if (errno == EEXIST) {
655 /* Save the persistent mount data into a file. Lustre must pre-read
656 this file to get the real mount options. */
657 vprint("Writing %s\n", MOUNT_DATA_FILE);
658 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
659 filep = fopen(filepnm, "w");
661 fprintf(stderr, "%s: Unable to create %s file: %s\n",
662 progname, filepnm, strerror(errno));
665 fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
670 /* Check for upgrade */
671 if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS))
672 == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
675 vprint("Copying old logs\n");
677 /* Copy the old client log to fsname-client */
678 sprintf(filepnm, "%s/%s/%s-client",
679 mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
680 sprintf(cmd, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
682 ret = run_command(cmd);
684 fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
685 "(%d)\n", progname, MDT_LOGS_DIR, ret);
686 fprintf(stderr, "mount -t ldiskfs %s somewhere, "
687 "find the client log for fs %s and "
688 "copy it manually into %s/%s-client, "
691 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
692 mop->mo_ldd.ldd_fsname);
696 /* We need to use the old mdt log because otherwise mdt won't
697 have complete lov if old clients connect before all
699 /* Copy the old mdt log to fsname-MDT0000 (get old
700 name from mdt_UUID) */
702 strcpy(filepnm, mop->mo_ldd.ldd_uuid);
703 term = strstr(filepnm, "_UUID");
706 sprintf(cmd, "cp %s/%s/%s %s/%s/%s",
707 mntpt, MDT_LOGS_DIR, filepnm,
708 mntpt, MOUNT_CONFIGS_DIR,
709 mop->mo_ldd.ldd_svname);
710 ret = run_command(cmd);
713 fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
714 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
715 fprintf(stderr, "mount -t ext3 %s somewhere, "
716 "find the MDT log for fs %s and "
717 "copy it manually into %s/%s, "
720 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
721 mop->mo_ldd.ldd_svname);
736 int read_local_files(struct mkfs_opts *mop)
738 char tmpdir[] = "/tmp/dirXXXXXX";
745 /* Make a temporary directory to hold Lustre data files. */
746 if (!mkdtemp(tmpdir)) {
747 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
748 progname, tmpdir, strerror(errno));
752 dev = mop->mo_device;
754 /* Construct debugfs command line. */
755 memset(cmd, 0, sizeof(cmd));
756 sprintf(cmd, "debugfs -c -R 'rdump /%s %s' %s",
757 MOUNT_CONFIGS_DIR, tmpdir, dev);
759 ret = run_command(cmd);
761 verrprint("%s: Unable to dump %s dir (%d)\n",
762 progname, MOUNT_CONFIGS_DIR, ret);
764 sprintf(filepnm, "%s/%s", tmpdir, MOUNT_DATA_FILE);
765 filep = fopen(filepnm, "r");
767 vprint("Reading %s\n", MOUNT_DATA_FILE);
768 fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
771 /* Try to read pre-1.6 config from last_rcvd */
772 struct lr_server_data lsd;
773 verrprint("%s: Unable to read %s (%s).\n",
774 progname, MOUNT_DATA_FILE, strerror(errno));
775 verrprint("Contents of %s:\n", MOUNT_CONFIGS_DIR);
776 sprintf(cmd, "ls -l %s", tmpdir);
778 verrprint("Trying last_rcvd\n");
780 /* Construct debugfs command line. */
781 memset(cmd, 0, sizeof(cmd));
783 "debugfs -c -R 'dump /%s %s/%s' %s",
784 LAST_RCVD, tmpdir, LAST_RCVD, dev);
786 ret = run_command(cmd);
788 fprintf(stderr, "%s: Unable to dump %s file\n",
789 progname, LAST_RCVD);
793 sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
794 filep = fopen(filepnm, "r");
796 fprintf(stderr, "%s: Unable to read old data\n",
801 vprint("Reading %s\n", LAST_RCVD);
802 ret = fread(&lsd, 1, sizeof(lsd), filep);
803 if (ret < sizeof(lsd)) {
804 fprintf(stderr, "%s: Short read (%d of %d)\n",
805 progname, ret, sizeof(lsd));
806 ret = -ferror(filep);
810 vprint("Feature compat=%x, incompat=%x\n",
811 lsd.lsd_feature_compat, lsd.lsd_feature_incompat);
813 if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
814 (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
815 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
816 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
817 } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
818 (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
819 /* We must co-locate so mgs can see old logs.
820 If user doesn't want this, they can copy the old
821 logs manually and re-tunefs. */
822 mop->mo_ldd.ldd_flags =
823 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
824 mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
826 /* If neither is set, we're pre-1.4.6, make a guess. */
827 /* Construct debugfs command line. */
828 memset(cmd, 0, sizeof(cmd));
830 "debugfs -c -R 'rdump /%s %s' %s",
831 MDT_LOGS_DIR, tmpdir, dev);
835 sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
836 if (lsd.lsd_ost_index > 0) {
837 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
838 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
840 /* If there's a LOGS dir, it's an MDT */
841 if ((ret = access(filepnm, F_OK)) == 0) {
842 mop->mo_ldd.ldd_flags =
845 /* Old MDT's are always index 0
847 mop->mo_ldd.ldd_svindex = 0;
849 /* The index may not be correct */
850 mop->mo_ldd.ldd_flags =
851 LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
852 verrprint("OST with unknown index\n");
858 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid,
859 sizeof(mop->mo_ldd.ldd_uuid));
860 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
867 memset(cmd, 0, sizeof(cmd));
868 sprintf(cmd, "rm -rf %s", tmpdir);
874 void set_defaults(struct mkfs_opts *mop)
876 mop->mo_ldd.ldd_magic = LDD_MAGIC;
877 mop->mo_ldd.ldd_config_ver = 1;
878 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
879 mop->mo_mgs_failnodes = 0;
880 strcpy(mop->mo_ldd.ldd_fsname, "lustre");
881 if (get_os_version() == 24)
882 mop->mo_ldd.ldd_mount_type = LDD_MT_EXT3;
884 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
886 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
887 mop->mo_stripe_count = 1;
890 static inline void badopt(const char *opt, char *type)
892 fprintf(stderr, "%s: '--%s' only valid for %s\n",
893 progname, opt, type);
897 static int add_param(char *buf, char *key, char *val)
899 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
900 int start = strlen(buf);
904 keylen = strlen(key);
905 if (start + 1 + keylen + strlen(val) >= end) {
906 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
907 progname, buf, key ? key : "", val);
911 sprintf(buf + start, " %s%s", key ? key : "", val);
915 /* from mount_lustre */
916 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
917 #define MAXNIDSTR 1024
918 static char *convert_hostnames(char *s1)
920 char *converted, *s2 = 0, *c;
921 int left = MAXNIDSTR;
924 converted = malloc(left);
925 if (converted == NULL) {
930 while ((left > 0) && ((s2 = strsep(&s1, ",: \0")))) {
931 nid = libcfs_str2nid(s2);
932 if (nid == LNET_NID_ANY) {
936 fprintf(stderr, "%s: Can't parse NID '%s'\n",
942 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
943 strlen("127.0.0.1")) == 0) {
944 fprintf(stderr, "%s: The NID '%s' resolves to the "
945 "loopback address '%s'. Lustre requires a "
946 "non-loopback address.\n",
947 progname, s2, libcfs_nid2str(nid));
952 c += snprintf(c, left, "%s,", libcfs_nid2str(nid));
953 left = converted + MAXNIDSTR - c;
959 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
962 static struct option long_opt[] = {
963 {"backfstype", 1, 0, 'b'},
964 {"stripe-count-hint", 1, 0, 'c'},
965 {"comment", 1, 0, 'u'},
966 {"configdev", 1, 0, 'C'},
967 {"device-size", 1, 0, 'd'},
968 {"erase-params", 0, 0, 'e'},
969 {"failnode", 1, 0, 'f'},
970 {"failover", 1, 0, 'f'},
973 {"index", 1, 0, 'i'},
974 {"mkfsoptions", 1, 0, 'k'},
975 {"mgsnode", 1, 0, 'm'},
976 {"mgsnid", 1, 0, 'm'},
978 {"fsname",1, 0, 'L'},
979 {"noformat", 0, 0, 'n'},
980 {"nomgs", 0, 0, 'N'},
981 {"mountfsoptions", 1, 0, 'o'},
983 {"param", 1, 0, 'p'},
984 {"print", 0, 0, 'n'},
985 {"quiet", 0, 0, 'q'},
986 {"reformat", 0, 0, 'r'},
987 {"verbose", 0, 0, 'v'},
988 {"writeconf", 0, 0, 'w'},
991 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqru:vw";
995 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
1000 while (i < LDD_MT_LAST) {
1001 if (strcmp(optarg, mt_str(i)) == 0) {
1002 mop->mo_ldd.ldd_mount_type = i;
1010 if (IS_MDT(&mop->mo_ldd)) {
1011 int stripe_count = atol(optarg);
1012 if (stripe_count <= 0) {
1013 fprintf(stderr, "%s: bad stripe count "
1014 "%d\n", progname, stripe_count);
1017 mop->mo_stripe_count = stripe_count;
1019 badopt(long_opt[longidx].name, "MDT");
1023 case 'C': /* Configdev */
1025 printf("Configdev not implemented\n");
1028 mop->mo_device_sz = atol(optarg);
1031 mop->mo_ldd.ldd_params[0] = '\0';
1032 /* Must update the mgs logs */
1033 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1036 char *nids = convert_hostnames(optarg);
1039 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
1044 /* Must update the mgs logs */
1045 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1049 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
1055 if (!(mop->mo_ldd.ldd_flags &
1056 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1057 LDD_F_WRITECONF))) {
1058 fprintf(stderr, "%s: cannot change the index of"
1059 " a registered target\n", progname);
1062 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
1063 mop->mo_ldd.ldd_svindex = atol(optarg);
1064 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
1066 badopt(long_opt[longidx].name, "MDT,OST");
1071 strncpy(mop->mo_mkfsopts, optarg,
1072 sizeof(mop->mo_mkfsopts) - 1);
1075 if (!(mop->mo_ldd.ldd_flags &
1076 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1077 LDD_F_WRITECONF))) {
1078 fprintf(stderr, "%s: cannot change the name of"
1079 " a registered target\n", progname);
1082 if (strlen(optarg) > 8) {
1083 fprintf(stderr, "%s: filesystem name must be "
1084 "<= 8 chars\n", progname);
1088 strncpy(mop->mo_ldd.ldd_fsname, optarg,
1089 sizeof(mop->mo_ldd.ldd_fsname) - 1);
1092 char *nids = convert_hostnames(optarg);
1095 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
1100 mop->mo_mgs_failnodes++;
1104 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1110 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1113 *mountopts = optarg;
1116 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1119 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1122 /* Must update the mgs logs */
1123 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1129 mop->mo_flags |= MO_FORCEFORMAT;
1132 strncpy(mop->mo_ldd.ldd_userdata, optarg,
1133 sizeof(mop->mo_ldd.ldd_userdata));
1134 mop->mo_ldd.ldd_userdata[
1135 sizeof(mop->mo_ldd.ldd_userdata) - 1] = 0;
1141 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1146 fprintf(stderr, "Unknown option '%c'\n", opt);
1152 if (optind >= argc) {
1154 fprintf(stderr, "Bad arguments\n");
1162 #include <lustre/libiam.h>
1164 #define LDISKFS_IOC_GETVERSION _IOR('f', 3, long)
1166 static int mkfs_iam_insert(int key_need_convert, char *keybuf,
1167 int rec_need_convert, char *recbuf, char *filename)
1171 struct iam_uapi_info ua;
1173 fd = iam_open(filename, &ua);
1175 fprintf(stderr, "failed to iam_open %s\n", filename);
1179 ret = iam_insert(fd, &ua,
1180 key_need_convert, keybuf,
1181 rec_need_convert, recbuf);
1184 fprintf(stderr, "failed to iam_insert %s\n", filename);
1191 static int touch_file(char *filename)
1195 if (filename == NULL) {
1199 fd = open(filename, O_CREAT | O_TRUNC, 0600);
1208 static int get_generation(char *filename, unsigned long *result)
1213 if (filename == NULL) {
1217 fd = open(filename, O_RDONLY);
1219 fprintf(stderr, "%s: failed to open %s\n",
1220 __FUNCTION__, filename);
1224 ret = ioctl(fd, LDISKFS_IOC_GETVERSION, result);
1227 return ((ret < 0) ? ret : 0);
1230 static int mkfs_mdt(struct mkfs_opts *mop)
1232 char mntpt[] = "/tmp/mntXXXXXX";
1233 char fstype[] = "ldiskfs";
1238 unsigned long generation;
1241 source = mop->mo_device;
1242 if (mop->mo_flags & MO_IS_LOOP) {
1243 source = mop->mo_loopdev;
1246 if ((source == NULL) || (*source == 0)) {
1250 if (!mkdtemp(mntpt)) {
1251 fprintf(stderr, "%s: failed to mkdtemp %s\n",
1252 __FUNCTION__, mntpt);
1256 ret = mount(source, mntpt, fstype, 0, NULL);
1261 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "seq_ctl");
1262 ret = touch_file(filepnm);
1267 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "seq_srv");
1268 ret = touch_file(filepnm);
1273 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "last_received");
1274 ret = touch_file(filepnm);
1279 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "lov_objid");
1280 ret = touch_file(filepnm);
1285 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "root");
1286 ret = iam_creat(filepnm, FMT_LVAR, L_BLOCK_SIZE, 4, 16, 4);
1291 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "fld");
1292 ret = iam_creat(filepnm, FMT_LFIX, L_BLOCK_SIZE, 8, 8, 4);
1297 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "orphans");
1298 ret = iam_creat(filepnm, FMT_LFIX, L_BLOCK_SIZE, 20, 8, 4);
1303 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "oi");
1304 ret = iam_creat(filepnm, FMT_LFIX, L_BLOCK_SIZE, 16, 16, 4);
1309 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, CAPA_KEYS);
1310 ret = touch_file(filepnm);
1316 ret = mount(source, mntpt, fstype, 0, NULL);
1321 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "root");
1322 ret = iam_polymorph(filepnm, 040755);
1324 perror("IAM_IOC_POLYMORPH");
1329 ret = mount(source, mntpt, fstype, 0, NULL);
1334 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "fld");
1335 ret = mkfs_iam_insert(1, "0000000000000002", 1, "0000000000000000", filepnm);
1340 ret = mkfs_iam_insert(1, "0000000000000001", 1, "0000000000000000", filepnm);
1345 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "root");
1346 ret = stat(filepnm, &st);
1351 ret = get_generation(filepnm, &generation);
1356 snprintf(recbuf, sizeof(recbuf) - 1, "0000000000000001%8.8x%8.8x",
1357 (unsigned int)st.st_ino, (unsigned int)generation);
1358 ret = mkfs_iam_insert(0, ".", 1, recbuf, filepnm);
1363 ret = mkfs_iam_insert(0, "..", 1, recbuf, filepnm);
1375 int main(int argc, char *argv[])
1377 struct mkfs_opts mop;
1378 struct lustre_disk_data *ldd;
1379 char *mountopts = NULL;
1380 char always_mountopts[512] = "";
1381 char default_mountopts[512] = "";
1384 if ((progname = strrchr(argv[0], '/')) != NULL)
1389 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
1394 memset(&mop, 0, sizeof(mop));
1397 /* device is last arg */
1398 strcpy(mop.mo_device, argv[argc - 1]);
1400 /* Are we using a loop device? */
1401 ret = is_block(mop.mo_device);
1405 mop.mo_flags |= MO_IS_LOOP;
1408 /* For tunefs, we must read in the old values before parsing any
1411 /* Check whether the disk has already been formatted by mkfs.lustre */
1412 ret = is_lustre_target(&mop);
1415 fprintf(stderr, "Device %s has not been formatted with "
1416 "mkfs.lustre\n", mop.mo_device);
1421 ret = read_local_files(&mop);
1424 fprintf(stderr, "Failed to read previous Lustre data from %s\n",
1428 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
1429 mop.mo_mgs_failnodes++;
1432 print_ldd("Read previous values", &(mop.mo_ldd));
1435 ret = parse_opts(argc, argv, &mop, &mountopts);
1441 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1443 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
1448 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
1450 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
1455 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
1456 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
1458 fprintf(stderr, "Can't find the target index, "
1459 "specify with --index\n");
1465 * Comment out these 2 checks temporarily, since for multi-MDSes
1466 * in single node only 1 mds node could have mgs service
1468 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1469 verrprint("No management node specified, adding MGS to this "
1471 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
1473 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1475 fprintf(stderr, "Must specify either --mgs or --mgsnode\n");
1481 /* These are the permanent mount options (always included) */
1482 switch (ldd->ldd_mount_type) {
1484 case LDD_MT_LDISKFS: {
1485 sprintf(always_mountopts, "errors=remount-ro");
1486 if (IS_MDT(ldd) || IS_MGS(ldd))
1487 strcat(always_mountopts,
1488 ",iopen_nopriv,user_xattr");
1489 if ((get_os_version() == 24) && IS_OST(ldd))
1490 strcat(always_mountopts, ",asyncdel");
1491 /* NB: Files created while extents are enabled cannot be read
1492 if mounted with a kernel that doesn't include the CFS
1495 ldd->ldd_mount_type == LDD_MT_LDISKFS) {
1496 strcat(default_mountopts, ",extents,mballoc");
1501 mop.mo_flags |= MO_IS_LOOP;
1502 sprintf(always_mountopts, "type=ext3,dev=%s",
1508 fprintf(stderr, "unknown fs type %d '%s'\n",
1509 ldd->ldd_mount_type,
1517 /* If user specifies mount opts, don't use defaults,
1518 but always use always_mountopts */
1519 sprintf(ldd->ldd_mount_opts, "%s,%s",
1520 always_mountopts, mountopts);
1523 if (ldd->ldd_mount_opts[0] == 0)
1524 /* use the defaults unless old opts exist */
1527 sprintf(ldd->ldd_mount_opts, "%s%s",
1528 always_mountopts, default_mountopts);
1532 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1533 ldd->ldd_fsname, ldd->ldd_svname);
1536 print_ldd("Permanent disk data", ldd);
1539 printf("exiting before disk write.\n");
1543 if (check_mtab_entry(mop.mo_device))
1546 /* Create the loopback file */
1547 if (mop.mo_flags & MO_IS_LOOP) {
1548 ret = access(mop.mo_device, F_OK);
1551 #ifndef TUNEFS /* mkfs.lustre */
1552 /* Reformat the loopback file */
1553 if (ret || (mop.mo_flags & MO_FORCEFORMAT))
1554 ret = loop_format(&mop);
1557 ret = loop_setup(&mop);
1560 fprintf(stderr, "Loop device setup for %s failed: %s\n",
1561 mop.mo_device, strerror(ret));
1566 #ifndef TUNEFS /* mkfs.lustre */
1567 /* Check whether the disk has already been formatted by mkfs.lustre */
1568 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1569 ret = is_lustre_target(&mop);
1572 fprintf(stderr, "Device %s was previously formatted "
1573 "for lustre. Use --reformat to reformat it, "
1574 "or tunefs.lustre to modify.\n",
1580 /* Format the backing filesystem */
1581 ret = make_lustre_backfs(&mop);
1584 fprintf(stderr, "mkfs failed %d\n", ret);
1589 /* Write our config files */
1590 ret = write_local_files(&mop);
1593 fprintf(stderr, "failed to write local files\n");
1597 #ifndef TUNEFS /* mkfs.lustre */
1599 ret = mkfs_mdt(&mop);
1601 fprintf(stderr, "failed to mkfs_mdt\n");