了解ext3文件系统
根据最后的讨论(第1章),我们将尝试读取组描述符。
首先,我们将尝试了解什么是组描述符。
众所周知,超级块和组描述符表在每个块组中都是重复的。
组描述符表是组描述符的数组
每个块组都有自己的组描述符。 并按顺序存储在组描述符表中。
换句话说,每个块组都具有所有组描述符,因为描述符表被复制到每个块组中。
这意味着,如果我们查看块组3,则可以在描述符表的3位置找到它的组描述符。
在本文中,我们将从表中检索第一个组描述符结构。 因此,毫无疑问,它将代表第一个区块组。
让我们看看单个组描述符可以包含哪些信息:
bg_block_bitmap:块位图的块号
bg_inode_bitmap:索引节点位图的块数
bg_inode_table:第一个inode表块的块号
bg_free_blocks_count:组中的可用块数
bg_free_inodes_count:组中空闲索引节点的数量
bg_used_dirs_count:组中的目录数
bg_pad:与单词对齐
bg_reserved:填充24个字节的空值
组描述符在Linux内核中表示为“ ext2_group_desc”结构。
上述字段属于同一结构。
从上面的信息中我们可以看到,它将为我们提供其块组的所有信息。
让我们看看如何编写代码以检索组描述符结构。
第一个块是引导块:1024 B
后来居住超级街区
我们假设块大小为4096。
以后可以找到4096 B组描述符表。
因此,我们将打开任何ext2分区并查找4096个字节,读取第一个组描述符。
#include<linux/ext2_fs.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *buff = (char *)malloc(sizeof(struct ext2_group_desc));
struct ext2_group_desc * gdesc = (struct ext2_group_desc *)malloc(sizeof(struct ext2_group_desc));
//open any partition for testing,must be ext2/ext3.
int fd = open("/dev/hda3",O_RDONLY);
//skip the boot block and super block
lseek(fd,4096,SEEK_CUR);
//read the raw data from disk to buff
read(fd,buff,sizeof(struct ext2_group_desc));
//copy buffer to gdesc, you can use casting or union for this.
memcpy((void *)gdesc,(void *)buff,sizeof(struct ext2_group_desc));
//At this position you can be assured that you read group descriptor successfully.
close(fd);
return 0;
}
翻译自: https://bytes.com/topic/unix/insights/773180-understanding-ext-2-file-system-chapter-2-a
了解ext3文件系统