Whamcloud - gitweb
ChangeLog, dumpe2fs.8.in, dumpe2fs.c, mke2fs.8.in, mke2fs.c, partinfo.c:
[tools/e2fsprogs.git] / misc / mke2fs.c
1 /*
2  * mke2fs.c - Make a ext2fs filesystem.
3  * 
4  * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 /* Usage: mke2fs [options] device
13  * 
14  * The device may be a block device or a image of one, but this isn't
15  * enforced (but it's not much fun on a character device :-). 
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <ctype.h>
22 #include <termios.h>
23 #include <time.h>
24 #ifdef linux
25 #include <sys/utsname.h>
26 #endif
27 #ifdef HAVE_GETOPT_H
28 #include <getopt.h>
29 #endif
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_ERRNO_H
37 #include <errno.h>
38 #endif
39 #ifdef HAVE_MNTENT_H
40 #include <mntent.h>
41 #endif
42 #include <sys/ioctl.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45
46 #ifdef HAVE_LINUX_FS_H
47 #include <linux/fs.h>
48 #endif
49 #include <linux/ext2_fs.h>
50 #ifdef HAVE_LINUX_MAJOR_H
51 #include <linux/major.h>
52 #endif
53
54 #include "et/com_err.h"
55 #include "uuid/uuid.h"
56 #include "ext2fs/ext2fs.h"
57 #include "../version.h"
58
59 /* Everything is STDC, these days */
60 #define NOARGS void
61
62 #define STRIDE_LENGTH 8
63
64 #ifndef sparc
65 #define ZAP_BOOTBLOCK
66 #endif
67
68 extern int isatty(int);
69 extern FILE *fpopen(const char *cmd, const char *mode);
70
71 const char * program_name = "mke2fs";
72 const char * device_name = NULL;
73
74 /* Command line options */
75 int     cflag = 0;
76 int     verbose = 0;
77 int     quiet = 0;
78 int     super_only = 0;
79 int     force = 0;
80 int     noaction = 0;
81 char    *bad_blocks_filename = 0;
82 __u32   fs_stride = 0;
83
84 struct ext2_super_block param;
85 char *creator_os = NULL;
86 char *volume_label = NULL;
87 char *mount_dir = NULL;
88
89 static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
90
91 static void usage(NOARGS)
92 {
93         fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
94         "[-f fragment-size]\n\t[-i bytes-per-inode] "
95         " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
96         "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
97         "[-M last-mounted-directory] [-r fs-revision]\n\t[-R raid_opts]"
98         "[-s sparse-super-flag] [-qvSV] device [blocks-count]\n",
99                 program_name);
100         exit(1);
101 }
102
103 static int log2(int arg)
104 {
105         int     l = 0;
106
107         arg >>= 1;
108         while (arg) {
109                 l++;
110                 arg >>= 1;
111         }
112         return l;
113 }
114
115 static int log10(unsigned int arg)
116 {
117         int     l;
118
119         for (l=0; arg ; l++)
120                 arg = arg / 10;
121         return l;
122 }
123
124 static void proceed_question(NOARGS)
125 {
126         fflush(stdout);
127         fflush(stderr);
128         printf("Proceed anyway? (y,n) ");
129         if (getchar() != 'y')
130                 exit(1);
131 }
132
133 #ifndef SCSI_BLK_MAJOR
134 #define SCSI_BLK_MAJOR(M)  ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
135 #endif
136
137 static void check_plausibility(NOARGS)
138 {
139 #ifdef HAVE_LINUX_MAJOR_H
140         int val;
141         struct stat s;
142         
143         val = stat(device_name, &s);
144         
145         if(val == -1) {
146                 fprintf(stderr, "Could not stat %s --- %s\n",
147                         device_name, error_message(errno));
148                 if (errno == ENOENT)
149                         fprintf(stderr, "\nThe device apparently does "
150                                "not exist; did you specify it correctly?\n");
151                 exit(1);
152         }
153         if(!S_ISBLK(s.st_mode)) {
154                 printf("%s is not a block special device.\n", device_name);
155                 proceed_question();
156                 return;
157         } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
158                     MINOR(s.st_rdev)%64 == 0) ||
159                    (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
160                        MINOR(s.st_rdev)%16 == 0)) {
161                 printf("%s is entire device, not just one partition!\n", 
162                        device_name);
163                 proceed_question();
164         }
165 #endif
166 }
167
168 static void check_mount(NOARGS)
169 {
170         errcode_t       retval;
171         int             mount_flags;
172
173         retval = ext2fs_check_if_mounted(device_name, &mount_flags);
174         if (retval) {
175                 com_err("ext2fs_check_if_mount", retval,
176                         "while determining whether %s is mounted.",
177                         device_name);
178                 return;
179         }
180         if (!(mount_flags & EXT2_MF_MOUNTED))
181                 return;
182
183         fprintf(stderr, "%s is mounted; ", device_name);
184         if (force) {
185                 fprintf(stderr, "mke2fs forced anyway.  "
186                         "Hope /etc/mtab is incorrect.\n");
187         } else {
188                 fprintf(stderr, "will not make a filesystem here!\n");
189                 exit(1);
190         }
191 }
192
193 /*
194  * This function sets the default parameters for a filesystem
195  *
196  * The type is specified by the user.  The size is the maximum size
197  * (in megabytes) for which a set of parameters applies, with a size
198  * of zero meaning that it is the default parameter for the type.
199  * Note that order is important in the table below.
200  */
201 static char default_str[] = "default";
202 struct mke2fs_defaults {
203         char    *type;
204         int     size;
205         int     blocksize;
206         int     inode_ratio;
207 } settings[] = {
208         { default_str, 0, 4096, 8192 },
209         { default_str, 512, 1024, 4096 },
210         { default_str, 3, 1024, 8192 },
211         { "news", 0, 4096, 4096 },
212         { 0, 0, 0, 0},
213 };
214
215 static void set_fs_defaults(char *fs_type, struct ext2fs_sb *param,
216                             int blocksize, int *inode_ratio)
217 {
218         int     megs;
219         int     ratio = 0;
220         struct mke2fs_defaults *p;
221
222         megs = (param->s_blocks_count * (EXT2_BLOCK_SIZE(param) / 1024) /
223                 1024);
224         if (inode_ratio)
225                 ratio = *inode_ratio;
226         if (!fs_type)
227                 fs_type = default_str;
228         for (p = settings; p->type; p++) {
229                 if ((strcmp(p->type, fs_type) != 0) &&
230                     (strcmp(p->type, default_str) != 0))
231                         continue;
232                 if ((p->size != 0) &&
233                     (megs > p->size))
234                         continue;
235                 if (ratio == 0)
236                         *inode_ratio = p->inode_ratio;
237                 if (blocksize == 0) {
238                         param->s_log_frag_size = param->s_log_block_size =
239                                 log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
240                 }
241         }
242         if (blocksize == 0)
243                 param->s_blocks_count /= EXT2_BLOCK_SIZE(param) / 1024;
244 }
245
246 /*
247  * Helper function for read_bb_file and test_disk
248  */
249 static void invalid_block(ext2_filsys fs, blk_t blk)
250 {
251         printf("Bad block %u out of range; ignored.\n", blk);
252         return;
253 }
254
255 /*
256  * Reads the bad blocks list from a file
257  */
258 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
259                          const char *bad_blocks_file)
260 {
261         FILE            *f;
262         errcode_t       retval;
263
264         f = fopen(bad_blocks_file, "r");
265         if (!f) {
266                 com_err("read_bad_blocks_file", errno,
267                         "while trying to open %s", bad_blocks_file);
268                 exit(1);
269         }
270         retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
271         fclose (f);
272         if (retval) {
273                 com_err("ext2fs_read_bb_FILE", retval,
274                         "while reading in list of bad blocks from file");
275                 exit(1);
276         }
277 }
278
279 /*
280  * Runs the badblocks program to test the disk
281  */
282 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
283 {
284         FILE            *f;
285         errcode_t       retval;
286         char            buf[1024];
287
288         sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
289                 quiet ? "" : "-s ", fs->device_name,
290                 fs->super->s_blocks_count);
291         if (verbose)
292                 printf("Running command: %s\n", buf);
293         f = popen(buf, "r");
294         if (!f) {
295                 com_err("popen", errno,
296                         "while trying run '%s'", buf);
297                 exit(1);
298         }
299         retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
300         pclose(f);
301         if (retval) {
302                 com_err("ext2fs_read_bb_FILE", retval,
303                         "while processing list of bad blocks from program");
304                 exit(1);
305         }
306 }
307
308 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
309 {
310         int                     i, j;
311         int                     must_be_good;
312         blk_t                   blk;
313         badblocks_iterate       bb_iter;
314         errcode_t               retval;
315         blk_t                   group_block;
316         int                     group;
317         int                     group_bad;
318
319         if (!bb_list)
320                 return;
321         
322         /*
323          * The primary superblock and group descriptors *must* be
324          * good; if not, abort.
325          */
326         must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
327         for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
328                 if (badblocks_list_test(bb_list, i)) {
329                         fprintf(stderr, "Block %d in primary superblock/group "
330                                 "descriptor area bad.\n", i);
331                         fprintf(stderr, "Blocks %d through %d must be good "
332                                 "in order to build a filesystem.\n",
333                                 fs->super->s_first_data_block, must_be_good);
334                         fprintf(stderr, "Aborting....\n");
335                         exit(1);
336                 }
337         }
338
339         /*
340          * See if any of the bad blocks are showing up in the backup
341          * superblocks and/or group descriptors.  If so, issue a
342          * warning and adjust the block counts appropriately.
343          */
344         group_block = fs->super->s_first_data_block +
345                 fs->super->s_blocks_per_group;
346         
347         for (i = 1; i < fs->group_desc_count; i++) {
348                 group_bad = 0;
349                 for (j=0; j < fs->desc_blocks+1; j++) {
350                         if (badblocks_list_test(bb_list, group_block +
351                                                 j)) {
352                                 if (!group_bad) 
353                                         fprintf(stderr,
354 "Warning: the backup superblock/group descriptors at block %d contain\n"
355 "       bad blocks.\n\n",
356                                                 group_block);
357                                 group_bad++;
358                                 group = ext2fs_group_of_blk(fs, group_block+j);
359                                 fs->group_desc[group].bg_free_blocks_count++;
360                                 fs->super->s_free_blocks_count++;
361                         }
362                 }
363                 group_block += fs->super->s_blocks_per_group;
364         }
365         
366         /*
367          * Mark all the bad blocks as used...
368          */
369         retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
370         if (retval) {
371                 com_err("badblocks_list_iterate_begin", retval,
372                         "while marking bad blocks as used");
373                 exit(1);
374         }
375         while (badblocks_list_iterate(bb_iter, &blk)) 
376                 ext2fs_mark_block_bitmap(fs->block_map, blk);
377         badblocks_list_iterate_end(bb_iter);
378 }
379
380 static void write_inode_tables(ext2_filsys fs)
381 {
382         errcode_t       retval;
383         blk_t           blk;
384         int             i, j, num, count;
385         char            *buf;
386         char            format[20], backup[80];
387         int             sync_kludge = 0;
388         char            *mke2fs_sync;
389
390         mke2fs_sync = getenv("MKE2FS_SYNC");
391         if (mke2fs_sync)
392                 sync_kludge = atoi(mke2fs_sync);
393
394         buf = malloc(fs->blocksize * STRIDE_LENGTH);
395         if (!buf) {
396                 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
397                 exit(1);
398         }
399         memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
400
401         /*
402          * Figure out how many digits we need
403          */
404         i = log10(fs->group_desc_count);
405         sprintf(format, "%%%dd/%%%dld", i, i);
406         memset(backup, '\b', sizeof(backup)-1);
407         backup[sizeof(backup)-1] = 0;
408         if ((2*i)+1 < sizeof(backup))
409                 backup[(2*i)+1] = 0;
410
411         if (!quiet)
412                 printf("Writing inode tables: ");
413         for (i = 0; i < fs->group_desc_count; i++) {
414                 if (!quiet)
415                         printf(format, i, fs->group_desc_count);
416                 
417                 blk = fs->group_desc[i].bg_inode_table;
418                 num = fs->inode_blocks_per_group;
419                 
420                 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
421                         if (num-j > STRIDE_LENGTH)
422                                 count = STRIDE_LENGTH;
423                         else
424                                 count = num - j;
425                         retval = io_channel_write_blk(fs->io, blk, count, buf);
426                         if (retval)
427                                 printf("Warning: could not write %d blocks "
428                                        "in inode table starting at %d: %s\n",
429                                        count, blk, error_message(retval));
430                 }
431                 if (!quiet) 
432                         fputs(backup, stdout);
433                 if (sync_kludge) {
434                         if (sync_kludge == 1)
435                                 sync();
436                         else if ((i % sync_kludge) == 0)
437                                 sync();
438                 }
439         }
440         free(buf);
441         if (!quiet)
442                 fputs("done                            \n", stdout);
443 }
444
445 static void create_root_dir(ext2_filsys fs)
446 {
447         errcode_t       retval;
448         struct ext2_inode       inode;
449
450         retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
451         if (retval) {
452                 com_err("ext2fs_mkdir", retval, "while creating root dir");
453                 exit(1);
454         }
455         if (geteuid()) {
456                 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
457                 if (retval) {
458                         com_err("ext2fs_read_inode", retval,
459                                 "while reading root inode");
460                         exit(1);
461                 }
462                 inode.i_uid = getuid();
463                 if (inode.i_uid)
464                         inode.i_gid = getgid();
465                 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
466                 if (retval) {
467                         com_err("ext2fs_write_inode", retval,
468                                 "while setting root inode ownership");
469                         exit(1);
470                 }
471         }
472 }
473
474 static void create_lost_and_found(ext2_filsys fs)
475 {
476         errcode_t               retval;
477         ino_t                   ino;
478         const char              *name = "lost+found";
479         int                     i;
480         int                     lpf_size = 0;
481
482         retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
483         if (retval) {
484                 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
485                 exit(1);
486         }
487
488         retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
489         if (retval) {
490                 com_err("ext2_lookup", retval, "while looking up /lost+found");
491                 exit(1);
492         }
493         
494         for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
495                 if ((lpf_size += fs->blocksize) >= 16*1024)
496                         break;
497                 retval = ext2fs_expand_dir(fs, ino);
498                 if (retval) {
499                         com_err("ext2fs_expand_dir", retval,
500                                 "while expanding /lost+found");
501                         exit(1);
502                 }
503         }               
504 }
505
506 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
507 {
508         errcode_t       retval;
509         
510         ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
511         fs->group_desc[0].bg_free_inodes_count--;
512         fs->super->s_free_inodes_count--;
513         retval = ext2fs_update_bb_inode(fs, bb_list);
514         if (retval) {
515                 com_err("ext2fs_update_bb_inode", retval,
516                         "while setting bad block inode");
517                 exit(1);
518         }
519
520 }
521
522 static void reserve_inodes(ext2_filsys fs)
523 {
524         ino_t   i;
525         int     group;
526
527         for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
528                 ext2fs_mark_inode_bitmap(fs->inode_map, i);
529                 group = ext2fs_group_of_ino(fs, i);
530                 fs->group_desc[group].bg_free_inodes_count--;
531                 fs->super->s_free_inodes_count--;
532         }
533         ext2fs_mark_ib_dirty(fs);
534 }
535
536 #ifdef ZAP_BOOTBLOCK
537 static void zap_bootblock(ext2_filsys fs)
538 {
539         char buf[512];
540         int retval;
541
542         memset(buf, 0, 512);
543         
544         retval = io_channel_write_blk(fs->io, 0, -512, buf);
545         if (retval)
546                 printf("Warning: could not erase block 0: %s\n", 
547                        error_message(retval));
548 }
549 #endif
550         
551
552 static void show_stats(ext2_filsys fs)
553 {
554         struct ext2fs_sb        *s = (struct ext2fs_sb *) fs->super;
555         char                    buf[80];
556         blk_t                   group_block;
557         int                     i, need, col_left;
558         
559         if (param.s_blocks_count != s->s_blocks_count)
560                 printf("warning: %d blocks unused.\n\n",
561                        param.s_blocks_count - s->s_blocks_count);
562
563         memset(buf, 0, sizeof(buf));
564         strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
565         printf("Filesystem label=%s\n", buf);
566         printf("OS type: ");
567         switch (fs->super->s_creator_os) {
568             case EXT2_OS_LINUX: printf ("Linux"); break;
569             case EXT2_OS_HURD:  printf ("GNU/hurd");   break;
570             case EXT2_OS_MASIX: printf ("Masix"); break;
571             default:            printf ("(unknown os)");
572         }
573         printf("\n");
574         printf("Block size=%u (log=%u)\n", fs->blocksize,
575                 s->s_log_block_size);
576         printf("Fragment size=%u (log=%u)\n", fs->fragsize,
577                 s->s_log_frag_size);
578         printf("%u inodes, %u blocks\n", s->s_inodes_count,
579                s->s_blocks_count);
580         printf("%u blocks (%2.2f%%) reserved for the super user\n",
581                 s->s_r_blocks_count,
582                100.0 * s->s_r_blocks_count / s->s_blocks_count);
583         printf("First data block=%u\n", s->s_first_data_block);
584         printf("%lu block group%s\n", fs->group_desc_count,
585                 (fs->group_desc_count > 1) ? "s" : "");
586         printf("%u blocks per group, %u fragments per group\n",
587                s->s_blocks_per_group, s->s_frags_per_group);
588         printf("%u inodes per group\n", s->s_inodes_per_group);
589
590         if (fs->group_desc_count == 1) {
591                 printf("\n");
592                 return;
593         }
594         
595         printf("Superblock backups stored on blocks: ");
596         group_block = s->s_first_data_block;
597         col_left = 0;
598         for (i = 1; i < fs->group_desc_count; i++) {
599                 group_block += s->s_blocks_per_group;
600                 if (!ext2fs_bg_has_super(fs, i))
601                         continue;
602                 need = log10(group_block) + 2;
603                 if (need > col_left) {
604                         printf("\n\t");
605                         col_left = 72;
606                 }
607                 col_left -= need;
608                 printf("%u", group_block);
609                 if (i != fs->group_desc_count - 1)
610                         printf(", ");
611         }
612         printf("\n\n");
613 }
614
615 #ifndef HAVE_STRCASECMP
616 static int strcasecmp (char *s1, char *s2)
617 {
618         while (*s1 && *s2) {
619                 int ch1 = *s1++, ch2 = *s2++;
620                 if (isupper (ch1))
621                         ch1 = tolower (ch1);
622                 if (isupper (ch2))
623                         ch2 = tolower (ch2);
624                 if (ch1 != ch2)
625                         return ch1 - ch2;
626         }
627         return *s1 ? 1 : *s2 ? -1 : 0;
628 }
629 #endif
630
631 /*
632  * Set the S_CREATOR_OS field.  Return true if OS is known,
633  * otherwise, 0.
634  */
635 static int set_os(struct ext2_super_block *sb, char *os)
636 {
637         if (isdigit (*os))
638                 sb->s_creator_os = atoi (os);
639         else if (strcasecmp(os, "linux") == 0)
640                 sb->s_creator_os = EXT2_OS_LINUX;
641         else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
642                 sb->s_creator_os = EXT2_OS_HURD;
643         else if (strcasecmp(os, "masix") == 0)
644                 sb->s_creator_os = EXT2_OS_MASIX;
645         else
646                 return 0;
647         return 1;
648 }
649
650 #define PATH_SET "PATH=/sbin"
651
652 static void parse_raid_opts(const char *opts)
653 {
654         char    *buf, *token, *next, *p, *arg;
655         int     len;
656         int     raid_usage = 0;
657
658         len = strlen(opts);
659         buf = malloc(len+1);
660         if (!buf) {
661                 fprintf(stderr, "Couldn't allocate memory to parse "
662                         "raid options!\n");
663                 exit(1);
664         }
665         strcpy(buf, opts);
666         for (token = buf; token && *token; token = next) {
667                 p = strchr(token, ',');
668                 next = 0;
669                 if (p) {
670                         *p = 0;
671                         next = p+1;
672                 } 
673                 arg = strchr(token, '=');
674                 if (arg) {
675                         *arg = 0;
676                         arg++;
677                 }
678                 if (strcmp(token, "stride") == 0) {
679                         if (!arg) {
680                                 raid_usage++;
681                                 continue;
682                         }
683                         fs_stride = strtoul(arg, &p, 0);
684                         if (*p || (fs_stride == 0)) {
685                                 fprintf(stderr, "Invalid stride parameter.\n");
686                                 raid_usage++;
687                                 continue;
688                         }
689                 } else
690                         raid_usage++;
691         }
692         if (raid_usage) {
693                 fprintf(stderr, "\nBad raid options specified.\n\n"
694                         "Raid options are separated by commas, "
695                         "and may take an argument which\n"
696                         "\tis set off by an equals ('=') sign.\n\n"
697                         "Valid raid options are:\n"
698                         "\tstride=<stride length in blocks>\n\n");
699                 exit(1);
700         }
701 }       
702
703
704
705 static void PRS(int argc, char *argv[])
706 {
707         int     c;
708         int     size;
709         char    * tmp;
710         blk_t   max = 8192;
711         int     blocksize = 0;
712         int     inode_ratio = 0;
713         int     reserved_ratio = 5;
714         ino_t   num_inodes = 0;
715         errcode_t       retval;
716         int     sparse_option = 1;
717         char    *oldpath = getenv("PATH");
718         struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) &param;
719         char    *raid_opts = 0;
720         char    *fs_type = 0;
721         blk_t   dev_size;
722 #ifdef linux
723         struct utsname ut;
724
725         if (uname(&ut)) {
726                 perror("uname");
727                 exit(1);
728         }
729         if (ut.release[0] == '2' && ut.release[1] == '.' &&
730             ut.release[2] < '2' && ut.release[3] == '.')
731                 sparse_option = 0;
732 #endif
733         /* Update our PATH to include /sbin  */
734         if (oldpath) {
735                 char *newpath;
736                 
737                 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
738                 strcpy (newpath, PATH_SET);
739                 strcat (newpath, ":");
740                 strcat (newpath, oldpath);
741                 putenv (newpath);
742         } else
743                 putenv (PATH_SET);
744
745         setbuf(stdout, NULL);
746         setbuf(stderr, NULL);
747         initialize_ext2_error_table();
748         memset(&param, 0, sizeof(struct ext2_super_block));
749         param.s_rev_level = 1;  /* Create revision 1 filesystems now */
750         
751         fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
752                  E2FSPROGS_VERSION, E2FSPROGS_DATE,
753                  EXT2FS_VERSION, EXT2FS_DATE);
754         if (argc && *argv)
755                 program_name = *argv;
756         while ((c = getopt (argc, argv,
757                             "b:cf:g:i:l:m:no:qr:R:s:tvI:ST:FL:M:N:V")) != EOF)
758                 switch (c) {
759                 case 'b':
760                         blocksize = strtoul(optarg, &tmp, 0);
761                         if (blocksize < 1024 || blocksize > 4096 || *tmp) {
762                                 com_err(program_name, 0, "bad block size - %s",
763                                         optarg);
764                                 exit(1);
765                         }
766                         param.s_log_block_size =
767                                 log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
768                         max = blocksize * 8;
769                         break;
770                 case 'c':
771                 case 't':       /* Check for bad blocks */
772                         cflag = 1;
773                         break;
774                 case 'f':
775                         size = strtoul(optarg, &tmp, 0);
776                         if (size < 1024 || size > 4096 || *tmp) {
777                                 com_err(program_name, 0, "bad fragment size - %s",
778                                         optarg);
779                                 exit(1);
780                         }
781                         param.s_log_frag_size =
782                                 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
783                         printf("Warning: fragments not supported.  "
784                                "Ignoring -f option\n");
785                         break;
786                 case 'g':
787                         param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
788                         if (*tmp) {
789                                 com_err(program_name, 0,
790                                         "Illegal number for blocks per group");
791                                 exit(1);
792                         }
793                         if ((param.s_blocks_per_group % 8) != 0) {
794                                 com_err(program_name, 0,
795                                 "blocks per group must be multiple of 8");
796                                 exit(1);
797                         }
798                         break;
799                 case 'i':
800                         inode_ratio = strtoul(optarg, &tmp, 0);
801                         if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
802                             *tmp) {
803                                 com_err(program_name, 0, "bad inode ratio - %s",
804                                         optarg);
805                                 exit(1);
806                         }
807                         break;
808                 case 'l':
809                         bad_blocks_filename = malloc(strlen(optarg)+1);
810                         if (!bad_blocks_filename) {
811                                 com_err(program_name, ENOMEM,
812                                         "in malloc for bad_blocks_filename");
813                                 exit(1);
814                         }
815                         strcpy(bad_blocks_filename, optarg);
816                         break;
817                 case 'm':
818                         reserved_ratio = strtoul(optarg, &tmp, 0);
819                         if (reserved_ratio > 50 || *tmp) {
820                                 com_err(program_name, 0,
821                                         "bad reserved blocks percent - %s",
822                                         optarg);
823                                 exit(1);
824                         }
825                         break;
826                 case 'n':
827                         noaction++;
828                         break;
829                 case 'o':
830                         creator_os = optarg;
831                         break;
832                 case 'r':
833                         param.s_rev_level = atoi(optarg);
834                         break;
835                 case 's':
836                         sparse_option = atoi(optarg);
837                         break;
838 #ifdef EXT2_DYNAMIC_REV
839                 case 'I':
840                         param.s_inode_size = atoi(optarg);
841                         break;
842 #endif
843                 case 'N':
844                         num_inodes = atoi(optarg);
845                         break;
846                 case 'v':
847                         verbose = 1;
848                         break;
849                 case 'q':
850                         quiet = 1;
851                         break;
852                 case 'F':
853                         force = 1;
854                         break;
855                 case 'L':
856                         volume_label = optarg;
857                         break;
858                 case 'M':
859                         mount_dir = optarg;
860                         break;
861                 case 'R':
862                         raid_opts = optarg;
863                         break;
864                 case 'S':
865                         super_only = 1;
866                         break;
867                 case 'T':
868                         fs_type = optarg;
869                         break;
870                 case 'V':
871                         /* Print version number and exit */
872                         fprintf(stderr, "\tUsing %s\n",
873                                 error_message(EXT2_ET_BASE));
874                         exit(0);
875                 default:
876                         usage();
877                 }
878         if (optind == argc)
879                 usage();
880         device_name = argv[optind];
881         optind++;
882         if (optind < argc) {
883                 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
884                 if (*tmp) {
885                         com_err(program_name, 0, "bad blocks count - %s",
886                                 argv[optind - 1]);
887                         exit(1);
888                 }
889         }
890         if (optind < argc)
891                 usage();
892
893         if (raid_opts)
894                 parse_raid_opts(raid_opts);
895
896         if (!force)
897                 check_plausibility();
898         check_mount();
899
900         param.s_log_frag_size = param.s_log_block_size;
901
902         if (noaction && param.s_blocks_count) {
903                 dev_size = param.s_blocks_count;
904                 retval = 0;
905         } else
906                 retval = ext2fs_get_device_size(device_name,
907                                                 EXT2_BLOCK_SIZE(&param),
908                                                 &dev_size);
909         if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
910                 com_err(program_name, retval,
911                         "while trying to determine filesystem size");
912                 exit(1);
913         }
914         if (!param.s_blocks_count) {
915                 if (retval == EXT2_ET_UNIMPLEMENTED) {
916                         com_err(program_name, 0,
917                                 "Couldn't determine device size; you "
918                                 "must specify\nthe size of the "
919                                 "filesystem\n");
920                         exit(1);
921                 } else
922                         param.s_blocks_count = dev_size;
923         } else if (!force && (param.s_blocks_count > dev_size)) {
924                 com_err(program_name, 0,
925                         "Filesystem larger than apparent filesystem size.");
926                 proceed_question();
927         }
928
929         set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
930
931         if (param.s_blocks_per_group) {
932                 if (param.s_blocks_per_group < 256 ||
933                     param.s_blocks_per_group > max || *tmp) {
934                         com_err(program_name, 0,
935                                 "blocks per group count out of range");
936                         exit(1);
937                 }
938         }
939
940         /*
941          * Calculate number of inodes based on the inode ratio
942          */
943         param.s_inodes_count = num_inodes ? num_inodes : 
944                 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
945                         / inode_ratio;
946
947         /*
948          * Calculate number of blocks to reserve
949          */
950         param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
951
952 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
953         if (sparse_option)
954                 param_ext2->s_feature_ro_compat |=
955                         EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
956 #endif
957 }
958                                         
959 int main (int argc, char *argv[])
960 {
961         errcode_t       retval = 0;
962         ext2_filsys     fs;
963         badblocks_list  bb_list = 0;
964         struct ext2fs_sb *s;
965         
966         PRS(argc, argv);
967
968         /*
969          * Initialize the superblock....
970          */
971         retval = ext2fs_initialize(device_name, 0, &param,
972                                    unix_io_manager, &fs);
973         if (retval) {
974                 com_err(device_name, retval, "while setting up superblock");
975                 exit(1);
976         }
977
978         /*
979          * Generate a UUID for it...
980          */
981         s = (struct ext2fs_sb *) fs->super;
982         uuid_generate(s->s_uuid);
983
984         /*
985          * Override the creator OS, if applicable
986          */
987         if (creator_os && !set_os(fs->super, creator_os)) {
988                 com_err (program_name, 0, "unknown os - %s", creator_os);
989                 exit(1);
990         }
991
992         /*
993          * Set the volume label...
994          */
995         if (volume_label) {
996                 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
997                 strncpy(s->s_volume_name, volume_label,
998                         sizeof(s->s_volume_name));
999         }
1000
1001         /*
1002          * Set the last mount directory
1003          */
1004         if (mount_dir) {
1005                 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1006                 strncpy(s->s_last_mounted, mount_dir,
1007                         sizeof(s->s_last_mounted));
1008         }
1009         
1010         if (!quiet || noaction)
1011                 show_stats(fs);
1012
1013         if (noaction)
1014                 exit(0);
1015
1016         if (bad_blocks_filename)
1017                 read_bb_file(fs, &bb_list, bad_blocks_filename);
1018         if (cflag)
1019                 test_disk(fs, &bb_list);
1020
1021         handle_bad_blocks(fs, bb_list);
1022         fs->stride = fs_stride;
1023         retval = ext2fs_allocate_tables(fs);
1024         if (retval) {
1025                 com_err(program_name, retval,
1026                         "while trying to allocate filesystem tables");
1027                 exit(1);
1028         }
1029         if (super_only) {
1030                 fs->super->s_state |= EXT2_ERROR_FS;
1031                 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1032         } else {
1033                 write_inode_tables(fs);
1034                 create_root_dir(fs);
1035                 create_lost_and_found(fs);
1036                 reserve_inodes(fs);
1037                 create_bad_block_inode(fs, bb_list);
1038 #ifdef ZAP_BOOTBLOCK
1039                 zap_bootblock(fs);
1040 #endif
1041         }
1042         
1043         if (!quiet)
1044                 printf("Writing superblocks and "
1045                        "filesystem accounting information: ");
1046         retval = ext2fs_flush(fs);
1047         if (retval) {
1048                 printf("\nWarning, had trouble writing out superblocks.");
1049         }
1050         if (!quiet)
1051                 printf("done\n");
1052         ext2fs_close(fs);
1053         return 0;
1054 }