Chinaunix首页 | 论坛 | 博客
  • 博客访问: 848732
  • 博文数量: 581
  • 博客积分: 7803
  • 博客等级: 少将
  • 技术积分: 3653
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-27 08:21
文章分类

全部博文(581)

文章存档

2013年(7)

2012年(414)

2011年(159)

2009年(1)

分类: LINUX

2011-06-27 14:27:03

参考文档: ext2磁盘格式分析

1.生成一个ext3.img文件
# dd if=/dev/zero of=ext3.img bs=4096 count=10240
10240+0 records in
10240+0 records out
# ls -l
total 40960
-rwxr-xr-x  1 root root 41943040 Jun  8 13:40 ext3.img

2.格式化为ext3
# mkfs.ext3 ext3.img ////////////这里默认的block size是1024
mke2fs 1.38 (30-Jun-2005)
ext3.img is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
10240 inodes, 40960 blocks
2048 blocks (5.00%) reserved for the super user
First data block=1
5 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
#

# mkfs.ext3 ext3.img -b 4096 /////这里指定block size为4096
mke2fs 1.38 (30-Jun-2005)
ext3.img is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
10240 inodes, 10240 blocks
512 blocks (5.00%) reserved for the super user
First data block=0
1 block group
32768 blocks per group, 32768 fragments per group
10240 inodes per group
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
#
从命令输出可以得到:
Block size=4096 (log=2)    /////block size是4096
10240 inodes, 10240 blocks /////inode数量,block数量
1 block group              /////block group数

3.挂载
# mkdir myext3
# mount -o loop ext3.img myext3
# ls -a myext3/
.  ..  lost+found
#
这个分区中有三个文件夹:. .. lost+found

4.打开ext3.img分析
vi打开,在命令模式下输入:%!xxd,来设置16进制显示
分区格式为:
  +-------+-----------+----------+-----------+
  | boot  |   block   |   ...    |   block   |
  | block |  group 0  |   ...    |  group N  |
  +-------+-----------+----------+-----------+

第一部分:引导块
第二部分:块组
一般硬盘512Byte是一个扇区,多个扇区组成一块,多个块组成一个块组,多个块组和
启动块成分区。块的大小可以在格式化视进行指定,最小为1024Byte即2个扇区。分区
时没有指定块的大小则块大小为1024Byte,每个块组大小相等。
  +-------+-----------+----------+-----------+
  | boot  |   block   |   ...    |   block   |
  | block |  group 0  |   ...    |  group N  |
  +-------+-----------+----------+-----------+
      /--.                   \-------------
  ./--                                     \-------------
 +-------+-------+--------+--------+-------+-------------\-----+
 | super | group | block  | inode  | inode |  data             |
 | block | desc  | bitmap | bitmap | table |  blocks           |
 +-------+-------+--------+--------+-------+-------------------+
  1 block n block 1 block  1 block  n block

第一部分:超级块
第二部分:组描述
第三部分:块位图
第四部分:节点位图
第五部分:节点表
第六部分:数据块

任何ext234分区中的第一个1024空间都是不受ext234文件系统的管理,这个是为分区的
引导扇区所保留的,ext234其余部分组成块组(block group)。
问题1:一个分区有多少个block group呢?
A:这取决于分区的大小和block的大小。其主要限制在于block bitmap,因为block bitmap
   必须放在一个单独的block中,block bitmap用来标识一个block group中block的占用
   和空闲状态。所以,每个block group中至多有8*b个block,b是以字节为单位的block
   大小,因此,block group的总数大约是s/(8*b),s是分区所有的block数。

1.ext3.img中0x000-0x3FF,是为引导扇区保留的
2.ext3.img中00x0400-0x7FF,是b||k group 0的superblock
00000400: 00 28 00 00  //s_inodes_count     =0x00002800=10240
       4: 00 28 00 00  //s_blocks_count     =0x00002800=10240
       8: 00 02 00 00  //s_r_blocks_count   =0x00000200=512(=10240*5%)
       C: B5 22 00 00  //s_free_blocks_count=0x000022B5=8885
00000410: F5 27 00 00  //s_free_inodes_count=0x000027F5=10229
       4: 00 00 00 00  //s_first_data_block =0x00000000
       8: 02 00 00 00  //s_log_block_size   =0x00000002
       C: 02 00 00 00  //s_log_frag_size    =0x00000002
00000420: 00 80 00 00  //s_blocks_per_group =0x00008000=32768
       4: 00 80 00 00  //s_frags_per_group  =0x00008000=32768
       8: 00 28 00 00  //s_inodes_per_group =0x00002800=10240
       C: 46 0E EF 4D  //s_mtime            =0x4DEF0E46(mount time)
