1 /* vi: set sw=4 ts=4: */
3 * mke2fs.c - Make a ext2fs filesystem.
5 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
6 * 2003, 2004, 2005 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
12 /* Usage: mke2fs [options] device
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 :-).
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
32 #include "ext2fs/ext2_fs.h"
33 #include "uuid/uuid.h"
35 #include "ext2fs/ext2fs.h"
38 #define STRIDE_LENGTH 8
44 static const char * device_name;
46 /* Command line options */
49 static int super_only;
52 static int journal_size;
53 static int journal_flags;
54 static const char *bad_blocks_filename;
55 static __u32 fs_stride;
57 static struct ext2_super_block param;
58 static char *creator_os;
59 static char *volume_label;
60 static char *mount_dir;
61 static char *journal_device = NULL;
62 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
64 static int sys_page_size = 4096;
65 static int linux_version_code = 0;
67 static int int_log2(int arg)
79 static int int_log10(unsigned int arg)
89 * This function sets the default parameters for a filesystem
91 * The type is specified by the user. The size is the maximum size
92 * (in megabytes) for which a set of parameters applies, with a size
93 * of zero meaning that it is the default parameter for the type.
94 * Note that order is important in the table below.
96 #define DEF_MAX_BLOCKSIZE -1
97 static const char default_str[] = "default";
98 struct mke2fs_defaults {
105 static const struct mke2fs_defaults settings[] = {
106 { default_str, 0, 4096, 8192 },
107 { default_str, 512, 1024, 4096 },
108 { default_str, 3, 1024, 8192 },
109 { "journal", 0, 4096, 8192 },
110 { "news", 0, 4096, 4096 },
111 { "largefile", 0, 4096, 1024 * 1024 },
112 { "largefile4", 0, 4096, 4096 * 1024 },
116 static void set_fs_defaults(const char *fs_type,
117 struct ext2_super_block *super,
118 int blocksize, int sector_size,
123 const struct mke2fs_defaults *p;
124 int use_bsize = 1024;
126 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
128 ratio = *inode_ratio;
130 fs_type = default_str;
131 for (p = settings; p->type; p++) {
132 if ((strcmp(p->type, fs_type) != 0) &&
133 (strcmp(p->type, default_str) != 0))
135 if ((p->size != 0) && (megs > p->size))
138 *inode_ratio = p->inode_ratio < blocksize ?
139 blocksize : p->inode_ratio;
140 use_bsize = p->blocksize;
142 if (blocksize <= 0) {
143 if (use_bsize == DEF_MAX_BLOCKSIZE) {
144 use_bsize = sys_page_size;
145 if ((linux_version_code < (2*65536 + 6*256)) &&
149 if (sector_size && use_bsize < sector_size)
150 use_bsize = sector_size;
151 if ((blocksize < 0) && (use_bsize < (-blocksize)))
152 use_bsize = -blocksize;
153 blocksize = use_bsize;
154 super->s_blocks_count /= blocksize / 1024;
156 super->s_log_frag_size = super->s_log_block_size =
157 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
162 * Helper function for read_bb_file and test_disk
164 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
166 bb_error_msg("Bad block %u out of range; ignored", blk);
173 static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)__attribute__ ((format (printf, 2, 3)));
174 static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)
180 bb_fprintf(stderr,"\nCould not ");
181 bb_vfprintf(stderr, fmt, ap);
182 bb_fprintf(stderr, "\n");
188 static void mke2fs_verbose(const char *fmt, ...)__attribute__ ((format (printf, 1, 2)));
189 static void mke2fs_verbose(const char *fmt, ...)
195 bb_vfprintf(stdout, fmt, ap);
201 static void mke2fs_verbose_done(void)
203 mke2fs_verbose("done\n");
206 static void mke2fs_warning_msg(int retval, char *fmt, ... )__attribute__ ((format (printf, 2, 3)));
207 static void mke2fs_warning_msg(int retval, char *fmt, ... )
213 bb_fprintf(stderr,"\nWarning: ");
214 bb_vfprintf(stderr, fmt, ap);
215 bb_fprintf(stderr, "\n");
221 * Reads the bad blocks list from a file
223 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
224 const char *bad_blocks_file)
229 f = xfopen(bad_blocks_file, "r");
230 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
232 mke2fs_error_msg_and_die(retval, "read bad blocks from list");
236 * Runs the badblocks program to test the disk
238 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
244 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
245 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
246 fs->device_name, fs->super->s_blocks_count);
247 mke2fs_verbose("Running command: %s\n", buf);
250 bb_perror_msg_and_die("Could not run '%s'", buf);
252 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
254 mke2fs_error_msg_and_die(retval, "read bad blocks from program");
257 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
261 unsigned must_be_good;
263 badblocks_iterate bb_iter;
273 * The primary superblock and group descriptors *must* be
274 * good; if not, abort.
276 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
277 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
278 if (ext2fs_badblocks_list_test(bb_list, i)) {
279 bb_error_msg_and_die(
280 "Block %d in primary superblock/group descriptor area bad\n"
281 "Blocks %d through %d must be good in order to build a filesystem\n"
282 "Aborting ...", i, fs->super->s_first_data_block, must_be_good);
287 * See if any of the bad blocks are showing up in the backup
288 * superblocks and/or group descriptors. If so, issue a
289 * warning and adjust the block counts appropriately.
291 group_block = fs->super->s_first_data_block +
292 fs->super->s_blocks_per_group;
294 for (i = 1; i < fs->group_desc_count; i++) {
296 for (j=0; j < fs->desc_blocks+1; j++) {
297 if (ext2fs_badblocks_list_test(bb_list,
299 mke2fs_warning_msg(!group_bad,
300 "the backup superblock/group descriptors at block %d contain\n"
301 "bad blocks\n", group_block);
303 group = ext2fs_group_of_blk(fs, group_block+j);
304 fs->group_desc[group].bg_free_blocks_count++;
305 fs->super->s_free_blocks_count++;
308 group_block += fs->super->s_blocks_per_group;
312 * Mark all the bad blocks as used...
314 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
315 mke2fs_error_msg_and_die(retval, "mark bad blocks as used");
317 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
318 ext2fs_mark_block_bitmap(fs->block_map, blk);
319 ext2fs_badblocks_list_iterate_end(bb_iter);
323 * These functions implement a generalized progress meter.
325 struct progress_struct {
332 static void progress_init(struct progress_struct *progress,
333 const char *label,__u32 max)
337 memset(progress, 0, sizeof(struct progress_struct));
342 * Figure out how many digits we need
345 sprintf(progress->format, "%%%dd/%%%dld", i, i);
346 memset(progress->backup, '\b', sizeof(progress->backup)-1);
347 progress->backup[sizeof(progress->backup)-1] = 0;
348 if ((2*i)+1 < (int) sizeof(progress->backup))
349 progress->backup[(2*i)+1] = 0;
352 progress->skip_progress = 0;
353 if (getenv("MKE2FS_SKIP_PROGRESS"))
354 progress->skip_progress++;
356 fputs(label, stdout);
360 static void progress_update(struct progress_struct *progress, __u32 val)
362 if ((progress->format[0] == 0) || progress->skip_progress)
364 printf(progress->format, val, progress->max);
365 fputs(progress->backup, stdout);
368 static void progress_close(struct progress_struct *progress)
370 if (progress->format[0] == 0)
372 printf("%-28s\n", "done");
377 * Helper function which zeros out _num_ blocks starting at _blk_. In
378 * case of an error, the details of the error is returned via _ret_blk_
379 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
380 * success, and an error code on an error.
382 * As a special case, if the first argument is NULL, then it will
383 * attempt to free the static zeroizing buffer. (This is to keep
384 * programs that check for memory leaks happy.)
386 static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
387 struct progress_struct *progress,
388 blk_t *ret_blk, int *ret_count)
390 int j, count, next_update, next_update_incr;
394 /* If fs is null, clean up the static buffer and return */
402 /* Allocate the zeroizing buffer if necessary */
404 buf = xzalloc(fs->blocksize * STRIDE_LENGTH);
406 /* OK, do the write loop */
408 next_update_incr = num / 100;
409 if (next_update_incr < 1)
410 next_update_incr = 1;
411 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
413 if (count > STRIDE_LENGTH)
414 count = STRIDE_LENGTH;
415 retval = io_channel_write_blk(fs->io, blk, count, buf);
423 if (progress && j > next_update) {
424 next_update += num / 100;
425 progress_update(progress, blk);
431 static void write_inode_tables(ext2_filsys fs)
437 struct progress_struct progress;
440 memset(&progress, 0, sizeof(progress));
442 progress_init(&progress, "Writing inode tables: ",
443 fs->group_desc_count);
445 for (i = 0; i < fs->group_desc_count; i++) {
446 progress_update(&progress, i);
448 blk = fs->group_desc[i].bg_inode_table;
449 num = fs->inode_blocks_per_group;
451 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
452 mke2fs_error_msg_and_die(retval,
453 "write %d blocks in inode table starting at %d.",
456 if (sync_kludge == 1)
458 else if ((i % sync_kludge) == 0)
462 zero_blocks(0, 0, 0, 0, 0, 0);
463 progress_close(&progress);
466 static void create_root_dir(ext2_filsys fs)
469 struct ext2_inode inode;
471 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
472 mke2fs_error_msg_and_die(retval, "create root dir");
474 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
475 mke2fs_error_msg_and_die(retval, "read root inode");
476 inode.i_uid = getuid();
478 inode.i_gid = getgid();
479 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
480 mke2fs_error_msg_and_die(retval, "set root inode ownership");
484 static void create_lost_and_found(ext2_filsys fs)
488 const char *name = "lost+found";
490 char *msg = "create";
494 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
496 goto CREATE_LOST_AND_FOUND_ERROR;
499 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
502 goto CREATE_LOST_AND_FOUND_ERROR;
505 for (; i < EXT2_NDIR_BLOCKS; i++) {
506 if ((lpf_size += fs->blocksize) >= 16*1024)
508 retval = ext2fs_expand_dir(fs, ino);
510 CREATE_LOST_AND_FOUND_ERROR:
511 mke2fs_error_msg_and_die(retval, "%s %s", msg, name);
515 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
519 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
520 fs->group_desc[0].bg_free_inodes_count--;
521 fs->super->s_free_inodes_count--;
522 retval = ext2fs_update_bb_inode(fs, bb_list);
523 mke2fs_error_msg_and_die(retval, "set bad block inode");
526 static void reserve_inodes(ext2_filsys fs)
531 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
532 ext2fs_mark_inode_bitmap(fs->inode_map, i);
533 group = ext2fs_group_of_ino(fs, i);
534 fs->group_desc[group].bg_free_inodes_count--;
535 fs->super->s_free_inodes_count--;
537 ext2fs_mark_ib_dirty(fs);
540 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
541 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
542 #define BSD_LABEL_OFFSET 64
544 static void zap_sector(ext2_filsys fs, int sect, int nsect)
547 char *fmt = "could not %s %d";
551 buf = xmalloc(512*nsect);
554 /* Check for a BSD disklabel, and don't erase it if so */
555 retval = io_channel_read_blk(fs->io, 0, -512, buf);
557 mke2fs_warning_msg(retval, fmt, "read block", 0);
559 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
560 if ((*magic == BSD_DISKMAGIC) ||
561 (*magic == BSD_MAGICDISK))
566 memset(buf, 0, 512*nsect);
567 io_channel_set_blksize(fs->io, 512);
568 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
569 io_channel_set_blksize(fs->io, fs->blocksize);
571 mke2fs_warning_msg(retval, fmt, "erase sector", sect);
574 static void create_journal_dev(ext2_filsys fs)
576 struct progress_struct progress;
579 char *fmt = "%s journal superblock";
583 retval = ext2fs_create_journal_superblock(fs,
584 fs->super->s_blocks_count, 0, &buf);
585 mke2fs_error_msg_and_die(retval, fmt, "init");
587 memset(&progress, 0, sizeof(progress));
589 progress_init(&progress, "Zeroing journal device: ",
590 fs->super->s_blocks_count);
592 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
593 &progress, &blk, &count);
594 mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)",
596 zero_blocks(0, 0, 0, 0, 0, 0);
598 retval = io_channel_write_blk(fs->io,
599 fs->super->s_first_data_block+1,
601 mke2fs_error_msg_and_die(retval, fmt, "write");
602 progress_close(&progress);
605 static void show_stats(ext2_filsys fs)
607 struct ext2_super_block *s = fs->super;
613 mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count),
614 "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count);
615 os = e2p_os2string(fs->super->s_creator_os);
616 printf( "Filesystem label=%.*s\n"
618 "Block size=%u (log=%u)\n"
619 "Fragment size=%u (log=%u)\n"
620 "%u inodes, %u blocks\n"
621 "%u blocks (%2.2f%%) reserved for the super user\n"
622 "First data block=%u\n",
623 (int) sizeof(s->s_volume_name),
626 fs->blocksize, s->s_log_block_size,
627 fs->fragsize, s->s_log_frag_size,
628 s->s_inodes_count, s->s_blocks_count,
629 s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count,
630 s->s_first_data_block);
632 if (s->s_reserved_gdt_blocks) {
633 printf("Maximum filesystem blocks=%lu\n",
634 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
635 (fs->blocksize / sizeof(struct ext2_group_desc)) *
636 s->s_blocks_per_group);
638 printf( "%u block group%s\n"
639 "%u blocks per group, %u fragments per group\n"
640 "%u inodes per group\n",
641 fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "",
642 s->s_blocks_per_group, s->s_frags_per_group,
643 s->s_inodes_per_group);
644 if (fs->group_desc_count == 1) {
649 printf("Superblock backups stored on blocks: ");
650 group_block = s->s_first_data_block;
652 for (i = 1; i < fs->group_desc_count; i++) {
653 group_block += s->s_blocks_per_group;
654 if (!ext2fs_bg_has_super(fs, i))
658 need = int_log10(group_block) + 2;
659 if (need > col_left) {
664 printf("%u", group_block);
670 * Set the S_CREATOR_OS field. Return true if OS is known,
673 static int set_os(struct ext2_super_block *sb, char *os)
676 sb->s_creator_os = atoi (os);
680 if((sb->s_creator_os = e2p_string2os(os)) >= 0) {
682 } else if (!strcasecmp("GNU", os)) {
683 sb->s_creator_os = EXT2_OS_HURD;
689 static void parse_extended_opts(struct ext2_super_block *sb_param,
692 char *buf, *token, *next, *p, *arg;
696 for (token = buf; token && *token; token = next) {
697 p = strchr(token, ',');
703 arg = strchr(token, '=');
708 if (strcmp(token, "stride") == 0) {
713 fs_stride = strtoul(arg, &p, 0);
714 if (*p || (fs_stride == 0)) {
715 bb_error_msg("Invalid stride parameter: %s", arg);
719 } else if (!strcmp(token, "resize")) {
720 unsigned long resize, bpg, rsv_groups;
721 unsigned long group_desc_count, desc_blocks;
722 unsigned int gdpb, blocksize;
730 resize = parse_num_blocks(arg,
731 sb_param->s_log_block_size);
734 bb_error_msg("Invalid resize parameter: %s", arg);
738 if (resize <= sb_param->s_blocks_count) {
739 bb_error_msg("The resize maximum must be greater "
740 "than the filesystem size");
745 blocksize = EXT2_BLOCK_SIZE(sb_param);
746 bpg = sb_param->s_blocks_per_group;
749 gdpb = blocksize / sizeof(struct ext2_group_desc);
750 group_desc_count = (sb_param->s_blocks_count +
752 desc_blocks = (group_desc_count +
754 rsv_groups = (resize + bpg - 1) / bpg;
755 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
757 if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param))
758 rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param);
761 sb_param->s_feature_compat |=
762 EXT2_FEATURE_COMPAT_RESIZE_INODE;
764 sb_param->s_reserved_gdt_blocks = rsv_gdb;
770 bb_error_msg_and_die(
771 "\nBad options specified.\n\n"
772 "Extended options are separated by commas, "
773 "and may take an argument which\n"
774 "\tis set off by an equals ('=') sign.\n\n"
775 "Valid extended options are:\n"
776 "\tstride=<stride length in blocks>\n"
777 "\tresize=<resize maximum size in blocks>\n");
781 static __u32 ok_features[3] = {
782 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
783 EXT2_FEATURE_COMPAT_RESIZE_INODE |
784 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
785 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
786 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
787 EXT2_FEATURE_INCOMPAT_META_BG,
788 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
791 static int PRS(int argc, char *argv[])
799 int reserved_ratio = 5;
801 int show_version_only = 0;
802 ext2_ino_t num_inodes = 0;
804 char * extended_opts = 0;
805 const char * fs_type = 0;
809 /* Update our PATH to include /sbin */
810 e2fs_set_sbin_path();
812 tmp = getenv("MKE2FS_SYNC");
814 sync_kludge = atoi(tmp);
816 /* Determine the system page size if possible */
817 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
818 #define _SC_PAGESIZE _SC_PAGE_SIZE
821 sysval = sysconf(_SC_PAGESIZE);
823 sys_page_size = sysval;
824 #endif /* _SC_PAGESIZE */
826 setbuf(stdout, NULL);
827 setbuf(stderr, NULL);
828 memset(¶m, 0, sizeof(struct ext2_super_block));
829 param.s_rev_level = 1; /* Create revision 1 filesystems now */
830 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
831 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
834 linux_version_code = get_linux_version_code();
835 if (linux_version_code && linux_version_code < KERNEL_VERSION(2,2,0)) {
836 param.s_rev_level = 0;
837 param.s_feature_incompat = 0;
838 param.s_feature_compat = 0;
839 param.s_feature_ro_compat = 0;
843 /* If called as mkfs.ext3, create a journal inode */
844 if (last_char_is(bb_applet_name, '3'))
847 while ((c = getopt (argc, argv,
848 "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
851 if (safe_strtoi(optarg, &blocksize))
852 goto BLOCKSIZE_ERROR;
853 b = (blocksize > 0) ? blocksize : -blocksize;
854 if (b < EXT2_MIN_BLOCK_SIZE ||
855 b > EXT2_MAX_BLOCK_SIZE) {
857 bb_error_msg_and_die("invalid block size - %s", optarg);
859 mke2fs_warning_msg((blocksize > 4096),
860 "blocksize %d not usable on most systems",
863 param.s_log_block_size =
864 int_log2(blocksize >>
865 EXT2_MIN_BLOCK_LOG_SIZE);
867 case 'c': /* Check for bad blocks */
868 case 't': /* deprecated */
872 if (safe_strtoi(optarg, &size) || size < EXT2_MIN_BLOCK_SIZE || size > EXT2_MAX_BLOCK_SIZE ){
873 bb_error_msg_and_die("invalid fragment size - %s", optarg);
875 param.s_log_frag_size =
876 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
877 mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option");
882 if (safe_strtoi(optarg, &foo)) {
883 bb_error_msg_and_die("Illegal number for blocks per group");
885 param.s_blocks_per_group = foo;
887 if ((param.s_blocks_per_group % 8) != 0) {
888 bb_error_msg_and_die("blocks per group must be multiple of 8");
892 if (safe_strtoi(optarg, &inode_ratio)
893 || inode_ratio < EXT2_MIN_BLOCK_SIZE
894 || inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024) {
895 bb_error_msg_and_die("invalid inode ratio %s (min %d/max %d)",
896 optarg, EXT2_MIN_BLOCK_SIZE,
897 EXT2_MAX_BLOCK_SIZE);
901 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
904 param.s_feature_compat |=
905 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
910 bad_blocks_filename = optarg;
913 if (safe_strtoi(optarg, &reserved_ratio) || reserved_ratio > 50 ) {
914 bb_error_msg_and_die("invalid reserved blocks percent - %s", optarg);
924 param.s_rev_level = atoi(optarg);
925 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
926 param.s_feature_incompat = 0;
927 param.s_feature_compat = 0;
928 param.s_feature_ro_compat = 0;
931 case 's': /* deprecated */
933 param.s_feature_ro_compat |=
934 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
936 param.s_feature_ro_compat &=
937 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
939 #ifdef EXT2_DYNAMIC_REV
941 if (safe_strtoi(optarg, &inode_size)) {
942 bb_error_msg_and_die("invalid inode size - %s", optarg);
947 num_inodes = atoi(optarg);
959 volume_label = optarg;
965 if (!strcmp(optarg, "none")) {
966 param.s_feature_compat = 0;
967 param.s_feature_incompat = 0;
968 param.s_feature_ro_compat = 0;
971 if (e2p_edit_feature(optarg,
972 ¶m.s_feature_compat,
974 bb_error_msg_and_die("Invalid filesystem option set: %s", optarg);
979 extended_opts = optarg;
988 /* Print version number and exit */
989 show_version_only = 1;
996 if ((optind == argc) /*&& !show_version_only*/)
998 device_name = argv[optind++];
1000 mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1002 if (show_version_only) {
1007 * If there's no blocksize specified and there is a journal
1008 * device, use it to figure out the blocksize
1010 if (blocksize <= 0 && journal_device) {
1014 #ifdef CONFIG_TESTIO_DEBUG
1015 io_ptr = test_io_manager;
1016 test_io_backing_manager = unix_io_manager;
1018 io_ptr = unix_io_manager;
1020 retval = ext2fs_open(journal_device,
1021 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1023 mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device);
1024 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1025 bb_error_msg_and_die(
1026 "Journal dev blocksize (%d) smaller than "
1027 "minimum blocksize %d\n", jfs->blocksize,
1030 blocksize = jfs->blocksize;
1031 param.s_log_block_size =
1032 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1036 if (blocksize > sys_page_size) {
1037 mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)",
1038 blocksize, sys_page_size);
1042 bb_error_msg("Forced to continue");
1044 mke2fs_warning_msg(((blocksize > 4096) &&
1045 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)),
1046 "some 2.4 kernels do not support "
1047 "blocksizes greater than 4096 using ext3.\n"
1048 "Use -b 4096 if this is an issue for you\n");
1050 if (optind < argc) {
1051 param.s_blocks_count = parse_num_blocks(argv[optind++],
1052 param.s_log_block_size);
1053 mke2fs_error_msg_and_die(!param.s_blocks_count, "invalid blocks count - %s", argv[optind - 1]);
1058 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1060 fs_type = "journal";
1062 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1063 param.s_feature_compat = 0;
1064 param.s_feature_ro_compat = 0;
1066 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1067 (param.s_feature_compat || param.s_feature_ro_compat ||
1068 param.s_feature_incompat))
1069 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1071 check_plausibility(device_name , force);
1072 check_mount(device_name, force, "filesystem");
1074 param.s_log_frag_size = param.s_log_block_size;
1076 if (noaction && param.s_blocks_count) {
1077 dev_size = param.s_blocks_count;
1081 retval = ext2fs_get_device_size(device_name,
1082 EXT2_BLOCK_SIZE(¶m),
1084 if ((retval == EFBIG) &&
1086 (param.s_log_block_size == 0)) {
1087 param.s_log_block_size = 2;
1093 mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size");
1095 if (!param.s_blocks_count) {
1096 if (retval == EXT2_ET_UNIMPLEMENTED) {
1097 mke2fs_error_msg_and_die(1,
1098 "determine device size; you "
1099 "must specify\nthe size of the "
1102 if (dev_size == 0) {
1103 bb_error_msg_and_die(
1104 "Device size reported to be zero. "
1105 "Invalid partition specified, or\n\t"
1106 "partition table wasn't reread "
1107 "after running fdisk, due to\n\t"
1108 "a modified partition being busy "
1109 "and in use. You may need to reboot\n\t"
1110 "to re-read your partition table.\n"
1113 param.s_blocks_count = dev_size;
1114 if (sys_page_size > EXT2_BLOCK_SIZE(¶m))
1115 param.s_blocks_count &= ~((sys_page_size /
1116 EXT2_BLOCK_SIZE(¶m))-1);
1119 } else if (!force && (param.s_blocks_count > dev_size)) {
1120 bb_error_msg("Filesystem larger than apparent device size");
1125 * If the user asked for HAS_JOURNAL, then make sure a journal
1128 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1132 /* Set first meta blockgroup via an environment variable */
1133 /* (this is mostly for debugging purposes) */
1134 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1135 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1136 param.s_first_meta_bg = atoi(tmp);
1138 /* Get the hardware sector size, if available */
1139 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1140 mke2fs_error_msg_and_die(retval, "determine hardware sector size");
1142 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1143 sector_size = atoi(tmp);
1145 set_fs_defaults(fs_type, ¶m, blocksize, sector_size, &inode_ratio);
1146 blocksize = EXT2_BLOCK_SIZE(¶m);
1149 parse_extended_opts(¶m, extended_opts);
1151 /* Since sparse_super is the default, we would only have a problem
1152 * here if it was explicitly disabled.
1154 if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1155 !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1156 bb_error_msg_and_die("reserved online resize blocks not supported "
1157 "on non-sparse filesystem");
1160 if (param.s_blocks_per_group) {
1161 if (param.s_blocks_per_group < 256 ||
1162 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1163 bb_error_msg_and_die("blocks per group count out of range");
1167 if (!force && param.s_blocks_count >= (1 << 31)) {
1168 bb_error_msg_and_die("Filesystem too large. No more than 2**31-1 blocks\n"
1169 "\t (8TB using a blocksize of 4k) are currently supported.");
1173 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1174 inode_size > EXT2_BLOCK_SIZE(¶m) ||
1175 inode_size & (inode_size - 1)) {
1176 bb_error_msg_and_die("invalid inode size %d (min %d/max %d)",
1177 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1180 mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE),
1181 "%d-byte inodes not usable on most systems",
1183 param.s_inode_size = inode_size;
1187 * Calculate number of inodes based on the inode ratio
1189 param.s_inodes_count = num_inodes ? num_inodes :
1190 ((__u64) param.s_blocks_count * blocksize)
1194 * Calculate number of blocks to reserve
1196 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1200 static void mke2fs_clean_up(void)
1202 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
1205 int mke2fs_main (int argc, char *argv[])
1209 badblocks_list bb_list = 0;
1214 if (ENABLE_FEATURE_CLEAN_UP)
1215 atexit(mke2fs_clean_up);
1216 if(!PRS(argc, argv))
1219 #ifdef CONFIG_TESTIO_DEBUG
1220 io_ptr = test_io_manager;
1221 test_io_backing_manager = unix_io_manager;
1223 io_ptr = unix_io_manager;
1227 * Initialize the superblock....
1229 retval = ext2fs_initialize(device_name, 0, ¶m,
1231 mke2fs_error_msg_and_die(retval, "set up superblock");
1234 * Wipe out the old on-disk superblock
1237 zap_sector(fs, 2, 6);
1240 * Generate a UUID for it...
1242 uuid_generate(fs->super->s_uuid);
1245 * Initialize the directory index variables
1247 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1248 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1251 * Add "jitter" to the superblock's check interval so that we
1252 * don't check all the filesystems at the same time. We use a
1253 * kludgy hack of using the UUID to derive a random jitter value.
1255 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1256 val += fs->super->s_uuid[i];
1257 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1260 * Override the creator OS, if applicable
1262 if (creator_os && !set_os(fs->super, creator_os)) {
1263 bb_error_msg_and_die("unknown os - %s", creator_os);
1267 * For the Hurd, we will turn off filetype since it doesn't
1270 if (fs->super->s_creator_os == EXT2_OS_HURD)
1271 fs->super->s_feature_incompat &=
1272 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1275 * Set the volume label...
1278 snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label);
1282 * Set the last mount directory
1285 snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir);
1288 if (!quiet || noaction)
1294 if (fs->super->s_feature_incompat &
1295 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1296 create_journal_dev(fs);
1297 return (ext2fs_close(fs) ? 1 : 0);
1300 if (bad_blocks_filename)
1301 read_bb_file(fs, &bb_list, bad_blocks_filename);
1303 test_disk(fs, &bb_list);
1305 handle_bad_blocks(fs, bb_list);
1306 fs->stride = fs_stride;
1307 retval = ext2fs_allocate_tables(fs);
1308 mke2fs_error_msg_and_die(retval, "allocate filesystem tables");
1310 fs->super->s_state |= EXT2_ERROR_FS;
1311 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1313 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1314 unsigned int rsv = 65536 / fs->blocksize;
1315 unsigned long blocks = fs->super->s_blocks_count;
1316 unsigned long start;
1319 #ifdef ZAP_BOOTBLOCK
1320 zap_sector(fs, 0, 2);
1324 * Wipe out any old MD RAID (or other) metadata at the end
1325 * of the device. This will also verify that the device is
1326 * as large as we think. Be careful with very small devices.
1328 start = (blocks & ~(rsv - 1));
1332 retval = zero_blocks(fs, start, blocks - start,
1333 NULL, &ret_blk, NULL);
1335 mke2fs_warning_msg(retval, "could not zero block %u at end of filesystem", ret_blk);
1336 write_inode_tables(fs);
1337 create_root_dir(fs);
1338 create_lost_and_found(fs);
1340 create_bad_block_inode(fs, bb_list);
1341 if (fs->super->s_feature_compat &
1342 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1343 retval = ext2fs_create_resize_inode(fs);
1344 mke2fs_error_msg_and_die(retval, "reserve blocks for online resize");
1348 if (journal_device) {
1349 make_journal_device(journal_device, fs, quiet, force);
1350 } else if (journal_size) {
1351 make_journal_blocks(fs, journal_size, journal_flags, quiet);
1354 mke2fs_verbose("Writing superblocks and filesystem accounting information: ");
1355 retval = ext2fs_flush(fs);
1356 mke2fs_warning_msg(retval, "had trouble writing out superblocks");
1357 mke2fs_verbose_done();
1358 if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG"))
1359 print_check_message(fs);
1360 val = ext2fs_close(fs);
1361 return (retval || val) ? 1 : 0;