任务详情:自学教材第11章,提交学习笔记
Part1 知识点归纳&GPT提问
知识点归纳
EXT2文件系统数据结构
在Linux下,命令mke2fs [-b blksize-N ninodes]
虚拟磁盘布局
一个简单的EXT2文件系统布局
块描述符
Block#2
块和索引节点位图
BLOCK#8:块位图
BLOCK:9:索引节点位图
索引节点
BLOCK#10:索引节点
直接块
间接块
双重间接块
三重间接块
数据块
紧跟在索引节点块后面的是文件存储数据块
目录条目
包含dir_entry结构.
邮差算法
Linear_address LA = N*block + house;
Block_address BA = (LA/N,LA%N);
3级文件系统函数
挂载算法
mount filesys mount_point
可将某个文件系统挂载到mount_point目录上
卸载算法同理
GPT提问
Part2 问题与解决思路
问题
首先生成一个ext2文件系统。我划出1M的空间来试验,dd命令用来创建一个文件,通过执行这个dd命令生成了一个全零的大小为1024*1KB的文件,即1MB的文件。losetup是设定循环设备(loop service)的,循环设备可以将文件模拟成块设备。然后在块设备上建立我们的ext2文件系统.但是这里总是给我报错,说我没有权限
gpt解决
Part3 实践过程截图
显示位图
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/io.h>
#include <ext2fs/ext2_fs.h>
typedef unsigned char u8;
typedef struct ext2_super_block SUPER;
typedef struct ext2_group_desc GD;
#define BLKSIZE 1024
SUPER *sp;
GD *gp;
char buf[BLKSIZE];
int fd;
// get_block() reads a disk block into a buf[]?
int get_block(int fd, int blk, char *buf)
{
lseek(fd, (long)blk*BLKSIZE, SEEK_SET);
return read(fd, buf, BLKSIZE);
}
int imap(char *device)
{
int i, ninodes, blksize, imapblk;
fd = open(device, O_RDONLY);
if (fd < 0)
{
printf("open %s failed\n", device);
exit(1);
}
get_block(fd, 1, buf); // get superblock
sp = (SUPER *)buf;
// check magic number to ensure itz s an EXT2 FS ninodes = sp->s_inodes_count //
ninodes = sp->s_inodes_count;
printf("ninodes = %d\n", ninodes);
get_block(fd, 2, buf); //
gp = (GD *)buf;
imapblk = gp->bg_inode_bitmap;
printf("imapblk = %d\n", imapblk);
get_block(fd, imapblk, buf);
for ( i = 0; i <= ninodes/8; i++)
{
printf("%02x ", (u8)buf[i]);
}
printf("\n");
}
char *dev = "mydisk";
int main(int argc, char*argv[])
{
if(argc>1) dev = argv[1];
imap(dev);
}
显示根索引节点
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/io.h>
#include <ext2fs/ext2_fs.h>
typedef unsigned char u8;
typedef struct ext2_super_block SUPER;
typedef struct ext2_group_desc GD;
#define BLKSIZE 1024
SUPER *sp;
GD *gp;
char buf[BLKSIZE];
int fd;
// get_block() reads a disk block into a buf[]?
int get_block(int fd, int blk, char *buf)
{
lseek(fd, (long)blk*BLKSIZE, SEEK_SET);
return read(fd, buf, BLKSIZE);
}
int imap(char *device)
{
int i, ninodes, blksize, imapblk;
fd = open(device, O_RDONLY);
if (fd < 0)
{
printf("open %s failed\n", device);
exit(1);
}
get_block(fd, 1, buf); // get superblock
sp = (SUPER *)buf;
// check magic number to ensure itz s an EXT2 FS ninodes = sp->s_inodes_count //
ninodes = sp->s_inodes_count;
printf("ninodes = %d\n", ninodes);
get_block(fd, 2, buf); //
gp = (GD *)buf;
imapblk = gp->bg_inode_bitmap;
printf("imapblk = %d\n", imapblk);
get_block(fd, imapblk, buf);
for ( i = 0; i <= ninodes/8; i++)
{
printf("%02x ", (u8)buf[i]);
}
printf("\n");
}
char *dev = "mydisk";
int main(int argc, char*argv[])
{
if(argc>1) dev = argv[1];
imap(dev);
}