ext2 被系统保留的部分分析

从 Block 24 开始就是数据块了。块组描述符中指出,空闲的数据块有 986 个,由于文件系统是新创建的,空闲块是连续的 Block 38-1023,用掉了前面的 Block 24-37。
  • 这些被保留的信息究竟是什么呢?如果不弄清楚,想实现预分配就比较困难。

  • 先看看 disk 的十六进制数据。

$ od -tx1 -Ax disk
000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
000400 80 00 00 00 00 04 00 00 33 00 00 00 da 03 00 00
000410 75 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
000420 00 20 00 00 00 20 00 00 80 00 00 00 00 00 00 00
000430 4b 47 f1 5a 00 00 ff ff 53 ef 01 00 01 00 00 00
000440 4b 47 f1 5a 00 00 00 00 00 00 00 00 01 00 00 00
000450 00 00 00 00 0b 00 00 00 80 00 00 00 38 00 00 00
000460 02 00 00 00 03 00 00 00 5a 9c 02 44 31 e5 41 7a
000470 97 66 ee b9 58 a7 3d db 00 00 00 00 00 00 00 00
000480 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
0004c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 00
0004d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0004e0 00 00 00 00 00 00 00 00 00 00 00 00 ad 79 7b d7
0004f0 a1 1b 4b 45 a8 f1 a7 ff 00 46 89 fe 01 00 00 00
000500 0c 00 00 00 00 00 00 00 4b 47 f1 5a 00 00 00 00
000510 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
000560 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000570 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
000800 06 00 00 00 07 00 00 00 08 00 00 00 da 03 75 00
000810 02 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00
000820 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
001800 ff ff ff ff 1f 00 00 00 00 00 00 00 00 00 00 00
001810 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
001870 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80
001880 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
*
001c00 ff 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00
001c10 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
*
002000 00 00 00 00 00 00 00 00 4b 47 f1 5a 4b 47 f1 5a
002010 4b 47 f1 5a 00 00 00 00 00 00 00 00 00 00 00 00
002020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
002080 ed 41 00 00 00 04 00 00 4b 47 f1 5a 4b 47 f1 5a
002090 4b 47 f1 5a 00 00 00 00 00 00 03 00 02 00 00 00
0020a0 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00
0020b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
002300 80 81 00 00 00 30 04 04 4b 47 f1 5a 4b 47 f1 5a
002310 4b 47 f1 5a 00 00 00 00 00 00 01 00 08 00 00 00
002320 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
002350 00 00 00 00 00 00 00 00 00 00 00 00 25 00 00 00
002360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
002500 c0 41 00 00 00 30 00 00 4b 47 f1 5a 4b 47 f1 5a
002510 4b 47 f1 5a 00 00 00 00 00 00 02 00 18 00 00 00
002520 00 00 00 00 00 00 00 00 19 00 00 00 1a 00 00 00
002530 1b 00 00 00 1c 00 00 00 1d 00 00 00 1e 00 00 00
002540 1f 00 00 00 20 00 00 00 21 00 00 00 22 00 00 00
002550 23 00 00 00 24 00 00 00 00 00 00 00 00 00 00 00
002560 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
006000 02 00 00 00 0c 00 01 02 2e 00 00 00 02 00 00 00
006010 0c 00 02 02 2e 2e 00 00 0b 00 00 00 e8 03 0a 02
006020 6c 6f 73 74 2b 66 6f 75 6e 64 00 00 00 00 00 00
006030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
006400 0b 00 00 00 0c 00 01 02 2e 00 00 00 02 00 00 00
006410 f4 03 02 02 2e 2e 00 00 00 00 00 00 00 00 00 00
006420 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
006800 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
006810 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
006c00 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
006c10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
007000 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
007010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
007400 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
007410 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
007800 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
007810 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
007c00 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
007c10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
008000 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
008010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
008400 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
008410 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
008800 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
008810 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
008c00 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
008c10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
009000 00 00 00 00 00 04 00 00 00 00 00 00 00 00 00 00
009010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
009400 00 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00
009410 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
100000

根据前面几节的分析:


  • 从 000480 到 0007ff,这些数据似乎没用。因此将其删除。

  • 从 002000 到 00207f,也删去。

  • 假设,我们不要格式化自己产生的三个链接(根目录的 ., .., lost+found 下的 ..),全部清空,系统是否仍然能够认出 disk 呢?这里将 002080 之后的数据也全部清零,试一试。


清零代码如下

