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);
172 static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
173 static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)
179 fprintf(stderr, "\nCould not ");
180 vfprintf(stderr, fmt, ap);
181 fprintf(stderr, "\n");
187 static void mke2fs_verbose(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
188 static void mke2fs_verbose(const char *fmt, ...)
194 vfprintf(stdout, fmt, ap);
200 static void mke2fs_verbose_done(void)
202 mke2fs_verbose("done\n");
205 static void mke2fs_warning_msg(int retval, char *fmt, ... ) __attribute__ ((format (printf, 2, 3)));
206 static void mke2fs_warning_msg(int retval, char *fmt, ... )
212 fprintf(stderr, "\nWarning: ");
213 vfprintf(stderr, fmt, ap);
214 fprintf(stderr, "\n");
220 * Reads the bad blocks list from a file
222 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
223 const char *bad_blocks_file)
228 f = xfopen_for_read(bad_blocks_file);
229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
231 mke2fs_error_msg_and_die(retval, "read bad blocks from list");
235 * Runs the badblocks program to test the disk
237 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
243 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
244 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
245 fs->device_name, fs->super->s_blocks_count);
246 mke2fs_verbose("Running command: %s\n", buf);
249 bb_perror_msg_and_die("cannot run '%s'", buf);
251 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
253 mke2fs_error_msg_and_die(retval, "read bad blocks from program");
256 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
260 unsigned must_be_good;
262 badblocks_iterate bb_iter;
272 * The primary superblock and group descriptors *must* be
273 * good; if not, abort.
275 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
276 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
277 if (ext2fs_badblocks_list_test(bb_list, i)) {
278 bb_error_msg_and_die(
279 "Block %d in primary superblock/group descriptor area bad\n"
280 "Blocks %d through %d must be good in order to build a filesystem\n"
281 "Aborting ...", i, fs->super->s_first_data_block, must_be_good);
286 * See if any of the bad blocks are showing up in the backup
287 * superblocks and/or group descriptors. If so, issue a
288 * warning and adjust the block counts appropriately.
290 group_block = fs->super->s_first_data_block +
291 fs->super->s_blocks_per_group;
293 for (i = 1; i < fs->group_desc_count; i++) {
295 for (j=0; j < fs->desc_blocks+1; j++) {
296 if (ext2fs_badblocks_list_test(bb_list,
298 mke2fs_warning_msg(!group_bad,
299 "the backup superblock/group descriptors at block %d contain\n"
300 "bad blocks\n", group_block);
302 group = ext2fs_group_of_blk(fs, group_block+j);
303 fs->group_desc[group].bg_free_blocks_count++;
304 fs->super->s_free_blocks_count++;
307 group_block += fs->super->s_blocks_per_group;
311 * Mark all the bad blocks as used...
313 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
314 mke2fs_error_msg_and_die(retval, "mark bad blocks as used");
316 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
317 ext2fs_mark_block_bitmap(fs->block_map, blk);
318 ext2fs_badblocks_list_iterate_end(bb_iter);
322 * These functions implement a generalized progress meter.
324 struct progress_struct {
331 static void progress_init(struct progress_struct *progress,
332 const char *label,__u32 max)
336 memset(progress, 0, sizeof(struct progress_struct));
341 * Figure out how many digits we need
344 sprintf(progress->format, "%%%dd/%%%dld", i, i);
345 memset(progress->backup, '\b', sizeof(progress->backup)-1);
346 progress->backup[sizeof(progress->backup)-1] = 0;
347 if ((2*i)+1 < (int) sizeof(progress->backup))
348 progress->backup[(2*i)+1] = 0;
351 progress->skip_progress = 0;
352 if (getenv("MKE2FS_SKIP_PROGRESS"))
353 progress->skip_progress++;
355 fputs(label, stdout);
359 static void progress_update(struct progress_struct *progress, __u32 val)
361 if ((progress->format[0] == 0) || progress->skip_progress)
363 printf(progress->format, val, progress->max);
364 fputs(progress->backup, stdout);
367 static void progress_close(struct progress_struct *progress)
369 if (progress->format[0] == 0)
371 printf("%-28s\n", "done");
376 * Helper function which zeros out _num_ blocks starting at _blk_. In
377 * case of an error, the details of the error is returned via _ret_blk_
378 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
379 * success, and an error code on an error.
381 * As a special case, if the first argument is NULL, then it will
382 * attempt to free the static zeroizing buffer. (This is to keep
383 * programs that check for memory leaks happy.)
385 static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
386 struct progress_struct *progress,
387 blk_t *ret_blk, int *ret_count)
389 int j, count, next_update, next_update_incr;
393 /* If fs is null, clean up the static buffer and return */
401 /* Allocate the zeroizing buffer if necessary */
403 buf = xzalloc(fs->blocksize * STRIDE_LENGTH);
405 /* OK, do the write loop */
407 next_update_incr = num / 100;
408 if (next_update_incr < 1)
409 next_update_incr = 1;
410 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
412 if (count > STRIDE_LENGTH)
413 count = STRIDE_LENGTH;
414 retval = io_channel_write_blk(fs->io, blk, count, buf);
422 if (progress && j > next_update) {
423 next_update += num / 100;
424 progress_update(progress, blk);
430 static void write_inode_tables(ext2_filsys fs)
436 struct progress_struct progress;
439 memset(&progress, 0, sizeof(progress));
441 progress_init(&progress, "Writing inode tables: ",
442 fs->group_desc_count);
444 for (i = 0; i < fs->group_desc_count; i++) {
445 progress_update(&progress, i);
447 blk = fs->group_desc[i].bg_inode_table;
448 num = fs->inode_blocks_per_group;
450 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
451 mke2fs_error_msg_and_die(retval,
452 "write %d blocks in inode table starting at %d.",
455 if (sync_kludge == 1)
457 else if ((i % sync_kludge) == 0)
461 zero_blocks(0, 0, 0, 0, 0, 0);
462 progress_close(&progress);
465 static void create_root_dir(ext2_filsys fs)
468 struct ext2_inode inode;
470 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
471 mke2fs_error_msg_and_die(retval, "create root dir");
473 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
474 mke2fs_error_msg_and_die(retval, "read root inode");
475 inode.i_uid = getuid();
477 inode.i_gid = getgid();
478 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
479 mke2fs_error_msg_and_die(retval, "set root inode ownership");
483 static void create_lost_and_found(ext2_filsys fs)
487 const char *name = "lost+found";
489 char *msg = "create";
493 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
495 goto CREATE_LOST_AND_FOUND_ERROR;
498 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
501 goto CREATE_LOST_AND_FOUND_ERROR;
504 for (; i < EXT2_NDIR_BLOCKS; i++) {
505 if ((lpf_size += fs->blocksize) >= 16*1024)
507 retval = ext2fs_expand_dir(fs, ino);
509 CREATE_LOST_AND_FOUND_ERROR:
510 mke2fs_error_msg_and_die(retval, "%s %s", msg, name);
514 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
518 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
519 fs->group_desc[0].bg_free_inodes_count--;
520 fs->super->s_free_inodes_count--;
521 retval = ext2fs_update_bb_inode(fs, bb_list);
522 mke2fs_error_msg_and_die(retval, "set bad block inode");
525 static void reserve_inodes(ext2_filsys fs)
530 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
531 ext2fs_mark_inode_bitmap(fs->inode_map, i);
532 group = ext2fs_group_of_ino(fs, i);
533 fs->group_desc[group].bg_free_inodes_count--;
534 fs->super->s_free_inodes_count--;
536 ext2fs_mark_ib_dirty(fs);
539 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
540 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
541 #define BSD_LABEL_OFFSET 64
543 static void zap_sector(ext2_filsys fs, int sect, int nsect)
546 char *fmt = "could not %s %d";
550 buf = xmalloc(512*nsect);
553 /* Check for a BSD disklabel, and don't erase it if so */
554 retval = io_channel_read_blk(fs->io, 0, -512, buf);
556 mke2fs_warning_msg(retval, fmt, "read block", 0);
558 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
559 if ((*magic == BSD_DISKMAGIC) ||
560 (*magic == BSD_MAGICDISK))
565 memset(buf, 0, 512*nsect);
566 io_channel_set_blksize(fs->io, 512);
567 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
568 io_channel_set_blksize(fs->io, fs->blocksize);
570 mke2fs_warning_msg(retval, fmt, "erase sector", sect);
573 static void create_journal_dev(ext2_filsys fs)
575 struct progress_struct progress;
578 char *fmt = "%s journal superblock";
582 retval = ext2fs_create_journal_superblock(fs,
583 fs->super->s_blocks_count, 0, &buf);
584 mke2fs_error_msg_and_die(retval, fmt, "init");
586 memset(&progress, 0, sizeof(progress));
588 progress_init(&progress, "Zeroing journal device: ",
589 fs->super->s_blocks_count);
591 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
592 &progress, &blk, &count);
593 mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)",
595 zero_blocks(0, 0, 0, 0, 0, 0);
597 retval = io_channel_write_blk(fs->io,
598 fs->super->s_first_data_block+1,
600 mke2fs_error_msg_and_die(retval, fmt, "write");
601 progress_close(&progress);
604 static void show_stats(ext2_filsys fs)
606 struct ext2_super_block *s = fs->super;
612 mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count),
613 "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count);
614 os = e2p_os2string(fs->super->s_creator_os);
615 printf( "Filesystem label=%.*s\n"
617 "Block size=%u (log=%u)\n"
618 "Fragment size=%u (log=%u)\n"
619 "%u inodes, %u blocks\n"
620 "%u blocks (%2.2f%%) reserved for the super user\n"
621 "First data block=%u\n",
622 (int) sizeof(s->s_volume_name),
625 fs->blocksize, s->s_log_block_size,
626 fs->fragsize, s->s_log_frag_size,
627 s->s_inodes_count, s->s_blocks_count,
628 s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count,
629 s->s_first_data_block);
631 if (s->s_reserved_gdt_blocks) {
632 printf("Maximum filesystem blocks=%lu\n",
633 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
634 (fs->blocksize / sizeof(struct ext2_group_desc)) *
635 s->s_blocks_per_group);
637 printf( "%u block group%s\n"
638 "%u blocks per group, %u fragments per group\n"
639 "%u inodes per group\n",
640 fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "",
641 s->s_blocks_per_group, s->s_frags_per_group,
642 s->s_inodes_per_group);
643 if (fs->group_desc_count == 1) {
648 printf("Superblock backups stored on blocks: ");
649 group_block = s->s_first_data_block;
651 for (i = 1; i < fs->group_desc_count; i++) {
652 group_block += s->s_blocks_per_group;
653 if (!ext2fs_bg_has_super(fs, i))
657 need = int_log10(group_block) + 2;
658 if (need > col_left) {
663 printf("%u", group_block);
669 * Set the S_CREATOR_OS field. Return true if OS is known,
672 static int set_os(struct ext2_super_block *sb, char *os)
675 sb->s_creator_os = atoi(os);
679 if ((sb->s_creator_os = e2p_string2os(os)) >= 0) {
681 } else if (!strcasecmp("GNU", os)) {
682 sb->s_creator_os = EXT2_OS_HURD;
688 static void parse_extended_opts(struct ext2_super_block *sb_param,
691 char *buf, *token, *next, *p, *arg;
695 for (token = buf; token && *token; token = next) {
696 p = strchr(token, ',');
702 arg = strchr(token, '=');
707 if (strcmp(token, "stride") == 0) {
712 fs_stride = strtoul(arg, &p, 0);
713 if (*p || (fs_stride == 0)) {
714 bb_error_msg("Invalid stride parameter: %s", arg);
718 } else if (!strcmp(token, "resize")) {
719 unsigned long resize, bpg, rsv_groups;
720 unsigned long group_desc_count, desc_blocks;
721 unsigned int gdpb, blocksize;
729 resize = parse_num_blocks(arg,
730 sb_param->s_log_block_size);
733 bb_error_msg("Invalid resize parameter: %s", arg);
737 if (resize <= sb_param->s_blocks_count) {
738 bb_error_msg("The resize maximum must be greater "
739 "than the filesystem size");
744 blocksize = EXT2_BLOCK_SIZE(sb_param);
745 bpg = sb_param->s_blocks_per_group;
748 gdpb = blocksize / sizeof(struct ext2_group_desc);
749 group_desc_count = (sb_param->s_blocks_count +
751 desc_blocks = (group_desc_count +
753 rsv_groups = (resize + bpg - 1) / bpg;
754 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
756 if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param))
757 rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param);
760 sb_param->s_feature_compat |=
761 EXT2_FEATURE_COMPAT_RESIZE_INODE;
763 sb_param->s_reserved_gdt_blocks = rsv_gdb;
769 bb_error_msg_and_die(
770 "\nBad options specified.\n\n"
771 "Extended options are separated by commas, "
772 "and may take an argument which\n"
773 "\tis set off by an equals ('=') sign.\n\n"
774 "Valid extended options are:\n"
775 "\tstride=<stride length in blocks>\n"
776 "\tresize=<resize maximum size in blocks>\n");
780 static __u32 ok_features[3] = {
781 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
782 EXT2_FEATURE_COMPAT_RESIZE_INODE |
783 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
784 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
785 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
786 EXT2_FEATURE_INCOMPAT_META_BG,
787 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
790 static int PRS(int argc, char **argv)
798 int reserved_ratio = 5;
800 int show_version_only = 0;
801 ext2_ino_t num_inodes = 0;
803 char * extended_opts = 0;
804 const char * fs_type = 0;
808 /* Update our PATH to include /sbin */
809 e2fs_set_sbin_path();
811 tmp = getenv("MKE2FS_SYNC");
813 sync_kludge = atoi(tmp);
815 /* Determine the system page size if possible */
816 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
817 #define _SC_PAGESIZE _SC_PAGE_SIZE
820 sysval = sysconf(_SC_PAGESIZE);
822 sys_page_size = sysval;
823 #endif /* _SC_PAGESIZE */
825 setbuf(stdout, NULL);
826 setbuf(stderr, NULL);
827 memset(¶m, 0, sizeof(struct ext2_super_block));
828 param.s_rev_level = 1; /* Create revision 1 filesystems now */
829 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
830 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
833 linux_version_code = get_linux_version_code();
834 if (linux_version_code && linux_version_code < KERNEL_VERSION(2,2,0)) {
835 param.s_rev_level = 0;
836 param.s_feature_incompat = 0;
837 param.s_feature_compat = 0;
838 param.s_feature_ro_compat = 0;
842 /* If called as mkfs.ext3, create a journal inode */
843 if (last_char_is(applet_name, '3'))
846 while ((c = getopt (argc, argv,
847 "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
850 blocksize = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE);
851 mke2fs_warning_msg((blocksize > 4096),
852 "blocksize %d not usable on most systems",
854 param.s_log_block_size =
855 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
857 case 'c': /* Check for bad blocks */
858 case 't': /* deprecated */
862 size = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE);
863 param.s_log_frag_size =
864 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
865 mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option");
868 param.s_blocks_per_group = xatou32(optarg);
869 if ((param.s_blocks_per_group % 8) != 0) {
870 bb_error_msg_and_die("blocks per group must be multiple of 8");
874 /* Huh? is "* 1024" correct? */
875 inode_ratio = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE * 1024);
878 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
881 param.s_feature_compat |=
882 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
887 bad_blocks_filename = optarg;
890 reserved_ratio = xatou_range(optarg, 0, 50);
899 param.s_rev_level = xatoi_u(optarg);
900 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
901 param.s_feature_incompat = 0;
902 param.s_feature_compat = 0;
903 param.s_feature_ro_compat = 0;
906 case 's': /* deprecated */
908 param.s_feature_ro_compat |=
909 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
911 param.s_feature_ro_compat &=
912 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
914 #ifdef EXT2_DYNAMIC_REV
916 inode_size = xatoi_u(optarg);
920 num_inodes = xatoi_u(optarg);
932 volume_label = optarg;
938 if (!strcmp(optarg, "none")) {
939 param.s_feature_compat = 0;
940 param.s_feature_incompat = 0;
941 param.s_feature_ro_compat = 0;
944 if (e2p_edit_feature(optarg,
945 ¶m.s_feature_compat,
947 bb_error_msg_and_die("Invalid filesystem option set: %s", optarg);
952 extended_opts = optarg;
961 /* Print version number and exit */
962 show_version_only = 1;
969 if ((optind == argc) /*&& !show_version_only*/)
971 device_name = argv[optind++];
973 mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
975 if (show_version_only) {
980 * If there's no blocksize specified and there is a journal
981 * device, use it to figure out the blocksize
983 if (blocksize <= 0 && journal_device) {
987 #ifdef CONFIG_TESTIO_DEBUG
988 io_ptr = test_io_manager;
989 test_io_backing_manager = unix_io_manager;
991 io_ptr = unix_io_manager;
993 retval = ext2fs_open(journal_device,
994 EXT2_FLAG_JOURNAL_DEV_OK, 0,
996 mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device);
997 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
998 bb_error_msg_and_die(
999 "Journal dev blocksize (%d) smaller than "
1000 "minimum blocksize %d\n", jfs->blocksize,
1003 blocksize = jfs->blocksize;
1004 param.s_log_block_size =
1005 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1009 if (blocksize > sys_page_size) {
1010 mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)",
1011 blocksize, sys_page_size);
1015 bb_error_msg("Forced to continue");
1017 mke2fs_warning_msg(((blocksize > 4096) &&
1018 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)),
1019 "some 2.4 kernels do not support "
1020 "blocksizes greater than 4096 using ext3.\n"
1021 "Use -b 4096 if this is an issue for you\n");
1023 if (optind < argc) {
1024 param.s_blocks_count = parse_num_blocks(argv[optind++],
1025 param.s_log_block_size);
1026 mke2fs_error_msg_and_die(!param.s_blocks_count, "invalid blocks count - %s", argv[optind - 1]);
1031 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1033 fs_type = "journal";
1035 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1036 param.s_feature_compat = 0;
1037 param.s_feature_ro_compat = 0;
1039 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1040 (param.s_feature_compat || param.s_feature_ro_compat ||
1041 param.s_feature_incompat))
1042 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1044 check_plausibility(device_name , force);
1045 check_mount(device_name, force, "filesystem");
1047 param.s_log_frag_size = param.s_log_block_size;
1049 if (noaction && param.s_blocks_count) {
1050 dev_size = param.s_blocks_count;
1054 retval = ext2fs_get_device_size(device_name,
1055 EXT2_BLOCK_SIZE(¶m),
1057 if ((retval == EFBIG) &&
1059 (param.s_log_block_size == 0)) {
1060 param.s_log_block_size = 2;
1066 mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size");
1068 if (!param.s_blocks_count) {
1069 if (retval == EXT2_ET_UNIMPLEMENTED) {
1070 mke2fs_error_msg_and_die(1,
1071 "determine device size; you "
1072 "must specify\nthe size of the "
1075 if (dev_size == 0) {
1076 bb_error_msg_and_die(
1077 "Device size reported to be zero. "
1078 "Invalid partition specified, or\n\t"
1079 "partition table wasn't reread "
1080 "after running fdisk, due to\n\t"
1081 "a modified partition being busy "
1082 "and in use. You may need to reboot\n\t"
1083 "to re-read your partition table.\n"
1086 param.s_blocks_count = dev_size;
1087 if (sys_page_size > EXT2_BLOCK_SIZE(¶m))
1088 param.s_blocks_count &= ~((sys_page_size /
1089 EXT2_BLOCK_SIZE(¶m))-1);
1092 } else if (!force && (param.s_blocks_count > dev_size)) {
1093 bb_error_msg("Filesystem larger than apparent device size");
1098 * If the user asked for HAS_JOURNAL, then make sure a journal
1101 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1105 /* Set first meta blockgroup via an environment variable */
1106 /* (this is mostly for debugging purposes) */
1107 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1108 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1109 param.s_first_meta_bg = atoi(tmp);
1111 /* Get the hardware sector size, if available */
1112 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1113 mke2fs_error_msg_and_die(retval, "determine hardware sector size");
1115 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1116 sector_size = atoi(tmp);
1118 set_fs_defaults(fs_type, ¶m, blocksize, sector_size, &inode_ratio);
1119 blocksize = EXT2_BLOCK_SIZE(¶m);
1122 parse_extended_opts(¶m, extended_opts);
1124 /* Since sparse_super is the default, we would only have a problem
1125 * here if it was explicitly disabled.
1127 if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1128 !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1129 bb_error_msg_and_die("reserved online resize blocks not supported "
1130 "on non-sparse filesystem");
1133 if (param.s_blocks_per_group) {
1134 if (param.s_blocks_per_group < 256 ||
1135 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1136 bb_error_msg_and_die("blocks per group count out of range");
1140 if (!force && param.s_blocks_count >= (1 << 31)) {
1141 bb_error_msg_and_die("Filesystem too large. No more than 2**31-1 blocks\n"
1142 "\t (8TB using a blocksize of 4k) are currently supported.");
1146 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1147 inode_size > EXT2_BLOCK_SIZE(¶m) ||
1148 inode_size & (inode_size - 1)) {
1149 bb_error_msg_and_die("invalid inode size %d (min %d/max %d)",
1150 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1153 mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE),
1154 "%d-byte inodes not usable on most systems",
1156 param.s_inode_size = inode_size;
1160 * Calculate number of inodes based on the inode ratio
1162 param.s_inodes_count = num_inodes ? num_inodes :
1163 ((__u64) param.s_blocks_count * blocksize)
1167 * Calculate number of blocks to reserve
1169 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1173 static void mke2fs_clean_up(void)
1175 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
1178 int mke2fs_main (int argc, char **argv);
1179 int mke2fs_main (int argc, char **argv)
1183 badblocks_list bb_list = 0;
1188 if (ENABLE_FEATURE_CLEAN_UP)
1189 atexit(mke2fs_clean_up);
1190 if (!PRS(argc, argv))
1193 #ifdef CONFIG_TESTIO_DEBUG
1194 io_ptr = test_io_manager;
1195 test_io_backing_manager = unix_io_manager;
1197 io_ptr = unix_io_manager;
1201 * Initialize the superblock....
1203 retval = ext2fs_initialize(device_name, 0, ¶m,
1205 mke2fs_error_msg_and_die(retval, "set up superblock");
1208 * Wipe out the old on-disk superblock
1211 zap_sector(fs, 2, 6);
1214 * Generate a UUID for it...
1216 uuid_generate(fs->super->s_uuid);
1219 * Initialize the directory index variables
1221 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1222 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1225 * Add "jitter" to the superblock's check interval so that we
1226 * don't check all the filesystems at the same time. We use a
1227 * kludgy hack of using the UUID to derive a random jitter value.
1229 for (i = 0, val = 0; i < sizeof(fs->super->s_uuid); i++)
1230 val += fs->super->s_uuid[i];
1231 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1234 * Override the creator OS, if applicable
1236 if (creator_os && !set_os(fs->super, creator_os)) {
1237 bb_error_msg_and_die("unknown os - %s", creator_os);
1241 * For the Hurd, we will turn off filetype since it doesn't
1244 if (fs->super->s_creator_os == EXT2_OS_HURD)
1245 fs->super->s_feature_incompat &=
1246 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1249 * Set the volume label...
1252 snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label);
1256 * Set the last mount directory
1259 snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir);
1262 if (!quiet || noaction)
1268 if (fs->super->s_feature_incompat &
1269 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1270 create_journal_dev(fs);
1271 return (ext2fs_close(fs) ? 1 : 0);
1274 if (bad_blocks_filename)
1275 read_bb_file(fs, &bb_list, bad_blocks_filename);
1277 test_disk(fs, &bb_list);
1279 handle_bad_blocks(fs, bb_list);
1280 fs->stride = fs_stride;
1281 retval = ext2fs_allocate_tables(fs);
1282 mke2fs_error_msg_and_die(retval, "allocate filesystem tables");
1284 fs->super->s_state |= EXT2_ERROR_FS;
1285 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1287 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1288 unsigned int rsv = 65536 / fs->blocksize;
1289 unsigned long blocks = fs->super->s_blocks_count;
1290 unsigned long start;
1293 #ifdef ZAP_BOOTBLOCK
1294 zap_sector(fs, 0, 2);
1298 * Wipe out any old MD RAID (or other) metadata at the end
1299 * of the device. This will also verify that the device is
1300 * as large as we think. Be careful with very small devices.
1302 start = (blocks & ~(rsv - 1));
1306 retval = zero_blocks(fs, start, blocks - start,
1307 NULL, &ret_blk, NULL);
1309 mke2fs_warning_msg(retval, "cannot zero block %u at end of filesystem", ret_blk);
1310 write_inode_tables(fs);
1311 create_root_dir(fs);
1312 create_lost_and_found(fs);
1314 create_bad_block_inode(fs, bb_list);
1315 if (fs->super->s_feature_compat &
1316 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1317 retval = ext2fs_create_resize_inode(fs);
1318 mke2fs_error_msg_and_die(retval, "reserve blocks for online resize");
1322 if (journal_device) {
1323 make_journal_device(journal_device, fs, quiet, force);
1324 } else if (journal_size) {
1325 make_journal_blocks(fs, journal_size, journal_flags, quiet);
1328 mke2fs_verbose("Writing superblocks and filesystem accounting information: ");
1329 retval = ext2fs_flush(fs);
1330 mke2fs_warning_msg(retval, "had trouble writing out superblocks");
1331 mke2fs_verbose_done();
1332 if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG"))
1333 print_check_message(fs);
1334 val = ext2fs_close(fs);
1335 return (retval || val) ? 1 : 0;