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