免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 14768 | 回复: 15

ext2 磁盘格式分析 [复制链接]

论坛徽章:
0
发表于 2011-06-07 14:20 |显示全部楼层
通过读内核的源码和ULK,Professional Linux Kernel Architecture对ext2文件布局有一点点认识,我现在把我的想法写出来,希望通过大侠的批评来校正我理解的误点!
首先文件系统针对硬盘分区,
例如:把某个分区格式化成ext2,或者vfat,再或者ntfs,我还没有见过把一个分区格式化成两种文件系统。
把某个分区格式化成ext2,则这个分区的数据格式如下
1.JPG

第一部分:启动部分:每个分区都可以引导系统
第二部分:块组
一般硬盘512Byte是一个扇区,多个扇区组成一块,多个块 组成一个块组,多个块组和启动块成分区。块的大小可以在格式化视进行指定,最小为1024Byte即2个扇区。分区时没有指定块的大小则块大小为1024Byte,每个块组大小相等
接下来针对某一块组进行详细的说明
块组格式如下
2.JPG
第一部分:超级块super block
第二部分:组描述
第三部分:块位图
第四部分:节点位图
第五部分:节点表
第六部分:数据块
超级块结构描述
  1. /*
  2. * Structure of the super block
  3. */
  4. struct ext2_super_block {
  5.         __le32        s_inodes_count;                /* Inodes count */
  6.         __le32        s_blocks_count;                /* Blocks count */
  7.         __le32        s_r_blocks_count;        /* Reserved blocks count */
  8.         __le32        s_free_blocks_count;        /* Free blocks count */
  9.         __le32        s_free_inodes_count;        /* Free inodes count */
  10.         __le32        s_first_data_block;        /* First Data Block */
  11.         __le32        s_log_block_size;        /* Block size */
  12.         __le32        s_log_frag_size;        /* Fragment size */
  13.         __le32        s_blocks_per_group;        /* # Blocks per group */
  14.         __le32        s_frags_per_group;        /* # Fragments per group */
  15.         __le32        s_inodes_per_group;        /* # Inodes per group */
  16.         __le32        s_mtime;                /* Mount time */
  17.         __le32        s_wtime;                /* Write time */
  18.         __le16        s_mnt_count;                /* Mount count */
  19.         __le16        s_max_mnt_count;        /* Maximal mount count */
  20.         __le16        s_magic;                /* Magic signature */
  21.         __le16        s_state;                /* File system state */
  22.         __le16        s_errors;                /* Behaviour when detecting errors */
  23.         __le16        s_minor_rev_level;         /* minor revision level */
  24.         __le32        s_lastcheck;                /* time of last check */
  25.         __le32        s_checkinterval;        /* max. time between checks */
  26.         __le32        s_creator_os;                /* OS */
  27.         __le32        s_rev_level;                /* Revision level */
  28.         __le16        s_def_resuid;                /* Default uid for reserved blocks */
  29.         __le16        s_def_resgid;                /* Default gid for reserved blocks */
  30.        
  31.         __le32        s_first_ino;                 /* First non-reserved inode */
  32.         __le16   s_inode_size;                 /* size of inode structure */
  33.         __le16        s_block_group_nr;         /* block group # of this superblock */
  34.         __le32        s_feature_compat;         /* compatible feature set */
  35.         __le32        s_feature_incompat;         /* incompatible feature set */
  36.         __le32        s_feature_ro_compat;         /* readonly-compatible feature set */
  37.         __u8        s_uuid[16];                /* 128-bit uuid for volume */
  38.         char        s_volume_name[16];         /* volume name */
  39.         char        s_last_mounted[64];         /* directory where last mounted */
  40.         __le32        s_algorithm_usage_bitmap; /* For compression */
  41.         /*
  42.          * Performance hints.  Directory preallocation should only
  43.          * happen if the EXT2_COMPAT_PREALLOC flag is on.
  44.          */
  45.         __u8        s_prealloc_blocks;        /* Nr of blocks to try to preallocate*/
  46.         __u8        s_prealloc_dir_blocks;        /* Nr to preallocate for dirs */
  47.         __u16        s_padding1;
  48.         /*
  49.          * Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set.
  50.          */
  51.         __u8        s_journal_uuid[16];        /* uuid of journal superblock */
  52.         __u32        s_journal_inum;                /* inode number of journal file */
  53.         __u32        s_journal_dev;                /* device number of journal file */
  54.         __u32        s_last_orphan;                /* start of list of inodes to delete */
  55.         __u32        s_hash_seed[4];                /* HTREE hash seed */
  56.         __u8        s_def_hash_version;        /* Default hash version to use */
  57.         __u8        s_reserved_char_pad;
  58.         __u16        s_reserved_word_pad;
  59.         __le32        s_default_mount_opts;
  60.         __le32        s_first_meta_bg;         /* First metablock block group */
  61.         __u32        s_reserved[190];        /* Padding to the end of the block */
  62. };
