From 15406bbe98556a2109f983f1bccc0ac90632d0e0 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 31 Jan 2007 10:50:56 -0500 Subject: This creates an almost valid empty ext2 filesystem, except for a few superblock fields (blank free_blocks and free_inodes), and no root directory. (And technically, no lost+found either.) --- toys/mke2fs.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/toys/mke2fs.c b/toys/mke2fs.c index 28aea87a..bf019567 100644 --- a/toys/mke2fs.c +++ b/toys/mke2fs.c @@ -28,6 +28,7 @@ // E - extended options (stride=stripe-size blocks) // O - none,dir_index,filetype,has_journal,journal_dev,sparse_super +#define INODES_RESERVED 10 // According to http://www.opengroup.org/onlinepubs/9629399/apdxa.htm // we should generate a uuid structure by reading a clock with 100 nanosecond @@ -74,7 +75,7 @@ static void init_superblock(struct ext2_superblock *sb) temp = (TT.blocks * (uint64_t)TT.reserved_percent) /100; sb->r_blocks_count = SWAP_LE32(temp); - sb->first_data_block = TT.blocksize == SWAP_LE32(1024 ? 1 : 0); + sb->first_data_block = SWAP_LE32(TT.blocksize == 1024 ? 1 : 0); // Set blocks_per_group and frags_per_group, which is the size of an // allocation bitmap that fits in one block (I.E. how many bits per block)? @@ -98,7 +99,7 @@ static void init_superblock(struct ext2_superblock *sb) // How many blocks of inodes total, rounded up temp = TT.inodes / (TT.blocksize/sizeof(struct ext2_inode)); - if (TT.inodes & (TT.blocksize-1)) temp++; + if (temp * (TT.blocksize/sizeof(struct ext2_inode)) != TT.inodes) temp++; // How many blocks of inodes per group, again rounded up TT.inodes = temp / TT.groups; if (temp & (TT.groups-1)) TT.inodes++; @@ -116,7 +117,7 @@ static void init_superblock(struct ext2_superblock *sb) sb->state = sb->errors = SWAP_LE16(1); sb->rev_level = SWAP_LE32(1); - sb->first_ino = SWAP_LE32(11); // First 10 reserved for special uses. + sb->first_ino = SWAP_LE32(INODES_RESERVED+1); sb->inode_size = SWAP_LE16(sizeof(struct ext2_inode)); sb->feature_incompat = SWAP_LE32(EXT2_FEATURE_INCOMPAT_FILETYPE); sb->feature_ro_compat = SWAP_LE32(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER); @@ -165,9 +166,11 @@ static void bits_set(char *array, int start, int len) if ((start&7) || len<8) { array[start/8]|=(1<<(start&7)); start++; + len--; } else { array[start/8]=255; start+=8; + len-=8; } } } @@ -214,9 +217,16 @@ int mke2fs_main(void) for (i=0; i TT.blocks) end = TT.blocks & (blockbits-1); + + // Blocks used by inode table + itable = ((TT.inodes/TT.groups)*sizeof(struct ext2_inode))/TT.blocksize; + // If a superblock goes here, write it out. start = group_superblock_used(i); if (start) { @@ -233,7 +243,6 @@ int mke2fs_main(void) // Loop through groups to write group descriptor table. for(j=0; j TT.blocks) { - temp = TT.blocks & (blockbits-1); - bits_set(toybuf, temp, blockbits-temp); - } + bits_set(toybuf, 0, start+itable); + if (end!=blockbits) bits_set(toybuf, end, blockbits-end); xwrite(TT.fsfd, toybuf, TT.blocksize); - temp = TT.inodes/TT.groups; - // Write inode bitmap (TODO) + temp = TT.inodes/TT.groups; memset(toybuf, 0, TT.blocksize); - if (!i) bits_set(toybuf,0,10); + if (!i) bits_set(toybuf, 0, INODES_RESERVED); bits_set(toybuf, temp, blockbits-temp); xwrite(TT.fsfd, toybuf, TT.blocksize); @@ -303,6 +304,11 @@ int mke2fs_main(void) } } xwrite(TT.fsfd, in, TT.blocksize); + + // Write empty data blocks + memset(toybuf, 0, TT.blocksize); + for (j = start; j < end; j++) + xwrite(TT.fsfd, toybuf, TT.blocksize); } return 0; -- cgit v1.2.3