2 * mke2fs.c - Make a ext2fs filesystem.
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
8 /* Usage: mke2fs [options] device
10 * The device may be a block device or a image of one, but this isn't
11 * enforced (but it's not much fun on a character device :-).
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
41 #ifdef HAVE_LINUX_FS_H
44 #include <linux/ext2_fs.h>
45 #ifdef HAVE_LINUX_MAJOR_H
46 #include <linux/major.h>
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
51 #include "ext2fs/ext2fs.h"
52 #include "../version.h"
54 #define STRIDE_LENGTH 8
60 extern int isatty(int);
61 extern FILE *fpopen(const char *cmd, const char *mode);
63 const char * program_name = "mke2fs";
64 const char * device_name = NULL;
66 /* Command line options */
72 char *bad_blocks_filename = 0;
74 struct ext2_super_block param;
75 char *creator_os = NULL;
76 char *volume_label = NULL;
77 char *mount_dir = NULL;
79 static void usage(NOARGS)
81 fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
82 "[-f fragment-size]\n\t[-i bytes-per-inode] "
83 "[-m reserved-blocks-percentage] [-qvS]\n\t"
84 "[-o creator-os] [-g blocks-per-group] [-L volume-label]\n\t"
85 "[-M last-mounted-directory] device [blocks-count]\n",
90 static int log2(int arg)
102 static void check_plausibility(NOARGS)
104 #ifdef HAVE_LINUX_MAJOR_H
108 val = stat(device_name, &s);
114 if(!S_ISBLK(s.st_mode)) {
115 printf("%s is not a block special device.\n", device_name);
116 printf("Proceed anyway? (y,n) ");
117 if (getchar() != 'y')
121 if ((MAJOR(s.st_rdev) == HD_MAJOR && MINOR(s.st_rdev)%64 == 0) ||
122 (MAJOR(s.st_rdev) == SCSI_DISK_MAJOR &&
123 MINOR(s.st_rdev)%16 == 0)) {
124 printf("%s is entire device, not just one partition!\n",
126 printf("Proceed anyway? (y,n) ");
127 if (getchar() != 'y')
134 static void check_mount(NOARGS)
139 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
141 com_err("ext2fs_check_if_mount", retval,
142 "while determining whether %s is mounted.",
146 if (!(mount_flags & EXT2_MF_MOUNTED))
149 fprintf(stderr, "%s is mounted; will not make a filesystem here!\n",
155 * Helper function for read_bb_file and test_disk
157 static void invalid_block(ext2_filsys fs, blk_t blk)
159 printf("Bad block %u out of range; ignored.\n", blk);
164 * Reads the bad blocks list from a file
166 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
167 const char *bad_blocks_file)
172 f = fopen(bad_blocks_file, "r");
174 com_err("read_bad_blocks_file", errno,
175 "while trying to open %s", bad_blocks_file);
178 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
181 com_err("ext2fs_read_bb_FILE", retval,
182 "while reading in list of bad blocks from file");
188 * Runs the badblocks program to test the disk
190 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
196 sprintf(buf, "badblocks %s%s %d", quiet ? "" : "-s ",
198 fs->super->s_blocks_count);
200 printf("Running command: %s\n", buf);
203 com_err("popen", errno,
204 "while trying run '%s'", buf);
207 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
210 com_err("ext2fs_read_bb_FILE", retval,
211 "while processing list of bad blocks from program");
216 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
221 badblocks_iterate bb_iter;
231 * The primary superblock and group descriptors *must* be
232 * good; if not, abort.
234 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
235 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
236 if (badblocks_list_test(bb_list, i)) {
237 fprintf(stderr, "Block %d in primary superblock/group "
238 "descriptor area bad.\n", i);
239 fprintf(stderr, "Blocks %d through %d must be good "
240 "in order to build a filesystem.\n",
241 fs->super->s_first_data_block, must_be_good);
242 fprintf(stderr, "Aborting....\n");
248 * See if any of the bad blocks are showing up in the backup
249 * superblocks and/or group descriptors. If so, issue a
250 * warning and adjust the block counts appropriately.
252 group_block = fs->super->s_first_data_block +
253 fs->super->s_blocks_per_group;
256 for (i = 1; i < fs->group_desc_count; i++) {
257 for (j=0; j < fs->desc_blocks+1; j++) {
258 if (badblocks_list_test(bb_list, group_block +
262 "Warning: the backup superblock/group descriptors at block %d contain\n"
266 group = ext2fs_group_of_blk(fs, group_block+j);
267 fs->group_desc[group].bg_free_blocks_count++;
268 fs->super->s_free_blocks_count++;
271 group_block += fs->super->s_blocks_per_group;
275 * Mark all the bad blocks as used...
277 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
279 com_err("badblocks_list_iterate_begin", retval,
280 "while marking bad blocks as used");
283 while (badblocks_list_iterate(bb_iter, &blk))
284 ext2fs_mark_block_bitmap(fs->block_map, blk);
285 badblocks_list_iterate_end(bb_iter);
288 static void new_table_block(ext2_filsys fs, blk_t first_block,
289 const char *name, int num, int initialize,
290 const char *buf, blk_t *new_block)
297 retval = ext2fs_get_free_blocks(fs, first_block,
298 first_block + fs->super->s_blocks_per_group,
299 num, fs->block_map, new_block);
301 printf("Could not allocate %d block(s) for %s: %s\n",
302 num, name, error_message(retval));
303 ext2fs_unmark_valid(fs);
308 for (i=0; i < num; i += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
309 if (num-i > STRIDE_LENGTH)
310 count = STRIDE_LENGTH;
313 retval = io_channel_write_blk(fs->io, blk, count, buf);
315 printf("Warning: could not write %d blocks "
316 "starting at %d for %s: %s\n",
318 error_message(retval));
322 for (i = 0; i < num; i++, blk++)
323 ext2fs_mark_block_bitmap(fs->block_map, blk);
326 static void alloc_tables(ext2_filsys fs)
332 buf = malloc(fs->blocksize * STRIDE_LENGTH);
334 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
337 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
339 group_blk = fs->super->s_first_data_block;
341 printf("Writing inode tables: ");
342 for (i = 0; i < fs->group_desc_count; i++) {
344 printf("%4d/%4ld", i, fs->group_desc_count);
345 new_table_block(fs, group_blk, "block bitmap", 1, 0, buf,
346 &fs->group_desc[i].bg_block_bitmap);
347 new_table_block(fs, group_blk, "inode bitmap", 1, 0, buf,
348 &fs->group_desc[i].bg_inode_bitmap);
349 new_table_block(fs, group_blk, "inode table",
350 fs->inode_blocks_per_group,
352 &fs->group_desc[i].bg_inode_table);
354 group_blk += fs->super->s_blocks_per_group;
356 printf("\b\b\b\b\b\b\b\b\b");
362 static void create_root_dir(ext2_filsys fs)
365 struct ext2_inode inode;
367 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
369 com_err("ext2fs_mkdir", retval, "while creating root dir");
373 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
375 com_err("ext2fs_read_inode", retval,
376 "while reading root inode");
379 inode.i_uid = geteuid();
380 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
382 com_err("ext2fs_write_inode", retval,
383 "while setting root inode ownership");
389 static void create_lost_and_found(ext2_filsys fs)
393 const char *name = "lost+found";
396 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
398 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
402 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
404 com_err("ext2_lookup", retval, "while looking up /lost+found");
408 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
409 retval = ext2fs_expand_dir(fs, ino);
411 com_err("ext2fs_expand_dir", retval,
412 "while expanding /lost+found");
418 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
422 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
423 fs->group_desc[0].bg_free_inodes_count--;
424 fs->super->s_free_inodes_count--;
425 retval = ext2fs_update_bb_inode(fs, bb_list);
427 com_err("ext2fs_update_bb_inode", retval,
428 "while setting bad block inode");
434 static void reserve_inodes(ext2_filsys fs)
439 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
440 ext2fs_mark_inode_bitmap(fs->inode_map, i);
441 group = ext2fs_group_of_ino(fs, i);
442 fs->group_desc[group].bg_free_inodes_count--;
443 fs->super->s_free_inodes_count--;
445 ext2fs_mark_ib_dirty(fs);
449 static void zap_bootblock(ext2_filsys fs)
456 retval = io_channel_write_blk(fs->io, 0, -512, buf);
458 printf("Warning: could not erase block 0: %s\n",
459 error_message(retval));
464 static void show_stats(ext2_filsys fs)
466 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
471 if (param.s_blocks_count != s->s_blocks_count)
472 printf("warning: %d blocks unused.\n\n",
473 param.s_blocks_count - s->s_blocks_count);
475 switch (fs->super->s_creator_os) {
476 case EXT2_OS_LINUX: printf ("Linux"); break;
477 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
478 case EXT2_OS_MASIX: printf ("Masix"); break;
479 default: printf ("(unknown os)");
481 printf (" ext2 filesystem format\n");
482 memset(buf, 0, sizeof(buf));
483 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
484 printf("Filesystem label=%s\n", buf);
485 printf("%u inodes, %u blocks\n", s->s_inodes_count,
487 printf("%u blocks (%2.2f%%) reserved for the super user\n",
489 100.0 * s->s_r_blocks_count / s->s_blocks_count);
490 printf("First data block=%u\n", s->s_first_data_block);
491 printf("Block size=%u (log=%u)\n", fs->blocksize,
492 s->s_log_block_size);
493 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
495 printf("%lu block group%s\n", fs->group_desc_count,
496 (fs->group_desc_count > 1) ? "s" : "");
497 printf("%u blocks per group, %u fragments per group\n",
498 s->s_blocks_per_group, s->s_frags_per_group);
499 printf("%u inodes per group\n", s->s_inodes_per_group);
501 if (fs->group_desc_count == 1) {
506 printf("Superblock backups stored on blocks: ");
507 group_block = s->s_first_data_block;
509 for (i = 1; i < fs->group_desc_count; i++) {
510 group_block += s->s_blocks_per_group;
515 printf("%u", group_block);
516 if (i != fs->group_desc_count - 1)
522 #ifndef HAVE_STRCASECMP
523 static int strcasecmp (char *s1, char *s2)
526 int ch1 = *s1++, ch2 = *s2++;
534 return *s1 ? 1 : *s2 ? -1 : 0;
539 * Set the S_CREATOR_OS field. Return true if OS is known,
542 static int set_os(struct ext2_super_block *sb, char *os)
545 sb->s_creator_os = atoi (os);
546 else if (strcasecmp(os, "linux") == 0)
547 sb->s_creator_os = EXT2_OS_LINUX;
548 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
549 sb->s_creator_os = EXT2_OS_HURD;
550 else if (strcasecmp(os, "masix") == 0)
551 sb->s_creator_os = EXT2_OS_MASIX;
557 #define PATH_SET "PATH=/sbin"
559 static void PRS(int argc, char *argv[])
564 int inode_ratio = 4096;
565 int reserved_ratio = 5;
567 char *oldpath = getenv("PATH");
569 /* Update our PATH to include /sbin */
573 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
574 strcpy (newpath, PATH_SET);
575 strcat (newpath, ":");
576 strcat (newpath, oldpath);
581 setbuf(stdout, NULL);
582 setbuf(stderr, NULL);
583 initialize_ext2_error_table();
584 memset(¶m, 0, sizeof(struct ext2_super_block));
586 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
587 E2FSPROGS_VERSION, E2FSPROGS_DATE,
588 EXT2FS_VERSION, EXT2FS_DATE);
590 program_name = *argv;
591 while ((c = getopt (argc, argv,
592 "b:cf:g:i:l:m:o:qr:tvI:SFL:M:")) != EOF)
595 size = strtoul(optarg, &tmp, 0);
596 if (size < 1024 || size > 4096 || *tmp) {
597 com_err(program_name, 0, "bad block size - %s",
601 param.s_log_block_size =
602 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
605 case 't': /* Check for bad blocks */
609 size = strtoul(optarg, &tmp, 0);
610 if (size < 1024 || size > 4096 || *tmp) {
611 com_err(program_name, 0, "bad fragment size - %s",
615 param.s_log_frag_size =
616 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
617 printf("Warning: fragments not supported. "
618 "Ignoring -f option\n");
621 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
623 com_err(program_name, 0,
624 "Illegal number for blocks per group");
627 if (param.s_blocks_per_group < 256 ||
628 param.s_blocks_per_group > 8192 || *tmp) {
629 com_err(program_name, 0,
630 "blocks per group count out of range");
633 if ((param.s_blocks_per_group % 8) != 0) {
634 com_err(program_name, 0,
635 "blocks per group must be multiple of 8");
640 inode_ratio = strtoul(optarg, &tmp, 0);
641 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
643 com_err(program_name, 0, "bad inode ratio - %s",
649 bad_blocks_filename = malloc(strlen(optarg)+1);
650 if (!bad_blocks_filename) {
651 com_err(program_name, ENOMEM,
652 "in malloc for bad_blocks_filename");
655 strcpy(bad_blocks_filename, optarg);
658 reserved_ratio = strtoul(optarg, &tmp, 0);
659 if (reserved_ratio > 50 || *tmp) {
660 com_err(program_name, 0,
661 "bad reserved blocks percent - %s",
670 param.s_rev_level = atoi(optarg);
672 #ifdef EXT2_DYNAMIC_REV
674 param.s_inode_size = atoi(optarg);
687 volume_label = optarg;
700 device_name = argv[optind];
703 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
705 com_err(program_name, 0, "bad blocks count - %s",
714 check_plausibility();
717 param.s_log_frag_size = param.s_log_block_size;
719 if (!param.s_blocks_count) {
720 retval = ext2fs_get_device_size(device_name,
721 EXT2_BLOCK_SIZE(¶m),
722 ¶m.s_blocks_count);
724 com_err(program_name, retval,
725 "while trying to determine filesystem size");
731 * Calculate number of inodes based on the inode ratio
733 param.s_inodes_count =
734 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m))
738 * Calculate number of blocks to reserve
740 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
743 int main (int argc, char *argv[])
745 errcode_t retval = 0;
747 badblocks_list bb_list = 0;
753 * Initialize the superblock....
755 retval = ext2fs_initialize(device_name, 0, ¶m,
756 unix_io_manager, &fs);
758 com_err(device_name, retval, "while setting up superblock");
763 * Generate a UUID for it...
765 s = (struct ext2fs_sb *) fs->super;
766 uuid_generate(s->s_uuid);
769 * Override the creator OS, if applicable
771 if (creator_os && !set_os(fs->super, creator_os)) {
772 com_err (program_name, 0, "unknown os - %s", creator_os);
777 * Set the volume label...
780 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
781 strncpy(s->s_volume_name, volume_label,
782 sizeof(s->s_volume_name));
786 * Set the last mount directory
789 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
790 strncpy(s->s_last_mounted, mount_dir,
791 sizeof(s->s_last_mounted));
797 if (bad_blocks_filename)
798 read_bb_file(fs, &bb_list, bad_blocks_filename);
800 test_disk(fs, &bb_list);
802 handle_bad_blocks(fs, bb_list);
805 fs->super->s_state |= EXT2_ERROR_FS;
806 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
809 create_lost_and_found(fs);
811 create_bad_block_inode(fs, bb_list);
818 printf("Writing superblocks and "
819 "filesystem accounting information: ");