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 :-).
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
43 #ifdef HAVE_LINUX_FS_H
46 #include <linux/ext2_fs.h>
47 #ifdef HAVE_LINUX_MAJOR_H
48 #include <linux/major.h>
51 #include "et/com_err.h"
52 #include "uuid/uuid.h"
53 #include "ext2fs/ext2fs.h"
54 #include "../version.h"
56 /* Everything is STDC, these days */
59 #define STRIDE_LENGTH 8
65 extern int isatty(int);
66 extern FILE *fpopen(const char *cmd, const char *mode);
68 const char * program_name = "mke2fs";
69 const char * device_name = NULL;
71 /* Command line options */
77 char *bad_blocks_filename = 0;
80 struct ext2_super_block param;
81 char *creator_os = NULL;
82 char *volume_label = NULL;
83 char *mount_dir = NULL;
85 static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
87 static void usage(NOARGS)
89 fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
90 "[-f fragment-size]\n\t[-i bytes-per-inode] "
91 "[-m reserved-blocks-percentage] [-qvSV]\n\t"
92 "[-o creator-os] [-g blocks-per-group] [-L volume-label]\n\t"
93 "[-M last-mounted-directory] [-r fs-revision] [-R raid_opts]\n\t"
94 "device [blocks-count]\n",
99 static int log2(int arg)
111 static void proceed_question(NOARGS)
115 printf("Proceed anyway? (y,n) ");
116 if (getchar() != 'y')
120 static void check_plausibility(NOARGS)
122 #ifdef HAVE_LINUX_MAJOR_H
126 val = stat(device_name, &s);
129 fprintf(stderr, "Could not stat %s --- %s\n",
130 device_name, error_message(errno));
132 fprintf(stderr, "\nThe device apparently does "
133 "not exist; did you specify it correctly?\n");
136 if(!S_ISBLK(s.st_mode)) {
137 printf("%s is not a block special device.\n", device_name);
140 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
141 MINOR(s.st_rdev)%64 == 0) ||
142 (MAJOR(s.st_rdev) == SCSI_DISK_MAJOR &&
143 MINOR(s.st_rdev)%16 == 0)) {
144 printf("%s is entire device, not just one partition!\n",
151 static void check_mount(NOARGS)
156 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
158 com_err("ext2fs_check_if_mount", retval,
159 "while determining whether %s is mounted.",
163 if (!(mount_flags & EXT2_MF_MOUNTED))
166 fprintf(stderr, "%s is mounted; will not make a filesystem here!\n",
172 * Helper function for read_bb_file and test_disk
174 static void invalid_block(ext2_filsys fs, blk_t blk)
176 printf("Bad block %u out of range; ignored.\n", blk);
181 * Reads the bad blocks list from a file
183 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
184 const char *bad_blocks_file)
189 f = fopen(bad_blocks_file, "r");
191 com_err("read_bad_blocks_file", errno,
192 "while trying to open %s", bad_blocks_file);
195 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
198 com_err("ext2fs_read_bb_FILE", retval,
199 "while reading in list of bad blocks from file");
205 * Runs the badblocks program to test the disk
207 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
213 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
214 quiet ? "" : "-s ", fs->device_name,
215 fs->super->s_blocks_count);
217 printf("Running command: %s\n", buf);
220 com_err("popen", errno,
221 "while trying run '%s'", buf);
224 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
227 com_err("ext2fs_read_bb_FILE", retval,
228 "while processing list of bad blocks from program");
233 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
238 badblocks_iterate bb_iter;
248 * The primary superblock and group descriptors *must* be
249 * good; if not, abort.
251 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
252 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
253 if (badblocks_list_test(bb_list, i)) {
254 fprintf(stderr, "Block %d in primary superblock/group "
255 "descriptor area bad.\n", i);
256 fprintf(stderr, "Blocks %d through %d must be good "
257 "in order to build a filesystem.\n",
258 fs->super->s_first_data_block, must_be_good);
259 fprintf(stderr, "Aborting....\n");
265 * See if any of the bad blocks are showing up in the backup
266 * superblocks and/or group descriptors. If so, issue a
267 * warning and adjust the block counts appropriately.
269 group_block = fs->super->s_first_data_block +
270 fs->super->s_blocks_per_group;
272 for (i = 1; i < fs->group_desc_count; i++) {
274 for (j=0; j < fs->desc_blocks+1; j++) {
275 if (badblocks_list_test(bb_list, group_block +
279 "Warning: the backup superblock/group descriptors at block %d contain\n"
283 group = ext2fs_group_of_blk(fs, group_block+j);
284 fs->group_desc[group].bg_free_blocks_count++;
285 fs->super->s_free_blocks_count++;
288 group_block += fs->super->s_blocks_per_group;
292 * Mark all the bad blocks as used...
294 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
296 com_err("badblocks_list_iterate_begin", retval,
297 "while marking bad blocks as used");
300 while (badblocks_list_iterate(bb_iter, &blk))
301 ext2fs_mark_block_bitmap(fs->block_map, blk);
302 badblocks_list_iterate_end(bb_iter);
305 static void write_inode_tables(ext2_filsys fs)
309 int i, j, num, count;
312 buf = malloc(fs->blocksize * STRIDE_LENGTH);
314 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
317 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
320 printf("Writing inode tables: ");
321 for (i = 0; i < fs->group_desc_count; i++) {
323 printf("%4d/%4ld", i, fs->group_desc_count);
325 blk = fs->group_desc[i].bg_inode_table;
326 num = fs->inode_blocks_per_group;
328 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
329 if (num-j > STRIDE_LENGTH)
330 count = STRIDE_LENGTH;
333 retval = io_channel_write_blk(fs->io, blk, count, buf);
335 printf("Warning: could not write %d blocks "
336 "in inode table starting at %d: %s\n",
337 count, blk, error_message(retval));
340 printf("\b\b\b\b\b\b\b\b\b");
347 static void create_root_dir(ext2_filsys fs)
350 struct ext2_inode inode;
352 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
354 com_err("ext2fs_mkdir", retval, "while creating root dir");
358 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
360 com_err("ext2fs_read_inode", retval,
361 "while reading root inode");
364 inode.i_uid = getuid();
366 inode.i_gid = getgid();
367 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
369 com_err("ext2fs_write_inode", retval,
370 "while setting root inode ownership");
376 static void create_lost_and_found(ext2_filsys fs)
380 const char *name = "lost+found";
383 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
385 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
389 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
391 com_err("ext2_lookup", retval, "while looking up /lost+found");
395 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
396 retval = ext2fs_expand_dir(fs, ino);
398 com_err("ext2fs_expand_dir", retval,
399 "while expanding /lost+found");
405 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
409 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
410 fs->group_desc[0].bg_free_inodes_count--;
411 fs->super->s_free_inodes_count--;
412 retval = ext2fs_update_bb_inode(fs, bb_list);
414 com_err("ext2fs_update_bb_inode", retval,
415 "while setting bad block inode");
421 static void reserve_inodes(ext2_filsys fs)
426 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
427 ext2fs_mark_inode_bitmap(fs->inode_map, i);
428 group = ext2fs_group_of_ino(fs, i);
429 fs->group_desc[group].bg_free_inodes_count--;
430 fs->super->s_free_inodes_count--;
432 ext2fs_mark_ib_dirty(fs);
436 static void zap_bootblock(ext2_filsys fs)
443 retval = io_channel_write_blk(fs->io, 0, -512, buf);
445 printf("Warning: could not erase block 0: %s\n",
446 error_message(retval));
451 static void show_stats(ext2_filsys fs)
453 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
458 if (param.s_blocks_count != s->s_blocks_count)
459 printf("warning: %d blocks unused.\n\n",
460 param.s_blocks_count - s->s_blocks_count);
462 switch (fs->super->s_creator_os) {
463 case EXT2_OS_LINUX: printf ("Linux"); break;
464 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
465 case EXT2_OS_MASIX: printf ("Masix"); break;
466 default: printf ("(unknown os)");
468 printf (" ext2 filesystem format\n");
469 memset(buf, 0, sizeof(buf));
470 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
471 printf("Filesystem label=%s\n", buf);
472 printf("%u inodes, %u blocks\n", s->s_inodes_count,
474 printf("%u blocks (%2.2f%%) reserved for the super user\n",
476 100.0 * s->s_r_blocks_count / s->s_blocks_count);
477 printf("First data block=%u\n", s->s_first_data_block);
478 printf("Block size=%u (log=%u)\n", fs->blocksize,
479 s->s_log_block_size);
480 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
482 printf("%lu block group%s\n", fs->group_desc_count,
483 (fs->group_desc_count > 1) ? "s" : "");
484 printf("%u blocks per group, %u fragments per group\n",
485 s->s_blocks_per_group, s->s_frags_per_group);
486 printf("%u inodes per group\n", s->s_inodes_per_group);
488 if (fs->group_desc_count == 1) {
493 printf("Superblock backups stored on blocks: ");
494 group_block = s->s_first_data_block;
496 for (i = 1; i < fs->group_desc_count; i++) {
497 group_block += s->s_blocks_per_group;
498 if (!ext2fs_bg_has_super(fs, i))
504 printf("%u", group_block);
505 if (i != fs->group_desc_count - 1)
511 #ifndef HAVE_STRCASECMP
512 static int strcasecmp (char *s1, char *s2)
515 int ch1 = *s1++, ch2 = *s2++;
523 return *s1 ? 1 : *s2 ? -1 : 0;
528 * Set the S_CREATOR_OS field. Return true if OS is known,
531 static int set_os(struct ext2_super_block *sb, char *os)
534 sb->s_creator_os = atoi (os);
535 else if (strcasecmp(os, "linux") == 0)
536 sb->s_creator_os = EXT2_OS_LINUX;
537 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
538 sb->s_creator_os = EXT2_OS_HURD;
539 else if (strcasecmp(os, "masix") == 0)
540 sb->s_creator_os = EXT2_OS_MASIX;
546 #define PATH_SET "PATH=/sbin"
548 static void parse_raid_opts(const char *opts)
550 char *buf, *token, *next, *p, *arg;
557 fprintf(stderr, "Couldn't allocate memory to parse "
562 for (token = buf; token && *token; token = next) {
563 p = strchr(token, ',');
569 arg = strchr(token, '=');
574 if (strcmp(token, "stride") == 0) {
579 fs_stride = strtoul(arg, &p, 0);
580 if (*p || (fs_stride == 0)) {
581 fprintf(stderr, "Invalid stride parameter.\n");
589 fprintf(stderr, "\nBad raid options specified.\n\n"
590 "Raid options are separated by commas, "
591 "and may take an argument which\n"
592 "\tis set off by an equals ('=') sign.\n\n"
593 "Valid raid options are:\n"
594 "\tstride=<stride length in blocks>\n\n");
601 static void PRS(int argc, char *argv[])
607 int inode_ratio = 4096;
608 int reserved_ratio = 5;
610 int sparse_option = -1;
611 char *oldpath = getenv("PATH");
612 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) ¶m;
616 /* Update our PATH to include /sbin */
620 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
621 strcpy (newpath, PATH_SET);
622 strcat (newpath, ":");
623 strcat (newpath, oldpath);
628 setbuf(stdout, NULL);
629 setbuf(stderr, NULL);
630 initialize_ext2_error_table();
631 memset(¶m, 0, sizeof(struct ext2_super_block));
633 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
634 E2FSPROGS_VERSION, E2FSPROGS_DATE,
635 EXT2FS_VERSION, EXT2FS_DATE);
637 program_name = *argv;
638 while ((c = getopt (argc, argv,
639 "b:cf:g:i:l:m:o:qr:R:s:tvI:SFL:M:V")) != EOF)
642 size = strtoul(optarg, &tmp, 0);
643 if (size < 1024 || size > 4096 || *tmp) {
644 com_err(program_name, 0, "bad block size - %s",
648 param.s_log_block_size =
649 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
653 case 't': /* Check for bad blocks */
657 size = strtoul(optarg, &tmp, 0);
658 if (size < 1024 || size > 4096 || *tmp) {
659 com_err(program_name, 0, "bad fragment size - %s",
663 param.s_log_frag_size =
664 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
665 printf("Warning: fragments not supported. "
666 "Ignoring -f option\n");
669 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
671 com_err(program_name, 0,
672 "Illegal number for blocks per group");
675 if ((param.s_blocks_per_group % 8) != 0) {
676 com_err(program_name, 0,
677 "blocks per group must be multiple of 8");
682 inode_ratio = strtoul(optarg, &tmp, 0);
683 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
685 com_err(program_name, 0, "bad inode ratio - %s",
691 bad_blocks_filename = malloc(strlen(optarg)+1);
692 if (!bad_blocks_filename) {
693 com_err(program_name, ENOMEM,
694 "in malloc for bad_blocks_filename");
697 strcpy(bad_blocks_filename, optarg);
700 reserved_ratio = strtoul(optarg, &tmp, 0);
701 if (reserved_ratio > 50 || *tmp) {
702 com_err(program_name, 0,
703 "bad reserved blocks percent - %s",
712 param.s_rev_level = atoi(optarg);
715 sparse_option = atoi(optarg);
717 #ifdef EXT2_DYNAMIC_REV
719 param.s_inode_size = atoi(optarg);
732 volume_label = optarg;
744 /* Print version number and exit */
745 fprintf(stderr, "\tUsing %s\n",
746 error_message(EXT2_ET_BASE));
753 device_name = argv[optind];
756 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
758 com_err(program_name, 0, "bad blocks count - %s",
767 parse_raid_opts(raid_opts);
770 check_plausibility();
773 param.s_log_frag_size = param.s_log_block_size;
775 retval = ext2fs_get_device_size(device_name,
776 EXT2_BLOCK_SIZE(¶m),
778 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
779 com_err(program_name, retval,
780 "while trying to determine filesystem size");
783 if (!param.s_blocks_count) {
784 if (retval == EXT2_ET_UNIMPLEMENTED) {
785 com_err(program_name, 0,
786 "Couldn't determine device size; you "
787 "must specify\nthe size of the "
791 param.s_blocks_count = dev_size;
792 } else if (!force && (param.s_blocks_count > dev_size)) {
793 com_err(program_name, 0,
794 "Filesystem larger than apparent filesystem size.");
798 if (param.s_blocks_per_group) {
799 if (param.s_blocks_per_group < 256 ||
800 param.s_blocks_per_group > max || *tmp) {
801 com_err(program_name, 0,
802 "blocks per group count out of range");
808 * Calculate number of inodes based on the inode ratio
810 param.s_inodes_count =
811 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m))
815 * Calculate number of blocks to reserve
817 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
820 * If we are using revision #1, use the sparse super feature
823 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
824 if ((sparse_option == 1)
825 #ifdef EXT2_DYNAMIC_REV
826 || ((param.s_rev_level >= EXT2_DYNAMIC_REV) && (!sparse_option))
829 param_ext2->s_feature_ro_compat |=
830 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
834 int main (int argc, char *argv[])
836 errcode_t retval = 0;
838 badblocks_list bb_list = 0;
844 * Initialize the superblock....
846 retval = ext2fs_initialize(device_name, 0, ¶m,
847 unix_io_manager, &fs);
849 com_err(device_name, retval, "while setting up superblock");
854 * Generate a UUID for it...
856 s = (struct ext2fs_sb *) fs->super;
857 uuid_generate(s->s_uuid);
860 * Override the creator OS, if applicable
862 if (creator_os && !set_os(fs->super, creator_os)) {
863 com_err (program_name, 0, "unknown os - %s", creator_os);
868 * Set the volume label...
871 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
872 strncpy(s->s_volume_name, volume_label,
873 sizeof(s->s_volume_name));
877 * Set the last mount directory
880 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
881 strncpy(s->s_last_mounted, mount_dir,
882 sizeof(s->s_last_mounted));
888 if (bad_blocks_filename)
889 read_bb_file(fs, &bb_list, bad_blocks_filename);
891 test_disk(fs, &bb_list);
893 handle_bad_blocks(fs, bb_list);
894 fs->stride = fs_stride;
895 retval = ext2fs_allocate_tables(fs);
897 com_err(program_name, retval,
898 "while trying to allocate filesystem tables");
902 fs->super->s_state |= EXT2_ERROR_FS;
903 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
905 write_inode_tables(fs);
907 create_lost_and_found(fs);
909 create_bad_block_inode(fs, bb_list);
916 printf("Writing superblocks and "
917 "filesystem accounting information: ");