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 :-).
25 #include <sys/utsname.h>
45 #include <sys/ioctl.h>
46 #include <sys/types.h>
48 #include "ext2fs/ext2_fs.h"
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
52 #include "ext2fs/ext2fs.h"
56 #include "../version.h"
57 #include "nls-enable.h"
59 #define STRIDE_LENGTH 8
65 extern int isatty(int);
66 extern FILE *fpopen(const char *cmd, const char *mode);
68 const char * program_name = "mke2fs";
69 const char * device_name /* = NULL */;
71 /* Command line options */
80 char *bad_blocks_filename;
83 struct ext2_super_block fs_param;
88 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
92 int sys_page_size = 4096;
93 int linux_version_code = 0;
95 static void usage(void)
97 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
98 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
99 "[-j] [-J journal-options]\n"
100 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
101 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
102 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
103 "[-r fs-revision] [-E extended-option[,...]] [-qvSV]\n\tdevice [blocks-count]\n"),
108 static int int_log2(int arg)
120 static int int_log10(unsigned int arg)
129 static int parse_version_number(const char *s)
131 int major, minor, rev;
137 major = strtol(cp, &endptr, 10);
138 if (cp == endptr || *endptr != '.')
141 minor = strtol(cp, &endptr, 10);
142 if (cp == endptr || *endptr != '.')
145 rev = strtol(cp, &endptr, 10);
148 return ((((major * 256) + minor) * 256) + rev);
152 * Helper function for read_bb_file and test_disk
154 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
156 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
161 * Reads the bad blocks list from a file
163 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
164 const char *bad_blocks_file)
169 f = fopen(bad_blocks_file, "r");
171 com_err("read_bad_blocks_file", errno,
172 _("while trying to open %s"), bad_blocks_file);
175 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
178 com_err("ext2fs_read_bb_FILE", retval,
179 _("while reading in list of bad blocks from file"));
185 * Runs the badblocks program to test the disk
187 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
193 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
194 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
195 fs->device_name, fs->super->s_blocks_count-1);
197 printf(_("Running command: %s\n"), buf);
200 com_err("popen", errno,
201 _("while trying to run '%s'"), buf);
204 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
207 com_err("ext2fs_read_bb_FILE", retval,
208 _("while processing list of bad blocks from program"));
213 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
217 unsigned must_be_good;
219 badblocks_iterate bb_iter;
229 * The primary superblock and group descriptors *must* be
230 * good; if not, abort.
232 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
233 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
234 if (ext2fs_badblocks_list_test(bb_list, i)) {
235 fprintf(stderr, _("Block %d in primary "
236 "superblock/group descriptor area bad.\n"), i);
237 fprintf(stderr, _("Blocks %u through %u must be good "
238 "in order to build a filesystem.\n"),
239 fs->super->s_first_data_block, must_be_good);
240 fputs(_("Aborting....\n"), stderr);
246 * See if any of the bad blocks are showing up in the backup
247 * superblocks and/or group descriptors. If so, issue a
248 * warning and adjust the block counts appropriately.
250 group_block = fs->super->s_first_data_block +
251 fs->super->s_blocks_per_group;
253 for (i = 1; i < fs->group_desc_count; i++) {
255 for (j=0; j < fs->desc_blocks+1; j++) {
256 if (ext2fs_badblocks_list_test(bb_list,
260 _("Warning: the backup superblock/group descriptors at block %u contain\n"
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 com_err("ext2fs_badblocks_list_iterate_begin", retval,
278 _("while marking bad blocks as used"));
281 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
282 ext2fs_mark_block_bitmap(fs->block_map, blk);
283 ext2fs_badblocks_list_iterate_end(bb_iter);
287 * These functions implement a generalized progress meter.
289 struct progress_struct {
296 static void progress_init(struct progress_struct *progress,
297 const char *label,__u32 max)
301 memset(progress, 0, sizeof(struct progress_struct));
306 * Figure out how many digits we need
309 sprintf(progress->format, "%%%dd/%%%dld", i, i);
310 memset(progress->backup, '\b', sizeof(progress->backup)-1);
311 progress->backup[sizeof(progress->backup)-1] = 0;
312 if ((2*i)+1 < (int) sizeof(progress->backup))
313 progress->backup[(2*i)+1] = 0;
316 progress->skip_progress = 0;
317 if (getenv("MKE2FS_SKIP_PROGRESS"))
318 progress->skip_progress++;
320 fputs(label, stdout);
324 static void progress_update(struct progress_struct *progress, __u32 val)
326 if ((progress->format[0] == 0) || progress->skip_progress)
328 printf(progress->format, val, progress->max);
329 fputs(progress->backup, stdout);
332 static void progress_close(struct progress_struct *progress)
334 if (progress->format[0] == 0)
336 fputs(_("done \n"), stdout);
341 * Helper function which zeros out _num_ blocks starting at _blk_. In
342 * case of an error, the details of the error is returned via _ret_blk_
343 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
344 * success, and an error code on an error.
346 * As a special case, if the first argument is NULL, then it will
347 * attempt to free the static zeroizing buffer. (This is to keep
348 * programs that check for memory leaks happy.)
350 static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
351 struct progress_struct *progress,
352 blk_t *ret_blk, int *ret_count)
354 int j, count, next_update, next_update_incr;
358 /* If fs is null, clean up the static buffer and return */
366 /* Allocate the zeroizing buffer if necessary */
368 buf = malloc(fs->blocksize * STRIDE_LENGTH);
370 com_err("malloc", ENOMEM,
371 _("while allocating zeroizing buffer"));
374 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
376 /* OK, do the write loop */
378 next_update_incr = num / 100;
379 if (next_update_incr < 1)
380 next_update_incr = 1;
381 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
383 if (count > STRIDE_LENGTH)
384 count = STRIDE_LENGTH;
385 retval = io_channel_write_blk(fs->io, blk, count, buf);
393 if (progress && j > next_update) {
394 next_update += num / 100;
395 progress_update(progress, blk);
401 static void write_inode_tables(ext2_filsys fs)
407 struct progress_struct progress;
411 memset(&progress, 0, sizeof(progress));
413 progress_init(&progress, _("Writing inode tables: "),
414 fs->group_desc_count);
416 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
417 EXT2_FEATURE_COMPAT_LAZY_BG))
420 for (i = 0; i < fs->group_desc_count; i++) {
421 progress_update(&progress, i);
423 blk = fs->group_desc[i].bg_inode_table;
424 num = fs->inode_blocks_per_group;
427 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
428 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
430 fprintf(stderr, _("\nCould not write %d "
431 "blocks in inode table starting at %u: %s\n"),
432 num, blk, error_message(retval));
437 if (sync_kludge == 1)
439 else if ((i % sync_kludge) == 0)
443 zero_blocks(0, 0, 0, 0, 0, 0);
444 progress_close(&progress);
447 static void setup_lazy_bg(ext2_filsys fs)
451 struct ext2_super_block *sb = fs->super;
452 struct ext2_group_desc *bg = fs->group_desc;
454 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
455 EXT2_FEATURE_COMPAT_LAZY_BG)) {
456 for (i = 0; i < fs->group_desc_count; i++, bg++) {
458 (i == fs->group_desc_count-1))
460 if (bg->bg_free_inodes_count ==
461 sb->s_inodes_per_group) {
462 bg->bg_free_inodes_count = 0;
463 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
464 sb->s_free_inodes_count -=
465 sb->s_inodes_per_group;
467 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
468 if (bg->bg_free_blocks_count == blks) {
469 bg->bg_free_blocks_count = 0;
470 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
471 sb->s_free_blocks_count -= blks;
478 static void create_root_dir(ext2_filsys fs)
481 struct ext2_inode inode;
484 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
486 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
490 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
492 com_err("ext2fs_read_inode", retval,
493 _("while reading root inode"));
498 inode.i_uid_high = uid >> 16;
502 inode.i_gid_high = gid >> 16;
504 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
506 com_err("ext2fs_write_inode", retval,
507 _("while setting root inode ownership"));
513 static void create_lost_and_found(ext2_filsys fs)
517 const char *name = "lost+found";
522 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
524 com_err("ext2fs_mkdir", retval,
525 _("while creating /lost+found"));
529 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
531 com_err("ext2_lookup", retval,
532 _("while looking up /lost+found"));
536 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
537 if ((lpf_size += fs->blocksize) >= 16*1024)
539 retval = ext2fs_expand_dir(fs, ino);
541 com_err("ext2fs_expand_dir", retval,
542 _("while expanding /lost+found"));
548 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
552 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
553 fs->group_desc[0].bg_free_inodes_count--;
554 fs->super->s_free_inodes_count--;
555 retval = ext2fs_update_bb_inode(fs, bb_list);
557 com_err("ext2fs_update_bb_inode", retval,
558 _("while setting bad block inode"));
564 static void reserve_inodes(ext2_filsys fs)
569 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
570 ext2fs_mark_inode_bitmap(fs->inode_map, i);
571 group = ext2fs_group_of_ino(fs, i);
572 fs->group_desc[group].bg_free_inodes_count--;
573 fs->super->s_free_inodes_count--;
575 ext2fs_mark_ib_dirty(fs);
578 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
579 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
580 #define BSD_LABEL_OFFSET 64
582 static void zap_sector(ext2_filsys fs, int sect, int nsect)
588 buf = malloc(512*nsect);
590 printf(_("Out of memory erasing sectors %d-%d\n"),
591 sect, sect + nsect - 1);
596 /* Check for a BSD disklabel, and don't erase it if so */
597 retval = io_channel_read_blk(fs->io, 0, -512, buf);
600 _("Warning: could not read block 0: %s\n"),
601 error_message(retval));
603 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
604 if ((*magic == BSD_DISKMAGIC) ||
605 (*magic == BSD_MAGICDISK))
610 memset(buf, 0, 512*nsect);
611 io_channel_set_blksize(fs->io, 512);
612 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
613 io_channel_set_blksize(fs->io, fs->blocksize);
616 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
617 sect, error_message(retval));
620 static void create_journal_dev(ext2_filsys fs)
622 struct progress_struct progress;
628 retval = ext2fs_create_journal_superblock(fs,
629 fs->super->s_blocks_count, 0, &buf);
631 com_err("create_journal_dev", retval,
632 _("while initializing journal superblock"));
636 memset(&progress, 0, sizeof(progress));
638 progress_init(&progress, _("Zeroing journal device: "),
639 fs->super->s_blocks_count);
641 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
642 &progress, &blk, &count);
644 com_err("create_journal_dev", retval,
645 _("while zeroing journal device (block %u, count %d)"),
649 zero_blocks(0, 0, 0, 0, 0, 0);
651 retval = io_channel_write_blk(fs->io,
652 fs->super->s_first_data_block+1,
655 com_err("create_journal_dev", retval,
656 _("while writing journal superblock"));
659 progress_close(&progress);
662 static void show_stats(ext2_filsys fs)
664 struct ext2_super_block *s = fs->super;
671 if (fs_param.s_blocks_count != s->s_blocks_count)
672 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
673 fs_param.s_blocks_count - s->s_blocks_count);
675 memset(buf, 0, sizeof(buf));
676 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
677 printf(_("Filesystem label=%s\n"), buf);
678 fputs(_("OS type: "), stdout);
679 os = e2p_os2string(fs->super->s_creator_os);
683 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
684 s->s_log_block_size);
685 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
687 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
689 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
691 100.0 * s->s_r_blocks_count / s->s_blocks_count);
692 printf(_("First data block=%u\n"), s->s_first_data_block);
693 if (s->s_reserved_gdt_blocks)
694 printf(_("Maximum filesystem blocks=%lu\n"),
695 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
696 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
697 if (fs->group_desc_count > 1)
698 printf(_("%u block groups\n"), fs->group_desc_count);
700 printf(_("%u block group\n"), fs->group_desc_count);
701 printf(_("%u blocks per group, %u fragments per group\n"),
702 s->s_blocks_per_group, s->s_frags_per_group);
703 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
705 if (fs->group_desc_count == 1) {
710 printf(_("Superblock backups stored on blocks: "));
711 group_block = s->s_first_data_block;
713 for (i = 1; i < fs->group_desc_count; i++) {
714 group_block += s->s_blocks_per_group;
715 if (!ext2fs_bg_has_super(fs, i))
719 need = int_log10(group_block) + 2;
720 if (need > col_left) {
725 printf("%u", group_block);
731 * Set the S_CREATOR_OS field. Return true if OS is known,
734 static int set_os(struct ext2_super_block *sb, char *os)
737 sb->s_creator_os = atoi (os);
738 else if (strcasecmp(os, "linux") == 0)
739 sb->s_creator_os = EXT2_OS_LINUX;
740 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
741 sb->s_creator_os = EXT2_OS_HURD;
742 else if (strcasecmp(os, "freebsd") == 0)
743 sb->s_creator_os = EXT2_OS_FREEBSD;
744 else if (strcasecmp(os, "lites") == 0)
745 sb->s_creator_os = EXT2_OS_LITES;
751 #define PATH_SET "PATH=/sbin"
753 static void parse_extended_opts(struct ext2_super_block *param,
756 char *buf, *token, *next, *p, *arg;
764 _("Couldn't allocate memory to parse options!\n"));
768 for (token = buf; token && *token; token = next) {
769 p = strchr(token, ',');
775 arg = strchr(token, '=');
780 if (strcmp(token, "stride") == 0) {
785 fs_stride = strtoul(arg, &p, 0);
786 if (*p || (fs_stride == 0)) {
788 _("Invalid stride parameter: %s\n"),
793 } else if (!strcmp(token, "resize")) {
794 unsigned long resize, bpg, rsv_groups;
795 unsigned long group_desc_count, desc_blocks;
796 unsigned int gdpb, blocksize;
804 resize = parse_num_blocks(arg,
805 param->s_log_block_size);
809 _("Invalid resize parameter: %s\n"),
814 if (resize <= param->s_blocks_count) {
816 _("The resize maximum must be greater "
817 "than the filesystem size.\n"));
822 blocksize = EXT2_BLOCK_SIZE(param);
823 bpg = param->s_blocks_per_group;
826 gdpb = EXT2_DESC_PER_BLOCK(param);
828 ext2fs_div_ceil(param->s_blocks_count, bpg);
829 desc_blocks = (group_desc_count +
831 rsv_groups = ext2fs_div_ceil(resize, bpg);
832 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
834 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
835 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
838 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
840 _("On-line resizing not supported with revision 0 filesystems\n"));
844 param->s_feature_compat |=
845 EXT2_FEATURE_COMPAT_RESIZE_INODE;
847 param->s_reserved_gdt_blocks = rsv_gdb;
853 fprintf(stderr, _("\nBad options specified.\n\n"
854 "Extended options are separated by commas, "
855 "and may take an argument which\n"
856 "\tis set off by an equals ('=') sign.\n\n"
857 "Valid extended options are:\n"
858 "\tstride=<stride length in blocks>\n"
859 "\tresize=<resize maximum size in blocks>\n\n"));
866 static __u32 ok_features[3] = {
867 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
868 EXT2_FEATURE_COMPAT_RESIZE_INODE |
869 EXT2_FEATURE_COMPAT_DIR_INDEX |
870 EXT2_FEATURE_COMPAT_LAZY_BG, /* Compat */
871 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
872 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
873 EXT2_FEATURE_INCOMPAT_META_BG|
874 EXT4_FEATURE_INCOMPAT_FLEX_BG,
875 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
879 static void syntax_err_report(const char *filename, long err, int line_num)
882 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
883 filename, line_num, error_message(err));
887 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
889 static void edit_feature(const char *str, __u32 *compat_array)
894 if (e2p_edit_feature(str, compat_array, ok_features)) {
895 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
901 extern const char *mke2fs_default_profile;
902 static const char *default_files[] = { "<default>", 0 };
904 static void PRS(int argc, char *argv[])
912 double reserved_ratio = 5.0;
914 int show_version_only = 0;
915 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
917 char * oldpath = getenv("PATH");
918 char * extended_opts = 0;
919 const char * fs_type = 0;
925 int s_opt = -1, r_opt = -1;
926 char *fs_features = 0;
929 int pathlen = sizeof(PATH_SET) + 1;
932 pathlen += strlen(oldpath);
933 newpath = malloc(pathlen);
934 strcpy(newpath, PATH_SET);
936 /* Update our PATH to include /sbin */
938 strcat (newpath, ":");
939 strcat (newpath, oldpath);
943 tmp = getenv("MKE2FS_SYNC");
945 sync_kludge = atoi(tmp);
947 /* Determine the system page size if possible */
949 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
950 #define _SC_PAGESIZE _SC_PAGE_SIZE
953 sysval = sysconf(_SC_PAGESIZE);
955 sys_page_size = sysval;
956 #endif /* _SC_PAGESIZE */
957 #endif /* HAVE_SYSCONF */
959 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
961 profile_set_syntax_err_cb(syntax_err_report);
962 retval = profile_init(config_fn, &profile);
963 if (retval == ENOENT) {
964 profile_init(default_files, &profile);
965 profile_set_default(profile, mke2fs_default_profile);
968 setbuf(stdout, NULL);
969 setbuf(stderr, NULL);
970 add_error_table(&et_ext2_error_table);
971 add_error_table(&et_prof_error_table);
972 memset(&fs_param, 0, sizeof(struct ext2_super_block));
973 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
980 linux_version_code = parse_version_number(ut.release);
981 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
982 fs_param.s_rev_level = 0;
986 program_name = get_progname(*argv);
988 /* If called as mkfs.ext3, create a journal inode */
989 if (!strcmp(program_name, "mkfs.ext3"))
993 while ((c = getopt (argc, argv,
994 "b:cf:g:i:jl:m:no:qr:s:tvE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
997 blocksize = strtol(optarg, &tmp, 0);
998 b = (blocksize > 0) ? blocksize : -blocksize;
999 if (b < EXT2_MIN_BLOCK_SIZE ||
1000 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1001 com_err(program_name, 0,
1002 _("invalid block size - %s"), optarg);
1005 if (blocksize > 4096)
1006 fprintf(stderr, _("Warning: blocksize %d not "
1007 "usable on most systems.\n"),
1010 fs_param.s_log_block_size =
1011 int_log2(blocksize >>
1012 EXT2_MIN_BLOCK_LOG_SIZE);
1014 case 'c': /* Check for bad blocks */
1015 case 't': /* deprecated */
1019 size = strtoul(optarg, &tmp, 0);
1020 if (size < EXT2_MIN_BLOCK_SIZE ||
1021 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1022 com_err(program_name, 0,
1023 _("invalid fragment size - %s"),
1027 fs_param.s_log_frag_size =
1028 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1029 fprintf(stderr, _("Warning: fragments not supported. "
1030 "Ignoring -f option\n"));
1033 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1035 com_err(program_name, 0,
1036 _("Illegal number for blocks per group"));
1039 if ((fs_param.s_blocks_per_group % 8) != 0) {
1040 com_err(program_name, 0,
1041 _("blocks per group must be multiple of 8"));
1046 inode_ratio = strtoul(optarg, &tmp, 0);
1047 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1048 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1050 com_err(program_name, 0,
1051 _("invalid inode ratio %s (min %d/max %d)"),
1052 optarg, EXT2_MIN_BLOCK_SIZE,
1053 EXT2_MAX_BLOCK_SIZE);
1058 parse_journal_opts(optarg);
1065 bad_blocks_filename = malloc(strlen(optarg)+1);
1066 if (!bad_blocks_filename) {
1067 com_err(program_name, ENOMEM,
1068 _("in malloc for bad_blocks_filename"));
1071 strcpy(bad_blocks_filename, optarg);
1074 reserved_ratio = strtod(optarg, &tmp);
1075 if (reserved_ratio > 50 || *tmp) {
1076 com_err(program_name, 0,
1077 _("invalid reserved blocks percent - %s"),
1086 creator_os = optarg;
1092 r_opt = strtoul(optarg, &tmp, 0);
1094 com_err(program_name, 0,
1095 _("bad revision level - %s"), optarg);
1098 fs_param.s_rev_level = r_opt;
1100 case 's': /* deprecated */
1101 s_opt = atoi(optarg);
1104 inode_size = strtoul(optarg, &tmp, 0);
1106 com_err(program_name, 0,
1107 _("invalid inode size - %s"), optarg);
1118 volume_label = optarg;
1124 num_inodes = strtoul(optarg, &tmp, 0);
1126 com_err(program_name, 0,
1127 _("bad num inodes - %s"), optarg);
1132 fs_features = optarg;
1136 extended_opts = optarg;
1145 /* Print version number and exit */
1146 show_version_only++;
1152 if ((optind == argc) && !show_version_only)
1154 device_name = argv[optind++];
1156 if (!quiet || show_version_only)
1157 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1160 if (show_version_only) {
1161 fprintf(stderr, _("\tUsing %s\n"),
1162 error_message(EXT2_ET_BASE));
1167 * If there's no blocksize specified and there is a journal
1168 * device, use it to figure out the blocksize
1170 if (blocksize <= 0 && journal_device) {
1174 #ifdef CONFIG_TESTIO_DEBUG
1175 io_ptr = test_io_manager;
1176 test_io_backing_manager = unix_io_manager;
1178 io_ptr = unix_io_manager;
1180 retval = ext2fs_open(journal_device,
1181 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1184 com_err(program_name, retval,
1185 _("while trying to open journal device %s\n"),
1189 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1190 com_err(program_name, 0,
1191 _("Journal dev blocksize (%d) smaller than "
1192 "minimum blocksize %d\n"), jfs->blocksize,
1196 blocksize = jfs->blocksize;
1197 fs_param.s_log_block_size =
1198 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1202 if (blocksize > sys_page_size) {
1204 com_err(program_name, 0,
1205 _("%d-byte blocks too big for system (max %d)"),
1206 blocksize, sys_page_size);
1209 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1210 "(max %d), forced to continue\n"),
1211 blocksize, sys_page_size);
1213 if (optind < argc) {
1214 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1215 fs_param.s_log_block_size);
1216 if (!fs_param.s_blocks_count) {
1217 com_err(program_name, 0, _("invalid blocks count - %s"),
1226 check_plausibility(device_name);
1227 check_mount(device_name, force, _("filesystem"));
1229 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1231 if (noaction && fs_param.s_blocks_count) {
1232 dev_size = fs_param.s_blocks_count;
1236 retval = ext2fs_get_device_size(device_name,
1237 EXT2_BLOCK_SIZE(&fs_param),
1239 if ((retval == EFBIG) &&
1241 (fs_param.s_log_block_size == 0)) {
1242 fs_param.s_log_block_size = 2;
1248 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1249 com_err(program_name, retval,
1250 _("while trying to determine filesystem size"));
1253 if (!fs_param.s_blocks_count) {
1254 if (retval == EXT2_ET_UNIMPLEMENTED) {
1255 com_err(program_name, 0,
1256 _("Couldn't determine device size; you "
1257 "must specify\nthe size of the "
1261 if (dev_size == 0) {
1262 com_err(program_name, 0,
1263 _("Device size reported to be zero. "
1264 "Invalid partition specified, or\n\t"
1265 "partition table wasn't reread "
1266 "after running fdisk, due to\n\t"
1267 "a modified partition being busy "
1268 "and in use. You may need to reboot\n\t"
1269 "to re-read your partition table.\n"
1273 fs_param.s_blocks_count = dev_size;
1274 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1275 fs_param.s_blocks_count &= ~((sys_page_size /
1276 EXT2_BLOCK_SIZE(&fs_param))-1);
1279 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1280 com_err(program_name, 0,
1281 _("Filesystem larger than apparent device size."));
1286 int megs = (__u64)fs_param.s_blocks_count *
1287 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1289 if (fs_param.s_feature_incompat &
1290 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1291 fs_type = "journal";
1294 else if (megs <= 512)
1297 fs_type = "default";
1300 /* Figure out what features should be enabled */
1303 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1304 profile_get_string(profile, "defaults", "base_features", 0,
1305 "sparse_super,filetype,resize_inode,dir_index",
1307 profile_get_string(profile, "fs_types", fs_type,
1308 "base_features", tmp, &tmp2);
1309 edit_feature(tmp2, &fs_param.s_feature_compat);
1314 profile_get_string(profile, "defaults", "default_features", 0,
1316 profile_get_string(profile, "fs_types", fs_type,
1317 "default_features", tmp, &tmp2);
1319 edit_feature(fs_features ? fs_features : tmp2,
1320 &fs_param.s_feature_compat);
1326 if (r_opt == EXT2_GOOD_OLD_REV &&
1327 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1328 fs_param.s_feature_incompat)) {
1329 fprintf(stderr, _("Filesystem features not supported "
1330 "with revision 0 filesystems\n"));
1335 if (r_opt == EXT2_GOOD_OLD_REV) {
1336 fprintf(stderr, _("Sparse superblocks not supported "
1337 "with revision 0 filesystems\n"));
1340 fs_param.s_feature_ro_compat |=
1341 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1342 } else if (s_opt == 0)
1343 fs_param.s_feature_ro_compat &=
1344 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1346 if (journal_size != 0) {
1347 if (r_opt == EXT2_GOOD_OLD_REV) {
1348 fprintf(stderr, _("Journals not supported "
1349 "with revision 0 filesystems\n"));
1352 fs_param.s_feature_compat |=
1353 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1356 if (fs_param.s_feature_incompat &
1357 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1359 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1360 fs_param.s_feature_compat = 0;
1361 fs_param.s_feature_ro_compat = 0;
1364 /* Set first meta blockgroup via an environment variable */
1365 /* (this is mostly for debugging purposes) */
1366 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1367 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1368 fs_param.s_first_meta_bg = atoi(tmp);
1370 /* Get the hardware sector size, if available */
1371 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1373 com_err(program_name, retval,
1374 _("while trying to determine hardware sector size"));
1378 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1379 sector_size = atoi(tmp);
1381 if (blocksize <= 0) {
1382 profile_get_integer(profile, "defaults", "blocksize", 0,
1384 profile_get_integer(profile, "fs_types", fs_type,
1385 "blocksize", use_bsize, &use_bsize);
1387 if (use_bsize == -1) {
1388 use_bsize = sys_page_size;
1389 if ((linux_version_code < (2*65536 + 6*256)) &&
1393 if (sector_size && use_bsize < sector_size)
1394 use_bsize = sector_size;
1395 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1396 use_bsize = -blocksize;
1397 blocksize = use_bsize;
1398 fs_param.s_blocks_count /= blocksize / 1024;
1401 if (inode_ratio == 0) {
1402 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1403 8192, &inode_ratio);
1404 profile_get_integer(profile, "fs_types", fs_type,
1405 "inode_ratio", inode_ratio,
1408 if (inode_ratio < blocksize)
1409 inode_ratio = blocksize;
1412 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1413 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1415 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1418 parse_extended_opts(&fs_param, extended_opts);
1420 /* Since sparse_super is the default, we would only have a problem
1421 * here if it was explicitly disabled.
1423 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1424 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1425 com_err(program_name, 0,
1426 _("reserved online resize blocks not supported "
1427 "on non-sparse filesystem"));
1431 if (fs_param.s_blocks_per_group) {
1432 if (fs_param.s_blocks_per_group < 256 ||
1433 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1434 com_err(program_name, 0,
1435 _("blocks per group count out of range"));
1440 if (!force && fs_param.s_blocks_count >= ((unsigned) 1 << 31)) {
1441 com_err(program_name, 0,
1442 _("Filesystem too large. No more than 2**31-1 blocks\n"
1443 "\t (8TB using a blocksize of 4k) are currently supported."));
1447 if ((blocksize > 4096) &&
1448 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1449 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1450 "blocksizes greater than 4096\n\tusing ext3. "
1451 "Use -b 4096 if this is an issue for you.\n\n"));
1453 if (inode_size == 0) {
1454 profile_get_integer(profile, "defaults", "inode_size", NULL,
1456 profile_get_integer(profile, "fs_types", fs_type,
1457 "inode_size", inode_size,
1461 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1462 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1463 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1464 inode_size & (inode_size - 1)) {
1465 com_err(program_name, 0,
1466 _("invalid inode size %d (min %d/max %d)"),
1467 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1471 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1472 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1473 "on older systems\n"),
1475 fs_param.s_inode_size = inode_size;
1478 /* Make sure number of inodes specified will fit in 32 bits */
1479 if (num_inodes == 0) {
1480 unsigned long long n;
1481 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
1483 com_err(program_name, 0,
1484 _("too many inodes (%llu), raise inode ratio?"), n);
1487 } else if (num_inodes > ~0U) {
1488 com_err(program_name, 0,
1489 _("too many inodes (%llu), specify < 2^32 inodes"),
1494 * Calculate number of inodes based on the inode ratio
1496 fs_param.s_inodes_count = num_inodes ? num_inodes :
1497 ((__u64) fs_param.s_blocks_count * blocksize)
1500 if ((((long long)fs_param.s_inodes_count) *
1501 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1502 (((long long)fs_param.s_blocks_count) *
1503 EXT2_BLOCK_SIZE(&fs_param))) {
1504 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1505 "(%u) too big for a\n\t"
1506 "filesystem with %lu blocks, "
1507 "specify higher inode_ratio (-i)\n\t"
1508 "or lower inode count (-N).\n"),
1509 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1510 fs_param.s_inodes_count,
1511 (unsigned long) fs_param.s_blocks_count);
1516 * Calculate number of blocks to reserve
1518 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1519 fs_param.s_blocks_count);
1522 int main (int argc, char *argv[])
1524 errcode_t retval = 0;
1526 badblocks_list bb_list = 0;
1527 unsigned int journal_blocks;
1533 setlocale(LC_MESSAGES, "");
1534 setlocale(LC_CTYPE, "");
1535 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1536 textdomain(NLS_CAT_NAME);
1540 #ifdef CONFIG_TESTIO_DEBUG
1541 io_ptr = test_io_manager;
1542 test_io_backing_manager = unix_io_manager;
1544 io_ptr = unix_io_manager;
1548 * Initialize the superblock....
1550 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
1553 com_err(device_name, retval, _("while setting up superblock"));
1558 * Wipe out the old on-disk superblock
1561 zap_sector(fs, 2, 6);
1564 * Generate a UUID for it...
1566 uuid_generate(fs->super->s_uuid);
1569 * Initialize the directory index variables
1571 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1572 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1575 * Add "jitter" to the superblock's check interval so that we
1576 * don't check all the filesystems at the same time. We use a
1577 * kludgy hack of using the UUID to derive a random jitter value.
1579 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1580 val += fs->super->s_uuid[i];
1581 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1584 * Override the creator OS, if applicable
1586 if (creator_os && !set_os(fs->super, creator_os)) {
1587 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1592 * For the Hurd, we will turn off filetype since it doesn't
1595 if (fs->super->s_creator_os == EXT2_OS_HURD)
1596 fs->super->s_feature_incompat &=
1597 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1600 * Set the volume label...
1603 memset(fs->super->s_volume_name, 0,
1604 sizeof(fs->super->s_volume_name));
1605 strncpy(fs->super->s_volume_name, volume_label,
1606 sizeof(fs->super->s_volume_name));
1610 * Set the last mount directory
1613 memset(fs->super->s_last_mounted, 0,
1614 sizeof(fs->super->s_last_mounted));
1615 strncpy(fs->super->s_last_mounted, mount_dir,
1616 sizeof(fs->super->s_last_mounted));
1619 if (!quiet || noaction)
1625 if (fs->super->s_feature_incompat &
1626 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1627 create_journal_dev(fs);
1628 exit(ext2fs_close(fs) ? 1 : 0);
1631 if (bad_blocks_filename)
1632 read_bb_file(fs, &bb_list, bad_blocks_filename);
1634 test_disk(fs, &bb_list);
1636 handle_bad_blocks(fs, bb_list);
1637 fs->stride = fs->super->s_raid_stride = fs_stride;
1638 retval = ext2fs_allocate_tables(fs);
1640 com_err(program_name, retval,
1641 _("while trying to allocate filesystem tables"));
1645 fs->super->s_state |= EXT2_ERROR_FS;
1646 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1648 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1649 unsigned int rsv = 65536 / fs->blocksize;
1650 unsigned long blocks = fs->super->s_blocks_count;
1651 unsigned long start;
1654 #ifdef ZAP_BOOTBLOCK
1655 zap_sector(fs, 0, 2);
1659 * Wipe out any old MD RAID (or other) metadata at the end
1660 * of the device. This will also verify that the device is
1661 * as large as we think. Be careful with very small devices.
1663 start = (blocks & ~(rsv - 1));
1667 retval = zero_blocks(fs, start, blocks - start,
1668 NULL, &ret_blk, NULL);
1671 com_err(program_name, retval,
1672 _("while zeroing block %u at end of filesystem"),
1676 write_inode_tables(fs);
1677 create_root_dir(fs);
1678 create_lost_and_found(fs);
1680 create_bad_block_inode(fs, bb_list);
1681 if (fs->super->s_feature_compat &
1682 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1683 retval = ext2fs_create_resize_inode(fs);
1685 com_err("ext2fs_create_resize_inode", retval,
1686 _("while reserving blocks for online resize"));
1692 if (journal_device) {
1696 check_plausibility(journal_device);
1697 check_mount(journal_device, force, _("journal"));
1699 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1700 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1701 fs->blocksize, unix_io_manager, &jfs);
1703 com_err(program_name, retval,
1704 _("while trying to open journal device %s\n"),
1709 printf(_("Adding journal to device %s: "),
1713 retval = ext2fs_add_journal_device(fs, jfs);
1715 com_err (program_name, retval,
1716 _("\n\twhile trying to add journal to device %s"),
1721 printf(_("done\n"));
1723 free(journal_device);
1724 } else if ((journal_size) ||
1725 (fs_param.s_feature_compat &
1726 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1727 journal_blocks = figure_journal_size(journal_size, fs);
1729 if (!journal_blocks) {
1730 fs->super->s_feature_compat &=
1731 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1735 printf(_("Creating journal (%u blocks): "),
1739 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1742 com_err (program_name, retval,
1743 _("\n\twhile trying to create journal"));
1747 printf(_("done\n"));
1752 printf(_("Writing superblocks and "
1753 "filesystem accounting information: "));
1754 retval = ext2fs_flush(fs);
1757 _("\nWarning, had trouble writing out superblocks."));
1760 printf(_("done\n\n"));
1761 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1762 print_check_message(fs);
1764 val = ext2fs_close(fs);
1765 remove_error_table(&et_ext2_error_table);
1766 remove_error_table(&et_prof_error_table);
1767 return (retval || val) ? 1 : 0;