2 * mke2fs.c - Make a ext2fs filesystem.
4 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
12 /* Usage: mke2fs [options] device
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
24 #include <sys/utsname.h>
44 #include <sys/ioctl.h>
45 #include <sys/types.h>
47 #include <linux/ext2_fs.h>
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
52 #include "ext2fs/ext2fs.h"
54 #include "../version.h"
55 #include "nls-enable.h"
57 #define STRIDE_LENGTH 8
63 extern int isatty(int);
64 extern FILE *fpopen(const char *cmd, const char *mode);
66 const char * program_name = "mke2fs";
67 const char * device_name /* = NULL */;
69 /* Command line options */
71 int verbose /* = 0 */ ;
73 int super_only /* = 0 */ ;
75 int noaction /* = 0 */ ;
76 int journal_size /* = 0 */ ;
77 int journal_flags /* = 0 */ ;
78 char *bad_blocks_filename /* = 0 */ ;
79 __u32 fs_stride /* = 0 */ ;
81 struct ext2_super_block param;
82 char *creator_os /* = NULL */ ;
83 char *volume_label /* = NULL */ ;
84 char *mount_dir /* = NULL */ ;
85 char *journal_device /* = NULL */ ;
87 static void usage(void)
89 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
90 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j journal-options]"
91 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
92 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
93 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
94 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
95 "[-qvSV] device [blocks-count]\n"),
100 static int int_log2(int arg)
112 static int int_log10(unsigned int arg)
122 * This function sets the default parameters for a filesystem
124 * The type is specified by the user. The size is the maximum size
125 * (in megabytes) for which a set of parameters applies, with a size
126 * of zero meaning that it is the default parameter for the type.
127 * Note that order is important in the table below.
129 static char default_str[] = "default";
130 struct mke2fs_defaults {
136 { default_str, 0, 4096, 8192 },
137 { default_str, 512, 1024, 4096 },
138 { default_str, 3, 1024, 8192 },
139 { "news", 0, 4096, 4096 },
143 static void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
144 int blocksize, int *inode_ratio)
148 struct mke2fs_defaults *p;
150 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
153 ratio = *inode_ratio;
155 fs_type = default_str;
156 for (p = settings; p->type; p++) {
157 if ((strcmp(p->type, fs_type) != 0) &&
158 (strcmp(p->type, default_str) != 0))
160 if ((p->size != 0) &&
164 *inode_ratio = p->inode_ratio;
165 if (blocksize == 0) {
166 super->s_log_frag_size = super->s_log_block_size =
167 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
171 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
175 * Helper function for read_bb_file and test_disk
177 static void invalid_block(ext2_filsys fs, blk_t blk)
179 printf(_("Bad block %u out of range; ignored.\n"), blk);
184 * Reads the bad blocks list from a file
186 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
187 const char *bad_blocks_file)
192 f = fopen(bad_blocks_file, "r");
194 com_err("read_bad_blocks_file", errno,
195 _("while trying to open %s"), bad_blocks_file);
198 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
201 com_err("ext2fs_read_bb_FILE", retval,
202 _("while reading in list of bad blocks from file"));
208 * Runs the badblocks program to test the disk
210 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
216 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
217 quiet ? "" : "-s ", fs->device_name,
218 fs->super->s_blocks_count);
220 printf(_("Running command: %s\n"), buf);
223 com_err("popen", errno,
224 _("while trying run '%s'"), buf);
227 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
230 com_err("ext2fs_read_bb_FILE", retval,
231 _("while processing list of bad blocks from program"));
236 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
241 badblocks_iterate bb_iter;
251 * The primary superblock and group descriptors *must* be
252 * good; if not, abort.
254 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
255 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
256 if (badblocks_list_test(bb_list, i)) {
257 fprintf(stderr, _("Block %d in primary "
258 "superblock/group descriptor area bad.\n"), i);
259 fprintf(stderr, _("Blocks %d through %d must be good "
260 "in order to build a filesystem.\n"),
261 fs->super->s_first_data_block, must_be_good);
262 fprintf(stderr, _("Aborting....\n"));
268 * See if any of the bad blocks are showing up in the backup
269 * superblocks and/or group descriptors. If so, issue a
270 * warning and adjust the block counts appropriately.
272 group_block = fs->super->s_first_data_block +
273 fs->super->s_blocks_per_group;
275 for (i = 1; i < fs->group_desc_count; i++) {
277 for (j=0; j < fs->desc_blocks+1; j++) {
278 if (badblocks_list_test(bb_list, group_block +
282 _("Warning: the backup superblock/group descriptors at block %d contain\n"
286 group = ext2fs_group_of_blk(fs, group_block+j);
287 fs->group_desc[group].bg_free_blocks_count++;
288 fs->super->s_free_blocks_count++;
291 group_block += fs->super->s_blocks_per_group;
295 * Mark all the bad blocks as used...
297 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
299 com_err("badblocks_list_iterate_begin", retval,
300 _("while marking bad blocks as used"));
303 while (badblocks_list_iterate(bb_iter, &blk))
304 ext2fs_mark_block_bitmap(fs->block_map, blk);
305 badblocks_list_iterate_end(bb_iter);
308 static void write_inode_tables(ext2_filsys fs)
312 int i, j, num, count;
314 char format[20], backup[80];
318 mke2fs_sync = getenv("MKE2FS_SYNC");
320 sync_kludge = atoi(mke2fs_sync);
322 buf = malloc(fs->blocksize * STRIDE_LENGTH);
324 com_err("malloc", ENOMEM,
325 _("while allocating zeroizing buffer"));
328 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
331 * Figure out how many digits we need
333 i = int_log10(fs->group_desc_count);
334 sprintf(format, "%%%dd/%%%dld", i, i);
335 memset(backup, '\b', sizeof(backup)-1);
336 backup[sizeof(backup)-1] = 0;
337 if ((2*i)+1 < sizeof(backup))
341 printf(_("Writing inode tables: "));
342 for (i = 0; i < fs->group_desc_count; i++) {
344 printf(format, i, fs->group_desc_count);
346 blk = fs->group_desc[i].bg_inode_table;
347 num = fs->inode_blocks_per_group;
349 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
350 if (num-j > STRIDE_LENGTH)
351 count = STRIDE_LENGTH;
354 retval = io_channel_write_blk(fs->io, blk, count, buf);
356 printf(_("Warning: could not write %d blocks "
357 "in inode table starting at %d: %s\n"),
358 count, blk, error_message(retval));
361 fputs(backup, stdout);
363 if (sync_kludge == 1)
365 else if ((i % sync_kludge) == 0)
371 fputs(_("done \n"), stdout);
374 static void create_root_dir(ext2_filsys fs)
377 struct ext2_inode inode;
379 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
381 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
385 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
387 com_err("ext2fs_read_inode", retval,
388 _("while reading root inode"));
391 inode.i_uid = getuid();
393 inode.i_gid = getgid();
394 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
396 com_err("ext2fs_write_inode", retval,
397 _("while setting root inode ownership"));
403 static void create_lost_and_found(ext2_filsys fs)
407 const char *name = "lost+found";
411 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
413 com_err("ext2fs_mkdir", retval,
414 _("while creating /lost+found"));
418 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
420 com_err("ext2_lookup", retval,
421 _("while looking up /lost+found"));
425 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
426 if ((lpf_size += fs->blocksize) >= 16*1024)
428 retval = ext2fs_expand_dir(fs, ino);
430 com_err("ext2fs_expand_dir", retval,
431 _("while expanding /lost+found"));
437 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
441 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
442 fs->group_desc[0].bg_free_inodes_count--;
443 fs->super->s_free_inodes_count--;
444 retval = ext2fs_update_bb_inode(fs, bb_list);
446 com_err("ext2fs_update_bb_inode", retval,
447 _("while setting bad block inode"));
453 static void reserve_inodes(ext2_filsys fs)
458 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
459 ext2fs_mark_inode_bitmap(fs->inode_map, i);
460 group = ext2fs_group_of_ino(fs, i);
461 fs->group_desc[group].bg_free_inodes_count--;
462 fs->super->s_free_inodes_count--;
464 ext2fs_mark_ib_dirty(fs);
467 static void zap_sector(ext2_filsys fs, int sect)
474 io_channel_set_blksize(fs->io, 512);
475 retval = io_channel_write_blk(fs->io, sect, -512, buf);
476 io_channel_set_blksize(fs->io, fs->blocksize);
478 printf(_("Warning: could not erase sector %d: %s\n"), sect,
479 error_message(retval));
483 static void show_stats(ext2_filsys fs)
485 struct ext2_super_block *s = fs->super;
488 int i, need, col_left;
490 if (param.s_blocks_count != s->s_blocks_count)
491 printf(_("warning: %d blocks unused.\n\n"),
492 param.s_blocks_count - s->s_blocks_count);
494 memset(buf, 0, sizeof(buf));
495 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
496 printf(_("Filesystem label=%s\n"), buf);
497 printf(_("OS type: "));
498 switch (fs->super->s_creator_os) {
499 case EXT2_OS_LINUX: printf ("Linux"); break;
500 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
501 case EXT2_OS_MASIX: printf ("Masix"); break;
502 default: printf (_("(unknown os)"));
505 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
506 s->s_log_block_size);
507 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
509 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
511 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
513 100.0 * s->s_r_blocks_count / s->s_blocks_count);
514 printf(_("First data block=%u\n"), s->s_first_data_block);
515 if (fs->group_desc_count > 1)
516 printf(_("%lu block groups\n"), fs->group_desc_count);
518 printf(_("%lu block group\n"), fs->group_desc_count);
519 printf(_("%u blocks per group, %u fragments per group\n"),
520 s->s_blocks_per_group, s->s_frags_per_group);
521 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
523 if (fs->group_desc_count == 1) {
528 printf(_("Superblock backups stored on blocks: "));
529 group_block = s->s_first_data_block;
531 for (i = 1; i < fs->group_desc_count; i++) {
532 group_block += s->s_blocks_per_group;
533 if (!ext2fs_bg_has_super(fs, i))
537 need = int_log10(group_block) + 2;
538 if (need > col_left) {
543 printf("%u", group_block);
549 * Set the S_CREATOR_OS field. Return true if OS is known,
552 static int set_os(struct ext2_super_block *sb, char *os)
555 sb->s_creator_os = atoi (os);
556 else if (strcasecmp(os, "linux") == 0)
557 sb->s_creator_os = EXT2_OS_LINUX;
558 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
559 sb->s_creator_os = EXT2_OS_HURD;
560 else if (strcasecmp(os, "masix") == 0)
561 sb->s_creator_os = EXT2_OS_MASIX;
567 #define PATH_SET "PATH=/sbin"
569 static void parse_raid_opts(const char *opts)
571 char *buf, *token, *next, *p, *arg;
578 fprintf(stderr, _("Couldn't allocate memory to parse "
583 for (token = buf; token && *token; token = next) {
584 p = strchr(token, ',');
590 arg = strchr(token, '=');
595 if (strcmp(token, "stride") == 0) {
600 fs_stride = strtoul(arg, &p, 0);
601 if (*p || (fs_stride == 0)) {
603 _("Invalid stride parameter.\n"));
611 fprintf(stderr, _("\nBad raid options specified.\n\n"
612 "Raid options are separated by commas, "
613 "and may take an argument which\n"
614 "\tis set off by an equals ('=') sign.\n\n"
615 "Valid raid options are:\n"
616 "\tstride=<stride length in blocks>\n\n"));
621 static __u32 ok_features[3] = {
623 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
624 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
628 static void PRS(int argc, char *argv[])
633 blk_t group_blk_max = 8192;
636 int reserved_ratio = 5;
637 ino_t num_inodes = 0;
639 int sparse_option = 1;
640 char * oldpath = getenv("PATH");
641 struct ext2_super_block *param_ext2 = ¶m;
642 char * raid_opts = 0;
643 char * journal_opts = 0;
645 const char * feature_set = "filetype";
654 if ((ut.release[0] == '1') ||
655 (ut.release[0] == '2' && ut.release[1] == '.' &&
656 ut.release[2] < '2' && ut.release[3] == '.'))
659 /* Update our PATH to include /sbin */
663 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
664 strcpy (newpath, PATH_SET);
665 strcat (newpath, ":");
666 strcat (newpath, oldpath);
671 setbuf(stdout, NULL);
672 setbuf(stderr, NULL);
673 initialize_ext2_error_table();
674 memset(¶m, 0, sizeof(struct ext2_super_block));
675 param.s_rev_level = 1; /* Create revision 1 filesystems now */
677 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
678 E2FSPROGS_VERSION, E2FSPROGS_DATE,
679 EXT2FS_VERSION, EXT2FS_DATE);
681 program_name = *argv;
682 while ((c = getopt (argc, argv,
683 "b:cf:g:i:j:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
686 blocksize = strtoul(optarg, &tmp, 0);
687 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
688 com_err(program_name, 0,
689 _("bad block size - %s"), optarg);
692 param.s_log_block_size =
693 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
694 group_blk_max = blocksize * 8;
697 case 't': /* Check for bad blocks */
701 size = strtoul(optarg, &tmp, 0);
702 if (size < 1024 || size > 4096 || *tmp) {
703 com_err(program_name, 0,
704 _("bad fragment size - %s"),
708 param.s_log_frag_size =
709 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
710 printf(_("Warning: fragments not supported. "
711 "Ignoring -f option\n"));
714 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
716 com_err(program_name, 0,
717 _("Illegal number for blocks per group"));
720 if ((param.s_blocks_per_group % 8) != 0) {
721 com_err(program_name, 0,
722 _("blocks per group must be multiple of 8"));
727 inode_ratio = strtoul(optarg, &tmp, 0);
728 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
730 com_err(program_name, 0,
731 _("bad inode ratio - %s"), optarg);
736 journal_opts = optarg;
739 bad_blocks_filename = malloc(strlen(optarg)+1);
740 if (!bad_blocks_filename) {
741 com_err(program_name, ENOMEM,
742 _("in malloc for bad_blocks_filename"));
745 strcpy(bad_blocks_filename, optarg);
748 reserved_ratio = strtoul(optarg, &tmp, 0);
749 if (reserved_ratio > 50 || *tmp) {
750 com_err(program_name, 0,
751 _("bad reserved blocks percent - %s"),
763 param.s_rev_level = atoi(optarg);
766 sparse_option = atoi(optarg);
768 #ifdef EXT2_DYNAMIC_REV
770 param.s_inode_size = atoi(optarg);
774 num_inodes = atoi(optarg);
786 volume_label = optarg;
792 feature_set = optarg;
804 /* Print version number and exit */
805 fprintf(stderr, _("\tUsing %s\n"),
806 error_message(EXT2_ET_BASE));
813 device_name = argv[optind];
816 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
818 if ((*tmp) || (tmp2 > 0xfffffffful)) {
819 com_err(program_name, 0, _("bad blocks count - %s"),
823 param.s_blocks_count = tmp2;
829 parse_raid_opts(raid_opts);
832 parse_journal_opts(journal_opts);
835 check_plausibility(device_name);
836 check_mount(device_name, force, _("filesystem"));
838 param.s_log_frag_size = param.s_log_block_size;
840 if (noaction && param.s_blocks_count) {
841 dev_size = param.s_blocks_count;
844 retval = ext2fs_get_device_size(device_name,
845 EXT2_BLOCK_SIZE(¶m),
847 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
848 com_err(program_name, retval,
849 _("while trying to determine filesystem size"));
852 if (!param.s_blocks_count) {
853 if (retval == EXT2_ET_UNIMPLEMENTED) {
854 com_err(program_name, 0,
855 _("Couldn't determine device size; you "
856 "must specify\nthe size of the "
861 com_err(program_name, 0,
862 _("Device size reported to be zero. "
863 "Invalid partition specified, or\n\t"
864 "partition table wasn't reread "
865 "after running fdisk, due to\n\t"
866 "a modified partition being busy "
867 "and in use. You may need to reboot\n\t"
868 "to re-read your partition table.\n"
872 param.s_blocks_count = dev_size;
875 } else if (!force && (param.s_blocks_count > dev_size)) {
876 com_err(program_name, 0,
877 _("Filesystem larger than apparent filesystem size."));
881 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
883 if (param.s_blocks_per_group) {
884 if (param.s_blocks_per_group < 256 ||
885 param.s_blocks_per_group > group_blk_max || *tmp) {
886 com_err(program_name, 0,
887 _("blocks per group count out of range"));
893 * Calculate number of inodes based on the inode ratio
895 param.s_inodes_count = num_inodes ? num_inodes :
896 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m))
900 * Calculate number of blocks to reserve
902 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
904 /* Turn off features not supported by the earlier filesystem version */
905 if (param.s_rev_level == 0) {
910 param_ext2->s_feature_ro_compat |=
911 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
913 if (feature_set && !strncasecmp(feature_set, "none", 4))
915 if (feature_set && e2p_edit_feature(feature_set,
916 ¶m_ext2->s_feature_compat,
918 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
924 int main (int argc, char *argv[])
926 errcode_t retval = 0;
928 badblocks_list bb_list = 0;
932 setlocale(LC_MESSAGES, "");
933 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
934 textdomain(NLS_CAT_NAME);
939 * Initialize the superblock....
941 retval = ext2fs_initialize(device_name, 0, ¶m,
942 unix_io_manager, &fs);
944 com_err(device_name, retval, _("while setting up superblock"));
949 * Wipe out the old on-disk superblock
954 * Generate a UUID for it...
956 uuid_generate(fs->super->s_uuid);
959 * Override the creator OS, if applicable
961 if (creator_os && !set_os(fs->super, creator_os)) {
962 com_err (program_name, 0, _("unknown os - %s"), creator_os);
967 * For the Hurd, we will turn off filetype since it doesn't
970 if (fs->super->s_creator_os == EXT2_OS_HURD)
971 fs->super->s_feature_incompat &=
972 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
975 * Set the volume label...
978 memset(fs->super->s_volume_name, 0,
979 sizeof(fs->super->s_volume_name));
980 strncpy(fs->super->s_volume_name, volume_label,
981 sizeof(fs->super->s_volume_name));
985 * Set the last mount directory
988 memset(fs->super->s_last_mounted, 0,
989 sizeof(fs->super->s_last_mounted));
990 strncpy(fs->super->s_last_mounted, mount_dir,
991 sizeof(fs->super->s_last_mounted));
994 if (!quiet || noaction)
1000 if (bad_blocks_filename)
1001 read_bb_file(fs, &bb_list, bad_blocks_filename);
1003 test_disk(fs, &bb_list);
1005 handle_bad_blocks(fs, bb_list);
1006 fs->stride = fs_stride;
1007 retval = ext2fs_allocate_tables(fs);
1009 com_err(program_name, retval,
1010 _("while trying to allocate filesystem tables"));
1014 fs->super->s_state |= EXT2_ERROR_FS;
1015 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1017 write_inode_tables(fs);
1018 create_root_dir(fs);
1019 create_lost_and_found(fs);
1021 create_bad_block_inode(fs, bb_list);
1022 #ifdef ZAP_BOOTBLOCK
1027 journal_blocks = journal_size * 1024 / (fs->blocksize / 1024);
1028 if (journal_device) {
1030 check_plausibility(journal_device);
1031 check_mount(journal_device, force, _("journal"));
1034 printf(_("Creating journal on device %s: "),
1036 retval = ext2fs_add_journal_device(fs, journal_device,
1040 com_err (program_name, retval,
1041 _("while trying to create journal on device %s"),
1046 printf(_("done\n"));
1047 } else if (journal_size) {
1049 printf(_("Creating journal (%d blocks): "),
1051 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1054 com_err (program_name, retval,
1055 _("while trying to create journal"));
1059 printf(_("done\n"));
1063 printf(_("Writing superblocks and "
1064 "filesystem accounting information: "));
1065 retval = ext2fs_flush(fs);
1067 printf(_("\nWarning, had trouble writing out superblocks."));
1070 printf(_("done\n"));