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 */
28 #include <sys/utsname.h>
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
52 #include <blkid/blkid.h>
54 #include "ext2fs/ext2_fs.h"
55 #include "ext2fs/ext2fsP.h"
56 #include "et/com_err.h"
57 #include "uuid/uuid.h"
59 #include "ext2fs/ext2fs.h"
63 #include "../version.h"
64 #include "nls-enable.h"
66 #define STRIDE_LENGTH 8
72 extern int isatty(int);
73 extern FILE *fpopen(const char *cmd, const char *mode);
75 const char * program_name = "mke2fs";
76 const char * device_name /* = NULL */;
78 /* Command line options */
88 char *bad_blocks_filename;
91 struct ext2_super_block fs_param;
97 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
102 int sys_page_size = 4096;
103 int linux_version_code = 0;
105 static void usage(void)
107 fprintf(stderr, _("Usage: %s [-c|-l filename] [-b block-size] "
108 "[-f fragment-size]\n\t[-i bytes-per-inode] [-I inode-size] "
109 "[-J journal-options]\n"
110 "\t[-G meta group size] [-N number-of-inodes]\n"
111 "\t[-m reserved-blocks-percentage] [-o creator-os]\n"
112 "\t[-g blocks-per-group] [-L volume-label] "
113 "[-M last-mounted-directory]\n\t[-O feature[,...]] "
114 "[-r fs-revision] [-E extended-option[,...]]\n"
115 "\t[-T fs-type] [-U UUID] [-jnqvFSV] device [blocks-count]\n"),
120 static int int_log2(int arg)
132 static int int_log10(unsigned int arg)
141 static int parse_version_number(const char *s)
143 int major, minor, rev;
149 major = strtol(cp, &endptr, 10);
150 if (cp == endptr || *endptr != '.')
153 minor = strtol(cp, &endptr, 10);
154 if (cp == endptr || *endptr != '.')
157 rev = strtol(cp, &endptr, 10);
160 return ((((major * 256) + minor) * 256) + rev);
164 * Helper function for read_bb_file and test_disk
166 static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
168 fprintf(stderr, _("Bad block %u out of range; ignored.\n"), blk);
173 * Reads the bad blocks list from a file
175 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
176 const char *bad_blocks_file)
181 f = fopen(bad_blocks_file, "r");
183 com_err("read_bad_blocks_file", errno,
184 _("while trying to open %s"), bad_blocks_file);
187 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
190 com_err("ext2fs_read_bb_FILE", retval,
191 _("while reading in list of bad blocks from file"));
197 * Runs the badblocks program to test the disk
199 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
205 sprintf(buf, "badblocks -b %d -X %s%s%s %llu", fs->blocksize,
206 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
207 fs->device_name, ext2fs_blocks_count(fs->super)-1);
209 printf(_("Running command: %s\n"), buf);
212 com_err("popen", errno,
213 _("while trying to run '%s'"), buf);
216 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
219 com_err("ext2fs_read_bb_FILE", retval,
220 _("while processing list of bad blocks from program"));
225 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
229 unsigned must_be_good;
231 badblocks_iterate bb_iter;
241 * The primary superblock and group descriptors *must* be
242 * good; if not, abort.
244 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
245 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
246 if (ext2fs_badblocks_list_test(bb_list, i)) {
247 fprintf(stderr, _("Block %d in primary "
248 "superblock/group descriptor area bad.\n"), i);
249 fprintf(stderr, _("Blocks %u through %u must be good "
250 "in order to build a filesystem.\n"),
251 fs->super->s_first_data_block, must_be_good);
252 fputs(_("Aborting....\n"), stderr);
258 * See if any of the bad blocks are showing up in the backup
259 * superblocks and/or group descriptors. If so, issue a
260 * warning and adjust the block counts appropriately.
262 group_block = fs->super->s_first_data_block +
263 fs->super->s_blocks_per_group;
265 for (i = 1; i < fs->group_desc_count; i++) {
267 for (j=0; j < fs->desc_blocks+1; j++) {
268 if (ext2fs_badblocks_list_test(bb_list,
272 _("Warning: the backup superblock/group descriptors at block %u contain\n"
276 group = ext2fs_group_of_blk(fs, group_block+j);
277 ext2fs_bg_free_blocks_count_set(fs, group, ext2fs_bg_free_blocks_count(fs, group) + 1);
278 ext2fs_group_desc_csum_set(fs, group);
279 ext2fs_free_blocks_count_add(fs->super, 1);
282 group_block += fs->super->s_blocks_per_group;
286 * Mark all the bad blocks as used...
288 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
290 com_err("ext2fs_badblocks_list_iterate_begin", retval,
291 _("while marking bad blocks as used"));
294 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
295 ext2fs_mark_block_bitmap2(fs->block_map, blk);
296 ext2fs_badblocks_list_iterate_end(bb_iter);
299 static void write_inode_tables(ext2_filsys fs, int lazy_flag)
305 struct ext2fs_numeric_progress_struct progress;
307 ext2fs_numeric_progress_init(fs, &progress,
308 _("Writing inode tables: "),
309 fs->group_desc_count);
311 for (i = 0; i < fs->group_desc_count; i++) {
312 ext2fs_numeric_progress_update(fs, &progress, i);
314 blk = ext2fs_inode_table_loc(fs, i);
315 num = fs->inode_blocks_per_group;
318 ipb = fs->blocksize / EXT2_INODE_SIZE(fs->super);
319 num = ((((fs->super->s_inodes_per_group -
320 ext2fs_bg_itable_unused(fs, i)) *
321 EXT2_INODE_SIZE(fs->super)) +
322 EXT2_BLOCK_SIZE(fs->super) - 1) /
323 EXT2_BLOCK_SIZE(fs->super));
325 /* The kernel doesn't need to zero the itable blocks */
326 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_ZEROED);
327 ext2fs_group_desc_csum_set(fs, i);
329 retval = ext2fs_zero_blocks(fs, blk, num, &blk, &num);
331 fprintf(stderr, _("\nCould not write %d "
332 "blocks in inode table starting at %u: %s\n"),
333 num, blk, error_message(retval));
337 if (sync_kludge == 1)
339 else if ((i % sync_kludge) == 0)
343 ext2fs_zero_blocks(0, 0, 0, 0, 0);
344 ext2fs_numeric_progress_close(fs, &progress,
348 static void create_root_dir(ext2_filsys fs)
351 struct ext2_inode inode;
354 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
356 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
360 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
362 com_err("ext2fs_read_inode", retval,
363 _("while reading root inode"));
368 ext2fs_set_i_uid_high(inode, uid >> 16);
372 ext2fs_set_i_gid_high(inode, gid >> 16);
374 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
376 com_err("ext2fs_write_inode", retval,
377 _("while setting root inode ownership"));
383 static void create_lost_and_found(ext2_filsys fs)
385 unsigned int lpf_size = 0;
388 const char *name = "lost+found";
392 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
394 com_err("ext2fs_mkdir", retval,
395 _("while creating /lost+found"));
399 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
401 com_err("ext2_lookup", retval,
402 _("while looking up /lost+found"));
406 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
407 /* Ensure that lost+found is at least 2 blocks, so we always
408 * test large empty blocks for big-block filesystems. */
409 if ((lpf_size += fs->blocksize) >= 16*1024 &&
410 lpf_size >= 2 * fs->blocksize)
412 retval = ext2fs_expand_dir(fs, ino);
414 com_err("ext2fs_expand_dir", retval,
415 _("while expanding /lost+found"));
421 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
425 ext2fs_mark_inode_bitmap2(fs->inode_map, EXT2_BAD_INO);
426 ext2fs_inode_alloc_stats2(fs, EXT2_BAD_INO, +1, 0);
427 retval = ext2fs_update_bb_inode(fs, bb_list);
429 com_err("ext2fs_update_bb_inode", retval,
430 _("while setting bad block inode"));
436 static void reserve_inodes(ext2_filsys fs)
440 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++)
441 ext2fs_inode_alloc_stats2(fs, i, +1, 0);
442 ext2fs_mark_ib_dirty(fs);
445 #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
446 #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
447 #define BSD_LABEL_OFFSET 64
449 static void zap_sector(ext2_filsys fs, int sect, int nsect)
455 buf = malloc(512*nsect);
457 printf(_("Out of memory erasing sectors %d-%d\n"),
458 sect, sect + nsect - 1);
463 /* Check for a BSD disklabel, and don't erase it if so */
464 retval = io_channel_read_blk64(fs->io, 0, -512, buf);
467 _("Warning: could not read block 0: %s\n"),
468 error_message(retval));
470 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
471 if ((*magic == BSD_DISKMAGIC) ||
472 (*magic == BSD_MAGICDISK))
477 memset(buf, 0, 512*nsect);
478 io_channel_set_blksize(fs->io, 512);
479 retval = io_channel_write_blk64(fs->io, sect, -512*nsect, buf);
480 io_channel_set_blksize(fs->io, fs->blocksize);
483 fprintf(stderr, _("Warning: could not erase sector %d: %s\n"),
484 sect, error_message(retval));
487 static void create_journal_dev(ext2_filsys fs)
489 struct ext2fs_numeric_progress_struct progress;
493 int c, count, err_count;
495 retval = ext2fs_create_journal_superblock(fs,
496 ext2fs_blocks_count(fs->super), 0, &buf);
498 com_err("create_journal_dev", retval,
499 _("while initializing journal superblock"));
502 ext2fs_numeric_progress_init(fs, &progress,
503 _("Zeroing journal device: "),
504 ext2fs_blocks_count(fs->super));
506 count = ext2fs_blocks_count(fs->super);
512 retval = ext2fs_zero_blocks(fs, blk, c, &err_blk, &err_count);
514 com_err("create_journal_dev", retval,
515 _("while zeroing journal device "
516 "(block %u, count %d)"),
522 ext2fs_numeric_progress_update(fs, &progress, blk);
524 ext2fs_zero_blocks(0, 0, 0, 0, 0);
526 retval = io_channel_write_blk64(fs->io,
527 fs->super->s_first_data_block+1,
530 com_err("create_journal_dev", retval,
531 _("while writing journal superblock"));
534 ext2fs_numeric_progress_close(fs, &progress, NULL);
537 static void show_stats(ext2_filsys fs)
539 struct ext2_super_block *s = fs->super;
546 if (ext2fs_blocks_count(&fs_param) != ext2fs_blocks_count(s))
547 fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
548 ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
550 memset(buf, 0, sizeof(buf));
551 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
552 printf(_("Filesystem label=%s\n"), buf);
553 fputs(_("OS type: "), stdout);
554 os = e2p_os2string(fs->super->s_creator_os);
558 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
559 s->s_log_block_size);
560 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
562 printf(_("Stride=%u blocks, Stripe width=%u blocks\n"),
563 s->s_raid_stride, s->s_raid_stripe_width);
564 printf(_("%u inodes, %llu blocks\n"), s->s_inodes_count,
565 ext2fs_blocks_count(s));
566 printf(_("%llu blocks (%2.2f%%) reserved for the super user\n"),
567 ext2fs_r_blocks_count(s),
568 100.0 * ext2fs_r_blocks_count(s) / ext2fs_blocks_count(s));
569 printf(_("First data block=%u\n"), s->s_first_data_block);
570 if (s->s_reserved_gdt_blocks)
571 printf(_("Maximum filesystem blocks=%lu\n"),
572 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
573 EXT2_DESC_PER_BLOCK(s) * s->s_blocks_per_group);
574 if (fs->group_desc_count > 1)
575 printf(_("%u block groups\n"), fs->group_desc_count);
577 printf(_("%u block group\n"), fs->group_desc_count);
578 printf(_("%u blocks per group, %u fragments per group\n"),
579 s->s_blocks_per_group, s->s_frags_per_group);
580 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
582 if (fs->group_desc_count == 1) {
587 printf(_("Superblock backups stored on blocks: "));
588 group_block = s->s_first_data_block;
590 for (i = 1; i < fs->group_desc_count; i++) {
591 group_block += s->s_blocks_per_group;
592 if (!ext2fs_bg_has_super(fs, i))
596 need = int_log10(group_block) + 2;
597 if (need > col_left) {
602 printf("%u", group_block);
608 * Set the S_CREATOR_OS field. Return true if OS is known,
611 static int set_os(struct ext2_super_block *sb, char *os)
614 sb->s_creator_os = atoi (os);
615 else if (strcasecmp(os, "linux") == 0)
616 sb->s_creator_os = EXT2_OS_LINUX;
617 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
618 sb->s_creator_os = EXT2_OS_HURD;
619 else if (strcasecmp(os, "freebsd") == 0)
620 sb->s_creator_os = EXT2_OS_FREEBSD;
621 else if (strcasecmp(os, "lites") == 0)
622 sb->s_creator_os = EXT2_OS_LITES;
628 #define PATH_SET "PATH=/sbin"
630 static void parse_extended_opts(struct ext2_super_block *param,
633 char *buf, *token, *next, *p, *arg, *badopt = 0;
641 _("Couldn't allocate memory to parse options!\n"));
645 for (token = buf; token && *token; token = next) {
646 p = strchr(token, ',');
652 arg = strchr(token, '=');
657 if (strcmp(token, "stride") == 0) {
663 param->s_raid_stride = strtoul(arg, &p, 0);
664 if (*p || (param->s_raid_stride == 0)) {
666 _("Invalid stride parameter: %s\n"),
671 } else if (strcmp(token, "stripe-width") == 0 ||
672 strcmp(token, "stripe_width") == 0) {
678 param->s_raid_stripe_width = strtoul(arg, &p, 0);
679 if (*p || (param->s_raid_stripe_width == 0)) {
681 _("Invalid stripe-width parameter: %s\n"),
686 } else if (!strcmp(token, "resize")) {
687 unsigned long resize, bpg, rsv_groups;
688 unsigned long group_desc_count, desc_blocks;
689 unsigned int gdpb, blocksize;
698 resize = parse_num_blocks(arg,
699 param->s_log_block_size);
703 _("Invalid resize parameter: %s\n"),
708 if (resize <= ext2fs_blocks_count(param)) {
710 _("The resize maximum must be greater "
711 "than the filesystem size.\n"));
716 blocksize = EXT2_BLOCK_SIZE(param);
717 bpg = param->s_blocks_per_group;
720 gdpb = EXT2_DESC_PER_BLOCK(param);
721 group_desc_count = (__u32) ext2fs_div64_ceil(
722 ext2fs_blocks_count(param), bpg);
723 desc_blocks = (group_desc_count +
725 rsv_groups = ext2fs_div_ceil(resize, bpg);
726 rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) -
728 if (rsv_gdb > (int) EXT2_ADDR_PER_BLOCK(param))
729 rsv_gdb = EXT2_ADDR_PER_BLOCK(param);
732 if (param->s_rev_level == EXT2_GOOD_OLD_REV) {
734 _("On-line resizing not supported with revision 0 filesystems\n"));
738 param->s_feature_compat |=
739 EXT2_FEATURE_COMPAT_RESIZE_INODE;
741 param->s_reserved_gdt_blocks = rsv_gdb;
743 } else if (!strcmp(token, "test_fs")) {
744 param->s_flags |= EXT2_FLAGS_TEST_FILESYS;
745 } else if (!strcmp(token, "lazy_itable_init")) {
747 lazy_itable_init = strtoul(arg, &p, 0);
749 lazy_itable_init = 1;
756 fprintf(stderr, _("\nBad option(s) specified: %s\n\n"
757 "Extended options are separated by commas, "
758 "and may take an argument which\n"
759 "\tis set off by an equals ('=') sign.\n\n"
760 "Valid extended options are:\n"
761 "\tstride=<RAID per-disk data chunk in blocks>\n"
762 "\tstripe-width=<RAID stride * data disks in blocks>\n"
763 "\tresize=<resize maximum size in blocks>\n"
764 "\tlazy_itable_init=<0 to disable, 1 to enable>\n"
766 badopt ? badopt : "");
770 if (param->s_raid_stride &&
771 (param->s_raid_stripe_width % param->s_raid_stride) != 0)
772 fprintf(stderr, _("\nWarning: RAID stripe-width %u not an even "
773 "multiple of stride %u.\n\n"),
774 param->s_raid_stripe_width, param->s_raid_stride);
779 static __u32 ok_features[3] = {
781 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
782 EXT2_FEATURE_COMPAT_RESIZE_INODE |
783 EXT2_FEATURE_COMPAT_DIR_INDEX |
784 EXT2_FEATURE_COMPAT_EXT_ATTR,
786 EXT2_FEATURE_INCOMPAT_FILETYPE|
787 EXT3_FEATURE_INCOMPAT_EXTENTS|
788 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
789 EXT2_FEATURE_INCOMPAT_META_BG|
790 EXT4_FEATURE_INCOMPAT_FLEX_BG,
792 EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
793 EXT4_FEATURE_RO_COMPAT_HUGE_FILE|
794 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
795 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE|
796 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|
797 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
801 static void syntax_err_report(const char *filename, long err, int line_num)
804 _("Syntax error in mke2fs config file (%s, line #%d)\n\t%s\n"),
805 filename, line_num, error_message(err));
809 static const char *config_fn[] = { ROOT_SYSCONFDIR "/mke2fs.conf", 0 };
811 static void edit_feature(const char *str, __u32 *compat_array)
816 if (e2p_edit_feature(str, compat_array, ok_features)) {
817 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
829 static errcode_t init_list(struct str_list *sl)
833 sl->list = malloc((sl->max+1) * sizeof(char *));
840 static errcode_t push_string(struct str_list *sl, const char *str)
844 if (sl->num >= sl->max) {
846 new_list = realloc(sl->list, (sl->max+1) * sizeof(char *));
851 sl->list[sl->num] = malloc(strlen(str)+1);
852 if (sl->list[sl->num] == 0)
854 strcpy(sl->list[sl->num], str);
856 sl->list[sl->num] = 0;
860 static void print_str_list(char **list)
864 for (cpp = list; *cpp; cpp++) {
865 printf("'%s'", *cpp);
872 static char **parse_fs_type(const char *fs_type,
873 const char *usage_types,
874 struct ext2_super_block *fs_param,
877 const char *ext_type = 0;
879 char *profile_type = 0;
881 const char *size_type;
882 struct str_list list;
886 if (init_list(&list))
889 if (creator_os && (!strcasecmp(creator_os, "GNU") ||
890 !strcasecmp(creator_os, "hurd")))
897 else if (!strcmp(program_name, "mke3fs"))
900 ext_type = strrchr(progname, '/');
906 if (!strncmp(ext_type, "mkfs.", 5)) {
908 if (ext_type[0] == 0)
915 profile_get_string(profile, "defaults", "fs_type", 0,
916 "ext2", &profile_type);
917 ext_type = profile_type;
918 if (!strcmp(ext_type, "ext2") && (journal_size != 0))
922 if (!strcmp(ext_type, "ext3") || !strcmp(ext_type, "ext4") ||
923 !strcmp(ext_type, "ext4dev")) {
924 profile_get_string(profile, "fs_types", ext_type, "features",
927 printf(_("\nWarning! Your mke2fs.conf file does "
928 "not define the %s filesystem type.\n"),
930 printf(_("You probably need to install an updated "
931 "mke2fs.conf file.\n\n"));
936 meg = (1024 * 1024) / EXT2_BLOCK_SIZE(fs_param);
937 if (ext2fs_blocks_count(fs_param) < 3 * meg)
938 size_type = "floppy";
939 else if (ext2fs_blocks_count(fs_param) < 512 * meg)
942 size_type = "default";
945 usage_types = size_type;
947 parse_str = malloc(usage_types ? strlen(usage_types)+1 : 1);
953 strcpy(parse_str, usage_types);
958 push_string(&list, ext_type);
966 push_string(&list, cp);
977 push_string(&list, "hurd");
981 static char *get_string_from_profile(char **fs_types, const char *opt,
987 for (i=0; fs_types[i]; i++);
988 for (i-=1; i >=0 ; i--) {
989 profile_get_string(profile, "fs_types", fs_types[i],
994 profile_get_string(profile, "defaults", opt, 0, def_val, &ret);
998 static int get_int_from_profile(char **fs_types, const char *opt, int def_val)
1003 profile_get_integer(profile, "defaults", opt, 0, def_val, &ret);
1004 for (cpp = fs_types; *cpp; cpp++)
1005 profile_get_integer(profile, "fs_types", *cpp, opt, ret, &ret);
1009 static int get_bool_from_profile(char **fs_types, const char *opt, int def_val)
1014 profile_get_boolean(profile, "defaults", opt, 0, def_val, &ret);
1015 for (cpp = fs_types; *cpp; cpp++)
1016 profile_get_boolean(profile, "fs_types", *cpp, opt, ret, &ret);
1020 extern const char *mke2fs_default_profile;
1021 static const char *default_files[] = { "<default>", 0 };
1023 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1025 * Sets the geometry of a device (stripe/stride), and returns the
1026 * device's alignment offset, if any, or a negative error.
1028 static int ext2fs_get_device_geometry(const char *file,
1029 struct ext2_super_block *fs_param)
1035 unsigned long min_io;
1036 unsigned long opt_io;
1038 pr = blkid_new_probe_from_filename(file);
1042 tp = blkid_probe_get_topology(pr);
1046 min_io = blkid_topology_get_minimum_io_size(tp);
1047 opt_io = blkid_topology_get_optimal_io_size(tp);
1048 blocksize = EXT2_BLOCK_SIZE(fs_param);
1050 fs_param->s_raid_stride = min_io / blocksize;
1051 fs_param->s_raid_stripe_width = opt_io / blocksize;
1053 rc = blkid_topology_get_alignment_offset(tp);
1055 blkid_free_probe(pr);
1060 static void PRS(int argc, char *argv[])
1066 int inode_ratio = 0;
1068 unsigned long flex_bg_size = 0;
1069 double reserved_ratio = 5.0;
1070 int sector_size = 0;
1071 int show_version_only = 0;
1072 unsigned long long num_inodes = 0; /* unsigned long long to catch too-large input */
1074 char * oldpath = getenv("PATH");
1075 char * extended_opts = 0;
1076 const char * fs_type = 0;
1077 const char * usage_types = 0;
1083 int s_opt = -1, r_opt = -1;
1084 char *fs_features = 0;
1087 int pathlen = sizeof(PATH_SET) + 1;
1090 pathlen += strlen(oldpath);
1091 newpath = malloc(pathlen);
1092 strcpy(newpath, PATH_SET);
1094 /* Update our PATH to include /sbin */
1096 strcat (newpath, ":");
1097 strcat (newpath, oldpath);
1101 tmp = getenv("MKE2FS_SYNC");
1103 sync_kludge = atoi(tmp);
1105 /* Determine the system page size if possible */
1107 #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
1108 #define _SC_PAGESIZE _SC_PAGE_SIZE
1111 sysval = sysconf(_SC_PAGESIZE);
1113 sys_page_size = sysval;
1114 #endif /* _SC_PAGESIZE */
1115 #endif /* HAVE_SYSCONF */
1117 if ((tmp = getenv("MKE2FS_CONFIG")) != NULL)
1119 profile_set_syntax_err_cb(syntax_err_report);
1120 retval = profile_init(config_fn, &profile);
1121 if (retval == ENOENT) {
1122 profile_init(default_files, &profile);
1123 profile_set_default(profile, mke2fs_default_profile);
1126 setbuf(stdout, NULL);
1127 setbuf(stderr, NULL);
1128 add_error_table(&et_ext2_error_table);
1129 add_error_table(&et_prof_error_table);
1130 memset(&fs_param, 0, sizeof(struct ext2_super_block));
1131 fs_param.s_rev_level = 1; /* Create revision 1 filesystems now */
1138 linux_version_code = parse_version_number(ut.release);
1139 if (linux_version_code && linux_version_code < (2*65536 + 2*256))
1140 fs_param.s_rev_level = 0;
1143 if (argc && *argv) {
1144 program_name = get_progname(*argv);
1146 /* If called as mkfs.ext3, create a journal inode */
1147 if (!strcmp(program_name, "mkfs.ext3") ||
1148 !strcmp(program_name, "mke3fs"))
1152 while ((c = getopt (argc, argv,
1153 "b:cf:g:G:i:jl:m:no:qr:s:t:vE:FI:J:L:M:N:O:R:ST:U:V")) != EOF) {
1156 blocksize = strtol(optarg, &tmp, 0);
1157 b = (blocksize > 0) ? blocksize : -blocksize;
1158 if (b < EXT2_MIN_BLOCK_SIZE ||
1159 b > EXT2_MAX_BLOCK_SIZE || *tmp) {
1160 com_err(program_name, 0,
1161 _("invalid block size - %s"), optarg);
1164 if (blocksize > 4096)
1165 fprintf(stderr, _("Warning: blocksize %d not "
1166 "usable on most systems.\n"),
1169 fs_param.s_log_block_size =
1170 int_log2(blocksize >>
1171 EXT2_MIN_BLOCK_LOG_SIZE);
1173 case 'c': /* Check for bad blocks */
1177 size = strtoul(optarg, &tmp, 0);
1178 if (size < EXT2_MIN_BLOCK_SIZE ||
1179 size > EXT2_MAX_BLOCK_SIZE || *tmp) {
1180 com_err(program_name, 0,
1181 _("invalid fragment size - %s"),
1185 fs_param.s_log_frag_size =
1186 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
1187 fprintf(stderr, _("Warning: fragments not supported. "
1188 "Ignoring -f option\n"));
1191 fs_param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
1193 com_err(program_name, 0,
1194 _("Illegal number for blocks per group"));
1197 if ((fs_param.s_blocks_per_group % 8) != 0) {
1198 com_err(program_name, 0,
1199 _("blocks per group must be multiple of 8"));
1204 flex_bg_size = strtoul(optarg, &tmp, 0);
1206 com_err(program_name, 0,
1207 _("Illegal number for flex_bg size"));
1210 if (flex_bg_size < 2 ||
1211 (flex_bg_size & (flex_bg_size-1)) != 0) {
1212 com_err(program_name, 0,
1213 _("flex_bg size must be a power of 2"));
1218 inode_ratio = strtoul(optarg, &tmp, 0);
1219 if (inode_ratio < EXT2_MIN_BLOCK_SIZE ||
1220 inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024 ||
1222 com_err(program_name, 0,
1223 _("invalid inode ratio %s (min %d/max %d)"),
1224 optarg, EXT2_MIN_BLOCK_SIZE,
1225 EXT2_MAX_BLOCK_SIZE);
1230 parse_journal_opts(optarg);
1237 bad_blocks_filename = malloc(strlen(optarg)+1);
1238 if (!bad_blocks_filename) {
1239 com_err(program_name, ENOMEM,
1240 _("in malloc for bad_blocks_filename"));
1243 strcpy(bad_blocks_filename, optarg);
1246 reserved_ratio = strtod(optarg, &tmp);
1247 if ( *tmp || reserved_ratio > 50 ||
1248 reserved_ratio < 0) {
1249 com_err(program_name, 0,
1250 _("invalid reserved blocks percent - %s"),
1259 creator_os = optarg;
1265 r_opt = strtoul(optarg, &tmp, 0);
1267 com_err(program_name, 0,
1268 _("bad revision level - %s"), optarg);
1271 fs_param.s_rev_level = r_opt;
1273 case 's': /* deprecated */
1274 s_opt = atoi(optarg);
1277 inode_size = strtoul(optarg, &tmp, 0);
1279 com_err(program_name, 0,
1280 _("invalid inode size - %s"), optarg);
1291 volume_label = optarg;
1297 num_inodes = strtoul(optarg, &tmp, 0);
1299 com_err(program_name, 0,
1300 _("bad num inodes - %s"), optarg);
1305 fs_features = optarg;
1309 extended_opts = optarg;
1318 usage_types = optarg;
1324 /* Print version number and exit */
1325 show_version_only++;
1331 if ((optind == argc) && !show_version_only)
1333 device_name = argv[optind++];
1335 if (!quiet || show_version_only)
1336 fprintf (stderr, "mke2fs %s (%s)\n", E2FSPROGS_VERSION,
1339 if (show_version_only) {
1340 fprintf(stderr, _("\tUsing %s\n"),
1341 error_message(EXT2_ET_BASE));
1346 * If there's no blocksize specified and there is a journal
1347 * device, use it to figure out the blocksize
1349 if (blocksize <= 0 && journal_device) {
1353 #ifdef CONFIG_TESTIO_DEBUG
1354 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1355 io_ptr = test_io_manager;
1356 test_io_backing_manager = unix_io_manager;
1359 io_ptr = unix_io_manager;
1360 retval = ext2fs_open(journal_device,
1361 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1364 com_err(program_name, retval,
1365 _("while trying to open journal device %s\n"),
1369 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1370 com_err(program_name, 0,
1371 _("Journal dev blocksize (%d) smaller than "
1372 "minimum blocksize %d\n"), jfs->blocksize,
1376 blocksize = jfs->blocksize;
1377 printf(_("Using journal device's blocksize: %d\n"), blocksize);
1378 fs_param.s_log_block_size =
1379 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1383 if (blocksize > sys_page_size) {
1385 com_err(program_name, 0,
1386 _("%d-byte blocks too big for system (max %d)"),
1387 blocksize, sys_page_size);
1390 fprintf(stderr, _("Warning: %d-byte blocks too big for system "
1391 "(max %d), forced to continue\n"),
1392 blocksize, sys_page_size);
1394 if (optind < argc) {
1395 ext2fs_blocks_count_set(&fs_param, parse_num_blocks(argv[optind++],
1396 fs_param.s_log_block_size));
1397 if (!ext2fs_blocks_count(&fs_param)) {
1398 com_err(program_name, 0, _("invalid blocks count - %s"),
1407 check_plausibility(device_name);
1408 check_mount(device_name, force, _("filesystem"));
1410 fs_param.s_log_frag_size = fs_param.s_log_block_size;
1412 if (noaction && ext2fs_blocks_count(&fs_param)) {
1413 dev_size = ext2fs_blocks_count(&fs_param);
1417 retval = ext2fs_get_device_size(device_name,
1418 EXT2_BLOCK_SIZE(&fs_param),
1420 if ((retval == EFBIG) &&
1422 (fs_param.s_log_block_size == 0)) {
1423 fs_param.s_log_block_size = 2;
1429 if (retval == EFBIG) {
1430 blk64_t big_dev_size;
1432 if (blocksize < 4096) {
1433 fs_param.s_log_block_size = 2;
1436 retval = ext2fs_get_device_size2(device_name,
1437 EXT2_BLOCK_SIZE(&fs_param), &big_dev_size);
1439 goto get_size_failure;
1440 if (big_dev_size == (1ULL << 32)) {
1441 dev_size = (blk_t) (big_dev_size - 1);
1444 fprintf(stderr, _("%s: Size of device %s too big "
1445 "to be expressed in 32 bits\n\t"
1446 "using a blocksize of %d.\n"),
1447 program_name, device_name, EXT2_BLOCK_SIZE(&fs_param));
1451 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1452 com_err(program_name, retval,
1453 _("while trying to determine filesystem size"));
1457 if (!ext2fs_blocks_count(&fs_param)) {
1458 if (retval == EXT2_ET_UNIMPLEMENTED) {
1459 com_err(program_name, 0,
1460 _("Couldn't determine device size; you "
1461 "must specify\nthe size of the "
1465 if (dev_size == 0) {
1466 com_err(program_name, 0,
1467 _("Device size reported to be zero. "
1468 "Invalid partition specified, or\n\t"
1469 "partition table wasn't reread "
1470 "after running fdisk, due to\n\t"
1471 "a modified partition being busy "
1472 "and in use. You may need to reboot\n\t"
1473 "to re-read your partition table.\n"
1477 ext2fs_blocks_count_set(&fs_param, dev_size);
1478 if (sys_page_size > EXT2_BLOCK_SIZE(&fs_param)) {
1479 blk64_t tmp = ext2fs_blocks_count(&fs_param);
1481 tmp &= ~((blk64_t) ((sys_page_size /
1482 EXT2_BLOCK_SIZE(&fs_param))-1));
1483 ext2fs_blocks_count_set(&fs_param, tmp);
1486 } else if (!force && (ext2fs_blocks_count(&fs_param) > dev_size)) {
1487 com_err(program_name, 0,
1488 _("Filesystem larger than apparent device size."));
1492 fs_types = parse_fs_type(fs_type, usage_types, &fs_param, argv[0]);
1494 fprintf(stderr, _("Failed to parse fs types list\n"));
1498 /* Figure out what features should be enabled */
1501 if (fs_param.s_rev_level != EXT2_GOOD_OLD_REV) {
1502 tmp = get_string_from_profile(fs_types, "base_features",
1503 "sparse_super,filetype,resize_inode,dir_index");
1504 edit_feature(tmp, &fs_param.s_feature_compat);
1507 for (cpp = fs_types; *cpp; cpp++) {
1509 profile_get_string(profile, "fs_types", *cpp,
1510 "features", "", &tmp);
1512 edit_feature(tmp, &fs_param.s_feature_compat);
1515 tmp = get_string_from_profile(fs_types, "default_features",
1518 edit_feature(fs_features ? fs_features : tmp,
1519 &fs_param.s_feature_compat);
1522 if (fs_param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1523 fs_types[0] = strdup("journal");
1528 fputs(_("fs_types for mke2fs.conf resolution: "), stdout);
1529 print_str_list(fs_types);
1532 if (r_opt == EXT2_GOOD_OLD_REV &&
1533 (fs_param.s_feature_compat || fs_param.s_feature_incompat ||
1534 fs_param.s_feature_ro_compat)) {
1535 fprintf(stderr, _("Filesystem features not supported "
1536 "with revision 0 filesystems\n"));
1541 if (r_opt == EXT2_GOOD_OLD_REV) {
1542 fprintf(stderr, _("Sparse superblocks not supported "
1543 "with revision 0 filesystems\n"));
1546 fs_param.s_feature_ro_compat |=
1547 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1548 } else if (s_opt == 0)
1549 fs_param.s_feature_ro_compat &=
1550 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1552 if (journal_size != 0) {
1553 if (r_opt == EXT2_GOOD_OLD_REV) {
1554 fprintf(stderr, _("Journals not supported "
1555 "with revision 0 filesystems\n"));
1558 fs_param.s_feature_compat |=
1559 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1562 if (fs_param.s_feature_incompat &
1563 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1565 fs_param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1566 fs_param.s_feature_compat = 0;
1567 fs_param.s_feature_ro_compat = 0;
1570 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1571 (fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)) {
1572 fprintf(stderr, _("The resize_inode and meta_bg features "
1573 "are not compatible.\n"
1574 "They can not be both enabled "
1575 "simultaneously.\n"));
1579 /* Set first meta blockgroup via an environment variable */
1580 /* (this is mostly for debugging purposes) */
1581 if ((fs_param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1582 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1583 fs_param.s_first_meta_bg = atoi(tmp);
1585 /* Get the hardware sector size, if available */
1586 retval = ext2fs_get_device_sectsize(device_name, §or_size);
1588 com_err(program_name, retval,
1589 _("while trying to determine hardware sector size"));
1593 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1594 sector_size = atoi(tmp);
1596 if (blocksize <= 0) {
1597 use_bsize = get_int_from_profile(fs_types, "blocksize", 4096);
1599 if (use_bsize == -1) {
1600 use_bsize = sys_page_size;
1601 if ((linux_version_code < (2*65536 + 6*256)) &&
1605 if (sector_size && use_bsize < sector_size)
1606 use_bsize = sector_size;
1607 if ((blocksize < 0) && (use_bsize < (-blocksize)))
1608 use_bsize = -blocksize;
1609 blocksize = use_bsize;
1610 ext2fs_blocks_count_set(&fs_param,
1611 ext2fs_blocks_count(&fs_param) /
1612 (blocksize / 1024));
1615 if (inode_ratio == 0) {
1616 inode_ratio = get_int_from_profile(fs_types, "inode_ratio",
1618 if (inode_ratio < blocksize)
1619 inode_ratio = blocksize;
1622 fs_param.s_log_frag_size = fs_param.s_log_block_size =
1623 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1625 #ifdef HAVE_BLKID_PROBE_GET_TOPOLOGY
1626 retval = ext2fs_get_device_geometry(device_name, &fs_param);
1629 _("warning: Unable to get device geometry for %s"),
1631 } else if (retval) {
1632 printf(_("%s alignment is offset by %lu bytes.\n"),
1633 device_name, retval);
1634 printf(_("This may result in very poor performance, "
1635 "(re)-partitioning suggested.\n"));
1640 blocksize = EXT2_BLOCK_SIZE(&fs_param);
1642 lazy_itable_init = get_bool_from_profile(fs_types,
1643 "lazy_itable_init", 0);
1645 /* Get options from profile */
1646 for (cpp = fs_types; *cpp; cpp++) {
1648 profile_get_string(profile, "fs_types", *cpp, "options", "", &tmp);
1650 parse_extended_opts(&fs_param, tmp);
1655 parse_extended_opts(&fs_param, extended_opts);
1657 /* Since sparse_super is the default, we would only have a problem
1658 * here if it was explicitly disabled.
1660 if ((fs_param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1661 !(fs_param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1662 com_err(program_name, 0,
1663 _("reserved online resize blocks not supported "
1664 "on non-sparse filesystem"));
1668 if (fs_param.s_blocks_per_group) {
1669 if (fs_param.s_blocks_per_group < 256 ||
1670 fs_param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1671 com_err(program_name, 0,
1672 _("blocks per group count out of range"));
1677 if (inode_size == 0)
1678 inode_size = get_int_from_profile(fs_types, "inode_size", 0);
1679 if (!flex_bg_size && (fs_param.s_feature_incompat &
1680 EXT4_FEATURE_INCOMPAT_FLEX_BG))
1681 flex_bg_size = get_int_from_profile(fs_types,
1682 "flex_bg_size", 16);
1684 if (!(fs_param.s_feature_incompat &
1685 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
1686 com_err(program_name, 0,
1687 _("Flex_bg feature not enabled, so "
1688 "flex_bg size may not be specified"));
1691 fs_param.s_log_groups_per_flex = int_log2(flex_bg_size);
1694 if (inode_size && fs_param.s_rev_level >= EXT2_DYNAMIC_REV) {
1695 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1696 inode_size > EXT2_BLOCK_SIZE(&fs_param) ||
1697 inode_size & (inode_size - 1)) {
1698 com_err(program_name, 0,
1699 _("invalid inode size %d (min %d/max %d)"),
1700 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1704 fs_param.s_inode_size = inode_size;
1707 /* Make sure number of inodes specified will fit in 32 bits */
1708 if (num_inodes == 0) {
1709 unsigned long long n;
1710 n = ext2fs_blocks_count(&fs_param) * blocksize / inode_ratio;
1712 com_err(program_name, 0,
1713 _("too many inodes (%llu), raise inode ratio?"), n);
1716 } else if (num_inodes > ~0U) {
1717 com_err(program_name, 0,
1718 _("too many inodes (%llu), specify < 2^32 inodes"),
1723 * Calculate number of inodes based on the inode ratio
1725 fs_param.s_inodes_count = num_inodes ? num_inodes :
1726 (ext2fs_blocks_count(&fs_param) * blocksize) / inode_ratio;
1728 if ((((long long)fs_param.s_inodes_count) *
1729 (inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE)) >=
1730 ((ext2fs_blocks_count(&fs_param)) *
1731 EXT2_BLOCK_SIZE(&fs_param))) {
1732 com_err(program_name, 0, _("inode_size (%u) * inodes_count "
1733 "(%u) too big for a\n\t"
1734 "filesystem with %llu blocks, "
1735 "specify higher inode_ratio (-i)\n\t"
1736 "or lower inode count (-N).\n"),
1737 inode_size ? inode_size : EXT2_GOOD_OLD_INODE_SIZE,
1738 fs_param.s_inodes_count,
1739 (unsigned long long) ext2fs_blocks_count(&fs_param));
1744 * Calculate number of blocks to reserve
1746 ext2fs_r_blocks_count_set(&fs_param, reserved_ratio *
1747 ext2fs_blocks_count(&fs_param) / 100.0);
1750 static int should_do_undo(const char *name)
1755 struct ext2_super_block super;
1756 io_manager manager = unix_io_manager;
1757 int csum_flag, force_undo;
1759 csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(&fs_param,
1760 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
1761 force_undo = get_int_from_profile(fs_types, "force_undo", 0);
1762 if (!force_undo && (!csum_flag || !lazy_itable_init))
1765 retval = manager->open(name, IO_FLAG_EXCLUSIVE, &channel);
1768 * We don't handle error cases instead we
1769 * declare that the file system doesn't exist
1770 * and let the rest of mke2fs take care of
1777 io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
1778 retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
1784 #if defined(WORDS_BIGENDIAN)
1785 s_magic = ext2fs_swab16(super.s_magic);
1787 s_magic = super.s_magic;
1790 if (s_magic == EXT2_SUPER_MAGIC)
1794 io_channel_close(channel);
1801 static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
1803 errcode_t retval = 0;
1804 char *tdb_dir, *tdb_file;
1805 char *device_name, *tmp_name;
1808 * Configuration via a conf file would be
1811 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1813 profile_get_string(profile, "defaults",
1814 "undo_dir", 0, "/var/lib/e2fsprogs",
1817 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1818 access(tdb_dir, W_OK))
1821 tmp_name = strdup(name);
1824 com_err(program_name, ENOMEM,
1825 _("Couldn't allocate memory for tdb filename\n"));
1828 device_name = basename(tmp_name);
1829 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(device_name) + 7 + 1);
1832 sprintf(tdb_file, "%s/mke2fs-%s.e2undo", tdb_dir, device_name);
1834 if (!access(tdb_file, F_OK)) {
1835 if (unlink(tdb_file) < 0) {
1837 com_err(program_name, retval,
1838 _("while trying to delete %s"),
1845 set_undo_io_backing_manager(*io_ptr);
1846 *io_ptr = undo_io_manager;
1847 set_undo_io_backup_file(tdb_file);
1848 printf(_("Overwriting existing filesystem; this can be undone "
1849 "using the command:\n"
1850 " e2undo %s %s\n\n"), tdb_file, name);
1857 int main (int argc, char *argv[])
1859 errcode_t retval = 0;
1861 badblocks_list bb_list = 0;
1862 unsigned int journal_blocks;
1868 char tdb_string[40];
1872 setlocale(LC_MESSAGES, "");
1873 setlocale(LC_CTYPE, "");
1874 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1875 textdomain(NLS_CAT_NAME);
1879 #ifdef CONFIG_TESTIO_DEBUG
1880 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1881 io_ptr = test_io_manager;
1882 test_io_backing_manager = unix_io_manager;
1885 io_ptr = unix_io_manager;
1887 if (should_do_undo(device_name)) {
1888 retval = mke2fs_setup_tdb(device_name, &io_ptr);
1894 * Initialize the superblock....
1896 flags = EXT2_FLAG_EXCLUSIVE;
1897 profile_get_boolean(profile, "options", "old_bitmaps", 0, 0,
1900 flags |= EXT2_FLAG_64BITS;
1902 * By default, we print how many inode tables or block groups
1903 * or whatever we've written so far. The quiet flag disables
1904 * this, along with a lot of other output.
1907 flags |= EXT2_FLAG_PRINT_PROGRESS;
1908 retval = ext2fs_initialize(device_name, flags, &fs_param, io_ptr, &fs);
1910 com_err(device_name, retval, _("while setting up superblock"));
1914 sprintf(tdb_string, "tdb_data_size=%d", fs->blocksize <= 4096 ?
1915 32768 : fs->blocksize * 8);
1916 io_channel_set_options(fs->io, tdb_string);
1918 if (fs_param.s_flags & EXT2_FLAGS_TEST_FILESYS)
1919 fs->super->s_flags |= EXT2_FLAGS_TEST_FILESYS;
1921 if ((fs_param.s_feature_incompat &
1922 (EXT3_FEATURE_INCOMPAT_EXTENTS|EXT4_FEATURE_INCOMPAT_FLEX_BG)) ||
1923 (fs_param.s_feature_ro_compat &
1924 (EXT4_FEATURE_RO_COMPAT_HUGE_FILE|EXT4_FEATURE_RO_COMPAT_GDT_CSUM|
1925 EXT4_FEATURE_RO_COMPAT_DIR_NLINK|
1926 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)))
1927 fs->super->s_kbytes_written = 1;
1930 * Wipe out the old on-disk superblock
1933 zap_sector(fs, 2, 6);
1936 * Parse or generate a UUID for the filesystem
1939 if (uuid_parse(fs_uuid, fs->super->s_uuid) !=0) {
1940 com_err(device_name, 0, "could not parse UUID: %s\n",
1945 uuid_generate(fs->super->s_uuid);
1948 * Initialize the directory index variables
1950 hash_alg_str = get_string_from_profile(fs_types, "hash_alg",
1952 hash_alg = e2p_string2hash(hash_alg_str);
1953 fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
1955 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1958 * Add "jitter" to the superblock's check interval so that we
1959 * don't check all the filesystems at the same time. We use a
1960 * kludgy hack of using the UUID to derive a random jitter value.
1962 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1963 val += fs->super->s_uuid[i];
1964 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1967 * Override the creator OS, if applicable
1969 if (creator_os && !set_os(fs->super, creator_os)) {
1970 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1975 * For the Hurd, we will turn off filetype since it doesn't
1978 if (fs->super->s_creator_os == EXT2_OS_HURD)
1979 fs->super->s_feature_incompat &=
1980 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1983 * Set the volume label...
1986 memset(fs->super->s_volume_name, 0,
1987 sizeof(fs->super->s_volume_name));
1988 strncpy(fs->super->s_volume_name, volume_label,
1989 sizeof(fs->super->s_volume_name));
1993 * Set the last mount directory
1996 memset(fs->super->s_last_mounted, 0,
1997 sizeof(fs->super->s_last_mounted));
1998 strncpy(fs->super->s_last_mounted, mount_dir,
1999 sizeof(fs->super->s_last_mounted));
2002 if (!quiet || noaction)
2008 if (fs->super->s_feature_incompat &
2009 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
2010 create_journal_dev(fs);
2011 exit(ext2fs_close(fs) ? 1 : 0);
2014 if (bad_blocks_filename)
2015 read_bb_file(fs, &bb_list, bad_blocks_filename);
2017 test_disk(fs, &bb_list);
2019 handle_bad_blocks(fs, bb_list);
2020 fs->stride = fs_stride = fs->super->s_raid_stride;
2022 printf(_("Allocating group tables: "));
2023 retval = ext2fs_allocate_tables(fs);
2025 com_err(program_name, retval,
2026 _("while trying to allocate filesystem tables"));
2030 printf(_("done \n"));
2032 fs->super->s_state |= EXT2_ERROR_FS;
2033 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
2035 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
2036 unsigned int rsv = 65536 / fs->blocksize;
2037 unsigned long blocks = ext2fs_blocks_count(fs->super);
2038 unsigned long start;
2041 #ifdef ZAP_BOOTBLOCK
2042 zap_sector(fs, 0, 2);
2046 * Wipe out any old MD RAID (or other) metadata at the end
2047 * of the device. This will also verify that the device is
2048 * as large as we think. Be careful with very small devices.
2050 start = (blocks & ~(rsv - 1));
2054 retval = ext2fs_zero_blocks(fs, start, blocks - start,
2058 com_err(program_name, retval,
2059 _("while zeroing block %u at end of filesystem"),
2062 write_inode_tables(fs, lazy_itable_init);
2063 create_root_dir(fs);
2064 create_lost_and_found(fs);
2066 create_bad_block_inode(fs, bb_list);
2067 if (fs->super->s_feature_compat &
2068 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
2069 retval = ext2fs_create_resize_inode(fs);
2071 com_err("ext2fs_create_resize_inode", retval,
2072 _("while reserving blocks for online resize"));
2078 if (journal_device) {
2082 check_plausibility(journal_device);
2083 check_mount(journal_device, force, _("journal"));
2085 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
2086 EXT2_FLAG_JOURNAL_DEV_OK, 0,
2087 fs->blocksize, unix_io_manager, &jfs);
2089 com_err(program_name, retval,
2090 _("while trying to open journal device %s\n"),
2095 printf(_("Adding journal to device %s: "),
2099 retval = ext2fs_add_journal_device(fs, jfs);
2101 com_err (program_name, retval,
2102 _("\n\twhile trying to add journal to device %s"),
2107 printf(_("done\n"));
2109 free(journal_device);
2110 } else if ((journal_size) ||
2111 (fs_param.s_feature_compat &
2112 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
2113 journal_blocks = figure_journal_size(journal_size, fs);
2116 printf(_("Skipping journal creation in super-only mode\n"));
2117 fs->super->s_journal_inum = EXT2_JOURNAL_INO;
2121 if (!journal_blocks) {
2122 fs->super->s_feature_compat &=
2123 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
2127 printf(_("Creating journal (%u blocks): "),
2131 retval = ext2fs_add_journal_inode(fs, journal_blocks,
2134 com_err (program_name, retval,
2135 _("\n\twhile trying to create journal"));
2139 printf(_("done\n"));
2144 printf(_("Writing superblocks and "
2145 "filesystem accounting information: "));
2146 retval = ext2fs_flush(fs);
2149 _("\nWarning, had trouble writing out superblocks."));
2152 printf(_("done\n\n"));
2153 if (!getenv("MKE2FS_SKIP_CHECK_MSG"))
2154 print_check_message(fs);
2156 val = ext2fs_close(fs);
2157 remove_error_table(&et_ext2_error_table);
2158 remove_error_table(&et_prof_error_table);
2159 profile_release(profile);
2160 return (retval || val) ? 1 : 0;