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>
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
44 #ifdef HAVE_LINUX_FS_H
47 #include <linux/ext2_fs.h>
48 #ifdef HAVE_LINUX_MAJOR_H
49 #include <linux/major.h>
50 #include <sys/stat.h> /* Only need sys/stat.h for major nr test */
53 #include "et/com_err.h"
54 #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] [-O feature[,...]]\n\t"
98 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
99 "[-qvSV] device [blocks-count]\n",
104 static int log2(int arg)
116 static int log10(unsigned int arg)
125 static void proceed_question(NOARGS)
129 printf("Proceed anyway? (y,n) ");
130 if (getchar() != 'y')
134 #ifndef SCSI_BLK_MAJOR
135 #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
138 static void check_plausibility(NOARGS)
140 #ifdef HAVE_LINUX_MAJOR_H
144 val = stat(device_name, &s);
147 fprintf(stderr, "Could not stat %s --- %s\n",
148 device_name, error_message(errno));
150 fprintf(stderr, "\nThe device apparently does "
151 "not exist; did you specify it correctly?\n");
154 if(!S_ISBLK(s.st_mode)) {
155 printf("%s is not a block special device.\n", device_name);
158 } else if ((MAJOR(s.st_rdev) == HD_MAJOR &&
159 MINOR(s.st_rdev)%64 == 0) ||
160 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
161 MINOR(s.st_rdev)%16 == 0)) {
162 printf("%s is entire device, not just one partition!\n",
169 static void check_mount(NOARGS)
174 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
176 com_err("ext2fs_check_if_mount", retval,
177 "while determining whether %s is mounted.",
181 if (!(mount_flags & EXT2_MF_MOUNTED))
184 fprintf(stderr, "%s is mounted; ", device_name);
186 fprintf(stderr, "mke2fs forced anyway. "
187 "Hope /etc/mtab is incorrect.\n");
189 fprintf(stderr, "will not make a filesystem here!\n");
195 * This function sets the default parameters for a filesystem
197 * The type is specified by the user. The size is the maximum size
198 * (in megabytes) for which a set of parameters applies, with a size
199 * of zero meaning that it is the default parameter for the type.
200 * Note that order is important in the table below.
202 static char default_str[] = "default";
203 struct mke2fs_defaults {
209 { default_str, 0, 4096, 8192 },
210 { default_str, 512, 1024, 4096 },
211 { default_str, 3, 1024, 8192 },
212 { "news", 0, 4096, 4096 },
216 static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super,
217 int blocksize, int *inode_ratio)
221 struct mke2fs_defaults *p;
223 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
226 ratio = *inode_ratio;
228 fs_type = default_str;
229 for (p = settings; p->type; p++) {
230 if ((strcmp(p->type, fs_type) != 0) &&
231 (strcmp(p->type, default_str) != 0))
233 if ((p->size != 0) &&
237 *inode_ratio = p->inode_ratio;
238 if (blocksize == 0) {
239 super->s_log_frag_size = super->s_log_block_size =
240 log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
244 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
248 * Helper function for read_bb_file and test_disk
250 static void invalid_block(ext2_filsys fs, blk_t blk)
252 printf("Bad block %u out of range; ignored.\n", blk);
257 * Reads the bad blocks list from a file
259 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
260 const char *bad_blocks_file)
265 f = fopen(bad_blocks_file, "r");
267 com_err("read_bad_blocks_file", errno,
268 "while trying to open %s", bad_blocks_file);
271 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
274 com_err("ext2fs_read_bb_FILE", retval,
275 "while reading in list of bad blocks from file");
281 * Runs the badblocks program to test the disk
283 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
289 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
290 quiet ? "" : "-s ", fs->device_name,
291 fs->super->s_blocks_count);
293 printf("Running command: %s\n", buf);
296 com_err("popen", errno,
297 "while trying run '%s'", buf);
300 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
303 com_err("ext2fs_read_bb_FILE", retval,
304 "while processing list of bad blocks from program");
309 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
314 badblocks_iterate bb_iter;
324 * The primary superblock and group descriptors *must* be
325 * good; if not, abort.
327 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
328 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
329 if (badblocks_list_test(bb_list, i)) {
330 fprintf(stderr, "Block %d in primary superblock/group "
331 "descriptor area bad.\n", i);
332 fprintf(stderr, "Blocks %d through %d must be good "
333 "in order to build a filesystem.\n",
334 fs->super->s_first_data_block, must_be_good);
335 fprintf(stderr, "Aborting....\n");
341 * See if any of the bad blocks are showing up in the backup
342 * superblocks and/or group descriptors. If so, issue a
343 * warning and adjust the block counts appropriately.
345 group_block = fs->super->s_first_data_block +
346 fs->super->s_blocks_per_group;
348 for (i = 1; i < fs->group_desc_count; i++) {
350 for (j=0; j < fs->desc_blocks+1; j++) {
351 if (badblocks_list_test(bb_list, group_block +
355 "Warning: the backup superblock/group descriptors at block %d contain\n"
359 group = ext2fs_group_of_blk(fs, group_block+j);
360 fs->group_desc[group].bg_free_blocks_count++;
361 fs->super->s_free_blocks_count++;
364 group_block += fs->super->s_blocks_per_group;
368 * Mark all the bad blocks as used...
370 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
372 com_err("badblocks_list_iterate_begin", retval,
373 "while marking bad blocks as used");
376 while (badblocks_list_iterate(bb_iter, &blk))
377 ext2fs_mark_block_bitmap(fs->block_map, blk);
378 badblocks_list_iterate_end(bb_iter);
381 static void write_inode_tables(ext2_filsys fs)
385 int i, j, num, count;
387 char format[20], backup[80];
391 mke2fs_sync = getenv("MKE2FS_SYNC");
393 sync_kludge = atoi(mke2fs_sync);
395 buf = malloc(fs->blocksize * STRIDE_LENGTH);
397 com_err("malloc", ENOMEM, "while allocating zeroizing buffer");
400 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
403 * Figure out how many digits we need
405 i = log10(fs->group_desc_count);
406 sprintf(format, "%%%dd/%%%dld", i, i);
407 memset(backup, '\b', sizeof(backup)-1);
408 backup[sizeof(backup)-1] = 0;
409 if ((2*i)+1 < sizeof(backup))
413 printf("Writing inode tables: ");
414 for (i = 0; i < fs->group_desc_count; i++) {
416 printf(format, i, fs->group_desc_count);
418 blk = fs->group_desc[i].bg_inode_table;
419 num = fs->inode_blocks_per_group;
421 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
422 if (num-j > STRIDE_LENGTH)
423 count = STRIDE_LENGTH;
426 retval = io_channel_write_blk(fs->io, blk, count, buf);
428 printf("Warning: could not write %d blocks "
429 "in inode table starting at %d: %s\n",
430 count, blk, error_message(retval));
433 fputs(backup, stdout);
435 if (sync_kludge == 1)
437 else if ((i % sync_kludge) == 0)
443 fputs("done \n", stdout);
446 static void create_root_dir(ext2_filsys fs)
449 struct ext2_inode inode;
451 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
453 com_err("ext2fs_mkdir", retval, "while creating root dir");
457 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
459 com_err("ext2fs_read_inode", retval,
460 "while reading root inode");
463 inode.i_uid = getuid();
465 inode.i_gid = getgid();
466 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
468 com_err("ext2fs_write_inode", retval,
469 "while setting root inode ownership");
475 static void create_lost_and_found(ext2_filsys fs)
479 const char *name = "lost+found";
483 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
485 com_err("ext2fs_mkdir", retval, "while creating /lost+found");
489 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
491 com_err("ext2_lookup", retval, "while looking up /lost+found");
495 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
496 if ((lpf_size += fs->blocksize) >= 16*1024)
498 retval = ext2fs_expand_dir(fs, ino);
500 com_err("ext2fs_expand_dir", retval,
501 "while expanding /lost+found");
507 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
511 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
512 fs->group_desc[0].bg_free_inodes_count--;
513 fs->super->s_free_inodes_count--;
514 retval = ext2fs_update_bb_inode(fs, bb_list);
516 com_err("ext2fs_update_bb_inode", retval,
517 "while setting bad block inode");
523 static void reserve_inodes(ext2_filsys fs)
528 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
529 ext2fs_mark_inode_bitmap(fs->inode_map, i);
530 group = ext2fs_group_of_ino(fs, i);
531 fs->group_desc[group].bg_free_inodes_count--;
532 fs->super->s_free_inodes_count--;
534 ext2fs_mark_ib_dirty(fs);
538 static void zap_bootblock(ext2_filsys fs)
545 retval = io_channel_write_blk(fs->io, 0, -512, buf);
547 printf("Warning: could not erase block 0: %s\n",
548 error_message(retval));
553 static void show_stats(ext2_filsys fs)
555 struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super;
558 int i, need, col_left;
560 if (param.s_blocks_count != s->s_blocks_count)
561 printf("warning: %d blocks unused.\n\n",
562 param.s_blocks_count - s->s_blocks_count);
564 memset(buf, 0, sizeof(buf));
565 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
566 printf("Filesystem label=%s\n", buf);
568 switch (fs->super->s_creator_os) {
569 case EXT2_OS_LINUX: printf ("Linux"); break;
570 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
571 case EXT2_OS_MASIX: printf ("Masix"); break;
572 default: printf ("(unknown os)");
575 printf("Block size=%u (log=%u)\n", fs->blocksize,
576 s->s_log_block_size);
577 printf("Fragment size=%u (log=%u)\n", fs->fragsize,
579 printf("%u inodes, %u blocks\n", s->s_inodes_count,
581 printf("%u blocks (%2.2f%%) reserved for the super user\n",
583 100.0 * s->s_r_blocks_count / s->s_blocks_count);
584 printf("First data block=%u\n", s->s_first_data_block);
585 printf("%lu block group%s\n", fs->group_desc_count,
586 (fs->group_desc_count > 1) ? "s" : "");
587 printf("%u blocks per group, %u fragments per group\n",
588 s->s_blocks_per_group, s->s_frags_per_group);
589 printf("%u inodes per group\n", s->s_inodes_per_group);
591 if (fs->group_desc_count == 1) {
596 printf("Superblock backups stored on blocks: ");
597 group_block = s->s_first_data_block;
599 for (i = 1; i < fs->group_desc_count; i++) {
600 group_block += s->s_blocks_per_group;
601 if (!ext2fs_bg_has_super(fs, i))
605 need = log10(group_block) + 2;
606 if (need > col_left) {
611 printf("%u", group_block);
616 #ifndef HAVE_STRCASECMP
617 static int strcasecmp (char *s1, char *s2)
620 int ch1 = *s1++, ch2 = *s2++;
628 return *s1 ? 1 : *s2 ? -1 : 0;
633 * Set the S_CREATOR_OS field. Return true if OS is known,
636 static int set_os(struct ext2_super_block *sb, char *os)
639 sb->s_creator_os = atoi (os);
640 else if (strcasecmp(os, "linux") == 0)
641 sb->s_creator_os = EXT2_OS_LINUX;
642 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
643 sb->s_creator_os = EXT2_OS_HURD;
644 else if (strcasecmp(os, "masix") == 0)
645 sb->s_creator_os = EXT2_OS_MASIX;
651 #define PATH_SET "PATH=/sbin"
653 static void parse_raid_opts(const char *opts)
655 char *buf, *token, *next, *p, *arg;
662 fprintf(stderr, "Couldn't allocate memory to parse "
667 for (token = buf; token && *token; token = next) {
668 p = strchr(token, ',');
674 arg = strchr(token, '=');
679 if (strcmp(token, "stride") == 0) {
684 fs_stride = strtoul(arg, &p, 0);
685 if (*p || (fs_stride == 0)) {
686 fprintf(stderr, "Invalid stride parameter.\n");
694 fprintf(stderr, "\nBad raid options specified.\n\n"
695 "Raid options are separated by commas, "
696 "and may take an argument which\n"
697 "\tis set off by an equals ('=') sign.\n\n"
698 "Valid raid options are:\n"
699 "\tstride=<stride length in blocks>\n\n");
704 static __u32 ok_features[3] = {
706 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
707 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
711 static void PRS(int argc, char *argv[])
719 int reserved_ratio = 5;
720 ino_t num_inodes = 0;
722 int sparse_option = 0;
723 char * oldpath = getenv("PATH");
724 struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) ¶m;
725 char * raid_opts = 0;
727 const char * feature_set = "filetype,sparse_super";
736 if ((ut.release[0] == '1') ||
737 (ut.release[0] == '2' && ut.release[1] == '.' &&
738 ut.release[2] < '2' && ut.release[3] == '.'))
741 /* Update our PATH to include /sbin */
745 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
746 strcpy (newpath, PATH_SET);
747 strcat (newpath, ":");
748 strcat (newpath, oldpath);
753 setbuf(stdout, NULL);
754 setbuf(stderr, NULL);
755 initialize_ext2_error_table();
756 memset(¶m, 0, sizeof(struct ext2_super_block));
757 param.s_rev_level = 1; /* Create revision 1 filesystems now */
759 fprintf (stderr, "mke2fs %s, %s for EXT2 FS %s, %s\n",
760 E2FSPROGS_VERSION, E2FSPROGS_DATE,
761 EXT2FS_VERSION, EXT2FS_DATE);
763 program_name = *argv;
764 while ((c = getopt (argc, argv,
765 "b:cf:g:i:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
768 blocksize = strtoul(optarg, &tmp, 0);
769 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
770 com_err(program_name, 0, "bad block size - %s",
774 param.s_log_block_size =
775 log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
779 case 't': /* Check for bad blocks */
783 size = strtoul(optarg, &tmp, 0);
784 if (size < 1024 || size > 4096 || *tmp) {
785 com_err(program_name, 0, "bad fragment size - %s",
789 param.s_log_frag_size =
790 log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
791 printf("Warning: fragments not supported. "
792 "Ignoring -f option\n");
795 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
797 com_err(program_name, 0,
798 "Illegal number for blocks per group");
801 if ((param.s_blocks_per_group % 8) != 0) {
802 com_err(program_name, 0,
803 "blocks per group must be multiple of 8");
808 inode_ratio = strtoul(optarg, &tmp, 0);
809 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
811 com_err(program_name, 0, "bad inode ratio - %s",
817 bad_blocks_filename = malloc(strlen(optarg)+1);
818 if (!bad_blocks_filename) {
819 com_err(program_name, ENOMEM,
820 "in malloc for bad_blocks_filename");
823 strcpy(bad_blocks_filename, optarg);
826 reserved_ratio = strtoul(optarg, &tmp, 0);
827 if (reserved_ratio > 50 || *tmp) {
828 com_err(program_name, 0,
829 "bad reserved blocks percent - %s",
841 param.s_rev_level = atoi(optarg);
844 sparse_option = atoi(optarg);
846 #ifdef EXT2_DYNAMIC_REV
848 param.s_inode_size = atoi(optarg);
852 num_inodes = atoi(optarg);
864 volume_label = optarg;
870 feature_set = optarg;
882 /* Print version number and exit */
883 fprintf(stderr, "\tUsing %s\n",
884 error_message(EXT2_ET_BASE));
891 device_name = argv[optind];
894 param.s_blocks_count = strtoul(argv[optind++], &tmp, 0);
896 com_err(program_name, 0, "bad blocks count - %s",
905 parse_raid_opts(raid_opts);
908 check_plausibility();
911 param.s_log_frag_size = param.s_log_block_size;
913 if (noaction && param.s_blocks_count) {
914 dev_size = param.s_blocks_count;
917 retval = ext2fs_get_device_size(device_name,
918 EXT2_BLOCK_SIZE(¶m),
920 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
921 com_err(program_name, retval,
922 "while trying to determine filesystem size");
925 if (!param.s_blocks_count) {
926 if (retval == EXT2_ET_UNIMPLEMENTED) {
927 com_err(program_name, 0,
928 "Couldn't determine device size; you "
929 "must specify\nthe size of the "
933 param.s_blocks_count = dev_size;
934 } else if (!force && (param.s_blocks_count > dev_size)) {
935 com_err(program_name, 0,
936 "Filesystem larger than apparent filesystem size.");
940 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
942 if (param.s_blocks_per_group) {
943 if (param.s_blocks_per_group < 256 ||
944 param.s_blocks_per_group > max || *tmp) {
945 com_err(program_name, 0,
946 "blocks per group count out of range");
952 * Calculate number of inodes based on the inode ratio
954 param.s_inodes_count = num_inodes ? num_inodes :
955 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m))
959 * Calculate number of blocks to reserve
961 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
963 #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
965 param_ext2->s_feature_ro_compat |=
966 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
968 if (feature_set && !strncasecmp(feature_set, "none", 4))
970 if (feature_set && e2p_edit_feature(feature_set,
971 ¶m_ext2->s_feature_compat,
973 fprintf(stderr, "Invalid filesystem option set: %s\n",
979 int main (int argc, char *argv[])
981 errcode_t retval = 0;
983 badblocks_list bb_list = 0;
989 * Initialize the superblock....
991 retval = ext2fs_initialize(device_name, 0, ¶m,
992 unix_io_manager, &fs);
994 com_err(device_name, retval, "while setting up superblock");
999 * Generate a UUID for it...
1001 s = (struct ext2fs_sb *) fs->super;
1002 uuid_generate(s->s_uuid);
1005 * Override the creator OS, if applicable
1007 if (creator_os && !set_os(fs->super, creator_os)) {
1008 com_err (program_name, 0, "unknown os - %s", creator_os);
1013 * Set the volume label...
1016 memset(s->s_volume_name, 0, sizeof(s->s_volume_name));
1017 strncpy(s->s_volume_name, volume_label,
1018 sizeof(s->s_volume_name));
1022 * Set the last mount directory
1025 memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted));
1026 strncpy(s->s_last_mounted, mount_dir,
1027 sizeof(s->s_last_mounted));
1030 if (!quiet || noaction)
1036 if (bad_blocks_filename)
1037 read_bb_file(fs, &bb_list, bad_blocks_filename);
1039 test_disk(fs, &bb_list);
1041 handle_bad_blocks(fs, bb_list);
1042 fs->stride = fs_stride;
1043 retval = ext2fs_allocate_tables(fs);
1045 com_err(program_name, retval,
1046 "while trying to allocate filesystem tables");
1050 fs->super->s_state |= EXT2_ERROR_FS;
1051 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1053 write_inode_tables(fs);
1054 create_root_dir(fs);
1055 create_lost_and_found(fs);
1057 create_bad_block_inode(fs, bb_list);
1058 #ifdef ZAP_BOOTBLOCK
1064 printf("Writing superblocks and "
1065 "filesystem accounting information: ");
1066 retval = ext2fs_flush(fs);
1068 printf("\nWarning, had trouble writing out superblocks.");