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/quotaio.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;
105 static int proceed_delay = -1;
106 static blk64_t dev_size;
108 static struct ext2_super_block fs_param;
109 static char *fs_uuid = NULL;
110 static char *creator_os;
111 static char *volume_label;
112 static char *mount_dir;
113 char *journal_device;
114 static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
117 static profile_t profile;
119 static int sys_page_size = 4096;
121 static void usage(void)
123 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
124 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
125 "[-J journal-options]\n"
126 "\t[-G flex-group-size] [-N number-of-inodes]\n"
127 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
128 "\t[-g blocks-per-group] [-L volume-label] "
129 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
130 "[-r fs-revision] [-E extended-option[,...]]\n"
131 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
132 "[-jnqvDFKSV] device [blocks-count]\n"),
137 static int int_log2(unsigned long long arg)
149 int int_log10(unsigned long long arg)
159 static int parse_version_number(const char *s)
161 int major, minor, rev;
167 major = strtol(cp, &endptr, 10);
168 if (cp == endptr || *endptr != '.')
171 minor = strtol(cp, &endptr, 10);
172 if (cp == endptr || *endptr != '.')
175 rev = strtol(cp, &endptr, 10);
178 return KERNEL_VERSION(major, minor, rev);
181 static int is_before_linux_ver(unsigned int major, unsigned int minor,
185 static int linux_version_code = -1;
191 if (linux_version_code < 0)
192 linux_version_code = parse_version_number(ut.release);
193 if (linux_version_code == 0)
196 return linux_version_code < KERNEL_VERSION(major, minor, rev);
199 static int is_before_linux_ver(unsigned int major, unsigned int minor,
207 * Helper function for read_bb_file and test_disk
209 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
211 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
216 * Reads the bad blocks list from a file
218 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
219 const char *bad_blocks_file)
224 f = fopen(bad_blocks_file, "r");
226 com_err("read_bad_blocks_file", errno,
227 _("while trying to open %s"), bad_blocks_file);
230 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
233 com_err("ext2fs_read_bb_FILE", retval, "%s",
234 _("while reading in list of bad blocks from file"));
240 * Runs the badblocks program to test the disk
242 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
248 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
249 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
250 fs->device_name, ext2fs_blocks_count(fs->super)-1);
252 printf(_("Running command: %s\n"), buf);
255 com_err("popen", errno,
256 _("while trying to run '%s'"), buf);
259 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
262 com_err("ext2fs_read_bb_FILE", retval, "%s",
263 _("while processing list of bad blocks from program"));
268 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
272 unsigned must_be_good;
274 badblocks_iterate bb_iter;
284 * The primary superblock and group descriptors *must* be
285 * good; if not, abort.
287 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
288 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
289 if (ext2fs_badblocks_list_test(bb_list, i)) {
290 fprintf(stderr, _("Block %d in primary "
291 "superblock/group descriptor area bad.\n"), i);
292 fprintf(stderr, _("Blocks %u through %u must be good "
293 "in order to build a filesystem.\n"),
294 fs->super->s_first_data_block, must_be_good);
295 fputs(_("Aborting....\n"), stderr);
301 * See if any of the bad blocks are showing up in the backup
302 * superblocks and/or group descriptors. If so, issue a
303 * warning and adjust the block counts appropriately.
305 group_block = fs->super->s_first_data_block +
306 fs->super->s_blocks_per_group;
308 for (i = 1; i < fs->group_desc_count; i++) {
310 for (j=0; j < fs->desc_blocks+1; j++) {
311 if (ext2fs_badblocks_list_test(bb_list,
315 _("Warning: the backup superblock/group descriptors at block %u contain\n"
319 group = ext2fs_group_of_blk2(fs, group_block+j);
320 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
321 ext2fs_group_desc_csum_set(fs, group);
322 ext2fs_free_blocks_count_add(fs->super, 1);
325 group_block += fs->super->s_blocks_per_group;
329 * Mark all the bad blocks as used...
331 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
333 com_err("ext2fs_badblocks_list_iterate_begin", retval, "%s",
334 _("while marking bad blocks as used"));
337 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
338 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
339 ext2fs_badblocks_list_iterate_end(bb_iter);
342 static errcode_t packed_allocate_tables(ext2_filsys fs)
348 for (i = 0; i < fs->group_desc_count; i++) {
349 retval = ext2fs_new_block2(fs, goal, NULL, &goal);
352 ext2fs_block_alloc_stats2(fs, goal, +1);
353 ext2fs_block_bitmap_loc_set(fs, i, goal);
355 for (i = 0; i < fs->group_desc_count; i++) {
356 retval = ext2fs_new_block2(fs, goal, NULL, &goal);
359 ext2fs_block_alloc_stats2(fs, goal, +1);
360 ext2fs_inode_bitmap_loc_set(fs, i, goal);
362 for (i = 0; i < fs->group_desc_count; i++) {
363 blk64_t end = ext2fs_blocks_count(fs->super) - 1;
364 retval = ext2fs_get_free_blocks2(fs, goal, end,
365 fs->inode_blocks_per_group,
366 fs->block_map, &goal);
369 ext2fs_block_alloc_stats_range(fs, goal,
370 fs->inode_blocks_per_group, +1);
371 ext2fs_inode_table_loc_set(fs, i, goal);
372 ext2fs_group_desc_csum_set(fs, i);
377 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
383 struct ext2fs_numeric_progress_struct progress;
385 ext2fs_numeric_progress_init(fs, &progress,
386 _("Writing inode tables: "),
387 fs->group_desc_count);
389 for (i = 0; i < fs->group_desc_count; i++) {
390 ext2fs_numeric_progress_update(fs, &progress, i);
392 blk = ext2fs_inode_table_loc(fs, i);
393 num = fs->inode_blocks_per_group;
396 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
397 ext2fs_bg_itable_unused(fs, i)) *
398 EXT2_INODE_SIZE(fs->super),
399 EXT2_BLOCK_SIZE(fs->super));
400 if (!lazy_flag || itable_zeroed) {
401 /* The kernel doesn't need to zero the itable blocks */
402 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
403 ext2fs_group_desc_csum_set(fs, i);
405 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
407 fprintf(stderr, _("\nCould not write %d "
408 "blocks in inode table starting at %llu: %s\n"),
409 num, blk, error_message(retval));
413 if (sync_kludge == 1)
415 else if ((i % sync_kludge) == 0)
419 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
420 ext2fs_numeric_progress_close(fs, &progress,
424 static void create_root_dir(ext2_filsys fs)
427 struct ext2_inode inode;
429 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
431 com_err("ext2fs_mkdir", retval, "%s",
432 _("while creating root dir"));
435 if (root_uid != 0 || root_gid != 0) {
436 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
438 com_err("ext2fs_read_inode", retval, "%s",
439 _("while reading root inode"));
443 inode.i_uid = root_uid;
444 ext2fs_set_i_uid_high(inode, root_uid >> 16);
445 inode.i_gid = root_gid;
446 ext2fs_set_i_gid_high(inode, root_gid >> 16);
448 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
450 com_err("ext2fs_write_inode", retval, "%s",
451 _("while setting root inode ownership"));
457 static void create_lost_and_found(ext2_filsys fs)
459 unsigned int lpf_size = 0;
462 const char *name = "lost+found";
466 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
468 com_err("ext2fs_mkdir", retval, "%s",
469 _("while creating /lost+found"));
473 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
475 com_err("ext2_lookup", retval, "%s",
476 _("while looking up /lost+found"));
480 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
481 /* Ensure that lost+found is at least 2 blocks, so we always
482 * test large empty blocks for big-block filesystems. */
483 if ((lpf_size += fs->blocksize) >= 16*1024 &&
484 lpf_size >= 2 * fs->blocksize)
486 retval = ext2fs_expand_dir(fs, ino);
488 com_err("ext2fs_expand_dir", retval, "%s",
489 _("while expanding /lost+found"));
495 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
499 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
500 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
501 retval = ext2fs_update_bb_inode(fs, bb_list);
503 com_err("ext2fs_update_bb_inode", retval, "%s",
504 _("while setting bad block inode"));
510 static void reserve_inodes(ext2_filsys fs)
514 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
515 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
516 ext2fs_mark_ib_dirty(fs);
519 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
520 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
521 #define BSD_LABEL_OFFSET 64
523 static void zap_sector(ext2_filsys fs, int sect, int nsect)
529 buf = malloc(512*nsect);
531 printf(_("Out of memory erasing sectors %d-%d\n"),
532 sect, sect + nsect - 1);
537 /* Check for a BSD disklabel, and don't erase it if so */
538 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
541 _("Warning: could not read block 0: %s\n"),
542 error_message(retval));
544 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
545 if ((*magic == BSD_DISKMAGIC) ||
546 (*magic == BSD_MAGICDISK))
551 memset(buf, 0, 512*nsect);
552 io_channel_set_blksize(fs->io, 512);
553 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
554 io_channel_set_blksize(fs->io, fs->blocksize);
557 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
558 sect, error_message(retval));
561 static void create_journal_dev(ext2_filsys fs)
563 struct ext2fs_numeric_progress_struct progress;
566 blk64_t blk, err_blk;
567 int c, count, err_count;
569 retval = ext2fs_create_journal_superblock(fs,
570 ext2fs_blocks_count(fs->super), 0, &buf);
572 com_err("create_journal_dev", retval, "%s",
573 _("while initializing journal superblock"));
577 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
578 goto write_superblock;
580 ext2fs_numeric_progress_init(fs, &progress,
581 _("Zeroing journal device: "),
582 ext2fs_blocks_count(fs->super));
584 count = ext2fs_blocks_count(fs->super);
590 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
592 com_err("create_journal_dev", retval,
593 _("while zeroing journal device "
594 "(block %llu, count %d)"),
600 ext2fs_numeric_progress_update(fs, &progress, blk);
602 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
604 ext2fs_numeric_progress_close(fs, &progress, NULL);
606 retval = io_channel_write_blk64(fs->io,
607 fs->super->s_first_data_block+1,
610 com_err("create_journal_dev", retval, "%s",
611 _("while writing journal superblock"));
616 static void show_stats(ext2_filsys fs)
618 struct ext2_super_block *s = fs->super;
626 printf(_("Creating filesystem with %llu %dk blocks and "
628 ext2fs_blocks_count(s), fs->blocksize >> 10,
633 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
634 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
635 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
637 memset(buf, 0, sizeof(buf));
638 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
639 printf(_("Filesystem label=%s\n"), buf);
640 os = e2p_os2string(fs->super->s_creator_os);
642 printf(_("OS type: %s\n"), os);
644 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
645 s->s_log_block_size);
646 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
647 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
648 printf(_("Cluster size=%u (log=%u)\n"),
649 fs->blocksize << fs->cluster_ratio_bits,
650 s->s_log_cluster_size);
652 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
653 s->s_log_cluster_size);
654 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
655 s->s_raid_stride, s->s_raid_stripe_width);
656 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
657 ext2fs_blocks_count(s));
658 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
659 ext2fs_r_blocks_count(s),
660 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
661 printf(_("First data block=%u\n"), s->s_first_data_block);
662 if (root_uid != 0 || root_gid != 0)
663 printf(_("Root directory owner=%u:%u\n"), root_uid, root_gid);
664 if (s->s_reserved_gdt_blocks)
665 printf(_("Maximum filesystem blocks=%lu\n"),
666 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
667 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
668 if (fs->group_desc_count > 1)
669 printf(_("%u block groups\n"), fs->group_desc_count);
671 printf(_("%u block group\n"), fs->group_desc_count);
672 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
673 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
674 printf(_("%u blocks per group, %u clusters per group\n"),
675 s->s_blocks_per_group, s->s_clusters_per_group);
677 printf(_("%u blocks per group, %u fragments per group\n"),
678 s->s_blocks_per_group, s->s_clusters_per_group);
679 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
682 if (fs->group_desc_count == 1) {
687 if (!e2p_is_null_uuid(s->s_uuid))
688 printf(_("Filesystem UUID: %s\n"), e2p_uuid2str(s->s_uuid));
689 printf("%s", _("Superblock backups stored on blocks: "));
690 group_block = s->s_first_data_block;
692 for (i = 1; i < fs->group_desc_count; i++) {
693 group_block += s->s_blocks_per_group;
694 if (!ext2fs_bg_has_super(fs, i))
698 need = int_log10(group_block) + 2;
699 if (need > col_left) {
704 printf("%llu", group_block);
710 * Set the S_CREATOR_OS field. Return true if OS is known,
713 static int set_os(struct ext2_super_block *sb, char *os)
716 sb->s_creator_os = atoi (os);
717 else if (strcasecmp(os, "linux") == 0)
718 sb->s_creator_os = EXT2_OS_LINUX;
719 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
720 sb->s_creator_os = EXT2_OS_HURD;
721 else if (strcasecmp(os, "freebsd") == 0)
722 sb->s_creator_os = EXT2_OS_FREEBSD;
723 else if (strcasecmp(os, "lites") == 0)
724 sb->s_creator_os = EXT2_OS_LITES;
730 #define PATH_SET "PATH=/sbin"
732 static void parse_extended_opts(struct ext2_super_block *param,
735 char *buf, *token, *next, *p, *arg, *badopt = 0;
742 fprintf(stderr, "%s",
743 _("Couldn't allocate memory to parse options!\n"));
747 for (token = buf; token && *token; token = next) {
748 p = strchr(token, ',');
754 arg = strchr(token, '=');
759 if (strcmp(token, "desc-size") == 0 ||
760 strcmp(token, "desc_size") == 0) {
763 if (!(fs_param.s_feature_incompat &
764 EXT4_FEATURE_INCOMPAT_64BIT)) {
766 _("%s requires '-O 64bit'\n"), token);
770 if (param->s_reserved_gdt_blocks != 0) {
772 _("'%s' must be before 'resize=%u'\n"),
773 token, param->s_reserved_gdt_blocks);
782 desc_size = strtoul(arg, &p, 0);
783 if (*p || (desc_size & (desc_size - 1))) {
785 _("Invalid desc_size: '%s'\n"), arg);
789 param->s_desc_size = desc_size;
790 } else if (strcmp(token, "offset") == 0) {
796 offset = strtoull(arg, &p, 0);
798 fprintf(stderr, _("Invalid offset: %s\n"),
803 } else if (strcmp(token, "mmp_update_interval") == 0) {
809 param->s_mmp_update_interval = strtoul(arg, &p, 0);
812 _("Invalid mmp_update_interval: %s\n"),
817 } else if (strcmp(token, "num_backup_sb") == 0) {
823 num_backups = strtoul(arg, &p, 0);
824 if (*p || num_backups > 2) {
826 _("Invalid # of backup "
827 "superblocks: %s\n"),
832 } else if (strcmp(token, "packed_meta_blocks") == 0) {
834 packed_meta_blocks = strtoul(arg, &p, 0);
836 packed_meta_blocks = 1;
837 if (packed_meta_blocks)
838 journal_location = 0;
839 } else if (strcmp(token, "stride") == 0) {
845 param->s_raid_stride = strtoul(arg, &p, 0);
848 _("Invalid stride parameter: %s\n"),
853 } else if (strcmp(token, "stripe-width") == 0 ||
854 strcmp(token, "stripe_width") == 0) {
860 param->s_raid_stripe_width = strtoul(arg, &p, 0);
863 _("Invalid stripe-width parameter: %s\n"),
868 } else if (!strcmp(token, "resize")) {
870 unsigned long bpg, rsv_groups;
871 unsigned long group_desc_count, desc_blocks;
872 unsigned int gdpb, blocksize;
881 resize = parse_num_blocks2(arg,
882 param->s_log_block_size);
886 _("Invalid resize parameter: %s\n"),
891 if (resize <= ext2fs_blocks_count(param)) {
892 fprintf(stderr, "%s",
893 _("The resize maximum must be greater "
894 "than the filesystem size.\n"));
899 blocksize = EXT2_BLOCK_SIZE(param);
900 bpg = param->s_blocks_per_group;
903 gdpb = EXT2_DESC_PER_BLOCK(param);
904 group_desc_count = (__u32) ext2fs_div64_ceil(
905 ext2fs_blocks_count(param), bpg);
906 desc_blocks = (group_desc_count +
908 rsv_groups = ext2fs_div64_ceil(resize, bpg);
909 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
911 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
912 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
915 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
916 fprintf(stderr, "%s",
917 _("On-line resizing not supported with revision 0 filesystems\n"));
921 param->s_feature_compat |=
922 EXT2_FEATURE_COMPAT_RESIZE_INODE;
924 param->s_reserved_gdt_blocks = rsv_gdb;
926 } else if (!strcmp(token, "test_fs")) {
927 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
928 } else if (!strcmp(token, "lazy_itable_init")) {
930 lazy_itable_init = strtoul(arg, &p, 0);
932 lazy_itable_init = 1;
933 } else if (!strcmp(token, "lazy_journal_init")) {
935 journal_flags |= strtoul(arg, &p, 0) ?
936 EXT2_MKJOURNAL_LAZYINIT : 0;
938 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
939 } else if (!strcmp(token, "root_owner")) {
941 root_uid = strtoul(arg, &p, 0);
944 _("Invalid root_owner: '%s'\n"),
950 root_gid = strtoul(p, &p, 0);
953 _("Invalid root_owner: '%s'\n"),
962 } else if (!strcmp(token, "discard")) {
964 } else if (!strcmp(token, "nodiscard")) {
966 } else if (!strcmp(token, "quotatype")) {
972 if (!strncmp(arg, "usr", 3)) {
974 } else if (!strncmp(arg, "grp", 3)) {
978 _("Invalid quotatype parameter: %s\n"),
989 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
990 "Extended options are separated by commas, "
991 "and may take an argument which\n"
992 "\tis set off by an equals ('=') sign.\n\n"
993 "Valid extended options are:\n"
994 "\tmmp_update_interval=<interval>\n"
995 "\tnum_backup_sb=<0|1|2>\n"
996 "\tstride=<RAID per-disk data chunk in blocks>\n"
997 "\tstripe-width=<RAID stride * data disks in blocks>\n"
998 "\toffset=<offset to create the file system>\n"
999 "\tresize=<resize maximum size in blocks>\n"
1000 "\tpacked_meta_blocks=<0 to disable, 1 to enable>\n"
1001 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
1002 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
1003 "\troot_uid=<uid of root directory>\n"
1004 "\troot_gid=<gid of root directory>\n"
1008 "\tquotatype=<usr OR grp>\n\n"),
1009 badopt ? badopt : "");
1013 if (param->s_raid_stride &&
1014 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
1015 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
1016 "multiple of stride %u.\n\n"),
1017 param->s_raid_stripe_width, param->s_raid_stride);
1022 static __u32 ok_features[3] = {
1024 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
1025 EXT2_FEATURE_COMPAT_RESIZE_INODE |
1026 EXT2_FEATURE_COMPAT_DIR_INDEX |
1027 EXT2_FEATURE_COMPAT_EXT_ATTR |
1028 EXT4_FEATURE_COMPAT_SPARSE_SUPER2,
1030 EXT2_FEATURE_INCOMPAT_FILETYPE|
1031 EXT3_FEATURE_INCOMPAT_EXTENTS|
1032 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
1033 EXT2_FEATURE_INCOMPAT_META_BG|
1034 EXT4_FEATURE_INCOMPAT_FLEX_BG|
1035 EXT4_FEATURE_INCOMPAT_MMP |
1036 EXT4_FEATURE_INCOMPAT_64BIT,
1038 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
1039 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
1040 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
1041 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
1042 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
1043 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1044 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
1046 EXT4_FEATURE_RO_COMPAT_QUOTA|
1052 static void syntax_err_report(const char *filename, long err, int line_num)
1055 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
1056 filename, line_num, error_message(err));
1060 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
1062 static void edit_feature(const char *str, __u32 *compat_array)
1067 if (e2p_edit_feature(str, compat_array, ok_features)) {
1068 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
1074 static void edit_mntopts(const char *str, __u32 *mntopts)
1079 if (e2p_edit_mntopts(str, mntopts, ~0)) {
1080 fprintf(stderr, _("Invalid mount option set: %s\n"),
1092 static errcode_t init_list(struct str_list *sl)
1096 sl->list = malloc((sl->max+1) * sizeof(char *));
1103 static errcode_t push_string(struct str_list *sl, const char *str)
1107 if (sl->num >= sl->max) {
1109 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
1112 sl->list = new_list;
1114 sl->list[sl->num] = malloc(strlen(str)+1);
1115 if (sl->list[sl->num] == 0)
1117 strcpy(sl->list[sl->num], str);
1119 sl->list[sl->num] = 0;
1123 static void print_str_list(char **list)
1127 for (cpp = list; *cpp; cpp++) {
1128 printf("'%s'", *cpp);
1130 fputs(", ", stdout);
1132 fputc('\n', stdout);
1136 * Return TRUE if the profile has the given subsection
1138 static int profile_has_subsection(profile_t prof, const char *section,
1139 const char *subsection)
1142 const char *names[4];
1147 names[1] = subsection;
1150 if (profile_iterator_create(prof, names,
1151 PROFILE_ITER_LIST_SECTION |
1152 PROFILE_ITER_RELATIONS_ONLY, &state))
1155 if ((profile_iterator(&state, &name, 0) == 0) && name) {
1160 profile_iterator_free(&state);
1164 static char **parse_fs_type(const char *fs_type,
1165 const char *usage_types,
1166 struct ext2_super_block *sb,
1167 blk64_t fs_blocks_count,
1170 const char *ext_type = 0;
1172 char *profile_type = 0;
1174 const char *size_type;
1175 struct str_list list;
1176 unsigned long long meg;
1179 if (init_list(&list))
1182 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1183 !strcasecmp(creator_os, "hurd")))
1190 else if (!strcmp(program_name, "mke3fs"))
1192 else if (!strcmp(program_name, "mke4fs"))
1194 else if (progname) {
1195 ext_type = strrchr(progname, '/');
1199 ext_type = progname;
1201 if (!strncmp(ext_type, "mkfs.", 5)) {
1203 if (ext_type[0] == 0)
1210 profile_get_string(profile, "defaults", "fs_type", 0,
1211 "ext2", &profile_type);
1212 ext_type = profile_type;
1213 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1218 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1219 strcmp(ext_type, "ext2")) {
1220 printf(_("\nYour mke2fs.conf file does not define the "
1221 "%s filesystem type.\n"), ext_type);
1222 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1223 !strcmp(ext_type, "ext4dev")) {
1224 printf("%s", _("You probably need to install an "
1225 "updated mke2fs.conf file.\n\n"));
1228 printf("%s", _("Aborting...\n"));
1233 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(sb);
1234 if (fs_blocks_count < 3 * meg)
1235 size_type = "floppy";
1236 else if (fs_blocks_count < 512 * meg)
1237 size_type = "small";
1238 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1239 size_type = "default";
1240 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1246 usage_types = size_type;
1248 parse_str = malloc(strlen(usage_types)+1);
1254 strcpy(parse_str, usage_types);
1257 push_string(&list, ext_type);
1260 t = strchr(cp, ',');
1265 if (profile_has_subsection(profile, "fs_types", cp))
1266 push_string(&list, cp);
1267 else if (strcmp(cp, "default") != 0)
1269 _("\nWarning: the fs_type %s is not "
1270 "defined in mke2fs.conf\n\n"),
1281 push_string(&list, "hurd");
1285 char *get_string_from_profile(char **types, const char *opt,
1286 const char *def_val)
1291 for (i=0; types[i]; i++);
1292 for (i-=1; i >=0 ; i--) {
1293 profile_get_string(profile, "fs_types", types[i],
1298 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1302 int get_int_from_profile(char **types, const char *opt, int def_val)
1307 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1308 for (cpp = types; *cpp; cpp++)
1309 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1313 static unsigned int get_uint_from_profile(char **types, const char *opt,
1314 unsigned int def_val)
1319 profile_get_uint(profile, "defaults", opt, 0, def_val, &ret);
1320 for (cpp = types; *cpp; cpp++)
1321 profile_get_uint(profile, "fs_types", *cpp, opt, ret, &ret);
1325 static double get_double_from_profile(char **types, const char *opt,
1331 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1332 for (cpp = types; *cpp; cpp++)
1333 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1337 int get_bool_from_profile(char **types, const char *opt, int def_val)
1342 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1343 for (cpp = types; *cpp; cpp++)
1344 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1348 extern const char *mke2fs_default_profile;
1349 static const char *default_files[] = { "<default>", 0 };
1351 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1353 * Sets the geometry of a device (stripe/stride), and returns the
1354 * device's alignment offset, if any, or a negative error.
1356 static int get_device_geometry(const char *file,
1357 struct ext2_super_block *fs_param,
1364 unsigned long min_io;
1365 unsigned long opt_io;
1366 struct stat statbuf;
1368 /* Nothing to do for a regular file */
1369 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1372 pr = blkid_new_probe_from_filename(file);
1376 tp = blkid_probe_get_topology(pr);
1380 min_io = blkid_topology_get_minimum_io_size(tp);
1381 opt_io = blkid_topology_get_optimal_io_size(tp);
1382 blocksize = EXT2_BLOCK_SIZE(fs_param);
1383 if ((min_io == 0) && (psector_size > blocksize))
1384 min_io = psector_size;
1385 if ((opt_io == 0) && min_io)
1387 if ((opt_io == 0) && (psector_size > blocksize))
1388 opt_io = psector_size;
1390 /* setting stripe/stride to blocksize is pointless */
1391 if (min_io > blocksize)
1392 fs_param->s_raid_stride = min_io / blocksize;
1393 if (opt_io > blocksize)
1394 fs_param->s_raid_stripe_width = opt_io / blocksize;
1396 rc = blkid_topology_get_alignment_offset(tp);
1398 blkid_free_probe(pr);
1403 static void PRS(int argc, char *argv[])
1406 int cluster_size = 0;
1409 int inode_ratio = 0;
1411 unsigned long flex_bg_size = 0;
1412 double reserved_ratio = -1.0;
1413 int lsector_size = 0, psector_size = 0;
1414 int show_version_only = 0, is_device = 0;
1415 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1417 char * oldpath = getenv("PATH");
1418 char * extended_opts = 0;
1420 char * usage_types = 0;
1422 * NOTE: A few words about fs_blocks_count and blocksize:
1424 * Initially, blocksize is set to zero, which implies 1024.
1425 * If -b is specified, blocksize is updated to the user's value.
1427 * Next, the device size or the user's "blocks" command line argument
1428 * is used to set fs_blocks_count; the units are blocksize.
1430 * Later, if blocksize hasn't been set and the profile specifies a
1431 * blocksize, then blocksize is updated and fs_blocks_count is scaled
1432 * appropriately. Note the change in units!
1434 * Finally, we complain about fs_blocks_count > 2^32 on a non-64bit fs.
1436 blk64_t fs_blocks_count = 0;
1438 int s_opt = -1, r_opt = -1;
1439 char *fs_features = 0;
1442 int pathlen = sizeof(PATH_SET) + 1;
1445 pathlen += strlen(oldpath);
1446 newpath = malloc(pathlen);
1448 fprintf(stderr, "%s",
1449 _("Couldn't allocate memory for new PATH.\n"));
1452 strcpy(newpath, PATH_SET);
1454 /* Update our PATH to include /sbin */
1456 strcat (newpath, ":");
1457 strcat (newpath, oldpath);
1461 tmp = getenv("MKE2FS_SYNC");
1463 sync_kludge = atoi(tmp);
1465 /* Determine the system page size if possible */
1467 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1468 #define _SC_PAGESIZE _SC_PAGE_SIZE
1471 sysval = sysconf(_SC_PAGESIZE);
1473 sys_page_size = sysval;
1474 #endif /* _SC_PAGESIZE */
1475 #endif /* HAVE_SYSCONF */
1477 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1479 profile_set_syntax_err_cb(syntax_err_report);
1480 retval = profile_init(config_fn, &profile);
1481 if (retval == ENOENT) {
1482 retval = profile_init(default_files, &profile);
1485 retval = profile_set_default(profile, mke2fs_default_profile);
1488 } else if (retval) {
1490 fprintf(stderr, _("Couldn't init profile successfully"
1491 " (error: %ld).\n"), retval);
1495 setbuf(stdout, NULL);
1496 setbuf(stderr, NULL);
1497 add_error_table(&et_ext2_error_table);
1498 add_error_table(&et_prof_error_table);
1499 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1500 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1502 if (is_before_linux_ver(2, 2, 0))
1503 fs_param.s_rev_level = 0;
1505 if (argc && *argv) {
1506 program_name = get_progname(*argv);
1508 /* If called as mkfs.ext3, create a journal inode */
1509 if (!strcmp(program_name, "mkfs.ext3") ||
1510 !strcmp(program_name, "mke3fs"))
1514 while ((c = getopt (argc, argv,
1515 "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1518 blocksize = parse_num_blocks2(optarg, -1);
1519 b = (blocksize > 0) ? blocksize : -blocksize;
1520 if (b < EXT2_MIN_BLOCK_SIZE ||
1521 b > EXT2_MAX_BLOCK_SIZE) {
1522 com_err(program_name, 0,
1523 _("invalid block size - %s"), optarg);
1526 if (blocksize > 4096)
1527 fprintf(stderr, _("Warning: blocksize %d not "
1528 "usable on most systems.\n"),
1531 fs_param.s_log_block_size =
1532 int_log2(blocksize >>
1533 EXT2_MIN_BLOCK_LOG_SIZE);
1535 case 'c': /* Check for bad blocks */
1539 cluster_size = parse_num_blocks2(optarg, -1);
1540 if (cluster_size <= EXT2_MIN_CLUSTER_SIZE ||
1541 cluster_size > EXT2_MAX_CLUSTER_SIZE) {
1542 com_err(program_name, 0,
1543 _("invalid cluster size - %s"),
1552 com_err(program_name, 0, "%s",
1553 _("'-R' is deprecated, use '-E' instead"));
1556 extended_opts = optarg;
1562 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1564 com_err(program_name, 0, "%s",
1565 _("Illegal number for blocks per group"));
1568 if ((fs_param.s_blocks_per_group % 8) != 0) {
1569 com_err(program_name, 0, "%s",
1570 _("blocks per group must be multiple of 8"));
1575 flex_bg_size = strtoul(optarg, &tmp, 0);
1577 com_err(program_name, 0, "%s",
1578 _("Illegal number for flex_bg size"));
1581 if (flex_bg_size < 1 ||
1582 (flex_bg_size & (flex_bg_size-1)) != 0) {
1583 com_err(program_name, 0, "%s",
1584 _("flex_bg size must be a power of 2"));
1587 if (flex_bg_size > MAX_32_NUM) {
1588 com_err(program_name, 0,
1589 _("flex_bg size (%lu) must be less than"
1590 " or equal to 2^31"), flex_bg_size);
1595 inode_ratio = strtoul(optarg, &tmp, 0);
1596 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1597 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1599 com_err(program_name, 0,
1600 _("invalid inode ratio %s (min %d/max %d)"),
1601 optarg, EXT2_MIN_BLOCK_SIZE,
1602 EXT2_MAX_BLOCK_SIZE * 1024);
1607 inode_size = strtoul(optarg, &tmp, 0);
1609 com_err(program_name, 0,
1610 _("invalid inode size - %s"), optarg);
1619 parse_journal_opts(optarg);
1622 fprintf(stderr, "%s",
1623 _("Warning: -K option is deprecated and "
1624 "should not be used anymore. Use "
1625 "\'-E nodiscard\' extended option "
1630 bad_blocks_filename = realloc(bad_blocks_filename,
1631 strlen(optarg) + 1);
1632 if (!bad_blocks_filename) {
1633 com_err(program_name, ENOMEM, "%s",
1634 _("in malloc for bad_blocks_filename"));
1637 strcpy(bad_blocks_filename, optarg);
1640 volume_label = optarg;
1643 reserved_ratio = strtod(optarg, &tmp);
1644 if ( *tmp || reserved_ratio > 50 ||
1645 reserved_ratio < 0) {
1646 com_err(program_name, 0,
1647 _("invalid reserved blocks percent - %s"),
1659 num_inodes = strtoul(optarg, &tmp, 0);
1661 com_err(program_name, 0,
1662 _("bad num inodes - %s"), optarg);
1667 creator_os = optarg;
1670 fs_features = optarg;
1676 r_opt = strtoul(optarg, &tmp, 0);
1678 com_err(program_name, 0,
1679 _("bad revision level - %s"), optarg);
1682 if (r_opt > EXT2_MAX_SUPP_REV) {
1683 com_err(program_name, EXT2_ET_REV_TOO_HIGH,
1684 _("while trying to create revision %d"), r_opt);
1687 fs_param.s_rev_level = r_opt;
1689 case 's': /* deprecated */
1690 s_opt = atoi(optarg);
1697 com_err(program_name, 0, "%s",
1698 _("The -t option may only be used once"));
1701 fs_type = strdup(optarg);
1705 com_err(program_name, 0, "%s",
1706 _("The -T option may only be used once"));
1709 usage_types = strdup(optarg);
1718 /* Print version number and exit */
1719 show_version_only++;
1725 if ((optind == argc) && !show_version_only)
1727 device_name = argv[optind++];
1729 if (!quiet || show_version_only)
1730 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1733 if (show_version_only) {
1734 fprintf(stderr, _("\tUsing %s\n"),
1735 error_message(EXT2_ET_BASE));
1740 * If there's no blocksize specified and there is a journal
1741 * device, use it to figure out the blocksize
1743 if (blocksize <= 0 && journal_device) {
1747 #ifdef CONFIG_TESTIO_DEBUG
1748 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1749 io_ptr = test_io_manager;
1750 test_io_backing_manager = unix_io_manager;
1753 io_ptr = unix_io_manager;
1754 retval = ext2fs_open(journal_device,
1755 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1758 com_err(program_name, retval,
1759 _("while trying to open journal device %s\n"),
1763 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1764 com_err(program_name, 0,
1765 _("Journal dev blocksize (%d) smaller than "
1766 "minimum blocksize %d\n"), jfs->blocksize,
1770 blocksize = jfs->blocksize;
1771 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1772 fs_param.s_log_block_size =
1773 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1774 ext2fs_close_free(&jfs);
1777 if (optind < argc) {
1778 fs_blocks_count = parse_num_blocks2(argv[optind++],
1779 fs_param.s_log_block_size);
1780 if (!fs_blocks_count) {
1781 com_err(program_name, 0,
1782 _("invalid blocks '%s' on device '%s'"),
1783 argv[optind - 1], device_name);
1790 profile_get_integer(profile, "options", "proceed_delay", 0, 0,
1793 /* The isatty() test is so we don't break existing scripts */
1794 flags = CREATE_FILE;
1795 if (isatty(0) && isatty(1))
1796 flags |= CHECK_FS_EXIST;
1798 flags |= VERBOSE_CREATE;
1799 if (!check_plausibility(device_name, flags, &is_device) && !force)
1800 proceed_question(proceed_delay);
1802 check_mount(device_name, force, _("filesystem"));
1804 /* Determine the size of the device (if possible) */
1805 if (noaction && fs_blocks_count) {
1806 dev_size = fs_blocks_count;
1809 retval = ext2fs_get_device_size2(device_name,
1810 EXT2_BLOCK_SIZE(&fs_param),
1813 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1814 com_err(program_name, retval, "%s",
1815 _("while trying to determine filesystem size"));
1818 if (!fs_blocks_count) {
1819 if (retval == EXT2_ET_UNIMPLEMENTED) {
1820 com_err(program_name, 0, "%s",
1821 _("Couldn't determine device size; you "
1822 "must specify\nthe size of the "
1826 if (dev_size == 0) {
1827 com_err(program_name, 0, "%s",
1828 _("Device size reported to be zero. "
1829 "Invalid partition specified, or\n\t"
1830 "partition table wasn't reread "
1831 "after running fdisk, due to\n\t"
1832 "a modified partition being busy "
1833 "and in use. You may need to reboot\n\t"
1834 "to re-read your partition table.\n"
1838 fs_blocks_count = dev_size;
1839 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1840 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1841 EXT2_BLOCK_SIZE(&fs_param))-1));
1843 } else if (!force && is_device && (fs_blocks_count > dev_size)) {
1844 com_err(program_name, 0, "%s",
1845 _("Filesystem larger than apparent device size."));
1846 proceed_question(proceed_delay);
1850 profile_get_string(profile, "devices", device_name,
1851 "fs_type", 0, &fs_type);
1853 profile_get_string(profile, "devices", device_name,
1854 "usage_types", 0, &usage_types);
1857 * We have the file system (or device) size, so we can now
1858 * determine the appropriate file system types so the fs can
1859 * be appropriately configured.
1861 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1862 fs_blocks_count ? fs_blocks_count : dev_size,
1865 fprintf(stderr, "%s", _("Failed to parse fs types list\n"));
1869 /* Figure out what features should be enabled */
1872 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1873 tmp = get_string_from_profile(fs_types, "base_features",
1874 "sparse_super,filetype,resize_inode,dir_index");
1875 edit_feature(tmp, &fs_param.s_feature_compat);
1878 /* And which mount options as well */
1879 tmp = get_string_from_profile(fs_types, "default_mntopts",
1881 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1885 for (cpp = fs_types; *cpp; cpp++) {
1887 profile_get_string(profile, "fs_types", *cpp,
1888 "features", "", &tmp);
1890 edit_feature(tmp, &fs_param.s_feature_compat);
1894 tmp = get_string_from_profile(fs_types, "default_features",
1897 edit_feature(fs_features ? fs_features : tmp,
1898 &fs_param.s_feature_compat);
1902 /* Get the hardware sector sizes, if available */
1903 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1905 com_err(program_name, retval, "%s",
1906 _("while trying to determine hardware sector size"));
1909 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1911 com_err(program_name, retval, "%s",
1912 _("while trying to determine physical sector size"));
1916 tmp = getenv("MKE2FS_DEVICE_SECTSIZE");
1918 lsector_size = atoi(tmp);
1919 tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE");
1921 psector_size = atoi(tmp);
1923 /* Older kernels may not have physical/logical distinction */
1925 psector_size = lsector_size;
1927 if (blocksize <= 0) {
1928 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1930 if (use_bsize == -1) {
1931 use_bsize = sys_page_size;
1932 if (is_before_linux_ver(2, 6, 0) && use_bsize > 4096)
1935 if (lsector_size && use_bsize < lsector_size)
1936 use_bsize = lsector_size;
1937 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1938 use_bsize = -blocksize;
1939 blocksize = use_bsize;
1940 fs_blocks_count /= (blocksize / 1024);
1942 if (blocksize < lsector_size) { /* Impossible */
1943 com_err(program_name, EINVAL, "%s",
1944 _("while setting blocksize; too small "
1947 } else if ((blocksize < psector_size) &&
1948 (psector_size <= sys_page_size)) { /* Suboptimal */
1949 fprintf(stderr, _("Warning: specified blocksize %d is "
1950 "less than device physical sectorsize %d\n"),
1951 blocksize, psector_size);
1955 fs_param.s_log_block_size =
1956 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1959 * We now need to do a sanity check of fs_blocks_count for
1960 * 32-bit vs 64-bit block number support.
1962 if ((fs_blocks_count > MAX_32_NUM) &&
1963 (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT))
1964 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1965 if ((fs_blocks_count > MAX_32_NUM) &&
1966 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1967 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1968 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1969 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1971 if ((fs_blocks_count > MAX_32_NUM) &&
1972 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1973 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1974 "too big to be expressed\n\t"
1975 "in 32 bits using a blocksize of %d.\n"),
1976 program_name, fs_blocks_count, device_name,
1977 EXT2_BLOCK_SIZE(&fs_param));
1981 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1983 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1984 fs_types[0] = strdup("journal");
1989 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1990 print_str_list(fs_types);
1993 if (r_opt == EXT2_GOOD_OLD_REV &&
1994 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1995 fs_param.s_feature_ro_compat)) {
1996 fprintf(stderr, "%s", _("Filesystem features not supported "
1997 "with revision 0 filesystems\n"));
2002 if (r_opt == EXT2_GOOD_OLD_REV) {
2003 fprintf(stderr, "%s",
2004 _("Sparse superblocks not supported "
2005 "with revision 0 filesystems\n"));
2008 fs_param.s_feature_ro_compat |=
2009 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2010 } else if (s_opt == 0)
2011 fs_param.s_feature_ro_compat &=
2012 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
2014 if (journal_size != 0) {
2015 if (r_opt == EXT2_GOOD_OLD_REV) {
2016 fprintf(stderr, "%s", _("Journals not supported with "
2017 "revision 0 filesystems\n"));
2020 fs_param.s_feature_compat |=
2021 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2024 /* Get reserved_ratio from profile if not specified on cmd line. */
2025 if (reserved_ratio < 0.0) {
2026 reserved_ratio = get_double_from_profile(
2027 fs_types, "reserved_ratio", 5.0);
2028 if (reserved_ratio > 50 || reserved_ratio < 0) {
2029 com_err(program_name, 0,
2030 _("invalid reserved blocks percent - %lf"),
2036 if (fs_param.s_feature_incompat &
2037 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2039 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
2040 fs_param.s_feature_compat = 0;
2041 fs_param.s_feature_ro_compat = 0;
2044 /* Check the user's mkfs options for 64bit */
2045 if ((fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
2046 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2047 printf("%s", _("Extents MUST be enabled for a 64-bit "
2048 "filesystem. Pass -O extents to rectify.\n"));
2052 /* Set first meta blockgroup via an environment variable */
2053 /* (this is mostly for debugging purposes) */
2054 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2055 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
2056 fs_param.s_first_meta_bg = atoi(tmp);
2057 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2059 cluster_size = get_int_from_profile(fs_types,
2062 fs_param.s_log_cluster_size =
2063 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
2064 if (fs_param.s_log_cluster_size &&
2065 fs_param.s_log_cluster_size < fs_param.s_log_block_size) {
2066 com_err(program_name, 0, "%s",
2067 _("The cluster size may not be "
2068 "smaller than the block size.\n"));
2071 } else if (cluster_size) {
2072 com_err(program_name, 0, "%s",
2073 _("specifying a cluster size requires the "
2074 "bigalloc feature"));
2077 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
2079 if (inode_ratio == 0) {
2080 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
2082 if (inode_ratio < blocksize)
2083 inode_ratio = blocksize;
2084 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
2085 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
2088 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
2089 retval = get_device_geometry(device_name, &fs_param, psector_size);
2092 _("warning: Unable to get device geometry for %s\n"),
2094 } else if (retval) {
2095 printf(_("%s alignment is offset by %lu bytes.\n"),
2096 device_name, retval);
2097 printf(_("This may result in very poor performance, "
2098 "(re)-partitioning suggested.\n"));
2102 num_backups = get_int_from_profile(fs_types, "num_backup_sb", 2);
2104 blocksize = EXT2_BLOCK_SIZE(&fs_param);
2107 * Initialize s_desc_size so that the parse_extended_opts()
2108 * can correctly handle "-E resize=NNN" if the 64-bit option
2111 if (fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)
2112 fs_param.s_desc_size = EXT2_MIN_DESC_SIZE_64BIT;
2114 /* This check should happen beyond the last assignment to blocksize */
2115 if (blocksize > sys_page_size) {
2117 com_err(program_name, 0,
2118 _("%d-byte blocks too big for system (max %d)"),
2119 blocksize, sys_page_size);
2120 proceed_question(proceed_delay);
2122 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
2123 "(max %d), forced to continue\n"),
2124 blocksize, sys_page_size);
2128 * On newer kernels we do have lazy_itable_init support. So pick the
2129 * right default in case ext4 module is not loaded.
2131 if (is_before_linux_ver(2, 6, 37))
2132 lazy_itable_init = 0;
2134 lazy_itable_init = 1;
2136 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
2137 lazy_itable_init = 1;
2139 lazy_itable_init = get_bool_from_profile(fs_types,
2142 discard = get_bool_from_profile(fs_types, "discard" , discard);
2143 journal_flags |= get_bool_from_profile(fs_types,
2144 "lazy_journal_init", 0) ?
2145 EXT2_MKJOURNAL_LAZYINIT : 0;
2146 journal_flags |= EXT2_MKJOURNAL_NO_MNT_CHECK;
2148 if (!journal_location_string)
2149 journal_location_string = get_string_from_profile(fs_types,
2150 "journal_location", "");
2151 if ((journal_location == ~0ULL) && journal_location_string &&
2152 *journal_location_string)
2153 journal_location = parse_num_blocks2(journal_location_string,
2154 fs_param.s_log_block_size);
2155 free(journal_location_string);
2157 packed_meta_blocks = get_bool_from_profile(fs_types,
2158 "packed_meta_blocks", 0);
2159 if (packed_meta_blocks)
2160 journal_location = 0;
2162 /* Get options from profile */
2163 for (cpp = fs_types; *cpp; cpp++) {
2165 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
2167 parse_extended_opts(&fs_param, tmp);
2172 parse_extended_opts(&fs_param, extended_opts);
2174 /* Can't support bigalloc feature without extents feature */
2175 if ((fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2176 !(fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)) {
2177 com_err(program_name, 0, "%s",
2178 _("Can't support bigalloc feature without "
2179 "extents feature"));
2183 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
2184 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
2185 fprintf(stderr, "%s", _("The resize_inode and meta_bg "
2186 "features are not compatible.\n"
2187 "They can not be both enabled "
2188 "simultaneously.\n"));
2193 (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2194 fprintf(stderr, "%s", _("\nWarning: the bigalloc feature is "
2195 "still under development\n"
2196 "See https://ext4.wiki.kernel.org/"
2197 "index.php/Bigalloc for more information\n\n"));
2199 /* Since sparse_super is the default, we would only have a problem
2200 * here if it was explicitly disabled.
2202 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
2203 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
2204 com_err(program_name, 0, "%s",
2205 _("reserved online resize blocks not supported "
2206 "on non-sparse filesystem"));
2210 if (fs_param.s_blocks_per_group) {
2211 if (fs_param.s_blocks_per_group < 256 ||
2212 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
2213 com_err(program_name, 0, "%s",
2214 _("blocks per group count out of range"));
2220 * If the bigalloc feature is enabled, then the -g option will
2221 * specify the number of clusters per group.
2223 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
2224 fs_param.s_clusters_per_group = fs_param.s_blocks_per_group;
2225 fs_param.s_blocks_per_group = 0;
2228 if (inode_size == 0)
2229 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
2230 if (!flex_bg_size && (fs_param.s_feature_incompat &
2231 EXT4_FEATURE_INCOMPAT_FLEX_BG))
2232 flex_bg_size = get_uint_from_profile(fs_types,
2233 "flex_bg_size", 16);
2235 if (!(fs_param.s_feature_incompat &
2236 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
2237 com_err(program_name, 0, "%s",
2238 _("Flex_bg feature not enabled, so "
2239 "flex_bg size may not be specified"));
2242 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
2245 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
2246 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
2247 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
2248 inode_size & (inode_size - 1)) {
2249 com_err(program_name, 0,
2250 _("invalid inode size %d (min %d/max %d)"),
2251 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
2255 fs_param.s_inode_size = inode_size;
2258 /* Make sure number of inodes specified will fit in 32 bits */
2259 if (num_inodes == 0) {
2260 unsigned long long n;
2261 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
2262 if (n > MAX_32_NUM) {
2263 if (fs_param.s_feature_incompat &
2264 EXT4_FEATURE_INCOMPAT_64BIT)
2265 num_inodes = MAX_32_NUM;
2267 com_err(program_name, 0,
2268 _("too many inodes (%llu), raise "
2269 "inode ratio?"), n);
2273 } else if (num_inodes > MAX_32_NUM) {
2274 com_err(program_name, 0,
2275 _("too many inodes (%llu), specify < 2^32 inodes"),
2280 * Calculate number of inodes based on the inode ratio
2282 fs_param.s_inodes_count = num_inodes ? num_inodes :
2283 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
2285 if ((((unsigned long long)fs_param.s_inodes_count) *
2286 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
2287 ((ext2fs_blocks_count(&fs_param)) *
2288 EXT2_BLOCK_SIZE(&fs_param))) {
2289 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
2290 "(%u) too big for a\n\t"
2291 "filesystem with %llu blocks, "
2292 "specify higher inode_ratio (-i)\n\t"
2293 "or lower inode count (-N).\n"),
2294 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2295 fs_param.s_inodes_count,
2296 (unsigned long long) ext2fs_blocks_count(&fs_param));
2301 * Calculate number of blocks to reserve
2303 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2304 ext2fs_blocks_count(&fs_param) / 100.0);
2306 if (fs_param.s_feature_compat & EXT4_FEATURE_COMPAT_SPARSE_SUPER2) {
2307 if (num_backups >= 1)
2308 fs_param.s_backup_bgs[0] = 1;
2309 if (num_backups >= 2)
2310 fs_param.s_backup_bgs[1] = ~0;
2317 static int should_do_undo(const char *name)
2322 struct ext2_super_block super;
2323 io_manager manager = unix_io_manager;
2324 int csum_flag, force_undo;
2326 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2327 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2328 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2329 if (!force_undo && (!csum_flag || !lazy_itable_init))
2332 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2335 * We don't handle error cases instead we
2336 * declare that the file system doesn't exist
2337 * and let the rest of mke2fs take care of
2344 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2345 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2351 #if defined(WORDS_BIGENDIAN)
2352 s_magic = ext2fs_swab16(super.s_magic);
2354 s_magic = super.s_magic;
2357 if (s_magic == EXT2_SUPER_MAGIC)
2361 io_channel_close(channel);
2368 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2370 errcode_t retval = ENOMEM;
2371 char *tdb_dir = NULL, *tdb_file = NULL;
2372 char *dev_name, *tmp_name;
2373 int free_tdb_dir = 0;
2376 * Configuration via a conf file would be
2379 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2381 profile_get_string(profile, "defaults",
2382 "undo_dir", 0, "/var/lib/e2fsprogs",
2387 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2388 access(tdb_dir, W_OK)) {
2394 tmp_name = strdup(name);
2397 dev_name = basename(tmp_name);
2398 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
2403 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, dev_name);
2406 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
2411 set_undo_io_backing_manager(*io_ptr);
2412 *io_ptr = undo_io_manager;
2413 retval = set_undo_io_backup_file(tdb_file);
2416 printf(_("Overwriting existing filesystem; this can be undone "
2417 "using the command:\n"
2418 " e2undo %s %s\n\n"), tdb_file, name);
2429 com_err(program_name, retval, "%s",
2430 _("while trying to setup undo file\n"));
2434 static int mke2fs_discard_device(ext2_filsys fs)
2436 struct ext2fs_numeric_progress_struct progress;
2437 blk64_t blocks = ext2fs_blocks_count(fs->super);
2438 blk64_t count = DISCARD_STEP_MB;
2443 * Let's try if discard really works on the device, so
2444 * we do not print numeric progress resulting in failure
2447 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2450 cur = fs->blocksize;
2452 count *= (1024 * 1024);
2453 count /= fs->blocksize;
2455 ext2fs_numeric_progress_init(fs, &progress,
2456 _("Discarding device blocks: "),
2458 while (cur < blocks) {
2459 ext2fs_numeric_progress_update(fs, &progress, cur);
2461 if (cur + count > blocks)
2462 count = blocks - cur;
2464 retval = io_channel_discard(fs->io, cur, count);
2471 ext2fs_numeric_progress_close(fs, &progress,
2474 printf("%s\n",error_message(retval));
2476 ext2fs_numeric_progress_close(fs, &progress,
2482 static void fix_cluster_bg_counts(ext2_filsys fs)
2484 blk64_t block, num_blocks, last_block, next;
2485 blk64_t tot_free = 0;
2490 num_blocks = ext2fs_blocks_count(fs->super);
2491 last_block = ext2fs_group_last_block2(fs, group);
2492 block = fs->super->s_first_data_block;
2493 while (block < num_blocks) {
2494 retval = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
2495 block, last_block, &next);
2499 block = last_block + 1;
2503 retval = ext2fs_find_first_set_block_bitmap2(fs->block_map,
2504 block, last_block, &next);
2506 next = last_block + 1;
2507 grp_free += EXT2FS_NUM_B2C(fs, next - block);
2508 tot_free += next - block;
2511 if (block > last_block) {
2513 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2514 ext2fs_group_desc_csum_set(fs, group);
2517 last_block = ext2fs_group_last_block2(fs, group);
2520 ext2fs_free_blocks_count_set(fs->super, tot_free);
2523 static int create_quota_inodes(ext2_filsys fs)
2527 quota_init_context(&qctx, fs, -1);
2528 quota_compute_usage(qctx);
2529 quota_write_inode(qctx, quotatype);
2530 quota_release_context(&qctx);
2535 int main (int argc, char *argv[])
2537 errcode_t retval = 0;
2539 badblocks_list bb_list = 0;
2540 unsigned int journal_blocks = 0;
2541 unsigned int i, checkinterval;
2547 char opt_string[40];
2549 int itable_zeroed = 0;
2552 setlocale(LC_MESSAGES, "");
2553 setlocale(LC_CTYPE, "");
2554 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2555 textdomain(NLS_CAT_NAME);
2556 set_com_err_gettext(gettext);
2560 #ifdef CONFIG_TESTIO_DEBUG
2561 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2562 io_ptr = test_io_manager;
2563 test_io_backing_manager = unix_io_manager;
2566 io_ptr = unix_io_manager;
2568 if (should_do_undo(device_name)) {
2569 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2575 * Initialize the superblock....
2577 flags = EXT2_FLAG_EXCLUSIVE;
2579 flags |= EXT2_FLAG_DIRECT_IO;
2580 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2583 flags |= EXT2_FLAG_64BITS;
2585 * By default, we print how many inode tables or block groups
2586 * or whatever we've written so far. The quiet flag disables
2587 * this, along with a lot of other output.
2590 flags |= EXT2_FLAG_PRINT_PROGRESS;
2591 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2593 com_err(device_name, retval, "%s",
2594 _("while setting up superblock"));
2598 /* Calculate journal blocks */
2599 if (!journal_device && ((journal_size) ||
2600 (fs_param.s_feature_compat &
2601 EXT3_FEATURE_COMPAT_HAS_JOURNAL)))
2602 journal_blocks = figure_journal_size(journal_size, fs);
2604 /* Can't undo discard ... */
2605 if (!noaction && discard && dev_size && (io_ptr != undo_io_manager)) {
2606 retval = mke2fs_discard_device(fs);
2607 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2610 _("Discard succeeded and will return "
2611 "0s - skipping inode table wipe\n"));
2612 lazy_itable_init = 1;
2618 sprintf(opt_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2619 32768 : fs->blocksize * 8);
2620 io_channel_set_options(fs->io, opt_string);
2622 sprintf(opt_string, "offset=%llu", offset);
2623 io_channel_set_options(fs->io, opt_string);
2626 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2627 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2629 if ((fs_param.s_feature_incompat &
2630 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2631 (fs_param.s_feature_ro_compat &
2632 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2633 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2634 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2635 fs->super->s_kbytes_written = 1;
2638 * Wipe out the old on-disk superblock
2641 zap_sector(fs, 2, 6);
2644 * Parse or generate a UUID for the filesystem
2647 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2648 com_err(device_name, 0, "could not parse UUID: %s\n",
2653 uuid_generate(fs->super->s_uuid);
2656 * Initialize the directory index variables
2658 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2660 hash_alg = e2p_string2hash(hash_alg_str);
2662 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2664 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2667 * Periodic checks can be enabled/disabled via config file.
2668 * Note we override the kernel include file's idea of what the default
2669 * check interval (never) should be. It's a good idea to check at
2670 * least *occasionally*, specially since servers will never rarely get
2671 * to reboot, since Linux is so robust these days. :-)
2673 * 180 days (six months) seems like a good value.
2675 #ifdef EXT2_DFL_CHECKINTERVAL
2676 #undef EXT2_DFL_CHECKINTERVAL
2678 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2680 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2681 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2682 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2684 * Add "jitter" to the superblock's check interval so that we
2685 * don't check all the filesystems at the same time. We use a
2686 * kludgy hack of using the UUID to derive a random jitter value
2688 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2689 val += fs->super->s_uuid[i];
2690 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2692 fs->super->s_max_mnt_count = -1;
2695 * Override the creator OS, if applicable
2697 if (creator_os && !set_os(fs->super, creator_os)) {
2698 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2703 * For the Hurd, we will turn off filetype since it doesn't
2706 if (fs->super->s_creator_os == EXT2_OS_HURD)
2707 fs->super->s_feature_incompat &=
2708 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2711 * Set the volume label...
2714 memset(fs->super->s_volume_name, 0,
2715 sizeof(fs->super->s_volume_name));
2716 strncpy(fs->super->s_volume_name, volume_label,
2717 sizeof(fs->super->s_volume_name));
2721 * Set the last mount directory
2724 memset(fs->super->s_last_mounted, 0,
2725 sizeof(fs->super->s_last_mounted));
2726 strncpy(fs->super->s_last_mounted, mount_dir,
2727 sizeof(fs->super->s_last_mounted));
2730 if (!quiet || noaction)
2736 if (fs->super->s_feature_incompat &
2737 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2738 create_journal_dev(fs);
2739 exit(ext2fs_close_free(&fs) ? 1 : 0);
2742 if (bad_blocks_filename)
2743 read_bb_file(fs, &bb_list, bad_blocks_filename);
2745 test_disk(fs, &bb_list);
2746 handle_bad_blocks(fs, bb_list);
2748 fs->stride = fs_stride = fs->super->s_raid_stride;
2750 printf("%s", _("Allocating group tables: "));
2751 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) &&
2753 retval = packed_allocate_tables(fs);
2755 retval = ext2fs_allocate_tables(fs);
2757 com_err(program_name, retval, "%s",
2758 _("while trying to allocate filesystem tables"));
2762 printf("%s", _("done \n"));
2764 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2766 com_err(program_name, retval, "%s",
2767 _("\n\twhile converting subcluster bitmap"));
2772 fs->super->s_state |= EXT2_ERROR_FS;
2773 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2775 * The command "mke2fs -S" is used to recover
2776 * corrupted file systems, so do not mark any of the
2777 * inodes as unused; we want e2fsck to consider all
2778 * inodes as potentially containing recoverable data.
2780 if (fs->super->s_feature_ro_compat &
2781 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2782 for (i = 0; i < fs->group_desc_count; i++)
2783 ext2fs_bg_itable_unused_set(fs, i, 0);
2786 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2787 blk64_t rsv = 65536 / fs->blocksize;
2788 blk64_t blocks = ext2fs_blocks_count(fs->super);
2792 #ifdef ZAP_BOOTBLOCK
2793 zap_sector(fs, 0, 2);
2797 * Wipe out any old MD RAID (or other) metadata at the end
2798 * of the device. This will also verify that the device is
2799 * as large as we think. Be careful with very small devices.
2801 start = (blocks & ~(rsv - 1));
2805 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2809 com_err(program_name, retval,
2810 _("while zeroing block %llu at end of filesystem"),
2813 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2814 create_root_dir(fs);
2815 create_lost_and_found(fs);
2817 create_bad_block_inode(fs, bb_list);
2818 if (fs->super->s_feature_compat &
2819 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2820 retval = ext2fs_create_resize_inode(fs);
2822 com_err("ext2fs_create_resize_inode", retval,
2824 _("while reserving blocks for online resize"));
2830 if (journal_device) {
2833 if (!check_plausibility(journal_device, CHECK_BLOCK_DEV,
2835 proceed_question(proceed_delay);
2836 check_mount(journal_device, force, _("journal"));
2838 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2839 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2840 fs->blocksize, unix_io_manager, &jfs);
2842 com_err(program_name, retval,
2843 _("while trying to open journal device %s\n"),
2848 printf(_("Adding journal to device %s: "),
2852 retval = ext2fs_add_journal_device(fs, jfs);
2854 com_err (program_name, retval,
2855 _("\n\twhile trying to add journal to device %s"),
2860 printf("%s", _("done\n"));
2861 ext2fs_close_free(&jfs);
2862 free(journal_device);
2863 } else if ((journal_size) ||
2864 (fs_param.s_feature_compat &
2865 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2867 printf("%s", _("Skipping journal creation in super-only mode\n"));
2868 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2872 if (!journal_blocks) {
2873 fs->super->s_feature_compat &=
2874 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2878 printf(_("Creating journal (%u blocks): "),
2882 retval = ext2fs_add_journal_inode2(fs, journal_blocks,
2886 com_err(program_name, retval, "%s",
2887 _("\n\twhile trying to create journal"));
2891 printf("%s", _("done\n"));
2895 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2896 retval = ext2fs_mmp_init(fs);
2898 fprintf(stderr, "%s",
2899 _("\nError while enabling multiple "
2900 "mount protection feature."));
2904 printf(_("Multiple mount protection is enabled "
2905 "with update interval %d seconds.\n"),
2906 fs->super->s_mmp_update_interval);
2909 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2910 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2911 fix_cluster_bg_counts(fs);
2912 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2913 EXT4_FEATURE_RO_COMPAT_QUOTA))
2914 create_quota_inodes(fs);
2916 retval = mk_hugefiles(fs, device_name);
2918 com_err(program_name, retval, "while creating huge files");
2921 printf("%s", _("Writing superblocks and "
2922 "filesystem accounting information: "));
2923 checkinterval = fs->super->s_checkinterval;
2924 max_mnt_count = fs->super->s_max_mnt_count;
2925 retval = ext2fs_close_free(&fs);
2927 fprintf(stderr, "%s",
2928 _("\nWarning, had trouble writing out superblocks."));
2929 } else if (!quiet) {
2930 printf("%s", _("done\n\n"));
2931 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2932 print_check_message(max_mnt_count, checkinterval);
2935 remove_error_table(&et_ext2_error_table);
2936 remove_error_table(&et_prof_error_table);
2937 profile_release(profile);
2938 for (i=0; fs_types[i]; i++)