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 <linux/ext2_fs.h>
49 #include "et/com_err.h"
50 #include "uuid/uuid.h"
52 #include "ext2fs/ext2fs.h"
54 #include "../version.h"
55 #include "nls-enable.h"
57 #define STRIDE_LENGTH 8
63 extern int isatty(int);
64 extern FILE *fpopen(const char *cmd, const char *mode);
66 const char * program_name = "mke2fs";
67 const char * device_name /* = NULL */;
69 /* Command line options */
78 char *bad_blocks_filename;
81 struct ext2_super_block param;
86 int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
88 static void usage(void)
90 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
91 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]"
92 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
93 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
94 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
95 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
96 "[-qvSV] device [blocks-count]\n"),
101 static int int_log2(int arg)
113 static int int_log10(unsigned int arg)
123 * This function sets the default parameters for a filesystem
125 * The type is specified by the user. The size is the maximum size
126 * (in megabytes) for which a set of parameters applies, with a size
127 * of zero meaning that it is the default parameter for the type.
128 * Note that order is important in the table below.
130 static char default_str[] = "default";
131 struct mke2fs_defaults {
137 { default_str, 0, 4096, 8192 },
138 { default_str, 512, 1024, 4096 },
139 { default_str, 3, 1024, 8192 },
140 { "journal", 0, 4096, 8192 },
141 { "news", 0, 4096, 4096 },
142 { "largefile", 0, 4096, 1024 * 1024 },
143 { "largefile4", 0, 4096, 4096 * 1024 },
147 static void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
148 int blocksize, int *inode_ratio)
152 struct mke2fs_defaults *p;
154 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
157 ratio = *inode_ratio;
159 fs_type = default_str;
160 for (p = settings; p->type; p++) {
161 if ((strcmp(p->type, fs_type) != 0) &&
162 (strcmp(p->type, default_str) != 0))
164 if ((p->size != 0) &&
168 *inode_ratio = p->inode_ratio;
169 if (blocksize == 0) {
170 super->s_log_frag_size = super->s_log_block_size =
171 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
175 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
179 * Helper function for read_bb_file and test_disk
181 static void invalid_block(ext2_filsys fs, blk_t blk)
183 printf(_("Bad block %u out of range; ignored.\n"), blk);
188 * Reads the bad blocks list from a file
190 static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
191 const char *bad_blocks_file)
196 f = fopen(bad_blocks_file, "r");
198 com_err("read_bad_blocks_file", errno,
199 _("while trying to open %s"), bad_blocks_file);
202 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
205 com_err("ext2fs_read_bb_FILE", retval,
206 _("while reading in list of bad blocks from file"));
212 * Runs the badblocks program to test the disk
214 static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
220 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
221 quiet ? "" : "-s ", fs->device_name,
222 fs->super->s_blocks_count);
224 printf(_("Running command: %s\n"), buf);
227 com_err("popen", errno,
228 _("while trying run '%s'"), buf);
231 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
234 com_err("ext2fs_read_bb_FILE", retval,
235 _("while processing list of bad blocks from program"));
240 static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
245 badblocks_iterate bb_iter;
255 * The primary superblock and group descriptors *must* be
256 * good; if not, abort.
258 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
259 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
260 if (badblocks_list_test(bb_list, i)) {
261 fprintf(stderr, _("Block %d in primary "
262 "superblock/group descriptor area bad.\n"), i);
263 fprintf(stderr, _("Blocks %d through %d must be good "
264 "in order to build a filesystem.\n"),
265 fs->super->s_first_data_block, must_be_good);
266 fprintf(stderr, _("Aborting....\n"));
272 * See if any of the bad blocks are showing up in the backup
273 * superblocks and/or group descriptors. If so, issue a
274 * warning and adjust the block counts appropriately.
276 group_block = fs->super->s_first_data_block +
277 fs->super->s_blocks_per_group;
279 for (i = 1; i < fs->group_desc_count; i++) {
281 for (j=0; j < fs->desc_blocks+1; j++) {
282 if (badblocks_list_test(bb_list, group_block +
286 _("Warning: the backup superblock/group descriptors at block %d contain\n"
290 group = ext2fs_group_of_blk(fs, group_block+j);
291 fs->group_desc[group].bg_free_blocks_count++;
292 fs->super->s_free_blocks_count++;
295 group_block += fs->super->s_blocks_per_group;
299 * Mark all the bad blocks as used...
301 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
303 com_err("badblocks_list_iterate_begin", retval,
304 _("while marking bad blocks as used"));
307 while (badblocks_list_iterate(bb_iter, &blk))
308 ext2fs_mark_block_bitmap(fs->block_map, blk);
309 badblocks_list_iterate_end(bb_iter);
313 * These functions implement a generalized progress meter.
315 struct progress_struct {
321 static void progress_init(struct progress_struct *progress,
322 char *label,__u32 max)
326 memset(progress, 0, sizeof(struct progress_struct));
331 * Figure out how many digits we need
334 sprintf(progress->format, "%%%dd/%%%dld", i, i);
335 memset(progress->backup, '\b', sizeof(progress->backup)-1);
336 progress->backup[sizeof(progress->backup)-1] = 0;
337 if ((2*i)+1 < sizeof(progress->backup))
338 progress->backup[(2*i)+1] = 0;
341 fputs(label, stdout);
345 static void progress_update(struct progress_struct *progress, __u32 val)
347 if (progress->format[0] == 0)
349 printf(progress->format, val, progress->max);
350 fputs(progress->backup, stdout);
353 static void progress_close(struct progress_struct *progress)
355 if (progress->format[0] == 0)
357 fputs(_("done \n"), stdout);
362 * Helper function which zeros out _num_ blocks starting at _blk_. In
363 * case of an error, the details of the error is returned via _ret_blk_
364 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
365 * success, and an error code on an error.
367 * As a special case, if the first argument is NULL, then it will
368 * attempt to free the static zeroizing buffer. (This is to keep
369 * programs that check for memory leaks happy.)
371 static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
372 struct progress_struct *progress,
373 blk_t *ret_blk, int *ret_count)
375 int j, count, next_update, next_update_incr;
379 /* If fs is null, clean up the static buffer and return */
387 /* Allocate the zeroizing buffer if necessary */
389 buf = malloc(fs->blocksize * STRIDE_LENGTH);
391 com_err("malloc", ENOMEM,
392 _("while allocating zeroizing buffer"));
395 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
397 /* OK, do the write loop */
399 next_update_incr = num / 100;
400 if (next_update_incr < 1)
401 next_update_incr = 1;
402 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
403 if (num-j > STRIDE_LENGTH)
404 count = STRIDE_LENGTH;
407 retval = io_channel_write_blk(fs->io, blk, count, buf);
415 if (progress && j > next_update) {
416 next_update += num / 100;
417 progress_update(progress, blk);
423 static void write_inode_tables(ext2_filsys fs)
428 struct progress_struct progress;
431 memset(&progress, 0, sizeof(progress));
433 progress_init(&progress, _("Writing inode tables: "),
434 fs->group_desc_count);
436 for (i = 0; i < fs->group_desc_count; i++) {
437 progress_update(&progress, i);
439 blk = fs->group_desc[i].bg_inode_table;
440 num = fs->inode_blocks_per_group;
442 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
444 printf(_("\nCould not write %d blocks "
445 "in inode table starting at %d: %s\n"),
446 num, blk, error_message(retval));
450 if (sync_kludge == 1)
452 else if ((i % sync_kludge) == 0)
456 zero_blocks(0, 0, 0, 0, 0, 0);
457 progress_close(&progress);
460 static void create_root_dir(ext2_filsys fs)
463 struct ext2_inode inode;
465 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
467 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
471 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
473 com_err("ext2fs_read_inode", retval,
474 _("while reading root inode"));
477 inode.i_uid = getuid();
479 inode.i_gid = getgid();
480 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
482 com_err("ext2fs_write_inode", retval,
483 _("while setting root inode ownership"));
489 static void create_lost_and_found(ext2_filsys fs)
493 const char *name = "lost+found";
497 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
499 com_err("ext2fs_mkdir", retval,
500 _("while creating /lost+found"));
504 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
506 com_err("ext2_lookup", retval,
507 _("while looking up /lost+found"));
511 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
512 if ((lpf_size += fs->blocksize) >= 16*1024)
514 retval = ext2fs_expand_dir(fs, ino);
516 com_err("ext2fs_expand_dir", retval,
517 _("while expanding /lost+found"));
523 static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
527 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
528 fs->group_desc[0].bg_free_inodes_count--;
529 fs->super->s_free_inodes_count--;
530 retval = ext2fs_update_bb_inode(fs, bb_list);
532 com_err("ext2fs_update_bb_inode", retval,
533 _("while setting bad block inode"));
539 static void reserve_inodes(ext2_filsys fs)
544 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
545 ext2fs_mark_inode_bitmap(fs->inode_map, i);
546 group = ext2fs_group_of_ino(fs, i);
547 fs->group_desc[group].bg_free_inodes_count--;
548 fs->super->s_free_inodes_count--;
550 ext2fs_mark_ib_dirty(fs);
553 static void zap_sector(ext2_filsys fs, int sect)
560 io_channel_set_blksize(fs->io, 512);
561 retval = io_channel_write_blk(fs->io, sect, -512, buf);
562 io_channel_set_blksize(fs->io, fs->blocksize);
564 printf(_("Warning: could not erase sector %d: %s\n"), sect,
565 error_message(retval));
568 static void create_journal_dev(ext2_filsys fs)
570 struct progress_struct progress;
577 memset(&progress, 0, sizeof(progress));
579 progress_init(&progress, _("Zeroing journal device: "),
580 fs->super->s_blocks_count);
582 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
583 &progress, &blk, &count);
585 com_err("create_journal_dev", retval,
586 "while zeroing journal device (block %u, count %d",
590 zero_blocks(0, 0, 0, 0, 0, 0);
592 retval = ext2fs_create_journal_superblock(fs,
593 fs->super->s_blocks_count, 0, &buf);
595 com_err("create_journal_dev", retval,
596 _("while initialization journal superblock"));
599 retval = io_channel_write_blk(fs->io,
600 fs->super->s_first_data_block+1,
603 com_err("create_journal_dev", retval,
604 _("while writing journal superblock"));
607 progress_close(&progress);
610 static void show_stats(ext2_filsys fs)
612 struct ext2_super_block *s = fs->super;
615 int i, need, col_left;
617 if (param.s_blocks_count != s->s_blocks_count)
618 printf(_("warning: %d blocks unused.\n\n"),
619 param.s_blocks_count - s->s_blocks_count);
621 memset(buf, 0, sizeof(buf));
622 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
623 printf(_("Filesystem label=%s\n"), buf);
624 printf(_("OS type: "));
625 switch (fs->super->s_creator_os) {
626 case EXT2_OS_LINUX: printf ("Linux"); break;
627 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
628 case EXT2_OS_MASIX: printf ("Masix"); break;
629 default: printf (_("(unknown os)"));
632 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
633 s->s_log_block_size);
634 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
636 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
638 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
640 100.0 * s->s_r_blocks_count / s->s_blocks_count);
641 printf(_("First data block=%u\n"), s->s_first_data_block);
642 if (fs->group_desc_count > 1)
643 printf(_("%u block groups\n"), fs->group_desc_count);
645 printf(_("%u block group\n"), fs->group_desc_count);
646 printf(_("%u blocks per group, %u fragments per group\n"),
647 s->s_blocks_per_group, s->s_frags_per_group);
648 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
650 if (fs->group_desc_count == 1) {
655 printf(_("Superblock backups stored on blocks: "));
656 group_block = s->s_first_data_block;
658 for (i = 1; i < fs->group_desc_count; i++) {
659 group_block += s->s_blocks_per_group;
660 if (!ext2fs_bg_has_super(fs, i))
664 need = int_log10(group_block) + 2;
665 if (need > col_left) {
670 printf("%u", group_block);
676 * Set the S_CREATOR_OS field. Return true if OS is known,
679 static int set_os(struct ext2_super_block *sb, char *os)
682 sb->s_creator_os = atoi (os);
683 else if (strcasecmp(os, "linux") == 0)
684 sb->s_creator_os = EXT2_OS_LINUX;
685 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
686 sb->s_creator_os = EXT2_OS_HURD;
687 else if (strcasecmp(os, "masix") == 0)
688 sb->s_creator_os = EXT2_OS_MASIX;
694 #define PATH_SET "PATH=/sbin"
696 static void parse_raid_opts(const char *opts)
698 char *buf, *token, *next, *p, *arg;
705 fprintf(stderr, _("Couldn't allocate memory to parse "
710 for (token = buf; token && *token; token = next) {
711 p = strchr(token, ',');
717 arg = strchr(token, '=');
722 if (strcmp(token, "stride") == 0) {
727 fs_stride = strtoul(arg, &p, 0);
728 if (*p || (fs_stride == 0)) {
730 _("Invalid stride parameter.\n"));
738 fprintf(stderr, _("\nBad raid options specified.\n\n"
739 "Raid options are separated by commas, "
740 "and may take an argument which\n"
741 "\tis set off by an equals ('=') sign.\n\n"
742 "Valid raid options are:\n"
743 "\tstride=<stride length in blocks>\n\n"));
748 static __u32 ok_features[3] = {
749 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */
750 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
751 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
752 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
756 static void PRS(int argc, char *argv[])
761 blk_t group_blk_max = 8192;
764 int reserved_ratio = 5;
765 ext2_ino_t num_inodes = 0;
767 int sparse_option = 1;
768 char * oldpath = getenv("PATH");
769 char * raid_opts = 0;
771 const char * feature_set = "filetype";
780 if ((ut.release[0] == '1') ||
781 (ut.release[0] == '2' && ut.release[1] == '.' &&
782 ut.release[2] < '2' && ut.release[3] == '.'))
785 /* Update our PATH to include /sbin */
789 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
790 strcpy (newpath, PATH_SET);
791 strcat (newpath, ":");
792 strcat (newpath, oldpath);
797 tmp = getenv("MKE2FS_SYNC");
799 sync_kludge = atoi(tmp);
801 setbuf(stdout, NULL);
802 setbuf(stderr, NULL);
803 initialize_ext2_error_table();
804 memset(¶m, 0, sizeof(struct ext2_super_block));
805 param.s_rev_level = 1; /* Create revision 1 filesystems now */
807 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
808 E2FSPROGS_VERSION, E2FSPROGS_DATE,
809 EXT2FS_VERSION, EXT2FS_DATE);
811 program_name = *argv;
812 while ((c = getopt (argc, argv,
813 "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF)
816 blocksize = strtoul(optarg, &tmp, 0);
817 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
818 com_err(program_name, 0,
819 _("bad block size - %s"), optarg);
822 param.s_log_block_size =
823 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
824 group_blk_max = blocksize * 8;
827 case 't': /* Check for bad blocks */
831 size = strtoul(optarg, &tmp, 0);
832 if (size < 1024 || size > 4096 || *tmp) {
833 com_err(program_name, 0,
834 _("bad fragment size - %s"),
838 param.s_log_frag_size =
839 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
840 printf(_("Warning: fragments not supported. "
841 "Ignoring -f option\n"));
844 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
846 com_err(program_name, 0,
847 _("Illegal number for blocks per group"));
850 if ((param.s_blocks_per_group % 8) != 0) {
851 com_err(program_name, 0,
852 _("blocks per group must be multiple of 8"));
857 inode_ratio = strtoul(optarg, &tmp, 0);
858 if (inode_ratio < 1024 || inode_ratio > 4096 * 1024 ||
860 com_err(program_name, 0,
861 _("bad inode ratio - %s"), optarg);
866 parse_journal_opts(optarg);
873 bad_blocks_filename = malloc(strlen(optarg)+1);
874 if (!bad_blocks_filename) {
875 com_err(program_name, ENOMEM,
876 _("in malloc for bad_blocks_filename"));
879 strcpy(bad_blocks_filename, optarg);
882 reserved_ratio = strtoul(optarg, &tmp, 0);
883 if (reserved_ratio > 50 || *tmp) {
884 com_err(program_name, 0,
885 _("bad reserved blocks percent - %s"),
897 param.s_rev_level = atoi(optarg);
900 sparse_option = atoi(optarg);
902 #ifdef EXT2_DYNAMIC_REV
904 param.s_inode_size = atoi(optarg);
908 num_inodes = atoi(optarg);
920 volume_label = optarg;
926 feature_set = optarg;
938 /* Print version number and exit */
939 fprintf(stderr, _("\tUsing %s\n"),
940 error_message(EXT2_ET_BASE));
947 device_name = argv[optind];
950 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
952 if ((*tmp) || (tmp2 > 0xfffffffful)) {
953 com_err(program_name, 0, _("bad blocks count - %s"),
957 param.s_blocks_count = tmp2;
963 parse_raid_opts(raid_opts);
965 /* Parse the user-supplied feature_set, if any. */
966 if (feature_set && !strncasecmp(feature_set, "none", 4))
968 if (feature_set && e2p_edit_feature(feature_set,
969 ¶m.s_feature_compat,
971 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
977 check_plausibility(device_name);
978 check_mount(device_name, force, _("filesystem"));
980 param.s_log_frag_size = param.s_log_block_size;
982 if (noaction && param.s_blocks_count) {
983 dev_size = param.s_blocks_count;
986 retval = ext2fs_get_device_size(device_name,
987 EXT2_BLOCK_SIZE(¶m),
989 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
990 com_err(program_name, retval,
991 _("while trying to determine filesystem size"));
994 if (!param.s_blocks_count) {
995 if (retval == EXT2_ET_UNIMPLEMENTED) {
996 com_err(program_name, 0,
997 _("Couldn't determine device size; you "
998 "must specify\nthe size of the "
1002 if (dev_size == 0) {
1003 com_err(program_name, 0,
1004 _("Device size reported to be zero. "
1005 "Invalid partition specified, or\n\t"
1006 "partition table wasn't reread "
1007 "after running fdisk, due to\n\t"
1008 "a modified partition being busy "
1009 "and in use. You may need to reboot\n\t"
1010 "to re-read your partition table.\n"
1014 param.s_blocks_count = dev_size;
1017 } else if (!force && (param.s_blocks_count > dev_size)) {
1018 com_err(program_name, 0,
1019 _("Filesystem larger than apparent filesystem size."));
1024 * If the user asked for HAS_JOURNAL, then make sure a journal
1027 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1031 (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV))
1032 fs_type = "journal";
1033 set_fs_defaults(fs_type, ¶m, blocksize, &inode_ratio);
1035 if (param.s_blocks_per_group) {
1036 if (param.s_blocks_per_group < 256 ||
1037 param.s_blocks_per_group > group_blk_max || *tmp) {
1038 com_err(program_name, 0,
1039 _("blocks per group count out of range"));
1045 * Calculate number of inodes based on the inode ratio
1047 param.s_inodes_count = num_inodes ? num_inodes :
1048 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m))
1052 * Calculate number of blocks to reserve
1054 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
1056 /* Turn off features not supported by the earlier filesystem version */
1057 if (param.s_rev_level == 0) {
1062 param.s_feature_ro_compat |=
1063 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
1067 int main (int argc, char *argv[])
1069 errcode_t retval = 0;
1071 badblocks_list bb_list = 0;
1076 setlocale(LC_MESSAGES, "");
1077 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1078 textdomain(NLS_CAT_NAME);
1083 * Initialize the superblock....
1085 retval = ext2fs_initialize(device_name, 0, ¶m,
1086 unix_io_manager, &fs);
1088 com_err(device_name, retval, _("while setting up superblock"));
1093 * Wipe out the old on-disk superblock
1098 * Generate a UUID for it...
1100 uuid_generate(fs->super->s_uuid);
1103 * Add "jitter" to the superblock's check interval so that we
1104 * don't check all the filesystems at the same time. We use a
1105 * kludgy hack of using the UUID to derive a random jitter value.
1107 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1108 val += fs->super->s_uuid[i];
1109 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1112 * Override the creator OS, if applicable
1114 if (creator_os && !set_os(fs->super, creator_os)) {
1115 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1120 * For the Hurd, we will turn off filetype since it doesn't
1123 if (fs->super->s_creator_os == EXT2_OS_HURD)
1124 fs->super->s_feature_incompat &=
1125 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1128 * Set the volume label...
1131 memset(fs->super->s_volume_name, 0,
1132 sizeof(fs->super->s_volume_name));
1133 strncpy(fs->super->s_volume_name, volume_label,
1134 sizeof(fs->super->s_volume_name));
1138 * Set the last mount directory
1141 memset(fs->super->s_last_mounted, 0,
1142 sizeof(fs->super->s_last_mounted));
1143 strncpy(fs->super->s_last_mounted, mount_dir,
1144 sizeof(fs->super->s_last_mounted));
1147 if (!quiet || noaction)
1153 if (fs->super->s_feature_incompat &
1154 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1155 create_journal_dev(fs);
1160 if (bad_blocks_filename)
1161 read_bb_file(fs, &bb_list, bad_blocks_filename);
1163 test_disk(fs, &bb_list);
1165 handle_bad_blocks(fs, bb_list);
1166 fs->stride = fs_stride;
1167 retval = ext2fs_allocate_tables(fs);
1169 com_err(program_name, retval,
1170 _("while trying to allocate filesystem tables"));
1174 fs->super->s_state |= EXT2_ERROR_FS;
1175 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1177 write_inode_tables(fs);
1178 create_root_dir(fs);
1179 create_lost_and_found(fs);
1181 create_bad_block_inode(fs, bb_list);
1182 #ifdef ZAP_BOOTBLOCK
1187 if (journal_device) {
1191 check_plausibility(journal_device);
1192 check_mount(journal_device, force, _("journal"));
1194 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
1195 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1196 fs->blocksize, unix_io_manager, &jfs);
1198 com_err(program_name, retval,
1199 _("while trying to open journal device %s\n"),
1204 printf(_("Creating journal on device %s: "),
1208 retval = ext2fs_add_journal_device(fs, jfs);
1210 com_err (program_name, retval,
1211 _("while trying to add journal to device %s"),
1216 printf(_("done\n"));
1218 } else if (journal_size) {
1219 if (journal_size < 0)
1220 journal_blocks = journal_default_size(fs->super->s_blocks_count);
1222 journal_blocks = journal_size * 1024 /
1223 (fs->blocksize / 1024);
1225 if (!journal_blocks) {
1226 fs->super->s_feature_compat &=
1227 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1231 printf(_("Creating journal (%d blocks): "),
1235 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1238 com_err (program_name, retval,
1239 _("while trying to create journal"));
1243 printf(_("done\n"));
1248 printf(_("Writing superblocks and "
1249 "filesystem accounting information: "));
1250 retval = ext2fs_flush(fs);
1252 printf(_("\nWarning, had trouble writing out superblocks."));
1255 printf(_("done\n"));
1256 printf(_("e2fsck will be run every %d mounts or %4g days"),
1257 fs->super->s_max_mnt_count,
1258 (double)fs->super->s_checkinterval / (3600 * 24));