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"
55 #include "../version.h"
56 #include "nls-enable.h"
58 #define STRIDE_LENGTH 8
64 extern int isatty(int);
65 extern FILE *fpopen(const char *cmd, const char *mode);
67 const char * program_name = "mke2fs";
68 const char * device_name /* = NULL */;
70 /* Command line options */
79 char *bad_blocks_filename;
82 struct ext2_super_block fs_param;
87 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
91 int sys_page_size = 4096;
92 int linux_version_code = 0;
94 static void usage(void)
96 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
97 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
98 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
99 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
100 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
101 "[-r fs-revision] [-R options] [-qvSV] device [blocks-count]\n"),
106 static int int_log2(int arg)
118 static int int_log10(unsigned int arg)
127 static int parse_version_number(const char *s)
129 int major, minor, rev;
135 major = strtol(cp, &endptr, 10);
136 if (cp == endptr || *endptr != '.')
139 minor = strtol(cp, &endptr, 10);
140 if (cp == endptr || *endptr != '.')
143 rev = strtol(cp, &endptr, 10);
146 return ((((major * 256) + minor) * 256) + rev);
150 * Helper function for read_bb_file and test_disk
152 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
154 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
159 * Reads the bad blocks list from a file
161 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
162 const char *bad_blocks_file)
167 f = fopen(bad_blocks_file, "r");
169 com_err("read_bad_blocks_file", errno,
170 _("while trying to open %s"), bad_blocks_file);
173 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
176 com_err("ext2fs_read_bb_FILE", retval,
177 _("while reading in list of bad blocks from file"));
183 * Runs the badblocks program to test the disk
185 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
191 sprintf(buf, "badblocks -b %d -X %s%s%s %d", fs->blocksize,
192 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
193 fs->device_name, fs->super->s_blocks_count);
195 printf(_("Running command: %s\n"), buf);
198 com_err("popen", errno,
199 _("while trying to run '%s'"), buf);
202 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
205 com_err("ext2fs_read_bb_FILE", retval,
206 _("while processing list of bad blocks from program"));
211 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
215 unsigned must_be_good;
217 badblocks_iterate bb_iter;
227 * The primary superblock and group descriptors *must* be
228 * good; if not, abort.
230 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
231 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
232 if (ext2fs_badblocks_list_test(bb_list, i)) {
233 fprintf(stderr, _("Block %d in primary "
234 "superblock/group descriptor area bad.\n"), i);
235 fprintf(stderr, _("Blocks %u through %d must be good "
236 "in order to build a filesystem.\n"),
237 fs->super->s_first_data_block, must_be_good);
238 fputs(_("Aborting....\n"), stderr);
244 * See if any of the bad blocks are showing up in the backup
245 * superblocks and/or group descriptors. If so, issue a
246 * warning and adjust the block counts appropriately.
248 group_block = fs->super->s_first_data_block +
249 fs->super->s_blocks_per_group;
251 for (i = 1; i < fs->group_desc_count; i++) {
253 for (j=0; j < fs->desc_blocks+1; j++) {
254 if (ext2fs_badblocks_list_test(bb_list,
258 _("Warning: the backup superblock/group descriptors at block %u contain\n"
262 group = ext2fs_group_of_blk(fs, group_block+j);
263 fs->group_desc[group].bg_free_blocks_count++;
264 fs->super->s_free_blocks_count++;
267 group_block += fs->super->s_blocks_per_group;
271 * Mark all the bad blocks as used...
273 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
275 com_err("ext2fs_badblocks_list_iterate_begin", retval,
276 _("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 = malloc(fs->blocksize * STRIDE_LENGTH);
368 com_err("malloc", ENOMEM,
369 _("while allocating zeroizing buffer"));
372 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
374 /* OK, do the write loop */
376 next_update_incr = num / 100;
377 if (next_update_incr < 1)
378 next_update_incr = 1;
379 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
381 if (count > STRIDE_LENGTH)
382 count = STRIDE_LENGTH;
383 retval = io_channel_write_blk(fs->io, blk, count, buf);
391 if (progress && j > next_update) {
392 next_update += num / 100;
393 progress_update(progress, blk);
399 static void write_inode_tables(ext2_filsys fs)
405 struct progress_struct progress;
409 memset(&progress, 0, sizeof(progress));
411 progress_init(&progress, _("Writing inode tables: "),
412 fs->group_desc_count);
414 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
415 EXT2_FEATURE_COMPAT_LAZY_BG))
418 for (i = 0; i < fs->group_desc_count; i++) {
419 progress_update(&progress, i);
421 blk = fs->group_desc[i].bg_inode_table;
422 num = fs->inode_blocks_per_group;
425 (fs->group_desc[i].bg_flags & EXT2_BG_INODE_UNINIT))) {
426 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
428 fprintf(stderr, _("\nCould not write %d "
429 "blocks in inode table starting at %u: %s\n"),
430 num, blk, error_message(retval));
435 if (sync_kludge == 1)
437 else if ((i % sync_kludge) == 0)
441 zero_blocks(0, 0, 0, 0, 0, 0);
442 progress_close(&progress);
445 static void setup_lazy_bg(ext2_filsys fs)
449 struct ext2_super_block *sb = fs->super;
450 struct ext2_group_desc *bg = fs->group_desc;
452 if (EXT2_HAS_COMPAT_FEATURE(fs->super,
453 EXT2_FEATURE_COMPAT_LAZY_BG)) {
454 for (i = 0; i < fs->group_desc_count; i++, bg++) {
456 (i == fs->group_desc_count-1))
458 if (bg->bg_free_inodes_count ==
459 sb->s_inodes_per_group) {
460 bg->bg_free_inodes_count = 0;
461 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
462 sb->s_free_inodes_count -=
463 sb->s_inodes_per_group;
465 blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
466 if (bg->bg_free_blocks_count == blks) {
467 bg->bg_free_blocks_count = 0;
468 bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
469 sb->s_free_blocks_count -= blks;
476 static void create_root_dir(ext2_filsys fs)
479 struct ext2_inode inode;
481 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
483 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
487 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
489 com_err("ext2fs_read_inode", retval,
490 _("while reading root inode"));
493 inode.i_uid = getuid();
495 inode.i_gid = getgid();
496 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
498 com_err("ext2fs_write_inode", retval,
499 _("while setting root inode ownership"));
505 static void create_lost_and_found(ext2_filsys fs)
509 const char *name = "lost+found";
514 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
516 com_err("ext2fs_mkdir", retval,
517 _("while creating /lost+found"));
521 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
523 com_err("ext2_lookup", retval,
524 _("while looking up /lost+found"));
528 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
529 if ((lpf_size += fs->blocksize) >= 16*1024)
531 retval = ext2fs_expand_dir(fs, ino);
533 com_err("ext2fs_expand_dir", retval,
534 _("while expanding /lost+found"));
540 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
544 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
545 fs->group_desc[0].bg_free_inodes_count--;
546 fs->super->s_free_inodes_count--;
547 retval = ext2fs_update_bb_inode(fs, bb_list);
549 com_err("ext2fs_update_bb_inode", retval,
550 _("while setting bad block inode"));
556 static void reserve_inodes(ext2_filsys fs)
561 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
562 ext2fs_mark_inode_bitmap(fs->inode_map, i);
563 group = ext2fs_group_of_ino(fs, i);
564 fs->group_desc[group].bg_free_inodes_count--;
565 fs->super->s_free_inodes_count--;
567 ext2fs_mark_ib_dirty(fs);
570 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
571 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
572 #define BSD_LABEL_OFFSET 64
574 static void zap_sector(ext2_filsys fs, int sect, int nsect)
580 buf = malloc(512*nsect);
582 printf(_("Out of memory erasing sectors %d-%d\n"),
583 sect, sect + nsect - 1);
588 /* Check for a BSD disklabel, and don't erase it if so */
589 retval = io_channel_read_blk(fs->io, 0, -512, buf);
592 _("Warning: could not read block 0: %s\n"),
593 error_message(retval));
595 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
596 if ((*magic == BSD_DISKMAGIC) ||
597 (*magic == BSD_MAGICDISK))
602 memset(buf, 0, 512*nsect);
603 io_channel_set_blksize(fs->io, 512);
604 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
605 io_channel_set_blksize(fs->io, fs->blocksize);
608 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
609 sect, error_message(retval));
612 static void create_journal_dev(ext2_filsys fs)
614 struct progress_struct progress;
620 retval = ext2fs_create_journal_superblock(fs,
621 fs->super->s_blocks_count, 0, &buf);
623 com_err("create_journal_dev", retval,
624 _("while initializing journal superblock"));
628 memset(&progress, 0, sizeof(progress));
630 progress_init(&progress, _("Zeroing journal device: "),
631 fs->super->s_blocks_count);
633 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
634 &progress, &blk, &count);
636 com_err("create_journal_dev", retval,
637 _("while zeroing journal device (block %u, count %d)"),
641 zero_blocks(0, 0, 0, 0, 0, 0);
643 retval = io_channel_write_blk(fs->io,
644 fs->super->s_first_data_block+1,
647 com_err("create_journal_dev", retval,
648 _("while writing journal superblock"));
651 progress_close(&progress);
654 static void show_stats(ext2_filsys fs)
656 struct ext2_super_block *s = fs->super;
663 if (fs_param.s_blocks_count != s->s_blocks_count)
664 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
665 fs_param.s_blocks_count - s->s_blocks_count);
667 memset(buf, 0, sizeof(buf));
668 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
669 printf(_("Filesystem label=%s\n"), buf);
670 fputs(_("OS type: "), stdout);
671 os = e2p_os2string(fs->super->s_creator_os);
675 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
676 s->s_log_block_size);
677 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
679 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
681 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
683 100.0 * s->s_r_blocks_count / s->s_blocks_count);
684 printf(_("First data block=%u\n"), s->s_first_data_block);
685 if (s->s_reserved_gdt_blocks)
686 printf(_("Maximum filesystem blocks=%lu\n"),
687 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
688 (fs->blocksize / sizeof(struct ext2_group_desc)) *
689 s->s_blocks_per_group);
690 if (fs->group_desc_count > 1)
691 printf(_("%u block groups\n"), fs->group_desc_count);
693 printf(_("%u block group\n"), fs->group_desc_count);
694 printf(_("%u blocks per group, %u fragments per group\n"),
695 s->s_blocks_per_group, s->s_frags_per_group);
696 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
698 if (fs->group_desc_count == 1) {
703 printf(_("Superblock backups stored on blocks: "));
704 group_block = s->s_first_data_block;
706 for (i = 1; i < fs->group_desc_count; i++) {
707 group_block += s->s_blocks_per_group;
708 if (!ext2fs_bg_has_super(fs, i))
712 need = int_log10(group_block) + 2;
713 if (need > col_left) {
718 printf("%u", group_block);
724 * Set the S_CREATOR_OS field. Return true if OS is known,
727 static int set_os(struct ext2_super_block *sb, char *os)
730 sb->s_creator_os = atoi (os);
731 else if (strcasecmp(os, "linux") == 0)
732 sb->s_creator_os = EXT2_OS_LINUX;
733 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
734 sb->s_creator_os = EXT2_OS_HURD;
735 else if (strcasecmp(os, "masix") == 0)
736 sb->s_creator_os = EXT2_OS_MASIX;
737 else if (strcasecmp(os, "freebsd") == 0)
738 sb->s_creator_os = EXT2_OS_FREEBSD;
739 else if (strcasecmp(os, "lites") == 0)
740 sb->s_creator_os = EXT2_OS_LITES;
746 #define PATH_SET "PATH=/sbin"
748 static void parse_extended_opts(struct ext2_super_block *param,
751 char *buf, *token, *next, *p, *arg;
759 _("Couldn't allocate memory to parse options!\n"));
763 for (token = buf; token && *token; token = next) {
764 p = strchr(token, ',');
770 arg = strchr(token, '=');
775 if (strcmp(token, "stride") == 0) {
780 fs_stride = strtoul(arg, &p, 0);
781 if (*p || (fs_stride == 0)) {
783 _("Invalid stride parameter: %s\n"),
788 } else if (!strcmp(token, "resize")) {
789 unsigned long resize, bpg, rsv_groups;
790 unsigned long group_desc_count, desc_blocks;
791 unsigned int gdpb, blocksize;
799 resize = parse_num_blocks(arg,
800 param->s_log_block_size);
804 _("Invalid resize parameter: %s\n"),
809 if (resize <= param->s_blocks_count) {
811 _("The resize maximum must be greater "
812 "than the filesystem size.\n"));
817 blocksize = EXT2_BLOCK_SIZE(param);
818 bpg = param->s_blocks_per_group;
821 gdpb = blocksize / sizeof(struct ext2_group_desc);
822 group_desc_count = (param->s_blocks_count +
824 desc_blocks = (group_desc_count +
826 rsv_groups = (resize + bpg - 1) / bpg;
827 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
829 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
830 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
833 param->s_feature_compat |=
834 EXT2_FEATURE_COMPAT_RESIZE_INODE;
836 param->s_reserved_gdt_blocks = rsv_gdb;
842 fprintf(stderr, _("\nBad options specified.\n\n"
843 "Extended options are separated by commas, "
844 "and may take an argument which\n"
845 "\tis set off by an equals ('=') sign.\n\n"
846 "Valid extended options are:\n"
847 "\tstride=<stride length in blocks>\n"
848 "\tresize=<resize maximum size in blocks>\n\n"));
853 static __u32 ok_features[3] = {
854 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
855 EXT2_FEATURE_COMPAT_RESIZE_INODE |
856 EXT2_FEATURE_COMPAT_DIR_INDEX |
857 EXT2_FEATURE_COMPAT_LAZY_BG, /* Compat */
858 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
859 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
860 EXT2_FEATURE_INCOMPAT_META_BG,
861 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
865 static void syntax_err_report(const char *filename, long err, int line_num)
868 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
869 filename, line_num, error_message(err));
873 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
875 static void edit_feature(const char *str, __u32 *compat_array)
880 if (e2p_edit_feature(str, compat_array, ok_features)) {
881 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
887 static void PRS(int argc, char *argv[])
895 double reserved_ratio = 5.0;
897 int show_version_only = 0;
898 ext2_ino_t num_inodes = 0;
900 char * oldpath = getenv("PATH");
901 char * extended_opts = 0;
902 const char * fs_type = 0;
908 int s_opt = -1, r_opt = -1;
909 char *fs_features = 0;
912 /* Update our PATH to include /sbin */
916 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
917 strcpy (newpath, PATH_SET);
918 strcat (newpath, ":");
919 strcat (newpath, oldpath);
924 tmp = getenv("MKE2FS_SYNC");
926 sync_kludge = atoi(tmp);
928 /* Determine the system page size if possible */
930 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
931 #define _SC_PAGESIZE _SC_PAGE_SIZE
934 sysval = sysconf(_SC_PAGESIZE);
936 sys_page_size = sysval;
937 #endif /* _SC_PAGESIZE */
938 #endif /* HAVE_SYSCONF */
940 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
942 profile_set_syntax_err_cb(syntax_err_report);
943 profile_init(config_fn, &profile);
945 setbuf(stdout, NULL);
946 setbuf(stderr, NULL);
947 initialize_ext2_error_table();
948 memset(&fs_param, 0, sizeof(struct ext2_super_block));
949 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
956 linux_version_code = parse_version_number(ut.release);
957 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
958 fs_param.s_rev_level = 0;
962 program_name = get_progname(*argv);
964 /* If called as mkfs.ext3, create a journal inode */
965 if (!strcmp(program_name, "mkfs.ext3"))
969 while ((c = getopt (argc, argv,
970 "b:cf:g:i:jl:m:no:qr:s:tvE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
973 blocksize = strtol(optarg, &tmp, 0);
974 b = (blocksize > 0) ? blocksize : -blocksize;
975 if (b < EXT2_MIN_BLOCK_SIZE ||
976 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
977 com_err(program_name, 0,
978 _("invalid block size - %s"), optarg);
981 if (blocksize > 4096)
982 fprintf(stderr, _("Warning: blocksize %d not "
983 "usable on most systems.\n"),
986 fs_param.s_log_block_size =
987 int_log2(blocksize >>
988 EXT2_MIN_BLOCK_LOG_SIZE);
990 case 'c': /* Check for bad blocks */
991 case 't': /* deprecated */
995 size = strtoul(optarg, &tmp, 0);
996 if (size < EXT2_MIN_BLOCK_SIZE ||
997 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
998 com_err(program_name, 0,
999 _("invalid fragment size - %s"),
1003 fs_param.s_log_frag_size =
1004 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1005 fprintf(stderr, _("Warning: fragments not supported. "
1006 "Ignoring -f option\n"));
1009 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1011 com_err(program_name, 0,
1012 _("Illegal number for blocks per group"));
1015 if ((fs_param.s_blocks_per_group % 8) != 0) {
1016 com_err(program_name, 0,
1017 _("blocks per group must be multiple of 8"));
1022 inode_ratio = strtoul(optarg, &tmp, 0);
1023 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1024 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1026 com_err(program_name, 0,
1027 _("invalid inode ratio %s (min %d/max %d)"),
1028 optarg, EXT2_MIN_BLOCK_SIZE,
1029 EXT2_MAX_BLOCK_SIZE);
1034 parse_journal_opts(optarg);
1041 bad_blocks_filename = malloc(strlen(optarg)+1);
1042 if (!bad_blocks_filename) {
1043 com_err(program_name, ENOMEM,
1044 _("in malloc for bad_blocks_filename"));
1047 strcpy(bad_blocks_filename, optarg);
1050 reserved_ratio = strtod(optarg, &tmp);
1051 if (reserved_ratio > 50 || *tmp) {
1052 com_err(program_name, 0,
1053 _("invalid reserved blocks percent - %s"),
1062 creator_os = optarg;
1068 r_opt = strtoul(optarg, &tmp, 0);
1070 com_err(program_name, 0,
1071 _("bad revision level - %s"), optarg);
1074 fs_param.s_rev_level = r_opt;
1076 case 's': /* deprecated */
1077 s_opt = atoi(optarg);
1079 #ifdef EXT2_DYNAMIC_REV
1081 inode_size = strtoul(optarg, &tmp, 0);
1083 com_err(program_name, 0,
1084 _("invalid inode size - %s"), optarg);
1096 volume_label = optarg;
1102 num_inodes = strtoul(optarg, &tmp, 0);
1104 com_err(program_name, 0,
1105 _("bad num inodes - %s"), optarg);
1110 fs_features = optarg;
1114 extended_opts = optarg;
1123 /* Print version number and exit */
1124 show_version_only++;
1130 if ((optind == argc) && !show_version_only)
1132 device_name = argv[optind++];
1134 if (!quiet || show_version_only)
1135 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1138 if (show_version_only) {
1139 fprintf(stderr, _("\tUsing %s\n"),
1140 error_message(EXT2_ET_BASE));
1145 * If there's no blocksize specified and there is a journal
1146 * device, use it to figure out the blocksize
1148 if (blocksize <= 0 && journal_device) {
1152 #ifdef CONFIG_TESTIO_DEBUG
1153 io_ptr = test_io_manager;
1154 test_io_backing_manager = unix_io_manager;
1156 io_ptr = unix_io_manager;
1158 retval = ext2fs_open(journal_device,
1159 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1162 com_err(program_name, retval,
1163 _("while trying to open journal device %s\n"),
1167 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1168 com_err(program_name, 0,
1169 _("Journal dev blocksize (%d) smaller than "
1170 "minimum blocksize %d\n"), jfs->blocksize,
1174 blocksize = jfs->blocksize;
1175 fs_param.s_log_block_size =
1176 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1180 if (blocksize > sys_page_size) {
1182 com_err(program_name, 0,
1183 _("%d-byte blocks too big for system (max %d)"),
1184 blocksize, sys_page_size);
1187 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1188 "(max %d), forced to continue\n"),
1189 blocksize, sys_page_size);
1191 if (optind < argc) {
1192 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1193 fs_param.s_log_block_size);
1194 if (!fs_param.s_blocks_count) {
1195 com_err(program_name, 0, _("invalid blocks count - %s"),
1204 check_plausibility(device_name);
1205 check_mount(device_name, force, _("filesystem"));
1207 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1209 if (noaction && fs_param.s_blocks_count) {
1210 dev_size = fs_param.s_blocks_count;
1214 retval = ext2fs_get_device_size(device_name,
1215 EXT2_BLOCK_SIZE(&fs_param),
1217 if ((retval == EFBIG) &&
1219 (fs_param.s_log_block_size == 0)) {
1220 fs_param.s_log_block_size = 2;
1226 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1227 com_err(program_name, retval,
1228 _("while trying to determine filesystem size"));
1231 if (!fs_param.s_blocks_count) {
1232 if (retval == EXT2_ET_UNIMPLEMENTED) {
1233 com_err(program_name, 0,
1234 _("Couldn't determine device size; you "
1235 "must specify\nthe size of the "
1239 if (dev_size == 0) {
1240 com_err(program_name, 0,
1241 _("Device size reported to be zero. "
1242 "Invalid partition specified, or\n\t"
1243 "partition table wasn't reread "
1244 "after running fdisk, due to\n\t"
1245 "a modified partition being busy "
1246 "and in use. You may need to reboot\n\t"
1247 "to re-read your partition table.\n"
1251 fs_param.s_blocks_count = dev_size;
1252 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1253 fs_param.s_blocks_count &= ~((sys_page_size /
1254 EXT2_BLOCK_SIZE(&fs_param))-1);
1257 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1258 com_err(program_name, 0,
1259 _("Filesystem larger than apparent device size."));
1264 int megs = fs_param.s_blocks_count *
1265 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1269 else if (megs <= 512)
1272 fs_type = "default";
1275 /* Figure out what features should be enabled */
1277 if (r_opt == EXT2_GOOD_OLD_REV && fs_features) {
1278 fprintf(stderr, _("Filesystem features not supported "
1279 "with revision 0 filesystems\n"));
1283 profile_get_string(profile, "defaults", "base_features", 0,
1284 "filetype,sparse_super", &tmp);
1285 profile_get_string(profile, "fs_types", fs_type, "base_features",
1287 edit_feature(tmp2, &fs_param.s_feature_compat);
1291 profile_get_string(profile, "defaults", "default_features", 0,
1293 profile_get_string(profile, "fs_types", fs_type,
1294 "default_features", tmp, &tmp2);
1295 edit_feature(fs_features ? fs_features : tmp2,
1296 &fs_param.s_feature_compat);
1301 fs_param.s_feature_ro_compat |=
1302 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1303 else if (s_opt == 0)
1304 fs_param.s_feature_ro_compat &=
1305 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1307 if (journal_size != 0)
1308 fs_param.s_feature_compat |=
1309 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1311 if (fs_param.s_feature_incompat &
1312 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1314 fs_type = "journal";
1316 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1317 fs_param.s_feature_compat = 0;
1318 fs_param.s_feature_ro_compat = 0;
1321 if (fs_param.s_rev_level == EXT2_GOOD_OLD_REV) {
1322 fs_param.s_feature_incompat = 0;
1323 fs_param.s_feature_compat = 0;
1324 fs_param.s_feature_ro_compat = 0;
1327 /* Set first meta blockgroup via an environment variable */
1328 /* (this is mostly for debugging purposes) */
1329 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1330 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1331 fs_param.s_first_meta_bg = atoi(tmp);
1333 /* Get the hardware sector size, if available */
1334 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1336 com_err(program_name, retval,
1337 _("while trying to determine hardware sector size"));
1341 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1342 sector_size = atoi(tmp);
1344 if (blocksize <= 0) {
1345 profile_get_integer(profile, "defaults", "blocksize", 0,
1347 profile_get_integer(profile, "fs_types", fs_type,
1348 "blocksize", use_bsize, &use_bsize);
1350 if (use_bsize == -1) {
1351 use_bsize = sys_page_size;
1352 if ((linux_version_code < (2*65536 + 6*256)) &&
1356 if (sector_size && use_bsize < sector_size)
1357 use_bsize = sector_size;
1358 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1359 use_bsize = -blocksize;
1360 blocksize = use_bsize;
1361 fs_param.s_blocks_count /= blocksize / 1024;
1364 if (inode_ratio == 0) {
1365 profile_get_integer(profile, "defaults", "inode_ratio", 0,
1366 8192, &inode_ratio);
1367 profile_get_integer(profile, "fs_types", fs_type,
1368 "inode_ratio", inode_ratio,
1371 if (inode_ratio < blocksize)
1372 inode_ratio = blocksize;
1375 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1376 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1378 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1381 parse_extended_opts(&fs_param, extended_opts);
1383 /* Since sparse_super is the default, we would only have a problem
1384 * here if it was explicitly disabled.
1386 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1387 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1388 com_err(program_name, 0,
1389 _("reserved online resize blocks not supported "
1390 "on non-sparse filesystem"));
1394 if (fs_param.s_blocks_per_group) {
1395 if (fs_param.s_blocks_per_group < 256 ||
1396 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1397 com_err(program_name, 0,
1398 _("blocks per group count out of range"));
1403 if (!force && fs_param.s_blocks_count >= (1 << 31)) {
1404 com_err(program_name, 0,
1405 _("Filesystem too large. No more than 2**31-1 blocks\n"
1406 "\t (8TB using a blocksize of 4k) are currently supported."));
1410 if ((blocksize > 4096) &&
1411 (fs_param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1412 fprintf(stderr, _("\nWarning: some 2.4 kernels do not support "
1413 "blocksizes greater than 4096\n\tusing ext3. "
1414 "Use -b 4096 if this is an issue for you.\n\n"));
1417 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1418 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1419 inode_size & (inode_size - 1)) {
1420 com_err(program_name, 0,
1421 _("invalid inode size %d (min %d/max %d)"),
1422 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1426 if (inode_size != EXT2_GOOD_OLD_INODE_SIZE)
1427 fprintf(stderr, _("Warning: %d-byte inodes not usable "
1428 "on most systems\n"),
1430 fs_param.s_inode_size = inode_size;
1434 * Calculate number of inodes based on the inode ratio
1436 fs_param.s_inodes_count = num_inodes ? num_inodes :
1437 ((__u64) fs_param.s_blocks_count * blocksize)
1441 * Calculate number of blocks to reserve
1443 fs_param.s_r_blocks_count = (fs_param.s_blocks_count * reserved_ratio)
1447 int main (int argc, char *argv[])
1449 errcode_t retval = 0;
1451 badblocks_list bb_list = 0;
1458 setlocale(LC_MESSAGES, "");
1459 setlocale(LC_CTYPE, "");
1460 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1461 textdomain(NLS_CAT_NAME);
1465 #ifdef CONFIG_TESTIO_DEBUG
1466 io_ptr = test_io_manager;
1467 test_io_backing_manager = unix_io_manager;
1469 io_ptr = unix_io_manager;
1473 * Initialize the superblock....
1475 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
1478 com_err(device_name, retval, _("while setting up superblock"));
1483 * Wipe out the old on-disk superblock
1486 zap_sector(fs, 2, 6);
1489 * Generate a UUID for it...
1491 uuid_generate(fs->super->s_uuid);
1494 * Initialize the directory index variables
1496 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1497 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1500 * Add "jitter" to the superblock's check interval so that we
1501 * don't check all the filesystems at the same time. We use a
1502 * kludgy hack of using the UUID to derive a random jitter value.
1504 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1505 val += fs->super->s_uuid[i];
1506 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1509 * Override the creator OS, if applicable
1511 if (creator_os && !set_os(fs->super, creator_os)) {
1512 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1517 * For the Hurd, we will turn off filetype since it doesn't
1520 if (fs->super->s_creator_os == EXT2_OS_HURD)
1521 fs->super->s_feature_incompat &=
1522 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1525 * Set the volume label...
1528 memset(fs->super->s_volume_name, 0,
1529 sizeof(fs->super->s_volume_name));
1530 strncpy(fs->super->s_volume_name, volume_label,
1531 sizeof(fs->super->s_volume_name));
1535 * Set the last mount directory
1538 memset(fs->super->s_last_mounted, 0,
1539 sizeof(fs->super->s_last_mounted));
1540 strncpy(fs->super->s_last_mounted, mount_dir,
1541 sizeof(fs->super->s_last_mounted));
1544 if (!quiet || noaction)
1550 if (fs->super->s_feature_incompat &
1551 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1552 create_journal_dev(fs);
1553 exit(ext2fs_close(fs) ? 1 : 0);
1556 if (bad_blocks_filename)
1557 read_bb_file(fs, &bb_list, bad_blocks_filename);
1559 test_disk(fs, &bb_list);
1561 handle_bad_blocks(fs, bb_list);
1562 fs->stride = fs_stride;
1563 retval = ext2fs_allocate_tables(fs);
1565 com_err(program_name, retval,
1566 _("while trying to allocate filesystem tables"));
1570 fs->super->s_state |= EXT2_ERROR_FS;
1571 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1573 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1574 unsigned int rsv = 65536 / fs->blocksize;
1575 unsigned long blocks = fs->super->s_blocks_count;
1576 unsigned long start;
1579 #ifdef ZAP_BOOTBLOCK
1580 zap_sector(fs, 0, 2);
1584 * Wipe out any old MD RAID (or other) metadata at the end
1585 * of the device. This will also verify that the device is
1586 * as large as we think. Be careful with very small devices.
1588 start = (blocks & ~(rsv - 1));
1592 retval = zero_blocks(fs, start, blocks - start,
1593 NULL, &ret_blk, NULL);
1596 com_err(program_name, retval,
1597 _("while zeroing block %u at end of filesystem"),
1601 write_inode_tables(fs);
1602 create_root_dir(fs);
1603 create_lost_and_found(fs);
1605 create_bad_block_inode(fs, bb_list);
1606 if (fs->super->s_feature_compat &
1607 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1608 retval = ext2fs_create_resize_inode(fs);
1610 com_err("ext2fs_create_resize_inode", retval,
1611 _("while reserving blocks for online resize"));
1617 if (journal_device) {
1621 check_plausibility(journal_device);
1622 check_mount(journal_device, force, _("journal"));
1624 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1625 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1626 fs->blocksize, unix_io_manager, &jfs);
1628 com_err(program_name, retval,
1629 _("while trying to open journal device %s\n"),
1634 printf(_("Adding journal to device %s: "),
1638 retval = ext2fs_add_journal_device(fs, jfs);
1640 com_err (program_name, retval,
1641 _("\n\twhile trying to add journal to device %s"),
1646 printf(_("done\n"));
1648 free(journal_device);
1649 } else if ((journal_size) ||
1650 (fs_param.s_feature_compat &
1651 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1652 journal_blocks = figure_journal_size(journal_size, fs);
1654 if (!journal_blocks) {
1655 fs->super->s_feature_compat &=
1656 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1660 printf(_("Creating journal (%d blocks): "),
1664 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1667 com_err (program_name, retval,
1668 _("\n\twhile trying to create journal"));
1672 printf(_("done\n"));
1677 printf(_("Writing superblocks and "
1678 "filesystem accounting information: "));
1679 retval = ext2fs_flush(fs);
1682 _("\nWarning, had trouble writing out superblocks."));
1685 printf(_("done\n\n"));
1686 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1687 print_check_message(fs);
1689 val = ext2fs_close(fs);
1690 return (retval || val) ? 1 : 0;