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 :-).
25 #include <sys/utsname.h>
42 #include <sys/ioctl.h>
43 #include <sys/types.h>
46 #ifdef HAVE_LINUX_FS_H
49 #include <linux/ext2_fs.h>
50 #ifdef HAVE_LINUX_MAJOR_H
51 #include <linux/major.h>
54 #include "et/com_err.h"
55 #include "uuid/uuid.h"
56 #include "ext2fs/ext2fs.h"
57 #include "../version.h"
59 /* Everything is STDC, these days */
62 #define STRIDE_LENGTH 8
68 extern int isatty(int);
69 extern FILE *fpopen(const char *cmd, const char *mode);
71 const char * program_name = "mke2fs";
72 const char * device_name = NULL;
74 /* Command line options */
81 char *bad_blocks_filename = 0;
84 struct ext2_super_block param;
85 char *creator_os = NULL;
86 char *volume_label = NULL;
87 char *mount_dir = NULL;
89 static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS);
91 static void usage(NOARGS)
93 fprintf(stderr, "Usage: %s [-c|-t|-l filename] [-b block-size] "
94 "[-f fragment-size]\n\t[-i bytes-per-inode] "
95 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
96 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
97 "[-M last-mounted-directory] [-r fs-revision]\n\t[-R raid_opts]"
98 "[-s sparse-super-flag] [-qvSV] device [blocks-count]\n",
103 static int log2(int arg)
115 static int log10(unsigned int arg)
124 static void proceed_question(NOARGS)
128 printf("Proceed anyway? (y,n) ");
129 if (getchar() != 'y')
133 #ifndef SCSI_BLK_MAJOR
134 #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
137 static void check_plausibility(NOARGS)
139 #ifdef HAVE_LINUX_MAJOR_H
143 val = stat(device_name, &s);
146 fprintf(stderr, "Could not stat %s --- %s\n",
147 device_name, error_message(errno));
149 fprintf(stderr, "\nThe device apparently does "
150 "not exist; did you specify it correctly?\n");
153 if(!S_ISBLK(s.st_mode)) {
154 printf("%s is not a block special device.\n", device_name);
157 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
158 MINOR(s.st_rdev)%64 == 0) ||
159 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
160 MINOR(s.st_rdev)%16 == 0)) {
161 printf("%s is entire device, not just one partition!\n",
168 static void check_mount(NOARGS)
173 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
175 com_err("ext2fs_check_if_mount", retval,
176 "while determining whether %s is mounted.",
180 if (!(mount_flags & EXT2_MF_MOUNTED))
183 fprintf(stderr, "%s is mounted; ", device_name);
185 fprintf(stderr, "mke2fs forced anyway. "
186 "Hope /etc/mtab is incorrect.\n");
188 fprintf(stderr, "will not make a filesystem here!\n");
194 * This function sets the default parameters for a filesystem
196 * The type is specified by the user. The size is the maximum size
197 * (in megabytes) for which a set of parameters applies, with a size
198 * of zero meaning that it is the default parameter for the type.
199 * Note that order is important in the table below.
201 static char default_str[] = "default";
202 struct mke2fs_defaults {
208 { default_str, 0, 4096, 8192 },
209 { default_str, 512, 1024, 4096 },
210 { default_str, 3, 1024, 8192 },
211 { "news", 0, 4096, 4096 },
215 static void set_fs_defaults(char *fs_type, struct ext2fs_sb *param,
216 int blocksize, int *inode_ratio)
220 struct mke2fs_defaults *p;
222 megs = (param->s_blocks_count * (EXT2_BLOCK_SIZE(param) / 1024) /
225 ratio = *inode_ratio;
227 fs_type = default_str;
228 for (p = settings; p->type; p++) {
229 if ((strcmp(p->type, fs_type) != 0) &&
230 (strcmp(p->type, default_str) != 0))
232 if ((p->size != 0) &&
236 *inode_ratio = p->inode_ratio;
237 if (blocksize == 0) {
238 param->s_log_frag_size = param->s_log_block_size =
239 log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
243 param->s_blocks_count /= EXT2_BLOCK_SIZE(param) / 1024;
247 * Helper function for read_bb_file and test_disk
249 static void invalid_block(ext2_filsys fs, blk_t blk)
251 printf("Bad block %u out of range; ignored.\n", blk);
256 * Reads the bad blocks list from a file
258 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
259 const char *bad_blocks_file)
264 f = fopen(bad_blocks_file, "r");
266 com_err("read_bad_blocks_file", errno,
267 "while trying to open %s", bad_blocks_file);
270 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
273 com_err("ext2fs_read_bb_FILE", retval,
274 "while reading in list of bad blocks from file");
280 * Runs the badblocks program to test the disk
282 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
288 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
289 quiet ? "" : "-s ", fs->device_name,
290 fs->super->s_blocks_count);
292 printf("Running command: %s\n", buf);
295 com_err("popen", errno,
296 "while trying run '%s'", buf);
299 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
302 com_err("ext2fs_read_bb_FILE", retval,
303 "while processing list of bad blocks from program");
308 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
313 badblocks_iterate bb_iter;
323 * The primary superblock and group descriptors *must* be
324 * good; if not, abort.
326 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
327 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
328 if (badblocks_list_test(bb_list, i)) {
329 fprintf(stderr, "Block %d in primary superblock/group "
330 "descriptor area bad.\n", i);
331 fprintf(stderr, "Blocks %d through %d must be good "
332 "in order to build a filesystem.\n",
333 fs->super->s_first_data_block, must_be_good);
334 fprintf(stderr, "Aborting....\n");
340 * See if any of the bad blocks are showing up in the backup
341 * superblocks and/or group descriptors. If so, issue a
342 * warning and adjust the block counts appropriately.
344 group_block = fs->super->s_first_data_block +
345 fs->super->s_blocks_per_group;
347 for (i = 1; i < fs->group_desc_count; i++) {
349 for (j=0; j < fs->desc_blocks+1; j++) {
350 if (badblocks_list_test(bb_list, group_block +
354 "Warning: the backup superblock/group descriptors at block %d contain\n"
358 group = ext2fs_group_of_blk(fs, group_block+j);
359 fs->group_desc[group].bg_free_blocks_count++;
360 fs->super->s_free_blocks_count++;
363 group_block += fs->super->s_blocks_per_group;
367 * Mark all the bad blocks as used...
369 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
371 com_err("badblocks_list_iterate_begin", retval,
372 "while marking bad blocks as used");
375 while (badblocks_list_iterate(bb_iter, &blk))
376 ext2fs_mark_block_bitmap(fs->block_map, blk);
377 badblocks_list_iterate_end(bb_iter);
380 static void write_inode_tables(ext2_filsys fs)
384 int i, j, num, count;
386 char format[20], backup[80];
390 mke2fs_sync = getenv("MKE2FS_SYNC");
392 sync_kludge = atoi(mke2fs_sync);
394 buf = malloc(fs->blocksize * STRIDE_LENGTH);
396 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
399 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
402 * Figure out how many digits we need
404 i = log10(fs->group_desc_count);
405 sprintf(format, "%%%dd/%%%dld", i, i);
406 memset(backup, '\b', sizeof(backup)-1);
407 backup[sizeof(backup)-1] = 0;
408 if ((2*i)+1 < sizeof(backup))
412 printf("Writing inode tables: ");
413 for (i = 0; i < fs->group_desc_count; i++) {
415 printf(format, i, fs->group_desc_count);
417 blk = fs->group_desc[i].bg_inode_table;
418 num = fs->inode_blocks_per_group;
420 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
421 if (num-j > STRIDE_LENGTH)
422 count = STRIDE_LENGTH;
425 retval = io_channel_write_blk(fs->io, blk, count, buf);
427 printf("Warning: could not write %d blocks "
428 "in inode table starting at %d: %s\n",
429 count, blk, error_message(retval));
432 fputs(backup, stdout);
434 if (sync_kludge == 1)
436 else if ((i % sync_kludge) == 0)
442 fputs("done \n", stdout);
445 static void create_root_dir(ext2_filsys fs)
448 struct ext2_inode inode;
450 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
452 com_err("ext2fs_mkdir", retval, "while creating root dir");
456 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
458 com_err("ext2fs_read_inode", retval,
459 "while reading root inode");
462 inode.i_uid = getuid();
464 inode.i_gid = getgid();
465 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
467 com_err("ext2fs_write_inode", retval,
468 "while setting root inode ownership");
474 static void create_lost_and_found(ext2_filsys fs)
478 const char *name = "lost+found";
482 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
484 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
488 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
490 com_err("ext2_lookup", retval, "while looking up /lost+found");
494 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
495 if ((lpf_size += fs->blocksize) >= 16*1024)
497 retval = ext2fs_expand_dir(fs, ino);
499 com_err("ext2fs_expand_dir", retval,
500 "while expanding /lost+found");
506 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
510 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
511 fs->group_desc[0].bg_free_inodes_count--;
512 fs->super->s_free_inodes_count--;
513 retval = ext2fs_update_bb_inode(fs, bb_list);
515 com_err("ext2fs_update_bb_inode", retval,
516 "while setting bad block inode");
522 static void reserve_inodes(ext2_filsys fs)
527 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
528 ext2fs_mark_inode_bitmap(fs->inode_map, i);
529 group = ext2fs_group_of_ino(fs, i);
530 fs->group_desc[group].bg_free_inodes_count--;
531 fs->super->s_free_inodes_count--;
533 ext2fs_mark_ib_dirty(fs);
537 static void zap_bootblock(ext2_filsys fs)
544 retval = io_channel_write_blk(fs->io, 0, -512, buf);
546 printf("Warning: could not erase block 0: %s\n",
547 error_message(retval));
552 static void show_stats(ext2_filsys fs)
554 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
557 int i, need, col_left;
559 if (param.s_blocks_count != s->s_blocks_count)
560 printf("warning: %d blocks unused.\n\n",
561 param.s_blocks_count - s->s_blocks_count);
563 memset(buf, 0, sizeof(buf));
564 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
565 printf("Filesystem label=%s\n", buf);
567 switch (fs->super->s_creator_os) {
568 case EXT2_OS_LINUX: printf ("Linux"); break;
569 case EXT2_OS_HURD: printf ("GNU/hurd"); break;
570 case EXT2_OS_MASIX: printf ("Masix"); break;
571 default: printf ("(unknown os)");
574 printf("Block size=%u (log=%u)\n", fs->blocksize,
575 s->s_log_block_size);
576 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
578 printf("%u inodes, %u blocks\n", s->s_inodes_count,
580 printf("%u blocks (%2.2f%%) reserved for the super user\n",
582 100.0 * s->s_r_blocks_count / s->s_blocks_count);
583 printf("First data block=%u\n", s->s_first_data_block);
584 printf("%lu block group%s\n", fs->group_desc_count,
585 (fs->group_desc_count > 1) ? "s" : "");
586 printf("%u blocks per group, %u fragments per group\n",
587 s->s_blocks_per_group, s->s_frags_per_group);
588 printf("%u inodes per group\n", s->s_inodes_per_group);
590 if (fs->group_desc_count == 1) {
595 printf("Superblock backups stored on blocks: ");
596 group_block = s->s_first_data_block;
598 for (i = 1; i < fs->group_desc_count; i++) {
599 group_block += s->s_blocks_per_group;
600 if (!ext2fs_bg_has_super(fs, i))
602 need = log10(group_block) + 2;
603 if (need > col_left) {
608 printf("%u", group_block);
609 if (i != fs->group_desc_count - 1)
615 #ifndef HAVE_STRCASECMP
616 static int strcasecmp (char *s1, char *s2)
619 int ch1 = *s1++, ch2 = *s2++;
627 return *s1 ? 1 : *s2 ? -1 : 0;
632 * Set the S_CREATOR_OS field. Return true if OS is known,
635 static int set_os(struct ext2_super_block *sb, char *os)
638 sb->s_creator_os = atoi (os);
639 else if (strcasecmp(os, "linux") == 0)
640 sb->s_creator_os = EXT2_OS_LINUX;
641 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
642 sb->s_creator_os = EXT2_OS_HURD;
643 else if (strcasecmp(os, "masix") == 0)
644 sb->s_creator_os = EXT2_OS_MASIX;
650 #define PATH_SET "PATH=/sbin"
652 static void parse_raid_opts(const char *opts)
654 char *buf, *token, *next, *p, *arg;
661 fprintf(stderr, "Couldn't allocate memory to parse "
666 for (token = buf; token && *token; token = next) {
667 p = strchr(token, ',');
673 arg = strchr(token, '=');
678 if (strcmp(token, "stride") == 0) {
683 fs_stride = strtoul(arg, &p, 0);
684 if (*p || (fs_stride == 0)) {
685 fprintf(stderr, "Invalid stride parameter.\n");
693 fprintf(stderr, "\nBad raid options specified.\n\n"
694 "Raid options are separated by commas, "
695 "and may take an argument which\n"
696 "\tis set off by an equals ('=') sign.\n\n"
697 "Valid raid options are:\n"
698 "\tstride=<stride length in blocks>\n\n");
705 static void PRS(int argc, char *argv[])
713 int reserved_ratio = 5;
714 ino_t num_inodes = 0;
716 int sparse_option = 1;
717 char *oldpath = getenv("PATH");
718 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) ¶m;
729 if (ut.release[0] == '2' && ut.release[1] == '.' &&
730 ut.release[2] < '2' && ut.release[3] == '.')
733 /* Update our PATH to include /sbin */
737 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
738 strcpy (newpath, PATH_SET);
739 strcat (newpath, ":");
740 strcat (newpath, oldpath);
745 setbuf(stdout, NULL);
746 setbuf(stderr, NULL);
747 initialize_ext2_error_table();
748 memset(¶m, 0, sizeof(struct ext2_super_block));
749 param.s_rev_level = 1; /* Create revision 1 filesystems now */
751 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
752 E2FSPROGS_VERSION, E2FSPROGS_DATE,
753 EXT2FS_VERSION, EXT2FS_DATE);
755 program_name = *argv;
756 while ((c = getopt (argc, argv,
757 "b:cf:g:i:l:m:no:qr:R:s:tvI:ST:FL:M:N:V")) != EOF)
760 blocksize = strtoul(optarg, &tmp, 0);
761 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
762 com_err(program_name, 0, "bad block size - %s",
766 param.s_log_block_size =
767 log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
771 case 't': /* Check for bad blocks */
775 size = strtoul(optarg, &tmp, 0);
776 if (size < 1024 || size > 4096 || *tmp) {
777 com_err(program_name, 0, "bad fragment size - %s",
781 param.s_log_frag_size =
782 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
783 printf("Warning: fragments not supported. "
784 "Ignoring -f option\n");
787 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
789 com_err(program_name, 0,
790 "Illegal number for blocks per group");
793 if ((param.s_blocks_per_group % 8) != 0) {
794 com_err(program_name, 0,
795 "blocks per group must be multiple of 8");
800 inode_ratio = strtoul(optarg, &tmp, 0);
801 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
803 com_err(program_name, 0, "bad inode ratio - %s",
809 bad_blocks_filename = malloc(strlen(optarg)+1);
810 if (!bad_blocks_filename) {
811 com_err(program_name, ENOMEM,
812 "in malloc for bad_blocks_filename");
815 strcpy(bad_blocks_filename, optarg);
818 reserved_ratio = strtoul(optarg, &tmp, 0);
819 if (reserved_ratio > 50 || *tmp) {
820 com_err(program_name, 0,
821 "bad reserved blocks percent - %s",
833 param.s_rev_level = atoi(optarg);
836 sparse_option = atoi(optarg);
838 #ifdef EXT2_DYNAMIC_REV
840 param.s_inode_size = atoi(optarg);
844 num_inodes = atoi(optarg);
856 volume_label = optarg;
871 /* Print version number and exit */
872 fprintf(stderr, "\tUsing %s\n",
873 error_message(EXT2_ET_BASE));
880 device_name = argv[optind];
883 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
885 com_err(program_name, 0, "bad blocks count - %s",
894 parse_raid_opts(raid_opts);
897 check_plausibility();
900 param.s_log_frag_size = param.s_log_block_size;
902 if (noaction && param.s_blocks_count) {
903 dev_size = param.s_blocks_count;
906 retval = ext2fs_get_device_size(device_name,
907 EXT2_BLOCK_SIZE(¶m),
909 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
910 com_err(program_name, retval,
911 "while trying to determine filesystem size");
914 if (!param.s_blocks_count) {
915 if (retval == EXT2_ET_UNIMPLEMENTED) {
916 com_err(program_name, 0,
917 "Couldn't determine device size; you "
918 "must specify\nthe size of the "
922 param.s_blocks_count = dev_size;
923 } else if (!force && (param.s_blocks_count > dev_size)) {
924 com_err(program_name, 0,
925 "Filesystem larger than apparent filesystem size.");
929 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
931 if (param.s_blocks_per_group) {
932 if (param.s_blocks_per_group < 256 ||
933 param.s_blocks_per_group > max || *tmp) {
934 com_err(program_name, 0,
935 "blocks per group count out of range");
941 * Calculate number of inodes based on the inode ratio
943 param.s_inodes_count = num_inodes ? num_inodes :
944 ((long long) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m))
948 * Calculate number of blocks to reserve
950 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
952 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
954 param_ext2->s_feature_ro_compat |=
955 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
959 int main (int argc, char *argv[])
961 errcode_t retval = 0;
963 badblocks_list bb_list = 0;
969 * Initialize the superblock....
971 retval = ext2fs_initialize(device_name, 0, ¶m,
972 unix_io_manager, &fs);
974 com_err(device_name, retval, "while setting up superblock");
979 * Generate a UUID for it...
981 s = (struct ext2fs_sb *) fs->super;
982 uuid_generate(s->s_uuid);
985 * Override the creator OS, if applicable
987 if (creator_os && !set_os(fs->super, creator_os)) {
988 com_err (program_name, 0, "unknown os - %s", creator_os);
993 * Set the volume label...
996 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
997 strncpy(s->s_volume_name, volume_label,
998 sizeof(s->s_volume_name));
1002 * Set the last mount directory
1005 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1006 strncpy(s->s_last_mounted, mount_dir,
1007 sizeof(s->s_last_mounted));
1010 if (!quiet || noaction)
1016 if (bad_blocks_filename)
1017 read_bb_file(fs, &bb_list, bad_blocks_filename);
1019 test_disk(fs, &bb_list);
1021 handle_bad_blocks(fs, bb_list);
1022 fs->stride = fs_stride;
1023 retval = ext2fs_allocate_tables(fs);
1025 com_err(program_name, retval,
1026 "while trying to allocate filesystem tables");
1030 fs->super->s_state |= EXT2_ERROR_FS;
1031 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1033 write_inode_tables(fs);
1034 create_root_dir(fs);
1035 create_lost_and_found(fs);
1037 create_bad_block_inode(fs, bb_list);
1038 #ifdef ZAP_BOOTBLOCK
1044 printf("Writing superblocks and "
1045 "filesystem accounting information: ");
1046 retval = ext2fs_flush(fs);
1048 printf("\nWarning, had trouble writing out superblocks.");