Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1285931
  • 博文数量: 168
  • 博客积分: 3483
  • 博客等级: 中校
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 13:17
文章分类

全部博文(168)

文章存档

2015年(6)

2014年(9)

2013年(47)

2012年(11)

2011年(13)

2010年(18)

2009年(11)

2008年(42)

2007年(11)

分类: LINUX

2014-10-24 18:55:45

大家都知道,文件系统类型是靠磁盘上superblock区块里的magic number不同来区分的,
但是,今天同事遇到相关问题,引出这个问题:
    ext2/3/4 使用的magic numner都一样,怎么判断分区类型到底是哪一个呢?

google 到一些工具可以判断,但是,具体他们根据什么判断的呢?
多年不看文件系统了,但是还有一点印象:
    找磁盘superblock结构定义,hexdump -C -n 128 -s 1024 查看验证(-s 1024不用说了吧)

找到 ext4 的超级块定义代码,对比ext3/2,有 incompatible feature 的字样, 就是它了:
ext4 磁盘超级块结构定义节选:  ext2/3/4 magic相同,区分靠 feature; 不兼容 feature 信息对应的偏移地址/*60*/
        __le16  s_magic;                /* Magic signature */            /*38*/        <<<---magic
        __le16  s_state;                /* File system state */
        __le16  s_errors;               /* Behaviour when detecting errors */
        __le16  s_minor_rev_level;      /* minor revision level */
/*40*/  __le32  s_lastcheck;            /* time of last check */
        __le32  s_checkinterval;        /* max. time between checks */
        __le32  s_creator_os;           /* OS */
        __le32  s_rev_level;            /* Revision level */
/*50*/  __le16  s_def_resuid;           /* Default uid for reserved blocks */
        __le16  s_def_resgid;           /* Default gid for reserved blocks */
        /*
         * These fields are for EXT4_DYNAMIC_REV superblocks only.
         *
         * Note: the difference between the compatible feature set and
         * the incompatible feature set is that if there is a bit set
         * in the incompatible feature set that the kernel doesn't
         * know about, it should refuse to mount the filesystem.
         *
         * e2fsck's requirements are more strict; if it doesn't know
         * about a feature in either the compatible or incompatible
         * feature set, it must abort and not try to meddle with
         * things it doesn't understand...
         */
        __le32  s_first_ino;            /* First non-reserved inode */
        __le16  s_inode_size;           /* size of inode structure */
        __le16  s_block_group_nr;       /* block group # of this superblock */
        __le32  s_feature_compat;       /* compatible feature set */
/*60*/  __le32  s_feature_incompat;     /* incompatible feature set */         <<<--- ext2/3/4 magic相同,区分主要是feature,
        __le32  s_feature_ro_compat;    /* readonly-compatible feature set */
/*68*/  __u8    s_uuid[16];             /* 128-bit uuid for volume */
...
进一步看代码ext4 superblock初始化代码,会发现s_feature_incompat赋值里面
有EXT4_FEATURE_INCOMPAT_EXTENTS等多个ext4独有的特性,
所以使用magic+s_feature_*判断应该是可以的