00000430: 46 0E EF 4D  //s_wtime            =0x4DEF0E46(write time)
       4: 01 00        //s_mnt_count        =0x0001(mount count)
       6: 14 00        //s_max_mnt_count    =0x0014=20(max mount count)
       8: 53 EF        //s_magic            =0xEF53
       A: 01 00        //s_state            =0x0001
       C: 01 00        //s_errors           =0x0001
       E: 00 00        //s_minor_rev_level  =0x0000
...
00000450: 00 00
       2: 00 00
       4: 0B 00 00 00  //s_first_ino        =0x0000000B=11(First non-reserved inode)
       8: 80 00        //s_inode_size       =0x0008,inode size=2^8=256bytes
       A: 00 00        //s_block_group_nr   =0x0000(block group # of this superblock)

Inode count:            10240
Block count:            10240
Reserved block count:   512
Free blocks:            8885
Free inodes:            10229
First block:            0
Block size:             4096
Fragment size:          4096
Blocks per group:       32768
Fragments per group:    32768
Inodes per group:       10240
Inode blocks per group: 320
First inode:            11
Inode size:             128
Journal inode:          8

Group 0: (Blocks 0-10239)
  Primary superblock at 0,
  Group descriptors at 1-1
  Block bitmap at 2 (+2),
  Inode bitmap at 3 (+3)
  Inode table at 4-323 (+4)
  8885 free blocks, 10229 free inodes, 2 directories
  Free blocks: 1355-10239
  Free inodes: 12-10240

block   0                1            2        3        4       ...      10240
       +----------------+------------+--------+--------+-------+--------+
       |+----+-----+---+|+----------+|+------+|+------+|+-----+|+------+|
       ||boot|super|...|||  group   |||block |||inode |||inode|||      ||
       ||    |block|   |||descriptor|||bitmap|||bitmap|||table|||      ||
       |+----+-----+---+|+----------+|+------+|+------+|+-----+|+------+|
       +----------------+------------+--------+--------+-------+--------+
sector  0    2     4     8            16       24       32      ...      81920
byteoff 0    0x400       0x1000       0x2000   0x3000   0x4000

Inode count:            10240   ///inode数量
Block count:            10240   ///block数量
Reserved block count:   512     ///block保留
Free blocks:            8885    ///空闲block数量
Free inodes:            10229   ///空闲inode数量
First block:            0       ///第一个block
Block size:             4096    ///block大小
Fragment size:          4096    ///fragment大小
Blocks per group:       32768   ///每个group中的block数量
Fragments per group:    32768   ///每个group中的fragment数量
Inodes per group:       10240   ///每个group中的inode数量
Inode blocks per group: 320     ///每个group中inode所占用的block数量
First inode:            11      ///第一个inode
Inode size:             128     ///inode大小
Journal inode:          8

Group 0: (Blocks 0-10239)
  Primary superblock at 0,
  Group descriptors at 1-1
  Block bitmap at 2 (+2),
  Inode bitmap at 3 (+3)
  Inode table at 4-323 (+4)
  8885 free blocks, 10229 free inodes, 2 directories
  Free blocks: 1355-10239
  Free inodes: 12-10240

ext3.img:
              sector  byteoff
boot          0-1     0x0000
superblock    2-3     0x0400
-             4-7     0x0800
group desc    8-15    0x1000
block bitmap  16-23   0x2000
inode bitmap  24-31   0x3000
inode table   32-39   0x4000
00001000: 02 00 00 00  //bg_block_bitmap=0x00000002,block bitmap的块号
       4: 03 00 00 00  //bg_inode_bitmap=0x00000003,inode bitmap的块号
       8: 04 00 00 00  //bg_inode_table =0x00000004,inode table的块号
       C: B5 22        //bg_free_blocks_count=0x22B5=8885
       E: F5 27        //bg_free_inodes_count=0x27F5=10229
00001010: 02 00        //bg_used_dirs_count=0x0002
...
00002000: FF FF FF FF
       4: FF FF FF FF
...
000020A8: FF 07 00 00
//从0x2000-0x20A9,共0xA9=169个FF,共有169*8=1352个1,加上3个共1355=10240-8885
//11111111_11111111_..._11111111_00000111
//0x2000   0x2001       0x20A8   0x20A9
...
//////////////////////////////////////////////////////////////////////////
00002500: FF FF FF FF
...
00002FFC: FF FF FF FF
//从0x2500-0x2FFF,共0xB00=2816个bit位为1
/////////////////////////////////////////////////////////////////////////
...
00003000: FF 07 00 00  //有11个bit位为1
...
00003500: FF FF FF FF
...
00003FFC: FF FF FF FF
//从0x3500-0x3FFF,共0xB00=2816个bit位为1
阅读(1213) | 评论(0) | 转发(0) |
0

上一篇:ext234 on-disk layout

下一篇:积累

给主人留下些什么吧!~~