Whamcloud - gitweb
catch extra args without dashes
[fs/lustre-release.git] / lustre / utils / mkfs_lustre.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *   Copyright (C) 2006 Cluster File Systems, Inc.
5  *   Author: Nathan Rutman <nathan@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
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.
12  *
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.
17  *
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.
21  *
22  */
23  /* This source file is compiled into both mkfs.lustre and tunefs.lustre */
24
25 #define _GNU_SOURCE
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <stdarg.h>
31 #include <mntent.h>
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/mount.h>
36
37 #include <string.h>
38 #include <getopt.h>
39 #include <limits.h>
40
41 #ifdef __linux__
42 # include <linux/fs.h> /* for BLKGETSIZE64 */
43 #endif
44 #include <lustre_disk.h>
45 #include <lustre_param.h>
46 #include <lnet/lnetctl.h>
47 #include <lustre_ver.h>
48
49 #ifndef PATH_MAX
50 #define PATH_MAX 4096
51 #endif
52
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
58
59 /* used to describe the options to format the lustre disk, not persistent */
60 struct mkfs_opts {
61         struct lustre_disk_data mo_ldd; /* to be written in MOUNT_DATA_FILE */
62         char  mo_mount_type_string[20]; /* "ext3", "ldiskfs", ... */
63         char  mo_device[128];           /* disk device name */
64         char  mo_mkfsopts[128];         /* options to the backing-store mkfs */
65         char  mo_loopdev[128];          /* in case a loop dev is needed */
66         __u64 mo_device_sz;             /* in KB */
67         int   mo_stripe_count;
68         int   mo_flags; 
69         int   mo_mgs_failnodes;
70 };
71
72 static char *progname;
73 static int verbose = 1;
74 static int print_only = 0;
75
76
77 void usage(FILE *out)
78 {
79         fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
80         fprintf(out, "usage: %s <target types> [options] <device>\n", progname);
81         fprintf(out, 
82                 "\t<device>:block device or file (e.g /dev/sda or /tmp/ost1)\n"
83                 "\ttarget types:\n"
84                 "\t\t--ost: object storage, mutually exclusive with mdt,mgs\n"
85                 "\t\t--mdt: metadata storage, mutually exclusive with ost\n"
86                 "\t\t--mgs: configuration management service - one per site\n"
87                 "\toptions (in order of popularity):\n"
88                 "\t\t--mgsnode=<nid>[,<...>] : NID(s) of a remote mgs node\n"
89                 "\t\t\trequired for all targets other than the mgs node\n"
90                 "\t\t--fsname=<filesystem_name> : default is 'lustre'\n"
91                 "\t\t--failnode=<nid>[,<...>] : NID(s) of a failover partner\n"
92                 "\t\t--param <key>=<value> : set a permanent parameter\n"
93                 "\t\t\te.g. --param sys.timeout=40\n"
94                 "\t\t\t     --param lov.stripesize=2M\n"
95                 "\t\t--index=#N : target index (i.e. ost index within the lov)\n"
96                 /* FIXME implement 1.6.x
97                 "\t\t--configdev=<altdevice|file>: store configuration info\n"
98                 "\t\t\tfor this device on an alternate device\n"
99                 */
100                 "\t\t--comment=<user comment>: arbitrary user string (%d bytes)\n"
101                 "\t\t--mountfsoptions=<opts> : permanent mount options\n"
102 #ifndef TUNEFS
103                 "\t\t--backfstype=<fstype> : backing fs type (ext3, ldiskfs)\n"
104                 "\t\t--device-size=#N(KB) : device size for loop devices\n"
105                 "\t\t--mkfsoptions=<opts> : format options\n"
106                 "\t\t--reformat: overwrite an existing disk\n"
107                 "\t\t--stripe-count-hint=#N : used for optimizing MDT inode size\n"
108 #else
109                 "\t\t--erase-params : erase all old parameter settings\n"
110                 "\t\t--nomgs: turn off MGS service on this MDT\n"
111                 "\t\t--writeconf: erase all config logs for this fs.\n"
112 #endif
113                 "\t\t--dryrun: just report what we would do; "
114                 "don't write to disk\n"
115                 "\t\t--verbose : e.g. show mkfs progress\n"
116                 "\t\t--quiet\n",
117                 (int)sizeof(((struct lustre_disk_data *)0)->ldd_userdata));
118         return;
119 }
120
121 #define vprint if (verbose > 0) printf
122 #define verrprint if (verbose >= 0) printf
123
124 static void fatal(void)
125 {
126         verbose = 0;
127         fprintf(stderr, "\n%s FATAL: ", progname);
128 }
129
130 /*================ utility functions =====================*/
131
132 inline unsigned int 
133 dev_major (unsigned long long int __dev)
134 {
135         return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
136 }
137
138 inline unsigned int
139 dev_minor (unsigned long long int __dev)
140 {
141         return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
142 }
143
144 int get_os_version()
145 {
146         static int version = 0;
147
148         if (!version) {
149                 int fd;
150                 char release[4] = "";
151
152                 fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
153                 if (fd < 0) 
154                         fprintf(stderr, "%s: Warning: Can't resolve kernel "
155                                 "version, assuming 2.6\n", progname);
156                 else {
157                         read(fd, release, 4);
158                         close(fd);
159                 }
160                 if (strncmp(release, "2.4.", 4) == 0) 
161                         version = 24;
162                 else 
163                         version = 26;
164         }
165         return version;
166 }
167
168 int run_command(char *cmd, int cmdsz)
169 {
170         char log[] = "/tmp/mkfs_logXXXXXX";
171         int fd = -1, rc;
172         
173         if ((cmdsz - strlen(cmd)) < 6) {
174                 fatal();
175                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
176                         cmdsz, cmd);
177                 return ENOMEM;
178         }
179
180         if (verbose > 1) {
181                 printf("cmd: %s\n", cmd);
182         } else {
183                 if ((fd = mkstemp(log)) >= 0) {
184                         close(fd);
185                         strcat(cmd, " >");
186                         strcat(cmd, log);
187                 }
188         }
189         strcat(cmd, " 2>&1");
190
191         /* Can't use popen because we need the rv of the command */
192         rc = system(cmd);
193         if (rc && (fd >= 0)) {
194                 char buf[128];
195                 FILE *fp;
196                 fp = fopen(log, "r");
197                 if (fp) {
198                         while (fgets(buf, sizeof(buf), fp) != NULL) {
199                                 printf("   %s", buf);
200                         }
201                         fclose(fp);
202                 }
203         }
204         if (fd >= 0) 
205                 remove(log);
206         return rc;
207 }                                                       
208
209 static int check_mtab_entry(char *spec)
210 {
211         FILE *fp;
212         struct mntent *mnt;
213
214         fp = setmntent(MOUNTED, "r");
215         if (fp == NULL)
216                 return(0);
217
218         while ((mnt = getmntent(fp)) != NULL) {
219                 if (strcmp(mnt->mnt_fsname, spec) == 0) {
220                         endmntent(fp);
221                         fprintf(stderr, "%s: according to %s %s is "
222                                 "already mounted on %s\n",
223                                 progname, MOUNTED, spec, mnt->mnt_dir);
224                         return(EEXIST);
225                 }
226         }
227         endmntent(fp);
228
229         return(0);
230 }
231
232 /*============ disk dev functions ===================*/
233
234 /* Setup a file in the first unused loop_device */
235 int loop_setup(struct mkfs_opts *mop)
236 {
237         char loop_base[20];
238         char l_device[64];
239         int i,ret = 0;
240
241         /* Figure out the loop device names */
242         if (!access("/dev/loop0", F_OK | R_OK))
243                 strcpy(loop_base, "/dev/loop\0");
244         else if (!access("/dev/loop/0", F_OK | R_OK))
245                 strcpy(loop_base, "/dev/loop/\0");
246         else {
247                 fprintf(stderr, "%s: can't access loop devices\n", progname);
248                 return EACCES;
249         }
250
251         /* Find unused loop device */
252         for (i = 0; i < MAX_LOOP_DEVICES; i++) {
253                 char cmd[PATH_MAX];
254                 int cmdsz = sizeof(cmd);
255                 sprintf(l_device, "%s%d", loop_base, i);
256                 if (access(l_device, F_OK | R_OK)) 
257                         break;
258                 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
259                 ret = system(cmd);
260                 
261                 /* losetup gets 1 (ret=256) for non-set-up device */
262                 if (ret) {
263                         /* Set up a loopback device to our file */
264                         snprintf(cmd, cmdsz, "losetup %s %s", l_device,
265                                  mop->mo_device);
266                         ret = run_command(cmd, cmdsz);
267                         if (ret) {
268                                 fprintf(stderr, "%s: error %d on losetup: %s\n",
269                                         progname, ret, strerror(ret));
270                                 return ret;
271                         }
272                         strcpy(mop->mo_loopdev, l_device);
273                         return ret;
274                 }
275         }
276         
277         fprintf(stderr, "%s: out of loop devices!\n", progname);
278         return EMFILE;
279 }       
280
281 int loop_cleanup(struct mkfs_opts *mop)
282 {
283         char cmd[128];
284         int ret = 1;
285         if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
286                 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
287                 ret = run_command(cmd, sizeof(cmd));
288         }
289         return ret;
290 }
291
292 /* Determine if a device is a block device (as opposed to a file) */
293 int is_block(char* devname)
294 {
295         struct stat st;
296         int ret = 0;
297
298         ret = access(devname, F_OK);
299         if (ret != 0) 
300                 return 0;
301         ret = stat(devname, &st);
302         if (ret != 0) {
303                 fprintf(stderr, "%s: cannot stat %s\n", progname, devname);
304                 return -1;
305         }
306         return S_ISBLK(st.st_mode);
307 }
308
309 __u64 get_device_size(char* device) 
310 {
311         int ret, fd;
312         __u64 size = 0;
313
314         fd = open(device, O_RDONLY);
315         if (fd < 0) {
316                 fprintf(stderr, "%s: cannot open %s: %s\n", 
317                         progname, device, strerror(errno));
318                 return 0;
319         }
320
321 #ifdef BLKGETSIZE64
322         /* size in bytes. bz5831 */
323         ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
324 #else
325         {
326                 __u32 lsize = 0;
327                 /* size in blocks */
328                 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
329                 size = (__u64)lsize * 512; 
330         }
331 #endif
332         close(fd);
333         if (ret < 0) {
334                 fprintf(stderr, "%s: size ioctl failed: %s\n", 
335                         progname, strerror(errno));
336                 return 0;
337         }
338         
339         vprint("device size = "LPU64"MB\n", size >> 20);
340         /* return value in KB */
341         return size >> 10;
342 }
343
344 int loop_format(struct mkfs_opts *mop)
345 {
346         int ret = 0;
347        
348         if (mop->mo_device_sz == 0) {
349                 fatal();
350                 fprintf(stderr, "loop device requires a --device-size= "
351                         "param\n");
352                 return EINVAL;
353         }
354
355         ret = creat(mop->mo_device, S_IRUSR|S_IWUSR);
356         ret = truncate(mop->mo_device, mop->mo_device_sz * 1024);
357         if (ret != 0) {
358                 ret = errno;
359                 fprintf(stderr, "%s: Unable to create backing store: %d\n", 
360                         progname, ret);
361         }
362
363         return ret;
364 }
365
366 /* Check whether the file exists in the device */
367 static int file_in_dev(char *file_name, char *dev_name)
368 {
369         FILE *fp;
370         char debugfs_cmd[256];
371         unsigned int inode_num;
372         int i;
373
374         /* Construct debugfs command line. */
375         snprintf(debugfs_cmd, sizeof(debugfs_cmd), 
376                 "debugfs -c -R 'stat %s' %s 2>&1 | egrep '(Inode|unsupported)'",
377                 file_name, dev_name);
378
379         fp = popen(debugfs_cmd, "r");
380         if (!fp) {
381                 fprintf(stderr, "%s: %s\n", progname, strerror(errno));
382                 return 0;
383         }
384
385         if (fscanf(fp, "Inode: %u", &inode_num) == 1) { /* exist */
386                 pclose(fp);
387                 return 1;
388         }
389         i = fread(debugfs_cmd, 1, sizeof(debugfs_cmd), fp);
390         if (i) {
391                 debugfs_cmd[i] = 0;
392                 fprintf(stderr, "%s", debugfs_cmd);
393                 if (strstr(debugfs_cmd, "unsupported feature")) {
394                         fprintf(stderr, "In all likelihood, the "
395                                 "'unsupported feature' is 'extents', which "
396                                 "older debugfs does not understand.\n"  
397                                 "Use e2fsprogs-1.38-cfs1 or later, available "
398                                 "from ftp://ftp.lustre.org/pub/lustre/other/"
399                                 "e2fsprogs/\n");
400                 }
401                 return -1;
402         }
403         pclose(fp);
404         return 0;
405 }
406
407 /* Check whether the device has already been used with lustre */
408 static int is_lustre_target(struct mkfs_opts *mop)
409 {
410         int rc;
411         vprint("checking for existing Lustre data: ");
412         
413         if ((rc = file_in_dev(MOUNT_DATA_FILE, mop->mo_device))) {
414                 vprint("found %s\n", 
415                        (rc == 1) ? MOUNT_DATA_FILE : "extents");
416                  /* in the -1 case, 'extents' means this really IS a lustre
417                     target */
418                 return rc;
419         }
420         
421         if ((rc = file_in_dev(LAST_RCVD, mop->mo_device))) { 
422                 vprint("found %s\n", LAST_RCVD);
423                 return rc; 
424         }
425
426         vprint("not found\n");
427         return 0; /* The device is not a lustre target. */
428 }
429
430 /* Build fs according to type */
431 int make_lustre_backfs(struct mkfs_opts *mop)
432 {
433         char mkfs_cmd[PATH_MAX];
434         char buf[40];
435         char *dev;
436         int ret = 0;
437         int block_count = 0;
438         int left = sizeof(mkfs_cmd);
439
440         if (mop->mo_device_sz != 0) {
441                 if (mop->mo_device_sz < 8096){
442                         fprintf(stderr, "%s: size of filesystem must be larger "
443                                 "than 8MB, but is set to %lluKB\n",
444                                 progname, (long long)mop->mo_device_sz);
445                         return EINVAL;
446                 }
447                 block_count = mop->mo_device_sz / (L_BLOCK_SIZE >> 10);
448         }
449
450         if ((mop->mo_ldd.ldd_mount_type == LDD_MT_EXT3) ||
451             (mop->mo_ldd.ldd_mount_type == LDD_MT_LDISKFS)) {
452                 __u64 device_sz = mop->mo_device_sz;
453
454                 /* we really need the size */
455                 if (device_sz == 0) {
456                         device_sz = get_device_size(mop->mo_device);
457                         if (device_sz == 0)
458                                 return ENODEV;
459                 }
460
461                 /* Journal size in MB */
462                 if (strstr(mop->mo_mkfsopts, "-J") == NULL) {
463                         /* Choose our own default journal size */
464                         long journal_sz = 0, max_sz;
465                         if (device_sz > 1024 * 1024) /* 1GB */
466                                 journal_sz = (device_sz / 102400) * 4;
467                         /* man mkfs.ext3 */
468                         max_sz = (102400 * L_BLOCK_SIZE) >> 20; /* 400MB */
469                         if (journal_sz > max_sz)
470                                 journal_sz = max_sz;
471                         if (journal_sz) {
472                                 sprintf(buf, " -J size=%ld", journal_sz);
473                                 strcat(mop->mo_mkfsopts, buf);
474                         }
475                 }
476
477                 /* bytes_per_inode: disk size / num inodes */
478                 if (strstr(mop->mo_mkfsopts, "-i") == NULL) {
479                         long bytes_per_inode = 0;
480                                         
481                         if (IS_MDT(&mop->mo_ldd)) 
482                                 bytes_per_inode = 4096;
483
484                         /* Allocate fewer inodes on large OST devices.  Most
485                            filesystems can be much more aggressive than even 
486                            this. */
487                         if ((IS_OST(&mop->mo_ldd) && (device_sz > 1000000))) 
488                                 bytes_per_inode = 16384;
489                         
490                         if (bytes_per_inode > 0) {
491                                 sprintf(buf, " -i %ld", bytes_per_inode);
492                                 strcat(mop->mo_mkfsopts, buf);
493                         }
494                 }
495                 
496                 /* Inode size (for extended attributes) */
497                 if (strstr(mop->mo_mkfsopts, "-I") == NULL) {
498                         long inode_size = 0;
499                         if (IS_MDT(&mop->mo_ldd)) {
500                                 if (mop->mo_stripe_count > 72)
501                                         inode_size = 512; /* bz 7241 */
502                                 /* cray stripes across all osts (>60) */
503                                 else if (mop->mo_stripe_count > 32)
504                                         inode_size = 2048;
505                                 else if (mop->mo_stripe_count > 10)
506                                         inode_size = 1024;
507                                 else
508                                         inode_size = 512;
509                         } else if (IS_OST(&mop->mo_ldd)) {
510                                 /* now as we store fids in EA on OST we need
511                                    to make inode bigger */
512                                 inode_size = 256;
513                         }
514
515                         if (inode_size > 0) {
516                                 sprintf(buf, " -I %ld", inode_size);
517                                 strcat(mop->mo_mkfsopts, buf);
518                         }
519                         
520                 }
521
522                 if (verbose < 2) {
523                         strcat(mop->mo_mkfsopts, " -q");
524                 }
525
526                 /* Enable hashed b-tree directory lookup in large dirs bz6224 */
527                 if (strstr(mop->mo_mkfsopts, "-O") == NULL) {
528                         strcat(mop->mo_mkfsopts, " -O dir_index");
529                 }
530
531                 /* Allow reformat of full devices (as opposed to 
532                    partitions.)  We already checked for mounted dev. */
533                 strcat(mop->mo_mkfsopts, " -F");
534
535                 left -= snprintf(mkfs_cmd, left, 
536                                  "mkfs.ext2 -j -b %d -L %s ", L_BLOCK_SIZE,
537                                  mop->mo_ldd.ldd_svname);
538
539         } else if (mop->mo_ldd.ldd_mount_type == LDD_MT_REISERFS) {
540                 long journal_sz = 0; /* FIXME default journal size */
541                 if (journal_sz > 0) { 
542                         sprintf(buf, " --journal_size %ld", journal_sz);
543                         strcat(mop->mo_mkfsopts, buf);
544                 }
545                 left -= snprintf(mkfs_cmd, left, "mkreiserfs -ff ");
546
547         } else {
548                 fprintf(stderr,"%s: unsupported fs type: %d (%s)\n",
549                         progname, mop->mo_ldd.ldd_mount_type, 
550                         MT_STR(&mop->mo_ldd));
551                 return EINVAL;
552         }
553
554         /* For loop device format the dev, not the filename */
555         dev = mop->mo_device;
556         if (mop->mo_flags & MO_IS_LOOP) 
557                 dev = mop->mo_loopdev;
558         
559         vprint("formatting backing filesystem %s on %s\n",
560                MT_STR(&mop->mo_ldd), dev);
561         vprint("\ttarget name  %s\n", mop->mo_ldd.ldd_svname);
562         vprint("\t4k blocks     %d\n", block_count);
563         vprint("\toptions       %s\n", mop->mo_mkfsopts);
564
565         /* mkfs_cmd's trailing space is important! */
566         strncat(mkfs_cmd, mop->mo_mkfsopts, left);
567         left = sizeof(mkfs_cmd) - strlen(mkfs_cmd) - 1;
568         strncat(mkfs_cmd, " ", left);
569         left = sizeof(mkfs_cmd) - strlen(mkfs_cmd) - 1;
570         strncat(mkfs_cmd, dev, left);
571         left = sizeof(mkfs_cmd) - strlen(mkfs_cmd) - 1;
572         if (block_count != 0) {
573                 sprintf(buf, " %d", block_count);
574                 strncat(mkfs_cmd, buf, left);
575                 left = sizeof(mkfs_cmd) - strlen(mkfs_cmd) - 1;
576         }
577
578         vprint("mkfs_cmd = %s\n", mkfs_cmd);
579         ret = run_command(mkfs_cmd, sizeof(mkfs_cmd));
580         if (ret) {
581                 fatal();
582                 fprintf(stderr, "Unable to build fs %s (%d)\n", dev, ret);
583         }
584         return ret;
585 }
586
587 /* ==================== Lustre config functions =============*/
588
589 void print_ldd(char *str, struct lustre_disk_data *ldd)
590 {
591         printf("\n   %s:\n", str);
592         printf("Target:     %s\n", ldd->ldd_svname);
593         if (ldd->ldd_svindex == INDEX_UNASSIGNED) 
594                 printf("Index:      unassigned\n");
595         else
596                 printf("Index:      %d\n", ldd->ldd_svindex);
597         if (ldd->ldd_uuid[0])
598                 printf("UUID:       %s\n", (char *)ldd->ldd_uuid);
599         printf("Lustre FS:  %s\n", ldd->ldd_fsname);
600         printf("Mount type: %s\n", MT_STR(ldd));
601         printf("Flags:      %#x\n", ldd->ldd_flags);
602         printf("              (%s%s%s%s%s%s%s%s)\n",
603                IS_MDT(ldd) ? "MDT ":"", 
604                IS_OST(ldd) ? "OST ":"",
605                IS_MGS(ldd) ? "MGS ":"",
606                ldd->ldd_flags & LDD_F_NEED_INDEX ? "needs_index ":"",
607                ldd->ldd_flags & LDD_F_VIRGIN     ? "first_time ":"",
608                ldd->ldd_flags & LDD_F_UPDATE     ? "update ":"",
609                ldd->ldd_flags & LDD_F_WRITECONF  ? "writeconf ":"",
610                ldd->ldd_flags & LDD_F_UPGRADE14  ? "upgrade1.4 ":"");
611         printf("Persistent mount opts: %s\n", ldd->ldd_mount_opts);
612         printf("Parameters:%s\n", ldd->ldd_params);
613         if (ldd->ldd_userdata[0])
614                 printf("Comment: %s\n", ldd->ldd_userdata);
615         printf("\n");
616 }
617
618 /* Write the server config files */
619 int write_local_files(struct mkfs_opts *mop)
620 {
621         char mntpt[] = "/tmp/mntXXXXXX";
622         char filepnm[128];
623         char *dev;
624         FILE *filep;
625         int ret = 0;
626
627         /* Mount this device temporarily in order to write these files */
628         if (!mkdtemp(mntpt)) {
629                 fprintf(stderr, "%s: Can't create temp mount point %s: %s\n",
630                         progname, mntpt, strerror(errno));
631                 return errno;
632         }
633
634         dev = mop->mo_device;
635         if (mop->mo_flags & MO_IS_LOOP) 
636                 dev = mop->mo_loopdev;
637         
638         ret = mount(dev, mntpt, MT_STR(&mop->mo_ldd), 0, NULL);
639         if (ret) {
640                 fprintf(stderr, "%s: Unable to mount %s: %s\n", 
641                         progname, dev, strerror(errno));
642                 ret = errno;
643                 if (errno == ENODEV) {
644                         fprintf(stderr, "Is the %s module available?\n", 
645                                 MT_STR(&mop->mo_ldd));
646                 }
647                 goto out_rmdir;
648         }
649
650         /* Set up initial directories */
651         sprintf(filepnm, "%s/%s", mntpt, MOUNT_CONFIGS_DIR);
652         ret = mkdir(filepnm, 0777);
653         if ((ret != 0) && (errno != EEXIST)) {
654                 fprintf(stderr, "%s: Can't make configs dir %s: %s\n", 
655                         progname, filepnm, strerror(errno));
656                 goto out_umnt;
657         } else if (errno == EEXIST) {
658                 ret = 0;
659         }
660
661         /* Save the persistent mount data into a file. Lustre must pre-read
662            this file to get the real mount options. */
663         vprint("Writing %s\n", MOUNT_DATA_FILE);
664         sprintf(filepnm, "%s/%s", mntpt, MOUNT_DATA_FILE);
665         filep = fopen(filepnm, "w");
666         if (!filep) {
667                 fprintf(stderr, "%s: Unable to create %s file: %s\n",
668                         progname, filepnm, strerror(errno));
669                 goto out_umnt;
670         }
671         fwrite(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
672         fclose(filep);
673         
674         /* COMPAT_146 */
675 #ifdef TUNEFS
676         /* Check for upgrade */
677         if ((mop->mo_ldd.ldd_flags & (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) 
678             == (LDD_F_UPGRADE14 | LDD_F_SV_TYPE_MGS)) {
679                 char cmd[128];
680                 char *term;
681                 int cmdsz = sizeof(cmd);
682                 vprint("Copying old logs\n");
683                 
684                 /* Copy the old client log to fsname-client */
685                 sprintf(filepnm, "%s/%s/%s-client", 
686                         mntpt, MOUNT_CONFIGS_DIR, mop->mo_ldd.ldd_fsname);
687                 snprintf(cmd, cmdsz, "cp %s/%s/client %s", mntpt, MDT_LOGS_DIR,
688                          filepnm);
689                 ret = run_command(cmd, cmdsz);
690                 if (ret) {
691                         fprintf(stderr, "%s: Can't copy 1.4 config %s/client "
692                                 "(%d)\n", progname, MDT_LOGS_DIR, ret);
693                         fprintf(stderr, "mount -t ldiskfs %s somewhere, "
694                                 "find the client log for fs %s and "
695                                 "copy it manually into %s/%s-client, "
696                                 "then umount.\n",
697                                 mop->mo_device, 
698                                 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
699                                 mop->mo_ldd.ldd_fsname);
700                         goto out_umnt;
701                 }
702
703                 /* We need to use the old mdt log because otherwise mdt won't
704                    have complete lov if old clients connect before all 
705                    servers upgrade. */
706                 /* Copy the old mdt log to fsname-MDT0000 (get old
707                    name from mdt_UUID) */
708                 ret = 1;
709                 strcpy(filepnm, mop->mo_ldd.ldd_uuid);
710                 term = strstr(filepnm, "_UUID");
711                 if (term) {
712                         *term = '\0';
713                         snprintf(cmd, cmdsz, "cp %s/%s/%s %s/%s/%s",
714                                  mntpt, MDT_LOGS_DIR, filepnm, 
715                                  mntpt, MOUNT_CONFIGS_DIR,
716                                  mop->mo_ldd.ldd_svname);
717                         ret = run_command(cmd, cmdsz);
718                 }
719                 if (ret) {
720                         fprintf(stderr, "%s: Can't copy 1.4 config %s/%s "
721                                 "(%d)\n", progname, MDT_LOGS_DIR, filepnm, ret);
722                         fprintf(stderr, "mount -t ext3 %s somewhere, "
723                                 "find the MDT log for fs %s and "
724                                 "copy it manually into %s/%s, "
725                                 "then umount.\n",
726                                 mop->mo_device, 
727                                 mop->mo_ldd.ldd_fsname, MOUNT_CONFIGS_DIR,
728                                 mop->mo_ldd.ldd_svname);
729                         goto out_umnt;
730                 }
731         }
732 #endif
733         /* end COMPAT_146 */
734
735
736 out_umnt:
737         umount(mntpt);    
738 out_rmdir:
739         rmdir(mntpt);
740         return ret;
741 }
742
743 int read_local_files(struct mkfs_opts *mop)
744 {
745         char tmpdir[] = "/tmp/dirXXXXXX";
746         char cmd[PATH_MAX];
747         char filepnm[128];
748         char *dev;
749         FILE *filep;
750         int ret = 0;
751         int cmdsz = sizeof(cmd);
752
753         /* Make a temporary directory to hold Lustre data files. */
754         if (!mkdtemp(tmpdir)) {
755                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
756                         progname, tmpdir, strerror(errno));
757                 return errno;
758         }
759
760         dev = mop->mo_device;
761
762         /* Construct debugfs command line. */
763         snprintf(cmd, cmdsz, "debugfs -c -R 'dump /%s %s/mountdata' %s",
764                  MOUNT_DATA_FILE, tmpdir, dev);
765
766         ret = run_command(cmd, cmdsz);
767         if (ret)
768                 verrprint("%s: Unable to dump %s dir (%d)\n",
769                           progname, MOUNT_CONFIGS_DIR, ret);
770
771         sprintf(filepnm, "%s/mountdata", tmpdir);
772         filep = fopen(filepnm, "r");
773         if (filep) {
774                 vprint("Reading %s\n", MOUNT_DATA_FILE);
775                 fread(&mop->mo_ldd, sizeof(mop->mo_ldd), 1, filep);
776         } else {
777                 /* COMPAT_146 */
778                 /* Try to read pre-1.6 config from last_rcvd */
779                 struct lr_server_data lsd;
780                 verrprint("%s: Unable to read %s (%s).\n",
781                           progname, filepnm, strerror(errno));
782
783                 verrprint("Trying last_rcvd\n");
784                 sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
785
786                 /* Construct debugfs command line. */
787                 snprintf(cmd, cmdsz, "debugfs -c -R 'dump /%s %s' %s",
788                          LAST_RCVD, filepnm, dev);
789
790                 ret = run_command(cmd, cmdsz);
791                 if (ret) {
792                         fprintf(stderr, "%s: Unable to dump %s file (%d)\n",
793                                 progname, LAST_RCVD, ret);
794                         goto out_rmdir;
795                 }
796                 
797                 filep = fopen(filepnm, "r");
798                 if (!filep) {
799                         fprintf(stderr, "%s: Unable to open %s: %s\n",
800                                 progname, filepnm, strerror(errno));
801                         ret = errno;
802                         verrprint("Contents of %s:\n", tmpdir);
803                         verbose+=2;
804                         snprintf(cmd, cmdsz, "ls -l %s/", tmpdir);
805                         run_command(cmd, cmdsz);
806                         verrprint("Contents of disk:\n");
807                         snprintf(cmd, cmdsz, "debugfs -c -R 'ls -l /' %s", dev);
808                         run_command(cmd, cmdsz);
809
810                         goto out_rmdir;
811                 }
812                 vprint("Reading %s\n", LAST_RCVD);
813                 ret = fread(&lsd, 1, sizeof(lsd), filep);
814                 if (ret < sizeof(lsd)) {
815                         fprintf(stderr, "%s: Short read (%d of %d)\n",
816                                 progname, ret, (int)sizeof(lsd));
817                         ret = ferror(filep);
818                         if (ret)
819                                 goto out_close;
820                 }
821                 vprint("Feature compat=%x, incompat=%x\n",
822                        lsd.lsd_feature_compat, lsd.lsd_feature_incompat);
823
824                 if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
825                     (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
826                         mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
827                         mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
828                 } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
829                            (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
830                         /* We must co-locate so mgs can see old logs.
831                            If user doesn't want this, they can copy the old
832                            logs manually and re-tunefs. */
833                         mop->mo_ldd.ldd_flags = 
834                                 LDD_F_SV_TYPE_MDT | LDD_F_SV_TYPE_MGS;
835                         mop->mo_ldd.ldd_svindex = lsd.lsd_mdt_index;
836                 } else  {
837                         /* If neither is set, we're pre-1.4.6, make a guess. */
838                         /* Construct debugfs command line. */
839                         snprintf(cmd, cmdsz, "debugfs -c -R 'rdump /%s %s' %s",
840                                  MDT_LOGS_DIR, tmpdir, dev);
841                         run_command(cmd, cmdsz);
842
843                         sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
844                         if (lsd.lsd_ost_index > 0) {
845                                 mop->mo_ldd.ldd_flags = LDD_F_SV_TYPE_OST;
846                                 mop->mo_ldd.ldd_svindex = lsd.lsd_ost_index;
847                         } else {
848                                 /* If there's a LOGS dir, it's an MDT */
849                                 if ((ret = access(filepnm, F_OK)) == 0) {
850                                         mop->mo_ldd.ldd_flags =
851                                         LDD_F_SV_TYPE_MDT | 
852                                         LDD_F_SV_TYPE_MGS;
853                                         /* Old MDT's are always index 0 
854                                            (pre CMD) */
855                                         mop->mo_ldd.ldd_svindex = 0;
856                                 } else {
857                                         /* The index may not be correct */
858                                         mop->mo_ldd.ldd_flags =
859                                         LDD_F_SV_TYPE_OST | LDD_F_NEED_INDEX;
860                                         verrprint("OST with unknown index\n");
861                                 }
862                         }
863                 }
864                 
865                 ret = 0;
866                 memcpy(mop->mo_ldd.ldd_uuid, lsd.lsd_uuid, 
867                        sizeof(mop->mo_ldd.ldd_uuid));
868                 mop->mo_ldd.ldd_flags |= LDD_F_UPGRADE14;
869         }
870         /* end COMPAT_146 */
871 out_close:        
872         fclose(filep);
873
874 out_rmdir:
875         snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
876         run_command(cmd, cmdsz);
877         if (ret) 
878                 verrprint("Failed to read old data (%d)\n", ret);
879         return ret;
880 }
881
882
883 void set_defaults(struct mkfs_opts *mop)
884 {
885         mop->mo_ldd.ldd_magic = LDD_MAGIC;
886         mop->mo_ldd.ldd_config_ver = 1;
887         mop->mo_ldd.ldd_flags = LDD_F_NEED_INDEX | LDD_F_UPDATE | LDD_F_VIRGIN;
888         mop->mo_mgs_failnodes = 0;
889         strcpy(mop->mo_ldd.ldd_fsname, "lustre");
890         if (get_os_version() == 24) 
891                 mop->mo_ldd.ldd_mount_type = LDD_MT_EXT3;
892         else 
893                 mop->mo_ldd.ldd_mount_type = LDD_MT_LDISKFS;
894         
895         mop->mo_ldd.ldd_svindex = INDEX_UNASSIGNED;
896         mop->mo_stripe_count = 1;
897 }
898
899 static inline void badopt(const char *opt, char *type)
900 {
901         fprintf(stderr, "%s: '--%s' only valid for %s\n",
902                 progname, opt, type);
903         usage(stderr);
904 }
905
906 static int add_param(char *buf, char *key, char *val)
907 {
908         int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
909         int start = strlen(buf);
910         int keylen = 0;
911
912         if (key) 
913                 keylen = strlen(key);
914         if (start + 1 + keylen + strlen(val) >= end) {
915                 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
916                         progname, buf, key ? key : "", val);
917                 return 1;
918         }
919
920         sprintf(buf + start, " %s%s", key ? key : "", val);
921         return 0;
922 }
923
924 /* from mount_lustre */
925 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
926 #define MAXNIDSTR 1024
927 static char *convert_hostnames(char *s1)
928 {
929         char *converted, *s2 = 0, *c;
930         int left = MAXNIDSTR;
931         lnet_nid_t nid;
932         
933         converted = malloc(left);
934         c = converted;
935         while ((left > 0) && ((s2 = strsep(&s1, ",: \0")))) {
936                 nid = libcfs_str2nid(s2);
937                 if (nid == LNET_NID_ANY) {
938                         if (*s2 == '/') 
939                                 /* end of nids */
940                                 break;
941                         fprintf(stderr, "%s: Can't parse NID '%s'\n", 
942                                 progname, s2);
943                         free(converted);
944                         return NULL;
945                 }
946
947                 if (strncmp(libcfs_nid2str(nid), "127.0.0.1", 
948                             strlen("127.0.0.1")) == 0) {
949                         fprintf(stderr, "%s: The NID '%s' resolves to the "
950                                 "loopback address '%s'.  Lustre requires a "
951                                 "non-loopback address.\n", 
952                                 progname, s2, libcfs_nid2str(nid));
953                         free(converted);
954                         return NULL;
955                 }
956
957                 c += snprintf(c, left, "%s,", libcfs_nid2str(nid));
958                 left = converted + MAXNIDSTR - c;
959         }
960         *(c - 1) = '\0';
961         return converted;
962 }
963
964 int parse_opts(int argc, char *const argv[], struct mkfs_opts *mop,
965                char **mountopts)
966 {
967         static struct option long_opt[] = {
968                 {"backfstype", 1, 0, 'b'},
969                 {"stripe-count-hint", 1, 0, 'c'},
970                 {"comment", 1, 0, 'u'},
971                 {"configdev", 1, 0, 'C'},
972                 {"device-size", 1, 0, 'd'},
973                 {"dryrun", 0, 0, 'n'},
974                 {"erase-params", 0, 0, 'e'},
975                 {"failnode", 1, 0, 'f'},
976                 {"failover", 1, 0, 'f'},
977                 {"mgs", 0, 0, 'G'},
978                 {"help", 0, 0, 'h'},
979                 {"index", 1, 0, 'i'},
980                 {"mkfsoptions", 1, 0, 'k'},
981                 {"mgsnode", 1, 0, 'm'},
982                 {"mgsnid", 1, 0, 'm'},
983                 {"mdt", 0, 0, 'M'},
984                 {"fsname",1, 0, 'L'},
985                 {"noformat", 0, 0, 'n'},
986                 {"nomgs", 0, 0, 'N'},
987                 {"mountfsoptions", 1, 0, 'o'},
988                 {"ost", 0, 0, 'O'},
989                 {"param", 1, 0, 'p'},
990                 {"print", 0, 0, 'n'},
991                 {"quiet", 0, 0, 'q'},
992                 {"reformat", 0, 0, 'r'},
993                 {"verbose", 0, 0, 'v'},
994                 {"writeconf", 0, 0, 'w'},
995                 {0, 0, 0, 0}
996         };
997         char *optstring = "b:c:C:d:ef:Ghi:k:L:m:MnNo:Op:Pqru:vw";
998         int opt;
999         int rc, longidx;
1000
1001         while ((opt = getopt_long(argc, argv, optstring, long_opt, &longidx)) != 
1002                EOF) {
1003                 switch (opt) {
1004                 case 'b': {
1005                         int i = 0;
1006                         while (i < LDD_MT_LAST) {
1007                                 if (strcmp(optarg, mt_str(i)) == 0) {
1008                                         mop->mo_ldd.ldd_mount_type = i;
1009                                         break;
1010                                 }
1011                                 i++;
1012                         }
1013                         break;
1014                 }
1015                 case 'c':
1016                         if (IS_MDT(&mop->mo_ldd)) {
1017                                 int stripe_count = atol(optarg);
1018                                 if (stripe_count <= 0) {
1019                                         fprintf(stderr, "%s: bad stripe count "
1020                                                 "%d\n", progname, stripe_count);
1021                                         return 1;
1022                                 }
1023                                 mop->mo_stripe_count = stripe_count;
1024                         } else {
1025                                 badopt(long_opt[longidx].name, "MDT");
1026                                 return 1;
1027                         }
1028                         break;
1029                 case 'C': /* Configdev */
1030                         //FIXME
1031                         printf("Configdev not implemented\n");
1032                         return 1;
1033                 case 'd':
1034                         mop->mo_device_sz = atol(optarg); 
1035                         break;
1036                 case 'e':
1037                         mop->mo_ldd.ldd_params[0] = '\0';
1038                         /* Must update the mgs logs */
1039                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1040                         break;
1041                 case 'f': {
1042                         char *nids = convert_hostnames(optarg);
1043                         if (!nids) 
1044                                 return 1;
1045                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_FAILNODE, 
1046                                        nids); 
1047                         /* Combo needs to add MDT failnodes as MGS failnodes
1048                            as well */
1049                         if (!rc && IS_MGS(&mop->mo_ldd)) {
1050                                 rc = add_param(mop->mo_ldd.ldd_params, 
1051                                                PARAM_MGSNODE, nids); 
1052                         }
1053                         free(nids);
1054                         if (rc) 
1055                                 return rc;
1056                         /* Must update the mgs logs */
1057                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1058                         break;
1059                 }
1060                 case 'G':
1061                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MGS;
1062                         break;
1063                 case 'h':
1064                         usage(stdout);
1065                         return 1;
1066                 case 'i':
1067                         if (!(mop->mo_ldd.ldd_flags & 
1068                               (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1069                                LDD_F_WRITECONF))) {
1070                                 fprintf(stderr, "%s: cannot change the index of"
1071                                         " a registered target\n", progname);
1072                                 return 1;
1073                         }
1074                         if (IS_MDT(&mop->mo_ldd) || IS_OST(&mop->mo_ldd)) {
1075                                 mop->mo_ldd.ldd_svindex = atol(optarg);
1076                                 mop->mo_ldd.ldd_flags &= ~LDD_F_NEED_INDEX;
1077                         } else {
1078                                 badopt(long_opt[longidx].name, "MDT,OST");
1079                                 return 1;
1080                         }
1081                         break;
1082                 case 'k':
1083                         strncpy(mop->mo_mkfsopts, optarg, 
1084                                 sizeof(mop->mo_mkfsopts) - 1);
1085                         break;
1086                 case 'L': {
1087                         char *tmp;
1088                         if (!(mop->mo_flags & MO_FORCEFORMAT) &&
1089                             (!(mop->mo_ldd.ldd_flags & 
1090                                (LDD_F_UPGRADE14 | LDD_F_VIRGIN |
1091                                 LDD_F_WRITECONF)))) {
1092                                 fprintf(stderr, "%s: cannot change the name of"
1093                                         " a registered target\n", progname);
1094                                 return 1;
1095                         }
1096                         if ((strlen(optarg) < 1) || (strlen(optarg) > 8)) {
1097                                 fprintf(stderr, "%s: filesystem name must be "
1098                                         "1-8 chars\n", progname);
1099                                 return 1;
1100                         }
1101                         if ((tmp = strpbrk(optarg, "/:"))) {
1102                                 fprintf(stderr, "%s: char '%c' not allowed in "
1103                                         "filesystem name\n", progname, *tmp);
1104                                 return 1;
1105                         }
1106                         strncpy(mop->mo_ldd.ldd_fsname, optarg, 
1107                                 sizeof(mop->mo_ldd.ldd_fsname) - 1);
1108                         break;
1109                 }
1110                 case 'm': {
1111                         char *nids = convert_hostnames(optarg);
1112                         if (!nids) 
1113                                 return 1;
1114                         rc = add_param(mop->mo_ldd.ldd_params, PARAM_MGSNODE, 
1115                                        nids); 
1116                         free(nids);
1117                         if (rc) 
1118                                 return rc;
1119                         mop->mo_mgs_failnodes++;
1120                         break;
1121                 }
1122                 case 'M':
1123                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_MDT;
1124                         break;
1125                 case 'n':
1126                         print_only++;
1127                         break;
1128                 case 'N':
1129                         mop->mo_ldd.ldd_flags &= ~LDD_F_SV_TYPE_MGS;
1130                         break;
1131                 case 'o':
1132                         *mountopts = optarg;
1133                         break;
1134                 case 'O':
1135                         mop->mo_ldd.ldd_flags |= LDD_F_SV_TYPE_OST;
1136                         break;
1137                 case 'p':
1138                         rc = add_param(mop->mo_ldd.ldd_params, NULL, optarg);
1139                         if (rc) 
1140                                 return rc;
1141                         /* Must update the mgs logs */
1142                         mop->mo_ldd.ldd_flags |= LDD_F_UPDATE;
1143                         break;
1144                 case 'q':
1145                         verbose--;
1146                         break;
1147                 case 'r':
1148                         mop->mo_flags |= MO_FORCEFORMAT;
1149                         break;
1150                 case 'u':
1151                         strncpy(mop->mo_ldd.ldd_userdata, optarg,
1152                                 sizeof(mop->mo_ldd.ldd_userdata));
1153                         mop->mo_ldd.ldd_userdata[
1154                                 sizeof(mop->mo_ldd.ldd_userdata) - 1] = 0;
1155                         break;
1156                 case 'v':
1157                         verbose++;
1158                         break;
1159                 case 'w':
1160                         mop->mo_ldd.ldd_flags |= LDD_F_WRITECONF;
1161                         break;
1162                 default:
1163                         if (opt != '?') {
1164                                 fatal();
1165                                 fprintf(stderr, "Unknown option '%c'\n", opt);
1166                         }
1167                         return EINVAL;
1168                 }
1169         }//while
1170
1171         /* Last arg is device */
1172         if (optind != argc - 1) {
1173                 fatal();
1174                 fprintf(stderr, "Bad argument: %s\n", argv[optind]);
1175                 return EINVAL;
1176         }
1177
1178         return 0;
1179 }
1180
1181 int main(int argc, char *const argv[])
1182 {
1183         struct mkfs_opts mop;
1184         struct lustre_disk_data *ldd;
1185         char *mountopts = NULL;
1186         char always_mountopts[512] = "";
1187         char default_mountopts[512] = "";
1188         int  ret = 0;
1189
1190         if ((progname = strrchr(argv[0], '/')) != NULL)
1191                 progname++;
1192         else
1193                 progname = argv[0];
1194
1195         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
1196                 usage(stderr);
1197                 return(EINVAL);
1198         }
1199
1200         memset(&mop, 0, sizeof(mop));
1201         set_defaults(&mop);
1202
1203         /* device is last arg */
1204         strcpy(mop.mo_device, argv[argc - 1]);
1205
1206         /* Are we using a loop device? */
1207         ret = is_block(mop.mo_device);
1208         if (ret < 0) 
1209                 goto out;
1210         if (ret == 0) 
1211                 mop.mo_flags |= MO_IS_LOOP;
1212
1213 #ifdef TUNEFS
1214         /* For tunefs, we must read in the old values before parsing any
1215            new ones. */
1216         
1217         /* Check whether the disk has already been formatted by mkfs.lustre */
1218         ret = is_lustre_target(&mop);
1219         if (ret == 0) {
1220                 fatal();
1221                 fprintf(stderr, "Device %s has not been formatted with "
1222                         "mkfs.lustre\n", mop.mo_device);
1223                 ret = ENODEV;
1224                 goto out;
1225         }
1226
1227         ret = read_local_files(&mop);
1228         if (ret) {
1229                 fatal();
1230                 fprintf(stderr, "Failed to read previous Lustre data from %s "
1231                         "(%d)\n", mop.mo_device, ret);
1232                 goto out;
1233         }
1234         if (strstr(mop.mo_ldd.ldd_params, PARAM_MGSNODE))
1235             mop.mo_mgs_failnodes++;
1236
1237         if (verbose > 0) 
1238                 print_ldd("Read previous values", &(mop.mo_ldd));
1239 #endif
1240
1241         ret = parse_opts(argc, argv, &mop, &mountopts);
1242         if (ret) 
1243                 goto out;
1244
1245         ldd = &mop.mo_ldd;
1246         
1247         if (!(IS_MDT(ldd) || IS_OST(ldd) || IS_MGS(ldd))) {
1248                 fatal();
1249                 fprintf(stderr, "must set target type: MDT,OST,MGS\n");
1250                 ret = EINVAL;
1251                 goto out;
1252         }
1253
1254         if (((IS_MDT(ldd) || IS_MGS(ldd))) && IS_OST(ldd)) {
1255                 fatal();
1256                 fprintf(stderr, "OST type is exclusive with MDT,MGS\n");
1257                 ret = EINVAL;
1258                 goto out;
1259         }
1260
1261         if ((mop.mo_ldd.ldd_flags & (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) ==
1262             (LDD_F_NEED_INDEX | LDD_F_UPGRADE14)) {
1263                 fatal();
1264                 fprintf(stderr, "Can't find the target index, "
1265                         "specify with --index\n");
1266                 ret = EINVAL;
1267                 goto out;
1268         }
1269
1270         if (!IS_MGS(ldd) && (mop.mo_mgs_failnodes == 0)) {
1271                 fatal();
1272                 if (IS_MDT(ldd)) 
1273                         fprintf(stderr, "Must specify --mgs or --mgsnode=\n");
1274                 else 
1275                         fprintf(stderr, "Must specify --mgsnode=\n");
1276                 ret = EINVAL;
1277                 goto out;
1278         }
1279
1280         /* These are the permanent mount options (always included) */ 
1281         switch (ldd->ldd_mount_type) {
1282         case LDD_MT_EXT3:
1283         case LDD_MT_LDISKFS: {
1284                 sprintf(always_mountopts, "errors=remount-ro");
1285                 if (IS_MDT(ldd) || IS_MGS(ldd))
1286                         strcat(always_mountopts,
1287                                ",iopen_nopriv,user_xattr");
1288                 if ((get_os_version() == 24) && IS_OST(ldd))
1289                         strcat(always_mountopts, ",asyncdel");
1290                 /* NB: Files created while extents are enabled cannot be read
1291                    if mounted with a kernel that doesn't include the CFS 
1292                    patches! */
1293                 if (IS_OST(ldd) && 
1294                     ldd->ldd_mount_type == LDD_MT_LDISKFS) {
1295                         strcat(default_mountopts, ",extents,mballoc");
1296                 }
1297                 break;
1298         }
1299         case LDD_MT_SMFS: {
1300                 mop.mo_flags |= MO_IS_LOOP;
1301                 sprintf(always_mountopts, "type=ext3,dev=%s",
1302                         mop.mo_device);
1303                 break;
1304         }
1305         default: {
1306                 fatal();
1307                 fprintf(stderr, "unknown fs type %d '%s'\n",
1308                         ldd->ldd_mount_type,
1309                         MT_STR(ldd));
1310                 ret = EINVAL;
1311                 goto out;
1312         }
1313         }               
1314
1315         if (mountopts) {
1316                 /* If user specifies mount opts, don't use defaults,
1317                    but always use always_mountopts */
1318                 sprintf(ldd->ldd_mount_opts, "%s,%s", 
1319                         always_mountopts, mountopts);
1320         } else {
1321 #ifdef TUNEFS
1322                 if (ldd->ldd_mount_opts[0] == 0) 
1323                         /* use the defaults unless old opts exist */
1324 #endif
1325                 {
1326                         sprintf(ldd->ldd_mount_opts, "%s%s", 
1327                                 always_mountopts, default_mountopts);
1328                 }
1329         }
1330
1331         server_make_name(ldd->ldd_flags, ldd->ldd_svindex,
1332                          ldd->ldd_fsname, ldd->ldd_svname);
1333
1334         if (verbose >= 0)
1335                 print_ldd("Permanent disk data", ldd);
1336
1337         if (print_only) {
1338                 printf("exiting before disk write.\n");
1339                 goto out;
1340         }
1341
1342         if (check_mtab_entry(mop.mo_device))
1343                 return(EEXIST);
1344
1345         /* Create the loopback file */
1346         if (mop.mo_flags & MO_IS_LOOP) {
1347                 ret = access(mop.mo_device, F_OK);
1348                 if (ret) 
1349                         ret = errno;
1350 #ifndef TUNEFS /* mkfs.lustre */
1351                 /* Reformat the loopback file */
1352                 if (ret || (mop.mo_flags & MO_FORCEFORMAT))
1353                         ret = loop_format(&mop);
1354 #endif
1355                 if (ret == 0)  
1356                         ret = loop_setup(&mop);
1357                 if (ret) {
1358                         fatal();
1359                         fprintf(stderr, "Loop device setup for %s failed: %s\n",
1360                                 mop.mo_device, strerror(ret));
1361                         goto out;
1362                 }
1363         }
1364
1365 #ifndef TUNEFS /* mkfs.lustre */
1366         /* Check whether the disk has already been formatted by mkfs.lustre */
1367         if (!(mop.mo_flags & MO_FORCEFORMAT)) {
1368                 ret = is_lustre_target(&mop);
1369                 if (ret) {
1370                         fatal();
1371                         fprintf(stderr, "Device %s was previously formatted " 
1372                                 "for lustre. Use --reformat to reformat it, "
1373                                 "or tunefs.lustre to modify.\n",
1374                                 mop.mo_device);
1375                         goto out;
1376                 }
1377         }
1378
1379         /* Format the backing filesystem */
1380         ret = make_lustre_backfs(&mop);
1381         if (ret != 0) {
1382                 fatal();
1383                 fprintf(stderr, "mkfs failed %d\n", ret);
1384                 goto out;
1385         }
1386 #endif
1387
1388         /* Write our config files */
1389         ret = write_local_files(&mop);
1390         if (ret != 0) {
1391                 fatal();
1392                 fprintf(stderr, "failed to write local files\n");
1393                 goto out;
1394         }
1395
1396 out:
1397         loop_cleanup(&mop);    
1398
1399         /* Fix any crazy return values from system() */      
1400         if (ret && ((ret & 255) == 0))
1401                 return (1);
1402         if (ret)
1403                 verrprint("%s: exiting with %d (%s)\n",
1404                           progname, ret, strerror(ret));
1405         return (ret);
1406 }