验证: hexdump -C 查看超级块 feature 相关信息比较ext 4/3/2:
  ext4 ext3比较s_feature_incompat不同, ext3 ext2比较s_feature_compat 不同;

  1. [root@dhcp12-241 rdma.sunrpc]# hexdump -C -s 1024 ext4.image -n $((0x68))
  2. 00000400 10 f4 01 00 00 d0 07 00 00 64 00 00 d6 67 07 00 |.........d...g..|
  3. 00000410 05 f4 01 00 01 00 00 00 00 00 00 00 00 00 00 00 |................|
  4. 00000420 00 20 00 00 00 20 00 00 f0 07 00 00 00 00 00 00 |. ... ..........|
  5. 00000430 00 1e 4a 54 00 00 ff ff 53 ef 01 00 01 00 00 00 |..JT....S.......|
  6. 00000440 00 1e 4a 54 00 00 00 00 00 00 00 00 01 00 00 00 |..JT............|
  7. 00000450 00 00 00 00 0b 00 00 00 80 00 00 00 3c 00 00 00 |............<...|
  8. 00000460 42 02 00 00 79 00 00 00 |B...y...|
  9. 00000468
  10. [root@dhcp12-241 rdma.sunrpc]# hexdump -C -s 1024 ext3.image -n $((0x68))
  11. 00000400 10 f4 01 00 00 d0 07 00 00 64 00 00 b5 67 07 00 |.........d...g..|
  12. 00000410 05 f4 01 00 01 00 00 00 00 00 00 00 00 00 00 00 |................|
  13. 00000420 00 20 00 00 00 20 00 00 f0 07 00 00 00 00 00 00 |. ... ..........|
  14. 00000430 72 13 4a 54 00 00 ff ff 53 ef 01 00 01 00 00 00 |r.JT....S.......|
  15. 00000440 72 13 4a 54 00 00 00 00 00 00 00 00 01 00 00 00 |r.JT............|
  16. 00000450 00 00 00 00 0b 00 00 00 80 00 00 00 3c 00 00 00 |............<...|
  17. 00000460 02 00 00 00 01 00 00 00 |........|
  18. 00000468
  19. [root@dhcp12-241 rdma.sunrpc]# hexdump -C -s 1024 ext2.image -n $((0x68))
  20. 00000400 10 f4 01 00 00 d0 07 00 00 64 00 00 d6 87 07 00 |.........d......|
  21. 00000410 05 f4 01 00 01 00 00 00 00 00 00 00 00 00 00 00 |................|
  22. 00000420 00 20 00 00 00 20 00 00 f0 07 00 00 00 00 00 00 |. ... ..........|
  23. 00000430 61 13 4a 54 00 00 ff ff 53 ef 01 00 01 00 00 00 |a.JT....S.......|
  24. 00000440 61 13 4a 54 00 00 00 00 00 00 00 00 01 00 00 00 |a.JT............|
  25. 00000450 00 00 00 00 0b 00 00 00 80 00 00 00 38 00 00 00 |............8...|
  26. 00000460 02 00 00 00 01 00 00 00 |........|
  27. 00000468

*手动改一个ext3的的superblock /*5c~60*/位置的值,再用file查看是否文件系统类型发生变化

将ext4.image superblock的 s_feature_incompat 值覆盖到ext3.image, 再 file ext3.image 查看 类型就变成ext4了!
  1. [root@dhcp12-241 rdma.sunrpc]# dd if=ext4.image skip=$((1024+0x60))c bs=8 count=1 iflag=skip_bytes of=ext3.image seek=$((1024+0x60))c oflag=seek_bytes
  2. 1+0 records in
  3. 1+0 records out
  4. 8 bytes (8 B) copied, 0.000174478 s, 45.9 kB/s
  5. [root@dhcp12-241 rdma.sunrpc]# file ext3.image
  6. ext3.image: Linux rev 1.0 ext4 filesystem data (extents) (huge files)
  1. [root@dhcp12-241 rdma.sunrpc]# file *.image
  2. ext2.image: Linux rev 1.0 ext2 filesystem data, UUID=b63e742f-e5cc-4610-a702-f36d563e782c
  3. ext3.image: Linux rev 1.0 ext3 filesystem data, UUID=d986c054-9cc1-4b38-bfed-09caa75ee142
  4. ext4.image: Linux rev 1.0 ext4 filesystem data, UUID=f1fe77bf-ed5a-4c87-93fd-3921462b7a54 (extents) (huge files)
  5. [root@dhcp12-241 rdma.sunrpc]# blkid ext3.image
  6. ext3.image: UUID="d986c054-9cc1-4b38-bfed-09caa75ee142" SEC_TYPE="ext2" TYPE="ext3"
  7. [root@dhcp12-241 rdma.sunrpc]# dd if=ext4.image skip=$((1024+0x60))c bs=8 count=1 iflag=skip_bytes of=ext3.image seek=$((1024+0x60))c oflag=seek_bytes conv=notrunc
  8. 1+0 records in
  9. 1+0 records out
  10. 8 bytes (8 B) copied, 9.2809e-05 s, 86.2 kB/s
  11. [root@dhcp12-241 rdma.sunrpc]# file *.image
  12. ext2.image: Linux rev 1.0 ext2 filesystem data, UUID=b63e742f-e5cc-4610-a702-f36d563e782c
  13. ext3.image: Linux rev 1.0 ext4 filesystem data, UUID=d986c054-9cc1-4b38-bfed-09caa75ee142 (extents) (huge files)
  14. ext4.image: Linux rev 1.0 ext4 filesystem data, UUID=f1fe77bf-ed5a-4c87-93fd-3921462b7a54 (extents) (huge files)
  15. [root@dhcp12-241 rdma.sunrpc]# blkid ext3.image
  16. ext3.image: UUID="d986c054-9cc1-4b38-bfed-09caa75ee142" TYPE="ext4"
  17. [root@dhcp12-241 rdma.sunrpc]#

ext2 变 ext3 同理,就不演示了,,


阅读(4263) | 评论(0) | 转发(0) |
0

上一篇:VoCore

下一篇:enable_coredump

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