int TryEraseExt2()
{
    uint32 i = 0;

    for(i=0x480; i<0x800; i++)
        MFileWriteByte(i, 0x00);
    for(i=0x2000; i<0xffff0; i++)
        MFileWriteByte(i, 0x00);

    return 0;
}
  • 在 main 函数里执行 TryEraseExt2(),并用 dumpe2fs 查看 disk 信息,结果如下:
$ dumpe2fs disk
dumpe2fs 1.42.13 (17-May-2015)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          5a9c0244-31e5-417a-9766-eeb958a73ddb
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      ext_attr resize_inode dir_index filetype sparse_super large_file
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              128
Block count:              1024
Reserved block count:     51
Free blocks:              986
Free inodes:              117
First block:              1
Block size:               1024
Fragment size:            1024
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         128
Inode blocks per group:   16
Last mount time:          n/a
Last write time:          Tue May  8 14:44:27 2018
Mount count:              0
Maximum mount count:      -1
Last checked:             Tue May  8 14:44:27 2018
Check interval:           0 (<none>)
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:           128
Default directory hash:   legacy


Group 0: (Blocks 1-1023)
  Primary superblock at 1, Group descriptors at 2-2
  Block bitmap at 6 (+5), Inode bitmap at 7 (+6)
  Inode table at 8-23 (+7)
  986 free blocks, 117 free inodes, 2 directories
  Free blocks: 38-1023
  Free inodes: 12-128
  • 似乎一切正常,再测试下 mount
$ sudo mount -o loop disk /mnt
mount: mount /dev/loop0 on /mnt failed: Stale file handle
  • 失败了,猜测应该是不可以缺少根目录信息。因此,继续测试,保留根目录的 inode 信息。将 TryEraseExt2() 修改如下:
int TryEraseExt2()
{
    uint32 i = 0;

    for(i=0x480; i<0x800; i++)
        MFileWriteByte(i, 0x00);
    for(i=0x2000; i<0x207f; i++)
        MFileWriteByte(i, 0x00);
    for(i=0x2300; i<0xffff0; i++)
        MFileWriteByte(i, 0x00);

    return 0;
}
  • 编译后,执行。再测试 mount, 结果如下:
$ sudo mount -o loop disk /mnt
$ ls /mnt -ahl
total 0
  • 可以看出,disk 此时能够被系统识别,并且链接信息被我们清除了。

  • 下面看看 disk 的十六进制信息:

$ od -tx1 -Ax disk
000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
000400 80 00 00 00 00 04 00 00 33 00 00 00 da 03 00 00
000410 75 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
000420 00 20 00 00 00 20 00 00 80 00 00 00 1b 4b f1 5a
000430 25 4b f1 5a 01 00 ff ff 53 ef 02 00 01 00 00 00
000440 d4 4a f1 5a 00 00 00 00 00 00 00 00 01 00 00 00
000450 00 00 00 00 0b 00 00 00 80 00 00 00 38 00 00 00
000460 02 00 00 00 03 00 00 00 cc 2a af c7 f1 4e 4d 6c
000470 ba 5f 44 20 10 da 1e f4 00 00 00 00 00 00 00 00
000480 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
000800 06 00 00 00 07 00 00 00 08 00 00 00 da 03 75 00
000810 02 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00
000820 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
001800 ff ff ff ff 1f 00 00 00 00 00 00 00 00 00 00 00
001810 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
001870 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80
001880 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
*
001c00 ff 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00
001c10 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
*
002000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
002080 ed 41 00 00 00 04 00 00 1f 4b f1 5a d4 4a f1 5a
002090 d4 4a f1 5a 00 00 00 00 00 00 03 00 02 00 00 00
0020a0 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00
0020b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
100000
  • 精简多了,系统保留的数据已经被我们删除了,上述数据意义,前面几节已经介绍清楚,我们已经能够全部掌握了。

  • 下一节,将用 c 写一个 ext2 的格式化工具。

阅读更多:   文件系统
暂无评论
  1. […] ext2 被系统保留的部分分析 […]

添加新评论

icon_redface.gificon_idea.gificon_cool.gif2016kuk.gificon_mrgreen.gif2016shuai.gif2016tp.gif2016db.gif2016ch.gificon_razz.gif2016zj.gificon_sad.gificon_cry.gif2016zhh.gificon_question.gif2016jk.gif2016bs.gificon_lol.gif2016qiao.gificon_surprised.gif2016fendou.gif2016ll.gif