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 */
27 #include <sys/utsname.h>
47 #include <sys/ioctl.h>
48 #include <sys/types.h>
52 #include "ext2fs/ext2_fs.h"
53 #include "et/com_err.h"
54 #include "uuid/uuid.h"
56 #include "ext2fs/ext2fs.h"
60 #include "../version.h"
61 #include "nls-enable.h"
63 #define STRIDE_LENGTH 8
69 extern int isatty(int);
70 extern FILE *fpopen(const char *cmd, const char *mode);
72 const char * program_name = "mke2fs";
73 const char * device_name /* = NULL */;
75 /* Command line options */
85 char *bad_blocks_filename;
88 struct ext2_super_block fs_param;
94 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
99 int sys_page_size = 4096;
100 int linux_version_code = 0;
102 static void usage(void)
104 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
105 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
106 "[-J journal-options]\n"
107 "\t[-G meta group size] [-N number-of-inodes]\n"
108 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
109 "\t[-g blocks-per-group] [-L volume-label] "
110 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
111 "[-r fs-revision] [-E extended-option[,...]]\n"
112 "\t[-T fs-type] [-U UUID] [-jnqvFSV] device [blocks-count]\n"),
117 static int int_log2(int arg)
129 static int int_log10(unsigned int arg)
138 static int parse_version_number(const char *s)
140 int major, minor, rev;
146 major = strtol(cp, &endptr, 10);
147 if (cp == endptr || *endptr != '.')
150 minor = strtol(cp, &endptr, 10);
151 if (cp == endptr || *endptr != '.')
154 rev = strtol(cp, &endptr, 10);
157 return ((((major * 256) + minor) * 256) + rev);
161 * Helper function for read_bb_file and test_disk
163 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
165 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
170 * Reads the bad blocks list from a file
172 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
173 const char *bad_blocks_file)
178 f = fopen(bad_blocks_file, "r");
180 com_err("read_bad_blocks_file", errno,
181 _("while trying to open %s"), bad_blocks_file);
184 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
187 com_err("ext2fs_read_bb_FILE", retval,
188 _("while reading in list of bad blocks from file"));
194 * Runs the badblocks program to test the disk
196 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
202 sprintf(buf, "badblocks -b %d -X %s%s%s %u", fs->blocksize,
203 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
204 fs->device_name, fs->super->s_blocks_count-1);
206 printf(_("Running command: %s\n"), buf);
209 com_err("popen", errno,
210 _("while trying to run '%s'"), buf);
213 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
216 com_err("ext2fs_read_bb_FILE", retval,
217 _("while processing list of bad blocks from program"));
222 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
226 unsigned must_be_good;
228 badblocks_iterate bb_iter;
238 * The primary superblock and group descriptors *must* be
239 * good; if not, abort.
241 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
242 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
243 if (ext2fs_badblocks_list_test(bb_list, i)) {
244 fprintf(stderr, _("Block %d in primary "
245 "superblock/group descriptor area bad.\n"), i);
246 fprintf(stderr, _("Blocks %u through %u must be good "
247 "in order to build a filesystem.\n"),
248 fs->super->s_first_data_block, must_be_good);
249 fputs(_("Aborting....\n"), stderr);
255 * See if any of the bad blocks are showing up in the backup
256 * superblocks and/or group descriptors. If so, issue a
257 * warning and adjust the block counts appropriately.
259 group_block = fs->super->s_first_data_block +
260 fs->super->s_blocks_per_group;
262 for (i = 1; i < fs->group_desc_count; i++) {
264 for (j=0; j < fs->desc_blocks+1; j++) {
265 if (ext2fs_badblocks_list_test(bb_list,
269 _("Warning: the backup superblock/group descriptors at block %u contain\n"
273 group = ext2fs_group_of_blk(fs, group_block+j);
274 fs->group_desc[group].bg_free_blocks_count++;
275 ext2fs_group_desc_csum_set(fs, group);
276 fs->super->s_free_blocks_count++;
279 group_block += fs->super->s_blocks_per_group;
283 * Mark all the bad blocks as used...
285 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
287 com_err("ext2fs_badblocks_list_iterate_begin", retval,
288 _("while marking bad blocks as used"));
291 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
292 ext2fs_mark_block_bitmap(fs->block_map, blk);
293 ext2fs_badblocks_list_iterate_end(bb_iter);
297 * These functions implement a generalized progress meter.
299 struct progress_struct {
306 static void progress_init(struct progress_struct *progress,
307 const char *label,__u32 max)
311 memset(progress, 0, sizeof(struct progress_struct));
316 * Figure out how many digits we need
319 sprintf(progress->format, "%%%dd/%%%dld", i, i);
320 memset(progress->backup, '\b', sizeof(progress->backup)-1);
321 progress->backup[sizeof(progress->backup)-1] = 0;
322 if ((2*i)+1 < (int) sizeof(progress->backup))
323 progress->backup[(2*i)+1] = 0;
326 progress->skip_progress = 0;
327 if (getenv("MKE2FS_SKIP_PROGRESS"))
328 progress->skip_progress++;
330 fputs(label, stdout);
334 static void progress_update(struct progress_struct *progress, __u32 val)
336 if ((progress->format[0] == 0) || progress->skip_progress)
338 printf(progress->format, val, progress->max);
339 fputs(progress->backup, stdout);
342 static void progress_close(struct progress_struct *progress)
344 if (progress->format[0] == 0)
346 fputs(_("done \n"), stdout);
349 static void write_inode_tables(ext2_filsys fs, int lazy_flag)
355 struct progress_struct progress;
358 memset(&progress, 0, sizeof(progress));
360 progress_init(&progress, _("Writing inode tables: "),
361 fs->group_desc_count);
363 for (i = 0; i < fs->group_desc_count; i++) {
364 progress_update(&progress, i);
366 blk = fs->group_desc[i].bg_inode_table;
367 num = fs->inode_blocks_per_group;
370 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
371 num = ((((fs->super->s_inodes_per_group -
372 fs->group_desc[i].bg_itable_unused) *
373 EXT2_INODE_SIZE(fs->super)) +
374 EXT2_BLOCK_SIZE(fs->super) - 1) /
375 EXT2_BLOCK_SIZE(fs->super));
377 /* The kernel doesn't need to zero the itable blocks */
378 fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
379 ext2fs_group_desc_csum_set(fs, i);
381 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
383 fprintf(stderr, _("\nCould not write %d "
384 "blocks in inode table starting at %u: %s\n"),
385 num, blk, error_message(retval));
389 if (sync_kludge == 1)
391 else if ((i % sync_kludge) == 0)
395 ext2fs_zero_blocks(0, 0, 0, 0, 0);
396 progress_close(&progress);
399 static void create_root_dir(ext2_filsys fs)
402 struct ext2_inode inode;
405 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
407 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
411 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
413 com_err("ext2fs_read_inode", retval,
414 _("while reading root inode"));
419 ext2fs_set_i_uid_high(inode, uid >> 16);
423 ext2fs_set_i_gid_high(inode, gid >> 16);
425 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
427 com_err("ext2fs_write_inode", retval,
428 _("while setting root inode ownership"));
434 static void create_lost_and_found(ext2_filsys fs)
436 unsigned int lpf_size = 0;
439 const char *name = "lost+found";
443 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
445 com_err("ext2fs_mkdir", retval,
446 _("while creating /lost+found"));
450 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
452 com_err("ext2_lookup", retval,
453 _("while looking up /lost+found"));
457 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
458 /* Ensure that lost+found is at least 2 blocks, so we always
459 * test large empty blocks for big-block filesystems. */
460 if ((lpf_size += fs->blocksize) >= 16*1024 &&
461 lpf_size >= 2 * fs->blocksize)
463 retval = ext2fs_expand_dir(fs, ino);
465 com_err("ext2fs_expand_dir", retval,
466 _("while expanding /lost+found"));
472 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
476 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
477 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
478 retval = ext2fs_update_bb_inode(fs, bb_list);
480 com_err("ext2fs_update_bb_inode", retval,
481 _("while setting bad block inode"));
487 static void reserve_inodes(ext2_filsys fs)
491 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
492 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
493 ext2fs_mark_ib_dirty(fs);
496 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
497 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
498 #define BSD_LABEL_OFFSET 64
500 static void zap_sector(ext2_filsys fs, int sect, int nsect)
506 buf = malloc(512*nsect);
508 printf(_("Out of memory erasing sectors %d-%d\n"),
509 sect, sect + nsect - 1);
514 /* Check for a BSD disklabel, and don't erase it if so */
515 retval = io_channel_read_blk(fs->io, 0, -512, buf);
518 _("Warning: could not read block 0: %s\n"),
519 error_message(retval));
521 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
522 if ((*magic == BSD_DISKMAGIC) ||
523 (*magic == BSD_MAGICDISK))
528 memset(buf, 0, 512*nsect);
529 io_channel_set_blksize(fs->io, 512);
530 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
531 io_channel_set_blksize(fs->io, fs->blocksize);
534 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
535 sect, error_message(retval));
538 static void create_journal_dev(ext2_filsys fs)
540 struct progress_struct progress;
544 int c, count, err_count;
546 retval = ext2fs_create_journal_superblock(fs,
547 fs->super->s_blocks_count, 0, &buf);
549 com_err("create_journal_dev", retval,
550 _("while initializing journal superblock"));
554 memset(&progress, 0, sizeof(progress));
556 progress_init(&progress, _("Zeroing journal device: "),
557 fs->super->s_blocks_count);
560 count = fs->super->s_blocks_count;
566 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
568 com_err("create_journal_dev", retval,
569 _("while zeroing journal device "
570 "(block %u, count %d)"),
576 progress_update(&progress, blk);
578 ext2fs_zero_blocks(0, 0, 0, 0, 0);
580 retval = io_channel_write_blk(fs->io,
581 fs->super->s_first_data_block+1,
584 com_err("create_journal_dev", retval,
585 _("while writing journal superblock"));
588 progress_close(&progress);
591 static void show_stats(ext2_filsys fs)
593 struct ext2_super_block *s = fs->super;
600 if (fs_param.s_blocks_count != s->s_blocks_count)
601 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
602 fs_param.s_blocks_count - s->s_blocks_count);
604 memset(buf, 0, sizeof(buf));
605 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
606 printf(_("Filesystem label=%s\n"), buf);
607 fputs(_("OS type: "), stdout);
608 os = e2p_os2string(fs->super->s_creator_os);
612 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
613 s->s_log_block_size);
614 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
616 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
618 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
620 100.0 * s->s_r_blocks_count / s->s_blocks_count);
621 printf(_("First data block=%u\n"), s->s_first_data_block);
622 if (s->s_reserved_gdt_blocks)
623 printf(_("Maximum filesystem blocks=%lu\n"),
624 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
625 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
626 if (fs->group_desc_count > 1)
627 printf(_("%u block groups\n"), fs->group_desc_count);
629 printf(_("%u block group\n"), fs->group_desc_count);
630 printf(_("%u blocks per group, %u fragments per group\n"),
631 s->s_blocks_per_group, s->s_frags_per_group);
632 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
634 if (fs->group_desc_count == 1) {
639 printf(_("Superblock backups stored on blocks: "));
640 group_block = s->s_first_data_block;
642 for (i = 1; i < fs->group_desc_count; i++) {
643 group_block += s->s_blocks_per_group;
644 if (!ext2fs_bg_has_super(fs, i))
648 need = int_log10(group_block) + 2;
649 if (need > col_left) {
654 printf("%u", group_block);
660 * Set the S_CREATOR_OS field. Return true if OS is known,
663 static int set_os(struct ext2_super_block *sb, char *os)
666 sb->s_creator_os = atoi (os);
667 else if (strcasecmp(os, "linux") == 0)
668 sb->s_creator_os = EXT2_OS_LINUX;
669 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
670 sb->s_creator_os = EXT2_OS_HURD;
671 else if (strcasecmp(os, "freebsd") == 0)
672 sb->s_creator_os = EXT2_OS_FREEBSD;
673 else if (strcasecmp(os, "lites") == 0)
674 sb->s_creator_os = EXT2_OS_LITES;
680 #define PATH_SET "PATH=/sbin"
682 static void parse_extended_opts(struct ext2_super_block *param,
685 char *buf, *token, *next, *p, *arg, *badopt = 0;
693 _("Couldn't allocate memory to parse options!\n"));
697 for (token = buf; token && *token; token = next) {
698 p = strchr(token, ',');
704 arg = strchr(token, '=');
709 if (strcmp(token, "stride") == 0) {
715 param->s_raid_stride = strtoul(arg, &p, 0);
716 if (*p || (param->s_raid_stride == 0)) {
718 _("Invalid stride parameter: %s\n"),
723 } else if (strcmp(token, "stripe-width") == 0 ||
724 strcmp(token, "stripe_width") == 0) {
730 param->s_raid_stripe_width = strtoul(arg, &p, 0);
731 if (*p || (param->s_raid_stripe_width == 0)) {
733 _("Invalid stripe-width parameter: %s\n"),
738 } else if (!strcmp(token, "resize")) {
739 unsigned long resize, bpg, rsv_groups;
740 unsigned long group_desc_count, desc_blocks;
741 unsigned int gdpb, blocksize;
750 resize = parse_num_blocks(arg,
751 param->s_log_block_size);
755 _("Invalid resize parameter: %s\n"),
760 if (resize <= param->s_blocks_count) {
762 _("The resize maximum must be greater "
763 "than the filesystem size.\n"));
768 blocksize = EXT2_BLOCK_SIZE(param);
769 bpg = param->s_blocks_per_group;
772 gdpb = EXT2_DESC_PER_BLOCK(param);
774 ext2fs_div_ceil(param->s_blocks_count, bpg);
775 desc_blocks = (group_desc_count +
777 rsv_groups = ext2fs_div_ceil(resize, bpg);
778 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
780 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
781 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
784 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
786 _("On-line resizing not supported with revision 0 filesystems\n"));
790 param->s_feature_compat |=
791 EXT2_FEATURE_COMPAT_RESIZE_INODE;
793 param->s_reserved_gdt_blocks = rsv_gdb;
795 } else if (!strcmp(token, "test_fs")) {
796 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
797 } else if (!strcmp(token, "lazy_itable_init")) {
799 lazy_itable_init = strtoul(arg, &p, 0);
801 lazy_itable_init = 1;
808 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
809 "Extended options are separated by commas, "
810 "and may take an argument which\n"
811 "\tis set off by an equals ('=') sign.\n\n"
812 "Valid extended options are:\n"
813 "\tstride=<RAID per-disk data chunk in blocks>\n"
814 "\tstripe-width=<RAID stride * data disks in blocks>\n"
815 "\tresize=<resize maximum size in blocks>\n"
816 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
818 badopt ? badopt : "");
822 if (param->s_raid_stride &&
823 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
824 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
825 "multiple of stride %u.\n\n"),
826 param->s_raid_stripe_width, param->s_raid_stride);
831 static __u32 ok_features[3] = {
833 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
834 EXT2_FEATURE_COMPAT_RESIZE_INODE |
835 EXT2_FEATURE_COMPAT_DIR_INDEX |
836 EXT2_FEATURE_COMPAT_EXT_ATTR,
838 EXT2_FEATURE_INCOMPAT_FILETYPE|
839 EXT3_FEATURE_INCOMPAT_EXTENTS|
840 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
841 EXT2_FEATURE_INCOMPAT_META_BG|
842 EXT4_FEATURE_INCOMPAT_FLEX_BG,
844 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
845 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
846 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
847 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
848 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
849 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
853 static void syntax_err_report(const char *filename, long err, int line_num)
856 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
857 filename, line_num, error_message(err));
861 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
863 static void edit_feature(const char *str, __u32 *compat_array)
868 if (e2p_edit_feature(str, compat_array, ok_features)) {
869 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
881 static errcode_t init_list(struct str_list *sl)
885 sl->list = malloc((sl->max+1) * sizeof(char *));
892 static errcode_t push_string(struct str_list *sl, const char *str)
896 if (sl->num >= sl->max) {
898 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
903 sl->list[sl->num] = malloc(strlen(str)+1);
904 if (sl->list[sl->num] == 0)
906 strcpy(sl->list[sl->num], str);
908 sl->list[sl->num] = 0;
912 static void print_str_list(char **list)
916 for (cpp = list; *cpp; cpp++) {
917 printf("'%s'", *cpp);
924 static char **parse_fs_type(const char *fs_type,
925 const char *usage_types,
926 struct ext2_super_block *fs_param,
929 const char *ext_type = 0;
931 char *profile_type = 0;
933 const char *size_type;
934 struct str_list list;
938 if (init_list(&list))
941 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
942 !strcasecmp(creator_os, "hurd")))
949 else if (!strcmp(program_name, "mke3fs"))
952 ext_type = strrchr(progname, '/');
958 if (!strncmp(ext_type, "mkfs.", 5)) {
960 if (ext_type[0] == 0)
967 profile_get_string(profile, "defaults", "fs_type", 0,
968 "ext2", &profile_type);
969 ext_type = profile_type;
970 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
974 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
975 !strcmp(ext_type, "ext4dev")) {
976 profile_get_string(profile, "fs_types", ext_type, "features",
979 printf(_("\nWarning! Your mke2fs.conf file does "
980 "not define the %s filesystem type.\n"),
982 printf(_("You probably need to install an updated "
983 "mke2fs.conf file.\n\n"));
988 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
989 if (fs_param->s_blocks_count < 3 * meg)
990 size_type = "floppy";
991 else if (fs_param->s_blocks_count < 512 * meg)
994 size_type = "default";
997 usage_types = size_type;
999 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
1005 strcpy(parse_str, usage_types);
1010 push_string(&list, ext_type);
1013 t = strchr(cp, ',');
1018 push_string(&list, cp);
1029 push_string(&list, "hurd");
1033 static char *get_string_from_profile(char **fs_types, const char *opt,
1034 const char *def_val)
1039 for (i=0; fs_types[i]; i++);
1040 for (i-=1; i >=0 ; i--) {
1041 profile_get_string(profile, "fs_types", fs_types[i],
1046 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
1050 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1055 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1056 for (cpp = fs_types; *cpp; cpp++)
1057 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1061 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1066 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1067 for (cpp = fs_types; *cpp; cpp++)
1068 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1072 extern const char *mke2fs_default_profile;
1073 static const char *default_files[] = { "<default>", 0 };
1075 static void PRS(int argc, char *argv[])
1081 int inode_ratio = 0;
1083 unsigned long flex_bg_size = 0;
1084 double reserved_ratio = 5.0;
1085 int sector_size = 0;
1086 int show_version_only = 0;
1087 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1089 char * oldpath = getenv("PATH");
1090 char * extended_opts = 0;
1091 const char * fs_type = 0;
1092 const char * usage_types = 0;
1098 int s_opt = -1, r_opt = -1;
1099 char *fs_features = 0;
1102 int pathlen = sizeof(PATH_SET) + 1;
1105 pathlen += strlen(oldpath);
1106 newpath = malloc(pathlen);
1107 strcpy(newpath, PATH_SET);
1109 /* Update our PATH to include /sbin */
1111 strcat (newpath, ":");
1112 strcat (newpath, oldpath);
1116 tmp = getenv("MKE2FS_SYNC");
1118 sync_kludge = atoi(tmp);
1120 /* Determine the system page size if possible */
1122 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1123 #define _SC_PAGESIZE _SC_PAGE_SIZE
1126 sysval = sysconf(_SC_PAGESIZE);
1128 sys_page_size = sysval;
1129 #endif /* _SC_PAGESIZE */
1130 #endif /* HAVE_SYSCONF */
1132 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1134 profile_set_syntax_err_cb(syntax_err_report);
1135 retval = profile_init(config_fn, &profile);
1136 if (retval == ENOENT) {
1137 profile_init(default_files, &profile);
1138 profile_set_default(profile, mke2fs_default_profile);
1141 setbuf(stdout, NULL);
1142 setbuf(stderr, NULL);
1143 add_error_table(&et_ext2_error_table);
1144 add_error_table(&et_prof_error_table);
1145 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1146 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1153 linux_version_code = parse_version_number(ut.release);
1154 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1155 fs_param.s_rev_level = 0;
1158 if (argc && *argv) {
1159 program_name = get_progname(*argv);
1161 /* If called as mkfs.ext3, create a journal inode */
1162 if (!strcmp(program_name, "mkfs.ext3") ||
1163 !strcmp(program_name, "mke3fs"))
1167 while ((c = getopt (argc, argv,
1168 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:U:V")) != EOF) {
1171 blocksize = strtol(optarg, &tmp, 0);
1172 b = (blocksize > 0) ? blocksize : -blocksize;
1173 if (b < EXT2_MIN_BLOCK_SIZE ||
1174 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1175 com_err(program_name, 0,
1176 _("invalid block size - %s"), optarg);
1179 if (blocksize > 4096)
1180 fprintf(stderr, _("Warning: blocksize %d not "
1181 "usable on most systems.\n"),
1184 fs_param.s_log_block_size =
1185 int_log2(blocksize >>
1186 EXT2_MIN_BLOCK_LOG_SIZE);
1188 case 'c': /* Check for bad blocks */
1192 size = strtoul(optarg, &tmp, 0);
1193 if (size < EXT2_MIN_BLOCK_SIZE ||
1194 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1195 com_err(program_name, 0,
1196 _("invalid fragment size - %s"),
1200 fs_param.s_log_frag_size =
1201 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1202 fprintf(stderr, _("Warning: fragments not supported. "
1203 "Ignoring -f option\n"));
1206 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1208 com_err(program_name, 0,
1209 _("Illegal number for blocks per group"));
1212 if ((fs_param.s_blocks_per_group % 8) != 0) {
1213 com_err(program_name, 0,
1214 _("blocks per group must be multiple of 8"));
1219 flex_bg_size = strtoul(optarg, &tmp, 0);
1221 com_err(program_name, 0,
1222 _("Illegal number for flex_bg size"));
1225 if (flex_bg_size < 2 ||
1226 (flex_bg_size & (flex_bg_size-1)) != 0) {
1227 com_err(program_name, 0,
1228 _("flex_bg size must be a power of 2"));
1233 inode_ratio = strtoul(optarg, &tmp, 0);
1234 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1235 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1237 com_err(program_name, 0,
1238 _("invalid inode ratio %s (min %d/max %d)"),
1239 optarg, EXT2_MIN_BLOCK_SIZE,
1240 EXT2_MAX_BLOCK_SIZE);
1245 parse_journal_opts(optarg);
1252 bad_blocks_filename = malloc(strlen(optarg)+1);
1253 if (!bad_blocks_filename) {
1254 com_err(program_name, ENOMEM,
1255 _("in malloc for bad_blocks_filename"));
1258 strcpy(bad_blocks_filename, optarg);
1261 reserved_ratio = strtod(optarg, &tmp);
1262 if ( *tmp || reserved_ratio > 50 ||
1263 reserved_ratio < 0) {
1264 com_err(program_name, 0,
1265 _("invalid reserved blocks percent - %s"),
1274 creator_os = optarg;
1280 r_opt = strtoul(optarg, &tmp, 0);
1282 com_err(program_name, 0,
1283 _("bad revision level - %s"), optarg);
1286 fs_param.s_rev_level = r_opt;
1288 case 's': /* deprecated */
1289 s_opt = atoi(optarg);
1292 inode_size = strtoul(optarg, &tmp, 0);
1294 com_err(program_name, 0,
1295 _("invalid inode size - %s"), optarg);
1306 volume_label = optarg;
1312 num_inodes = strtoul(optarg, &tmp, 0);
1314 com_err(program_name, 0,
1315 _("bad num inodes - %s"), optarg);
1320 fs_features = optarg;
1324 extended_opts = optarg;
1333 usage_types = optarg;
1339 /* Print version number and exit */
1340 show_version_only++;
1346 if ((optind == argc) && !show_version_only)
1348 device_name = argv[optind++];
1350 if (!quiet || show_version_only)
1351 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1354 if (show_version_only) {
1355 fprintf(stderr, _("\tUsing %s\n"),
1356 error_message(EXT2_ET_BASE));
1361 * If there's no blocksize specified and there is a journal
1362 * device, use it to figure out the blocksize
1364 if (blocksize <= 0 && journal_device) {
1368 #ifdef CONFIG_TESTIO_DEBUG
1369 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1370 io_ptr = test_io_manager;
1371 test_io_backing_manager = unix_io_manager;
1374 io_ptr = unix_io_manager;
1375 retval = ext2fs_open(journal_device,
1376 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1379 com_err(program_name, retval,
1380 _("while trying to open journal device %s\n"),
1384 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1385 com_err(program_name, 0,
1386 _("Journal dev blocksize (%d) smaller than "
1387 "minimum blocksize %d\n"), jfs->blocksize,
1391 blocksize = jfs->blocksize;
1392 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1393 fs_param.s_log_block_size =
1394 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1398 if (blocksize > sys_page_size) {
1400 com_err(program_name, 0,
1401 _("%d-byte blocks too big for system (max %d)"),
1402 blocksize, sys_page_size);
1405 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1406 "(max %d), forced to continue\n"),
1407 blocksize, sys_page_size);
1409 if (optind < argc) {
1410 fs_param.s_blocks_count = parse_num_blocks(argv[optind++],
1411 fs_param.s_log_block_size);
1412 if (!fs_param.s_blocks_count) {
1413 com_err(program_name, 0, _("invalid blocks count - %s"),
1422 check_plausibility(device_name);
1423 check_mount(device_name, force, _("filesystem"));
1425 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1427 if (noaction && fs_param.s_blocks_count) {
1428 dev_size = fs_param.s_blocks_count;
1432 retval = ext2fs_get_device_size(device_name,
1433 EXT2_BLOCK_SIZE(&fs_param),
1435 if ((retval == EFBIG) &&
1437 (fs_param.s_log_block_size == 0)) {
1438 fs_param.s_log_block_size = 2;
1444 if (retval == EFBIG) {
1445 blk64_t big_dev_size;
1447 if (blocksize < 4096) {
1448 fs_param.s_log_block_size = 2;
1451 retval = ext2fs_get_device_size2(device_name,
1452 EXT2_BLOCK_SIZE(&fs_param), &big_dev_size);
1454 goto get_size_failure;
1455 if (big_dev_size == (1ULL << 32)) {
1456 dev_size = (blk_t) (big_dev_size - 1);
1459 fprintf(stderr, _("%s: Size of device %s too big "
1460 "to be expressed in 32 bits\n\t"
1461 "using a blocksize of %d.\n"),
1462 program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1466 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1467 com_err(program_name, retval,
1468 _("while trying to determine filesystem size"));
1472 if (!fs_param.s_blocks_count) {
1473 if (retval == EXT2_ET_UNIMPLEMENTED) {
1474 com_err(program_name, 0,
1475 _("Couldn't determine device size; you "
1476 "must specify\nthe size of the "
1480 if (dev_size == 0) {
1481 com_err(program_name, 0,
1482 _("Device size reported to be zero. "
1483 "Invalid partition specified, or\n\t"
1484 "partition table wasn't reread "
1485 "after running fdisk, due to\n\t"
1486 "a modified partition being busy "
1487 "and in use. You may need to reboot\n\t"
1488 "to re-read your partition table.\n"
1492 fs_param.s_blocks_count = dev_size;
1493 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param))
1494 fs_param.s_blocks_count &= ~((sys_page_size /
1495 EXT2_BLOCK_SIZE(&fs_param))-1);
1498 } else if (!force && (fs_param.s_blocks_count > dev_size)) {
1499 com_err(program_name, 0,
1500 _("Filesystem larger than apparent device size."));
1504 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1506 fprintf(stderr, _("Failed to parse fs types list\n"));
1510 /* Figure out what features should be enabled */
1513 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1514 tmp = get_string_from_profile(fs_types, "base_features",
1515 "sparse_super,filetype,resize_inode,dir_index");
1516 edit_feature(tmp, &fs_param.s_feature_compat);
1519 for (cpp = fs_types; *cpp; cpp++) {
1521 profile_get_string(profile, "fs_types", *cpp,
1522 "features", "", &tmp);
1524 edit_feature(tmp, &fs_param.s_feature_compat);
1527 tmp = get_string_from_profile(fs_types, "default_features",
1530 edit_feature(fs_features ? fs_features : tmp,
1531 &fs_param.s_feature_compat);
1534 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1535 fs_types[0] = strdup("journal");
1540 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1541 print_str_list(fs_types);
1544 if (r_opt == EXT2_GOOD_OLD_REV &&
1545 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1546 fs_param.s_feature_ro_compat)) {
1547 fprintf(stderr, _("Filesystem features not supported "
1548 "with revision 0 filesystems\n"));
1553 if (r_opt == EXT2_GOOD_OLD_REV) {
1554 fprintf(stderr, _("Sparse superblocks not supported "
1555 "with revision 0 filesystems\n"));
1558 fs_param.s_feature_ro_compat |=
1559 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1560 } else if (s_opt == 0)
1561 fs_param.s_feature_ro_compat &=
1562 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1564 if (journal_size != 0) {
1565 if (r_opt == EXT2_GOOD_OLD_REV) {
1566 fprintf(stderr, _("Journals not supported "
1567 "with revision 0 filesystems\n"));
1570 fs_param.s_feature_compat |=
1571 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1574 if (fs_param.s_feature_incompat &
1575 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1577 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1578 fs_param.s_feature_compat = 0;
1579 fs_param.s_feature_ro_compat = 0;
1582 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1583 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1584 fprintf(stderr, _("The resize_inode and meta_bg features "
1585 "are not compatible.\n"
1586 "They can not be both enabled "
1587 "simultaneously.\n"));
1591 /* Set first meta blockgroup via an environment variable */
1592 /* (this is mostly for debugging purposes) */
1593 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1594 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1595 fs_param.s_first_meta_bg = atoi(tmp);
1597 /* Get the hardware sector size, if available */
1598 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1600 com_err(program_name, retval,
1601 _("while trying to determine hardware sector size"));
1605 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1606 sector_size = atoi(tmp);
1608 if (blocksize <= 0) {
1609 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1611 if (use_bsize == -1) {
1612 use_bsize = sys_page_size;
1613 if ((linux_version_code < (2*65536 + 6*256)) &&
1617 if (sector_size && use_bsize < sector_size)
1618 use_bsize = sector_size;
1619 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1620 use_bsize = -blocksize;
1621 blocksize = use_bsize;
1622 fs_param.s_blocks_count /= blocksize / 1024;
1625 if (inode_ratio == 0) {
1626 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1628 if (inode_ratio < blocksize)
1629 inode_ratio = blocksize;
1632 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1633 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1635 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1637 lazy_itable_init = get_bool_from_profile(fs_types,
1638 "lazy_itable_init", 0);
1640 /* Get options from profile */
1641 for (cpp = fs_types; *cpp; cpp++) {
1643 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1645 parse_extended_opts(&fs_param, tmp);
1650 parse_extended_opts(&fs_param, extended_opts);
1652 /* Since sparse_super is the default, we would only have a problem
1653 * here if it was explicitly disabled.
1655 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1656 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1657 com_err(program_name, 0,
1658 _("reserved online resize blocks not supported "
1659 "on non-sparse filesystem"));
1663 if (fs_param.s_blocks_per_group) {
1664 if (fs_param.s_blocks_per_group < 256 ||
1665 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1666 com_err(program_name, 0,
1667 _("blocks per group count out of range"));
1672 if (inode_size == 0)
1673 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1674 if (!flex_bg_size && (fs_param.s_feature_incompat &
1675 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1676 flex_bg_size = get_int_from_profile(fs_types,
1677 "flex_bg_size", 16);
1679 if (!(fs_param.s_feature_incompat &
1680 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1681 com_err(program_name, 0,
1682 _("Flex_bg feature not enabled, so "
1683 "flex_bg size may not be specified"));
1686 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1689 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1690 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1691 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1692 inode_size & (inode_size - 1)) {
1693 com_err(program_name, 0,
1694 _("invalid inode size %d (min %d/max %d)"),
1695 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1699 fs_param.s_inode_size = inode_size;
1702 /* Make sure number of inodes specified will fit in 32 bits */
1703 if (num_inodes == 0) {
1704 unsigned long long n;
1705 n = (unsigned long long) fs_param.s_blocks_count * blocksize / inode_ratio;
1707 com_err(program_name, 0,
1708 _("too many inodes (%llu), raise inode ratio?"), n);
1711 } else if (num_inodes > ~0U) {
1712 com_err(program_name, 0,
1713 _("too many inodes (%llu), specify < 2^32 inodes"),
1718 * Calculate number of inodes based on the inode ratio
1720 fs_param.s_inodes_count = num_inodes ? num_inodes :
1721 ((__u64) fs_param.s_blocks_count * blocksize)
1724 if ((((long long)fs_param.s_inodes_count) *
1725 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1726 (((long long)fs_param.s_blocks_count) *
1727 EXT2_BLOCK_SIZE(&fs_param))) {
1728 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1729 "(%u) too big for a\n\t"
1730 "filesystem with %lu blocks, "
1731 "specify higher inode_ratio (-i)\n\t"
1732 "or lower inode count (-N).\n"),
1733 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1734 fs_param.s_inodes_count,
1735 (unsigned long) fs_param.s_blocks_count);
1740 * Calculate number of blocks to reserve
1742 fs_param.s_r_blocks_count = (unsigned int) (reserved_ratio *
1743 fs_param.s_blocks_count / 100.0);
1746 static int should_do_undo(const char *name)
1751 struct ext2_super_block super;
1752 io_manager manager = unix_io_manager;
1753 int csum_flag, force_undo;
1755 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1756 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1757 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1758 if (!force_undo && (!csum_flag || !lazy_itable_init))
1761 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1764 * We don't handle error cases instead we
1765 * declare that the file system doesn't exist
1766 * and let the rest of mke2fs take care of
1773 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1774 retval = io_channel_read_blk(channel, 1, -SUPERBLOCK_SIZE, &super);
1780 #if defined(WORDS_BIGENDIAN)
1781 s_magic = ext2fs_swab16(super.s_magic);
1783 s_magic = super.s_magic;
1786 if (s_magic == EXT2_SUPER_MAGIC)
1790 io_channel_close(channel);
1797 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1799 errcode_t retval = 0;
1800 char *tdb_dir, *tdb_file;
1801 char *device_name, *tmp_name;
1804 * Configuration via a conf file would be
1807 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1809 profile_get_string(profile, "defaults",
1810 "undo_dir", 0, "/var/lib/e2fsprogs",
1813 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1814 access(tdb_dir, W_OK))
1817 tmp_name = strdup(name);
1820 com_err(program_name, ENOMEM,
1821 _("Couldn't allocate memory for tdb filename\n"));
1824 device_name = basename(tmp_name);
1825 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1828 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1830 if (!access(tdb_file, F_OK)) {
1831 if (unlink(tdb_file) < 0) {
1833 com_err(program_name, retval,
1834 _("while trying to delete %s"),
1841 set_undo_io_backing_manager(*io_ptr);
1842 *io_ptr = undo_io_manager;
1843 set_undo_io_backup_file(tdb_file);
1844 printf(_("Overwriting existing filesystem; this can be undone "
1845 "using the command:\n"
1846 " e2undo %s %s\n\n"), tdb_file, name);
1853 int main (int argc, char *argv[])
1855 errcode_t retval = 0;
1857 badblocks_list bb_list = 0;
1858 unsigned int journal_blocks;
1862 char tdb_string[40];
1866 setlocale(LC_MESSAGES, "");
1867 setlocale(LC_CTYPE, "");
1868 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1869 textdomain(NLS_CAT_NAME);
1873 #ifdef CONFIG_TESTIO_DEBUG
1874 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1875 io_ptr = test_io_manager;
1876 test_io_backing_manager = unix_io_manager;
1879 io_ptr = unix_io_manager;
1881 if (should_do_undo(device_name)) {
1882 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1888 * Initialize the superblock....
1890 retval = ext2fs_initialize(device_name, EXT2_FLAG_EXCLUSIVE, &fs_param,
1893 com_err(device_name, retval, _("while setting up superblock"));
1896 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1897 32768 : fs->blocksize * 8);
1898 io_channel_set_options(fs->io, tdb_string);
1900 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1901 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1903 if ((fs_param.s_feature_incompat &
1904 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
1905 (fs_param.s_feature_ro_compat &
1906 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1907 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
1908 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
1909 fs->super->s_kbytes_written = 1;
1912 * Wipe out the old on-disk superblock
1915 zap_sector(fs, 2, 6);
1918 * Parse or generate a UUID for the filesystem
1921 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
1922 com_err(device_name, 0, "could not parse UUID: %s\n",
1927 uuid_generate(fs->super->s_uuid);
1930 * Initialize the directory index variables
1932 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
1934 hash_alg = e2p_string2hash(hash_alg_str);
1935 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
1937 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1940 * Add "jitter" to the superblock's check interval so that we
1941 * don't check all the filesystems at the same time. We use a
1942 * kludgy hack of using the UUID to derive a random jitter value.
1944 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1945 val += fs->super->s_uuid[i];
1946 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1949 * Override the creator OS, if applicable
1951 if (creator_os && !set_os(fs->super, creator_os)) {
1952 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1957 * For the Hurd, we will turn off filetype since it doesn't
1960 if (fs->super->s_creator_os == EXT2_OS_HURD)
1961 fs->super->s_feature_incompat &=
1962 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1965 * Set the volume label...
1968 memset(fs->super->s_volume_name, 0,
1969 sizeof(fs->super->s_volume_name));
1970 strncpy(fs->super->s_volume_name, volume_label,
1971 sizeof(fs->super->s_volume_name));
1975 * Set the last mount directory
1978 memset(fs->super->s_last_mounted, 0,
1979 sizeof(fs->super->s_last_mounted));
1980 strncpy(fs->super->s_last_mounted, mount_dir,
1981 sizeof(fs->super->s_last_mounted));
1984 if (!quiet || noaction)
1990 if (fs->super->s_feature_incompat &
1991 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1992 create_journal_dev(fs);
1993 exit(ext2fs_close(fs) ? 1 : 0);
1996 if (bad_blocks_filename)
1997 read_bb_file(fs, &bb_list, bad_blocks_filename);
1999 test_disk(fs, &bb_list);
2001 handle_bad_blocks(fs, bb_list);
2002 fs->stride = fs_stride = fs->super->s_raid_stride;
2003 retval = ext2fs_allocate_tables(fs);
2005 com_err(program_name, retval,
2006 _("while trying to allocate filesystem tables"));
2010 fs->super->s_state |= EXT2_ERROR_FS;
2011 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2013 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2014 unsigned int rsv = 65536 / fs->blocksize;
2015 unsigned long blocks = fs->super->s_blocks_count;
2016 unsigned long start;
2019 #ifdef ZAP_BOOTBLOCK
2020 zap_sector(fs, 0, 2);
2024 * Wipe out any old MD RAID (or other) metadata at the end
2025 * of the device. This will also verify that the device is
2026 * as large as we think. Be careful with very small devices.
2028 start = (blocks & ~(rsv - 1));
2032 retval = ext2fs_zero_blocks(fs, start, blocks - start,
2036 com_err(program_name, retval,
2037 _("while zeroing block %u at end of filesystem"),
2040 write_inode_tables(fs, lazy_itable_init);
2041 create_root_dir(fs);
2042 create_lost_and_found(fs);
2044 create_bad_block_inode(fs, bb_list);
2045 if (fs->super->s_feature_compat &
2046 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2047 retval = ext2fs_create_resize_inode(fs);
2049 com_err("ext2fs_create_resize_inode", retval,
2050 _("while reserving blocks for online resize"));
2056 if (journal_device) {
2060 check_plausibility(journal_device);
2061 check_mount(journal_device, force, _("journal"));
2063 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2064 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2065 fs->blocksize, unix_io_manager, &jfs);
2067 com_err(program_name, retval,
2068 _("while trying to open journal device %s\n"),
2073 printf(_("Adding journal to device %s: "),
2077 retval = ext2fs_add_journal_device(fs, jfs);
2079 com_err (program_name, retval,
2080 _("\n\twhile trying to add journal to device %s"),
2085 printf(_("done\n"));
2087 free(journal_device);
2088 } else if ((journal_size) ||
2089 (fs_param.s_feature_compat &
2090 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2091 journal_blocks = figure_journal_size(journal_size, fs);
2094 printf(_("Skipping journal creation in super-only mode\n"));
2095 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2099 if (!journal_blocks) {
2100 fs->super->s_feature_compat &=
2101 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2105 printf(_("Creating journal (%u blocks): "),
2109 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2112 com_err (program_name, retval,
2113 _("\n\twhile trying to create journal"));
2117 printf(_("done\n"));
2122 printf(_("Writing superblocks and "
2123 "filesystem accounting information: "));
2124 retval = ext2fs_flush(fs);
2127 _("\nWarning, had trouble writing out superblocks."));
2130 printf(_("done\n\n"));
2131 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2132 print_check_message(fs);
2134 val = ext2fs_close(fs);
2135 remove_error_table(&et_ext2_error_table);
2136 remove_error_table(&et_prof_error_table);
2137 profile_release(profile);
2138 return (retval || val) ? 1 : 0;