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 "ext2fs/ext2_fs.h"
48 #include "et/com_err.h"
49 #include "uuid/uuid.h"
51 #include "ext2fs/ext2fs.h"
53 #include "../version.h"
54 #include "nls-enable.h"
56 #define STRIDE_LENGTH 8
62 extern int isatty(int);
63 extern FILE *fpopen(const char *cmd, const char *mode);
65 const char * program_name = "mke2fs";
66 const char * device_name /* = NULL */;
68 /* Command line options */
77 char *bad_blocks_filename;
80 struct ext2_super_block param;
85 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
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] [-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] [-qvSV] device [blocks-count]\n"),
99 static int int_log2(int arg)
111 static int int_log10(unsigned int arg)
121 * This function sets the default parameters for a filesystem
123 * The type is specified by the user. The size is the maximum size
124 * (in megabytes) for which a set of parameters applies, with a size
125 * of zero meaning that it is the default parameter for the type.
126 * Note that order is important in the table below.
128 static char default_str[] = "default";
129 struct mke2fs_defaults {
135 { default_str, 0, 4096, 8192 },
136 { default_str, 512, 1024, 4096 },
137 { default_str, 3, 1024, 8192 },
138 { "journal", 0, 4096, 8192 },
139 { "news", 0, 4096, 4096 },
140 { "largefile", 0, 4096, 1024 * 1024 },
141 { "largefile4", 0, 4096, 4096 * 1024 },
145 static void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
146 int blocksize, int *inode_ratio)
150 struct mke2fs_defaults *p;
152 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
155 ratio = *inode_ratio;
157 fs_type = default_str;
158 for (p = settings; p->type; p++) {
159 if ((strcmp(p->type, fs_type) != 0) &&
160 (strcmp(p->type, default_str) != 0))
162 if ((p->size != 0) &&
166 *inode_ratio = p->inode_ratio;
167 if (blocksize == 0) {
168 super->s_log_frag_size = super->s_log_block_size =
169 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
173 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
177 * Helper function for read_bb_file and test_disk
179 static void invalid_block(ext2_filsys fs, blk_t blk)
181 printf(_("Bad block %u out of range; ignored.\n"), blk);
186 * Reads the bad blocks list from a file
188 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
189 const char *bad_blocks_file)
194 f = fopen(bad_blocks_file, "r");
196 com_err("read_bad_blocks_file", errno,
197 _("while trying to open %s"), bad_blocks_file);
200 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
203 com_err("ext2fs_read_bb_FILE", retval,
204 _("while reading in list of bad blocks from file"));
210 * Runs the badblocks program to test the disk
212 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
218 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
219 quiet ? "" : "-s ", fs->device_name,
220 fs->super->s_blocks_count);
222 printf(_("Running command: %s\n"), buf);
225 com_err("popen", errno,
226 _("while trying run '%s'"), buf);
229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
232 com_err("ext2fs_read_bb_FILE", retval,
233 _("while processing list of bad blocks from program"));
238 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
243 badblocks_iterate bb_iter;
253 * The primary superblock and group descriptors *must* be
254 * good; if not, abort.
256 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
257 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
258 if (ext2fs_badblocks_list_test(bb_list, i)) {
259 fprintf(stderr, _("Block %d in primary "
260 "superblock/group descriptor area bad.\n"), i);
261 fprintf(stderr, _("Blocks %d through %d must be good "
262 "in order to build a filesystem.\n"),
263 fs->super->s_first_data_block, must_be_good);
264 fprintf(stderr, _("Aborting....\n"));
270 * See if any of the bad blocks are showing up in the backup
271 * superblocks and/or group descriptors. If so, issue a
272 * warning and adjust the block counts appropriately.
274 group_block = fs->super->s_first_data_block +
275 fs->super->s_blocks_per_group;
277 for (i = 1; i < fs->group_desc_count; i++) {
279 for (j=0; j < fs->desc_blocks+1; j++) {
280 if (ext2fs_badblocks_list_test(bb_list,
284 _("Warning: the backup superblock/group descriptors at block %d contain\n"
288 group = ext2fs_group_of_blk(fs, group_block+j);
289 fs->group_desc[group].bg_free_blocks_count++;
290 fs->super->s_free_blocks_count++;
293 group_block += fs->super->s_blocks_per_group;
297 * Mark all the bad blocks as used...
299 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
301 com_err("ext2fs_badblocks_list_iterate_begin", retval,
302 _("while marking bad blocks as used"));
305 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
306 ext2fs_mark_block_bitmap(fs->block_map, blk);
307 ext2fs_badblocks_list_iterate_end(bb_iter);
311 * These functions implement a generalized progress meter.
313 struct progress_struct {
319 static void progress_init(struct progress_struct *progress,
320 char *label,__u32 max)
324 memset(progress, 0, sizeof(struct progress_struct));
329 * Figure out how many digits we need
332 sprintf(progress->format, "%%%dd/%%%dld", i, i);
333 memset(progress->backup, '\b', sizeof(progress->backup)-1);
334 progress->backup[sizeof(progress->backup)-1] = 0;
335 if ((2*i)+1 < sizeof(progress->backup))
336 progress->backup[(2*i)+1] = 0;
339 fputs(label, stdout);
343 static void progress_update(struct progress_struct *progress, __u32 val)
345 if (progress->format[0] == 0)
347 printf(progress->format, val, progress->max);
348 fputs(progress->backup, stdout);
351 static void progress_close(struct progress_struct *progress)
353 if (progress->format[0] == 0)
355 fputs(_("done \n"), stdout);
360 * Helper function which zeros out _num_ blocks starting at _blk_. In
361 * case of an error, the details of the error is returned via _ret_blk_
362 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
363 * success, and an error code on an error.
365 * As a special case, if the first argument is NULL, then it will
366 * attempt to free the static zeroizing buffer. (This is to keep
367 * programs that check for memory leaks happy.)
369 static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
370 struct progress_struct *progress,
371 blk_t *ret_blk, int *ret_count)
373 int j, count, next_update, next_update_incr;
377 /* If fs is null, clean up the static buffer and return */
385 /* Allocate the zeroizing buffer if necessary */
387 buf = malloc(fs->blocksize * STRIDE_LENGTH);
389 com_err("malloc", ENOMEM,
390 _("while allocating zeroizing buffer"));
393 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
395 /* OK, do the write loop */
397 next_update_incr = num / 100;
398 if (next_update_incr < 1)
399 next_update_incr = 1;
400 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
401 if (num-j > STRIDE_LENGTH)
402 count = STRIDE_LENGTH;
405 retval = io_channel_write_blk(fs->io, blk, count, buf);
413 if (progress && j > next_update) {
414 next_update += num / 100;
415 progress_update(progress, blk);
421 static void write_inode_tables(ext2_filsys fs)
426 struct progress_struct progress;
429 memset(&progress, 0, sizeof(progress));
431 progress_init(&progress, _("Writing inode tables: "),
432 fs->group_desc_count);
434 for (i = 0; i < fs->group_desc_count; i++) {
435 progress_update(&progress, i);
437 blk = fs->group_desc[i].bg_inode_table;
438 num = fs->inode_blocks_per_group;
440 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
442 printf(_("\nCould not write %d blocks "
443 "in inode table starting at %d: %s\n"),
444 num, blk, error_message(retval));
448 if (sync_kludge == 1)
450 else if ((i % sync_kludge) == 0)
454 zero_blocks(0, 0, 0, 0, 0, 0);
455 progress_close(&progress);
458 static void create_root_dir(ext2_filsys fs)
461 struct ext2_inode inode;
463 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
465 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
469 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
471 com_err("ext2fs_read_inode", retval,
472 _("while reading root inode"));
475 inode.i_uid = getuid();
477 inode.i_gid = getgid();
478 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
480 com_err("ext2fs_write_inode", retval,
481 _("while setting root inode ownership"));
487 static void create_lost_and_found(ext2_filsys fs)
491 const char *name = "lost+found";
495 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
497 com_err("ext2fs_mkdir", retval,
498 _("while creating /lost+found"));
502 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
504 com_err("ext2_lookup", retval,
505 _("while looking up /lost+found"));
509 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
510 if ((lpf_size += fs->blocksize) >= 16*1024)
512 retval = ext2fs_expand_dir(fs, ino);
514 com_err("ext2fs_expand_dir", retval,
515 _("while expanding /lost+found"));
521 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
525 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
526 fs->group_desc[0].bg_free_inodes_count--;
527 fs->super->s_free_inodes_count--;
528 retval = ext2fs_update_bb_inode(fs, bb_list);
530 com_err("ext2fs_update_bb_inode", retval,
531 _("while setting bad block inode"));
537 static void reserve_inodes(ext2_filsys fs)
542 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
543 ext2fs_mark_inode_bitmap(fs->inode_map, i);
544 group = ext2fs_group_of_ino(fs, i);
545 fs->group_desc[group].bg_free_inodes_count--;
546 fs->super->s_free_inodes_count--;
548 ext2fs_mark_ib_dirty(fs);
551 static void zap_sector(ext2_filsys fs, int sect, int nsect)
558 io_channel_set_blksize(fs->io, 512);
559 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
560 io_channel_set_blksize(fs->io, fs->blocksize);
562 printf(_("Warning: could not erase sector %d: %s\n"), sect,
563 error_message(retval));
566 static void create_journal_dev(ext2_filsys fs)
568 struct progress_struct progress;
574 retval = ext2fs_create_journal_superblock(fs,
575 fs->super->s_blocks_count, 0, &buf);
577 com_err("create_journal_dev", retval,
578 _("while initializing journal superblock"));
582 memset(&progress, 0, sizeof(progress));
584 progress_init(&progress, _("Zeroing journal device: "),
585 fs->super->s_blocks_count);
587 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
588 &progress, &blk, &count);
590 com_err("create_journal_dev", retval,
591 _("while zeroing journal device (block %u, count %d)"),
595 zero_blocks(0, 0, 0, 0, 0, 0);
597 retval = io_channel_write_blk(fs->io,
598 fs->super->s_first_data_block+1,
601 com_err("create_journal_dev", retval,
602 _("while writing journal superblock"));
605 progress_close(&progress);
608 static void show_stats(ext2_filsys fs)
610 struct ext2_super_block *s = fs->super;
613 int i, need, col_left;
615 if (param.s_blocks_count != s->s_blocks_count)
616 printf(_("warning: %d blocks unused.\n\n"),
617 param.s_blocks_count - s->s_blocks_count);
619 memset(buf, 0, sizeof(buf));
620 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
621 printf(_("Filesystem label=%s\n"), buf);
622 printf(_("OS type: "));
623 switch (fs->super->s_creator_os) {
624 case EXT2_OS_LINUX: printf ("Linux"); break;
625 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
626 case EXT2_OS_MASIX: printf ("Masix"); break;
627 default: printf (_("(unknown os)"));
630 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
631 s->s_log_block_size);
632 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
634 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
636 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
638 100.0 * s->s_r_blocks_count / s->s_blocks_count);
639 printf(_("First data block=%u\n"), s->s_first_data_block);
640 if (fs->group_desc_count > 1)
641 printf(_("%u block groups\n"), fs->group_desc_count);
643 printf(_("%u block group\n"), fs->group_desc_count);
644 printf(_("%u blocks per group, %u fragments per group\n"),
645 s->s_blocks_per_group, s->s_frags_per_group);
646 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
648 if (fs->group_desc_count == 1) {
653 printf(_("Superblock backups stored on blocks: "));
654 group_block = s->s_first_data_block;
656 for (i = 1; i < fs->group_desc_count; i++) {
657 group_block += s->s_blocks_per_group;
658 if (!ext2fs_bg_has_super(fs, i))
662 need = int_log10(group_block) + 2;
663 if (need > col_left) {
668 printf("%u", group_block);
674 * Set the S_CREATOR_OS field. Return true if OS is known,
677 static int set_os(struct ext2_super_block *sb, char *os)
680 sb->s_creator_os = atoi (os);
681 else if (strcasecmp(os, "linux") == 0)
682 sb->s_creator_os = EXT2_OS_LINUX;
683 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
684 sb->s_creator_os = EXT2_OS_HURD;
685 else if (strcasecmp(os, "masix") == 0)
686 sb->s_creator_os = EXT2_OS_MASIX;
692 #define PATH_SET "PATH=/sbin"
694 static void parse_raid_opts(const char *opts)
696 char *buf, *token, *next, *p, *arg;
703 fprintf(stderr, _("Couldn't allocate memory to parse "
708 for (token = buf; token && *token; token = next) {
709 p = strchr(token, ',');
715 arg = strchr(token, '=');
720 if (strcmp(token, "stride") == 0) {
725 fs_stride = strtoul(arg, &p, 0);
726 if (*p || (fs_stride == 0)) {
728 _("Invalid stride parameter.\n"));
736 fprintf(stderr, _("\nBad raid options specified.\n\n"
737 "Raid options are separated by commas, "
738 "and may take an argument which\n"
739 "\tis set off by an equals ('=') sign.\n\n"
740 "Valid raid options are:\n"
741 "\tstride=<stride length in blocks>\n\n"));
746 static __u32 ok_features[3] = {
747 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
748 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
749 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
750 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
754 static void PRS(int argc, char *argv[])
759 blk_t group_blk_max = 8192;
762 int reserved_ratio = 5;
763 ext2_ino_t num_inodes = 0;
765 char * oldpath = getenv("PATH");
766 char * raid_opts = 0;
768 int default_features = 1;
774 /* Update our PATH to include /sbin */
778 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
779 strcpy (newpath, PATH_SET);
780 strcat (newpath, ":");
781 strcat (newpath, oldpath);
786 tmp = getenv("MKE2FS_SYNC");
788 sync_kludge = atoi(tmp);
790 setbuf(stdout, NULL);
791 setbuf(stderr, NULL);
792 initialize_ext2_error_table();
793 memset(¶m, 0, sizeof(struct ext2_super_block));
794 param.s_rev_level = 1; /* Create revision 1 filesystems now */
795 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
796 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
803 if ((ut.release[0] == '1') ||
804 (ut.release[0] == '2' && ut.release[1] == '.' &&
805 ut.release[2] < '2' && ut.release[3] == '.')) {
806 param.s_rev_level = 0;
807 param.s_feature_incompat = 0;
808 param.s_feature_compat = 0;
809 param.s_feature_ro_compat = 0;
812 fprintf (stderr, "mke2fs %s (%s)\n",
813 E2FSPROGS_VERSION, E2FSPROGS_DATE);
815 program_name = *argv;
816 while ((c = getopt (argc, argv,
817 "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
820 blocksize = strtoul(optarg, &tmp, 0);
821 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
822 com_err(program_name, 0,
823 _("bad block size - %s"), optarg);
826 param.s_log_block_size =
827 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
828 group_blk_max = blocksize * 8;
830 case 'c': /* Check for bad blocks */
831 case 't': /* deprecated */
835 size = strtoul(optarg, &tmp, 0);
836 if (size < 1024 || size > 4096 || *tmp) {
837 com_err(program_name, 0,
838 _("bad fragment size - %s"),
842 param.s_log_frag_size =
843 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
844 printf(_("Warning: fragments not supported. "
845 "Ignoring -f option\n"));
848 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
850 com_err(program_name, 0,
851 _("Illegal number for blocks per group"));
854 if ((param.s_blocks_per_group % 8) != 0) {
855 com_err(program_name, 0,
856 _("blocks per group must be multiple of 8"));
861 inode_ratio = strtoul(optarg, &tmp, 0);
862 if (inode_ratio < 1024 || inode_ratio > 4096 * 1024 ||
864 com_err(program_name, 0,
865 _("bad inode ratio - %s"), optarg);
870 parse_journal_opts(optarg);
873 param.s_feature_compat |=
874 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
879 bad_blocks_filename = malloc(strlen(optarg)+1);
880 if (!bad_blocks_filename) {
881 com_err(program_name, ENOMEM,
882 _("in malloc for bad_blocks_filename"));
885 strcpy(bad_blocks_filename, optarg);
888 reserved_ratio = strtoul(optarg, &tmp, 0);
889 if (reserved_ratio > 50 || *tmp) {
890 com_err(program_name, 0,
891 _("bad reserved blocks percent - %s"),
903 param.s_rev_level = atoi(optarg);
904 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
905 param.s_feature_incompat = 0;
906 param.s_feature_compat = 0;
907 param.s_feature_ro_compat = 0;
910 case 's': /* deprecated */
912 param.s_feature_ro_compat |=
913 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
915 param.s_feature_ro_compat &=
916 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
918 #ifdef EXT2_DYNAMIC_REV
920 param.s_inode_size = atoi(optarg);
924 num_inodes = atoi(optarg);
936 volume_label = optarg;
942 if (!strcmp(optarg, "none") || default_features) {
943 param.s_feature_compat = 0;
944 param.s_feature_incompat = 0;
945 param.s_feature_ro_compat = 0;
946 default_features = 0;
948 if (!strcmp(optarg, "none"))
950 if (e2p_edit_feature(optarg,
951 ¶m.s_feature_compat,
954 _("Invalid filesystem option set: %s\n"), optarg);
968 /* Print version number and exit */
969 fprintf(stderr, _("\tUsing %s\n"),
970 error_message(EXT2_ET_BASE));
977 device_name = argv[optind];
980 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
982 if ((*tmp) || (tmp2 > 0xfffffffful)) {
983 com_err(program_name, 0, _("bad blocks count - %s"),
987 param.s_blocks_count = tmp2;
993 parse_raid_opts(raid_opts);
996 * If there's no blocksize specified and there is a journal
997 * device, use it to figure out the blocksize
999 if (blocksize == 0 && journal_device) {
1002 retval = ext2fs_open(journal_device,
1003 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1004 0, unix_io_manager, &jfs);
1006 com_err(program_name, retval,
1007 _("while trying to open journal device %s\n"),
1011 blocksize = jfs->blocksize;
1012 param.s_log_block_size =
1013 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1017 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1019 fs_type = "journal";
1021 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1022 param.s_feature_compat = 0;
1023 param.s_feature_ro_compat = 0;
1025 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1026 (param.s_feature_compat || param.s_feature_ro_compat ||
1027 param.s_feature_incompat))
1028 param.s_rev_level = 1; /* Create a revision 1 filesystem */
1031 check_plausibility(device_name);
1032 check_mount(device_name, force, _("filesystem"));
1034 param.s_log_frag_size = param.s_log_block_size;
1036 if (noaction && param.s_blocks_count) {
1037 dev_size = param.s_blocks_count;
1040 retval = ext2fs_get_device_size(device_name,
1041 EXT2_BLOCK_SIZE(¶m),
1043 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
1044 com_err(program_name, retval,
1045 _("while trying to determine filesystem size"));
1048 if (!param.s_blocks_count) {
1049 if (retval == EXT2_ET_UNIMPLEMENTED) {
1050 com_err(program_name, 0,
1051 _("Couldn't determine device size; you "
1052 "must specify\nthe size of the "
1056 if (dev_size == 0) {
1057 com_err(program_name, 0,
1058 _("Device size reported to be zero. "
1059 "Invalid partition specified, or\n\t"
1060 "partition table wasn't reread "
1061 "after running fdisk, due to\n\t"
1062 "a modified partition being busy "
1063 "and in use. You may need to reboot\n\t"
1064 "to re-read your partition table.\n"
1068 param.s_blocks_count = dev_size;
1071 } else if (!force && (param.s_blocks_count > dev_size)) {
1072 com_err(program_name, 0,
1073 _("Filesystem larger than apparent filesystem size."));
1078 * If the user asked for HAS_JOURNAL, then make sure a journal
1081 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1085 set_fs_defaults(fs_type, ¶m, blocksize, &inode_ratio);
1087 if (param.s_blocks_per_group) {
1088 if (param.s_blocks_per_group < 256 ||
1089 param.s_blocks_per_group > group_blk_max || *tmp) {
1090 com_err(program_name, 0,
1091 _("blocks per group count out of range"));
1097 * Calculate number of inodes based on the inode ratio
1099 param.s_inodes_count = num_inodes ? num_inodes :
1100 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m))
1104 * Calculate number of blocks to reserve
1106 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1110 int main (int argc, char *argv[])
1112 errcode_t retval = 0;
1114 badblocks_list bb_list = 0;
1119 setlocale(LC_MESSAGES, "");
1120 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1121 textdomain(NLS_CAT_NAME);
1126 * Initialize the superblock....
1128 retval = ext2fs_initialize(device_name, 0, ¶m,
1129 unix_io_manager, &fs);
1131 com_err(device_name, retval, _("while setting up superblock"));
1136 * Wipe out the old on-disk superblock
1139 zap_sector(fs, 2, 6);
1142 * Generate a UUID for it...
1144 uuid_generate(fs->super->s_uuid);
1147 * Add "jitter" to the superblock's check interval so that we
1148 * don't check all the filesystems at the same time. We use a
1149 * kludgy hack of using the UUID to derive a random jitter value.
1151 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1152 val += fs->super->s_uuid[i];
1153 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1156 * Override the creator OS, if applicable
1158 if (creator_os && !set_os(fs->super, creator_os)) {
1159 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1164 * For the Hurd, we will turn off filetype since it doesn't
1167 if (fs->super->s_creator_os == EXT2_OS_HURD)
1168 fs->super->s_feature_incompat &=
1169 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1172 * Set the volume label...
1175 memset(fs->super->s_volume_name, 0,
1176 sizeof(fs->super->s_volume_name));
1177 strncpy(fs->super->s_volume_name, volume_label,
1178 sizeof(fs->super->s_volume_name));
1182 * Set the last mount directory
1185 memset(fs->super->s_last_mounted, 0,
1186 sizeof(fs->super->s_last_mounted));
1187 strncpy(fs->super->s_last_mounted, mount_dir,
1188 sizeof(fs->super->s_last_mounted));
1191 if (!quiet || noaction)
1197 if (fs->super->s_feature_incompat &
1198 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1199 create_journal_dev(fs);
1204 if (bad_blocks_filename)
1205 read_bb_file(fs, &bb_list, bad_blocks_filename);
1207 test_disk(fs, &bb_list);
1209 handle_bad_blocks(fs, bb_list);
1210 fs->stride = fs_stride;
1211 retval = ext2fs_allocate_tables(fs);
1213 com_err(program_name, retval,
1214 _("while trying to allocate filesystem tables"));
1218 fs->super->s_state |= EXT2_ERROR_FS;
1219 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1221 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1222 int rsv = 65536 / fs->blocksize;
1223 unsigned long blocks = fs->super->s_blocks_count;
1224 unsigned long start;
1227 #ifdef ZAP_BOOTBLOCK
1228 zap_sector(fs, 0, 2);
1232 * Wipe out any old MD RAID (or other) metadata at the end
1233 * of the device. This will also verify that the device is
1234 * as large as we think. Be careful with very small devices.
1236 start = (blocks & ~(rsv - 1));
1240 retval = zero_blocks(fs, start, blocks - start,
1241 NULL, &ret_blk, NULL);
1244 com_err(program_name, retval,
1245 _("zeroing block %u at end of filesystem"),
1249 write_inode_tables(fs);
1250 create_root_dir(fs);
1251 create_lost_and_found(fs);
1253 create_bad_block_inode(fs, bb_list);
1256 if (journal_device) {
1260 check_plausibility(journal_device);
1261 check_mount(journal_device, force, _("journal"));
1263 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1264 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1265 fs->blocksize, unix_io_manager, &jfs);
1267 com_err(program_name, retval,
1268 _("while trying to open journal device %s\n"),
1273 printf(_("Adding journal to device %s: "),
1277 retval = ext2fs_add_journal_device(fs, jfs);
1279 com_err (program_name, retval,
1280 _("\n\twhile trying to add journal to device %s"),
1285 printf(_("done\n"));
1287 free(journal_device);
1288 } else if (journal_size) {
1289 journal_blocks = figure_journal_size(journal_size, fs);
1291 if (!journal_blocks) {
1292 fs->super->s_feature_compat &=
1293 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1297 printf(_("Creating journal (%d blocks): "),
1301 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1304 com_err (program_name, retval,
1305 _("\n\twhile trying to create journal"));
1309 printf(_("done\n"));
1314 printf(_("Writing superblocks and "
1315 "filesystem accounting information: "));
1316 retval = ext2fs_flush(fs);
1318 printf(_("\nWarning, had trouble writing out superblocks."));
1321 printf(_("done\n\n"));
1322 print_check_message(fs);