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>
42 # include <linux/fs.h> /* for BLKGETSIZE64 */
44 #include <lustre_disk.h>
45 #include <lustre_param.h>
46 #include <lnet/lnetctl.h>
47 #include <lustre_ver.h>
53 #define MAX_LOOP_DEVICES 16
54 #define L_BLOCK_SIZE 4096
55 #define INDEX_UNASSIGNED 0xFFFF
56 #define MO_IS_LOOP 0x01
57 #define MO_FORCEFORMAT 0x02
59 /* used to describe the options to format the lustre disk, not persistent */
61 struct lustre_disk_data mo_ldd; /* to be written in MOUNT_DATA_FILE */
62 char mo_device[128]; /* disk device name */
63 char mo_mkfsopts[128]; /* options to the backing-store mkfs */
64 char mo_loopdev[128]; /* in case a loop dev is needed */
65 __u64 mo_device_sz; /* in KB */
71 static char *progname;
72 static int verbose = 1;
73 static int print_only = 0;
78 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
79 fprintf(out, "usage: %s <target types> [options] <device>\n", progname);
81 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
83 "\t\t--ost: object storage, mutually exclusive with mdt,mgs\n"
84 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
85 "\t\t--mgs: configuration management service - one per site\n"
86 "\toptions (in order of popularity):\n"
87 "\t\t--mgsnode=<nid>[,<...>] : NID(s) of a remote mgs node\n"
88 "\t\t\trequired for all targets other than the mgs node\n"
89 "\t\t--fsname=<filesystem_name> : default is 'lustre'\n"
90 "\t\t--failnode=<nid>[,<...>] : NID(s) of a failover partner\n"
91 "\t\t--param <key>=<value> : set a permanent parameter\n"
92 "\t\t\te.g. --param sys.timeout=40\n"
93 "\t\t\t --param lov.stripesize=2M\n"
94 "\t\t--index=#N : target index (i.e. ost index within the lov)\n"
95 /* FIXME implement 1.6.x
96 "\t\t--configdev=<altdevice|file>: store configuration info\n"
97 "\t\t\tfor this device on an alternate device\n"
99 "\t\t--comment=<user comment>: arbitrary user string (%d bytes)\n"
100 "\t\t--mountfsoptions=<opts> : permanent mount options\n"
102 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
103 "\t\t--device-size=#N(KB) : device size for loop devices\n"
104 "\t\t--mkfsoptions=<opts> : format options\n"
105 "\t\t--reformat: overwrite an existing disk\n"
106 "\t\t--stripe-count-hint=#N : used for optimizing MDT inode size\n"
108 "\t\t--erase-params : erase all old parameter settings\n"
109 "\t\t--nomgs: turn off MGS service on this MDT\n"
110 "\t\t--writeconf: erase all config logs for this fs.\n"
112 "\t\t--dryrun: just report what we would do; "
113 "don't write to disk\n"
114 "\t\t--verbose : e.g. show mkfs progress\n"
116 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
120 #define vprint if (verbose > 0) printf
121 #define verrprint if (verbose >= 0) printf
123 static void fatal(void)
126 fprintf(stderr, "\n%s FATAL: ", progname);
129 /*================ utility functions =====================*/
131 char *strscat(char *dst, char *src, int buflen) {
133 if (strlen(dst) + strlen(src) >= buflen) {
134 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
135 "\n", buflen, dst, src);
138 return strcat(dst, src);
142 char *strscpy(char *dst, char *src, int buflen) {
144 return strscat(dst, src, buflen);
148 dev_major (unsigned long long int __dev)
150 return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
154 dev_minor (unsigned long long int __dev)
156 return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
161 static int version = 0;
165 char release[4] = "";
167 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
169 fprintf(stderr, "%s: Warning: Can't resolve kernel "
170 "version, assuming 2.6\n", progname);
172 read(fd, release, 4);
175 if (strncmp(release, "2.4.", 4) == 0)
183 int run_command(char *cmd, int cmdsz)
185 char log[] = "/tmp/mkfs_logXXXXXX";
188 if ((cmdsz - strlen(cmd)) < 6) {
190 fprintf(stderr, "Command buffer overflow: %.*s...\n",
196 printf("cmd: %s\n", cmd);
198 if ((fd = mkstemp(log)) >= 0) {
204 strcat(cmd, " 2>&1");
206 /* Can't use popen because we need the rv of the command */
208 if (rc && (fd >= 0)) {
211 fp = fopen(log, "r");
213 while (fgets(buf, sizeof(buf), fp) != NULL) {
224 static int check_mtab_entry(char *spec)
229 fp = setmntent(MOUNTED, "r");
233 while ((mnt = getmntent(fp)) != NULL) {
234 if (strcmp(mnt->mnt_fsname, spec) == 0) {
236 fprintf(stderr, "%s: according to %s %s is "
237 "already mounted on %s\n",
238 progname, MOUNTED, spec, mnt->mnt_dir);
247 /*============ disk dev functions ===================*/
249 /* Setup a file in the first unused loop_device */
250 int loop_setup(struct mkfs_opts *mop)
256 /* Figure out the loop device names */
257 if (!access("/dev/loop0", F_OK | R_OK))
258 strcpy(loop_base, "/dev/loop\0");
259 else if (!access("/dev/loop/0", F_OK | R_OK))
260 strcpy(loop_base, "/dev/loop/\0");
262 fprintf(stderr, "%s: can't access loop devices\n", progname);
266 /* Find unused loop device */
267 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
269 int cmdsz = sizeof(cmd);
270 sprintf(l_device, "%s%d", loop_base, i);
271 if (access(l_device, F_OK | R_OK))
273 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
276 /* losetup gets 1 (ret=256) for non-set-up device */
278 /* Set up a loopback device to our file */
279 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
281 ret = run_command(cmd, cmdsz);
283 fprintf(stderr, "%s: error %d on losetup: %s\n",
284 progname, ret, strerror(ret));
287 strscpy(mop->mo_loopdev, l_device,
288 sizeof(mop->mo_loopdev));
293 fprintf(stderr, "%s: out of loop devices!\n", progname);
297 int loop_cleanup(struct mkfs_opts *mop)
301 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
302 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
303 ret = run_command(cmd, sizeof(cmd));
308 /* Determine if a device is a block device (as opposed to a file) */
309 int is_block(char* devname)
314 ret = access(devname, F_OK);
317 ret = stat(devname, &st);
319 fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
322 return S_ISBLK(st.st_mode);
325 __u64 get_device_size(char* device)
330 fd = open(device, O_RDONLY);
332 fprintf(stderr, "%s: cannot open %s: %s\n",
333 progname, device, strerror(errno));
338 /* size in bytes. bz5831 */
339 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
344 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
345 size = (__u64)lsize * 512;
350 fprintf(stderr, "%s: size ioctl failed: %s\n",
351 progname, strerror(errno));
355 vprint("device size = "LPU64"MB\n", size >> 20);
356 /* return value in KB */
360 int loop_format(struct mkfs_opts *mop)
364 if (mop->mo_device_sz == 0) {
366 fprintf(stderr, "loop device requires a --device-size= "
371 ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
372 ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
375 fprintf(stderr, "%s: Unable to create backing store: %d\n",
382 /* Check whether the file exists in the device */
383 static int file_in_dev(char *file_name, char *dev_name)
386 char debugfs_cmd[256];
387 unsigned int inode_num;
390 /* Construct debugfs command line. */
391 snprintf(debugfs_cmd, sizeof(debugfs_cmd),
392 "debugfs -c -R 'stat %s' %s 2>&1 | egrep '(Inode|unsupported)'",
393 file_name, dev_name);
395 fp = popen(debugfs_cmd, "r");
397 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
401 if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
405 i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
408 fprintf(stderr, "%s", debugfs_cmd);
409 if (strstr(debugfs_cmd, "unsupported feature")) {
410 fprintf(stderr, "In all likelihood, the "
411 "'unsupported feature' is 'extents', which "
412 "older debugfs does not understand.\n"
413 "Use e2fsprogs-1.39-cfs8 or later, available "
414 "from ftp://ftp.lustre.org/pub/lustre/other/"
423 /* Check whether the device has already been used with lustre */
424 static int is_lustre_target(struct mkfs_opts *mop)
427 vprint("checking for existing Lustre data: ");
429 if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))) {
431 (rc == 1) ? MOUNT_DATA_FILE : "extents");
432 /* in the -1 case, 'extents' means this really IS a lustre
437 if ((rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
438 vprint("found %s\n", LAST_RCVD);
442 vprint("not found\n");
443 return 0; /* The device is not a lustre target. */
446 /* Build fs according to type */
447 int make_lustre_backfs(struct mkfs_opts *mop)
449 char mkfs_cmd[PATH_MAX];
455 if (mop->mo_device_sz != 0) {
456 if (mop->mo_device_sz < 8096){
457 fprintf(stderr, "%s: size of filesystem must be larger "
458 "than 8MB, but is set to %lluKB\n",
459 progname, (long long)mop->mo_device_sz);
462 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
465 if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
466 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS) ||
467 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS2)) {
468 __u64 device_sz = mop->mo_device_sz;
470 /* we really need the size */
471 if (device_sz == 0) {
472 device_sz = get_device_size(mop->mo_device);
477 /* Journal size in MB */
478 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
479 /* Choose our own default journal size */
480 long journal_sz = 0, max_sz;
481 if (device_sz > 1024 * 1024) /* 1GB */
482 journal_sz = (device_sz / 102400) * 4;
484 max_sz = (102400 * L_BLOCK_SIZE) >> 20; /* 400MB */
485 if (journal_sz > max_sz)
488 sprintf(buf, " -J size=%ld", journal_sz);
489 strscat(mop->mo_mkfsopts, buf,
490 sizeof(mop->mo_mkfsopts));
494 /* bytes_per_inode: disk size / num inodes */
495 if (strstr(mop->mo_mkfsopts, "-i") == NULL) {
496 long bytes_per_inode = 0;
498 if (IS_MDT(&mop->mo_ldd))
499 bytes_per_inode = 4096;
501 /* Allocate fewer inodes on large OST devices. Most
502 filesystems can be much more aggressive than even
504 if ((IS_OST(&mop->mo_ldd) && (device_sz > 1000000)))
505 bytes_per_inode = 16384;
507 if (bytes_per_inode > 0) {
508 sprintf(buf, " -i %ld", bytes_per_inode);
509 strscat(mop->mo_mkfsopts, buf,
510 sizeof(mop->mo_mkfsopts));
514 /* Inode size (for extended attributes). The LOV EA size is
515 * 32 (EA hdr) + 32 (lov_mds_md) + stripes * 24 (lov_ost_data),
516 * and we want some margin above that for ACLs, other EAs... */
517 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
519 if (IS_MDT(&mop->mo_ldd)) {
520 if (mop->mo_stripe_count > 72)
521 inode_size = 512; /* bz 7241 */
522 /* cray stripes across all osts (>60) */
523 else if (mop->mo_stripe_count > 32)
525 else if (mop->mo_stripe_count > 10)
529 } else if (IS_OST(&mop->mo_ldd)) {
530 /* now as we store fids in EA on OST we need
531 to make inode bigger */
535 if (inode_size > 0) {
536 sprintf(buf, " -I %ld", inode_size);
537 strscat(mop->mo_mkfsopts, buf,
538 sizeof(mop->mo_mkfsopts));
543 strscat(mop->mo_mkfsopts, " -q",
544 sizeof(mop->mo_mkfsopts));
547 if (strstr(mop->mo_mkfsopts, "-O") == NULL) {
548 /* Enable hashed b-tree directory lookup in large dirs
549 bz6224, and don't initialize all groups. */
550 strscat(mop->mo_mkfsopts, " -O dir_index,uninit_groups",
551 sizeof(mop->mo_mkfsopts));
554 /* Allow reformat of full devices (as opposed to
555 partitions.) We already checked for mounted dev. */
556 strscat(mop->mo_mkfsopts, " -F", sizeof(mop->mo_mkfsopts));
558 snprintf(mkfs_cmd, sizeof(mkfs_cmd),
559 "mkfs.ext2 -j -b %d -L %s ", L_BLOCK_SIZE,
560 mop->mo_ldd.ldd_svname);
562 } else if (mop->mo_ldd.ldd_mount_type == LDD_MT_REISERFS) {
563 long journal_sz = 0; /* FIXME default journal size */
564 if (journal_sz > 0) {
565 sprintf(buf, " --journal_size %ld", journal_sz);
566 strscat(mop->mo_mkfsopts, buf,
567 sizeof(mop->mo_mkfsopts));
569 snprintf(mkfs_cmd, sizeof(mkfs_cmd), "mkreiserfs -ff ");
572 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
573 progname, mop->mo_ldd.ldd_mount_type,
574 MT_STR(&mop->mo_ldd));
578 /* For loop device format the dev, not the filename */
579 dev = mop->mo_device;
580 if (mop->mo_flags & MO_IS_LOOP)
581 dev = mop->mo_loopdev;
583 vprint("formatting backing filesystem %s on %s\n",
584 MT_STR(&mop->mo_ldd), dev);
585 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
586 vprint("\t4k blocks %d\n", block_count);
587 vprint("\toptions %s\n", mop->mo_mkfsopts);
589 /* mkfs_cmd's trailing space is important! */
590 strscat(mkfs_cmd, mop->mo_mkfsopts, sizeof(mkfs_cmd));
591 strscat(mkfs_cmd, " ", sizeof(mkfs_cmd));
592 strscat(mkfs_cmd, dev, sizeof(mkfs_cmd));
593 if (block_count != 0) {
594 sprintf(buf, " %d", block_count);
595 strscat(mkfs_cmd, buf, sizeof(mkfs_cmd));
598 vprint("mkfs_cmd = %s\n", mkfs_cmd);
599 ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
602 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
607 /* ==================== Lustre config functions =============*/
609 void print_ldd(char *str, struct lustre_disk_data *ldd)
611 printf("\n %s:\n", str);
612 printf("Target: %s\n", ldd->ldd_svname);
613 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
614 printf("Index: unassigned\n");
616 printf("Index: %d\n", ldd->ldd_svindex);
617 if (ldd->ldd_uuid[0])
618 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
619 printf("Lustre FS: %s\n", ldd->ldd_fsname);
620 printf("Mount type: %s\n", MT_STR(ldd));
621 printf("Flags: %#x\n", ldd->ldd_flags);
622 printf(" (%s%s%s%s%s%s%s%s)\n",
623 IS_MDT(ldd) ? "MDT ":"",
624 IS_OST(ldd) ? "OST ":"",
625 IS_MGS(ldd) ? "MGS ":"",
626 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
627 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
628 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
629 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
630 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
631 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
632 printf("Parameters:%s\n", ldd->ldd_params);
633 if (ldd->ldd_userdata[0])
634 printf("Comment: %s\n", ldd->ldd_userdata);
638 /* Write the server config files */
639 int write_local_files(struct mkfs_opts *mop)
641 char mntpt[] = "/tmp/mntXXXXXX";
647 /* Mount this device temporarily in order to write these files */
648 if (!mkdtemp(mntpt)) {
649 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
650 progname, mntpt, strerror(errno));
654 dev = mop->mo_device;
655 if (mop->mo_flags & MO_IS_LOOP)
656 dev = mop->mo_loopdev;
658 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
660 fprintf(stderr, "%s: Unable to mount %s: %s\n",
661 progname, dev, strerror(errno));
663 if (errno == ENODEV) {
664 fprintf(stderr, "Is the %s module available?\n",
665 MT_STR(&mop->mo_ldd));
670 /* Set up initial directories */
671 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
672 ret = mkdir(filepnm, 0777);
673 if ((ret != 0) && (errno != EEXIST)) {
674 fprintf(stderr, "%s: Can't make configs dir %s: %s\n",
675 progname, filepnm, strerror(errno));
677 } else if (errno == EEXIST) {
681 /* Save the persistent mount data into a file. Lustre must pre-read
682 this file to get the real mount options. */
683 vprint("Writing %s\n", MOUNT_DATA_FILE);
684 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
685 filep = fopen(filepnm, "w");
687 fprintf(stderr, "%s: Unable to create %s file: %s\n",
688 progname, filepnm, strerror(errno));
691 fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
696 /* Check for upgrade */
697 if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS))
698 == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
701 int cmdsz = sizeof(cmd);
702 vprint("Copying old logs\n");
704 /* Copy the old client log to fsname-client */
705 sprintf(filepnm, "%s/%s/%s-client",
706 mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
707 snprintf(cmd, cmdsz, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
709 ret = run_command(cmd, cmdsz);
711 fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
712 "(%d)\n", progname, MDT_LOGS_DIR, ret);
713 fprintf(stderr, "mount -t ldiskfs %s somewhere, "
714 "find the client log for fs %s and "
715 "copy it manually into %s/%s-client, "
718 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
719 mop->mo_ldd.ldd_fsname);
723 /* We need to use the old mdt log because otherwise mdt won't
724 have complete lov if old clients connect before all
726 /* Copy the old mdt log to fsname-MDT0000 (get old
727 name from mdt_UUID) */
729 strscpy(filepnm, (char *)mop->mo_ldd.ldd_uuid, sizeof(filepnm));
730 term = strstr(filepnm, "_UUID");
733 snprintf(cmd, cmdsz, "cp %s/%s/%s %s/%s/%s",
734 mntpt, MDT_LOGS_DIR, filepnm,
735 mntpt, MOUNT_CONFIGS_DIR,
736 mop->mo_ldd.ldd_svname);
737 ret = run_command(cmd, cmdsz);
740 fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
741 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
742 fprintf(stderr, "mount -t ext3 %s somewhere, "
743 "find the MDT log for fs %s and "
744 "copy it manually into %s/%s, "
747 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
748 mop->mo_ldd.ldd_svname);
763 int read_local_files(struct mkfs_opts *mop)
765 char tmpdir[] = "/tmp/dirXXXXXX";
771 int cmdsz = sizeof(cmd);
773 /* Make a temporary directory to hold Lustre data files. */
774 if (!mkdtemp(tmpdir)) {
775 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
776 progname, tmpdir, strerror(errno));
780 dev = mop->mo_device;
782 /* Construct debugfs command line. */
783 snprintf(cmd, cmdsz, "debugfs -c -R 'dump /%s %s/mountdata' %s",
784 MOUNT_DATA_FILE, tmpdir, dev);
786 ret = run_command(cmd, cmdsz);
788 verrprint("%s: Unable to dump %s dir (%d)\n",
789 progname, MOUNT_CONFIGS_DIR, ret);
791 sprintf(filepnm, "%s/mountdata", tmpdir);
792 filep = fopen(filepnm, "r");
794 vprint("Reading %s\n", MOUNT_DATA_FILE);
795 fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
798 /* Try to read pre-1.6 config from last_rcvd */
799 struct lr_server_data lsd;
800 verrprint("%s: Unable to read %s (%s).\n",
801 progname, filepnm, strerror(errno));
803 verrprint("Trying last_rcvd\n");
804 sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
806 /* Construct debugfs command line. */
807 snprintf(cmd, cmdsz, "debugfs -c -R 'dump /%s %s' %s",
808 LAST_RCVD, filepnm, dev);
810 ret = run_command(cmd, cmdsz);
812 fprintf(stderr, "%s: Unable to dump %s file (%d)\n",
813 progname, LAST_RCVD, ret);
817 filep = fopen(filepnm, "r");
819 fprintf(stderr, "%s: Unable to open %s: %s\n",
820 progname, filepnm, strerror(errno));
822 verrprint("Contents of %s:\n", tmpdir);
824 snprintf(cmd, cmdsz, "ls -l %s/", tmpdir);
825 run_command(cmd, cmdsz);
826 verrprint("Contents of disk:\n");
827 snprintf(cmd, cmdsz, "debugfs -c -R 'ls -l /' %s", dev);
828 run_command(cmd, cmdsz);
832 vprint("Reading %s\n", LAST_RCVD);
833 ret = fread(&lsd, 1, sizeof(lsd), filep);
834 if (ret < sizeof(lsd)) {
835 fprintf(stderr, "%s: Short read (%d of %d)\n",
836 progname, ret, (int)sizeof(lsd));
841 vprint("Feature compat=%x, incompat=%x\n",
842 lsd.lsd_feature_compat, lsd.lsd_feature_incompat);
844 if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
845 (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
846 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
847 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
848 } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
849 (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
850 /* We must co-locate so mgs can see old logs.
851 If user doesn't want this, they can copy the old
852 logs manually and re-tunefs. */
853 mop->mo_ldd.ldd_flags =
854 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
855 mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
857 /* If neither is set, we're pre-1.4.6, make a guess. */
858 /* Construct debugfs command line. */
859 snprintf(cmd, cmdsz, "debugfs -c -R 'rdump /%s %s' %s",
860 MDT_LOGS_DIR, tmpdir, dev);
861 run_command(cmd, cmdsz);
863 sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
864 if (lsd.lsd_ost_index > 0) {
865 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
866 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
868 /* If there's a LOGS dir, it's an MDT */
869 if ((ret = access(filepnm, F_OK)) == 0) {
870 mop->mo_ldd.ldd_flags =
873 /* Old MDT's are always index 0
875 mop->mo_ldd.ldd_svindex = 0;
877 /* The index may not be correct */
878 mop->mo_ldd.ldd_flags =
879 LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
880 verrprint("OST with unknown index\n");
886 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid,
887 sizeof(mop->mo_ldd.ldd_uuid));
888 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
895 snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
896 run_command(cmd, cmdsz);
898 verrprint("Failed to read old data (%d)\n", ret);
903 void set_defaults(struct mkfs_opts *mop)
905 mop->mo_ldd.ldd_magic = LDD_MAGIC;
906 mop->mo_ldd.ldd_config_ver = 1;
907 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
908 mop->mo_mgs_failnodes = 0;
909 strcpy(mop->mo_ldd.ldd_fsname, "lustre");
910 if (get_os_version() == 24)
911 mop->mo_ldd.ldd_mount_type = LDD_MT_EXT3;
913 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
915 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
916 mop->mo_stripe_count = 1;
919 static inline void badopt(const char *opt, char *type)
921 fprintf(stderr, "%s: '--%s' only valid for %s\n",
922 progname, opt, type);
926 static int add_param(char *buf, char *key, char *val)
928 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
929 int start = strlen(buf);
933 keylen = strlen(key);
934 if (start + 1 + keylen + strlen(val) >= end) {
935 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
936 progname, buf, key ? key : "", val);
940 sprintf(buf + start, " %s%s", key ? key : "", val);
944 /* from mount_lustre */
945 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
946 #define MAXNIDSTR 1024
947 static char *convert_hostnames(char *s1)
949 char *converted, *s2 = 0, *c;
950 int left = MAXNIDSTR;
953 converted = malloc(left);
955 while ((left > 0) && ((s2 = strsep(&s1, ",: \0")))) {
956 nid = libcfs_str2nid(s2);
957 if (nid == LNET_NID_ANY) {
961 fprintf(stderr, "%s: Can't parse NID '%s'\n",
967 if (strncmp(libcfs_nid2str(nid), "127.0.0.1",
968 strlen("127.0.0.1")) == 0) {
969 fprintf(stderr, "%s: The NID '%s' resolves to the "
970 "loopback address '%s'. Lustre requires a "
971 "non-loopback address.\n",
972 progname, s2, libcfs_nid2str(nid));
977 c += snprintf(c, left, "%s,", libcfs_nid2str(nid));
978 left = converted + MAXNIDSTR - c;
984 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
987 static struct option long_opt[] = {
988 {"backfstype", 1, 0, 'b'},
989 {"stripe-count-hint", 1, 0, 'c'},
990 {"comment", 1, 0, 'u'},
991 {"configdev", 1, 0, 'C'},
992 {"device-size", 1, 0, 'd'},
993 {"dryrun", 0, 0, 'n'},
994 {"erase-params", 0, 0, 'e'},
995 {"failnode", 1, 0, 'f'},
996 {"failover", 1, 0, 'f'},
999 {"index", 1, 0, 'i'},
1000 {"mkfsoptions", 1, 0, 'k'},
1001 {"mgsnode", 1, 0, 'm'},
1002 {"mgsnid", 1, 0, 'm'},
1004 {"fsname",1, 0, 'L'},
1005 {"noformat", 0, 0, 'n'},
1006 {"nomgs", 0, 0, 'N'},
1007 {"mountfsoptions", 1, 0, 'o'},
1009 {"param", 1, 0, 'p'},
1010 {"print", 0, 0, 'n'},
1011 {"quiet", 0, 0, 'q'},
1012 {"reformat", 0, 0, 'r'},
1013 {"verbose", 0, 0, 'v'},
1014 {"writeconf", 0, 0, 'w'},
1017 char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqru:vw";
1021 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
1026 while (i < LDD_MT_LAST) {
1027 if (strcmp(optarg, mt_str(i)) == 0) {
1028 mop->mo_ldd.ldd_mount_type = i;
1036 if (IS_MDT(&mop->mo_ldd)) {
1037 int stripe_count = atol(optarg);
1038 if (stripe_count <= 0) {
1039 fprintf(stderr, "%s: bad stripe count "
1040 "%d\n", progname, stripe_count);
1043 mop->mo_stripe_count = stripe_count;
1045 badopt(long_opt[longidx].name, "MDT");
1049 case 'C': /* Configdev */
1051 printf("Configdev not implemented\n");
1054 mop->mo_device_sz = atol(optarg);
1057 mop->mo_ldd.ldd_params[0] = '\0';
1058 /* Must update the mgs logs */
1059 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1062 char *nids = convert_hostnames(optarg);
1065 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
1067 /* Combo needs to add MDT failnodes as MGS failnodes
1069 if (!rc && IS_MGS(&mop->mo_ldd)) {
1070 rc = add_param(mop->mo_ldd.ldd_params,
1071 PARAM_MGSNODE, nids);
1076 /* Must update the mgs logs */
1077 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1081 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
1087 if (!(mop->mo_ldd.ldd_flags &
1088 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1089 LDD_F_WRITECONF))) {
1090 fprintf(stderr, "%s: cannot change the index of"
1091 " a registered target\n", progname);
1094 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
1095 mop->mo_ldd.ldd_svindex = atol(optarg);
1096 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
1098 badopt(long_opt[longidx].name, "MDT,OST");
1103 strscpy(mop->mo_mkfsopts, optarg,
1104 sizeof(mop->mo_mkfsopts));
1108 if (!(mop->mo_flags & MO_FORCEFORMAT) &&
1109 (!(mop->mo_ldd.ldd_flags &
1110 (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1111 LDD_F_WRITECONF)))) {
1112 fprintf(stderr, "%s: cannot change the name of"
1113 " a registered target\n", progname);
1116 if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
1117 fprintf(stderr, "%s: filesystem name must be "
1118 "1-8 chars\n", progname);
1121 if ((tmp = strpbrk(optarg, "/:"))) {
1122 fprintf(stderr, "%s: char '%c' not allowed in "
1123 "filesystem name\n", progname, *tmp);
1126 strscpy(mop->mo_ldd.ldd_fsname, optarg,
1127 sizeof(mop->mo_ldd.ldd_fsname));
1131 char *nids = convert_hostnames(optarg);
1134 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
1139 mop->mo_mgs_failnodes++;
1143 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1149 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1152 *mountopts = optarg;
1155 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1158 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1161 /* Must update the mgs logs */
1162 mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1168 mop->mo_flags |= MO_FORCEFORMAT;
1171 strscpy(mop->mo_ldd.ldd_userdata, optarg,
1172 sizeof(mop->mo_ldd.ldd_userdata));
1178 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1183 fprintf(stderr, "Unknown option '%c'\n", opt);
1189 /* Last arg is device */
1190 if (optind != argc - 1) {
1192 fprintf(stderr, "Bad argument: %s\n", argv[optind]);
1199 int main(int argc, char *const argv[])
1201 struct mkfs_opts mop;
1202 struct lustre_disk_data *ldd;
1203 char *mountopts = NULL;
1204 char always_mountopts[512] = "";
1205 char default_mountopts[512] = "";
1208 if ((progname = strrchr(argv[0], '/')) != NULL)
1213 if ((argc < 2) || (argv[argc - 1][0] == '-')) {
1218 memset(&mop, 0, sizeof(mop));
1221 /* device is last arg */
1222 strscpy(mop.mo_device, argv[argc - 1], sizeof(mop.mo_device));
1224 /* Are we using a loop device? */
1225 ret = is_block(mop.mo_device);
1229 mop.mo_flags |= MO_IS_LOOP;
1232 /* For tunefs, we must read in the old values before parsing any
1235 /* Check whether the disk has already been formatted by mkfs.lustre */
1236 ret = is_lustre_target(&mop);
1239 fprintf(stderr, "Device %s has not been formatted with "
1240 "mkfs.lustre\n", mop.mo_device);
1245 ret = read_local_files(&mop);
1248 fprintf(stderr, "Failed to read previous Lustre data from %s "
1249 "(%d)\n", mop.mo_device, ret);
1252 if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
1253 mop.mo_mgs_failnodes++;
1256 print_ldd("Read previous values", &(mop.mo_ldd));
1259 ret = parse_opts(argc, argv, &mop, &mountopts);
1265 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1267 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
1272 if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
1274 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
1279 if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
1280 (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
1282 fprintf(stderr, "Can't find the target index, "
1283 "specify with --index\n");
1288 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1291 fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
1293 fprintf(stderr, "Must specify --mgsnode=\n");
1298 /* These are the permanent mount options (always included) */
1299 switch (ldd->ldd_mount_type) {
1301 case LDD_MT_LDISKFS:
1302 case LDD_MT_LDISKFS2: {
1303 sprintf(always_mountopts, "errors=remount-ro");
1304 if (IS_MDT(ldd) || IS_MGS(ldd))
1305 strscat(always_mountopts, ",iopen_nopriv,user_xattr",
1306 sizeof(always_mountopts));
1307 if ((get_os_version() == 24) && IS_OST(ldd))
1308 strscat(always_mountopts, ",asyncdel",
1309 sizeof(always_mountopts));
1310 /* NB: Files created while extents are enabled cannot be read
1311 if mounted with a kernel that doesn't include the CFS
1314 (ldd->ldd_mount_type == LDD_MT_LDISKFS ||
1315 ldd->ldd_mount_type == LDD_MT_LDISKFS2)) {
1316 strscat(default_mountopts, ",extents,mballoc",
1317 sizeof(default_mountopts));
1322 mop.mo_flags |= MO_IS_LOOP;
1323 sprintf(always_mountopts, "type=ext3,dev=%s",
1329 fprintf(stderr, "unknown fs type %d '%s'\n",
1330 ldd->ldd_mount_type,
1338 /* If user specifies mount opts, don't use defaults,
1339 but always use always_mountopts */
1340 sprintf(ldd->ldd_mount_opts, "%s,%s",
1341 always_mountopts, mountopts);
1344 if (ldd->ldd_mount_opts[0] == 0)
1345 /* use the defaults unless old opts exist */
1348 sprintf(ldd->ldd_mount_opts, "%s%s",
1349 always_mountopts, default_mountopts);
1353 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1354 ldd->ldd_fsname, ldd->ldd_svname);
1357 print_ldd("Permanent disk data", ldd);
1360 printf("exiting before disk write.\n");
1364 if (check_mtab_entry(mop.mo_device))
1367 /* Create the loopback file */
1368 if (mop.mo_flags & MO_IS_LOOP) {
1369 ret = access(mop.mo_device, F_OK);
1372 #ifndef TUNEFS /* mkfs.lustre */
1373 /* Reformat the loopback file */
1374 if (ret || (mop.mo_flags & MO_FORCEFORMAT))
1375 ret = loop_format(&mop);
1378 ret = loop_setup(&mop);
1381 fprintf(stderr, "Loop device setup for %s failed: %s\n",
1382 mop.mo_device, strerror(ret));
1387 #ifndef TUNEFS /* mkfs.lustre */
1388 /* Check whether the disk has already been formatted by mkfs.lustre */
1389 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1390 ret = is_lustre_target(&mop);
1393 fprintf(stderr, "Device %s was previously formatted "
1394 "for lustre. Use --reformat to reformat it, "
1395 "or tunefs.lustre to modify.\n",
1401 /* Format the backing filesystem */
1402 ret = make_lustre_backfs(&mop);
1405 fprintf(stderr, "mkfs failed %d\n", ret);
1410 /* Write our config files */
1411 ret = write_local_files(&mop);
1414 fprintf(stderr, "failed to write local files\n");
1421 /* Fix any crazy return values from system() */
1422 if (ret && ((ret & 255) == 0))
1425 verrprint("%s: exiting with %d (%s)\n",
1426 progname, ret, strerror(ret));