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 :-).
19 #define _XOPEN_SOURCE 600 /* for inclusion of PATH_MAX in Solaris */
29 #include <sys/utsname.h>
30 #include <linux/version.h>
47 #include <sys/ioctl.h>
48 #include <sys/types.h>
52 #include <blkid/blkid.h>
54 #include "ext2fs/ext2_fs.h"
55 #include "ext2fs/ext2fsP.h"
56 #include "et/com_err.h"
57 #include "uuid/uuid.h"
59 #include "ext2fs/ext2fs.h"
63 #include "../version.h"
64 #include "nls-enable.h"
65 #include "quota/mkquota.h"
68 #define STRIDE_LENGTH 8
70 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
76 #define DISCARD_STEP_MB (2048)
78 extern int isatty(int);
79 extern FILE *fpopen(const char *cmd, const char *mode);
81 const char * program_name = "mke2fs";
82 static const char * device_name /* = NULL */;
84 /* Command line options */
88 static int super_only;
89 static int discard = 1; /* attempt to discard device before fs creation */
93 static int num_backups = 2; /* number of backup bg's for sparse_super2 */
94 static uid_t root_uid;
95 static gid_t root_gid;
98 static int lazy_itable_init;
99 static int packed_meta_blocks;
100 static char *bad_blocks_filename = NULL;
101 static __u32 fs_stride;
102 static int quotatype = -1; /* Initialize both user and group quotas by default */
104 static blk64_t journal_location = ~0LL;
106 static struct ext2_super_block fs_param;
107 static char *fs_uuid = NULL;
108 static char *creator_os;
109 static char *volume_label;
110 static char *mount_dir;
111 char *journal_device;
112 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
115 static profile_t profile;
117 static int sys_page_size = 4096;
119 static void usage(void)
121 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
122 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
123 "[-J journal-options]\n"
124 "\t[-G flex-group-size] [-N number-of-inodes]\n"
125 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
126 "\t[-g blocks-per-group] [-L volume-label] "
127 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
128 "[-r fs-revision] [-E extended-option[,...]]\n"
129 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
130 "[-jnqvDFKSV] device [blocks-count]\n"),
135 static int int_log2(unsigned long long arg)
147 int int_log10(unsigned long long arg)
157 static int parse_version_number(const char *s)
159 int major, minor, rev;
165 major = strtol(cp, &endptr, 10);
166 if (cp == endptr || *endptr != '.')
169 minor = strtol(cp, &endptr, 10);
170 if (cp == endptr || *endptr != '.')
173 rev = strtol(cp, &endptr, 10);
176 return KERNEL_VERSION(major, minor, rev);
179 static int is_before_linux_ver(unsigned int major, unsigned int minor)
182 static int linux_version_code = -1;
188 if (linux_version_code < 0)
189 linux_version_code = parse_version_number(ut.release);
190 if (linux_version_code == 0)
193 return linux_version_code < KERNEL_VERSION(major, minor, 0);
196 static int is_before_linux_ver(unsigned int major, unsigned int minor)
203 * Helper function for read_bb_file and test_disk
205 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
207 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
212 * Reads the bad blocks list from a file
214 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
215 const char *bad_blocks_file)
220 f = fopen(bad_blocks_file, "r");
222 com_err("read_bad_blocks_file", errno,
223 _("while trying to open %s"), bad_blocks_file);
226 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
229 com_err("ext2fs_read_bb_FILE", retval, "%s",
230 _("while reading in list of bad blocks from file"));
236 * Runs the badblocks program to test the disk
238 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
244 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
245 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
246 fs->device_name, ext2fs_blocks_count(fs->super)-1);
248 printf(_("Running command: %s\n"), buf);
251 com_err("popen", errno,
252 _("while trying to run '%s'"), buf);
255 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
258 com_err("ext2fs_read_bb_FILE", retval, "%s",
259 _("while processing list of bad blocks from program"));
264 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
268 unsigned must_be_good;
270 badblocks_iterate bb_iter;
280 * The primary superblock and group descriptors *must* be
281 * good; if not, abort.
283 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
284 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
285 if (ext2fs_badblocks_list_test(bb_list, i)) {
286 fprintf(stderr, _("Block %d in primary "
287 "superblock/group descriptor area bad.\n"), i);
288 fprintf(stderr, _("Blocks %u through %u must be good "
289 "in order to build a filesystem.\n"),
290 fs->super->s_first_data_block, must_be_good);
291 fputs(_("Aborting....\n"), stderr);
297 * See if any of the bad blocks are showing up in the backup
298 * superblocks and/or group descriptors. If so, issue a
299 * warning and adjust the block counts appropriately.
301 group_block = fs->super->s_first_data_block +
302 fs->super->s_blocks_per_group;
304 for (i = 1; i < fs->group_desc_count; i++) {
306 for (j=0; j < fs->desc_blocks+1; j++) {
307 if (ext2fs_badblocks_list_test(bb_list,
311 _("Warning: the backup superblock/group descriptors at block %u contain\n"
315 group = ext2fs_group_of_blk2(fs, group_block+j);
316 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
317 ext2fs_group_desc_csum_set(fs, group);
318 ext2fs_free_blocks_count_add(fs->super, 1);
321 group_block += fs->super->s_blocks_per_group;
325 * Mark all the bad blocks as used...
327 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
329 com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
330 _("while marking bad blocks as used"));
333 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
334 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
335 ext2fs_badblocks_list_iterate_end(bb_iter);
338 static errcode_t packed_allocate_tables(ext2_filsys fs)
344 for (i = 0; i < fs->group_desc_count; i++) {
345 retval = ext2fs_new_block2(fs, goal, NULL, &goal);
348 ext2fs_block_alloc_stats2(fs, goal, +1);
349 ext2fs_block_bitmap_loc_set(fs, i, goal);
351 for (i = 0; i < fs->group_desc_count; i++) {
352 retval = ext2fs_new_block2(fs, goal, NULL, &goal);
355 ext2fs_block_alloc_stats2(fs, goal, +1);
356 ext2fs_inode_bitmap_loc_set(fs, i, goal);
358 for (i = 0; i < fs->group_desc_count; i++) {
359 blk64_t end = ext2fs_blocks_count(fs->super) - 1;
360 retval = ext2fs_get_free_blocks2(fs, goal, end,
361 fs->inode_blocks_per_group,
362 fs->block_map, &goal);
365 ext2fs_block_alloc_stats_range(fs, goal,
366 fs->inode_blocks_per_group, +1);
367 ext2fs_inode_table_loc_set(fs, i, goal);
372 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
378 struct ext2fs_numeric_progress_struct progress;
380 ext2fs_numeric_progress_init(fs, &progress,
381 _("Writing inode tables: "),
382 fs->group_desc_count);
384 for (i = 0; i < fs->group_desc_count; i++) {
385 ext2fs_numeric_progress_update(fs, &progress, i);
387 blk = ext2fs_inode_table_loc(fs, i);
388 num = fs->inode_blocks_per_group;
391 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
392 ext2fs_bg_itable_unused(fs, i)) *
393 EXT2_INODE_SIZE(fs->super),
394 EXT2_BLOCK_SIZE(fs->super));
395 if (!lazy_flag || itable_zeroed) {
396 /* The kernel doesn't need to zero the itable blocks */
397 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
398 ext2fs_group_desc_csum_set(fs, i);
400 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
402 fprintf(stderr, _("\nCould not write %d "
403 "blocks in inode table starting at %llu: %s\n"),
404 num, blk, error_message(retval));
408 if (sync_kludge == 1)
410 else if ((i % sync_kludge) == 0)
414 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
415 ext2fs_numeric_progress_close(fs, &progress,
419 static void create_root_dir(ext2_filsys fs)
422 struct ext2_inode inode;
424 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
426 com_err("ext2fs_mkdir", retval, "%s",
427 _("while creating root dir"));
430 if (root_uid != 0 || root_gid != 0) {
431 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
433 com_err("ext2fs_read_inode", retval, "%s",
434 _("while reading root inode"));
438 inode.i_uid = root_uid;
439 ext2fs_set_i_uid_high(inode, root_uid >> 16);
440 inode.i_gid = root_gid;
441 ext2fs_set_i_gid_high(inode, root_gid >> 16);
443 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
445 com_err("ext2fs_write_inode", retval, "%s",
446 _("while setting root inode ownership"));
452 static void create_lost_and_found(ext2_filsys fs)
454 unsigned int lpf_size = 0;
457 const char *name = "lost+found";
461 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
463 com_err("ext2fs_mkdir", retval, "%s",
464 _("while creating /lost+found"));
468 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
470 com_err("ext2_lookup", retval, "%s",
471 _("while looking up /lost+found"));
475 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
476 /* Ensure that lost+found is at least 2 blocks, so we always
477 * test large empty blocks for big-block filesystems. */
478 if ((lpf_size += fs->blocksize) >= 16*1024 &&
479 lpf_size >= 2 * fs->blocksize)
481 retval = ext2fs_expand_dir(fs, ino);
483 com_err("ext2fs_expand_dir", retval, "%s",
484 _("while expanding /lost+found"));
490 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
494 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
495 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
496 retval = ext2fs_update_bb_inode(fs, bb_list);
498 com_err("ext2fs_update_bb_inode", retval, "%s",
499 _("while setting bad block inode"));
505 static void reserve_inodes(ext2_filsys fs)
509 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
510 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
511 ext2fs_mark_ib_dirty(fs);
514 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
515 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
516 #define BSD_LABEL_OFFSET 64
518 static void zap_sector(ext2_filsys fs, int sect, int nsect)
524 buf = malloc(512*nsect);
526 printf(_("Out of memory erasing sectors %d-%d\n"),
527 sect, sect + nsect - 1);
532 /* Check for a BSD disklabel, and don't erase it if so */
533 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
536 _("Warning: could not read block 0: %s\n"),
537 error_message(retval));
539 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
540 if ((*magic == BSD_DISKMAGIC) ||
541 (*magic == BSD_MAGICDISK))
546 memset(buf, 0, 512*nsect);
547 io_channel_set_blksize(fs->io, 512);
548 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
549 io_channel_set_blksize(fs->io, fs->blocksize);
552 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
553 sect, error_message(retval));
556 static void create_journal_dev(ext2_filsys fs)
558 struct ext2fs_numeric_progress_struct progress;
561 blk64_t blk, err_blk;
562 int c, count, err_count;
564 retval = ext2fs_create_journal_superblock(fs,
565 ext2fs_blocks_count(fs->super), 0, &buf);
567 com_err("create_journal_dev", retval, "%s",
568 _("while initializing journal superblock"));
572 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
573 goto write_superblock;
575 ext2fs_numeric_progress_init(fs, &progress,
576 _("Zeroing journal device: "),
577 ext2fs_blocks_count(fs->super));
579 count = ext2fs_blocks_count(fs->super);
585 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
587 com_err("create_journal_dev", retval,
588 _("while zeroing journal device "
589 "(block %llu, count %d)"),
595 ext2fs_numeric_progress_update(fs, &progress, blk);
597 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
599 ext2fs_numeric_progress_close(fs, &progress, NULL);
601 retval = io_channel_write_blk64(fs->io,
602 fs->super->s_first_data_block+1,
605 com_err("create_journal_dev", retval, "%s",
606 _("while writing journal superblock"));
611 static void show_stats(ext2_filsys fs)
613 struct ext2_super_block *s = fs->super;
620 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
621 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
622 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
624 memset(buf, 0, sizeof(buf));
625 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
626 printf(_("Filesystem label=%s\n"), buf);
627 os = e2p_os2string(fs->super->s_creator_os);
629 printf(_("OS type: %s\n"), os);
631 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
632 s->s_log_block_size);
633 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
634 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
635 printf(_("Cluster size=%u (log=%u)\n"),
636 fs->blocksize << fs->cluster_ratio_bits,
637 s->s_log_cluster_size);
639 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
640 s->s_log_cluster_size);
641 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
642 s->s_raid_stride, s->s_raid_stripe_width);
643 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
644 ext2fs_blocks_count(s));
645 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
646 ext2fs_r_blocks_count(s),
647 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
648 printf(_("First data block=%u\n"), s->s_first_data_block);
649 if (root_uid != 0 || root_gid != 0)
650 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
651 if (s->s_reserved_gdt_blocks)
652 printf(_("Maximum filesystem blocks=%lu\n"),
653 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
654 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
655 if (fs->group_desc_count > 1)
656 printf(_("%u block groups\n"), fs->group_desc_count);
658 printf(_("%u block group\n"), fs->group_desc_count);
659 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
660 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
661 printf(_("%u blocks per group, %u clusters per group\n"),
662 s->s_blocks_per_group, s->s_clusters_per_group);
664 printf(_("%u blocks per group, %u fragments per group\n"),
665 s->s_blocks_per_group, s->s_clusters_per_group);
666 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
668 if (fs->group_desc_count == 1) {
673 printf("%s", _("Superblock backups stored on blocks: "));
674 group_block = s->s_first_data_block;
676 for (i = 1; i < fs->group_desc_count; i++) {
677 group_block += s->s_blocks_per_group;
678 if (!ext2fs_bg_has_super(fs, i))
682 need = int_log10(group_block) + 2;
683 if (need > col_left) {
688 printf("%llu", group_block);
694 * Set the S_CREATOR_OS field. Return true if OS is known,
697 static int set_os(struct ext2_super_block *sb, char *os)
700 sb->s_creator_os = atoi (os);
701 else if (strcasecmp(os, "linux") == 0)
702 sb->s_creator_os = EXT2_OS_LINUX;
703 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
704 sb->s_creator_os = EXT2_OS_HURD;
705 else if (strcasecmp(os, "freebsd") == 0)
706 sb->s_creator_os = EXT2_OS_FREEBSD;
707 else if (strcasecmp(os, "lites") == 0)
708 sb->s_creator_os = EXT2_OS_LITES;
714 #define PATH_SET "PATH=/sbin"
716 static void parse_extended_opts(struct ext2_super_block *param,
719 char *buf, *token, *next, *p, *arg, *badopt = 0;
726 fprintf(stderr, "%s",
727 _("Couldn't allocate memory to parse options!\n"));
731 for (token = buf; token && *token; token = next) {
732 p = strchr(token, ',');
738 arg = strchr(token, '=');
743 if (strcmp(token, "desc-size") == 0 ||
744 strcmp(token, "desc_size") == 0) {
747 if (!(fs_param.s_feature_incompat &
748 EXT4_FEATURE_INCOMPAT_64BIT)) {
750 _("%s requires '-O 64bit'\n"), token);
754 if (param->s_reserved_gdt_blocks != 0) {
756 _("'%s' must be before 'resize=%u'\n"),
757 token, param->s_reserved_gdt_blocks);
766 desc_size = strtoul(arg, &p, 0);
767 if (*p || (desc_size & (desc_size - 1))) {
769 _("Invalid desc_size: '%s'\n"), arg);
773 param->s_desc_size = desc_size;
774 } else if (strcmp(token, "offset") == 0) {
780 offset = strtoull(arg, &p, 0);
782 fprintf(stderr, _("Invalid offset: %s\n"),
787 } else if (strcmp(token, "mmp_update_interval") == 0) {
793 param->s_mmp_update_interval = strtoul(arg, &p, 0);
796 _("Invalid mmp_update_interval: %s\n"),
801 } else if (strcmp(token, "num_backup_sb") == 0) {
807 num_backups = strtoul(arg, &p, 0);
808 if (*p || num_backups > 2) {
810 _("Invalid # of backup "
816 } else if (strcmp(token, "packed_meta_blocks") == 0) {
818 packed_meta_blocks = strtoul(arg, &p, 0);
820 packed_meta_blocks = 1;
821 if (packed_meta_blocks)
822 journal_location = 0;
823 } else if (strcmp(token, "stride") == 0) {
829 param->s_raid_stride = strtoul(arg, &p, 0);
832 _("Invalid stride parameter: %s\n"),
837 } else if (strcmp(token, "stripe-width") == 0 ||
838 strcmp(token, "stripe_width") == 0) {
844 param->s_raid_stripe_width = strtoul(arg, &p, 0);
847 _("Invalid stripe-width parameter: %s\n"),
852 } else if (!strcmp(token, "resize")) {
854 unsigned long bpg, rsv_groups;
855 unsigned long group_desc_count, desc_blocks;
856 unsigned int gdpb, blocksize;
865 resize = parse_num_blocks2(arg,
866 param->s_log_block_size);
870 _("Invalid resize parameter: %s\n"),
875 if (resize <= ext2fs_blocks_count(param)) {
876 fprintf(stderr, "%s",
877 _("The resize maximum must be greater "
878 "than the filesystem size.\n"));
883 blocksize = EXT2_BLOCK_SIZE(param);
884 bpg = param->s_blocks_per_group;
887 gdpb = EXT2_DESC_PER_BLOCK(param);
888 group_desc_count = (__u32) ext2fs_div64_ceil(
889 ext2fs_blocks_count(param), bpg);
890 desc_blocks = (group_desc_count +
892 rsv_groups = ext2fs_div64_ceil(resize, bpg);
893 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
895 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
896 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
899 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
900 fprintf(stderr, "%s",
901 _("On-line resizing not supported with revision 0 filesystems\n"));
905 param->s_feature_compat |=
906 EXT2_FEATURE_COMPAT_RESIZE_INODE;
908 param->s_reserved_gdt_blocks = rsv_gdb;
910 } else if (!strcmp(token, "test_fs")) {
911 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
912 } else if (!strcmp(token, "lazy_itable_init")) {
914 lazy_itable_init = strtoul(arg, &p, 0);
916 lazy_itable_init = 1;
917 } else if (!strcmp(token, "lazy_journal_init")) {
919 journal_flags |= strtoul(arg, &p, 0) ?
920 EXT2_MKJOURNAL_LAZYINIT : 0;
922 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
923 } else if (!strcmp(token, "root_owner")) {
925 root_uid = strtoul(arg, &p, 0);
928 _("Invalid root_owner: '%s'\n"),
934 root_gid = strtoul(p, &p, 0);
937 _("Invalid root_owner: '%s'\n"),
946 } else if (!strcmp(token, "discard")) {
948 } else if (!strcmp(token, "nodiscard")) {
950 } else if (!strcmp(token, "quotatype")) {
956 if (!strncmp(arg, "usr", 3)) {
958 } else if (!strncmp(arg, "grp", 3)) {
962 _("Invalid quotatype parameter: %s\n"),
973 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
974 "Extended options are separated by commas, "
975 "and may take an argument which\n"
976 "\tis set off by an equals ('=') sign.\n\n"
977 "Valid extended options are:\n"
978 "\tmmp_update_interval=<interval>\n"
979 "\tnum_backup_sb=<0|1|2>\n"
980 "\tstride=<RAID per-disk data chunk in blocks>\n"
981 "\tstripe-width=<RAID stride * data disks in blocks>\n"
982 "\toffset=<offset to create the file system>\n"
983 "\tresize=<resize maximum size in blocks>\n"
984 "\tpacked_meta_blocks=<0 to disable, 1 to enable>\n"
985 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
986 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
987 "\troot_uid=<uid of root directory>\n"
988 "\troot_gid=<gid of root directory>\n"
992 "\tquotatype=<usr OR grp>\n\n"),
993 badopt ? badopt : "");
997 if (param->s_raid_stride &&
998 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
999 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
1000 "multiple of stride %u.\n\n"),
1001 param->s_raid_stripe_width, param->s_raid_stride);
1006 static __u32 ok_features[3] = {
1008 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
1009 EXT2_FEATURE_COMPAT_RESIZE_INODE |
1010 EXT2_FEATURE_COMPAT_DIR_INDEX |
1011 EXT2_FEATURE_COMPAT_EXT_ATTR |
1012 EXT4_FEATURE_COMPAT_SPARSE_SUPER2,
1014 EXT2_FEATURE_INCOMPAT_FILETYPE|
1015 EXT3_FEATURE_INCOMPAT_EXTENTS|
1016 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
1017 EXT2_FEATURE_INCOMPAT_META_BG|
1018 EXT4_FEATURE_INCOMPAT_FLEX_BG|
1019 EXT4_FEATURE_INCOMPAT_MMP |
1020 EXT4_FEATURE_INCOMPAT_64BIT,
1022 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
1023 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
1024 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
1025 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
1026 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
1027 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1028 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
1030 EXT4_FEATURE_RO_COMPAT_QUOTA|
1036 static void syntax_err_report(const char *filename, long err, int line_num)
1039 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
1040 filename, line_num, error_message(err));
1044 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
1046 static void edit_feature(const char *str, __u32 *compat_array)
1051 if (e2p_edit_feature(str, compat_array, ok_features)) {
1052 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
1058 static void edit_mntopts(const char *str, __u32 *mntopts)
1063 if (e2p_edit_mntopts(str, mntopts, ~0)) {
1064 fprintf(stderr, _("Invalid mount option set: %s\n"),
1076 static errcode_t init_list(struct str_list *sl)
1080 sl->list = malloc((sl->max+1) * sizeof(char *));
1087 static errcode_t push_string(struct str_list *sl, const char *str)
1091 if (sl->num >= sl->max) {
1093 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
1096 sl->list = new_list;
1098 sl->list[sl->num] = malloc(strlen(str)+1);
1099 if (sl->list[sl->num] == 0)
1101 strcpy(sl->list[sl->num], str);
1103 sl->list[sl->num] = 0;
1107 static void print_str_list(char **list)
1111 for (cpp = list; *cpp; cpp++) {
1112 printf("'%s'", *cpp);
1114 fputs(", ", stdout);
1116 fputc('\n', stdout);
1120 * Return TRUE if the profile has the given subsection
1122 static int profile_has_subsection(profile_t prof, const char *section,
1123 const char *subsection)
1126 const char *names[4];
1131 names[1] = subsection;
1134 if (profile_iterator_create(prof, names,
1135 PROFILE_ITER_LIST_SECTION |
1136 PROFILE_ITER_RELATIONS_ONLY, &state))
1139 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1144 profile_iterator_free(&state);
1148 static char **parse_fs_type(const char *fs_type,
1149 const char *usage_types,
1150 struct ext2_super_block *sb,
1151 blk64_t fs_blocks_count,
1154 const char *ext_type = 0;
1156 char *profile_type = 0;
1158 const char *size_type;
1159 struct str_list list;
1160 unsigned long long meg;
1163 if (init_list(&list))
1166 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1167 !strcasecmp(creator_os, "hurd")))
1174 else if (!strcmp(program_name, "mke3fs"))
1176 else if (!strcmp(program_name, "mke4fs"))
1178 else if (progname) {
1179 ext_type = strrchr(progname, '/');
1183 ext_type = progname;
1185 if (!strncmp(ext_type, "mkfs.", 5)) {
1187 if (ext_type[0] == 0)
1194 profile_get_string(profile, "defaults", "fs_type", 0,
1195 "ext2", &profile_type);
1196 ext_type = profile_type;
1197 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1202 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1203 strcmp(ext_type, "ext2")) {
1204 printf(_("\nYour mke2fs.conf file does not define the "
1205 "%s filesystem type.\n"), ext_type);
1206 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1207 !strcmp(ext_type, "ext4dev")) {
1208 printf("%s", _("You probably need to install an "
1209 "updated mke2fs.conf file.\n\n"));
1212 printf("%s", _("Aborting...\n"));
1217 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1218 if (fs_blocks_count < 3 * meg)
1219 size_type = "floppy";
1220 else if (fs_blocks_count < 512 * meg)
1221 size_type = "small";
1222 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1223 size_type = "default";
1224 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1230 usage_types = size_type;
1232 parse_str = malloc(strlen(usage_types)+1);
1238 strcpy(parse_str, usage_types);
1241 push_string(&list, ext_type);
1244 t = strchr(cp, ',');
1249 if (profile_has_subsection(profile, "fs_types", cp))
1250 push_string(&list, cp);
1251 else if (strcmp(cp, "default") != 0)
1253 _("\nWarning: the fs_type %s is not "
1254 "defined in mke2fs.conf\n\n"),
1265 push_string(&list, "hurd");
1269 char *get_string_from_profile(char **types, const char *opt,
1270 const char *def_val)
1275 for (i=0; types[i]; i++);
1276 for (i-=1; i >=0 ; i--) {
1277 profile_get_string(profile, "fs_types", types[i],
1282 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1286 int get_int_from_profile(char **types, const char *opt, int def_val)
1291 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1292 for (cpp = types; *cpp; cpp++)
1293 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1297 static double get_double_from_profile(char **types, const char *opt,
1303 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1304 for (cpp = types; *cpp; cpp++)
1305 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1309 int get_bool_from_profile(char **types, const char *opt, int def_val)
1314 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1315 for (cpp = types; *cpp; cpp++)
1316 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1320 extern const char *mke2fs_default_profile;
1321 static const char *default_files[] = { "<default>", 0 };
1323 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1325 * Sets the geometry of a device (stripe/stride), and returns the
1326 * device's alignment offset, if any, or a negative error.
1328 static int get_device_geometry(const char *file,
1329 struct ext2_super_block *fs_param,
1336 unsigned long min_io;
1337 unsigned long opt_io;
1338 struct stat statbuf;
1340 /* Nothing to do for a regular file */
1341 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1344 pr = blkid_new_probe_from_filename(file);
1348 tp = blkid_probe_get_topology(pr);
1352 min_io = blkid_topology_get_minimum_io_size(tp);
1353 opt_io = blkid_topology_get_optimal_io_size(tp);
1354 blocksize = EXT2_BLOCK_SIZE(fs_param);
1355 if ((min_io == 0) && (psector_size > blocksize))
1356 min_io = psector_size;
1357 if ((opt_io == 0) && min_io)
1359 if ((opt_io == 0) && (psector_size > blocksize))
1360 opt_io = psector_size;
1362 /* setting stripe/stride to blocksize is pointless */
1363 if (min_io > blocksize)
1364 fs_param->s_raid_stride = min_io / blocksize;
1365 if (opt_io > blocksize)
1366 fs_param->s_raid_stripe_width = opt_io / blocksize;
1368 rc = blkid_topology_get_alignment_offset(tp);
1370 blkid_free_probe(pr);
1375 static void PRS(int argc, char *argv[])
1378 int cluster_size = 0;
1381 int inode_ratio = 0;
1383 unsigned long flex_bg_size = 0;
1384 double reserved_ratio = -1.0;
1385 int lsector_size = 0, psector_size = 0;
1386 int show_version_only = 0;
1387 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1389 char * oldpath = getenv("PATH");
1390 char * extended_opts = 0;
1392 char * usage_types = 0;
1395 * NOTE: A few words about fs_blocks_count and blocksize:
1397 * Initially, blocksize is set to zero, which implies 1024.
1398 * If -b is specified, blocksize is updated to the user's value.
1400 * Next, the device size or the user's "blocks" command line argument
1401 * is used to set fs_blocks_count; the units are blocksize.
1403 * Later, if blocksize hasn't been set and the profile specifies a
1404 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1405 * appropriately. Note the change in units!
1407 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1409 blk64_t fs_blocks_count = 0;
1411 int s_opt = -1, r_opt = -1;
1412 char *fs_features = 0;
1415 int pathlen = sizeof(PATH_SET) + 1;
1418 pathlen += strlen(oldpath);
1419 newpath = malloc(pathlen);
1421 fprintf(stderr, "%s",
1422 _("Couldn't allocate memory for new PATH.\n"));
1425 strcpy(newpath, PATH_SET);
1427 /* Update our PATH to include /sbin */
1429 strcat (newpath, ":");
1430 strcat (newpath, oldpath);
1434 tmp = getenv("MKE2FS_SYNC");
1436 sync_kludge = atoi(tmp);
1438 /* Determine the system page size if possible */
1440 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1441 #define _SC_PAGESIZE _SC_PAGE_SIZE
1444 sysval = sysconf(_SC_PAGESIZE);
1446 sys_page_size = sysval;
1447 #endif /* _SC_PAGESIZE */
1448 #endif /* HAVE_SYSCONF */
1450 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1452 profile_set_syntax_err_cb(syntax_err_report);
1453 retval = profile_init(config_fn, &profile);
1454 if (retval == ENOENT) {
1455 retval = profile_init(default_files, &profile);
1458 retval = profile_set_default(profile, mke2fs_default_profile);
1461 } else if (retval) {
1463 fprintf(stderr, _("Couldn't init profile successfully"
1464 " (error: %ld).\n"), retval);
1468 setbuf(stdout, NULL);
1469 setbuf(stderr, NULL);
1470 add_error_table(&et_ext2_error_table);
1471 add_error_table(&et_prof_error_table);
1472 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1473 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1475 if (is_before_linux_ver(2, 2))
1476 fs_param.s_rev_level = 0;
1478 if (argc && *argv) {
1479 program_name = get_progname(*argv);
1481 /* If called as mkfs.ext3, create a journal inode */
1482 if (!strcmp(program_name, "mkfs.ext3") ||
1483 !strcmp(program_name, "mke3fs"))
1487 while ((c = getopt (argc, argv,
1488 "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1491 blocksize = parse_num_blocks2(optarg, -1);
1492 b = (blocksize > 0) ? blocksize : -blocksize;
1493 if (b < EXT2_MIN_BLOCK_SIZE ||
1494 b > EXT2_MAX_BLOCK_SIZE) {
1495 com_err(program_name, 0,
1496 _("invalid block size - %s"), optarg);
1499 if (blocksize > 4096)
1500 fprintf(stderr, _("Warning: blocksize %d not "
1501 "usable on most systems.\n"),
1504 fs_param.s_log_block_size =
1505 int_log2(blocksize >>
1506 EXT2_MIN_BLOCK_LOG_SIZE);
1508 case 'c': /* Check for bad blocks */
1512 cluster_size = parse_num_blocks2(optarg, -1);
1513 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1514 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1515 com_err(program_name, 0,
1516 _("invalid cluster size - %s"),
1525 com_err(program_name, 0, "%s",
1526 _("'-R' is deprecated, use '-E' instead"));
1529 extended_opts = optarg;
1535 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1537 com_err(program_name, 0, "%s",
1538 _("Illegal number for blocks per group"));
1541 if ((fs_param.s_blocks_per_group % 8) != 0) {
1542 com_err(program_name, 0, "%s",
1543 _("blocks per group must be multiple of 8"));
1548 flex_bg_size = strtoul(optarg, &tmp, 0);
1550 com_err(program_name, 0, "%s",
1551 _("Illegal number for flex_bg size"));
1554 if (flex_bg_size < 1 ||
1555 (flex_bg_size & (flex_bg_size-1)) != 0) {
1556 com_err(program_name, 0, "%s",
1557 _("flex_bg size must be a power of 2"));
1562 inode_ratio = strtoul(optarg, &tmp, 0);
1563 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1564 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1566 com_err(program_name, 0,
1567 _("invalid inode ratio %s (min %d/max %d)"),
1568 optarg, EXT2_MIN_BLOCK_SIZE,
1569 EXT2_MAX_BLOCK_SIZE * 1024);
1574 inode_size = strtoul(optarg, &tmp, 0);
1576 com_err(program_name, 0,
1577 _("invalid inode size - %s"), optarg);
1586 parse_journal_opts(optarg);
1589 fprintf(stderr, "%s",
1590 _("Warning: -K option is deprecated and "
1591 "should not be used anymore. Use "
1592 "\'-E nodiscard\' extended option "
1597 bad_blocks_filename = realloc(bad_blocks_filename,
1598 strlen(optarg) + 1);
1599 if (!bad_blocks_filename) {
1600 com_err(program_name, ENOMEM, "%s",
1601 _("in malloc for bad_blocks_filename"));
1604 strcpy(bad_blocks_filename, optarg);
1607 volume_label = optarg;
1610 reserved_ratio = strtod(optarg, &tmp);
1611 if ( *tmp || reserved_ratio > 50 ||
1612 reserved_ratio < 0) {
1613 com_err(program_name, 0,
1614 _("invalid reserved blocks percent - %s"),
1626 num_inodes = strtoul(optarg, &tmp, 0);
1628 com_err(program_name, 0,
1629 _("bad num inodes - %s"), optarg);
1634 creator_os = optarg;
1637 fs_features = optarg;
1643 r_opt = strtoul(optarg, &tmp, 0);
1645 com_err(program_name, 0,
1646 _("bad revision level - %s"), optarg);
1649 fs_param.s_rev_level = r_opt;
1651 case 's': /* deprecated */
1652 s_opt = atoi(optarg);
1659 com_err(program_name, 0, "%s",
1660 _("The -t option may only be used once"));
1663 fs_type = strdup(optarg);
1667 com_err(program_name, 0, "%s",
1668 _("The -T option may only be used once"));
1671 usage_types = strdup(optarg);
1680 /* Print version number and exit */
1681 show_version_only++;
1687 if ((optind == argc) && !show_version_only)
1689 device_name = argv[optind++];
1691 if (!quiet || show_version_only)
1692 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1695 if (show_version_only) {
1696 fprintf(stderr, _("\tUsing %s\n"),
1697 error_message(EXT2_ET_BASE));
1702 * If there's no blocksize specified and there is a journal
1703 * device, use it to figure out the blocksize
1705 if (blocksize <= 0 && journal_device) {
1709 #ifdef CONFIG_TESTIO_DEBUG
1710 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1711 io_ptr = test_io_manager;
1712 test_io_backing_manager = unix_io_manager;
1715 io_ptr = unix_io_manager;
1716 retval = ext2fs_open(journal_device,
1717 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1720 com_err(program_name, retval,
1721 _("while trying to open journal device %s\n"),
1725 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1726 com_err(program_name, 0,
1727 _("Journal dev blocksize (%d) smaller than "
1728 "minimum blocksize %d\n"), jfs->blocksize,
1732 blocksize = jfs->blocksize;
1733 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1734 fs_param.s_log_block_size =
1735 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1739 if (optind < argc) {
1740 fs_blocks_count = parse_num_blocks2(argv[optind++],
1741 fs_param.s_log_block_size);
1742 if (!fs_blocks_count) {
1743 com_err(program_name, 0,
1744 _("invalid blocks '%s' on device '%s'"),
1745 argv[optind - 1], device_name);
1752 if (!check_plausibility(device_name, 0, NULL) && !force)
1755 check_mount(device_name, force, _("filesystem"));
1757 /* Determine the size of the device (if possible) */
1758 if (noaction && fs_blocks_count) {
1759 dev_size = fs_blocks_count;
1762 retval = ext2fs_get_device_size2(device_name,
1763 EXT2_BLOCK_SIZE(&fs_param),
1766 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1767 com_err(program_name, retval, "%s",
1768 _("while trying to determine filesystem size"));
1771 if (!fs_blocks_count) {
1772 if (retval == EXT2_ET_UNIMPLEMENTED) {
1773 com_err(program_name, 0, "%s",
1774 _("Couldn't determine device size; you "
1775 "must specify\nthe size of the "
1779 if (dev_size == 0) {
1780 com_err(program_name, 0, "%s",
1781 _("Device size reported to be zero. "
1782 "Invalid partition specified, or\n\t"
1783 "partition table wasn't reread "
1784 "after running fdisk, due to\n\t"
1785 "a modified partition being busy "
1786 "and in use. You may need to reboot\n\t"
1787 "to re-read your partition table.\n"
1791 fs_blocks_count = dev_size;
1792 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1793 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1794 EXT2_BLOCK_SIZE(&fs_param))-1));
1796 } else if (!force && (fs_blocks_count > dev_size)) {
1797 com_err(program_name, 0, "%s",
1798 _("Filesystem larger than apparent device size."));
1803 profile_get_string(profile, "devices", device_name,
1804 "fs_type", 0, &fs_type);
1806 profile_get_string(profile, "devices", device_name,
1807 "usage_types", 0, &usage_types);
1810 * We have the file system (or device) size, so we can now
1811 * determine the appropriate file system types so the fs can
1812 * be appropriately configured.
1814 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1815 fs_blocks_count ? fs_blocks_count : dev_size,
1818 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
1822 /* Figure out what features should be enabled */
1825 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1826 tmp = get_string_from_profile(fs_types, "base_features",
1827 "sparse_super,filetype,resize_inode,dir_index");
1828 edit_feature(tmp, &fs_param.s_feature_compat);
1831 /* And which mount options as well */
1832 tmp = get_string_from_profile(fs_types, "default_mntopts",
1834 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1838 for (cpp = fs_types; *cpp; cpp++) {
1840 profile_get_string(profile, "fs_types", *cpp,
1841 "features", "", &tmp);
1843 edit_feature(tmp, &fs_param.s_feature_compat);
1847 tmp = get_string_from_profile(fs_types, "default_features",
1850 edit_feature(fs_features ? fs_features : tmp,
1851 &fs_param.s_feature_compat);
1855 /* Get the hardware sector sizes, if available */
1856 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1858 com_err(program_name, retval, "%s",
1859 _("while trying to determine hardware sector size"));
1862 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1864 com_err(program_name, retval, "%s",
1865 _("while trying to determine physical sector size"));
1869 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1871 lsector_size = atoi(tmp);
1872 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1874 psector_size = atoi(tmp);
1876 /* Older kernels may not have physical/logical distinction */
1878 psector_size = lsector_size;
1880 if (blocksize <= 0) {
1881 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1883 if (use_bsize == -1) {
1884 use_bsize = sys_page_size;
1885 if (is_before_linux_ver(2, 6) && use_bsize > 4096)
1888 if (lsector_size && use_bsize < lsector_size)
1889 use_bsize = lsector_size;
1890 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1891 use_bsize = -blocksize;
1892 blocksize = use_bsize;
1893 fs_blocks_count /= (blocksize / 1024);
1895 if (blocksize < lsector_size) { /* Impossible */
1896 com_err(program_name, EINVAL, "%s",
1897 _("while setting blocksize; too small "
1900 } else if ((blocksize < psector_size) &&
1901 (psector_size <= sys_page_size)) { /* Suboptimal */
1902 fprintf(stderr, _("Warning: specified blocksize %d is "
1903 "less than device physical sectorsize %d\n"),
1904 blocksize, psector_size);
1908 fs_param.s_log_block_size =
1909 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1912 * We now need to do a sanity check of fs_blocks_count for
1913 * 32-bit vs 64-bit block number support.
1915 if ((fs_blocks_count > MAX_32_NUM) &&
1916 (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT))
1917 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1918 if ((fs_blocks_count > MAX_32_NUM) &&
1919 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1920 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1921 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1922 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1924 if ((fs_blocks_count > MAX_32_NUM) &&
1925 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1926 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1927 "too big to be expressed\n\t"
1928 "in 32 bits using a blocksize of %d.\n"),
1929 program_name, fs_blocks_count, device_name,
1930 EXT2_BLOCK_SIZE(&fs_param));
1934 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1936 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1937 fs_types[0] = strdup("journal");
1942 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1943 print_str_list(fs_types);
1946 if (r_opt == EXT2_GOOD_OLD_REV &&
1947 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1948 fs_param.s_feature_ro_compat)) {
1949 fprintf(stderr, "%s", _("Filesystem features not supported "
1950 "with revision 0 filesystems\n"));
1955 if (r_opt == EXT2_GOOD_OLD_REV) {
1956 fprintf(stderr, "%s",
1957 _("Sparse superblocks not supported "
1958 "with revision 0 filesystems\n"));
1961 fs_param.s_feature_ro_compat |=
1962 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1963 } else if (s_opt == 0)
1964 fs_param.s_feature_ro_compat &=
1965 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1967 if (journal_size != 0) {
1968 if (r_opt == EXT2_GOOD_OLD_REV) {
1969 fprintf(stderr, "%s", _("Journals not supported with "
1970 "revision 0 filesystems\n"));
1973 fs_param.s_feature_compat |=
1974 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1977 /* Get reserved_ratio from profile if not specified on cmd line. */
1978 if (reserved_ratio < 0.0) {
1979 reserved_ratio = get_double_from_profile(
1980 fs_types, "reserved_ratio", 5.0);
1981 if (reserved_ratio > 50 || reserved_ratio < 0) {
1982 com_err(program_name, 0,
1983 _("invalid reserved blocks percent - %lf"),
1989 if (fs_param.s_feature_incompat &
1990 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1992 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1993 fs_param.s_feature_compat = 0;
1994 fs_param.s_feature_ro_compat = 0;
1997 /* Check the user's mkfs options for 64bit */
1998 if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1999 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2000 printf("%s", _("Extents MUST be enabled for a 64-bit "
2001 "filesystem. Pass -O extents to rectify.\n"));
2005 /* Set first meta blockgroup via an environment variable */
2006 /* (this is mostly for debugging purposes) */
2007 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2008 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
2009 fs_param.s_first_meta_bg = atoi(tmp);
2010 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2012 cluster_size = get_int_from_profile(fs_types,
2015 fs_param.s_log_cluster_size =
2016 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
2017 if (fs_param.s_log_cluster_size &&
2018 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
2019 com_err(program_name, 0, "%s",
2020 _("The cluster size may not be "
2021 "smaller than the block size.\n"));
2024 } else if (cluster_size) {
2025 com_err(program_name, 0, "%s",
2026 _("specifying a cluster size requires the "
2027 "bigalloc feature"));
2030 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
2032 if (inode_ratio == 0) {
2033 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
2035 if (inode_ratio < blocksize)
2036 inode_ratio = blocksize;
2037 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
2038 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
2041 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
2042 retval = get_device_geometry(device_name, &fs_param, psector_size);
2045 _("warning: Unable to get device geometry for %s\n"),
2047 } else if (retval) {
2048 printf(_("%s alignment is offset by %lu bytes.\n"),
2049 device_name, retval);
2050 printf(_("This may result in very poor performance, "
2051 "(re)-partitioning suggested.\n"));
2055 num_backups = get_int_from_profile(fs_types, "num_backup_sb", 2);
2057 blocksize = EXT2_BLOCK_SIZE(&fs_param);
2060 * Initialize s_desc_size so that the parse_extended_opts()
2061 * can correctly handle "-E resize=NNN" if the 64-bit option
2064 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
2065 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
2067 /* This check should happen beyond the last assignment to blocksize */
2068 if (blocksize > sys_page_size) {
2070 com_err(program_name, 0,
2071 _("%d-byte blocks too big for system (max %d)"),
2072 blocksize, sys_page_size);
2075 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
2076 "(max %d), forced to continue\n"),
2077 blocksize, sys_page_size);
2080 lazy_itable_init = 0;
2081 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2082 lazy_itable_init = 1;
2084 lazy_itable_init = get_bool_from_profile(fs_types,
2087 discard = get_bool_from_profile(fs_types, "discard" , discard);
2088 journal_flags |= get_bool_from_profile(fs_types,
2089 "lazy_journal_init", 0) ?
2090 EXT2_MKJOURNAL_LAZYINIT : 0;
2091 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
2093 if (!journal_location_string)
2094 journal_location_string = get_string_from_profile(fs_types,
2095 "journal_location", "");
2096 if ((journal_location == ~0ULL) && journal_location_string &&
2097 *journal_location_string)
2098 journal_location = parse_num_blocks2(journal_location_string,
2099 fs_param.s_log_block_size);
2100 free(journal_location_string);
2102 packed_meta_blocks = get_bool_from_profile(fs_types,
2103 "packed_meta_blocks", 0);
2104 if (packed_meta_blocks)
2105 journal_location = 0;
2107 /* Get options from profile */
2108 for (cpp = fs_types; *cpp; cpp++) {
2110 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2112 parse_extended_opts(&fs_param, tmp);
2117 parse_extended_opts(&fs_param, extended_opts);
2119 /* Can't support bigalloc feature without extents feature */
2120 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2121 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2122 com_err(program_name, 0, "%s",
2123 _("Can't support bigalloc feature without "
2124 "extents feature"));
2128 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2129 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
2130 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2131 "features are not compatible.\n"
2132 "They can not be both enabled "
2133 "simultaneously.\n"));
2138 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2139 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2140 "still under development\n"
2141 "See https://ext4.wiki.kernel.org/"
2142 "index.php/Bigalloc for more information\n\n"));
2145 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_QUOTA))
2146 fprintf(stderr, "%s", _("\nWarning: the quota feature is "
2147 "still under development\n"
2148 "See https://ext4.wiki.kernel.org/"
2149 "index.php/Quota for more information\n\n"));
2151 /* Since sparse_super is the default, we would only have a problem
2152 * here if it was explicitly disabled.
2154 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2155 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
2156 com_err(program_name, 0, "%s",
2157 _("reserved online resize blocks not supported "
2158 "on non-sparse filesystem"));
2162 if (fs_param.s_blocks_per_group) {
2163 if (fs_param.s_blocks_per_group < 256 ||
2164 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2165 com_err(program_name, 0, "%s",
2166 _("blocks per group count out of range"));
2172 * If the bigalloc feature is enabled, then the -g option will
2173 * specify the number of clusters per group.
2175 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2176 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2177 fs_param.s_blocks_per_group = 0;
2180 if (inode_size == 0)
2181 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2182 if (!flex_bg_size && (fs_param.s_feature_incompat &
2183 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2184 flex_bg_size = get_int_from_profile(fs_types,
2185 "flex_bg_size", 16);
2187 if (!(fs_param.s_feature_incompat &
2188 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
2189 com_err(program_name, 0, "%s",
2190 _("Flex_bg feature not enabled, so "
2191 "flex_bg size may not be specified"));
2194 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2197 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2198 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2199 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2200 inode_size & (inode_size - 1)) {
2201 com_err(program_name, 0,
2202 _("invalid inode size %d (min %d/max %d)"),
2203 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2207 fs_param.s_inode_size = inode_size;
2210 /* Make sure number of inodes specified will fit in 32 bits */
2211 if (num_inodes == 0) {
2212 unsigned long long n;
2213 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2214 if (n > MAX_32_NUM) {
2215 if (fs_param.s_feature_incompat &
2216 EXT4_FEATURE_INCOMPAT_64BIT)
2217 num_inodes = MAX_32_NUM;
2219 com_err(program_name, 0,
2220 _("too many inodes (%llu), raise "
2221 "inode ratio?"), n);
2225 } else if (num_inodes > MAX_32_NUM) {
2226 com_err(program_name, 0,
2227 _("too many inodes (%llu), specify < 2^32 inodes"),
2232 * Calculate number of inodes based on the inode ratio
2234 fs_param.s_inodes_count = num_inodes ? num_inodes :
2235 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2237 if ((((unsigned long long)fs_param.s_inodes_count) *
2238 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2239 ((ext2fs_blocks_count(&fs_param)) *
2240 EXT2_BLOCK_SIZE(&fs_param))) {
2241 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2242 "(%u) too big for a\n\t"
2243 "filesystem with %llu blocks, "
2244 "specify higher inode_ratio (-i)\n\t"
2245 "or lower inode count (-N).\n"),
2246 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2247 fs_param.s_inodes_count,
2248 (unsigned long long) ext2fs_blocks_count(&fs_param));
2253 * Calculate number of blocks to reserve
2255 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2256 ext2fs_blocks_count(&fs_param) / 100.0);
2258 if (fs_param.s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
2259 if (num_backups >= 1)
2260 fs_param.s_backup_bgs[0] = 1;
2261 if (num_backups >= 2)
2262 fs_param.s_backup_bgs[1] = ~0;
2269 static int should_do_undo(const char *name)
2274 struct ext2_super_block super;
2275 io_manager manager = unix_io_manager;
2276 int csum_flag, force_undo;
2278 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2279 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2280 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2281 if (!force_undo && (!csum_flag || !lazy_itable_init))
2284 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2287 * We don't handle error cases instead we
2288 * declare that the file system doesn't exist
2289 * and let the rest of mke2fs take care of
2296 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2297 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2303 #if defined(WORDS_BIGENDIAN)
2304 s_magic = ext2fs_swab16(super.s_magic);
2306 s_magic = super.s_magic;
2309 if (s_magic == EXT2_SUPER_MAGIC)
2313 io_channel_close(channel);
2320 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2322 errcode_t retval = ENOMEM;
2323 char *tdb_dir = NULL, *tdb_file = NULL;
2324 char *dev_name, *tmp_name;
2325 int free_tdb_dir = 0;
2328 * Configuration via a conf file would be
2331 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2333 profile_get_string(profile, "defaults",
2334 "undo_dir", 0, "/var/lib/e2fsprogs",
2339 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2340 access(tdb_dir, W_OK)) {
2346 tmp_name = strdup(name);
2349 dev_name = basename(tmp_name);
2350 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2355 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2358 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2363 set_undo_io_backing_manager(*io_ptr);
2364 *io_ptr = undo_io_manager;
2365 retval = set_undo_io_backup_file(tdb_file);
2368 printf(_("Overwriting existing filesystem; this can be undone "
2369 "using the command:\n"
2370 " e2undo %s %s\n\n"), tdb_file, name);
2381 com_err(program_name, retval, "%s",
2382 _("while trying to setup undo file\n"));
2386 static int mke2fs_discard_device(ext2_filsys fs)
2388 struct ext2fs_numeric_progress_struct progress;
2389 blk64_t blocks = ext2fs_blocks_count(fs->super);
2390 blk64_t count = DISCARD_STEP_MB;
2395 * Let's try if discard really works on the device, so
2396 * we do not print numeric progress resulting in failure
2399 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2402 cur = fs->blocksize;
2404 count *= (1024 * 1024);
2405 count /= fs->blocksize;
2407 ext2fs_numeric_progress_init(fs, &progress,
2408 _("Discarding device blocks: "),
2410 while (cur < blocks) {
2411 ext2fs_numeric_progress_update(fs, &progress, cur);
2413 if (cur + count > blocks)
2414 count = blocks - cur;
2416 retval = io_channel_discard(fs->io, cur, count);
2423 ext2fs_numeric_progress_close(fs, &progress,
2426 printf("%s\n",error_message(retval));
2428 ext2fs_numeric_progress_close(fs, &progress,
2434 static void fix_cluster_bg_counts(ext2_filsys fs)
2436 blk64_t block, num_blocks, last_block, next;
2437 blk64_t tot_free = 0;
2442 num_blocks = ext2fs_blocks_count(fs->super);
2443 last_block = ext2fs_group_last_block2(fs, group);
2444 block = fs->super->s_first_data_block;
2445 while (block < num_blocks) {
2446 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
2447 block, last_block, &next);
2451 block = last_block + 1;
2455 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
2456 block, last_block, &next);
2458 next = last_block + 1;
2459 grp_free += EXT2FS_NUM_B2C(fs, next - block);
2460 tot_free += next - block;
2463 if (block > last_block) {
2465 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2466 ext2fs_group_desc_csum_set(fs, group);
2469 last_block = ext2fs_group_last_block2(fs, group);
2472 ext2fs_free_blocks_count_set(fs->super, tot_free);
2475 static int create_quota_inodes(ext2_filsys fs)
2479 quota_init_context(&qctx, fs, -1);
2480 quota_compute_usage(qctx);
2481 quota_write_inode(qctx, quotatype);
2482 quota_release_context(&qctx);
2487 int main (int argc, char *argv[])
2489 errcode_t retval = 0;
2491 badblocks_list bb_list = 0;
2492 unsigned int journal_blocks = 0;
2493 unsigned int i, checkinterval;
2499 char opt_string[40];
2501 int itable_zeroed = 0;
2504 setlocale(LC_MESSAGES, "");
2505 setlocale(LC_CTYPE, "");
2506 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2507 textdomain(NLS_CAT_NAME);
2508 set_com_err_gettext(gettext);
2512 #ifdef CONFIG_TESTIO_DEBUG
2513 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2514 io_ptr = test_io_manager;
2515 test_io_backing_manager = unix_io_manager;
2518 io_ptr = unix_io_manager;
2520 if (should_do_undo(device_name)) {
2521 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2527 * Initialize the superblock....
2529 flags = EXT2_FLAG_EXCLUSIVE;
2531 flags |= EXT2_FLAG_DIRECT_IO;
2532 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2535 flags |= EXT2_FLAG_64BITS;
2537 * By default, we print how many inode tables or block groups
2538 * or whatever we've written so far. The quiet flag disables
2539 * this, along with a lot of other output.
2542 flags |= EXT2_FLAG_PRINT_PROGRESS;
2543 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2545 com_err(device_name, retval, "%s",
2546 _("while setting up superblock"));
2550 /* Calculate journal blocks */
2551 if (!journal_device && ((journal_size) ||
2552 (fs_param.s_feature_compat &
2553 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2554 journal_blocks = figure_journal_size(journal_size, fs);
2556 /* Can't undo discard ... */
2557 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2558 retval = mke2fs_discard_device(fs);
2559 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2562 _("Discard succeeded and will return "
2563 "0s - skipping inode table wipe\n"));
2564 lazy_itable_init = 1;
2570 sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2571 32768 : fs->blocksize * 8);
2572 io_channel_set_options(fs->io, opt_string);
2574 sprintf(opt_string, "offset=%llu", offset);
2575 io_channel_set_options(fs->io, opt_string);
2578 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2579 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2581 if ((fs_param.s_feature_incompat &
2582 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2583 (fs_param.s_feature_ro_compat &
2584 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2585 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2586 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2587 fs->super->s_kbytes_written = 1;
2590 * Wipe out the old on-disk superblock
2593 zap_sector(fs, 2, 6);
2596 * Parse or generate a UUID for the filesystem
2599 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2600 com_err(device_name, 0, "could not parse UUID: %s\n",
2605 uuid_generate(fs->super->s_uuid);
2608 * Initialize the directory index variables
2610 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2612 hash_alg = e2p_string2hash(hash_alg_str);
2614 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2616 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2619 * Periodic checks can be enabled/disabled via config file.
2620 * Note we override the kernel include file's idea of what the default
2621 * check interval (never) should be. It's a good idea to check at
2622 * least *occasionally*, specially since servers will never rarely get
2623 * to reboot, since Linux is so robust these days. :-)
2625 * 180 days (six months) seems like a good value.
2627 #ifdef EXT2_DFL_CHECKINTERVAL
2628 #undef EXT2_DFL_CHECKINTERVAL
2630 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2632 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2633 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2634 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2636 * Add "jitter" to the superblock's check interval so that we
2637 * don't check all the filesystems at the same time. We use a
2638 * kludgy hack of using the UUID to derive a random jitter value
2640 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2641 val += fs->super->s_uuid[i];
2642 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2644 fs->super->s_max_mnt_count = -1;
2647 * Override the creator OS, if applicable
2649 if (creator_os && !set_os(fs->super, creator_os)) {
2650 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2655 * For the Hurd, we will turn off filetype since it doesn't
2658 if (fs->super->s_creator_os == EXT2_OS_HURD)
2659 fs->super->s_feature_incompat &=
2660 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2663 * Set the volume label...
2666 memset(fs->super->s_volume_name, 0,
2667 sizeof(fs->super->s_volume_name));
2668 strncpy(fs->super->s_volume_name, volume_label,
2669 sizeof(fs->super->s_volume_name));
2673 * Set the last mount directory
2676 memset(fs->super->s_last_mounted, 0,
2677 sizeof(fs->super->s_last_mounted));
2678 strncpy(fs->super->s_last_mounted, mount_dir,
2679 sizeof(fs->super->s_last_mounted));
2682 if (!quiet || noaction)
2688 if (fs->super->s_feature_incompat &
2689 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2690 create_journal_dev(fs);
2691 exit(ext2fs_close(fs) ? 1 : 0);
2694 if (bad_blocks_filename)
2695 read_bb_file(fs, &bb_list, bad_blocks_filename);
2697 test_disk(fs, &bb_list);
2698 handle_bad_blocks(fs, bb_list);
2700 fs->stride = fs_stride = fs->super->s_raid_stride;
2702 printf("%s", _("Allocating group tables: "));
2703 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
2705 retval = packed_allocate_tables(fs);
2707 retval = ext2fs_allocate_tables(fs);
2709 com_err(program_name, retval, "%s",
2710 _("while trying to allocate filesystem tables"));
2714 printf("%s", _("done \n"));
2716 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2718 com_err(program_name, retval, "%s",
2719 _("\n\twhile converting subcluster bitmap"));
2724 fs->super->s_state |= EXT2_ERROR_FS;
2725 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2727 * The command "mke2fs -S" is used to recover
2728 * corrupted file systems, so do not mark any of the
2729 * inodes as unused; we want e2fsck to consider all
2730 * inodes as potentially containing recoverable data.
2732 if (fs->super->s_feature_ro_compat &
2733 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2734 for (i = 0; i < fs->group_desc_count; i++)
2735 ext2fs_bg_itable_unused_set(fs, i, 0);
2738 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2739 blk64_t rsv = 65536 / fs->blocksize;
2740 blk64_t blocks = ext2fs_blocks_count(fs->super);
2744 #ifdef ZAP_BOOTBLOCK
2745 zap_sector(fs, 0, 2);
2749 * Wipe out any old MD RAID (or other) metadata at the end
2750 * of the device. This will also verify that the device is
2751 * as large as we think. Be careful with very small devices.
2753 start = (blocks & ~(rsv - 1));
2757 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2761 com_err(program_name, retval,
2762 _("while zeroing block %llu at end of filesystem"),
2765 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2766 create_root_dir(fs);
2767 create_lost_and_found(fs);
2769 create_bad_block_inode(fs, bb_list);
2770 if (fs->super->s_feature_compat &
2771 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2772 retval = ext2fs_create_resize_inode(fs);
2774 com_err("ext2fs_create_resize_inode", retval,
2776 _("while reserving blocks for online resize"));
2782 if (journal_device) {
2785 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
2788 check_mount(journal_device, force, _("journal"));
2790 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2791 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2792 fs->blocksize, unix_io_manager, &jfs);
2794 com_err(program_name, retval,
2795 _("while trying to open journal device %s\n"),
2800 printf(_("Adding journal to device %s: "),
2804 retval = ext2fs_add_journal_device(fs, jfs);
2806 com_err (program_name, retval,
2807 _("\n\twhile trying to add journal to device %s"),
2812 printf("%s", _("done\n"));
2814 free(journal_device);
2815 } else if ((journal_size) ||
2816 (fs_param.s_feature_compat &
2817 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2819 printf("%s", _("Skipping journal creation in super-only mode\n"));
2820 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2824 if (!journal_blocks) {
2825 fs->super->s_feature_compat &=
2826 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2830 printf(_("Creating journal (%u blocks): "),
2834 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
2838 com_err(program_name, retval, "%s",
2839 _("\n\twhile trying to create journal"));
2843 printf("%s", _("done\n"));
2847 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2848 retval = ext2fs_mmp_init(fs);
2850 fprintf(stderr, "%s",
2851 _("\nError while enabling multiple "
2852 "mount protection feature."));
2856 printf(_("Multiple mount protection is enabled "
2857 "with update interval %d seconds.\n"),
2858 fs->super->s_mmp_update_interval);
2861 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2862 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2863 fix_cluster_bg_counts(fs);
2864 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2865 EXT4_FEATURE_RO_COMPAT_QUOTA))
2866 create_quota_inodes(fs);
2868 retval = mk_hugefiles(fs);
2870 com_err(program_name, retval, "while creating huge files");
2873 printf("%s", _("Writing superblocks and "
2874 "filesystem accounting information: "));
2875 checkinterval = fs->super->s_checkinterval;
2876 max_mnt_count = fs->super->s_max_mnt_count;
2877 retval = ext2fs_close(fs);
2879 fprintf(stderr, "%s",
2880 _("\nWarning, had trouble writing out superblocks."));
2881 } else if (!quiet) {
2882 printf("%s", _("done\n\n"));
2883 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2884 print_check_message(max_mnt_count, checkinterval);
2887 remove_error_table(&et_ext2_error_table);
2888 remove_error_table(&et_prof_error_table);
2889 profile_release(profile);
2890 for (i=0; fs_types[i]; i++)