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 */
96 char *bad_blocks_filename;
98 int quotatype = -1; /* Initialize both user and group quotas by default */
100 struct ext2_super_block fs_param;
101 char *fs_uuid = NULL;
105 char *journal_device;
106 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
111 int sys_page_size = 4096;
112 int linux_version_code = 0;
114 static void usage(void)
116 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
117 "[-C cluster-size]\n\t[-i bytes-per-inode] [-I inode-size] "
118 "[-J journal-options]\n"
119 "\t[-G flex-group-size] [-N number-of-inodes]\n"
120 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
121 "\t[-g blocks-per-group] [-L volume-label] "
122 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
123 "[-r fs-revision] [-E extended-option[,...]]\n"
124 "\t[-t fs-type] [-T usage-type ] [-U UUID] "
125 "[-jnqvFKSV] device [blocks-count]\n"),
130 static int int_log2(unsigned long long arg)
142 static int int_log10(unsigned long long arg)
151 static int parse_version_number(const char *s)
153 int major, minor, rev;
159 major = strtol(cp, &endptr, 10);
160 if (cp == endptr || *endptr != '.')
163 minor = strtol(cp, &endptr, 10);
164 if (cp == endptr || *endptr != '.')
167 rev = strtol(cp, &endptr, 10);
170 return ((((major * 256) + minor) * 256) + rev);
174 * Helper function for read_bb_file and test_disk
176 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
178 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
183 * Reads the bad blocks list from a file
185 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
186 const char *bad_blocks_file)
191 f = fopen(bad_blocks_file, "r");
193 com_err("read_bad_blocks_file", errno,
194 _("while trying to open %s"), bad_blocks_file);
197 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
200 com_err("ext2fs_read_bb_FILE", retval,
201 _("while reading in list of bad blocks from file"));
207 * Runs the badblocks program to test the disk
209 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
215 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
216 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
217 fs->device_name, ext2fs_blocks_count(fs->super)-1);
219 printf(_("Running command: %s\n"), buf);
222 com_err("popen", errno,
223 _("while trying to run '%s'"), buf);
226 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
229 com_err("ext2fs_read_bb_FILE", retval,
230 _("while processing list of bad blocks from program"));
235 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
239 unsigned must_be_good;
241 badblocks_iterate bb_iter;
251 * The primary superblock and group descriptors *must* be
252 * good; if not, abort.
254 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
255 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
256 if (ext2fs_badblocks_list_test(bb_list, i)) {
257 fprintf(stderr, _("Block %d in primary "
258 "superblock/group descriptor area bad.\n"), i);
259 fprintf(stderr, _("Blocks %u through %u must be good "
260 "in order to build a filesystem.\n"),
261 fs->super->s_first_data_block, must_be_good);
262 fputs(_("Aborting....\n"), stderr);
268 * See if any of the bad blocks are showing up in the backup
269 * superblocks and/or group descriptors. If so, issue a
270 * warning and adjust the block counts appropriately.
272 group_block = fs->super->s_first_data_block +
273 fs->super->s_blocks_per_group;
275 for (i = 1; i < fs->group_desc_count; i++) {
277 for (j=0; j < fs->desc_blocks+1; j++) {
278 if (ext2fs_badblocks_list_test(bb_list,
282 _("Warning: the backup superblock/group descriptors at block %u contain\n"
286 group = ext2fs_group_of_blk2(fs, group_block+j);
287 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
288 ext2fs_group_desc_csum_set(fs, group);
289 ext2fs_free_blocks_count_add(fs->super, 1);
292 group_block += fs->super->s_blocks_per_group;
296 * Mark all the bad blocks as used...
298 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
300 com_err("ext2fs_badblocks_list_iterate_begin", retval,
301 _("while marking bad blocks as used"));
304 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
305 ext2fs_mark_block_bitmap2(fs->block_map, EXT2FS_B2C(fs, blk));
306 ext2fs_badblocks_list_iterate_end(bb_iter);
309 static void write_inode_tables(ext2_filsys fs, int lazy_flag, int itable_zeroed)
315 struct ext2fs_numeric_progress_struct progress;
317 ext2fs_numeric_progress_init(fs, &progress,
318 _("Writing inode tables: "),
319 fs->group_desc_count);
321 for (i = 0; i < fs->group_desc_count; i++) {
322 ext2fs_numeric_progress_update(fs, &progress, i);
324 blk = ext2fs_inode_table_loc(fs, i);
325 num = fs->inode_blocks_per_group;
328 num = ext2fs_div_ceil((fs->super->s_inodes_per_group -
329 ext2fs_bg_itable_unused(fs, i)) *
330 EXT2_INODE_SIZE(fs->super),
331 EXT2_BLOCK_SIZE(fs->super));
332 if (!lazy_flag || itable_zeroed) {
333 /* The kernel doesn't need to zero the itable blocks */
334 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
335 ext2fs_group_desc_csum_set(fs, i);
337 retval = ext2fs_zero_blocks2(fs, blk, num, &blk, &num);
339 fprintf(stderr, _("\nCould not write %d "
340 "blocks in inode table starting at %llu: %s\n"),
341 num, blk, error_message(retval));
345 if (sync_kludge == 1)
347 else if ((i % sync_kludge) == 0)
351 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
352 ext2fs_numeric_progress_close(fs, &progress,
356 static void create_root_dir(ext2_filsys fs)
359 struct ext2_inode inode;
362 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
364 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
368 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
370 com_err("ext2fs_read_inode", retval,
371 _("while reading root inode"));
376 ext2fs_set_i_uid_high(inode, uid >> 16);
380 ext2fs_set_i_gid_high(inode, gid >> 16);
382 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
384 com_err("ext2fs_write_inode", retval,
385 _("while setting root inode ownership"));
391 static void create_lost_and_found(ext2_filsys fs)
393 unsigned int lpf_size = 0;
396 const char *name = "lost+found";
400 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
402 com_err("ext2fs_mkdir", retval,
403 _("while creating /lost+found"));
407 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
409 com_err("ext2_lookup", retval,
410 _("while looking up /lost+found"));
414 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
415 /* Ensure that lost+found is at least 2 blocks, so we always
416 * test large empty blocks for big-block filesystems. */
417 if ((lpf_size += fs->blocksize) >= 16*1024 &&
418 lpf_size >= 2 * fs->blocksize)
420 retval = ext2fs_expand_dir(fs, ino);
422 com_err("ext2fs_expand_dir", retval,
423 _("while expanding /lost+found"));
429 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
433 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
434 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
435 retval = ext2fs_update_bb_inode(fs, bb_list);
437 com_err("ext2fs_update_bb_inode", retval,
438 _("while setting bad block inode"));
444 static void reserve_inodes(ext2_filsys fs)
448 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
449 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
450 ext2fs_mark_ib_dirty(fs);
453 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
454 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
455 #define BSD_LABEL_OFFSET 64
457 static void zap_sector(ext2_filsys fs, int sect, int nsect)
463 buf = malloc(512*nsect);
465 printf(_("Out of memory erasing sectors %d-%d\n"),
466 sect, sect + nsect - 1);
471 /* Check for a BSD disklabel, and don't erase it if so */
472 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
475 _("Warning: could not read block 0: %s\n"),
476 error_message(retval));
478 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
479 if ((*magic == BSD_DISKMAGIC) ||
480 (*magic == BSD_MAGICDISK))
485 memset(buf, 0, 512*nsect);
486 io_channel_set_blksize(fs->io, 512);
487 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
488 io_channel_set_blksize(fs->io, fs->blocksize);
491 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
492 sect, error_message(retval));
495 static void create_journal_dev(ext2_filsys fs)
497 struct ext2fs_numeric_progress_struct progress;
500 blk64_t blk, err_blk;
501 int c, count, err_count;
503 retval = ext2fs_create_journal_superblock(fs,
504 ext2fs_blocks_count(fs->super), 0, &buf);
506 com_err("create_journal_dev", retval,
507 _("while initializing journal superblock"));
511 if (journal_flags & EXT2_MKJOURNAL_LAZYINIT)
512 goto write_superblock;
514 ext2fs_numeric_progress_init(fs, &progress,
515 _("Zeroing journal device: "),
516 ext2fs_blocks_count(fs->super));
518 count = ext2fs_blocks_count(fs->super);
524 retval = ext2fs_zero_blocks2(fs, blk, c, &err_blk, &err_count);
526 com_err("create_journal_dev", retval,
527 _("while zeroing journal device "
528 "(block %llu, count %d)"),
534 ext2fs_numeric_progress_update(fs, &progress, blk);
536 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
538 ext2fs_numeric_progress_close(fs, &progress, NULL);
540 retval = io_channel_write_blk64(fs->io,
541 fs->super->s_first_data_block+1,
544 com_err("create_journal_dev", retval,
545 _("while writing journal superblock"));
550 static void show_stats(ext2_filsys fs)
552 struct ext2_super_block *s = fs->super;
559 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
560 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
561 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
563 memset(buf, 0, sizeof(buf));
564 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
565 printf(_("Filesystem label=%s\n"), buf);
566 os = e2p_os2string(fs->super->s_creator_os);
568 printf(_("OS type: %s\n"), os);
570 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
571 s->s_log_block_size);
572 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
573 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
574 printf(_("Cluster size=%u (log=%u)\n"),
575 fs->blocksize << fs->cluster_ratio_bits,
576 s->s_log_cluster_size);
578 printf(_("Fragment size=%u (log=%u)\n"), EXT2_CLUSTER_SIZE(s),
579 s->s_log_cluster_size);
580 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
581 s->s_raid_stride, s->s_raid_stripe_width);
582 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
583 ext2fs_blocks_count(s));
584 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
585 ext2fs_r_blocks_count(s),
586 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
587 printf(_("First data block=%u\n"), s->s_first_data_block);
588 if (s->s_reserved_gdt_blocks)
589 printf(_("Maximum filesystem blocks=%lu\n"),
590 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
591 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
592 if (fs->group_desc_count > 1)
593 printf(_("%u block groups\n"), fs->group_desc_count);
595 printf(_("%u block group\n"), fs->group_desc_count);
596 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
597 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
598 printf(_("%u blocks per group, %u clusters per group\n"),
599 s->s_blocks_per_group, s->s_clusters_per_group);
601 printf(_("%u blocks per group, %u fragments per group\n"),
602 s->s_blocks_per_group, s->s_clusters_per_group);
603 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
605 if (fs->group_desc_count == 1) {
610 printf(_("Superblock backups stored on blocks: "));
611 group_block = s->s_first_data_block;
613 for (i = 1; i < fs->group_desc_count; i++) {
614 group_block += s->s_blocks_per_group;
615 if (!ext2fs_bg_has_super(fs, i))
619 need = int_log10(group_block) + 2;
620 if (need > col_left) {
625 printf("%llu", group_block);
631 * Set the S_CREATOR_OS field. Return true if OS is known,
634 static int set_os(struct ext2_super_block *sb, char *os)
637 sb->s_creator_os = atoi (os);
638 else if (strcasecmp(os, "linux") == 0)
639 sb->s_creator_os = EXT2_OS_LINUX;
640 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
641 sb->s_creator_os = EXT2_OS_HURD;
642 else if (strcasecmp(os, "freebsd") == 0)
643 sb->s_creator_os = EXT2_OS_FREEBSD;
644 else if (strcasecmp(os, "lites") == 0)
645 sb->s_creator_os = EXT2_OS_LITES;
651 #define PATH_SET "PATH=/sbin"
653 static void parse_extended_opts(struct ext2_super_block *param,
656 char *buf, *token, *next, *p, *arg, *badopt = 0;
664 _("Couldn't allocate memory to parse options!\n"));
668 for (token = buf; token && *token; token = next) {
669 p = strchr(token, ',');
675 arg = strchr(token, '=');
680 if (strcmp(token, "mmp_update_interval") == 0) {
686 param->s_mmp_update_interval = strtoul(arg, &p, 0);
689 _("Invalid mmp_update_interval: %s\n"),
694 } else if (strcmp(token, "stride") == 0) {
700 param->s_raid_stride = strtoul(arg, &p, 0);
703 _("Invalid stride parameter: %s\n"),
708 } else if (strcmp(token, "stripe-width") == 0 ||
709 strcmp(token, "stripe_width") == 0) {
715 param->s_raid_stripe_width = strtoul(arg, &p, 0);
718 _("Invalid stripe-width parameter: %s\n"),
723 } else if (!strcmp(token, "resize")) {
725 unsigned long bpg, rsv_groups;
726 unsigned long group_desc_count, desc_blocks;
727 unsigned int gdpb, blocksize;
736 resize = parse_num_blocks2(arg,
737 param->s_log_block_size);
741 _("Invalid resize parameter: %s\n"),
746 if (resize <= ext2fs_blocks_count(param)) {
748 _("The resize maximum must be greater "
749 "than the filesystem size.\n"));
754 blocksize = EXT2_BLOCK_SIZE(param);
755 bpg = param->s_blocks_per_group;
758 gdpb = EXT2_DESC_PER_BLOCK(param);
759 group_desc_count = (__u32) ext2fs_div64_ceil(
760 ext2fs_blocks_count(param), bpg);
761 desc_blocks = (group_desc_count +
763 rsv_groups = ext2fs_div64_ceil(resize, bpg);
764 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
766 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
767 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
770 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
772 _("On-line resizing not supported with revision 0 filesystems\n"));
776 param->s_feature_compat |=
777 EXT2_FEATURE_COMPAT_RESIZE_INODE;
779 param->s_reserved_gdt_blocks = rsv_gdb;
781 } else if (!strcmp(token, "test_fs")) {
782 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
783 } else if (!strcmp(token, "lazy_itable_init")) {
785 lazy_itable_init = strtoul(arg, &p, 0);
787 lazy_itable_init = 1;
788 } else if (!strcmp(token, "lazy_journal_init")) {
790 journal_flags |= strtoul(arg, &p, 0) ?
791 EXT2_MKJOURNAL_LAZYINIT : 0;
793 journal_flags |= EXT2_MKJOURNAL_LAZYINIT;
794 } else if (!strcmp(token, "discard")) {
796 } else if (!strcmp(token, "nodiscard")) {
798 } else if (!strcmp(token, "quotatype")) {
804 if (!strncmp(arg, "usr", 3)) {
806 } else if (!strncmp(arg, "grp", 3)) {
810 _("Invalid quotatype parameter: %s\n"),
821 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
822 "Extended options are separated by commas, "
823 "and may take an argument which\n"
824 "\tis set off by an equals ('=') sign.\n\n"
825 "Valid extended options are:\n"
826 "\tstride=<RAID per-disk data chunk in blocks>\n"
827 "\tstripe-width=<RAID stride * data disks in blocks>\n"
828 "\tresize=<resize maximum size in blocks>\n"
829 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
830 "\tlazy_journal_init=<0 to disable, 1 to enable>\n"
834 "\tquotatype=<usr OR grp>\n\n"),
835 badopt ? badopt : "");
839 if (param->s_raid_stride &&
840 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
841 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
842 "multiple of stride %u.\n\n"),
843 param->s_raid_stripe_width, param->s_raid_stride);
848 static __u32 ok_features[3] = {
850 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
851 EXT2_FEATURE_COMPAT_RESIZE_INODE |
852 EXT2_FEATURE_COMPAT_DIR_INDEX |
853 EXT2_FEATURE_COMPAT_EXT_ATTR,
855 EXT2_FEATURE_INCOMPAT_FILETYPE|
856 EXT3_FEATURE_INCOMPAT_EXTENTS|
857 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
858 EXT2_FEATURE_INCOMPAT_META_BG|
859 EXT4_FEATURE_INCOMPAT_FLEX_BG|
860 EXT4_FEATURE_INCOMPAT_MMP |
861 EXT4_FEATURE_INCOMPAT_64BIT,
863 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
864 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
865 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
866 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
867 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
868 EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
869 EXT4_FEATURE_RO_COMPAT_BIGALLOC|
871 EXT4_FEATURE_RO_COMPAT_QUOTA|
877 static void syntax_err_report(const char *filename, long err, int line_num)
880 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
881 filename, line_num, error_message(err));
885 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
887 static void edit_feature(const char *str, __u32 *compat_array)
892 if (e2p_edit_feature(str, compat_array, ok_features)) {
893 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
899 static void edit_mntopts(const char *str, __u32 *mntopts)
904 if (e2p_edit_mntopts(str, mntopts, ~0)) {
905 fprintf(stderr, _("Invalid mount option set: %s\n"),
917 static errcode_t init_list(struct str_list *sl)
921 sl->list = malloc((sl->max+1) * sizeof(char *));
928 static errcode_t push_string(struct str_list *sl, const char *str)
932 if (sl->num >= sl->max) {
934 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
939 sl->list[sl->num] = malloc(strlen(str)+1);
940 if (sl->list[sl->num] == 0)
942 strcpy(sl->list[sl->num], str);
944 sl->list[sl->num] = 0;
948 static void print_str_list(char **list)
952 for (cpp = list; *cpp; cpp++) {
953 printf("'%s'", *cpp);
961 * Return TRUE if the profile has the given subsection
963 static int profile_has_subsection(profile_t profile, const char *section,
964 const char *subsection)
967 const char *names[4];
972 names[1] = subsection;
975 if (profile_iterator_create(profile, names,
976 PROFILE_ITER_LIST_SECTION |
977 PROFILE_ITER_RELATIONS_ONLY, &state))
980 if ((profile_iterator(&state, &name, 0) == 0) && name) {
985 profile_iterator_free(&state);
989 static char **parse_fs_type(const char *fs_type,
990 const char *usage_types,
991 struct ext2_super_block *fs_param,
992 blk64_t fs_blocks_count,
995 const char *ext_type = 0;
997 char *profile_type = 0;
999 const char *size_type;
1000 struct str_list list;
1001 unsigned long long meg;
1004 if (init_list(&list))
1007 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
1008 !strcasecmp(creator_os, "hurd")))
1015 else if (!strcmp(program_name, "mke3fs"))
1017 else if (!strcmp(program_name, "mke4fs"))
1019 else if (progname) {
1020 ext_type = strrchr(progname, '/');
1024 ext_type = progname;
1026 if (!strncmp(ext_type, "mkfs.", 5)) {
1028 if (ext_type[0] == 0)
1035 profile_get_string(profile, "defaults", "fs_type", 0,
1036 "ext2", &profile_type);
1037 ext_type = profile_type;
1038 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1043 if (!profile_has_subsection(profile, "fs_types", ext_type) &&
1044 strcmp(ext_type, "ext2")) {
1045 printf(_("\nYour mke2fs.conf file does not define the "
1046 "%s filesystem type.\n"), ext_type);
1047 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
1048 !strcmp(ext_type, "ext4dev")) {
1049 printf(_("You probably need to install an updated "
1050 "mke2fs.conf file.\n\n"));
1053 printf(_("Aborting...\n"));
1058 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1059 if (fs_blocks_count < 3 * meg)
1060 size_type = "floppy";
1061 else if (fs_blocks_count < 512 * meg)
1062 size_type = "small";
1063 else if (fs_blocks_count < 4 * 1024 * 1024 * meg)
1064 size_type = "default";
1065 else if (fs_blocks_count < 16 * 1024 * 1024 * meg)
1071 usage_types = size_type;
1073 parse_str = malloc(strlen(usage_types)+1);
1078 strcpy(parse_str, usage_types);
1081 push_string(&list, ext_type);
1084 t = strchr(cp, ',');
1089 if (profile_has_subsection(profile, "fs_types", cp))
1090 push_string(&list, cp);
1091 else if (strcmp(cp, "default") != 0)
1093 _("\nWarning: the fs_type %s is not "
1094 "defined in mke2fs.conf\n\n"),
1107 push_string(&list, "hurd");
1111 static char *get_string_from_profile(char **fs_types, const char *opt,
1112 const char *def_val)
1117 for (i=0; fs_types[i]; i++);
1118 for (i-=1; i >=0 ; i--) {
1119 profile_get_string(profile, "fs_types", fs_types[i],
1124 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1128 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1133 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1134 for (cpp = fs_types; *cpp; cpp++)
1135 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1139 static double get_double_from_profile(char **fs_types, const char *opt,
1145 profile_get_double(profile, "defaults", opt, 0, def_val, &ret);
1146 for (cpp = fs_types; *cpp; cpp++)
1147 profile_get_double(profile, "fs_types", *cpp, opt, ret, &ret);
1151 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1156 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1157 for (cpp = fs_types; *cpp; cpp++)
1158 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1162 extern const char *mke2fs_default_profile;
1163 static const char *default_files[] = { "<default>", 0 };
1165 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1167 * Sets the geometry of a device (stripe/stride), and returns the
1168 * device's alignment offset, if any, or a negative error.
1170 static int get_device_geometry(const char *file,
1171 struct ext2_super_block *fs_param,
1178 unsigned long min_io;
1179 unsigned long opt_io;
1180 struct stat statbuf;
1182 /* Nothing to do for a regular file */
1183 if (!stat(file, &statbuf) && S_ISREG(statbuf.st_mode))
1186 pr = blkid_new_probe_from_filename(file);
1190 tp = blkid_probe_get_topology(pr);
1194 min_io = blkid_topology_get_minimum_io_size(tp);
1195 opt_io = blkid_topology_get_optimal_io_size(tp);
1196 blocksize = EXT2_BLOCK_SIZE(fs_param);
1197 if ((min_io == 0) && (psector_size > blocksize))
1198 min_io = psector_size;
1199 if ((opt_io == 0) && min_io)
1201 if ((opt_io == 0) && (psector_size > blocksize))
1202 opt_io = psector_size;
1204 /* setting stripe/stride to blocksize is pointless */
1205 if (min_io > blocksize)
1206 fs_param->s_raid_stride = min_io / blocksize;
1207 if (opt_io > blocksize)
1208 fs_param->s_raid_stripe_width = opt_io / blocksize;
1210 rc = blkid_topology_get_alignment_offset(tp);
1212 blkid_free_probe(pr);
1217 static void PRS(int argc, char *argv[])
1220 int cluster_size = 0;
1223 int inode_ratio = 0;
1225 unsigned long flex_bg_size = 0;
1226 double reserved_ratio = -1.0;
1227 int lsector_size = 0, psector_size = 0;
1228 int show_version_only = 0;
1229 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1231 char * oldpath = getenv("PATH");
1232 char * extended_opts = 0;
1234 char * usage_types = 0;
1236 blk64_t fs_blocks_count = 0;
1241 int s_opt = -1, r_opt = -1;
1242 char *fs_features = 0;
1245 int pathlen = sizeof(PATH_SET) + 1;
1248 pathlen += strlen(oldpath);
1249 newpath = malloc(pathlen);
1251 fprintf(stderr, _("Couldn't allocate memory for new PATH.\n"));
1254 strcpy(newpath, PATH_SET);
1256 /* Update our PATH to include /sbin */
1258 strcat (newpath, ":");
1259 strcat (newpath, oldpath);
1263 tmp = getenv("MKE2FS_SYNC");
1265 sync_kludge = atoi(tmp);
1267 /* Determine the system page size if possible */
1269 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1270 #define _SC_PAGESIZE _SC_PAGE_SIZE
1273 sysval = sysconf(_SC_PAGESIZE);
1275 sys_page_size = sysval;
1276 #endif /* _SC_PAGESIZE */
1277 #endif /* HAVE_SYSCONF */
1279 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1281 profile_set_syntax_err_cb(syntax_err_report);
1282 retval = profile_init(config_fn, &profile);
1283 if (retval == ENOENT) {
1284 retval = profile_init(default_files, &profile);
1287 retval = profile_set_default(profile, mke2fs_default_profile);
1290 } else if (retval) {
1292 fprintf(stderr, _("Couldn't init profile successfully"
1293 " (error: %ld).\n"), retval);
1297 setbuf(stdout, NULL);
1298 setbuf(stderr, NULL);
1299 add_error_table(&et_ext2_error_table);
1300 add_error_table(&et_prof_error_table);
1301 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1302 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1309 linux_version_code = parse_version_number(ut.release);
1310 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1311 fs_param.s_rev_level = 0;
1314 if (argc && *argv) {
1315 program_name = get_progname(*argv);
1317 /* If called as mkfs.ext3, create a journal inode */
1318 if (!strcmp(program_name, "mkfs.ext3") ||
1319 !strcmp(program_name, "mke3fs"))
1323 while ((c = getopt (argc, argv,
1324 "b:cg:i:jl:m:no:qr:s:t:vC:E:FG:I:J:KL:M:N:O:R:ST:U:V")) != EOF) {
1327 blocksize = strtol(optarg, &tmp, 0);
1328 b = (blocksize > 0) ? blocksize : -blocksize;
1329 if (b < EXT2_MIN_BLOCK_SIZE ||
1330 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1331 com_err(program_name, 0,
1332 _("invalid block size - %s"), optarg);
1335 if (blocksize > 4096)
1336 fprintf(stderr, _("Warning: blocksize %d not "
1337 "usable on most systems.\n"),
1340 fs_param.s_log_block_size =
1341 int_log2(blocksize >>
1342 EXT2_MIN_BLOCK_LOG_SIZE);
1344 case 'c': /* Check for bad blocks */
1348 cluster_size = strtoul(optarg, &tmp, 0);
1349 if (cluster_size < EXT2_MIN_CLUSTER_SIZE ||
1350 cluster_size > EXT2_MAX_CLUSTER_SIZE || *tmp) {
1351 com_err(program_name, 0,
1352 _("invalid cluster size - %s"),
1358 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1360 com_err(program_name, 0,
1361 _("Illegal number for blocks per group"));
1364 if ((fs_param.s_blocks_per_group % 8) != 0) {
1365 com_err(program_name, 0,
1366 _("blocks per group must be multiple of 8"));
1371 flex_bg_size = strtoul(optarg, &tmp, 0);
1373 com_err(program_name, 0,
1374 _("Illegal number for flex_bg size"));
1377 if (flex_bg_size < 1 ||
1378 (flex_bg_size & (flex_bg_size-1)) != 0) {
1379 com_err(program_name, 0,
1380 _("flex_bg size must be a power of 2"));
1385 inode_ratio = strtoul(optarg, &tmp, 0);
1386 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1387 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1389 com_err(program_name, 0,
1390 _("invalid inode ratio %s (min %d/max %d)"),
1391 optarg, EXT2_MIN_BLOCK_SIZE,
1392 EXT2_MAX_BLOCK_SIZE * 1024);
1397 parse_journal_opts(optarg);
1400 fprintf(stderr, _("Warning: -K option is deprecated and "
1401 "should not be used anymore. Use "
1402 "\'-E nodiscard\' extended option "
1411 bad_blocks_filename = malloc(strlen(optarg)+1);
1412 if (!bad_blocks_filename) {
1413 com_err(program_name, ENOMEM,
1414 _("in malloc for bad_blocks_filename"));
1417 strcpy(bad_blocks_filename, optarg);
1420 reserved_ratio = strtod(optarg, &tmp);
1421 if ( *tmp || reserved_ratio > 50 ||
1422 reserved_ratio < 0) {
1423 com_err(program_name, 0,
1424 _("invalid reserved blocks percent - %s"),
1433 creator_os = optarg;
1439 r_opt = strtoul(optarg, &tmp, 0);
1441 com_err(program_name, 0,
1442 _("bad revision level - %s"), optarg);
1445 fs_param.s_rev_level = r_opt;
1447 case 's': /* deprecated */
1448 s_opt = atoi(optarg);
1451 inode_size = strtoul(optarg, &tmp, 0);
1453 com_err(program_name, 0,
1454 _("invalid inode size - %s"), optarg);
1465 volume_label = optarg;
1471 num_inodes = strtoul(optarg, &tmp, 0);
1473 com_err(program_name, 0,
1474 _("bad num inodes - %s"), optarg);
1479 fs_features = optarg;
1483 extended_opts = optarg;
1490 com_err(program_name, 0,
1491 _("The -t option may only be used once"));
1494 fs_type = strdup(optarg);
1498 com_err(program_name, 0,
1499 _("The -T option may only be used once"));
1502 usage_types = strdup(optarg);
1508 /* Print version number and exit */
1509 show_version_only++;
1515 if ((optind == argc) && !show_version_only)
1517 device_name = argv[optind++];
1519 if (!quiet || show_version_only)
1520 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1523 if (show_version_only) {
1524 fprintf(stderr, _("\tUsing %s\n"),
1525 error_message(EXT2_ET_BASE));
1530 * If there's no blocksize specified and there is a journal
1531 * device, use it to figure out the blocksize
1533 if (blocksize <= 0 && journal_device) {
1537 #ifdef CONFIG_TESTIO_DEBUG
1538 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1539 io_ptr = test_io_manager;
1540 test_io_backing_manager = unix_io_manager;
1543 io_ptr = unix_io_manager;
1544 retval = ext2fs_open(journal_device,
1545 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1548 com_err(program_name, retval,
1549 _("while trying to open journal device %s\n"),
1553 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1554 com_err(program_name, 0,
1555 _("Journal dev blocksize (%d) smaller than "
1556 "minimum blocksize %d\n"), jfs->blocksize,
1560 blocksize = jfs->blocksize;
1561 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1562 fs_param.s_log_block_size =
1563 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1567 if (optind < argc) {
1568 fs_blocks_count = parse_num_blocks2(argv[optind++],
1569 fs_param.s_log_block_size);
1570 if (!fs_blocks_count) {
1571 com_err(program_name, 0,
1572 _("invalid blocks '%s' on device '%s'"),
1573 argv[optind - 1], device_name);
1581 check_plausibility(device_name);
1582 check_mount(device_name, force, _("filesystem"));
1584 /* Determine the size of the device (if possible) */
1585 if (noaction && fs_blocks_count) {
1586 dev_size = fs_blocks_count;
1589 retval = ext2fs_get_device_size2(device_name,
1590 EXT2_BLOCK_SIZE(&fs_param),
1593 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1594 com_err(program_name, retval,
1595 _("while trying to determine filesystem size"));
1598 if (!fs_blocks_count) {
1599 if (retval == EXT2_ET_UNIMPLEMENTED) {
1600 com_err(program_name, 0,
1601 _("Couldn't determine device size; you "
1602 "must specify\nthe size of the "
1606 if (dev_size == 0) {
1607 com_err(program_name, 0,
1608 _("Device size reported to be zero. "
1609 "Invalid partition specified, or\n\t"
1610 "partition table wasn't reread "
1611 "after running fdisk, due to\n\t"
1612 "a modified partition being busy "
1613 "and in use. You may need to reboot\n\t"
1614 "to re-read your partition table.\n"
1618 fs_blocks_count = dev_size;
1619 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1620 fs_blocks_count &= ~((blk64_t) ((sys_page_size /
1621 EXT2_BLOCK_SIZE(&fs_param))-1));
1623 } else if (!force && (fs_blocks_count > dev_size)) {
1624 com_err(program_name, 0,
1625 _("Filesystem larger than apparent device size."));
1630 profile_get_string(profile, "devices", device_name,
1631 "fs_type", 0, &fs_type);
1633 profile_get_string(profile, "devices", device_name,
1634 "usage_types", 0, &usage_types);
1637 * We have the file system (or device) size, so we can now
1638 * determine the appropriate file system types so the fs can
1639 * be appropriately configured.
1641 fs_types = parse_fs_type(fs_type, usage_types, &fs_param,
1642 fs_blocks_count ? fs_blocks_count : dev_size,
1645 fprintf(stderr, _("Failed to parse fs types list\n"));
1649 /* Figure out what features should be enabled */
1652 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1653 tmp = get_string_from_profile(fs_types, "base_features",
1654 "sparse_super,filetype,resize_inode,dir_index");
1655 edit_feature(tmp, &fs_param.s_feature_compat);
1658 /* And which mount options as well */
1659 tmp = get_string_from_profile(fs_types, "default_mntopts",
1661 edit_mntopts(tmp, &fs_param.s_default_mount_opts);
1665 for (cpp = fs_types; *cpp; cpp++) {
1667 profile_get_string(profile, "fs_types", *cpp,
1668 "features", "", &tmp);
1670 edit_feature(tmp, &fs_param.s_feature_compat);
1674 tmp = get_string_from_profile(fs_types, "default_features",
1677 edit_feature(fs_features ? fs_features : tmp,
1678 &fs_param.s_feature_compat);
1683 * We now need to do a sanity check of fs_blocks_count for
1684 * 32-bit vs 64-bit block number support.
1686 if ((fs_blocks_count > MAX_32_NUM) && (blocksize == 0)) {
1687 fs_blocks_count /= 4; /* Try using a 4k blocksize */
1689 fs_param.s_log_block_size = 2;
1691 if ((fs_blocks_count > MAX_32_NUM) &&
1692 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT) &&
1693 get_bool_from_profile(fs_types, "auto_64-bit_support", 0)) {
1694 fs_param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
1695 fs_param.s_feature_compat &= ~EXT2_FEATURE_COMPAT_RESIZE_INODE;
1697 if ((fs_blocks_count > MAX_32_NUM) &&
1698 !(fs_param.s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT)) {
1699 fprintf(stderr, _("%s: Size of device (0x%llx blocks) %s "
1700 "too big to be expressed\n\t"
1701 "in 32 bits using a blocksize of %d.\n"),
1702 program_name, fs_blocks_count, device_name,
1703 EXT2_BLOCK_SIZE(&fs_param));
1707 ext2fs_blocks_count_set(&fs_param, fs_blocks_count);
1709 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1710 fs_types[0] = strdup("journal");
1715 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1716 print_str_list(fs_types);
1719 if (r_opt == EXT2_GOOD_OLD_REV &&
1720 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1721 fs_param.s_feature_ro_compat)) {
1722 fprintf(stderr, _("Filesystem features not supported "
1723 "with revision 0 filesystems\n"));
1728 if (r_opt == EXT2_GOOD_OLD_REV) {
1729 fprintf(stderr, _("Sparse superblocks not supported "
1730 "with revision 0 filesystems\n"));
1733 fs_param.s_feature_ro_compat |=
1734 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1735 } else if (s_opt == 0)
1736 fs_param.s_feature_ro_compat &=
1737 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1739 if (journal_size != 0) {
1740 if (r_opt == EXT2_GOOD_OLD_REV) {
1741 fprintf(stderr, _("Journals not supported "
1742 "with revision 0 filesystems\n"));
1745 fs_param.s_feature_compat |=
1746 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1749 /* Get reserved_ratio from profile if not specified on cmd line. */
1750 if (reserved_ratio < 0.0) {
1751 reserved_ratio = get_double_from_profile(
1752 fs_types, "reserved_ratio", 5.0);
1753 if (reserved_ratio > 50 || reserved_ratio < 0) {
1754 com_err(program_name, 0,
1755 _("invalid reserved blocks percent - %lf"),
1761 if (fs_param.s_feature_incompat &
1762 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1764 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1765 fs_param.s_feature_compat = 0;
1766 fs_param.s_feature_ro_compat = 0;
1769 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1770 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1771 fprintf(stderr, _("The resize_inode and meta_bg features "
1772 "are not compatible.\n"
1773 "They can not be both enabled "
1774 "simultaneously.\n"));
1778 /* Set first meta blockgroup via an environment variable */
1779 /* (this is mostly for debugging purposes) */
1780 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1781 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1782 fs_param.s_first_meta_bg = atoi(tmp);
1784 /* Get the hardware sector sizes, if available */
1785 retval = ext2fs_get_device_sectsize(device_name, &lsector_size);
1787 com_err(program_name, retval,
1788 _("while trying to determine hardware sector size"));
1791 retval = ext2fs_get_device_phys_sectsize(device_name, &psector_size);
1793 com_err(program_name, retval,
1794 _("while trying to determine physical sector size"));
1798 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1799 lsector_size = atoi(tmp);
1800 if ((tmp = getenv("MKE2FS_DEVICE_PHYS_SECTSIZE")) != NULL)
1801 psector_size = atoi(tmp);
1803 /* Older kernels may not have physical/logical distinction */
1805 psector_size = lsector_size;
1807 if (blocksize <= 0) {
1808 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1810 if (use_bsize == -1) {
1811 use_bsize = sys_page_size;
1812 if ((linux_version_code < (2*65536 + 6*256)) &&
1816 if (lsector_size && use_bsize < lsector_size)
1817 use_bsize = lsector_size;
1818 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1819 use_bsize = -blocksize;
1820 blocksize = use_bsize;
1821 ext2fs_blocks_count_set(&fs_param,
1822 ext2fs_blocks_count(&fs_param) /
1823 (blocksize / 1024));
1825 if (blocksize < lsector_size) { /* Impossible */
1826 com_err(program_name, EINVAL,
1827 _("while setting blocksize; too small "
1830 } else if ((blocksize < psector_size) &&
1831 (psector_size <= sys_page_size)) { /* Suboptimal */
1832 fprintf(stderr, _("Warning: specified blocksize %d is "
1833 "less than device physical sectorsize %d\n"),
1834 blocksize, psector_size);
1838 fs_param.s_log_block_size =
1839 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1840 if (fs_param.s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_BIGALLOC) {
1842 cluster_size = get_int_from_profile(fs_types,
1845 fs_param.s_log_cluster_size =
1846 int_log2(cluster_size >> EXT2_MIN_CLUSTER_LOG_SIZE);
1848 fs_param.s_log_cluster_size = fs_param.s_log_block_size;
1850 if (inode_ratio == 0) {
1851 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1853 if (inode_ratio < blocksize)
1854 inode_ratio = blocksize;
1855 if (inode_ratio < EXT2_CLUSTER_SIZE(&fs_param))
1856 inode_ratio = EXT2_CLUSTER_SIZE(&fs_param);
1859 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1860 retval = get_device_geometry(device_name, &fs_param, psector_size);
1863 _("warning: Unable to get device geometry for %s\n"),
1865 } else if (retval) {
1866 printf(_("%s alignment is offset by %lu bytes.\n"),
1867 device_name, retval);
1868 printf(_("This may result in very poor performance, "
1869 "(re)-partitioning suggested.\n"));
1873 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1875 /* This check should happen beyond the last assignment to blocksize */
1876 if (blocksize > sys_page_size) {
1878 com_err(program_name, 0,
1879 _("%d-byte blocks too big for system (max %d)"),
1880 blocksize, sys_page_size);
1883 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1884 "(max %d), forced to continue\n"),
1885 blocksize, sys_page_size);
1888 lazy_itable_init = 0;
1889 if (access("/sys/fs/ext4/features/lazy_itable_init", R_OK) == 0)
1890 lazy_itable_init = 1;
1892 lazy_itable_init = get_bool_from_profile(fs_types,
1895 discard = get_bool_from_profile(fs_types, "discard" , discard);
1896 journal_flags |= get_bool_from_profile(fs_types,
1897 "lazy_journal_init", 0) ?
1898 EXT2_MKJOURNAL_LAZYINIT : 0;
1900 /* Get options from profile */
1901 for (cpp = fs_types; *cpp; cpp++) {
1903 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1905 parse_extended_opts(&fs_param, tmp);
1910 parse_extended_opts(&fs_param, extended_opts);
1912 /* Since sparse_super is the default, we would only have a problem
1913 * here if it was explicitly disabled.
1915 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1916 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1917 com_err(program_name, 0,
1918 _("reserved online resize blocks not supported "
1919 "on non-sparse filesystem"));
1923 if (fs_param.s_blocks_per_group) {
1924 if (fs_param.s_blocks_per_group < 256 ||
1925 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1926 com_err(program_name, 0,
1927 _("blocks per group count out of range"));
1932 if (inode_size == 0)
1933 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1934 if (!flex_bg_size && (fs_param.s_feature_incompat &
1935 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1936 flex_bg_size = get_int_from_profile(fs_types,
1937 "flex_bg_size", 16);
1939 if (!(fs_param.s_feature_incompat &
1940 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1941 com_err(program_name, 0,
1942 _("Flex_bg feature not enabled, so "
1943 "flex_bg size may not be specified"));
1946 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1949 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1950 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1951 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1952 inode_size & (inode_size - 1)) {
1953 com_err(program_name, 0,
1954 _("invalid inode size %d (min %d/max %d)"),
1955 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1959 fs_param.s_inode_size = inode_size;
1962 /* Make sure number of inodes specified will fit in 32 bits */
1963 if (num_inodes == 0) {
1964 unsigned long long n;
1965 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1966 if (n > MAX_32_NUM) {
1967 if (fs_param.s_feature_incompat &
1968 EXT4_FEATURE_INCOMPAT_64BIT)
1969 num_inodes = MAX_32_NUM;
1971 com_err(program_name, 0,
1972 _("too many inodes (%llu), raise "
1973 "inode ratio?"), n);
1977 } else if (num_inodes > MAX_32_NUM) {
1978 com_err(program_name, 0,
1979 _("too many inodes (%llu), specify < 2^32 inodes"),
1984 * Calculate number of inodes based on the inode ratio
1986 fs_param.s_inodes_count = num_inodes ? num_inodes :
1987 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1989 if ((((long long)fs_param.s_inodes_count) *
1990 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1991 ((ext2fs_blocks_count(&fs_param)) *
1992 EXT2_BLOCK_SIZE(&fs_param))) {
1993 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1994 "(%u) too big for a\n\t"
1995 "filesystem with %llu blocks, "
1996 "specify higher inode_ratio (-i)\n\t"
1997 "or lower inode count (-N).\n"),
1998 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1999 fs_param.s_inodes_count,
2000 (unsigned long long) ext2fs_blocks_count(&fs_param));
2005 * Calculate number of blocks to reserve
2007 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
2008 ext2fs_blocks_count(&fs_param) / 100.0);
2014 static int should_do_undo(const char *name)
2019 struct ext2_super_block super;
2020 io_manager manager = unix_io_manager;
2021 int csum_flag, force_undo;
2023 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2024 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
2025 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
2026 if (!force_undo && (!csum_flag || !lazy_itable_init))
2029 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
2032 * We don't handle error cases instead we
2033 * declare that the file system doesn't exist
2034 * and let the rest of mke2fs take care of
2041 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
2042 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
2048 #if defined(WORDS_BIGENDIAN)
2049 s_magic = ext2fs_swab16(super.s_magic);
2051 s_magic = super.s_magic;
2054 if (s_magic == EXT2_SUPER_MAGIC)
2058 io_channel_close(channel);
2065 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
2067 errcode_t retval = ENOMEM;
2068 char *tdb_dir = NULL, *tdb_file = NULL;
2069 char *device_name, *tmp_name;
2070 int free_tdb_dir = 0;
2073 * Configuration via a conf file would be
2076 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
2078 profile_get_string(profile, "defaults",
2079 "undo_dir", 0, "/var/lib/e2fsprogs",
2084 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
2085 access(tdb_dir, W_OK))
2088 tmp_name = strdup(name);
2091 device_name = basename(tmp_name);
2092 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
2097 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
2100 if (!access(tdb_file, F_OK)) {
2101 if (unlink(tdb_file) < 0) {
2107 set_undo_io_backing_manager(*io_ptr);
2108 *io_ptr = undo_io_manager;
2109 retval = set_undo_io_backup_file(tdb_file);
2112 printf(_("Overwriting existing filesystem; this can be undone "
2113 "using the command:\n"
2114 " e2undo %s %s\n\n"), tdb_file, name);
2125 com_err(program_name, retval,
2126 _("while trying to setup undo file\n"));
2130 static int mke2fs_discard_device(ext2_filsys fs)
2132 struct ext2fs_numeric_progress_struct progress;
2133 blk64_t blocks = ext2fs_blocks_count(fs->super);
2134 blk64_t count = DISCARD_STEP_MB;
2139 * Let's try if discard really works on the device, so
2140 * we do not print numeric progress resulting in failure
2143 retval = io_channel_discard(fs->io, 0, fs->blocksize);
2146 cur = fs->blocksize;
2148 count *= (1024 * 1024);
2149 count /= fs->blocksize;
2151 ext2fs_numeric_progress_init(fs, &progress,
2152 _("Discarding device blocks: "),
2154 while (cur < blocks) {
2155 ext2fs_numeric_progress_update(fs, &progress, cur);
2157 if (cur + count > blocks)
2158 count = blocks - cur;
2160 retval = io_channel_discard(fs->io, cur, count);
2167 ext2fs_numeric_progress_close(fs, &progress,
2170 printf("%s\n",error_message(retval));
2172 ext2fs_numeric_progress_close(fs, &progress,
2178 static void fix_cluster_bg_counts(ext2_filsys fs)
2180 blk64_t cluster, num_clusters, tot_free;
2181 int grp_free, num_free, group, num;
2183 num_clusters = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super));
2184 tot_free = num_free = num = group = grp_free = 0;
2185 for (cluster = EXT2FS_B2C(fs, fs->super->s_first_data_block);
2186 cluster < num_clusters; cluster++) {
2187 if (!ext2fs_test_block_bitmap2(fs->block_map,
2188 EXT2FS_C2B(fs, cluster))) {
2193 if ((num == fs->super->s_clusters_per_group) ||
2194 (cluster == num_clusters-1)) {
2195 ext2fs_bg_free_blocks_count_set(fs, group, grp_free);
2196 ext2fs_group_desc_csum_set(fs, group);
2202 ext2fs_free_blocks_count_set(fs->super, EXT2FS_C2B(fs, tot_free));
2205 static int create_quota_inodes(ext2_filsys fs)
2209 quota_init_context(&qctx, fs, -1);
2210 quota_compute_usage(qctx);
2211 quota_write_inode(qctx, quotatype);
2212 quota_release_context(&qctx);
2217 int main (int argc, char *argv[])
2219 errcode_t retval = 0;
2221 badblocks_list bb_list = 0;
2222 unsigned int journal_blocks;
2223 unsigned int i, checkinterval;
2229 char tdb_string[40];
2231 int itable_zeroed = 0;
2234 setlocale(LC_MESSAGES, "");
2235 setlocale(LC_CTYPE, "");
2236 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
2237 textdomain(NLS_CAT_NAME);
2238 set_com_err_gettext(gettext);
2242 #ifdef CONFIG_TESTIO_DEBUG
2243 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
2244 io_ptr = test_io_manager;
2245 test_io_backing_manager = unix_io_manager;
2248 io_ptr = unix_io_manager;
2250 if (should_do_undo(device_name)) {
2251 retval = mke2fs_setup_tdb(device_name, &io_ptr);
2257 * Initialize the superblock....
2259 flags = EXT2_FLAG_EXCLUSIVE;
2260 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
2263 flags |= EXT2_FLAG_64BITS;
2265 * By default, we print how many inode tables or block groups
2266 * or whatever we've written so far. The quiet flag disables
2267 * this, along with a lot of other output.
2270 flags |= EXT2_FLAG_PRINT_PROGRESS;
2271 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
2273 com_err(device_name, retval, _("while setting up superblock"));
2277 /* Can't undo discard ... */
2278 if (!noaction && discard && (io_ptr != undo_io_manager)) {
2279 retval = mke2fs_discard_device(fs);
2280 if (!retval && io_channel_discard_zeroes_data(fs->io)) {
2282 printf(_("Discard succeeded and will return 0s "
2283 " - skipping inode table wipe\n"));
2284 lazy_itable_init = 1;
2289 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
2290 32768 : fs->blocksize * 8);
2291 io_channel_set_options(fs->io, tdb_string);
2293 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
2294 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
2296 if ((fs_param.s_feature_incompat &
2297 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
2298 (fs_param.s_feature_ro_compat &
2299 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
2300 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
2301 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
2302 fs->super->s_kbytes_written = 1;
2305 * Wipe out the old on-disk superblock
2308 zap_sector(fs, 2, 6);
2311 * Parse or generate a UUID for the filesystem
2314 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
2315 com_err(device_name, 0, "could not parse UUID: %s\n",
2320 uuid_generate(fs->super->s_uuid);
2323 * Initialize the directory index variables
2325 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
2327 hash_alg = e2p_string2hash(hash_alg_str);
2329 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
2331 uuid_generate((unsigned char *) fs->super->s_hash_seed);
2334 * Periodic checks can be enabled/disabled via config file.
2335 * Note we override the kernel include file's idea of what the default
2336 * check interval (never) should be. It's a good idea to check at
2337 * least *occasionally*, specially since servers will never rarely get
2338 * to reboot, since Linux is so robust these days. :-)
2340 * 180 days (six months) seems like a good value.
2342 #ifdef EXT2_DFL_CHECKINTERVAL
2343 #undef EXT2_DFL_CHECKINTERVAL
2345 #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
2347 if (get_bool_from_profile(fs_types, "enable_periodic_fsck", 0)) {
2348 fs->super->s_checkinterval = EXT2_DFL_CHECKINTERVAL;
2349 fs->super->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
2351 * Add "jitter" to the superblock's check interval so that we
2352 * don't check all the filesystems at the same time. We use a
2353 * kludgy hack of using the UUID to derive a random jitter value
2355 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
2356 val += fs->super->s_uuid[i];
2357 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
2359 fs->super->s_max_mnt_count = -1;
2362 * Override the creator OS, if applicable
2364 if (creator_os && !set_os(fs->super, creator_os)) {
2365 com_err (program_name, 0, _("unknown os - %s"), creator_os);
2370 * For the Hurd, we will turn off filetype since it doesn't
2373 if (fs->super->s_creator_os == EXT2_OS_HURD)
2374 fs->super->s_feature_incompat &=
2375 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
2378 * Set the volume label...
2381 memset(fs->super->s_volume_name, 0,
2382 sizeof(fs->super->s_volume_name));
2383 strncpy(fs->super->s_volume_name, volume_label,
2384 sizeof(fs->super->s_volume_name));
2388 * Set the last mount directory
2391 memset(fs->super->s_last_mounted, 0,
2392 sizeof(fs->super->s_last_mounted));
2393 strncpy(fs->super->s_last_mounted, mount_dir,
2394 sizeof(fs->super->s_last_mounted));
2397 if (!quiet || noaction)
2403 if (fs->super->s_feature_incompat &
2404 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2405 create_journal_dev(fs);
2406 exit(ext2fs_close(fs) ? 1 : 0);
2409 if (bad_blocks_filename)
2410 read_bb_file(fs, &bb_list, bad_blocks_filename);
2412 test_disk(fs, &bb_list);
2414 handle_bad_blocks(fs, bb_list);
2415 fs->stride = fs_stride = fs->super->s_raid_stride;
2417 printf(_("Allocating group tables: "));
2418 retval = ext2fs_allocate_tables(fs);
2420 com_err(program_name, retval,
2421 _("while trying to allocate filesystem tables"));
2425 printf(_("done \n"));
2427 retval = ext2fs_convert_subcluster_bitmap(fs, &fs->block_map);
2429 com_err(program_name, retval,
2430 _("\n\twhile converting subcluster bitmap"));
2435 fs->super->s_state |= EXT2_ERROR_FS;
2436 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2438 * The command "mke2fs -S" is used to recover
2439 * corrupted file systems, so do not mark any of the
2440 * inodes as unused; we want e2fsck to consider all
2441 * inodes as potentially containing recoverable data.
2443 if (fs->super->s_feature_ro_compat &
2444 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
2445 for (i = 1; i < fs->group_desc_count; i++)
2446 ext2fs_bg_itable_unused_set(fs, i, 0);
2449 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2450 blk64_t rsv = 65536 / fs->blocksize;
2451 blk64_t blocks = ext2fs_blocks_count(fs->super);
2455 #ifdef ZAP_BOOTBLOCK
2456 zap_sector(fs, 0, 2);
2460 * Wipe out any old MD RAID (or other) metadata at the end
2461 * of the device. This will also verify that the device is
2462 * as large as we think. Be careful with very small devices.
2464 start = (blocks & ~(rsv - 1));
2468 retval = ext2fs_zero_blocks2(fs, start, blocks - start,
2472 com_err(program_name, retval,
2473 _("while zeroing block %llu at end of filesystem"),
2476 write_inode_tables(fs, lazy_itable_init, itable_zeroed);
2477 create_root_dir(fs);
2478 create_lost_and_found(fs);
2480 create_bad_block_inode(fs, bb_list);
2481 if (fs->super->s_feature_compat &
2482 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2483 retval = ext2fs_create_resize_inode(fs);
2485 com_err("ext2fs_create_resize_inode", retval,
2486 _("while reserving blocks for online resize"));
2492 if (journal_device) {
2496 check_plausibility(journal_device);
2497 check_mount(journal_device, force, _("journal"));
2499 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2500 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2501 fs->blocksize, unix_io_manager, &jfs);
2503 com_err(program_name, retval,
2504 _("while trying to open journal device %s\n"),
2509 printf(_("Adding journal to device %s: "),
2513 retval = ext2fs_add_journal_device(fs, jfs);
2515 com_err (program_name, retval,
2516 _("\n\twhile trying to add journal to device %s"),
2521 printf(_("done\n"));
2523 free(journal_device);
2524 } else if ((journal_size) ||
2525 (fs_param.s_feature_compat &
2526 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2527 journal_blocks = figure_journal_size(journal_size, fs);
2530 printf(_("Skipping journal creation in super-only mode\n"));
2531 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2535 if (!journal_blocks) {
2536 fs->super->s_feature_compat &=
2537 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2541 printf(_("Creating journal (%u blocks): "),
2545 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2548 com_err (program_name, retval,
2549 _("\n\twhile trying to create journal"));
2553 printf(_("done\n"));
2557 fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
2558 retval = ext2fs_mmp_init(fs);
2560 fprintf(stderr, _("\nError while enabling multiple "
2561 "mount protection feature."));
2565 printf(_("Multiple mount protection is enabled "
2566 "with update interval %d seconds.\n"),
2567 fs->super->s_mmp_update_interval);
2570 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2571 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
2572 fix_cluster_bg_counts(fs);
2573 if (EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
2574 EXT4_FEATURE_RO_COMPAT_QUOTA))
2575 create_quota_inodes(fs);
2578 printf(_("Writing superblocks and "
2579 "filesystem accounting information: "));
2580 checkinterval = fs->super->s_checkinterval;
2581 max_mnt_count = fs->super->s_max_mnt_count;
2582 retval = ext2fs_close(fs);
2585 _("\nWarning, had trouble writing out superblocks."));
2586 } else if (!quiet) {
2587 printf(_("done\n\n"));
2588 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2589 print_check_message(max_mnt_count, checkinterval);
2592 remove_error_table(&et_ext2_error_table);
2593 remove_error_table(&et_prof_error_table);
2594 profile_release(profile);
2595 for (i=0; fs_types[i]; i++)