复制代码
组结构描述
  1. /*
  2. * Structure of a blocks group descriptor
  3. */
  4. struct ext2_group_desc
  5. {
  6.         __le32        bg_block_bitmap;                /* Blocks bitmap block */
  7.         __le32        bg_inode_bitmap;                /* Inodes bitmap block */
  8.         __le32        bg_inode_table;                /* Inodes table block */
  9.         __le16        bg_free_blocks_count;        /* Free blocks count */
  10.         __le16        bg_free_inodes_count;        /* Free inodes count */
  11.         __le16        bg_used_dirs_count;        /* Directories count */
  12.         __le16        bg_pad;
  13.         __le32        bg_reserved[3];
  14. };
复制代码
节点结构描述
  1. /*
  2. * Structure of an inode on the disk
  3. */
  4. struct ext2_inode {
  5.         __le16        i_mode;                /* File mode */
  6.         __le16        i_uid;                /* Low 16 bits of Owner Uid */
  7.         __le32        i_size;                /* Size in bytes */
  8.         __le32        i_atime;        /* Access time */
  9.         __le32        i_ctime;        /* Creation time */
  10.         __le32        i_mtime;        /* Modification time */
  11.         __le32        i_dtime;        /* Deletion Time */
  12.         __le16        i_gid;                /* Low 16 bits of Group Id */
  13.         __le16        i_links_count;        /* Links count */
  14.         __le32        i_blocks;        /* Blocks count */
  15.         __le32        i_flags;        /* File flags */
  16.         union {
  17.                 struct {
  18.                         __le32  l_i_reserved1;
  19.                 } linux1;
  20.                 struct {
  21.                         __le32  h_i_translator;
  22.                 } hurd1;
  23.                 struct {
  24.                         __le32  m_i_reserved1;
  25.                 } masix1;
  26.         } osd1;                                /* OS dependent 1 */
  27.         __le32        i_block[EXT2_N_BLOCKS];/* Pointers to blocks */
  28.         __le32        i_generation;        /* File version (for NFS) */
  29.         __le32        i_file_acl;        /* File ACL */
  30.         __le32        i_dir_acl;        /* Directory ACL */
  31.         __le32        i_faddr;        /* Fragment address */
  32.         union {
  33.                 struct {
  34.                         __u8        l_i_frag;        /* Fragment number */
  35.                         __u8        l_i_fsize;        /* Fragment size */
  36.                         __u16        i_pad1;
  37.                         __le16        l_i_uid_high;        /* these 2 fields    */
  38.                         __le16        l_i_gid_high;        /* were reserved2[0] */
  39.                         __u32        l_i_reserved2;
  40.                 } linux2;
  41.                 struct {
  42.                         __u8        h_i_frag;        /* Fragment number */
  43.                         __u8        h_i_fsize;        /* Fragment size */
  44.                         __le16        h_i_mode_high;
  45.                         __le16        h_i_uid_high;
  46.                         __le16        h_i_gid_high;
  47.                         __le32        h_i_author;
  48.                 } hurd2;
  49.                 struct {
  50.                         __u8        m_i_frag;        /* Fragment number */
  51.                         __u8        m_i_fsize;        /* Fragment size */
  52.                         __u16        m_pad1;
  53.                         __u32        m_i_reserved2[2];
  54.                 } masix2;
  55.         } osd2;                                /* OS dependent 2 */
  56. };
复制代码

评分

参与人数 1可用积分 +8 收起 理由
Godbach + 8 感谢分享

查看全部评分

论坛徽章:
0
发表于 2011-06-07 14:26 |显示全部楼层
接下来我们进行动手验证
1.生成一个ext2.img文件,大小1M
3.JPG
2.把这个文件格式化成ext2
4.JPG
输入y后,得到信息如下
5.JPG
块大小 1024Byte
节点数 128
块数目 1024
一个块组
6.JPG
我们可以把这个文件挂在到我们的文件系统之中,如下图操作
7.JPG
第一步:创建用于挂载的文件点
第二步:挂载文件
第三步:列出文件的内容。在这个文件中有一个lost+found文件夹,里面用于存放磁盘坏块,只有分区的根目录才会有!
8.JPG
这个分区有三个文件夹“.”,“..”,“lost+found”

