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.stripe.size=4194304\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--mountfsoptions=<opts> : permanent mount options\n"
98 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
99 "\t\t--device-size=#N(KB) : device size for loop devices\n"
100 "\t\t--mkfsoptions=<opts> : format options\n"
101 "\t\t--reformat: overwrite an existing disk\n"
102 "\t\t--stripe-count-hint=#N : used for optimizing MDT inode size\n"
104 "\t\t--erase-params : erase all old parameter settings\n"
105 "\t\t--nomgs: turn off MGS service on this MDT\n"
106 "\t\t--writeconf: erase all config logs for this fs.\n"
108 "\t\t--noformat: just report what we would do; "
109 "don't write to disk\n"
115 #define vprint if (verbose > 0) printf
116 #define verrprint if (verbose >= 0) printf
118 static void fatal(void)
121 fprintf(stderr, "\n%s FATAL: ", progname);
124 /*================ utility functions =====================*/
127 dev_major (unsigned long long int __dev)
129 return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
133 dev_minor (unsigned long long int __dev)
135 return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
140 static int version = 0;
144 char release[4] = "";
146 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
148 fprintf(stderr, "%s: Warning: Can't resolve kernel "
149 "version, assuming 2.6\n", progname);
151 read(fd, release, 4);
154 if (strncmp(release, "2.4.", 4) == 0)
162 int run_command(char *cmd)
164 char log[] = "/tmp/mkfs_logXXXXXX";
167 vprint("cmd: %s\n", cmd);
168 if ((fd = mkstemp(log)) >= 0) {
173 strcat(cmd, " 2>&1");
175 /* Can't use popen because we need the rv of the command */
177 if ((rc || (verbose > 2)) && (fd >= 0)) {
180 fp = fopen(log, "r");
182 while (fgets(buf, sizeof(buf), fp) != NULL) {
193 static int check_mtab_entry(char *spec)
198 fp = setmntent(MOUNTED, "r");
202 while ((mnt = getmntent(fp)) != NULL) {
203 if (strcmp(mnt->mnt_fsname, spec) == 0) {
205 fprintf(stderr, "%s: according to %s %s is "
206 "already mounted on %s\n",
207 progname, MOUNTED, spec, mnt->mnt_dir);
216 /*============ disk dev functions ===================*/
218 /* Setup a file in the first unused loop_device */
219 int loop_setup(struct mkfs_opts *mop)
226 /* Figure out the loop device names */
227 if (!access("/dev/loop0", F_OK | R_OK)) {
228 strcpy(loop_base, "/dev/loop\0");
229 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
230 strcpy(loop_base, "/dev/loop/\0");
232 fprintf(stderr, "%s: can't access loop devices\n", progname);
236 /* Find unused loop device */
237 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
239 sprintf(l_device, "%s%d", loop_base, i);
240 if (access(l_device, F_OK | R_OK))
242 sprintf(cmd, "losetup %s > /dev/null 2>&1", l_device);
245 /* losetup gets 1 (ret=256) for non-set-up device */
247 /* Set up a loopback device to our file */
248 sprintf(cmd, "losetup %s %s", l_device, mop->mo_device);
249 ret = run_command(cmd);
251 fprintf(stderr, "%s: error %d on losetup: %s\n",
252 progname, ret, strerror(ret));
255 strcpy(mop->mo_loopdev, l_device);
260 fprintf(stderr, "%s: out of loop devices!\n", progname);
264 int loop_cleanup(struct mkfs_opts *mop)
268 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
269 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
270 ret = run_command(cmd);
275 /* Determine if a device is a block device (as opposed to a file) */
276 int is_block(char* devname)
281 ret = access(devname, F_OK);
284 ret = stat(devname, &st);
286 fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
289 return S_ISBLK(st.st_mode);
292 __u64 get_device_size(char* device)
297 fd = open(device, O_RDONLY);
299 fprintf(stderr, "%s: cannot open %s: %s\n",
300 progname, device, strerror(errno));
305 /* size in bytes. bz5831 */
306 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
311 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
312 size = (__u64)lsize * 512;
317 fprintf(stderr, "%s: size ioctl failed: %s\n",
318 progname, strerror(errno));
322 vprint("device size = "LPU64"MB\n", size >> 20);
323 /* return value in KB */
327 int loop_format(struct mkfs_opts *mop)
331 if (mop->mo_device_sz == 0) {
333 fprintf(stderr, "loop device requires a --device-size= "
338 ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
341 fprintf(stderr, "%s: Unable to create backing store: %d\n",
347 ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
350 fprintf(stderr, "%s: Unable to truncate backing store: %d\n",
357 /* Check whether the file exists in the device */
358 static int file_in_dev(char *file_name, char *dev_name)
361 char debugfs_cmd[256];
362 unsigned int inode_num;
365 /* Construct debugfs command line. */
366 memset(debugfs_cmd, 0, sizeof(debugfs_cmd));
368 "debugfs -c -R 'stat %s' %s 2>&1 | egrep '(Inode|unsupported)'",
369 file_name, dev_name);
371 fp = popen(debugfs_cmd, "r");
373 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
377 if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
381 i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
384 fprintf(stderr, "%s", debugfs_cmd);
385 if (strstr(debugfs_cmd, "unsupported feature")) {
386 fprintf(stderr, "In all likelihood, the "
387 "'unsupported feature' is 'extents', which "
388 "older debugfs does not understand.\n"
389 "Use e2fsprogs-1.38-cfs1 or later, available "
390 "from ftp://ftp.lustre.org/pub/lustre/other/"
399 /* Check whether the device has already been used with lustre */
400 static int is_lustre_target(struct mkfs_opts *mop)
404 vprint("checking for existing Lustre data\n");
405 if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))
406 || (rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
407 vprint("found Lustre data\n");
408 /* in the -1 case, 'extents' means this really IS a lustre
413 return 0; /* The device is not a lustre target. */
416 /* Build fs according to type */
417 int make_lustre_backfs(struct mkfs_opts *mop)
425 if (mop->mo_device_sz != 0) {
426 if (mop->mo_device_sz < 8096){
427 fprintf(stderr, "%s: size of filesystem must be larger "
428 "than 8MB, but is set to %lldKB\n",
429 progname, mop->mo_device_sz);
432 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
435 if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
436 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS)) {
437 __u64 device_sz = mop->mo_device_sz;
439 /* we really need the size */
440 if (device_sz == 0) {
441 device_sz = get_device_size(mop->mo_device);
446 /* Journal size in MB */
447 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
448 /* Choose our own default journal size */
449 long journal_sz = 0, max_sz;
450 if (device_sz > 1024 * 1024) /* 1GB */
451 journal_sz = (device_sz / 102400) * 4;
453 max_sz = (102400 * L_BLOCK_SIZE) >> 20; /* 400MB */
454 if (journal_sz > max_sz)
457 sprintf(buf, " -J size=%ld", journal_sz);
458 strcat(mop->mo_mkfsopts, buf);
462 /* Default bytes_per_inode is block size */
463 if (strstr(mop->mo_mkfsopts, "-i") == NULL) {
464 long bytes_per_inode = 0;
466 if (IS_MDT(&mop->mo_ldd))
467 bytes_per_inode = 4096;
469 /* Allocate fewer inodes on large OST devices. Most
470 filesystems can be much more aggressive than even
472 if ((IS_OST(&mop->mo_ldd) && (device_sz > 1000000)))
473 bytes_per_inode = 16384;
475 if (bytes_per_inode > 0) {
476 sprintf(buf, " -i %ld", bytes_per_inode);
477 strcat(mop->mo_mkfsopts, buf);
481 /* This is an undocumented mke2fs option. Default is 128. */
482 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
484 if (IS_MDT(&mop->mo_ldd)) {
485 if (mop->mo_stripe_count > 77)
486 inode_size = 512; /* bz 7241 */
487 /* cray stripes across all osts (>60) */
488 else if (mop->mo_stripe_count > 34)
490 else if (mop->mo_stripe_count > 13)
494 } else if (IS_OST(&mop->mo_ldd)) {
495 /* now as we store fids in EA on OST we need
496 to make inode bigger */
500 if (inode_size > 0) {
501 sprintf(buf, " -I %ld", inode_size);
502 strcat(mop->mo_mkfsopts, buf);
508 strcat(mop->mo_mkfsopts, " -q");
511 /* Enable hashed b-tree directory lookup in large dirs bz6224 */
512 if (strstr(mop->mo_mkfsopts, "-O") == NULL) {
513 strcat(mop->mo_mkfsopts, " -O dir_index");
516 /* Allow reformat of full devices (as opposed to
517 partitions.) We already checked for mounted dev. */
518 strcat(mop->mo_mkfsopts, " -F");
520 sprintf(mkfs_cmd, "mkfs.ext2 -j -b %d -L %s ", L_BLOCK_SIZE,
521 mop->mo_ldd.ldd_svname);
523 } else if (mop->mo_ldd.ldd_mount_type == LDD_MT_REISERFS) {
524 long journal_sz = 0; /* FIXME default journal size */
525 if (journal_sz > 0) {
526 sprintf(buf, " --journal_size %ld", journal_sz);
527 strcat(mop->mo_mkfsopts, buf);
529 sprintf(mkfs_cmd, "mkreiserfs -ff ");
532 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
533 progname, mop->mo_ldd.ldd_mount_type,
534 MT_STR(&mop->mo_ldd));
538 /* For loop device format the dev, not the filename */
539 dev = mop->mo_device;
540 if (mop->mo_flags & MO_IS_LOOP)
541 dev = mop->mo_loopdev;
543 vprint("formatting backing filesystem %s on %s\n",
544 MT_STR(&mop->mo_ldd), dev);
545 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
546 vprint("\t4k blocks %d\n", block_count);
547 vprint("\toptions %s\n", mop->mo_mkfsopts);
549 /* mkfs_cmd's trailing space is important! */
550 strcat(mkfs_cmd, mop->mo_mkfsopts);
551 strcat(mkfs_cmd, " ");
552 strcat(mkfs_cmd, dev);
553 if (block_count != 0) {
554 sprintf(buf, " %d", block_count);
555 strcat(mkfs_cmd, buf);
558 vprint("mkfs_cmd = %s\n", mkfs_cmd);
559 ret = run_command(mkfs_cmd);
562 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
570 /* ==================== Lustre config functions =============*/
572 void print_ldd(char *str, struct lustre_disk_data *ldd)
574 printf("\n %s:\n", str);
575 printf("Target: %s\n", ldd->ldd_svname);
576 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
577 printf("Index: unassigned\n");
579 printf("Index: %d\n", ldd->ldd_svindex);
580 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
581 printf("Lustre FS: %s\n", ldd->ldd_fsname);
582 printf("Mount type: %s\n", MT_STR(ldd));
583 printf("Flags: %#x\n", ldd->ldd_flags);
584 printf(" (%s%s%s%s%s%s%s%s)\n",
585 IS_MDT(ldd) ? "MDT ":"",
586 IS_OST(ldd) ? "OST ":"",
587 IS_MGS(ldd) ? "MGS ":"",
588 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
589 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
590 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
591 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
592 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
593 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
594 printf("Parameters:%s\n", ldd->ldd_params);
598 /* Write the server config files */
599 int write_local_files(struct mkfs_opts *mop)
601 char mntpt[] = "/tmp/mntXXXXXX";
607 /* Mount this device temporarily in order to write these files */
608 if (!mkdtemp(mntpt)) {
609 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
610 progname, mntpt, strerror(errno));
614 dev = mop->mo_device;
615 if (mop->mo_flags & MO_IS_LOOP)
616 dev = mop->mo_loopdev;
618 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
620 fprintf(stderr, "%s: Unable to mount %s: %s\n",
621 progname, dev, strerror(errno));
622 if (errno == ENODEV) {
623 fprintf(stderr, "Is the %s module available?\n",
624 MT_STR(&mop->mo_ldd));
629 /* Set up initial directories */
630 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
631 ret = mkdir(filepnm, 0777);
632 if ((ret != 0) && (errno != EEXIST)) {
633 fprintf(stderr, "%s: Can't make configs dir %s (%d)\n",
634 progname, filepnm, ret);
636 } else if (errno == EEXIST) {
640 sprintf(filepnm, "%s/%s", mntpt, "ROOT");
641 ret = mkdir(filepnm, 0777);
642 if ((ret != 0) && (errno != EEXIST)) {
643 fprintf(stderr, "%s: Can't make ROOT dir %s (%d)\n",
644 progname, filepnm, ret);
646 } else if (errno == EEXIST) {
650 /* Save the persistent mount data into a file. Lustre must pre-read
651 this file to get the real mount options. */
652 vprint("Writing %s\n", MOUNT_DATA_FILE);
653 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
654 filep = fopen(filepnm, "w");
656 fprintf(stderr, "%s: Unable to create %s file\n",
660 fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
665 /* Check for upgrade */
666 if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS))
667 == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
670 vprint("Copying old logs\n");
672 /* Copy the old client log to fsname-client */
673 sprintf(filepnm, "%s/%s/%s-client",
674 mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
675 sprintf(cmd, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
677 ret = run_command(cmd);
679 fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
680 "(%d)\n", progname, MDT_LOGS_DIR, ret);
681 fprintf(stderr, "mount -t ldiskfs %s somewhere, "
682 "find the client log for fs %s and "
683 "copy it manually into %s/%s-client, "
686 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
687 mop->mo_ldd.ldd_fsname);
691 /* We need to use the old mdt log because otherwise mdt won't
692 have complete lov if old clients connect before all
694 /* Copy the old mdt log to fsname-MDT0000 (get old
695 name from mdt_UUID) */
697 strcpy(filepnm, mop->mo_ldd.ldd_uuid);
698 term = strstr(filepnm, "_UUID");
701 sprintf(cmd, "cp %s/%s/%s %s/%s/%s",
702 mntpt, MDT_LOGS_DIR, filepnm,
703 mntpt, MOUNT_CONFIGS_DIR,
704 mop->mo_ldd.ldd_svname);
705 ret = run_command(cmd);
708 fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
709 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
710 fprintf(stderr, "mount -t ext3 %s somewhere, "
711 "find the MDT log for fs %s and "
712 "copy it manually into %s/%s, "
715 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
716 mop->mo_ldd.ldd_svname);
731 int read_local_files(struct mkfs_opts *mop)
733 char tmpdir[] = "/tmp/dirXXXXXX";
740 /* Make a temporary directory to hold Lustre data files. */
741 if (!mkdtemp(tmpdir)) {
742 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
743 progname, tmpdir, strerror(errno));
747 dev = mop->mo_device;
749 /* Construct debugfs command line. */
750 memset(cmd, 0, sizeof(cmd));
751 sprintf(cmd, "debugfs -c -R 'rdump /%s %s' %s",
752 MOUNT_CONFIGS_DIR, tmpdir, dev);
754 ret = run_command(cmd);
756 verrprint("%s: Unable to dump %s dir (%d)\n",
757 progname, MOUNT_CONFIGS_DIR, ret);
759 sprintf(filepnm, "%s/%s", tmpdir, MOUNT_DATA_FILE);
760 filep = fopen(filepnm, "r");
762 vprint("Reading %s\n", MOUNT_DATA_FILE);
763 fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
766 /* Try to read pre-1.6 config from last_rcvd */
767 struct lr_server_data lsd;
768 verrprint("%s: Unable to read %s (%s).\n",
769 progname, MOUNT_DATA_FILE, strerror(errno));
770 verrprint("Contents of %s:\n", MOUNT_CONFIGS_DIR);
771 sprintf(cmd, "ls -l %s", tmpdir);
773 verrprint("Trying last_rcvd\n");
775 /* Construct debugfs command line. */
776 memset(cmd, 0, sizeof(cmd));
778 "debugfs -c -R 'dump /%s %s/%s' %s",
779 LAST_RCVD, tmpdir, LAST_RCVD, dev);
781 ret = run_command(cmd);
783 fprintf(stderr, "%s: Unable to dump %s file\n",
784 progname, LAST_RCVD);
788 sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
789 filep = fopen(filepnm, "r");
791 fprintf(stderr, "%s: Unable to read old data\n",
796 vprint("Reading %s\n", LAST_RCVD);
797 ret = fread(&lsd, 1, sizeof(lsd), filep);
798 if (ret < sizeof(lsd)) {
799 fprintf(stderr, "%s: Short read (%d of %d)\n",
800 progname, ret, sizeof(lsd));
801 ret = -ferror(filep);
805 vprint("Feature compat=%x, incompat=%x\n",
806 lsd.lsd_feature_compat, lsd.lsd_feature_incompat);
808 if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
809 (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
810 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
811 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
812 } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
813 (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
814 /* We must co-locate so mgs can see old logs.
815 If user doesn't want this, they can copy the old
816 logs manually and re-tunefs. */
817 mop->mo_ldd.ldd_flags =
818 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
819 mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
821 /* If neither is set, we're pre-1.4.6, make a guess. */
822 /* Construct debugfs command line. */
823 memset(cmd, 0, sizeof(cmd));
825 "debugfs -c -R 'rdump /%s %s' %s",
826 MDT_LOGS_DIR, tmpdir, dev);
830 sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
831 if (lsd.lsd_ost_index > 0) {
832 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
833 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
835 /* If there's a LOGS dir, it's an MDT */
836 if ((ret = access(filepnm, F_OK)) == 0) {
837 mop->mo_ldd.ldd_flags =
840 /* Old MDT's are always index 0
842 mop->mo_ldd.ldd_svindex = 0;
844 /* The index may not be correct */
845 mop->mo_ldd.ldd_flags =
846 LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
847 verrprint("OST with unknown index\n");
853 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid,
854 sizeof(mop->mo_ldd.ldd_uuid));
855 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
862 memset(cmd, 0, sizeof(cmd));
863 sprintf(cmd, "rm -rf %s", tmpdir);
869 void set_defaults(struct mkfs_opts *mop)
871 mop->mo_ldd.ldd_magic = LDD_MAGIC;
872 mop->mo_ldd.ldd_config_ver = 1;
873 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
874 mop->mo_mgs_failnodes = 0;
875 strcpy(mop->mo_ldd.ldd_fsname, "lustre");
876 if (get_os_version() == 24)
877 mop->mo_ldd.ldd_mount_type = LDD_MT_EXT3;
879 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
881 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
882 mop->mo_stripe_count = 1;
885 static inline void badopt(const char *opt, char *type)
887 fprintf(stderr, "%s: '--%s' only valid for %s\n",
888 progname, opt, type);
892 static int add_param(char *buf, char *key, char *val)
894 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
895 int start = strlen(buf);
899 keylen = strlen(key);
900 if (start + 1 + keylen + strlen(val) >= end) {
901 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
902 progname, buf, key ? key : "", val);
906 sprintf(buf + start, " %s%s", key ? key : "", val);
910 /* from mount_lustre */
911 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
912 #define MAXNIDSTR 1024
913 static char *convert_hostnames(char *s1)
915 char *converted, *s2 = 0, *c;
916 int left = MAXNIDSTR;
919 converted = malloc(left);
920 if (converted == NULL) {
925 while ((left > 0) && ((s2 = strsep(&s1, ",: \0")))) {
926 nid = libcfs_str2nid(s2);
927 if (nid == LNET_NID_ANY) {
931 fprintf(stderr, "%s: Can't parse NID '%s'\n",
937 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
938 strlen("127.0.0.1")) == 0) {
939 fprintf(stderr, "%s: The NID '%s' resolves to the "
940 "loopback address '%s'. Lustre requires a "
941 "non-loopback address.\n",
942 progname, s2, libcfs_nid2str(nid));
947 c += snprintf(c, left, "%s,", libcfs_nid2str(nid));
948 left = converted + MAXNIDSTR - c;
954 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
957 static struct option long_opt[] = {
958 {"backfstype", 1, 0, 'b'},
959 {"stripe-count-hint", 1, 0, 'c'},
960 {"configdev", 1, 0, 'C'},
961 {"device-size", 1, 0, 'd'},
962 {"erase-params", 0, 0, 'e'},
963 {"failnode", 1, 0, 'f'},
964 {"failover", 1, 0, 'f'},
967 {"index", 1, 0, 'i'},
968 {"mkfsoptions", 1, 0, 'k'},
969 {"mgsnode", 1, 0, 'm'},
970 {"mgsnid", 1, 0, 'm'},
972 {"fsname",1, 0, 'L'},
973 {"noformat", 0, 0, 'n'},
974 {"nomgs", 0, 0, 'N'},
975 {"mountfsoptions", 1, 0, 'o'},
977 {"param", 1, 0, 'p'},
978 {"print", 0, 0, 'n'},
979 {"quiet", 0, 0, 'q'},
980 {"reformat", 0, 0, 'r'},
981 {"verbose", 0, 0, 'v'},
982 {"writeconf", 0, 0, 'w'},
985 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqrvw";
989 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
994 while (i < LDD_MT_LAST) {
995 if (strcmp(optarg, mt_str(i)) == 0) {
996 mop->mo_ldd.ldd_mount_type = i;
1004 if (IS_MDT(&mop->mo_ldd)) {
1005 int stripe_count = atol(optarg);
1006 if (stripe_count <= 0) {
1007 fprintf(stderr, "%s: bad stripe count "
1008 "%d\n", progname, stripe_count);
1011 mop->mo_stripe_count = stripe_count;
1013 badopt(long_opt[longidx].name, "MDT");
1017 case 'C': /* Configdev */
1019 printf("Configdev not implemented\n");
1022 mop->mo_device_sz = atol(optarg);
1025 mop->mo_ldd.ldd_params[0] = '\0';
1026 /* Must update the mgs logs */
1027 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1030 char *nids = convert_hostnames(optarg);
1033 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
1038 /* Must update the mgs logs */
1039 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1043 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
1049 if (!(mop->mo_ldd.ldd_flags &
1050 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1051 LDD_F_WRITECONF))) {
1052 fprintf(stderr, "%s: cannot change the index of"
1053 " a registered target\n", progname);
1056 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
1057 mop->mo_ldd.ldd_svindex = atol(optarg);
1058 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
1060 badopt(long_opt[longidx].name, "MDT,OST");
1065 strncpy(mop->mo_mkfsopts, optarg,
1066 sizeof(mop->mo_mkfsopts) - 1);
1069 if (!(mop->mo_ldd.ldd_flags &
1070 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1071 LDD_F_WRITECONF))) {
1072 fprintf(stderr, "%s: cannot change the name of"
1073 " a registered target\n", progname);
1076 if (strlen(optarg) > 8) {
1077 fprintf(stderr, "%s: filesystem name must be "
1078 "<= 8 chars\n", progname);
1082 strncpy(mop->mo_ldd.ldd_fsname, optarg,
1083 sizeof(mop->mo_ldd.ldd_fsname) - 1);
1086 char *nids = convert_hostnames(optarg);
1089 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
1094 mop->mo_mgs_failnodes++;
1098 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1104 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1107 *mountopts = optarg;
1110 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1113 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1116 /* Must update the mgs logs */
1117 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1123 mop->mo_flags |= MO_FORCEFORMAT;
1129 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1134 fprintf(stderr, "Unknown option '%c'\n", opt);
1140 if (optind >= argc) {
1142 fprintf(stderr, "Bad arguments\n");
1150 #include <lustre/libiam.h>
1152 #define LDISKFS_IOC_GETVERSION _IOR('f', 3, long)
1154 static int mkfs_iam_insert(int key_need_convert, char *keybuf,
1155 int rec_need_convert, char *recbuf, char *filename)
1159 struct iam_uapi_info ua;
1161 fd = iam_open(filename, &ua);
1163 fprintf(stderr, "failed to iam_open %s\n", filename);
1167 ret = iam_insert(fd, &ua,
1168 key_need_convert, keybuf,
1169 rec_need_convert, recbuf);
1172 fprintf(stderr, "failed to iam_insert %s\n", filename);
1179 static int touch_file(char *filename)
1183 if (filename == NULL) {
1187 fd = open(filename, O_CREAT | O_TRUNC, 0600);
1196 static int get_generation(char *filename, unsigned long *result)
1201 if (filename == NULL) {
1205 fd = open(filename, O_RDONLY);
1207 fprintf(stderr, "%s: failed to open %s\n",
1208 __FUNCTION__, filename);
1212 ret = ioctl(fd, LDISKFS_IOC_GETVERSION, result);
1215 return ((ret < 0) ? ret : 0);
1218 static int mkfs_mdt(struct mkfs_opts *mop)
1220 char mntpt[] = "/tmp/mntXXXXXX";
1221 char fstype[] = "ldiskfs";
1226 unsigned long generation;
1229 source = mop->mo_device;
1230 if (mop->mo_flags & MO_IS_LOOP) {
1231 source = mop->mo_loopdev;
1234 if ((source == NULL) || (*source == 0)) {
1238 if (!mkdtemp(mntpt)) {
1239 fprintf(stderr, "%s: failed to mkdtemp %s\n",
1240 __FUNCTION__, mntpt);
1244 ret = mount(source, mntpt, fstype, 0, NULL);
1249 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "seq");
1250 ret = touch_file(filepnm);
1255 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "last_rcvd");
1256 ret = touch_file(filepnm);
1261 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "lov_objid");
1262 ret = touch_file(filepnm);
1267 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "root");
1268 ret = iam_creat(filepnm, FMT_LVAR, L_BLOCK_SIZE, 4, 16, 4);
1273 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "fld");
1274 ret = iam_creat(filepnm, FMT_LFIX, L_BLOCK_SIZE, 8, 8, 4);
1279 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "orphans");
1280 ret = iam_creat(filepnm, FMT_LFIX, L_BLOCK_SIZE, 20, 8, 4);
1285 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "oi");
1286 ret = iam_creat(filepnm, FMT_LFIX, L_BLOCK_SIZE, 16, 16, 4);
1292 ret = mount(source, mntpt, fstype, 0, NULL);
1297 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "root");
1298 ret = iam_polymorph(filepnm, 040755);
1300 perror("IAM_IOC_POLYMORPH");
1305 ret = mount(source, mntpt, fstype, 0, NULL);
1310 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "fld");
1311 ret = mkfs_iam_insert(1, "0000000000000002", 1, "0000000000000000", filepnm);
1316 ret = mkfs_iam_insert(1, "0000000000000001", 1, "0000000000000000", filepnm);
1321 snprintf(filepnm, sizeof(filepnm) - 1, "%s/%s", mntpt, "root");
1322 ret = stat(filepnm, &st);
1327 ret = get_generation(filepnm, &generation);
1332 snprintf(recbuf, sizeof(recbuf) - 1, "0000000000000001%8.8x%8.8x",
1333 (unsigned int)generation, (unsigned int)st.st_ino);
1334 ret = mkfs_iam_insert(0, ".", 1, recbuf, filepnm);
1339 ret = mkfs_iam_insert(0, "..", 1, recbuf, filepnm);
1351 int main(int argc, char *argv[])
1353 struct mkfs_opts mop;
1354 struct lustre_disk_data *ldd;
1355 char *mountopts = NULL;
1356 char always_mountopts[512] = "";
1357 char default_mountopts[512] = "";
1360 //printf("pad %d\n", offsetof(struct lustre_disk_data, ldd_padding));
1361 assert(offsetof(struct lustre_disk_data, ldd_padding) == 200);
1363 if ((progname = strrchr(argv[0], '/')) != NULL)
1368 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
1374 memset(&mop, 0, sizeof(mop));
1377 /* device is last arg */
1378 strcpy(mop.mo_device, argv[argc - 1]);
1380 /* Are we using a loop device? */
1381 ret = is_block(mop.mo_device);
1385 mop.mo_flags |= MO_IS_LOOP;
1388 /* For tunefs, we must read in the old values before parsing any
1391 /* Check whether the disk has already been formatted by mkfs.lustre */
1392 ret = is_lustre_target(&mop);
1395 fprintf(stderr, "Device %s has not been formatted with "
1396 "mkfs.lustre\n", mop.mo_device);
1401 ret = read_local_files(&mop);
1404 fprintf(stderr, "Failed to read previous Lustre data from %s\n",
1408 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
1409 mop.mo_mgs_failnodes++;
1412 print_ldd("Read previous values", &(mop.mo_ldd));
1415 ret = parse_opts(argc, argv, &mop, &mountopts);
1421 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1423 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
1428 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
1430 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
1435 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
1436 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
1438 fprintf(stderr, "Can't find the target index, "
1439 "specify with --index\n");
1444 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1445 verrprint("No management node specified, adding MGS to this "
1447 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
1450 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1452 fprintf(stderr, "Must specify either --mgs or --mgsnode\n");
1457 /* These are the permanent mount options (always included) */
1458 switch (ldd->ldd_mount_type) {
1460 case LDD_MT_LDISKFS: {
1461 sprintf(always_mountopts, "errors=remount-ro");
1462 if (IS_MDT(ldd) || IS_MGS(ldd))
1463 strcat(always_mountopts,
1464 ",iopen_nopriv,user_xattr");
1465 if ((get_os_version() == 24) && IS_OST(ldd))
1466 strcat(always_mountopts, ",asyncdel");
1467 /* NB: Files created while extents are enabled cannot be read
1468 if mounted with a kernel that doesn't include the CFS
1471 ldd->ldd_mount_type == LDD_MT_LDISKFS) {
1472 strcat(default_mountopts, ",extents,mballoc");
1477 mop.mo_flags |= MO_IS_LOOP;
1478 sprintf(always_mountopts, "type=ext3,dev=%s",
1484 fprintf(stderr, "unknown fs type %d '%s'\n",
1485 ldd->ldd_mount_type,
1493 /* If user specifies mount opts, don't use defaults,
1494 but always use always_mountopts */
1495 sprintf(ldd->ldd_mount_opts, "%s,%s",
1496 always_mountopts, mountopts);
1499 if (ldd->ldd_mount_opts[0] == 0)
1500 /* use the defaults unless old opts exist */
1503 sprintf(ldd->ldd_mount_opts, "%s%s",
1504 always_mountopts, default_mountopts);
1508 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1509 ldd->ldd_fsname, ldd->ldd_svname);
1512 print_ldd("Permanent disk data", ldd);
1515 printf("exiting before disk write.\n");
1519 if (check_mtab_entry(mop.mo_device))
1522 /* Create the loopback file */
1523 if (mop.mo_flags & MO_IS_LOOP) {
1524 ret = access(mop.mo_device, F_OK);
1527 #ifndef TUNEFS /* mkfs.lustre */
1528 /* Reformat the loopback file */
1529 if (ret || (mop.mo_flags & MO_FORCEFORMAT))
1530 ret = loop_format(&mop);
1533 ret = loop_setup(&mop);
1536 fprintf(stderr, "Loop device setup for %s failed: %s\n",
1537 mop.mo_device, strerror(ret));
1542 #ifndef TUNEFS /* mkfs.lustre */
1543 /* Check whether the disk has already been formatted by mkfs.lustre */
1544 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1545 ret = is_lustre_target(&mop);
1548 fprintf(stderr, "Device %s was previously formatted "
1549 "for lustre. Use --reformat to reformat it, "
1550 "or tunefs.lustre to modify.\n",
1556 /* Format the backing filesystem */
1557 ret = make_lustre_backfs(&mop);
1560 fprintf(stderr, "mkfs failed %d\n", ret);
1565 /* Write our config files */
1566 ret = write_local_files(&mop);
1569 fprintf(stderr, "failed to write local files\n");
1573 #ifndef TUNEFS /* mkfs.lustre */
1575 ret = mkfs_mdt(&mop);
1577 fprintf(stderr, "failed to mkfs_mdt\n");