1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Lin Song Tao <lincent@clusterfs.com>
6 * Author: Nathan Rutman <nathan@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <sys/types.h>
35 #include <sys/mount.h>
40 #include <linux/types.h>
41 //#define HAVE_SYS_VFS_H 1
42 #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
53 static char *progname;
54 static int verbose = 1;
55 static int print_only = 0;
60 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
61 fprintf(out, "usage: %s <target types> [options] <device>\n", progname);
63 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
65 "\t\t--ost: object storage, mutually exclusive with mdt\n"
66 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
67 "\t\t--mgs: configuration management service - one per site\n"
68 "\toptions (in order of popularity):\n"
69 "\t\t--mgsnode=<nid>[,<...>] : NID(s) of a remote mgs node\n"
70 "\t\t\trequired for all targets other than the mgs node\n"
71 "\t\t--fsname=<filesystem_name> : default is 'lustre'\n"
72 "\t\t--failnode=<nid>[,<...>] : NID(s) of a failover partner\n"
73 "\t\t--param <key>=<value> : set a permanent parameter\n"
74 "\t\t--index=#N : target index\n"
75 /* FIXME implement 1.6.x
76 "\t\t--configdev=<altdevice|file>: store configuration info\n"
77 "\t\t\tfor this device on an alternate device\n"
79 "\t\t--mountfsoptions=<opts> : permanent mount options\n"
80 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
81 "\t\t--device-size=#N(KB) : device size for loop devices\n"
83 "\t\t--mkfsoptions=<opts> : format options\n"
84 "\t\t--reformat: overwrite an existing disk\n"
85 "\t\t--stripe-count-hint=#N : used for optimizing MDT inode size\n"
87 "\t\t--erase-params : erase all old parameter settings\n"
88 "\t\t--nomgs: turn off MGS service on this MDT\n"
89 "\t\t--writeconf: erase all config logs for this fs.\n"
91 "\t\t--print: just report what we would do; don't write to "
98 #define vprint if (verbose > 0) printf
100 static void fatal(void)
103 fprintf(stderr, "\n%s FATAL: ", progname);
106 /*================ utility functions =====================*/
109 dev_major (unsigned long long int __dev)
111 return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
115 dev_minor (unsigned long long int __dev)
117 return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
122 static int version = 0;
126 char release[4] = "";
128 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
130 fprintf(stderr, "%s: Warning: Can't resolve kernel "
131 "version, assuming 2.6\n", progname);
133 read(fd, release, 4);
136 if (strncmp(release, "2.4.", 4) == 0)
144 int run_command(char *cmd)
146 char log[] = "/tmp/mkfs_logXXXXXX";
150 printf("cmd: %s\n", cmd);
152 if ((fd = mkstemp(log)) >= 0) {
157 strcat(cmd, " 2>&1");
159 /* Can't use popen because we need the rv of the command */
164 fp = fopen(log, "r");
166 while (fgets(buf, sizeof(buf), fp) != NULL) {
167 if (rc || verbose > 2)
178 static int check_mtab_entry(char *spec, char *type)
183 fp = setmntent(MOUNTED, "r");
187 while ((mnt = getmntent(fp)) != NULL) {
188 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
189 strcmp(mnt->mnt_type, type) == 0) {
191 fprintf(stderr, "%s: according to %s %s is "
192 "already mounted on %s\n",
193 progname, MOUNTED, spec, mnt->mnt_dir);
202 /*============ disk dev functions ===================*/
204 /* Setup a file in the first unused loop_device */
205 int loop_setup(struct mkfs_opts *mop)
211 /* Figure out the loop device names */
212 if (!access("/dev/loop0", F_OK | R_OK))
213 strcpy(loop_base, "/dev/loop\0");
214 else if (!access("/dev/loop/0", F_OK | R_OK))
215 strcpy(loop_base, "/dev/loop/\0");
217 fprintf(stderr, "%s: can't access loop devices\n", progname);
221 /* Find unused loop device */
222 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
224 sprintf(l_device, "%s%d", loop_base, i);
225 if (access(l_device, F_OK | R_OK))
227 sprintf(cmd, "losetup %s > /dev/null 2>&1", l_device);
229 /* losetup gets 1 (ret=256) for non-set-up device */
231 /* Set up a loopback device to our file */
232 sprintf(cmd, "losetup %s %s", l_device, mop->mo_device);
233 ret = run_command(cmd);
235 fprintf(stderr, "%s: error %d on losetup: %s\n",
236 progname, ret, strerror(ret));
239 strcpy(mop->mo_loopdev, l_device);
244 fprintf(stderr, "%s: out of loop devices!\n", progname);
248 int loop_cleanup(struct mkfs_opts *mop)
252 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
253 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
254 ret = run_command(cmd);
259 /* Determine if a device is a block device (as opposed to a file) */
260 int is_block(char* devname)
265 ret = access(devname, F_OK);
268 ret = stat(devname, &st);
270 fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
273 return S_ISBLK(st.st_mode);
276 __u64 get_device_size(char* device)
281 fd = open(device, O_RDONLY);
283 fprintf(stderr, "%s: cannot open %s: %s\n",
284 progname, device, strerror(errno));
288 /* size in bytes. bz5831 */
289 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
292 fprintf(stderr, "%s: size ioctl failed: %s\n",
293 progname, strerror(errno));
297 vprint("device size = "LPU64"MB\n", size >> 20);
298 /* return value in KB */
302 int loop_format(struct mkfs_opts *mop)
306 if (mop->mo_device_sz == 0) {
308 fprintf(stderr, "loop device requires a --device-size= "
313 ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
314 ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
317 fprintf(stderr, "%s: Unable to create backing store: %d\n",
324 /* Check whether the file exists in the device */
325 static int file_in_dev(char *file_name, char *dev_name)
328 char debugfs_cmd[256];
329 unsigned int inode_num;
332 /* Construct debugfs command line. */
333 memset(debugfs_cmd, 0, sizeof(debugfs_cmd));
335 "debugfs -c -R 'stat %s' %s 2>&1 | egrep '(Inode|unsupported)'",
336 file_name, dev_name);
338 fp = popen(debugfs_cmd, "r");
340 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
344 if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
348 i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
350 /* Filesystem has unsupported feature */
351 vprint("%.*s", i, debugfs_cmd);
352 /* in all likelihood, the "unsupported feature" is
353 'extents', which older debugfs does not understand.
354 Use e2fsprogs-1.38-cfs1 or later, available from
355 ftp://ftp.lustre.org/pub/lustre/other/e2fsprogs/ */
362 /* Check whether the device has already been used with lustre */
363 static int is_lustre_target(struct mkfs_opts *mop)
366 vprint("checking for existing Lustre data\n");
368 if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))
369 || (rc = file_in_dev(LAST_RCVD, mop->mo_device))) {
370 vprint("found Lustre data\n");
371 /* in the -1 case, 'extents' means this really IS a lustre
376 return 0; /* The device is not a lustre target. */
379 /* Build fs according to type */
380 int make_lustre_backfs(struct mkfs_opts *mop)
388 if (mop->mo_device_sz != 0) {
389 if (mop->mo_device_sz < 8096){
390 fprintf(stderr, "%s: size of filesystem must be larger "
391 "than 8MB, but is set to %lldKB\n",
392 progname, mop->mo_device_sz);
395 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
398 if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
399 (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS)) {
400 __u64 device_sz = mop->mo_device_sz;
402 /* we really need the size */
403 if (device_sz == 0) {
404 device_sz = get_device_size(mop->mo_device);
409 /* Journal size in MB */
410 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
411 /* Choose our own default journal size */
412 long journal_sz = 0, max_sz;
413 if (device_sz > 1024 * 1024) /* 1GB */
414 journal_sz = (device_sz / 102400) * 4;
416 max_sz = (102400 * L_BLOCK_SIZE) >> 20; /* 400MB */
417 if (journal_sz > max_sz)
420 sprintf(buf, " -J size=%ld", journal_sz);
421 strcat(mop->mo_mkfsopts, buf);
425 /* Default bytes_per_inode is block size */
426 if (strstr(mop->mo_mkfsopts, "-i") == NULL) {
427 long bytes_per_inode = 0;
429 if (IS_MDT(&mop->mo_ldd))
430 bytes_per_inode = 4096;
432 /* Allocate fewer inodes on large OST devices. Most
433 filesystems can be much more aggressive than even
435 if ((IS_OST(&mop->mo_ldd) && (device_sz > 1000000)))
436 bytes_per_inode = 16384;
438 if (bytes_per_inode > 0) {
439 sprintf(buf, " -i %ld", bytes_per_inode);
440 strcat(mop->mo_mkfsopts, buf);
444 /* This is an undocumented mke2fs option. Default is 128. */
445 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
447 if (IS_MDT(&mop->mo_ldd)) {
448 if (mop->mo_stripe_count > 77)
449 inode_size = 512; /* bz 7241 */
450 /* cray stripes across all osts (>60) */
451 else if (mop->mo_stripe_count > 34)
453 else if (mop->mo_stripe_count > 13)
457 } else if (IS_OST(&mop->mo_ldd)) {
458 /* now as we store fids in EA on OST we need
459 to make inode bigger */
463 if (inode_size > 0) {
464 sprintf(buf, " -I %ld", inode_size);
465 strcat(mop->mo_mkfsopts, buf);
471 strcat(mop->mo_mkfsopts, " -q");
474 /* Enable hashed b-tree directory lookup in large dirs bz6224 */
475 if (strstr(mop->mo_mkfsopts, "-O") == NULL) {
476 strcat(mop->mo_mkfsopts, " -O dir_index");
479 /* Allow reformat of full devices (as opposed to
480 partitions.) We already checked for mounted dev. */
481 strcat(mop->mo_mkfsopts, " -F");
483 sprintf(mkfs_cmd, "mkfs.ext2 -j -b %d -L %s ", L_BLOCK_SIZE,
484 mop->mo_ldd.ldd_svname);
486 } else if (mop->mo_ldd.ldd_mount_type == LDD_MT_REISERFS) {
487 long journal_sz = 0; /* FIXME default journal size */
488 if (journal_sz > 0) {
489 sprintf(buf, " --journal_size %ld", journal_sz);
490 strcat(mop->mo_mkfsopts, buf);
492 sprintf(mkfs_cmd, "mkreiserfs -ff ");
495 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
496 progname, mop->mo_ldd.ldd_mount_type,
497 MT_STR(&mop->mo_ldd));
501 /* For loop device format the dev, not the filename */
502 dev = mop->mo_device;
503 if (mop->mo_flags & MO_IS_LOOP)
504 dev = mop->mo_loopdev;
506 vprint("formatting backing filesystem %s on %s\n",
507 MT_STR(&mop->mo_ldd), dev);
508 vprint("\ttarget name %s\n", mop->mo_ldd.ldd_svname);
509 vprint("\t4k blocks %d\n", block_count);
510 vprint("\toptions %s\n", mop->mo_mkfsopts);
512 /* mkfs_cmd's trailing space is important! */
513 strcat(mkfs_cmd, mop->mo_mkfsopts);
514 strcat(mkfs_cmd, " ");
515 strcat(mkfs_cmd, dev);
516 if (block_count != 0) {
517 sprintf(buf, " %d", block_count);
518 strcat(mkfs_cmd, buf);
521 vprint("mkfs_cmd = %s\n", mkfs_cmd);
522 ret = run_command(mkfs_cmd);
525 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
533 /* ==================== Lustre config functions =============*/
535 void print_ldd(char *str, struct lustre_disk_data *ldd)
537 printf("\n %s:\n", str);
538 printf("Target: %s\n", ldd->ldd_svname);
539 if (ldd->ldd_svindex == INDEX_UNASSIGNED)
540 printf("Index: unassigned\n");
542 printf("Index: %d\n", ldd->ldd_svindex);
543 printf("UUID: %s\n", (char *)ldd->ldd_uuid);
544 printf("Lustre FS: %s\n", ldd->ldd_fsname);
545 printf("Mount type: %s\n", MT_STR(ldd));
546 printf("Flags: %#x\n", ldd->ldd_flags);
547 printf(" (%s%s%s%s%s%s%s%s)\n",
548 IS_MDT(ldd) ? "MDT ":"",
549 IS_OST(ldd) ? "OST ":"",
550 IS_MGS(ldd) ? "MGS ":"",
551 ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
552 ldd->ldd_flags & LDD_F_VIRGIN ? "first_time ":"",
553 ldd->ldd_flags & LDD_F_UPDATE ? "update ":"",
554 ldd->ldd_flags & LDD_F_WRITECONF ? "writeconf ":"",
555 ldd->ldd_flags & LDD_F_UPGRADE14 ? "upgrade1.4 ":"");
556 printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
557 printf("Parameters:%s\n", ldd->ldd_params);
561 /* Write the server config files */
562 int write_local_files(struct mkfs_opts *mop)
564 char mntpt[] = "/tmp/mntXXXXXX";
570 /* Mount this device temporarily in order to write these files */
571 if (!mkdtemp(mntpt)) {
572 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
573 progname, mntpt, strerror(errno));
577 dev = mop->mo_device;
578 if (mop->mo_flags & MO_IS_LOOP)
579 dev = mop->mo_loopdev;
581 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
583 fprintf(stderr, "%s: Unable to mount %s: %s\n",
584 progname, dev, strerror(errno));
585 if (errno == ENODEV) {
586 fprintf(stderr, "Is the %s module available?\n",
587 MT_STR(&mop->mo_ldd));
592 /* Set up initial directories */
593 sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
594 ret = mkdir(filepnm, 0777);
595 if ((ret != 0) && (errno != EEXIST)) {
596 fprintf(stderr, "%s: Can't make configs dir %s (%d)\n",
597 progname, filepnm, ret);
599 } else if (errno == EEXIST) {
603 sprintf(filepnm, "%s/%s", mntpt, "ROOT");
604 ret = mkdir(filepnm, 0777);
605 if ((ret != 0) && (errno != EEXIST)) {
606 fprintf(stderr, "%s: Can't make ROOT dir %s (%d)\n",
607 progname, filepnm, ret);
609 } else if (errno == EEXIST) {
613 /* Save the persistent mount data into a file. Lustre must pre-read
614 this file to get the real mount options. */
615 vprint("Writing %s\n", MOUNT_DATA_FILE);
616 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
617 filep = fopen(filepnm, "w");
619 fprintf(stderr, "%s: Unable to create %s file\n",
623 fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
628 /* Check for upgrade */
629 if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS))
630 == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
633 vprint("Copying old logs\n");
635 /* Generate new client log as servers upgrade. Starting a new client
636 may end up with short lov's, so will be degraded until all servers
638 /* Copy the old client log to fsname-client */
639 sprintf(filepnm, "%s/%s/%s-client",
640 mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
641 sprintf(cmd, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
644 printf("cmd: %s\n", cmd);
645 ret = run_command(cmd);
647 fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
648 "(%d)\n", progname, MDT_LOGS_DIR, ret);
649 fprintf(stderr, "mount -t ext3 %s somewhere, "
650 "find the client log for fs %s and "
651 "copy it manually into %s/%s-client, "
654 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
655 mop->mo_ldd.ldd_fsname);
659 /* We need to use the old mdt log because otherwise mdt won't
660 have complete lov if old clients connect before all
662 /* Copy the old mdt log to fsname-MDT0000 (get old
663 name from mdt_UUID) */
665 strcpy(filepnm, mop->mo_ldd.ldd_uuid);
666 term = strstr(filepnm, "_UUID");
669 sprintf(cmd, "cp %s/%s/%s %s/%s/%s",
670 mntpt, MDT_LOGS_DIR, filepnm,
671 mntpt, MOUNT_CONFIGS_DIR,
672 mop->mo_ldd.ldd_svname);
674 printf("cmd: %s\n", cmd);
675 ret = run_command(cmd);
678 fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
679 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
680 fprintf(stderr, "mount -t ext3 %s somewhere, "
681 "find the MDT log for fs %s and "
682 "copy it manually into %s/%s, "
685 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
686 mop->mo_ldd.ldd_svname);
701 int read_local_files(struct mkfs_opts *mop)
703 char mntpt[] = "/tmp/mntXXXXXX";
709 /* Mount this device temporarily in order to read these files */
710 if (!mkdtemp(mntpt)) {
711 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
712 progname, mntpt, strerror(errno));
716 dev = mop->mo_device;
717 if (mop->mo_flags & MO_IS_LOOP)
718 dev = mop->mo_loopdev;
720 ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
722 fprintf(stderr, "%s: Unable to mount %s: %s\n",
723 progname, dev, strerror(errno));
727 sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
728 filep = fopen(filepnm, "r");
730 vprint("Reading %s\n", MOUNT_DATA_FILE);
731 fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
734 /* Try to read pre-1.6 config from last_rcvd */
735 struct lr_server_data lsd;
736 vprint("%s: Unable to read %s, trying last_rcvd\n",
737 progname, MOUNT_DATA_FILE);
738 sprintf(filepnm, "%s/%s", mntpt, LAST_RCVD);
739 filep = fopen(filepnm, "r");
741 fprintf(stderr, "%s: Unable to read old data\n",
746 vprint("Reading %s\n", LAST_RCVD);
747 ret = fread(&lsd, 1, sizeof(lsd), filep);
748 if (ret < sizeof(lsd)) {
749 fprintf(stderr, "%s: Short read (%d of %d)\n",
750 progname, ret, sizeof(lsd));
751 ret = -ferror(filep);
756 if (lsd.lsd_feature_compat & OBD_COMPAT_OST) {
757 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
758 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
759 } else if (lsd.lsd_feature_compat & OBD_COMPAT_MDT) {
760 /* We must co-locate so mgs can see old logs.
761 If user doesn't want this, they can copy the old
762 logs manually and re-tunefs. */
763 mop->mo_ldd.ldd_flags =
764 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
765 mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
767 /* If neither is set, we're pre-1.4.6, make a guess. */
768 sprintf(filepnm, "%s/%s", mntpt, MDT_LOGS_DIR);
769 if (lsd.lsd_ost_index > 0) {
770 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
771 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
773 /* If there's a LOGS dir, it's an MDT */
774 if ((ret = access(filepnm, F_OK)) == 0) {
775 mop->mo_ldd.ldd_flags =
778 /* Old MDT's are always index 0
780 mop->mo_ldd.ldd_svindex = 0;
782 /* The index won't be correct */
783 mop->mo_ldd.ldd_flags =
784 LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
789 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid,
790 sizeof(mop->mo_ldd.ldd_uuid));
791 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
805 void set_defaults(struct mkfs_opts *mop)
807 mop->mo_ldd.ldd_magic = LDD_MAGIC;
808 mop->mo_ldd.ldd_config_ver = 1;
809 mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
810 mop->mo_mgs_failnodes = 0;
811 strcpy(mop->mo_ldd.ldd_fsname, "lustre");
812 if (get_os_version() == 24)
813 mop->mo_ldd.ldd_mount_type = LDD_MT_EXT3;
815 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
817 mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
818 mop->mo_stripe_count = 1;
821 static inline void badopt(const char *opt, char *type)
823 fprintf(stderr, "%s: '--%s' only valid for %s\n",
824 progname, opt, type);
828 static int add_param(char *buf, char *key, char *val)
830 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
831 int start = strlen(buf);
835 keylen = strlen(key);
836 if (start + 1 + keylen + strlen(val) >= end) {
837 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
838 progname, buf, key ? key : "", val);
842 sprintf(buf + start, " %s%s", key ? key : "", val);
846 /* from mount_lustre */
847 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
848 #define MAXNIDSTR 1024
849 static char *convert_hostnames(char *s1)
851 char *converted, *s2 = 0, *c;
852 int left = MAXNIDSTR;
855 converted = malloc(left);
857 while ((left > 0) && ((s2 = strsep(&s1, ",: \0")))) {
858 nid = libcfs_str2nid(s2);
859 if (nid == LNET_NID_ANY) {
863 fprintf(stderr, "%s: Can't parse NID '%s'\n",
868 if (LNET_NETTYP(LNET_NIDNET(nid)) == SOCKLND) {
869 __u32 addr = LNET_NIDADDR(nid);
870 c += snprintf(c, left, "%u.%u.%u.%u@%s%u,",
871 (addr >> 24) & 0xff, (addr >> 16) & 0xff,
872 (addr >> 8) & 0xff, addr & 0xff,
873 libcfs_lnd2str(SOCKLND),
874 LNET_NETNUM(LNET_NIDNET(nid)));
876 c += snprintf(c, left, "%s,", s2);
878 left = converted + MAXNIDSTR - c;
884 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
887 static struct option long_opt[] = {
888 {"backfstype", 1, 0, 'b'},
889 {"stripe-count-hint", 1, 0, 'c'},
890 {"configdev", 1, 0, 'C'},
891 {"device-size", 1, 0, 'd'},
892 {"erase-params", 0, 0, 'e'},
893 {"failnode", 1, 0, 'f'},
894 {"failover", 1, 0, 'f'},
897 {"index", 1, 0, 'i'},
898 {"mkfsoptions", 1, 0, 'k'},
899 {"mgsnode", 1, 0, 'm'},
900 {"mgsnid", 1, 0, 'm'},
902 {"fsname",1, 0, 'n'},
903 {"nomgs", 0, 0, 'N'},
904 {"mountfsoptions", 1, 0, 'o'},
906 {"param", 1, 0, 'p'},
907 {"print", 0, 0, 'P'},
908 {"quiet", 0, 0, 'q'},
909 {"reformat", 0, 0, 'r'},
910 {"verbose", 0, 0, 'v'},
911 {"writeconf", 0, 0, 'w'},
914 char *optstring = "b:c:C:d:ef:Ghi:k:m:Mn:No:Op:Pqrvw";
918 while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) !=
923 while (i < LDD_MT_LAST) {
924 if (strcmp(optarg, mt_str(i)) == 0) {
925 mop->mo_ldd.ldd_mount_type = i;
933 if (IS_MDT(&mop->mo_ldd)) {
934 int stripe_count = atol(optarg);
935 if (stripe_count <= 0) {
936 fprintf(stderr, "%s: bad stripe count "
937 "%d\n", progname, stripe_count);
940 mop->mo_stripe_count = stripe_count;
942 badopt(long_opt[longidx].name, "MDT");
946 case 'C': /* Configdev */
948 printf("Configdev not implemented\n");
951 mop->mo_device_sz = atol(optarg);
954 mop->mo_ldd.ldd_params[0] = '\0';
957 char *nids = convert_hostnames(optarg);
960 rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE,
968 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
974 if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
975 mop->mo_ldd.ldd_svindex = atol(optarg);
976 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
978 badopt(long_opt[longidx].name, "MDT,OST");
983 strncpy(mop->mo_mkfsopts, optarg,
984 sizeof(mop->mo_mkfsopts) - 1);
987 char *nids = convert_hostnames(optarg);
990 rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE,
995 mop->mo_mgs_failnodes++;
999 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1002 if (!(IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd))) {
1003 badopt(long_opt[longidx].name, "MDT,OST");
1006 if (strlen(optarg) > 8) {
1007 fprintf(stderr, "%s: filesystem name must be "
1008 "<= 8 chars\n", progname);
1012 strncpy(mop->mo_ldd.ldd_fsname, optarg,
1013 sizeof(mop->mo_ldd.ldd_fsname) - 1);
1016 mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1019 *mountopts = optarg;
1022 mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1025 rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1036 mop->mo_flags |= MO_FORCEFORMAT;
1042 mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1047 fprintf(stderr, "Unknown option '%c'\n", opt);
1053 if (optind >= argc) {
1055 fprintf(stderr, "Bad arguments\n");
1063 int main(int argc, char *const argv[])
1065 struct mkfs_opts mop;
1066 struct lustre_disk_data *ldd;
1067 char *mountopts = NULL;
1068 char always_mountopts[512] = "";
1069 char default_mountopts[512] = "";
1072 //printf("pad %d\n", offsetof(struct lustre_disk_data, ldd_padding));
1073 assert(offsetof(struct lustre_disk_data, ldd_padding) == 200);
1075 if ((progname = strrchr(argv[0], '/')) != NULL)
1086 memset(&mop, 0, sizeof(mop));
1089 /* device is last arg */
1090 strcpy(mop.mo_device, argv[argc - 1]);
1092 if (check_mtab_entry(mop.mo_device, "lustre"))
1095 /* Are we using a loop device? */
1096 ret = is_block(mop.mo_device);
1100 mop.mo_flags |= MO_IS_LOOP;
1103 /* For tunefs, we must read in the old values before parsing any
1105 /* Create the loopback file */
1106 if (mop.mo_flags & MO_IS_LOOP) {
1107 ret = access(mop.mo_device, F_OK);
1109 ret = loop_setup(&mop);
1112 fprintf(stderr, "Loop device setup for %s failed: %s\n",
1113 mop.mo_device, strerror(ret));
1118 /* Check whether the disk has already been formatted by mkfs.lustre */
1119 ret = is_lustre_target(&mop);
1122 fprintf(stderr, "Device %s has not been formatted with "
1123 "mkfs.lustre\n", mop.mo_device);
1127 ret = read_local_files(&mop);
1130 fprintf(stderr, "Failed to read previous Lustre data from %s\n",
1136 print_ldd("Read previous values", &(mop.mo_ldd));
1139 ret = parse_opts(argc, argv, &mop, &mountopts);
1144 if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1146 fprintf(stderr, "must set target type :{mdt,ost,mgs}\n");
1152 if (IS_MDT(ldd) && !IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1153 vprint("No management node specified, adding MGS to this "
1155 ldd->ldd_flags |= LDD_F_SV_TYPE_MGS;
1158 if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1160 fprintf(stderr, "Must specify either --mgs or --mgsnode\n");
1165 /* These are the permanent mount options (always included) */
1166 switch (ldd->ldd_mount_type) {
1168 case LDD_MT_LDISKFS: {
1169 sprintf(always_mountopts, "errors=remount-ro");
1170 if (IS_MDT(ldd) || IS_MGS(ldd))
1171 strcat(always_mountopts,
1172 ",iopen_nopriv,user_xattr");
1173 if ((get_os_version() == 24) && IS_OST(ldd))
1174 strcat(always_mountopts, ",asyncdel");
1176 /* Files created while extents are enabled cannot be read if
1177 mounted with a kernel that doesn't include the CFS patches.*/
1179 ldd->ldd_mount_type == LDD_MT_LDISKFS) {
1180 strcat(default_mountopts, ",extents,mballoc");
1186 mop.mo_flags |= MO_IS_LOOP;
1187 sprintf(always_mountopts, "type=ext3,dev=%s",
1193 fprintf(stderr, "unknown fs type %d '%s'\n",
1194 ldd->ldd_mount_type,
1202 /* If user specifies mount opts, don't use defaults,
1203 but always use always_mountopts */
1204 sprintf(ldd->ldd_mount_opts, "%s,%s",
1205 always_mountopts, mountopts);
1208 if (ldd->ldd_mount_opts[0] == 0)
1209 /* use the defaults unless old opts exist */
1212 if (default_mountopts[0])
1213 sprintf(ldd->ldd_mount_opts, "%s,%s",
1214 always_mountopts, default_mountopts);
1216 strcpy(ldd->ldd_mount_opts,
1221 server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1222 ldd->ldd_fsname, ldd->ldd_svname);
1225 print_ldd("Permanent disk data", ldd);
1228 printf("exiting before disk write.\n");
1232 #ifndef TUNEFS /* mkfs.lustre */
1233 /* Create the loopback file of the correct size */
1234 if (mop.mo_flags & MO_IS_LOOP) {
1235 ret = access(mop.mo_device, F_OK);
1236 /* Don't destroy the loopback file if no FORCEFORMAT */
1237 if (ret || (mop.mo_flags & MO_FORCEFORMAT))
1238 ret = loop_format(&mop);
1240 ret = loop_setup(&mop);
1243 fprintf(stderr, "Loop device setup failed: %s\n",
1249 /* Check whether the disk has already been formatted by mkfs.lustre */
1250 if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1251 ret = is_lustre_target(&mop);
1254 fprintf(stderr, "Device %s was previously formatted "
1255 "for lustre. Use --reformat to reformat it, "
1256 "or tunefs.lustre to modify.\n",
1262 /* Format the backing filesystem */
1263 ret = make_lustre_backfs(&mop);
1266 fprintf(stderr, "mkfs failed %d\n", ret);
1271 ret = write_local_files(&mop);
1274 fprintf(stderr, "failed to write local files\n");