2 * mke2fs.c - Make a ext2fs filesystem.
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
13 /* Usage: mke2fs [options] device
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
33 #include "ext2fs/ext2_fs.h"
34 #include "uuid/uuid.h"
36 #include "ext2fs/ext2fs.h"
39 #define STRIDE_LENGTH 8
45 static const char * device_name /* = NULL */;
47 /* Command line options */
50 static int super_only;
53 static int journal_size;
54 static int journal_flags;
55 static const char *bad_blocks_filename;
56 static __u32 fs_stride;
58 static struct ext2_super_block param;
59 static char *creator_os;
60 static char *volume_label;
61 static char *mount_dir;
62 static char *journal_device;
63 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
65 static int sys_page_size = 4096;
66 static int linux_version_code = 0;
68 static int int_log2(int arg)
80 static int int_log10(unsigned int arg)
90 * This function sets the default parameters for a filesystem
92 * The type is specified by the user. The size is the maximum size
93 * (in megabytes) for which a set of parameters applies, with a size
94 * of zero meaning that it is the default parameter for the type.
95 * Note that order is important in the table below.
97 #define DEF_MAX_BLOCKSIZE -1
98 static const char default_str[] = "default";
99 struct mke2fs_defaults {
106 static const struct mke2fs_defaults settings[] = {
107 { default_str, 0, 4096, 8192 },
108 { default_str, 512, 1024, 4096 },
109 { default_str, 3, 1024, 8192 },
110 { "journal", 0, 4096, 8192 },
111 { "news", 0, 4096, 4096 },
112 { "largefile", 0, 4096, 1024 * 1024 },
113 { "largefile4", 0, 4096, 4096 * 1024 },
117 static void set_fs_defaults(const char *fs_type,
118 struct ext2_super_block *super,
119 int blocksize, int sector_size,
124 const struct mke2fs_defaults *p;
125 int use_bsize = 1024;
127 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
129 ratio = *inode_ratio;
131 fs_type = default_str;
132 for (p = settings; p->type; p++) {
133 if ((strcmp(p->type, fs_type) != 0) &&
134 (strcmp(p->type, default_str) != 0))
136 if ((p->size != 0) && (megs > p->size))
139 *inode_ratio = p->inode_ratio < blocksize ?
140 blocksize : p->inode_ratio;
141 use_bsize = p->blocksize;
143 if (blocksize <= 0) {
144 if (use_bsize == DEF_MAX_BLOCKSIZE) {
145 use_bsize = sys_page_size;
146 if ((linux_version_code < (2*65536 + 6*256)) &&
150 if (sector_size && use_bsize < sector_size)
151 use_bsize = sector_size;
152 if ((blocksize < 0) && (use_bsize < (-blocksize)))
153 use_bsize = -blocksize;
154 blocksize = use_bsize;
155 super->s_blocks_count /= blocksize / 1024;
157 super->s_log_frag_size = super->s_log_block_size =
158 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
163 * Helper function for read_bb_file and test_disk
165 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
167 bb_error_msg("Bad block %u out of range; ignored", blk);
172 * Reads the bad blocks list from a file
174 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
175 const char *bad_blocks_file)
180 f = fopen(bad_blocks_file, "r");
182 bb_perror_msg_and_die("Could not read bad blocks file %s", bad_blocks_file);
184 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
187 bb_error_msg_and_die("Could not read bad blocks list");
192 * Runs the badblocks program to test the disk
194 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
200 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
201 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
202 fs->device_name, fs->super->s_blocks_count);
204 printf("Running command: %s\n", buf);
207 bb_perror_msg_and_die("Could not run '%s'", buf);
209 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
212 bb_error_msg_and_die(
213 "Could not get list of bad blocks from program");
217 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
221 unsigned must_be_good;
223 badblocks_iterate bb_iter;
233 * The primary superblock and group descriptors *must* be
234 * good; if not, abort.
236 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
237 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
238 if (ext2fs_badblocks_list_test(bb_list, i)) {
239 bb_error_msg_and_die(
240 "Block %d in primary superblock/group descriptor area bad\n"
241 "Blocks %d through %d must be good in order to build a filesystem\n"
242 "Aborting ...", i, fs->super->s_first_data_block, must_be_good);
247 * See if any of the bad blocks are showing up in the backup
248 * superblocks and/or group descriptors. If so, issue a
249 * warning and adjust the block counts appropriately.
251 group_block = fs->super->s_first_data_block +
252 fs->super->s_blocks_per_group;
254 for (i = 1; i < fs->group_desc_count; i++) {
256 for (j=0; j < fs->desc_blocks+1; j++) {
257 if (ext2fs_badblocks_list_test(bb_list,
261 "Warning: the backup superblock/group descriptors at block %d contain\n"
262 " bad blocks\n", group_block);
264 group = ext2fs_group_of_blk(fs, group_block+j);
265 fs->group_desc[group].bg_free_blocks_count++;
266 fs->super->s_free_blocks_count++;
269 group_block += fs->super->s_blocks_per_group;
273 * Mark all the bad blocks as used...
275 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
277 bb_error_msg_and_die("while marking bad blocks as used");
279 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
280 ext2fs_mark_block_bitmap(fs->block_map, blk);
281 ext2fs_badblocks_list_iterate_end(bb_iter);
285 * These functions implement a generalized progress meter.
287 struct progress_struct {
294 static void progress_init(struct progress_struct *progress,
295 const char *label,__u32 max)
299 memset(progress, 0, sizeof(struct progress_struct));
304 * Figure out how many digits we need
307 sprintf(progress->format, "%%%dd/%%%dld", i, i);
308 memset(progress->backup, '\b', sizeof(progress->backup)-1);
309 progress->backup[sizeof(progress->backup)-1] = 0;
310 if ((2*i)+1 < (int) sizeof(progress->backup))
311 progress->backup[(2*i)+1] = 0;
314 progress->skip_progress = 0;
315 if (getenv("MKE2FS_SKIP_PROGRESS"))
316 progress->skip_progress++;
318 fputs(label, stdout);
322 static void progress_update(struct progress_struct *progress, __u32 val)
324 if ((progress->format[0] == 0) || progress->skip_progress)
326 printf(progress->format, val, progress->max);
327 fputs(progress->backup, stdout);
330 static void progress_close(struct progress_struct *progress)
332 if (progress->format[0] == 0)
334 fputs("done \n", stdout);
339 * Helper function which zeros out _num_ blocks starting at _blk_. In
340 * case of an error, the details of the error is returned via _ret_blk_
341 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
342 * success, and an error code on an error.
344 * As a special case, if the first argument is NULL, then it will
345 * attempt to free the static zeroizing buffer. (This is to keep
346 * programs that check for memory leaks happy.)
348 static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
349 struct progress_struct *progress,
350 blk_t *ret_blk, int *ret_count)
352 int j, count, next_update, next_update_incr;
356 /* If fs is null, clean up the static buffer and return */
364 /* Allocate the zeroizing buffer if necessary */
366 buf = xcalloc(fs->blocksize, STRIDE_LENGTH);
368 /* OK, do the write loop */
370 next_update_incr = num / 100;
371 if (next_update_incr < 1)
372 next_update_incr = 1;
373 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
375 if (count > STRIDE_LENGTH)
376 count = STRIDE_LENGTH;
377 retval = io_channel_write_blk(fs->io, blk, count, buf);
385 if (progress && j > next_update) {
386 next_update += num / 100;
387 progress_update(progress, blk);
393 static void write_inode_tables(ext2_filsys fs)
399 struct progress_struct progress;
402 memset(&progress, 0, sizeof(progress));
404 progress_init(&progress, "Writing inode tables: ",
405 fs->group_desc_count);
407 for (i = 0; i < fs->group_desc_count; i++) {
408 progress_update(&progress, i);
410 blk = fs->group_desc[i].bg_inode_table;
411 num = fs->inode_blocks_per_group;
413 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
415 bb_error_msg_and_die(
416 "\nCould not write %d blocks "
417 "in inode table starting at %d.",
421 if (sync_kludge == 1)
423 else if ((i % sync_kludge) == 0)
427 zero_blocks(0, 0, 0, 0, 0, 0);
428 progress_close(&progress);
431 static void create_root_dir(ext2_filsys fs)
434 struct ext2_inode inode;
436 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
438 bb_error_msg_and_die("Could not create root dir");
441 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
443 bb_error_msg_and_die("Could not read root inode");
445 inode.i_uid = getuid();
447 inode.i_gid = getgid();
448 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
450 bb_error_msg_and_die("Could not set root inode ownership");
455 static void create_lost_and_found(ext2_filsys fs)
459 const char *name = "lost+found";
464 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
466 bb_error_msg_and_die("Could not create lost+found");
469 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
471 bb_error_msg_and_die("Could not look up lost+found");
474 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
475 if ((lpf_size += fs->blocksize) >= 16*1024)
477 retval = ext2fs_expand_dir(fs, ino);
479 bb_error_msg_and_die("Could not expand lost+found");
484 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
488 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
489 fs->group_desc[0].bg_free_inodes_count--;
490 fs->super->s_free_inodes_count--;
491 retval = ext2fs_update_bb_inode(fs, bb_list);
493 bb_error_msg_and_die("Could not set bad block inode");
498 static void reserve_inodes(ext2_filsys fs)
503 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
504 ext2fs_mark_inode_bitmap(fs->inode_map, i);
505 group = ext2fs_group_of_ino(fs, i);
506 fs->group_desc[group].bg_free_inodes_count--;
507 fs->super->s_free_inodes_count--;
509 ext2fs_mark_ib_dirty(fs);
512 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
513 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
514 #define BSD_LABEL_OFFSET 64
516 static void zap_sector(ext2_filsys fs, int sect, int nsect)
522 buf = xmalloc(512*nsect);
525 /* Check for a BSD disklabel, and don't erase it if so */
526 retval = io_channel_read_blk(fs->io, 0, -512, buf);
528 bb_error_msg("Warning: could not read block 0");
530 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
531 if ((*magic == BSD_DISKMAGIC) ||
532 (*magic == BSD_MAGICDISK))
537 memset(buf, 0, 512*nsect);
538 io_channel_set_blksize(fs->io, 512);
539 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
540 io_channel_set_blksize(fs->io, fs->blocksize);
543 bb_error_msg("Warning: could not erase sector %d", sect);
546 static void create_journal_dev(ext2_filsys fs)
548 struct progress_struct progress;
554 retval = ext2fs_create_journal_superblock(fs,
555 fs->super->s_blocks_count, 0, &buf);
557 bb_error_msg_and_die("Could not init journal superblock");
560 memset(&progress, 0, sizeof(progress));
562 progress_init(&progress, "Zeroing journal device: ",
563 fs->super->s_blocks_count);
565 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
566 &progress, &blk, &count);
568 bb_error_msg_and_die("Could not zero journal device (block %u, count %d)",
571 zero_blocks(0, 0, 0, 0, 0, 0);
573 retval = io_channel_write_blk(fs->io,
574 fs->super->s_first_data_block+1,
577 bb_error_msg_and_die("Could not write journal superblock");
579 progress_close(&progress);
582 static void show_stats(ext2_filsys fs)
584 struct ext2_super_block *s = fs->super;
591 if (param.s_blocks_count != s->s_blocks_count)
592 bb_error_msg("warning: %d blocks unused\n",
593 param.s_blocks_count - s->s_blocks_count);
595 memset(buf, 0, sizeof(buf));
596 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
597 printf("Filesystem label=%s\n", buf);
598 fputs("OS type: ", stdout);
599 os = e2p_os2string(fs->super->s_creator_os);
602 printf("\nBlock size=%u (log=%u)\n", fs->blocksize,
603 s->s_log_block_size);
604 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
606 printf("%u inodes, %u blocks\n", s->s_inodes_count,
608 printf("%u blocks (%2.2f%%) reserved for the super user\n",
610 100.0 * s->s_r_blocks_count / s->s_blocks_count);
611 printf("First data block=%u\n", s->s_first_data_block);
612 if (s->s_reserved_gdt_blocks)
613 printf("Maximum filesystem blocks=%lu\n",
614 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
615 (fs->blocksize / sizeof(struct ext2_group_desc)) *
616 s->s_blocks_per_group);
617 if (fs->group_desc_count > 1)
618 printf("%u block groups\n", fs->group_desc_count);
620 printf("%u block group\n", fs->group_desc_count);
621 printf("%u blocks per group, %u fragments per group\n",
622 s->s_blocks_per_group, s->s_frags_per_group);
623 printf("%u inodes per group\n", s->s_inodes_per_group);
625 if (fs->group_desc_count == 1) {
630 printf("Superblock backups stored on blocks: ");
631 group_block = s->s_first_data_block;
633 for (i = 1; i < fs->group_desc_count; i++) {
634 group_block += s->s_blocks_per_group;
635 if (!ext2fs_bg_has_super(fs, i))
639 need = int_log10(group_block) + 2;
640 if (need > col_left) {
645 printf("%u", group_block);
651 * Set the S_CREATOR_OS field. Return true if OS is known,
654 static int set_os(struct ext2_super_block *sb, char *os)
657 sb->s_creator_os = atoi (os);
658 else if (strcasecmp(os, "linux") == 0)
659 sb->s_creator_os = EXT2_OS_LINUX;
660 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
661 sb->s_creator_os = EXT2_OS_HURD;
662 else if (strcasecmp(os, "masix") == 0)
663 sb->s_creator_os = EXT2_OS_MASIX;
664 else if (strcasecmp(os, "freebsd") == 0)
665 sb->s_creator_os = EXT2_OS_FREEBSD;
666 else if (strcasecmp(os, "lites") == 0)
667 sb->s_creator_os = EXT2_OS_LITES;
674 #define PATH_SET "PATH=/sbin"
676 static void parse_extended_opts(struct ext2_super_block *sb_param,
679 char *buf, *token, *next, *p, *arg;
682 buf = bb_xstrdup(opts);
683 for (token = buf; token && *token; token = next) {
684 p = strchr(token, ',');
690 arg = strchr(token, '=');
695 if (strcmp(token, "stride") == 0) {
700 fs_stride = strtoul(arg, &p, 0);
701 if (*p || (fs_stride == 0)) {
702 bb_error_msg("Invalid stride parameter");
706 } else if (!strcmp(token, "resize")) {
707 unsigned long resize, bpg, rsv_groups;
708 unsigned long group_desc_count, desc_blocks;
709 unsigned int gdpb, blocksize;
717 resize = parse_num_blocks(arg,
718 sb_param->s_log_block_size);
721 bb_error_msg("Invalid resize parameter: %s", arg);
725 if (resize <= sb_param->s_blocks_count) {
726 bb_error_msg("The resize maximum must be greater than the filesystem size");
731 blocksize = EXT2_BLOCK_SIZE(sb_param);
732 bpg = sb_param->s_blocks_per_group;
735 gdpb = blocksize / sizeof(struct ext2_group_desc);
736 group_desc_count = (sb_param->s_blocks_count +
738 desc_blocks = (group_desc_count +
740 rsv_groups = (resize + bpg - 1) / bpg;
741 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
743 if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param))
744 rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param);
747 sb_param->s_feature_compat |=
748 EXT2_FEATURE_COMPAT_RESIZE_INODE;
750 sb_param->s_reserved_gdt_blocks = rsv_gdb;
756 bb_error_msg_and_die(
757 "\nBad options specified.\n\n"
758 "Options are separated by commas, "
759 "and may take an argument which\n"
760 "\tis set off by an equals ('=') sign.\n\n"
761 "Valid raid options are:\n"
762 "\tstride=<stride length in blocks>\n"
763 "\tresize=<resize maximum size in blocks>\n");
767 static __u32 ok_features[3] = {
768 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
769 EXT2_FEATURE_COMPAT_RESIZE_INODE |
770 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
771 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
772 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
773 EXT2_FEATURE_INCOMPAT_META_BG,
774 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
778 static int PRS(int argc, char *argv[])
786 int reserved_ratio = 5;
788 int show_version_only = 0;
789 ext2_ino_t num_inodes = 0;
791 char * oldpath = getenv("PATH");
792 char * extended_opts = 0;
793 const char * fs_type = 0;
797 /* Update our PATH to include /sbin */
801 bb_xasprintf(&newpath, "%s:%s", PATH_SET, oldpath);
805 tmp = getenv("MKE2FS_SYNC");
807 sync_kludge = atoi(tmp);
809 /* Determine the system page size if possible */
810 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
811 #define _SC_PAGESIZE _SC_PAGE_SIZE
814 sysval = sysconf(_SC_PAGESIZE);
816 sys_page_size = sysval;
817 #endif /* _SC_PAGESIZE */
819 setbuf(stdout, NULL);
820 setbuf(stderr, NULL);
821 memset(¶m, 0, sizeof(struct ext2_super_block));
822 param.s_rev_level = 1; /* Create revision 1 filesystems now */
823 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
824 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
826 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
830 linux_version_code = get_kernel_revision();
831 if (linux_version_code && linux_version_code < (2*65536 + 2*256)) {
832 param.s_rev_level = 0;
833 param.s_feature_incompat = 0;
834 param.s_feature_compat = 0;
835 param.s_feature_ro_compat = 0;
840 /* If called as mkfs.ext3, create a journal inode */
841 if (!strcmp(*argv + strlen(*argv) - 9, "mkfs.ext3"))
845 while ((c = getopt (argc, argv,
846 "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
849 blocksize = strtol(optarg, &tmp, 0);
850 b = (blocksize > 0) ? blocksize : -blocksize;
851 if (b < EXT2_MIN_BLOCK_SIZE ||
852 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
853 bb_error_msg_and_die("bad block size - %s", optarg);
855 if (blocksize > 4096)
857 "Warning: blocksize %d not usable on most systems",
860 param.s_log_block_size =
861 int_log2(blocksize >>
862 EXT2_MIN_BLOCK_LOG_SIZE);
864 case 'c': /* Check for bad blocks */
865 case 't': /* deprecated */
869 size = strtoul(optarg, &tmp, 0);
870 if (size < EXT2_MIN_BLOCK_SIZE ||
871 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
872 bb_error_msg_and_die("bad fragment size - %s", optarg);
874 param.s_log_frag_size =
875 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
877 "Warning: fragments not supported. "
878 "Ignoring -f option");
881 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
883 bb_error_msg_and_die("Illegal number for blocks per group");
885 if ((param.s_blocks_per_group % 8) != 0) {
886 bb_error_msg_and_die("blocks per group must be multiple of 8");
890 inode_ratio = strtoul(optarg, &tmp, 0);
891 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
892 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
894 bb_error_msg_and_die("bad inode ratio %s (min %d/max %d",
895 optarg, EXT2_MIN_BLOCK_SIZE,
896 EXT2_MAX_BLOCK_SIZE);
900 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
903 param.s_feature_compat |=
904 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
909 bad_blocks_filename = optarg;
912 reserved_ratio = strtoul(optarg, &tmp, 0);
913 if (reserved_ratio > 50 || *tmp) {
914 bb_error_msg_and_die("bad 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 inode_size = strtoul(optarg, &tmp, 0);
943 bb_error_msg_and_die("bad inode size - %s", optarg);
948 num_inodes = atoi(optarg);
960 volume_label = optarg;
966 if (!strcmp(optarg, "none")) {
967 param.s_feature_compat = 0;
968 param.s_feature_incompat = 0;
969 param.s_feature_ro_compat = 0;
972 if (e2p_edit_feature(optarg,
973 ¶m.s_feature_compat,
975 bb_error_msg_and_die("Invalid filesystem option set: %s", optarg);
980 extended_opts = optarg;
989 /* Print version number and exit */
996 if ((optind == argc) && !show_version_only)
998 device_name = argv[optind++];
1000 if (!quiet || show_version_only)
1001 bb_error_msg("mke2fs %s (%s)", E2FSPROGS_VERSION,
1004 if (show_version_only) {
1009 * If there's no blocksize specified and there is a journal
1010 * device, use it to figure out the blocksize
1012 if (blocksize <= 0 && journal_device) {
1016 #ifdef CONFIG_TESTIO_DEBUG
1017 io_ptr = test_io_manager;
1018 test_io_backing_manager = unix_io_manager;
1020 io_ptr = unix_io_manager;
1022 retval = ext2fs_open(journal_device,
1023 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1026 bb_error_msg_and_die("Could not open journal device %s", journal_device);
1028 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1029 bb_error_msg_and_die(
1030 "Journal dev blocksize (%d) smaller than "
1031 "minimum blocksize %d\n", jfs->blocksize,
1034 blocksize = jfs->blocksize;
1035 param.s_log_block_size =
1036 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1040 if (blocksize > sys_page_size) {
1042 bb_error_msg("%d-byte blocks too big for system (max %d)",
1043 blocksize, sys_page_size);
1047 "Warning: %d-byte blocks too big for system "
1048 "(max %d), forced to continue",
1049 blocksize, sys_page_size);
1051 if ((blocksize > 4096) &&
1052 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1054 "\nWarning: some 2.4 kernels do not support "
1055 "blocksizes greater than 4096 \n\tusing ext3."
1056 " Use -b 4096 if this is an issue for you\n");
1058 if (optind < argc) {
1059 param.s_blocks_count = parse_num_blocks(argv[optind++],
1060 param.s_log_block_size);
1061 if (!param.s_blocks_count) {
1062 bb_error_msg_and_die("bad blocks count - %s", argv[optind - 1]);
1068 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1070 fs_type = "journal";
1072 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1073 param.s_feature_compat = 0;
1074 param.s_feature_ro_compat = 0;
1076 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1077 (param.s_feature_compat || param.s_feature_ro_compat ||
1078 param.s_feature_incompat))
1079 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1082 check_plausibility(device_name);
1083 check_mount(device_name, force, "filesystem");
1085 param.s_log_frag_size = param.s_log_block_size;
1087 if (noaction && param.s_blocks_count) {
1088 dev_size = param.s_blocks_count;
1092 retval = ext2fs_get_device_size(device_name,
1093 EXT2_BLOCK_SIZE(¶m),
1095 if ((retval == EFBIG) &&
1097 (param.s_log_block_size == 0)) {
1098 param.s_log_block_size = 2;
1104 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1105 bb_error_msg_and_die("Could not determine filesystem size");
1107 if (!param.s_blocks_count) {
1108 if (retval == EXT2_ET_UNIMPLEMENTED) {
1109 bb_error_msg_and_die(
1110 "Couldn't determine device size; you "
1111 "must specify\nthe size of the "
1114 if (dev_size == 0) {
1115 bb_error_msg_and_die(
1116 "Device size reported to be zero. "
1117 "Invalid partition specified, or\n\t"
1118 "partition table wasn't reread "
1119 "after running fdisk, due to\n\t"
1120 "a modified partition being busy "
1121 "and in use. You may need to reboot\n\t"
1122 "to re-read your partition table.\n"
1125 param.s_blocks_count = dev_size;
1126 if (sys_page_size > EXT2_BLOCK_SIZE(¶m))
1127 param.s_blocks_count &= ~((sys_page_size /
1128 EXT2_BLOCK_SIZE(¶m))-1);
1131 } else if (!force && (param.s_blocks_count > dev_size)) {
1132 bb_error_msg("Filesystem larger than apparent device size");
1137 * If the user asked for HAS_JOURNAL, then make sure a journal
1140 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1144 /* Set first meta blockgroup via an environment variable */
1145 /* (this is mostly for debugging purposes) */
1146 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1147 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1148 param.s_first_meta_bg = atoi(tmp);
1150 /* Get the hardware sector size, if available */
1151 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1153 bb_error_msg_and_die("Could not determine hardware sector size");
1156 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1157 sector_size = atoi(tmp);
1159 set_fs_defaults(fs_type, ¶m, blocksize, sector_size, &inode_ratio);
1160 blocksize = EXT2_BLOCK_SIZE(¶m);
1163 parse_extended_opts(¶m, extended_opts);
1165 /* Since sparse_super is the default, we would only have a problem
1166 * here if it was explicitly disabled.
1168 if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1169 !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1170 bb_error_msg_and_die("reserved online resize blocks not supported "
1171 "on non-sparse filesystem");
1174 if (param.s_blocks_per_group) {
1175 if (param.s_blocks_per_group < 256 ||
1176 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1177 bb_error_msg_and_die("blocks per group count out of range");
1182 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1183 inode_size > EXT2_BLOCK_SIZE(¶m) ||
1184 inode_size & (inode_size - 1)) {
1185 bb_error_msg_and_die("bad inode size %d (min %d/max %d)",
1186 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1189 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1191 "Warning: %d-byte inodes not usable on most systems",
1193 param.s_inode_size = inode_size;
1197 * Calculate number of inodes based on the inode ratio
1199 param.s_inodes_count = num_inodes ? num_inodes :
1200 ((__u64) param.s_blocks_count * blocksize)
1204 * Calculate number of blocks to reserve
1206 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1210 int mke2fs_main (int argc, char *argv[])
1214 badblocks_list bb_list = 0;
1220 if(!PRS(argc, argv))
1223 #ifdef CONFIG_TESTIO_DEBUG
1224 io_ptr = test_io_manager;
1225 test_io_backing_manager = unix_io_manager;
1227 io_ptr = unix_io_manager;
1231 * Initialize the superblock....
1233 retval = ext2fs_initialize(device_name, 0, ¶m,
1236 bb_error_msg_and_die("Could not set up superblock");
1240 * Wipe out the old on-disk superblock
1243 zap_sector(fs, 2, 6);
1246 * Generate a UUID for it...
1248 uuid_generate(fs->super->s_uuid);
1251 * Initialize the directory index variables
1253 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1254 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1257 * Add "jitter" to the superblock's check interval so that we
1258 * don't check all the filesystems at the same time. We use a
1259 * kludgy hack of using the UUID to derive a random jitter value.
1261 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1262 val += fs->super->s_uuid[i];
1263 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1266 * Override the creator OS, if applicable
1268 if (creator_os && !set_os(fs->super, creator_os)) {
1269 bb_error_msg_and_die("unknown os - %s", creator_os);
1273 * For the Hurd, we will turn off filetype since it doesn't
1276 if (fs->super->s_creator_os == EXT2_OS_HURD)
1277 fs->super->s_feature_incompat &=
1278 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1281 * Set the volume label...
1284 memset(fs->super->s_volume_name, 0,
1285 sizeof(fs->super->s_volume_name));
1286 strncpy(fs->super->s_volume_name, volume_label,
1287 sizeof(fs->super->s_volume_name));
1291 * Set the last mount directory
1294 memset(fs->super->s_last_mounted, 0,
1295 sizeof(fs->super->s_last_mounted));
1296 strncpy(fs->super->s_last_mounted, mount_dir,
1297 sizeof(fs->super->s_last_mounted));
1300 if (!quiet || noaction)
1306 if (fs->super->s_feature_incompat &
1307 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1308 create_journal_dev(fs);
1309 return (ext2fs_close(fs) ? 1 : 0);
1312 if (bad_blocks_filename)
1313 read_bb_file(fs, &bb_list, bad_blocks_filename);
1315 test_disk(fs, &bb_list);
1317 handle_bad_blocks(fs, bb_list);
1318 fs->stride = fs_stride;
1319 retval = ext2fs_allocate_tables(fs);
1321 bb_error_msg_and_die("Could not allocate filesystem tables");
1324 fs->super->s_state |= EXT2_ERROR_FS;
1325 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1327 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1328 unsigned int rsv = 65536 / fs->blocksize;
1329 unsigned long blocks = fs->super->s_blocks_count;
1330 unsigned long start;
1333 #ifdef ZAP_BOOTBLOCK
1334 zap_sector(fs, 0, 2);
1338 * Wipe out any old MD RAID (or other) metadata at the end
1339 * of the device. This will also verify that the device is
1340 * as large as we think. Be careful with very small devices.
1342 start = (blocks & ~(rsv - 1));
1346 retval = zero_blocks(fs, start, blocks - start,
1347 NULL, &ret_blk, NULL);
1350 bb_error_msg("Could not zero block %u at end of filesystem", ret_blk);
1352 write_inode_tables(fs);
1353 create_root_dir(fs);
1354 create_lost_and_found(fs);
1356 create_bad_block_inode(fs, bb_list);
1357 if (fs->super->s_feature_compat &
1358 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1359 retval = ext2fs_create_resize_inode(fs);
1361 bb_error_msg_and_die("Could not reserve blocks for online resize");
1366 if (journal_device) {
1370 check_plausibility(journal_device);
1371 check_mount(journal_device, force, "journal");
1373 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1374 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1375 fs->blocksize, unix_io_manager, &jfs);
1377 bb_error_msg_and_die("Could not open journal device %s", journal_device);
1380 printf("Adding journal to device %s: ", journal_device);
1383 retval = ext2fs_add_journal_device(fs, jfs);
1385 bb_error_msg_and_die("Could not add journal to device %s", journal_device);
1390 free(journal_device);
1391 } else if (journal_size) {
1392 journal_blocks = figure_journal_size(journal_size, fs);
1394 if (!journal_blocks) {
1395 fs->super->s_feature_compat &=
1396 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1400 printf("Creating journal (%d blocks): ",
1404 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1407 bb_error_msg_and_die("Could not create journal");
1415 printf("Writing superblocks and "
1416 "filesystem accounting information: ");
1417 retval = ext2fs_flush(fs);
1419 bb_error_msg("\nWarning, had trouble writing out superblocks");
1423 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1424 print_check_message(fs);
1426 val = ext2fs_close(fs);
1427 return (retval || val) ? 1 : 0;