论坛徽章:
0
发表于 2011-06-07 14:38 |显示全部楼层
3.分析文件格式
使用vi 打开ext2.img文件如下图
9.JPG
设置16进制显示
在命令模式下输入
:%!xxd
10.JPG
4.现在我们磁盘结构开始查找数据,进行分析
根据前面的格式化可知,块大小为1024Byte=0x400,因为第一个块为启动块,所以越过第一个块即为第一个块组,我们查看0x400位置的数据
11.JPG
根据块组的格式,块组中第一个为超级块,大小总是1024Byte,我们只查看前面264Byte,(为什么?),我们对着超级块数据结构struct ext2_super_block {…}查看
第一个32为 8000 0000 即为0x80=128, s_inodes_count; /* Inodes count */
第二个32位 0004 0000 即为0x400=1024,s_blocks_count; /* Blocks count */
第三个32位 3300 0000 即为0x33=51,s_r_blocks_count;        /* Reserved blocks count */
第四个32位 da03 0000 即为0x3da=986,s_free_blocks_count;        /* Free blocks count */
……
不知你是否还记得格式化时数据
5.超级块后面是组描述,它紧跟超级块后面,数据如下
12.JPG
  1. struct ext2_group_desc
  2. {
  3.         __le32        bg_block_bitmap;                /* Blocks bitmap block */
  4.         __le32        bg_inode_bitmap;                /* Inodes bitmap block */
  5.         __le32        bg_inode_table;                /* Inodes table block */
  6.         __le16        bg_free_blocks_count;        /* Free blocks count */
  7.         __le16        bg_free_inodes_count;        /* Free inodes count */
  8.         __le16        bg_used_dirs_count;        /* Directories count */
  9.         __le16        bg_pad;
  10.         __le32        bg_reserved[3];
  11. };
复制代码
第一个32位 0600 0000 即为0x06=6
第二个 32位 0700 0000 即为0x07=7
第三个 32位 0800 0000 即为 0x08=8
第四个 16位 da03 即为0x3da=986
第五个 16位 7500 即为 0x75=117
…2
…4
……….
组描述后面是:块位图,节点位图,节点表,数据块。它们的起始块号都在组描述中指定
由上数据可知,块位图在第6块,节点位图在第7块,节点表在第8块,空闲的块数986
空闲节点117,目录个数为2个,
6.查看块位图
根据上述,块位图在第6块,地址=块大小*块数=1024*6=0x1800
数据如下:
13.JPG
7.查看节点位图
14.JPG
8.查看节点表
15.JPG
我们对着inode结构查看一下inode tables
  1. struct ext2_inode {
  2.         __le16        i_mode;                /* File mode */
  3.         __le16        i_uid;                /* Low 16 bits of Owner Uid */
  4.         __le32        i_size;                /* Size in bytes */
  5.         __le32        i_atime;        /* Access time */
  6.         __le32        i_ctime;        /* Creation time */
  7.         __le32        i_mtime;        /* Modification time */
  8.         __le32        i_dtime;        /* Deletion Time */
  9.         __le16        i_gid;                /* Low 16 bits of Group Id */
  10.         __le16        i_links_count;        /* Links count */
  11.         __le32        i_blocks;        /* Blocks count */
  12.         __le32        i_flags;        /* File flags */
  13.         __le32 ……..
  14. __le32        i_block[EXT2_N_BLOCKS];/* Pointers to blocks */ EXT2_N_BLOCKS=15
  15.         __le32        i_generation;        /* File version (for NFS) */
  16.         __le32        i_file_acl;        /* File ACL */
  17.         __le32        i_dir_acl;        /* Directory ACL */
  18.         __le32        i_faddr;        /* Fragment address */
  19. __le32 ……..
  20. __le32 ……..
  21. __le32 ……..
  22. };
复制代码
我们数数inode大小128字节,注意“__le32        i_block[EXT2_N_BLOCKS];/* Pointers to blocks */ EXT2_N_BLOCKS=15”
则一个inode为0x80
第二个inode其实地址为0x2080,第二个数据较为”丰富”,分析第二个Inode,重点看看
i_block[EXT2_N_BLOCKS]; i_block位置在偏移40Byte,即0x28;0x2080+0x28=0x20a8
16.JPG
可以看出该文件只有一个数据块,在第0x18块号即为24块处,查看24块内容 0x6000
17.JPG
分析数据可知此文件 为顶层文件夹....

论坛徽章:
0
发表于 2011-06-07 14:58 |显示全部楼层
楼主分析的真好
给精

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
发表于 2011-06-08 09:40 |显示全部楼层
回复 1# jjinl
感谢 LZ 分享,改天仔细拜读

论坛徽章:
0
发表于 2011-06-09 16:23 |显示全部楼层
回复 1# jjinl


    好文章,分析得很仔细

论坛徽章:
0
发表于 2011-06-10 10:41 |显示全部楼层
还没有分析完!现在工作有点忙!等过了这一阵子,我在把他补充完整...

论坛徽章:
0
发表于 2011-06-15 12:04 |显示全部楼层
我用WINHEX分析的。

话说建立文件系统的代码看起真老火呀!

楼主可否提供点思路!

论坛徽章:
0
发表于 2011-06-16 22:45 |显示全部楼层
回复 8# xianzq888


    可以看下《边干边学linux内核》中文件系统那块,以ext2来讲的,虽然是2.4内核,但还算透彻

论坛徽章:
0
发表于 2011-06-17 11:03 |显示全部楼层
LZ 的这个分析很给力。。   我以前分析视频文件格式 也是这样的。。呵呵。太给力哇
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP