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>
49 #include <sys/ioctl.h>
50 #include <sys/types.h>
54 #include <blkid/blkid.h>
56 #include "ext2fs/ext2_fs.h"
57 #include "ext2fs/ext2fsP.h"
58 #include "et/com_err.h"
59 #include "uuid/uuid.h"
61 #include "ext2fs/ext2fs.h"
65 #include "../version.h"
66 #include "nls-enable.h"
67 #include "quota/mkquota.h"
69 #define STRIDE_LENGTH 8
71 #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
77 #define DISCARD_STEP_MB (2048)
79 extern int isatty(int);
80 extern FILE *fpopen(const char *cmd, const char *mode);
82 const char * program_name = "mke2fs";
83 const char * device_name /* = NULL */;
85 /* Command line options */
90 int discard = 1; /* attempt to discard device before fs creation */
97 char *bad_blocks_filename;
99 int quotatype = -1; /* Initialize both user and group quotas by default */
101 struct ext2_super_block fs_param;
102 char *fs_uuid = NULL;
106 char *journal_device;
107 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
112 int sys_page_size = 4096;
113 int linux_version_code = 0;
115 static void usage(void)
117 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
118 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
119 "[-J journal-options]\n"
120 "\t[-G flex-group-size] [-N number-of-inodes]\n"
121 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
122 "\t[-g blocks-per-group] [-L volume-label] "
123 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
124 "[-r fs-revision] [-E extended-option[,...]]\n"
125 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
126 "[-jnqvDFKSV] device [blocks-count]\n"),
131 static int int_log2(unsigned long long arg)
143 static int int_log10(unsigned long long arg)
152 static int parse_version_number(const char *s)
154 int major, minor, rev;
160 major = strtol(cp, &endptr, 10);
161 if (cp == endptr || *endptr != '.')
164 minor = strtol(cp, &endptr, 10);
165 if (cp == endptr || *endptr != '.')
168 rev = strtol(cp, &endptr, 10);
171 return ((((major * 256) + minor) * 256) + rev);
175 * Helper function for read_bb_file and test_disk
177 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
179 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
184 * Reads the bad blocks list from a file
186 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
187 const char *bad_blocks_file)
192 f = fopen(bad_blocks_file, "r");
194 com_err("read_bad_blocks_file", errno,
195 _("while trying to open %s"), bad_blocks_file);
198 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
201 com_err("ext2fs_read_bb_FILE", retval,
202 _("while reading in list of bad blocks from file"));
208 * Runs the badblocks program to test the disk
210 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
216 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
217 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
218 fs->device_name, ext2fs_blocks_count(fs->super)-1);
220 printf(_("Running command: %s\n"), buf);
223 com_err("popen", errno,
224 _("while trying to run '%s'"), buf);
227 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
230 com_err("ext2fs_read_bb_FILE", retval,
231 _("while processing list of bad blocks from program"));
236 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
240 unsigned must_be_good;
242 badblocks_iterate bb_iter;
252 * The primary superblock and group descriptors *must* be
253 * good; if not, abort.
255 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
256 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
257 if (ext2fs_badblocks_list_test(bb_list, i)) {
258 fprintf(stderr, _("Block %d in primary "
259 "superblock/group descriptor area bad.\n"), i);
260 fprintf(stderr, _("Blocks %u through %u must be good "
261 "in order to build a filesystem.\n"),
262 fs->super->s_first_data_block, must_be_good);
263 fputs(_("Aborting....\n"), stderr);
269 * See if any of the bad blocks are showing up in the backup
270 * superblocks and/or group descriptors. If so, issue a
271 * warning and adjust the block counts appropriately.
273 group_block = fs->super->s_first_data_block +
274 fs->super->s_blocks_per_group;
276 for (i = 1; i < fs->group_desc_count; i++) {
278 for (j=0; j < fs->desc_blocks+1; j++) {
279 if (ext2fs_badblocks_list_test(bb_list,
283 _("Warning: the backup superblock/group descriptors at block %u contain\n"
287 group = ext2fs_group_of_blk2(fs, group_block+j);
288 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
289 ext2fs_group_desc_csum_set(fs, group);
290 ext2fs_free_blocks_count_add(fs->super, 1);
293 group_block += fs->super->s_blocks_per_group;
297 * Mark all the bad blocks as used...
299 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
301 com_err("ext2fs_badblocks_list_iterate_begin", retval,
302 _("while marking bad blocks as used"));
305 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
306 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
307 ext2fs_badblocks_list_iterate_end(bb_iter);
310 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
316 struct ext2fs_numeric_progress_struct progress;
318 ext2fs_numeric_progress_init(fs, &progress,
319 _("Writing inode tables: "),
320 fs->group_desc_count);
322 for (i = 0; i < fs->group_desc_count; i++) {
323 ext2fs_numeric_progress_update(fs, &progress, i);
325 blk = ext2fs_inode_table_loc(fs, i);
326 num = fs->inode_blocks_per_group;
329 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
330 ext2fs_bg_itable_unused(fs, i)) *
331 EXT2_INODE_SIZE(fs->super),
332 EXT2_BLOCK_SIZE(fs->super));
333 if (!lazy_flag || itable_zeroed) {
334 /* The kernel doesn't need to zero the itable blocks */
335 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
336 ext2fs_group_desc_csum_set(fs, i);
338 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
340 fprintf(stderr, _("\nCould not write %d "
341 "blocks in inode table starting at %llu: %s\n"),
342 num, blk, error_message(retval));
346 if (sync_kludge == 1)
348 else if ((i % sync_kludge) == 0)
352 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
353 ext2fs_numeric_progress_close(fs, &progress,
357 static void create_root_dir(ext2_filsys fs)
360 struct ext2_inode inode;
363 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
365 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
369 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
371 com_err("ext2fs_read_inode", retval,
372 _("while reading root inode"));
377 ext2fs_set_i_uid_high(inode, uid >> 16);
381 ext2fs_set_i_gid_high(inode, gid >> 16);
383 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
385 com_err("ext2fs_write_inode", retval,
386 _("while setting root inode ownership"));
392 static void create_lost_and_found(ext2_filsys fs)
394 unsigned int lpf_size = 0;
397 const char *name = "lost+found";
401 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
403 com_err("ext2fs_mkdir", retval,
404 _("while creating /lost+found"));
408 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
410 com_err("ext2_lookup", retval,
411 _("while looking up /lost+found"));
415 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
416 /* Ensure that lost+found is at least 2 blocks, so we always
417 * test large empty blocks for big-block filesystems. */
418 if ((lpf_size += fs->blocksize) >= 16*1024 &&
419 lpf_size >= 2 * fs->blocksize)
421 retval = ext2fs_expand_dir(fs, ino);
423 com_err("ext2fs_expand_dir", retval,
424 _("while expanding /lost+found"));
430 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
434 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
435 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
436 retval = ext2fs_update_bb_inode(fs, bb_list);
438 com_err("ext2fs_update_bb_inode", retval,
439 _("while setting bad block inode"));
445 static void reserve_inodes(ext2_filsys fs)
449 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
450 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
451 ext2fs_mark_ib_dirty(fs);
454 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
455 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
456 #define BSD_LABEL_OFFSET 64
458 static void zap_sector(ext2_filsys fs, int sect, int nsect)
464 buf = malloc(512*nsect);
466 printf(_("Out of memory erasing sectors %d-%d\n"),
467 sect, sect + nsect - 1);
472 /* Check for a BSD disklabel, and don't erase it if so */
473 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
476 _("Warning: could not read block 0: %s\n"),
477 error_message(retval));
479 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
480 if ((*magic == BSD_DISKMAGIC) ||
481 (*magic == BSD_MAGICDISK))
486 memset(buf, 0, 512*nsect);
487 io_channel_set_blksize(fs->io, 512);
488 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
489 io_channel_set_blksize(fs->io, fs->blocksize);
492 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
493 sect, error_message(retval));
496 static void create_journal_dev(ext2_filsys fs)
498 struct ext2fs_numeric_progress_struct progress;
501 blk64_t blk, err_blk;
502 int c, count, err_count;
504 retval = ext2fs_create_journal_superblock(fs,
505 ext2fs_blocks_count(fs->super), 0, &buf);
507 com_err("create_journal_dev", retval,
508 _("while initializing journal superblock"));
512 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
513 goto write_superblock;
515 ext2fs_numeric_progress_init(fs, &progress,
516 _("Zeroing journal device: "),
517 ext2fs_blocks_count(fs->super));
519 count = ext2fs_blocks_count(fs->super);
525 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
527 com_err("create_journal_dev", retval,
528 _("while zeroing journal device "
529 "(block %llu, count %d)"),
535 ext2fs_numeric_progress_update(fs, &progress, blk);
537 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
539 ext2fs_numeric_progress_close(fs, &progress, NULL);
541 retval = io_channel_write_blk64(fs->io,
542 fs->super->s_first_data_block+1,
545 com_err("create_journal_dev", retval,
546 _("while writing journal superblock"));
551 static void show_stats(ext2_filsys fs)
553 struct ext2_super_block *s = fs->super;
560 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
561 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
562 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
564 memset(buf, 0, sizeof(buf));
565 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
566 printf(_("Filesystem label=%s\n"), buf);
567 os = e2p_os2string(fs->super->s_creator_os);
569 printf(_("OS type: %s\n"), os);
571 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
572 s->s_log_block_size);
573 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
574 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
575 printf(_("Cluster size=%u (log=%u)\n"),
576 fs->blocksize << fs->cluster_ratio_bits,
577 s->s_log_cluster_size);
579 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
580 s->s_log_cluster_size);
581 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
582 s->s_raid_stride, s->s_raid_stripe_width);
583 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
584 ext2fs_blocks_count(s));
585 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
586 ext2fs_r_blocks_count(s),
587 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
588 printf(_("First data block=%u\n"), s->s_first_data_block);
589 if (s->s_reserved_gdt_blocks)
590 printf(_("Maximum filesystem blocks=%lu\n"),
591 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
592 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
593 if (fs->group_desc_count > 1)
594 printf(_("%u block groups\n"), fs->group_desc_count);
596 printf(_("%u block group\n"), fs->group_desc_count);
597 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
598 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
599 printf(_("%u blocks per group, %u clusters per group\n"),
600 s->s_blocks_per_group, s->s_clusters_per_group);
602 printf(_("%u blocks per group, %u fragments per group\n"),
603 s->s_blocks_per_group, s->s_clusters_per_group);
604 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
606 if (fs->group_desc_count == 1) {
611 printf(_("Superblock backups stored on blocks: "));
612 group_block = s->s_first_data_block;
614 for (i = 1; i < fs->group_desc_count; i++) {
615 group_block += s->s_blocks_per_group;
616 if (!ext2fs_bg_has_super(fs, i))
620 need = int_log10(group_block) + 2;
621 if (need > col_left) {
626 printf("%llu", group_block);
632 * Set the S_CREATOR_OS field. Return true if OS is known,
635 static int set_os(struct ext2_super_block *sb, char *os)
638 sb->s_creator_os = atoi (os);
639 else if (strcasecmp(os, "linux") == 0)
640 sb->s_creator_os = EXT2_OS_LINUX;
641 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
642 sb->s_creator_os = EXT2_OS_HURD;
643 else if (strcasecmp(os, "freebsd") == 0)
644 sb->s_creator_os = EXT2_OS_FREEBSD;
645 else if (strcasecmp(os, "lites") == 0)
646 sb->s_creator_os = EXT2_OS_LITES;
652 #define PATH_SET "PATH=/sbin"
654 static void parse_extended_opts(struct ext2_super_block *param,
657 char *buf, *token, *next, *p, *arg, *badopt = 0;
665 _("Couldn't allocate memory to parse options!\n"));
669 for (token = buf; token && *token; token = next) {
670 p = strchr(token, ',');
676 arg = strchr(token, '=');
681 if (strcmp(token, "mmp_update_interval") == 0) {
687 param->s_mmp_update_interval = strtoul(arg, &p, 0);
690 _("Invalid mmp_update_interval: %s\n"),
695 } else if (strcmp(token, "stride") == 0) {
701 param->s_raid_stride = strtoul(arg, &p, 0);
704 _("Invalid stride parameter: %s\n"),
709 } else if (strcmp(token, "stripe-width") == 0 ||
710 strcmp(token, "stripe_width") == 0) {
716 param->s_raid_stripe_width = strtoul(arg, &p, 0);
719 _("Invalid stripe-width parameter: %s\n"),
724 } else if (!strcmp(token, "resize")) {
726 unsigned long bpg, rsv_groups;
727 unsigned long group_desc_count, desc_blocks;
728 unsigned int gdpb, blocksize;
737 resize = parse_num_blocks2(arg,
738 param->s_log_block_size);
742 _("Invalid resize parameter: %s\n"),
747 if (resize <= ext2fs_blocks_count(param)) {
749 _("The resize maximum must be greater "
750 "than the filesystem size.\n"));
755 blocksize = EXT2_BLOCK_SIZE(param);
756 bpg = param->s_blocks_per_group;
759 gdpb = EXT2_DESC_PER_BLOCK(param);
760 group_desc_count = (__u32) ext2fs_div64_ceil(
761 ext2fs_blocks_count(param), bpg);
762 desc_blocks = (group_desc_count +
764 rsv_groups = ext2fs_div64_ceil(resize, bpg);
765 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
767 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
768 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
771 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
773 _("On-line resizing not supported with revision 0 filesystems\n"));
777 param->s_feature_compat |=
778 EXT2_FEATURE_COMPAT_RESIZE_INODE;
780 param->s_reserved_gdt_blocks = rsv_gdb;
782 } else if (!strcmp(token, "test_fs")) {
783 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
784 } else if (!strcmp(token, "lazy_itable_init")) {
786 lazy_itable_init = strtoul(arg, &p, 0);
788 lazy_itable_init = 1;
789 } else if (!strcmp(token, "lazy_journal_init")) {
791 journal_flags |= strtoul(arg, &p, 0) ?
792 EXT2_MKJOURNAL_LAZYINIT : 0;
794 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
795 } else if (!strcmp(token, "discard")) {
797 } else if (!strcmp(token, "nodiscard")) {
799 } else if (!strcmp(token, "quotatype")) {
805 if (!strncmp(arg, "usr", 3)) {
807 } else if (!strncmp(arg, "grp", 3)) {
811 _("Invalid quotatype parameter: %s\n"),
822 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
823 "Extended options are separated by commas, "
824 "and may take an argument which\n"
825 "\tis set off by an equals ('=') sign.\n\n"
826 "Valid extended options are:\n"
827 "\tstride=<RAID per-disk data chunk in blocks>\n"
828 "\tstripe-width=<RAID stride * data disks in blocks>\n"
829 "\tresize=<resize maximum size in blocks>\n"
830 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
831 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
835 "\tquotatype=<usr OR grp>\n\n"),
836 badopt ? badopt : "");
840 if (param->s_raid_stride &&
841 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
842 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
843 "multiple of stride %u.\n\n"),
844 param->s_raid_stripe_width, param->s_raid_stride);
849 static __u32 ok_features[3] = {
851 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
852 EXT2_FEATURE_COMPAT_RESIZE_INODE |
853 EXT2_FEATURE_COMPAT_DIR_INDEX |
854 EXT2_FEATURE_COMPAT_EXT_ATTR,
856 EXT2_FEATURE_INCOMPAT_FILETYPE|
857 EXT3_FEATURE_INCOMPAT_EXTENTS|
858 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
859 EXT2_FEATURE_INCOMPAT_META_BG|
860 EXT4_FEATURE_INCOMPAT_FLEX_BG|
861 EXT4_FEATURE_INCOMPAT_MMP |
862 EXT4_FEATURE_INCOMPAT_64BIT,
864 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
865 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
866 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
867 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
868 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
869 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
870 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
872 EXT4_FEATURE_RO_COMPAT_QUOTA|
878 static void syntax_err_report(const char *filename, long err, int line_num)
881 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
882 filename, line_num, error_message(err));
886 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
888 static void edit_feature(const char *str, __u32 *compat_array)
893 if (e2p_edit_feature(str, compat_array, ok_features)) {
894 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
900 static void edit_mntopts(const char *str, __u32 *mntopts)
905 if (e2p_edit_mntopts(str, mntopts, ~0)) {
906 fprintf(stderr, _("Invalid mount option set: %s\n"),
918 static errcode_t init_list(struct str_list *sl)
922 sl->list = malloc((sl->max+1) * sizeof(char *));
929 static errcode_t push_string(struct str_list *sl, const char *str)
933 if (sl->num >= sl->max) {
935 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
940 sl->list[sl->num] = malloc(strlen(str)+1);
941 if (sl->list[sl->num] == 0)
943 strcpy(sl->list[sl->num], str);
945 sl->list[sl->num] = 0;
949 static void print_str_list(char **list)
953 for (cpp = list; *cpp; cpp++) {
954 printf("'%s'", *cpp);
962 * Return TRUE if the profile has the given subsection
964 static int profile_has_subsection(profile_t profile, const char *section,
965 const char *subsection)
968 const char *names[4];
973 names[1] = subsection;
976 if (profile_iterator_create(profile, names,
977 PROFILE_ITER_LIST_SECTION |
978 PROFILE_ITER_RELATIONS_ONLY, &state))
981 if ((profile_iterator(&state, &name, 0) == 0) && name) {
986 profile_iterator_free(&state);
990 static char **parse_fs_type(const char *fs_type,
991 const char *usage_types,
992 struct ext2_super_block *fs_param,
993 blk64_t fs_blocks_count,
996 const char *ext_type = 0;
998 char *profile_type = 0;
1000 const char *size_type;
1001 struct str_list list;
1002 unsigned long long meg;
1005 if (init_list(&list))
1008 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1009 !strcasecmp(creator_os, "hurd")))
1016 else if (!strcmp(program_name, "mke3fs"))
1018 else if (!strcmp(program_name, "mke4fs"))
1020 else if (progname) {
1021 ext_type = strrchr(progname, '/');
1025 ext_type = progname;
1027 if (!strncmp(ext_type, "mkfs.", 5)) {
1029 if (ext_type[0] == 0)
1036 profile_get_string(profile, "defaults", "fs_type", 0,
1037 "ext2", &profile_type);
1038 ext_type = profile_type;
1039 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1044 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1045 strcmp(ext_type, "ext2")) {
1046 printf(_("\nYour mke2fs.conf file does not define the "
1047 "%s filesystem type.\n"), ext_type);
1048 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1049 !strcmp(ext_type, "ext4dev")) {
1050 printf(_("You probably need to install an updated "
1051 "mke2fs.conf file.\n\n"));
1054 printf(_("Aborting...\n"));
1059 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1060 if (fs_blocks_count < 3 * meg)
1061 size_type = "floppy";
1062 else if (fs_blocks_count < 512 * meg)
1063 size_type = "small";
1064 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1065 size_type = "default";
1066 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1072 usage_types = size_type;
1074 parse_str = malloc(strlen(usage_types)+1);
1079 strcpy(parse_str, usage_types);
1082 push_string(&list, ext_type);
1085 t = strchr(cp, ',');
1090 if (profile_has_subsection(profile, "fs_types", cp))
1091 push_string(&list, cp);
1092 else if (strcmp(cp, "default") != 0)
1094 _("\nWarning: the fs_type %s is not "
1095 "defined in mke2fs.conf\n\n"),
1108 push_string(&list, "hurd");
1112 static char *get_string_from_profile(char **fs_types, const char *opt,
1113 const char *def_val)
1118 for (i=0; fs_types[i]; i++);
1119 for (i-=1; i >=0 ; i--) {
1120 profile_get_string(profile, "fs_types", fs_types[i],
1125 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1129 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1134 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1135 for (cpp = fs_types; *cpp; cpp++)
1136 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1140 static double get_double_from_profile(char **fs_types, const char *opt,
1146 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1147 for (cpp = fs_types; *cpp; cpp++)
1148 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1152 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1157 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1158 for (cpp = fs_types; *cpp; cpp++)
1159 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1163 extern const char *mke2fs_default_profile;
1164 static const char *default_files[] = { "<default>", 0 };
1166 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1168 * Sets the geometry of a device (stripe/stride), and returns the
1169 * device's alignment offset, if any, or a negative error.
1171 static int get_device_geometry(const char *file,
1172 struct ext2_super_block *fs_param,
1179 unsigned long min_io;
1180 unsigned long opt_io;
1181 struct stat statbuf;
1183 /* Nothing to do for a regular file */
1184 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1187 pr = blkid_new_probe_from_filename(file);
1191 tp = blkid_probe_get_topology(pr);
1195 min_io = blkid_topology_get_minimum_io_size(tp);
1196 opt_io = blkid_topology_get_optimal_io_size(tp);
1197 blocksize = EXT2_BLOCK_SIZE(fs_param);
1198 if ((min_io == 0) && (psector_size > blocksize))
1199 min_io = psector_size;
1200 if ((opt_io == 0) && min_io)
1202 if ((opt_io == 0) && (psector_size > blocksize))
1203 opt_io = psector_size;
1205 /* setting stripe/stride to blocksize is pointless */
1206 if (min_io > blocksize)
1207 fs_param->s_raid_stride = min_io / blocksize;
1208 if (opt_io > blocksize)
1209 fs_param->s_raid_stripe_width = opt_io / blocksize;
1211 rc = blkid_topology_get_alignment_offset(tp);
1213 blkid_free_probe(pr);
1218 static void PRS(int argc, char *argv[])
1221 int cluster_size = 0;
1224 int inode_ratio = 0;
1226 unsigned long flex_bg_size = 0;
1227 double reserved_ratio = -1.0;
1228 int lsector_size = 0, psector_size = 0;
1229 int show_version_only = 0;
1230 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1232 char * oldpath = getenv("PATH");
1233 char * extended_opts = 0;
1235 char * usage_types = 0;
1237 blk64_t fs_blocks_count = 0;
1242 int s_opt = -1, r_opt = -1;
1243 char *fs_features = 0;
1246 int pathlen = sizeof(PATH_SET) + 1;
1249 pathlen += strlen(oldpath);
1250 newpath = malloc(pathlen);
1252 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
1255 strcpy(newpath, PATH_SET);
1257 /* Update our PATH to include /sbin */
1259 strcat (newpath, ":");
1260 strcat (newpath, oldpath);
1264 tmp = getenv("MKE2FS_SYNC");
1266 sync_kludge = atoi(tmp);
1268 /* Determine the system page size if possible */
1270 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1271 #define _SC_PAGESIZE _SC_PAGE_SIZE
1274 sysval = sysconf(_SC_PAGESIZE);
1276 sys_page_size = sysval;
1277 #endif /* _SC_PAGESIZE */
1278 #endif /* HAVE_SYSCONF */
1280 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1282 profile_set_syntax_err_cb(syntax_err_report);
1283 retval = profile_init(config_fn, &profile);
1284 if (retval == ENOENT) {
1285 retval = profile_init(default_files, &profile);
1288 retval = profile_set_default(profile, mke2fs_default_profile);
1291 } else if (retval) {
1293 fprintf(stderr, _("Couldn't init profile successfully"
1294 " (error: %ld).\n"), retval);
1298 setbuf(stdout, NULL);
1299 setbuf(stderr, NULL);
1300 add_error_table(&et_ext2_error_table);
1301 add_error_table(&et_prof_error_table);
1302 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1303 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1310 linux_version_code = parse_version_number(ut.release);
1311 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1312 fs_param.s_rev_level = 0;
1315 if (argc && *argv) {
1316 program_name = get_progname(*argv);
1318 /* If called as mkfs.ext3, create a journal inode */
1319 if (!strcmp(program_name, "mkfs.ext3") ||
1320 !strcmp(program_name, "mke3fs"))
1324 while ((c = getopt (argc, argv,
1325 "b:cg:i:jl:m:no:qr:s:t:vC:DE:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1328 blocksize = strtol(optarg, &tmp, 0);
1329 b = (blocksize > 0) ? blocksize : -blocksize;
1330 if (b < EXT2_MIN_BLOCK_SIZE ||
1331 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1332 com_err(program_name, 0,
1333 _("invalid block size - %s"), optarg);
1336 if (blocksize > 4096)
1337 fprintf(stderr, _("Warning: blocksize %d not "
1338 "usable on most systems.\n"),
1341 fs_param.s_log_block_size =
1342 int_log2(blocksize >>
1343 EXT2_MIN_BLOCK_LOG_SIZE);
1345 case 'c': /* Check for bad blocks */
1349 cluster_size = strtoul(optarg, &tmp, 0);
1350 if (cluster_size < EXT2_MIN_CLUSTER_SIZE ||
1351 cluster_size > EXT2_MAX_CLUSTER_SIZE || *tmp) {
1352 com_err(program_name, 0,
1353 _("invalid cluster size - %s"),
1362 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1364 com_err(program_name, 0,
1365 _("Illegal number for blocks per group"));
1368 if ((fs_param.s_blocks_per_group % 8) != 0) {
1369 com_err(program_name, 0,
1370 _("blocks per group must be multiple of 8"));
1375 flex_bg_size = strtoul(optarg, &tmp, 0);
1377 com_err(program_name, 0,
1378 _("Illegal number for flex_bg size"));
1381 if (flex_bg_size < 1 ||
1382 (flex_bg_size & (flex_bg_size-1)) != 0) {
1383 com_err(program_name, 0,
1384 _("flex_bg size must be a power of 2"));
1389 inode_ratio = strtoul(optarg, &tmp, 0);
1390 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1391 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1393 com_err(program_name, 0,
1394 _("invalid inode ratio %s (min %d/max %d)"),
1395 optarg, EXT2_MIN_BLOCK_SIZE,
1396 EXT2_MAX_BLOCK_SIZE * 1024);
1401 parse_journal_opts(optarg);
1404 fprintf(stderr, _("Warning: -K option is deprecated and "
1405 "should not be used anymore. Use "
1406 "\'-E nodiscard\' extended option "
1415 bad_blocks_filename = malloc(strlen(optarg)+1);
1416 if (!bad_blocks_filename) {
1417 com_err(program_name, ENOMEM,
1418 _("in malloc for bad_blocks_filename"));
1421 strcpy(bad_blocks_filename, optarg);
1424 reserved_ratio = strtod(optarg, &tmp);
1425 if ( *tmp || reserved_ratio > 50 ||
1426 reserved_ratio < 0) {
1427 com_err(program_name, 0,
1428 _("invalid reserved blocks percent - %s"),
1437 creator_os = optarg;
1443 r_opt = strtoul(optarg, &tmp, 0);
1445 com_err(program_name, 0,
1446 _("bad revision level - %s"), optarg);
1449 fs_param.s_rev_level = r_opt;
1451 case 's': /* deprecated */
1452 s_opt = atoi(optarg);
1455 inode_size = strtoul(optarg, &tmp, 0);
1457 com_err(program_name, 0,
1458 _("invalid inode size - %s"), optarg);
1469 volume_label = optarg;
1475 num_inodes = strtoul(optarg, &tmp, 0);
1477 com_err(program_name, 0,
1478 _("bad num inodes - %s"), optarg);
1483 fs_features = optarg;
1487 extended_opts = optarg;
1494 com_err(program_name, 0,
1495 _("The -t option may only be used once"));
1498 fs_type = strdup(optarg);
1502 com_err(program_name, 0,
1503 _("The -T option may only be used once"));
1506 usage_types = strdup(optarg);
1512 /* Print version number and exit */
1513 show_version_only++;
1519 if ((optind == argc) && !show_version_only)
1521 device_name = argv[optind++];
1523 if (!quiet || show_version_only)
1524 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1527 if (show_version_only) {
1528 fprintf(stderr, _("\tUsing %s\n"),
1529 error_message(EXT2_ET_BASE));
1534 * If there's no blocksize specified and there is a journal
1535 * device, use it to figure out the blocksize
1537 if (blocksize <= 0 && journal_device) {
1541 #ifdef CONFIG_TESTIO_DEBUG
1542 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1543 io_ptr = test_io_manager;
1544 test_io_backing_manager = unix_io_manager;
1547 io_ptr = unix_io_manager;
1548 retval = ext2fs_open(journal_device,
1549 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1552 com_err(program_name, retval,
1553 _("while trying to open journal device %s\n"),
1557 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1558 com_err(program_name, 0,
1559 _("Journal dev blocksize (%d) smaller than "
1560 "minimum blocksize %d\n"), jfs->blocksize,
1564 blocksize = jfs->blocksize;
1565 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1566 fs_param.s_log_block_size =
1567 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1571 if (optind < argc) {
1572 fs_blocks_count = parse_num_blocks2(argv[optind++],
1573 fs_param.s_log_block_size);
1574 if (!fs_blocks_count) {
1575 com_err(program_name, 0,
1576 _("invalid blocks '%s' on device '%s'"),
1577 argv[optind - 1], device_name);
1585 check_plausibility(device_name);
1586 check_mount(device_name, force, _("filesystem"));
1588 /* Determine the size of the device (if possible) */
1589 if (noaction && fs_blocks_count) {
1590 dev_size = fs_blocks_count;
1593 retval = ext2fs_get_device_size2(device_name,
1594 EXT2_BLOCK_SIZE(&fs_param),
1597 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1598 com_err(program_name, retval,
1599 _("while trying to determine filesystem size"));
1602 if (!fs_blocks_count) {
1603 if (retval == EXT2_ET_UNIMPLEMENTED) {
1604 com_err(program_name, 0,
1605 _("Couldn't determine device size; you "
1606 "must specify\nthe size of the "
1610 if (dev_size == 0) {
1611 com_err(program_name, 0,
1612 _("Device size reported to be zero. "
1613 "Invalid partition specified, or\n\t"
1614 "partition table wasn't reread "
1615 "after running fdisk, due to\n\t"
1616 "a modified partition being busy "
1617 "and in use. You may need to reboot\n\t"
1618 "to re-read your partition table.\n"
1622 fs_blocks_count = dev_size;
1623 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1624 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1625 EXT2_BLOCK_SIZE(&fs_param))-1));
1627 } else if (!force && (fs_blocks_count > dev_size)) {
1628 com_err(program_name, 0,
1629 _("Filesystem larger than apparent device size."));
1634 profile_get_string(profile, "devices", device_name,
1635 "fs_type", 0, &fs_type);
1637 profile_get_string(profile, "devices", device_name,
1638 "usage_types", 0, &usage_types);
1641 * We have the file system (or device) size, so we can now
1642 * determine the appropriate file system types so the fs can
1643 * be appropriately configured.
1645 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1646 fs_blocks_count ? fs_blocks_count : dev_size,
1649 fprintf(stderr, _("Failed to parse fs types list\n"));
1653 /* Figure out what features should be enabled */
1656 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1657 tmp = get_string_from_profile(fs_types, "base_features",
1658 "sparse_super,filetype,resize_inode,dir_index");
1659 edit_feature(tmp, &fs_param.s_feature_compat);
1662 /* And which mount options as well */
1663 tmp = get_string_from_profile(fs_types, "default_mntopts",
1665 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1669 for (cpp = fs_types; *cpp; cpp++) {
1671 profile_get_string(profile, "fs_types", *cpp,
1672 "features", "", &tmp);
1674 edit_feature(tmp, &fs_param.s_feature_compat);
1678 tmp = get_string_from_profile(fs_types, "default_features",
1681 edit_feature(fs_features ? fs_features : tmp,
1682 &fs_param.s_feature_compat);
1687 * We now need to do a sanity check of fs_blocks_count for
1688 * 32-bit vs 64-bit block number support.
1690 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1691 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1693 fs_param.s_log_block_size = 2;
1695 if ((fs_blocks_count > MAX_32_NUM) &&
1696 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1697 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1698 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1699 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1701 if ((fs_blocks_count > MAX_32_NUM) &&
1702 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1703 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1704 "too big to be expressed\n\t"
1705 "in 32 bits using a blocksize of %d.\n"),
1706 program_name, fs_blocks_count, device_name,
1707 EXT2_BLOCK_SIZE(&fs_param));
1711 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1713 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1714 fs_types[0] = strdup("journal");
1719 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1720 print_str_list(fs_types);
1723 if (r_opt == EXT2_GOOD_OLD_REV &&
1724 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1725 fs_param.s_feature_ro_compat)) {
1726 fprintf(stderr, _("Filesystem features not supported "
1727 "with revision 0 filesystems\n"));
1732 if (r_opt == EXT2_GOOD_OLD_REV) {
1733 fprintf(stderr, _("Sparse superblocks not supported "
1734 "with revision 0 filesystems\n"));
1737 fs_param.s_feature_ro_compat |=
1738 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1739 } else if (s_opt == 0)
1740 fs_param.s_feature_ro_compat &=
1741 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1743 if (journal_size != 0) {
1744 if (r_opt == EXT2_GOOD_OLD_REV) {
1745 fprintf(stderr, _("Journals not supported "
1746 "with revision 0 filesystems\n"));
1749 fs_param.s_feature_compat |=
1750 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1753 /* Get reserved_ratio from profile if not specified on cmd line. */
1754 if (reserved_ratio < 0.0) {
1755 reserved_ratio = get_double_from_profile(
1756 fs_types, "reserved_ratio", 5.0);
1757 if (reserved_ratio > 50 || reserved_ratio < 0) {
1758 com_err(program_name, 0,
1759 _("invalid reserved blocks percent - %lf"),
1765 if (fs_param.s_feature_incompat &
1766 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1768 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1769 fs_param.s_feature_compat = 0;
1770 fs_param.s_feature_ro_compat = 0;
1773 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1774 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1775 fprintf(stderr, _("The resize_inode and meta_bg features "
1776 "are not compatible.\n"
1777 "They can not be both enabled "
1778 "simultaneously.\n"));
1782 /* Set first meta blockgroup via an environment variable */
1783 /* (this is mostly for debugging purposes) */
1784 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1785 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1786 fs_param.s_first_meta_bg = atoi(tmp);
1788 /* Get the hardware sector sizes, if available */
1789 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1791 com_err(program_name, retval,
1792 _("while trying to determine hardware sector size"));
1795 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1797 com_err(program_name, retval,
1798 _("while trying to determine physical sector size"));
1802 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1803 lsector_size = atoi(tmp);
1804 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1805 psector_size = atoi(tmp);
1807 /* Older kernels may not have physical/logical distinction */
1809 psector_size = lsector_size;
1811 if (blocksize <= 0) {
1812 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1814 if (use_bsize == -1) {
1815 use_bsize = sys_page_size;
1816 if ((linux_version_code < (2*65536 + 6*256)) &&
1820 if (lsector_size && use_bsize < lsector_size)
1821 use_bsize = lsector_size;
1822 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1823 use_bsize = -blocksize;
1824 blocksize = use_bsize;
1825 ext2fs_blocks_count_set(&fs_param,
1826 ext2fs_blocks_count(&fs_param) /
1827 (blocksize / 1024));
1829 if (blocksize < lsector_size) { /* Impossible */
1830 com_err(program_name, EINVAL,
1831 _("while setting blocksize; too small "
1834 } else if ((blocksize < psector_size) &&
1835 (psector_size <= sys_page_size)) { /* Suboptimal */
1836 fprintf(stderr, _("Warning: specified blocksize %d is "
1837 "less than device physical sectorsize %d\n"),
1838 blocksize, psector_size);
1842 fs_param.s_log_block_size =
1843 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1844 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1846 cluster_size = get_int_from_profile(fs_types,
1849 fs_param.s_log_cluster_size =
1850 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1852 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1854 if (inode_ratio == 0) {
1855 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1857 if (inode_ratio < blocksize)
1858 inode_ratio = blocksize;
1859 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1860 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
1863 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1864 retval = get_device_geometry(device_name, &fs_param, psector_size);
1867 _("warning: Unable to get device geometry for %s\n"),
1869 } else if (retval) {
1870 printf(_("%s alignment is offset by %lu bytes.\n"),
1871 device_name, retval);
1872 printf(_("This may result in very poor performance, "
1873 "(re)-partitioning suggested.\n"));
1877 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1879 /* This check should happen beyond the last assignment to blocksize */
1880 if (blocksize > sys_page_size) {
1882 com_err(program_name, 0,
1883 _("%d-byte blocks too big for system (max %d)"),
1884 blocksize, sys_page_size);
1887 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1888 "(max %d), forced to continue\n"),
1889 blocksize, sys_page_size);
1892 lazy_itable_init = 0;
1893 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1894 lazy_itable_init = 1;
1896 lazy_itable_init = get_bool_from_profile(fs_types,
1899 discard = get_bool_from_profile(fs_types, "discard" , discard);
1900 journal_flags |= get_bool_from_profile(fs_types,
1901 "lazy_journal_init", 0) ?
1902 EXT2_MKJOURNAL_LAZYINIT : 0;
1904 /* Get options from profile */
1905 for (cpp = fs_types; *cpp; cpp++) {
1907 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1909 parse_extended_opts(&fs_param, tmp);
1914 parse_extended_opts(&fs_param, extended_opts);
1916 /* Since sparse_super is the default, we would only have a problem
1917 * here if it was explicitly disabled.
1919 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1920 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1921 com_err(program_name, 0,
1922 _("reserved online resize blocks not supported "
1923 "on non-sparse filesystem"));
1927 if (fs_param.s_blocks_per_group) {
1928 if (fs_param.s_blocks_per_group < 256 ||
1929 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1930 com_err(program_name, 0,
1931 _("blocks per group count out of range"));
1936 if (inode_size == 0)
1937 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1938 if (!flex_bg_size && (fs_param.s_feature_incompat &
1939 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1940 flex_bg_size = get_int_from_profile(fs_types,
1941 "flex_bg_size", 16);
1943 if (!(fs_param.s_feature_incompat &
1944 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1945 com_err(program_name, 0,
1946 _("Flex_bg feature not enabled, so "
1947 "flex_bg size may not be specified"));
1950 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1953 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1954 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1955 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1956 inode_size & (inode_size - 1)) {
1957 com_err(program_name, 0,
1958 _("invalid inode size %d (min %d/max %d)"),
1959 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1963 fs_param.s_inode_size = inode_size;
1966 /* Make sure number of inodes specified will fit in 32 bits */
1967 if (num_inodes == 0) {
1968 unsigned long long n;
1969 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1970 if (n > MAX_32_NUM) {
1971 if (fs_param.s_feature_incompat &
1972 EXT4_FEATURE_INCOMPAT_64BIT)
1973 num_inodes = MAX_32_NUM;
1975 com_err(program_name, 0,
1976 _("too many inodes (%llu), raise "
1977 "inode ratio?"), n);
1981 } else if (num_inodes > MAX_32_NUM) {
1982 com_err(program_name, 0,
1983 _("too many inodes (%llu), specify < 2^32 inodes"),
1988 * Calculate number of inodes based on the inode ratio
1990 fs_param.s_inodes_count = num_inodes ? num_inodes :
1991 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1993 if ((((long long)fs_param.s_inodes_count) *
1994 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1995 ((ext2fs_blocks_count(&fs_param)) *
1996 EXT2_BLOCK_SIZE(&fs_param))) {
1997 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1998 "(%u) too big for a\n\t"
1999 "filesystem with %llu blocks, "
2000 "specify higher inode_ratio (-i)\n\t"
2001 "or lower inode count (-N).\n"),
2002 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
2003 fs_param.s_inodes_count,
2004 (unsigned long long) ext2fs_blocks_count(&fs_param));
2009 * Calculate number of blocks to reserve
2011 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2012 ext2fs_blocks_count(&fs_param) / 100.0);
2018 static int should_do_undo(const char *name)
2023 struct ext2_super_block super;
2024 io_manager manager = unix_io_manager;
2025 int csum_flag, force_undo;
2027 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2028 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2029 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2030 if (!force_undo && (!csum_flag || !lazy_itable_init))
2033 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2036 * We don't handle error cases instead we
2037 * declare that the file system doesn't exist
2038 * and let the rest of mke2fs take care of
2045 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2046 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2052 #if defined(WORDS_BIGENDIAN)
2053 s_magic = ext2fs_swab16(super.s_magic);
2055 s_magic = super.s_magic;
2058 if (s_magic == EXT2_SUPER_MAGIC)
2062 io_channel_close(channel);
2069 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2071 errcode_t retval = ENOMEM;
2072 char *tdb_dir = NULL, *tdb_file = NULL;
2073 char *device_name, *tmp_name;
2074 int free_tdb_dir = 0;
2077 * Configuration via a conf file would be
2080 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2082 profile_get_string(profile, "defaults",
2083 "undo_dir", 0, "/var/lib/e2fsprogs",
2088 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2089 access(tdb_dir, W_OK))
2092 tmp_name = strdup(name);
2095 device_name = basename(tmp_name);
2096 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
2101 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
2104 if (!access(tdb_file, F_OK)) {
2105 if (unlink(tdb_file) < 0) {
2111 set_undo_io_backing_manager(*io_ptr);
2112 *io_ptr = undo_io_manager;
2113 retval = set_undo_io_backup_file(tdb_file);
2116 printf(_("Overwriting existing filesystem; this can be undone "
2117 "using the command:\n"
2118 " e2undo %s %s\n\n"), tdb_file, name);
2129 com_err(program_name, retval,
2130 _("while trying to setup undo file\n"));
2134 static int mke2fs_discard_device(ext2_filsys fs)
2136 struct ext2fs_numeric_progress_struct progress;
2137 blk64_t blocks = ext2fs_blocks_count(fs->super);
2138 blk64_t count = DISCARD_STEP_MB;
2143 * Let's try if discard really works on the device, so
2144 * we do not print numeric progress resulting in failure
2147 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2150 cur = fs->blocksize;
2152 count *= (1024 * 1024);
2153 count /= fs->blocksize;
2155 ext2fs_numeric_progress_init(fs, &progress,
2156 _("Discarding device blocks: "),
2158 while (cur < blocks) {
2159 ext2fs_numeric_progress_update(fs, &progress, cur);
2161 if (cur + count > blocks)
2162 count = blocks - cur;
2164 retval = io_channel_discard(fs->io, cur, count);
2171 ext2fs_numeric_progress_close(fs, &progress,
2174 printf("%s\n",error_message(retval));
2176 ext2fs_numeric_progress_close(fs, &progress,
2182 static void fix_cluster_bg_counts(ext2_filsys fs)
2184 blk64_t cluster, num_clusters, tot_free;
2185 int grp_free, num_free, group, num;
2187 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2188 tot_free = num_free = num = group = grp_free = 0;
2189 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2190 cluster < num_clusters; cluster++) {
2191 if (!ext2fs_test_block_bitmap2(fs->block_map,
2192 EXT2FS_C2B(fs, cluster))) {
2197 if ((num == fs->super->s_clusters_per_group) ||
2198 (cluster == num_clusters-1)) {
2199 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2200 ext2fs_group_desc_csum_set(fs, group);
2206 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2209 static int create_quota_inodes(ext2_filsys fs)
2213 quota_init_context(&qctx, fs, -1);
2214 quota_compute_usage(qctx);
2215 quota_write_inode(qctx, quotatype);
2216 quota_release_context(&qctx);
2221 int main (int argc, char *argv[])
2223 errcode_t retval = 0;
2225 badblocks_list bb_list = 0;
2226 unsigned int journal_blocks;
2227 unsigned int i, checkinterval;
2233 char tdb_string[40];
2235 int itable_zeroed = 0;
2238 setlocale(LC_MESSAGES, "");
2239 setlocale(LC_CTYPE, "");
2240 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2241 textdomain(NLS_CAT_NAME);
2242 set_com_err_gettext(gettext);
2246 #ifdef CONFIG_TESTIO_DEBUG
2247 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2248 io_ptr = test_io_manager;
2249 test_io_backing_manager = unix_io_manager;
2252 io_ptr = unix_io_manager;
2254 if (should_do_undo(device_name)) {
2255 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2261 * Initialize the superblock....
2263 flags = EXT2_FLAG_EXCLUSIVE;
2265 flags |= EXT2_FLAG_DIRECT_IO;
2266 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2269 flags |= EXT2_FLAG_64BITS;
2271 * By default, we print how many inode tables or block groups
2272 * or whatever we've written so far. The quiet flag disables
2273 * this, along with a lot of other output.
2276 flags |= EXT2_FLAG_PRINT_PROGRESS;
2277 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2279 com_err(device_name, retval, _("while setting up superblock"));
2283 /* Can't undo discard ... */
2284 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2285 retval = mke2fs_discard_device(fs);
2286 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2288 printf(_("Discard succeeded and will return 0s "
2289 " - skipping inode table wipe\n"));
2290 lazy_itable_init = 1;
2295 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2296 32768 : fs->blocksize * 8);
2297 io_channel_set_options(fs->io, tdb_string);
2299 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2300 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2302 if ((fs_param.s_feature_incompat &
2303 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2304 (fs_param.s_feature_ro_compat &
2305 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2306 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2307 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2308 fs->super->s_kbytes_written = 1;
2311 * Wipe out the old on-disk superblock
2314 zap_sector(fs, 2, 6);
2317 * Parse or generate a UUID for the filesystem
2320 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2321 com_err(device_name, 0, "could not parse UUID: %s\n",
2326 uuid_generate(fs->super->s_uuid);
2329 * Initialize the directory index variables
2331 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2333 hash_alg = e2p_string2hash(hash_alg_str);
2335 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2337 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2340 * Periodic checks can be enabled/disabled via config file.
2341 * Note we override the kernel include file's idea of what the default
2342 * check interval (never) should be. It's a good idea to check at
2343 * least *occasionally*, specially since servers will never rarely get
2344 * to reboot, since Linux is so robust these days. :-)
2346 * 180 days (six months) seems like a good value.
2348 #ifdef EXT2_DFL_CHECKINTERVAL
2349 #undef EXT2_DFL_CHECKINTERVAL
2351 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2353 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2354 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2355 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2357 * Add "jitter" to the superblock's check interval so that we
2358 * don't check all the filesystems at the same time. We use a
2359 * kludgy hack of using the UUID to derive a random jitter value
2361 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2362 val += fs->super->s_uuid[i];
2363 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2365 fs->super->s_max_mnt_count = -1;
2368 * Override the creator OS, if applicable
2370 if (creator_os && !set_os(fs->super, creator_os)) {
2371 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2376 * For the Hurd, we will turn off filetype since it doesn't
2379 if (fs->super->s_creator_os == EXT2_OS_HURD)
2380 fs->super->s_feature_incompat &=
2381 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2384 * Set the volume label...
2387 memset(fs->super->s_volume_name, 0,
2388 sizeof(fs->super->s_volume_name));
2389 strncpy(fs->super->s_volume_name, volume_label,
2390 sizeof(fs->super->s_volume_name));
2394 * Set the last mount directory
2397 memset(fs->super->s_last_mounted, 0,
2398 sizeof(fs->super->s_last_mounted));
2399 strncpy(fs->super->s_last_mounted, mount_dir,
2400 sizeof(fs->super->s_last_mounted));
2403 if (!quiet || noaction)
2409 if (fs->super->s_feature_incompat &
2410 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2411 create_journal_dev(fs);
2412 exit(ext2fs_close(fs) ? 1 : 0);
2415 if (bad_blocks_filename)
2416 read_bb_file(fs, &bb_list, bad_blocks_filename);
2418 test_disk(fs, &bb_list);
2420 handle_bad_blocks(fs, bb_list);
2421 fs->stride = fs_stride = fs->super->s_raid_stride;
2423 printf(_("Allocating group tables: "));
2424 retval = ext2fs_allocate_tables(fs);
2426 com_err(program_name, retval,
2427 _("while trying to allocate filesystem tables"));
2431 printf(_("done \n"));
2433 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2435 com_err(program_name, retval,
2436 _("\n\twhile converting subcluster bitmap"));
2441 fs->super->s_state |= EXT2_ERROR_FS;
2442 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2444 * The command "mke2fs -S" is used to recover
2445 * corrupted file systems, so do not mark any of the
2446 * inodes as unused; we want e2fsck to consider all
2447 * inodes as potentially containing recoverable data.
2449 if (fs->super->s_feature_ro_compat &
2450 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2451 for (i = 0; i < fs->group_desc_count; i++)
2452 ext2fs_bg_itable_unused_set(fs, i, 0);
2455 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2456 blk64_t rsv = 65536 / fs->blocksize;
2457 blk64_t blocks = ext2fs_blocks_count(fs->super);
2461 #ifdef ZAP_BOOTBLOCK
2462 zap_sector(fs, 0, 2);
2466 * Wipe out any old MD RAID (or other) metadata at the end
2467 * of the device. This will also verify that the device is
2468 * as large as we think. Be careful with very small devices.
2470 start = (blocks & ~(rsv - 1));
2474 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2478 com_err(program_name, retval,
2479 _("while zeroing block %llu at end of filesystem"),
2482 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2483 create_root_dir(fs);
2484 create_lost_and_found(fs);
2486 create_bad_block_inode(fs, bb_list);
2487 if (fs->super->s_feature_compat &
2488 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2489 retval = ext2fs_create_resize_inode(fs);
2491 com_err("ext2fs_create_resize_inode", retval,
2492 _("while reserving blocks for online resize"));
2498 if (journal_device) {
2502 check_plausibility(journal_device);
2503 check_mount(journal_device, force, _("journal"));
2505 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2506 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2507 fs->blocksize, unix_io_manager, &jfs);
2509 com_err(program_name, retval,
2510 _("while trying to open journal device %s\n"),
2515 printf(_("Adding journal to device %s: "),
2519 retval = ext2fs_add_journal_device(fs, jfs);
2521 com_err (program_name, retval,
2522 _("\n\twhile trying to add journal to device %s"),
2527 printf(_("done\n"));
2529 free(journal_device);
2530 } else if ((journal_size) ||
2531 (fs_param.s_feature_compat &
2532 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2533 journal_blocks = figure_journal_size(journal_size, fs);
2536 printf(_("Skipping journal creation in super-only mode\n"));
2537 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2541 if (!journal_blocks) {
2542 fs->super->s_feature_compat &=
2543 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2547 printf(_("Creating journal (%u blocks): "),
2551 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2554 com_err (program_name, retval,
2555 _("\n\twhile trying to create journal"));
2559 printf(_("done\n"));
2563 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2564 retval = ext2fs_mmp_init(fs);
2566 fprintf(stderr, _("\nError while enabling multiple "
2567 "mount protection feature."));
2571 printf(_("Multiple mount protection is enabled "
2572 "with update interval %d seconds.\n"),
2573 fs->super->s_mmp_update_interval);
2576 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2577 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2578 fix_cluster_bg_counts(fs);
2579 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2580 EXT4_FEATURE_RO_COMPAT_QUOTA))
2581 create_quota_inodes(fs);
2584 printf(_("Writing superblocks and "
2585 "filesystem accounting information: "));
2586 checkinterval = fs->super->s_checkinterval;
2587 max_mnt_count = fs->super->s_max_mnt_count;
2588 retval = ext2fs_close(fs);
2591 _("\nWarning, had trouble writing out superblocks."));
2592 } else if (!quiet) {
2593 printf(_("done\n\n"));
2594 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2595 print_check_message(max_mnt_count, checkinterval);
2598 remove_error_table(&et_ext2_error_table);
2599 remove_error_table(&et_prof_error_table);
2600 profile_release(profile);
2601 for (i=0; fs_types[i]; i++)