2 * mke2fs.c - Make a ext2fs filesystem.
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
13 /* Usage: mke2fs [options] device
15 * The device may be a block device or a image of one, but this isn't
16 * enforced (but it's not much fun on a character device :-).
25 #include <sys/utsname.h>
45 #include <sys/ioctl.h>
46 #include <sys/types.h>
48 #include "ext2fs/ext2_fs.h"
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
52 #include "ext2fs/ext2fs.h"
56 #include "../version.h"
57 #include "nls-enable.h"
59 #define STRIDE_LENGTH 8
65 extern int isatty(int);
66 extern FILE *fpopen(const char *cmd, const char *mode);
68 const char * program_name = "mke2fs";
69 const char * device_name /* = NULL */;
71 /* Command line options */
81 char *bad_blocks_filename;
84 struct ext2_super_block fs_param;
89 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
93 int sys_page_size = 4096;
94 int linux_version_code = 0;
96 static void usage(void)
98 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
99 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
100 "[-J journal-options]\n"
101 "\t[-N number-of-inodes] [-m reserved-blocks-percentage] "
102 "[-o creator-os]\n\t[-g blocks-per-group] [-L volume-label] "
103 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
104 "[-r fs-revision] [-E extended-option[,...]]\n"
105 "\t[-T fs-type] [-jnqvFSV] device [blocks-count]\n"),
110 static int int_log2(int arg)
122 static int int_log10(unsigned int arg)
131 static int parse_version_number(const char *s)
133 int major, minor, rev;
139 major = strtol(cp, &endptr, 10);
140 if (cp == endptr || *endptr != '.')
143 minor = strtol(cp, &endptr, 10);
144 if (cp == endptr || *endptr != '.')
147 rev = strtol(cp, &endptr, 10);
150 return ((((major * 256) + minor) * 256) + rev);
154 * Helper function for read_bb_file and test_disk
156 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
158 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
163 * Reads the bad blocks list from a file
165 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
166 const char *bad_blocks_file)
171 f = fopen(bad_blocks_file, "r");
173 com_err("read_bad_blocks_file", errno,
174 _("while trying to open %s"), bad_blocks_file);
177 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
180 com_err("ext2fs_read_bb_FILE", retval,
181 _("while reading in list of bad blocks from file"));
187 * Runs the badblocks program to test the disk
189 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
195 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
196 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
197 fs->device_name, fs->super->s_blocks_count-1);
199 printf(_("Running command: %s\n"), buf);
202 com_err("popen", errno,
203 _("while trying to run '%s'"), buf);
206 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
209 com_err("ext2fs_read_bb_FILE", retval,
210 _("while processing list of bad blocks from program"));
215 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
219 unsigned must_be_good;
221 badblocks_iterate bb_iter;
231 * The primary superblock and group descriptors *must* be
232 * good; if not, abort.
234 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
235 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
236 if (ext2fs_badblocks_list_test(bb_list, i)) {
237 fprintf(stderr, _("Block %d in primary "
238 "superblock/group descriptor area bad.\n"), i);
239 fprintf(stderr, _("Blocks %u through %u must be good "
240 "in order to build a filesystem.\n"),
241 fs->super->s_first_data_block, must_be_good);
242 fputs(_("Aborting....\n"), stderr);
248 * See if any of the bad blocks are showing up in the backup
249 * superblocks and/or group descriptors. If so, issue a
250 * warning and adjust the block counts appropriately.
252 group_block = fs->super->s_first_data_block +
253 fs->super->s_blocks_per_group;
255 for (i = 1; i < fs->group_desc_count; i++) {
257 for (j=0; j < fs->desc_blocks+1; j++) {
258 if (ext2fs_badblocks_list_test(bb_list,
262 _("Warning: the backup superblock/group descriptors at block %u contain\n"
266 group = ext2fs_group_of_blk(fs, group_block+j);
267 fs->group_desc[group].bg_free_blocks_count++;
268 ext2fs_group_desc_csum_set(fs, group);
269 fs->super->s_free_blocks_count++;
272 group_block += fs->super->s_blocks_per_group;
276 * Mark all the bad blocks as used...
278 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
280 com_err("ext2fs_badblocks_list_iterate_begin", retval,
281 _("while marking bad blocks as used"));
284 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
285 ext2fs_mark_block_bitmap(fs->block_map, blk);
286 ext2fs_badblocks_list_iterate_end(bb_iter);
290 * These functions implement a generalized progress meter.
292 struct progress_struct {
299 static void progress_init(struct progress_struct *progress,
300 const char *label,__u32 max)
304 memset(progress, 0, sizeof(struct progress_struct));
309 * Figure out how many digits we need
312 sprintf(progress->format, "%%%dd/%%%dld", i, i);
313 memset(progress->backup, '\b', sizeof(progress->backup)-1);
314 progress->backup[sizeof(progress->backup)-1] = 0;
315 if ((2*i)+1 < (int) sizeof(progress->backup))
316 progress->backup[(2*i)+1] = 0;
319 progress->skip_progress = 0;
320 if (getenv("MKE2FS_SKIP_PROGRESS"))
321 progress->skip_progress++;
323 fputs(label, stdout);
327 static void progress_update(struct progress_struct *progress, __u32 val)
329 if ((progress->format[0] == 0) || progress->skip_progress)
331 printf(progress->format, val, progress->max);
332 fputs(progress->backup, stdout);
335 static void progress_close(struct progress_struct *progress)
337 if (progress->format[0] == 0)
339 fputs(_("done \n"), stdout);
344 * Helper function which zeros out _num_ blocks starting at _blk_. In
345 * case of an error, the details of the error is returned via _ret_blk_
346 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
347 * success, and an error code on an error.
349 * As a special case, if the first argument is NULL, then it will
350 * attempt to free the static zeroizing buffer. (This is to keep
351 * programs that check for memory leaks happy.)
353 static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
354 struct progress_struct *progress,
355 blk_t *ret_blk, int *ret_count)
357 int j, count, next_update, next_update_incr;
361 /* If fs is null, clean up the static buffer and return */
369 /* Allocate the zeroizing buffer if necessary */
371 buf = malloc(fs->blocksize * STRIDE_LENGTH);
373 com_err("malloc", ENOMEM,
374 _("while allocating zeroizing buffer"));
377 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
379 /* OK, do the write loop */
381 next_update_incr = num / 100;
382 if (next_update_incr < 1)
383 next_update_incr = 1;
384 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
386 if (count > STRIDE_LENGTH)
387 count = STRIDE_LENGTH;
388 retval = io_channel_write_blk(fs->io, blk, count, buf);
396 if (progress && j > next_update) {
397 next_update += num / 100;
398 progress_update(progress, blk);
404 static void write_inode_tables(ext2_filsys fs, int lazy_flag)
410 struct progress_struct progress;
413 memset(&progress, 0, sizeof(progress));
415 progress_init(&progress, _("Writing inode tables: "),
416 fs->group_desc_count);
418 for (i = 0; i < fs->group_desc_count; i++) {
419 progress_update(&progress, i);
421 blk = fs->group_desc[i].bg_inode_table;
422 num = fs->inode_blocks_per_group;
425 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
426 printf("bg %i, num %d, ipb %d, unused %d ",
427 i, num, ipb, fs->group_desc[i].bg_itable_unused);
428 num = ((((fs->super->s_inodes_per_group -
429 fs->group_desc[i].bg_itable_unused) *
430 EXT2_INODE_SIZE(fs->super)) +
431 EXT2_BLOCK_SIZE(fs->super) - 1) /
432 EXT2_BLOCK_SIZE(fs->super));
433 printf("new num %d\n", num);
435 /* The kernel doesn't need to zero the itable blocks */
436 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
437 ext2fs_group_desc_csum_set(fs, i);
439 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
441 fprintf(stderr, _("\nCould not write %d "
442 "blocks in inode table starting at %u: %s\n"),
443 num, blk, error_message(retval));
447 if (sync_kludge == 1)
449 else if ((i % sync_kludge) == 0)
453 zero_blocks(0, 0, 0, 0, 0, 0);
454 progress_close(&progress);
457 static void create_root_dir(ext2_filsys fs)
460 struct ext2_inode inode;
463 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
465 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
469 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
471 com_err("ext2fs_read_inode", retval,
472 _("while reading root inode"));
477 ext2fs_set_i_uid_high(inode, uid >> 16);
481 ext2fs_set_i_gid_high(inode, gid >> 16);
483 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
485 com_err("ext2fs_write_inode", retval,
486 _("while setting root inode ownership"));
492 static void create_lost_and_found(ext2_filsys fs)
494 unsigned int lpf_size = 0;
497 const char *name = "lost+found";
501 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
503 com_err("ext2fs_mkdir", retval,
504 _("while creating /lost+found"));
508 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
510 com_err("ext2_lookup", retval,
511 _("while looking up /lost+found"));
515 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
516 /* Ensure that lost+found is at least 2 blocks, so we always
517 * test large empty blocks for big-block filesystems. */
518 if ((lpf_size += fs->blocksize) >= 16*1024 &&
519 lpf_size >= 2 * fs->blocksize)
521 retval = ext2fs_expand_dir(fs, ino);
523 com_err("ext2fs_expand_dir", retval,
524 _("while expanding /lost+found"));
530 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
534 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
535 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
536 retval = ext2fs_update_bb_inode(fs, bb_list);
538 com_err("ext2fs_update_bb_inode", retval,
539 _("while setting bad block inode"));
545 static void reserve_inodes(ext2_filsys fs)
550 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
551 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
552 ext2fs_mark_ib_dirty(fs);
555 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
556 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
557 #define BSD_LABEL_OFFSET 64
559 static void zap_sector(ext2_filsys fs, int sect, int nsect)
565 buf = malloc(512*nsect);
567 printf(_("Out of memory erasing sectors %d-%d\n"),
568 sect, sect + nsect - 1);
573 /* Check for a BSD disklabel, and don't erase it if so */
574 retval = io_channel_read_blk(fs->io, 0, -512, buf);
577 _("Warning: could not read block 0: %s\n"),
578 error_message(retval));
580 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
581 if ((*magic == BSD_DISKMAGIC) ||
582 (*magic == BSD_MAGICDISK))
587 memset(buf, 0, 512*nsect);
588 io_channel_set_blksize(fs->io, 512);
589 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
590 io_channel_set_blksize(fs->io, fs->blocksize);
593 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
594 sect, error_message(retval));
597 static void create_journal_dev(ext2_filsys fs)
599 struct progress_struct progress;
605 retval = ext2fs_create_journal_superblock(fs,
606 fs->super->s_blocks_count, 0, &buf);
608 com_err("create_journal_dev", retval,
609 _("while initializing journal superblock"));
613 memset(&progress, 0, sizeof(progress));
615 progress_init(&progress, _("Zeroing journal device: "),
616 fs->super->s_blocks_count);
618 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
619 &progress, &blk, &count);
621 com_err("create_journal_dev", retval,
622 _("while zeroing journal device (block %u, count %d)"),
626 zero_blocks(0, 0, 0, 0, 0, 0);
628 retval = io_channel_write_blk(fs->io,
629 fs->super->s_first_data_block+1,
632 com_err("create_journal_dev", retval,
633 _("while writing journal superblock"));
636 progress_close(&progress);
639 static void show_stats(ext2_filsys fs)
641 struct ext2_super_block *s = fs->super;
648 if (fs_param.s_blocks_count != s->s_blocks_count)
649 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
650 fs_param.s_blocks_count - s->s_blocks_count);
652 memset(buf, 0, sizeof(buf));
653 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
654 printf(_("Filesystem label=%s\n"), buf);
655 fputs(_("OS type: "), stdout);
656 os = e2p_os2string(fs->super->s_creator_os);
660 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
661 s->s_log_block_size);
662 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
664 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
666 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
668 100.0 * s->s_r_blocks_count / s->s_blocks_count);
669 printf(_("First data block=%u\n"), s->s_first_data_block);
670 if (s->s_reserved_gdt_blocks)
671 printf(_("Maximum filesystem blocks=%lu\n"),
672 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
673 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
674 if (fs->group_desc_count > 1)
675 printf(_("%u block groups\n"), fs->group_desc_count);
677 printf(_("%u block group\n"), fs->group_desc_count);
678 printf(_("%u blocks per group, %u fragments per group\n"),
679 s->s_blocks_per_group, s->s_frags_per_group);
680 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
682 if (fs->group_desc_count == 1) {
687 printf(_("Superblock backups stored on blocks: "));
688 group_block = s->s_first_data_block;
690 for (i = 1; i < fs->group_desc_count; i++) {
691 group_block += s->s_blocks_per_group;
692 if (!ext2fs_bg_has_super(fs, i))
696 need = int_log10(group_block) + 2;
697 if (need > col_left) {
702 printf("%u", group_block);
708 * Set the S_CREATOR_OS field. Return true if OS is known,
711 static int set_os(struct ext2_super_block *sb, char *os)
714 sb->s_creator_os = atoi (os);
715 else if (strcasecmp(os, "linux") == 0)
716 sb->s_creator_os = EXT2_OS_LINUX;
717 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
718 sb->s_creator_os = EXT2_OS_HURD;
719 else if (strcasecmp(os, "freebsd") == 0)
720 sb->s_creator_os = EXT2_OS_FREEBSD;
721 else if (strcasecmp(os, "lites") == 0)
722 sb->s_creator_os = EXT2_OS_LITES;
728 #define PATH_SET "PATH=/sbin"
730 static void parse_extended_opts(struct ext2_super_block *param,
733 char *buf, *token, *next, *p, *arg, *badopt = 0;
741 _("Couldn't allocate memory to parse options!\n"));
745 for (token = buf; token && *token; token = next) {
746 p = strchr(token, ',');
752 arg = strchr(token, '=');
757 if (strcmp(token, "stride") == 0) {
763 param->s_raid_stride = strtoul(arg, &p, 0);
764 if (*p || (param->s_raid_stride == 0)) {
766 _("Invalid stride parameter: %s\n"),
771 } else if (strcmp(token, "stripe-width") == 0 ||
772 strcmp(token, "stripe_width") == 0) {
778 param->s_raid_stripe_width = strtoul(arg, &p, 0);
779 if (*p || (param->s_raid_stripe_width == 0)) {
781 _("Invalid stripe-width parameter: %s\n"),
786 } else if (!strcmp(token, "resize")) {
787 unsigned long resize, bpg, rsv_groups;
788 unsigned long group_desc_count, desc_blocks;
789 unsigned int gdpb, blocksize;
798 resize = parse_num_blocks(arg,
799 param->s_log_block_size);
803 _("Invalid resize parameter: %s\n"),
808 if (resize <= param->s_blocks_count) {
810 _("The resize maximum must be greater "
811 "than the filesystem size.\n"));
816 blocksize = EXT2_BLOCK_SIZE(param);
817 bpg = param->s_blocks_per_group;
820 gdpb = EXT2_DESC_PER_BLOCK(param);
822 ext2fs_div_ceil(param->s_blocks_count, bpg);
823 desc_blocks = (group_desc_count +
825 rsv_groups = ext2fs_div_ceil(resize, bpg);
826 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
828 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
829 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
832 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
834 _("On-line resizing not supported with revision 0 filesystems\n"));
838 param->s_feature_compat |=
839 EXT2_FEATURE_COMPAT_RESIZE_INODE;
841 param->s_reserved_gdt_blocks = rsv_gdb;
843 } else if (!strcmp(token, "test_fs")) {
844 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
845 } else if (!strcmp(token, "lazy_itable_init")) {
851 lazy_itable_init = strtoul(arg, &p, 0);
858 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
859 "Extended options are separated by commas, "
860 "and may take an argument which\n"
861 "\tis set off by an equals ('=') sign.\n\n"
862 "Valid extended options are:\n"
863 "\tstride=<RAID per-disk data chunk in blocks>\n"
864 "\tstripe-width=<RAID stride * data disks in blocks>\n"
865 "\tresize=<resize maximum size in blocks>\n"
866 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
868 badopt ? badopt : "");
872 if (param->s_raid_stride &&
873 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
874 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
875 "multiple of stride %u.\n\n"),
876 param->s_raid_stripe_width, param->s_raid_stride);
881 static __u32 ok_features[3] = {
883 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
884 EXT2_FEATURE_COMPAT_RESIZE_INODE |
885 EXT2_FEATURE_COMPAT_DIR_INDEX |
886 EXT2_FEATURE_COMPAT_EXT_ATTR,
888 EXT2_FEATURE_INCOMPAT_FILETYPE|
889 EXT3_FEATURE_INCOMPAT_EXTENTS|
890 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
891 EXT2_FEATURE_INCOMPAT_META_BG|
892 EXT4_FEATURE_INCOMPAT_FLEX_BG,
894 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
895 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
896 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
900 static void syntax_err_report(const char *filename, long err, int line_num)
903 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
904 filename, line_num, error_message(err));
908 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
910 static void edit_feature(const char *str, __u32 *compat_array)
915 if (e2p_edit_feature(str, compat_array, ok_features)) {
916 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
928 static errcode_t init_list(struct str_list *sl)
932 sl->list = malloc((sl->max+1) * sizeof(char *));
939 static errcode_t push_string(struct str_list *sl, const char *str)
943 if (sl->num >= sl->max) {
945 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
950 sl->list[sl->num] = malloc(strlen(str)+1);
951 if (sl->list[sl->num] == 0)
953 strcpy(sl->list[sl->num], str);
955 sl->list[sl->num] = 0;
959 static void print_str_list(char **list)
963 for (cpp = list; *cpp; cpp++) {
964 printf("'%s'", *cpp);
971 static char **parse_fs_type(const char *fs_type,
972 const char *usage_types,
973 struct ext2_super_block *fs_param,
976 const char *ext_type = 0;
978 char *profile_type = 0;
980 const char *size_type;
981 struct str_list list;
985 if (init_list(&list))
991 ext_type = strrchr(progname, '/');
997 if (!strncmp(ext_type, "mkfs.", 5)) {
999 if (ext_type[0] == 0)
1006 profile_get_string(profile, "defaults", "fs_type", 0,
1007 "ext2", &profile_type);
1008 ext_type = profile_type;
1009 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
1013 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
1014 if (fs_param->s_blocks_count < 3 * meg)
1015 size_type = "floppy";
1016 else if (fs_param->s_blocks_count < 512 * meg)
1017 size_type = "small";
1019 size_type = "default";
1022 usage_types = size_type;
1024 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1030 strcpy(parse_str, usage_types);
1035 push_string(&list, ext_type);
1038 t = strchr(cp, ',');
1043 push_string(&list, cp);
1057 static char *get_string_from_profile(char **fs_types, const char *opt,
1058 const char *def_val)
1064 for (i=0; fs_types[i]; i++);
1065 for (i-=1; i >=0 ; i--) {
1066 profile_get_string(profile, "fs_types", fs_types[i],
1071 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1075 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1080 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1081 for (cpp = fs_types; *cpp; cpp++)
1082 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1086 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1091 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1092 for (cpp = fs_types; *cpp; cpp++)
1093 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1097 extern const char *mke2fs_default_profile;
1098 static const char *default_files[] = { "<default>", 0 };
1100 static void PRS(int argc, char *argv[])
1106 int inode_ratio = 0;
1108 double reserved_ratio = 5.0;
1109 int sector_size = 0;
1110 int show_version_only = 0;
1111 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1113 char * oldpath = getenv("PATH");
1114 char * extended_opts = 0;
1115 const char * fs_type = 0;
1116 const char * usage_types = 0;
1123 int s_opt = -1, r_opt = -1;
1124 char *fs_features = 0;
1127 int pathlen = sizeof(PATH_SET) + 1;
1130 pathlen += strlen(oldpath);
1131 newpath = malloc(pathlen);
1132 strcpy(newpath, PATH_SET);
1134 /* Update our PATH to include /sbin */
1136 strcat (newpath, ":");
1137 strcat (newpath, oldpath);
1141 tmp = getenv("MKE2FS_SYNC");
1143 sync_kludge = atoi(tmp);
1145 /* Determine the system page size if possible */
1147 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1148 #define _SC_PAGESIZE _SC_PAGE_SIZE
1151 sysval = sysconf(_SC_PAGESIZE);
1153 sys_page_size = sysval;
1154 #endif /* _SC_PAGESIZE */
1155 #endif /* HAVE_SYSCONF */
1157 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1159 profile_set_syntax_err_cb(syntax_err_report);
1160 retval = profile_init(config_fn, &profile);
1161 if (retval == ENOENT) {
1162 profile_init(default_files, &profile);
1163 profile_set_default(profile, mke2fs_default_profile);
1166 setbuf(stdout, NULL);
1167 setbuf(stderr, NULL);
1168 add_error_table(&et_ext2_error_table);
1169 add_error_table(&et_prof_error_table);
1170 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1171 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1178 linux_version_code = parse_version_number(ut.release);
1179 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1180 fs_param.s_rev_level = 0;
1183 if (argc && *argv) {
1184 program_name = get_progname(*argv);
1186 /* If called as mkfs.ext3, create a journal inode */
1187 if (!strcmp(program_name, "mkfs.ext3"))
1191 while ((c = getopt (argc, argv,
1192 "b:cf:g:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:V")) != EOF) {
1195 blocksize = strtol(optarg, &tmp, 0);
1196 b = (blocksize > 0) ? blocksize : -blocksize;
1197 if (b < EXT2_MIN_BLOCK_SIZE ||
1198 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1199 com_err(program_name, 0,
1200 _("invalid block size - %s"), optarg);
1203 if (blocksize > 4096)
1204 fprintf(stderr, _("Warning: blocksize %d not "
1205 "usable on most systems.\n"),
1208 fs_param.s_log_block_size =
1209 int_log2(blocksize >>
1210 EXT2_MIN_BLOCK_LOG_SIZE);
1212 case 'c': /* Check for bad blocks */
1216 size = strtoul(optarg, &tmp, 0);
1217 if (size < EXT2_MIN_BLOCK_SIZE ||
1218 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1219 com_err(program_name, 0,
1220 _("invalid fragment size - %s"),
1224 fs_param.s_log_frag_size =
1225 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1226 fprintf(stderr, _("Warning: fragments not supported. "
1227 "Ignoring -f option\n"));
1230 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1232 com_err(program_name, 0,
1233 _("Illegal number for blocks per group"));
1236 if ((fs_param.s_blocks_per_group % 8) != 0) {
1237 com_err(program_name, 0,
1238 _("blocks per group must be multiple of 8"));
1243 inode_ratio = strtoul(optarg, &tmp, 0);
1244 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1245 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1247 com_err(program_name, 0,
1248 _("invalid inode ratio %s (min %d/max %d)"),
1249 optarg, EXT2_MIN_BLOCK_SIZE,
1250 EXT2_MAX_BLOCK_SIZE);
1255 parse_journal_opts(optarg);
1262 bad_blocks_filename = malloc(strlen(optarg)+1);
1263 if (!bad_blocks_filename) {
1264 com_err(program_name, ENOMEM,
1265 _("in malloc for bad_blocks_filename"));
1268 strcpy(bad_blocks_filename, optarg);
1271 reserved_ratio = strtod(optarg, &tmp);
1272 if (reserved_ratio > 50 || *tmp) {
1273 com_err(program_name, 0,
1274 _("invalid reserved blocks percent - %s"),
1283 creator_os = optarg;
1289 r_opt = strtoul(optarg, &tmp, 0);
1291 com_err(program_name, 0,
1292 _("bad revision level - %s"), optarg);
1295 fs_param.s_rev_level = r_opt;
1297 case 's': /* deprecated */
1298 s_opt = atoi(optarg);
1301 inode_size = strtoul(optarg, &tmp, 0);
1303 com_err(program_name, 0,
1304 _("invalid inode size - %s"), optarg);
1315 volume_label = optarg;
1321 num_inodes = strtoul(optarg, &tmp, 0);
1323 com_err(program_name, 0,
1324 _("bad num inodes - %s"), optarg);
1329 fs_features = optarg;
1333 extended_opts = optarg;
1342 usage_types = optarg;
1345 /* Print version number and exit */
1346 show_version_only++;
1352 if ((optind == argc) && !show_version_only)
1354 device_name = argv[optind++];
1356 if (!quiet || show_version_only)
1357 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1360 if (show_version_only) {
1361 fprintf(stderr, _("\tUsing %s\n"),
1362 error_message(EXT2_ET_BASE));
1367 * If there's no blocksize specified and there is a journal
1368 * device, use it to figure out the blocksize
1370 if (blocksize <= 0 && journal_device) {
1374 #ifdef CONFIG_TESTIO_DEBUG
1375 io_ptr = test_io_manager;
1376 test_io_backing_manager = unix_io_manager;
1378 io_ptr = unix_io_manager;
1380 retval = ext2fs_open(journal_device,
1381 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1384 com_err(program_name, retval,
1385 _("while trying to open journal device %s\n"),
1389 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1390 com_err(program_name, 0,
1391 _("Journal dev blocksize (%d) smaller than "
1392 "minimum blocksize %d\n"), jfs->blocksize,
1396 blocksize = jfs->blocksize;
1397 fs_param.s_log_block_size =
1398 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1402 if (blocksize > sys_page_size) {
1404 com_err(program_name, 0,
1405 _("%d-byte blocks too big for system (max %d)"),
1406 blocksize, sys_page_size);
1409 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1410 "(max %d), forced to continue\n"),
1411 blocksize, sys_page_size);
1413 if (optind < argc) {
1414 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1415 fs_param.s_log_block_size);
1416 if (!fs_param.s_blocks_count) {
1417 com_err(program_name, 0, _("invalid blocks count - %s"),
1426 check_plausibility(device_name);
1427 check_mount(device_name, force, _("filesystem"));
1429 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1431 if (noaction && fs_param.s_blocks_count) {
1432 dev_size = fs_param.s_blocks_count;
1436 retval = ext2fs_get_device_size(device_name,
1437 EXT2_BLOCK_SIZE(&fs_param),
1439 if ((retval == EFBIG) &&
1441 (fs_param.s_log_block_size == 0)) {
1442 fs_param.s_log_block_size = 2;
1448 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1449 com_err(program_name, retval,
1450 _("while trying to determine filesystem size"));
1453 if (!fs_param.s_blocks_count) {
1454 if (retval == EXT2_ET_UNIMPLEMENTED) {
1455 com_err(program_name, 0,
1456 _("Couldn't determine device size; you "
1457 "must specify\nthe size of the "
1461 if (dev_size == 0) {
1462 com_err(program_name, 0,
1463 _("Device size reported to be zero. "
1464 "Invalid partition specified, or\n\t"
1465 "partition table wasn't reread "
1466 "after running fdisk, due to\n\t"
1467 "a modified partition being busy "
1468 "and in use. You may need to reboot\n\t"
1469 "to re-read your partition table.\n"
1473 fs_param.s_blocks_count = dev_size;
1474 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1475 fs_param.s_blocks_count &= ~((sys_page_size /
1476 EXT2_BLOCK_SIZE(&fs_param))-1);
1479 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1480 com_err(program_name, 0,
1481 _("Filesystem larger than apparent device size."));
1485 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1487 fprintf(stderr, _("Failed to parse fs types list\n"));
1491 fputs("Fs_types for mke2fs.conf resolution: ", stdout);
1492 print_str_list(fs_types);
1496 int megs = (__u64)fs_param.s_blocks_count *
1497 (EXT2_BLOCK_SIZE(&fs_param) / 1024) / 1024;
1499 if (fs_param.s_feature_incompat &
1500 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
1501 fs_type = "journal";
1504 else if (megs <= 512)
1507 fs_type = "default";
1510 /* Figure out what features should be enabled */
1513 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1516 tmp = get_string_from_profile(fs_types, "base_features",
1517 "sparse_super,filetype,resize_inode,dir_index");
1518 edit_feature(tmp, &fs_param.s_feature_compat);
1521 for (cpp = fs_types; *cpp; cpp++) {
1523 profile_get_string(profile, "fs_types", *cpp,
1524 "features", "", &tmp);
1526 edit_feature(tmp, &fs_param.s_feature_compat);
1530 tmp = get_string_from_profile(fs_types, "default_features",
1533 edit_feature(fs_features ? fs_features : tmp,
1534 &fs_param.s_feature_compat);
1538 if (r_opt == EXT2_GOOD_OLD_REV &&
1539 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1540 fs_param.s_feature_incompat)) {
1541 fprintf(stderr, _("Filesystem features not supported "
1542 "with revision 0 filesystems\n"));
1547 if (r_opt == EXT2_GOOD_OLD_REV) {
1548 fprintf(stderr, _("Sparse superblocks not supported "
1549 "with revision 0 filesystems\n"));
1552 fs_param.s_feature_ro_compat |=
1553 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1554 } else if (s_opt == 0)
1555 fs_param.s_feature_ro_compat &=
1556 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1558 if (journal_size != 0) {
1559 if (r_opt == EXT2_GOOD_OLD_REV) {
1560 fprintf(stderr, _("Journals not supported "
1561 "with revision 0 filesystems\n"));
1564 fs_param.s_feature_compat |=
1565 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1568 if (fs_param.s_feature_incompat &
1569 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1571 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1572 fs_param.s_feature_compat = 0;
1573 fs_param.s_feature_ro_compat = 0;
1576 /* Set first meta blockgroup via an environment variable */
1577 /* (this is mostly for debugging purposes) */
1578 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1579 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1580 fs_param.s_first_meta_bg = atoi(tmp);
1582 /* Get the hardware sector size, if available */
1583 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1585 com_err(program_name, retval,
1586 _("while trying to determine hardware sector size"));
1590 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1591 sector_size = atoi(tmp);
1593 if (blocksize <= 0) {
1594 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1596 if (use_bsize == -1) {
1597 use_bsize = sys_page_size;
1598 if ((linux_version_code < (2*65536 + 6*256)) &&
1602 if (sector_size && use_bsize < sector_size)
1603 use_bsize = sector_size;
1604 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1605 use_bsize = -blocksize;
1606 blocksize = use_bsize;
1607 fs_param.s_blocks_count /= blocksize / 1024;
1610 if (inode_ratio == 0) {
1611 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1613 if (inode_ratio < blocksize)
1614 inode_ratio = blocksize;
1617 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1618 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1620 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1622 lazy_itable_init = get_bool_from_profile(fs_types,
1623 "lazy_itable_init", 0);
1626 parse_extended_opts(&fs_param, extended_opts);
1628 /* Since sparse_super is the default, we would only have a problem
1629 * here if it was explicitly disabled.
1631 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1632 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1633 com_err(program_name, 0,
1634 _("reserved online resize blocks not supported "
1635 "on non-sparse filesystem"));
1639 if (fs_param.s_blocks_per_group) {
1640 if (fs_param.s_blocks_per_group < 256 ||
1641 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1642 com_err(program_name, 0,
1643 _("blocks per group count out of range"));
1648 if (inode_size == 0)
1649 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1651 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1652 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1653 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1654 inode_size & (inode_size - 1)) {
1655 com_err(program_name, 0,
1656 _("invalid inode size %d (min %d/max %d)"),
1657 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1661 fs_param.s_inode_size = inode_size;
1664 /* Make sure number of inodes specified will fit in 32 bits */
1665 if (num_inodes == 0) {
1666 unsigned long long n;
1667 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
1669 com_err(program_name, 0,
1670 _("too many inodes (%llu), raise inode ratio?"), n);
1673 } else if (num_inodes > ~0U) {
1674 com_err(program_name, 0,
1675 _("too many inodes (%llu), specify < 2^32 inodes"),
1680 * Calculate number of inodes based on the inode ratio
1682 fs_param.s_inodes_count = num_inodes ? num_inodes :
1683 ((__u64) fs_param.s_blocks_count * blocksize)
1686 if ((((long long)fs_param.s_inodes_count) *
1687 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1688 (((long long)fs_param.s_blocks_count) *
1689 EXT2_BLOCK_SIZE(&fs_param))) {
1690 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1691 "(%u) too big for a\n\t"
1692 "filesystem with %lu blocks, "
1693 "specify higher inode_ratio (-i)\n\t"
1694 "or lower inode count (-N).\n"),
1695 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1696 fs_param.s_inodes_count,
1697 (unsigned long) fs_param.s_blocks_count);
1702 * Calculate number of blocks to reserve
1704 fs_param.s_r_blocks_count = e2p_percent(reserved_ratio,
1705 fs_param.s_blocks_count);
1708 int main (int argc, char *argv[])
1710 errcode_t retval = 0;
1712 badblocks_list bb_list = 0;
1713 unsigned int journal_blocks;
1719 setlocale(LC_MESSAGES, "");
1720 setlocale(LC_CTYPE, "");
1721 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1722 textdomain(NLS_CAT_NAME);
1726 #ifdef CONFIG_TESTIO_DEBUG
1727 io_ptr = test_io_manager;
1728 test_io_backing_manager = unix_io_manager;
1730 io_ptr = unix_io_manager;
1734 * Initialize the superblock....
1736 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
1739 com_err(device_name, retval, _("while setting up superblock"));
1743 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1744 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1747 * Wipe out the old on-disk superblock
1750 zap_sector(fs, 2, 6);
1753 * Generate a UUID for it...
1755 uuid_generate(fs->super->s_uuid);
1758 * Initialize the directory index variables
1760 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1761 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1764 * Add "jitter" to the superblock's check interval so that we
1765 * don't check all the filesystems at the same time. We use a
1766 * kludgy hack of using the UUID to derive a random jitter value.
1768 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1769 val += fs->super->s_uuid[i];
1770 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1773 * Override the creator OS, if applicable
1775 if (creator_os && !set_os(fs->super, creator_os)) {
1776 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1781 * For the Hurd, we will turn off filetype since it doesn't
1784 if (fs->super->s_creator_os == EXT2_OS_HURD)
1785 fs->super->s_feature_incompat &=
1786 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1789 * Set the volume label...
1792 memset(fs->super->s_volume_name, 0,
1793 sizeof(fs->super->s_volume_name));
1794 strncpy(fs->super->s_volume_name, volume_label,
1795 sizeof(fs->super->s_volume_name));
1799 * Set the last mount directory
1802 memset(fs->super->s_last_mounted, 0,
1803 sizeof(fs->super->s_last_mounted));
1804 strncpy(fs->super->s_last_mounted, mount_dir,
1805 sizeof(fs->super->s_last_mounted));
1808 if (!quiet || noaction)
1814 if (fs->super->s_feature_incompat &
1815 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1816 create_journal_dev(fs);
1817 exit(ext2fs_close(fs) ? 1 : 0);
1820 if (bad_blocks_filename)
1821 read_bb_file(fs, &bb_list, bad_blocks_filename);
1823 test_disk(fs, &bb_list);
1825 handle_bad_blocks(fs, bb_list);
1826 fs->stride = fs_stride = fs->super->s_raid_stride;
1827 retval = ext2fs_allocate_tables(fs);
1829 com_err(program_name, retval,
1830 _("while trying to allocate filesystem tables"));
1834 fs->super->s_state |= EXT2_ERROR_FS;
1835 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1837 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1838 unsigned int rsv = 65536 / fs->blocksize;
1839 unsigned long blocks = fs->super->s_blocks_count;
1840 unsigned long start;
1843 #ifdef ZAP_BOOTBLOCK
1844 zap_sector(fs, 0, 2);
1848 * Wipe out any old MD RAID (or other) metadata at the end
1849 * of the device. This will also verify that the device is
1850 * as large as we think. Be careful with very small devices.
1852 start = (blocks & ~(rsv - 1));
1856 retval = zero_blocks(fs, start, blocks - start,
1857 NULL, &ret_blk, NULL);
1860 com_err(program_name, retval,
1861 _("while zeroing block %u at end of filesystem"),
1864 write_inode_tables(fs, lazy_itable_init);
1865 create_root_dir(fs);
1866 create_lost_and_found(fs);
1868 create_bad_block_inode(fs, bb_list);
1869 if (fs->super->s_feature_compat &
1870 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1871 retval = ext2fs_create_resize_inode(fs);
1873 com_err("ext2fs_create_resize_inode", retval,
1874 _("while reserving blocks for online resize"));
1880 if (journal_device) {
1884 check_plausibility(journal_device);
1885 check_mount(journal_device, force, _("journal"));
1887 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1888 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1889 fs->blocksize, unix_io_manager, &jfs);
1891 com_err(program_name, retval,
1892 _("while trying to open journal device %s\n"),
1897 printf(_("Adding journal to device %s: "),
1901 retval = ext2fs_add_journal_device(fs, jfs);
1903 com_err (program_name, retval,
1904 _("\n\twhile trying to add journal to device %s"),
1909 printf(_("done\n"));
1911 free(journal_device);
1912 } else if ((journal_size) ||
1913 (fs_param.s_feature_compat &
1914 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1915 journal_blocks = figure_journal_size(journal_size, fs);
1917 if (!journal_blocks) {
1918 fs->super->s_feature_compat &=
1919 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1923 printf(_("Creating journal (%u blocks): "),
1927 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1930 com_err (program_name, retval,
1931 _("\n\twhile trying to create journal"));
1935 printf(_("done\n"));
1940 printf(_("Writing superblocks and "
1941 "filesystem accounting information: "));
1942 retval = ext2fs_flush(fs);
1945 _("\nWarning, had trouble writing out superblocks."));
1948 printf(_("done\n\n"));
1949 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
1950 print_check_message(fs);
1952 val = ext2fs_close(fs);
1953 remove_error_table(&et_ext2_error_table);
1954 remove_error_table(&et_prof_error_table);
1955 profile_release(profile);
1956 return (retval || val) ? 1 : 0;