天天看点

【文件系统实验】奔跑吧Linux内核

文章目录

  • ​​1. 查看文件系统​​
  • ​​2. 删除文件内容​​
  • ​​3. 块设备​​
  • ​​4. 动手写一个简单的文件系统​​

1. 查看文件系统

  1. 使用dd命令创建磁盘文件file.img,并格式化为ext2文件系统,然后通过mount命令挂载到Linux主机的文件系统。
  2. 查看文件系统的信息,比如数据块的数量、数据块的大小、inode个数、空闲数据块的数量等信息,并画出文件系统的布局图。
  3. 在文件系统中创建文件a.txt,写入一些数据。查看a.txt文件的inode编号,统计a.txt文件占用了哪几个数据块。
  4. 使用dd或hexdump命令导出file.img磁盘文件的二进制数据并且分析超级块。

使用 ​

​dd​

​ 命令创建镜像、并高级格式化形成文件系统。

jiaming@ubuntu:~/Documents/linuxTest/Fs$ sudo dd if=/dev/zero of=./test.img bs=4K count=64
64+0 records in
64+0 records out
262144 bytes (262 kB, 256 KiB) copied, 0.00090901 s, 288 MB/s
jiaming@ubuntu:~/Documents/linuxTest/Fs$ ls
test.img     
jiaming@ubuntu:~/Documents/linuxTest/Fs$ sudo mkfs.ext2 test.img 
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done                            
Creating filesystem with 64 4k blocks and 32 inodes

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done      

挂载创建的文件系统。

jiaming@ubuntu:~/Documents/linuxTest/Fs$ sudo mkdir mediaFs
jiaming@ubuntu:~/Documents/linuxTest/Fs$ sudo mount test.img ./
mediaFs/  test.img  
jiaming@ubuntu:~/Documents/linuxTest/Fs$ sudo mount test.img ./mediaFs/      

​hexdump​

​ 命令查看镜像内容。

jiaming@ubuntu:~/Documents/linuxTest/Fs$ hexdump test.img 
0000000 0000 0000 0000 0000 0000 0000 0000 0000
*
0000400 0020 0000 0040 0000 0003 0000 0035 0000
0000410 0015 0000 0000 0000 0002 0000 0002 0000
0000420 8000 0000 8000 0000 0020 0000 6147 6158
0000430 6147 6158 0001 ffff ef53 0000 0001 0000
0000440 60c3 6158 0000 0000 0000 0000 0001 0000
0000450 0000 0000 000b 0000 0080 0000 0038 0000
0000460 0002 0000 0003 0000 0fb6 c3a2 a69c 554b
0000470 4ea0 3572 7029 6b60 0000 0000 0000 0000
0000480 0000 0000 0000 0000 0000 0000 0000 0000
*
00004e0 0000 0000 0000 0000 0000 0000 1153 c45d
00004f0 cf19 2040 8f98 e646 3f37 c616 0001 0000
0000500 000c 0000 0000 0000 60c3 6158 0000 0000
0000510 0000 0000 0000 0000 0000 0000 0000 0000
*
0000560 0001 0000 0000 0000 0000 0000 0000 0000
0000570 0000 0000 0000 0000 0000 0000 0000 0000
*
0001000 0002 0000 0003 0000 0004 0000 0035 0015
0001010 0002 0004 0000 0000 0000 0000 0000 0000
0001020 0000 0000 0000 0000 0000 0000 0000 0000
*
0002000 07ff 0000 0000 0000 ffff ffff ffff ffff
0002010 ffff ffff ffff ffff ffff ffff ffff ffff
*
0003000 07ff 0000 ffff ffff ffff ffff ffff ffff
0003010 ffff ffff ffff ffff ffff ffff ffff ffff
*
0004000 0000 0000 0000 0000 60c3 6158 60c3 6158
0004010 60c3 6158 0000 0000 0000 0000 0000 0000
0004020 0000 0000 0000 0000 0000 0000 0000 0000
*
0004080 41ed 0000 1000 0000 6147 6158 60c3 6158
0004090 60c3 6158 0000 0000 0000 0003 0008 0000
00040a0 0000 0000 0000 0000 0005 0000 0000 0000
00040b0 0000 0000 0000 0000 0000 0000 0000 0000
*
0004300 8180 0000 c000 0040 60c3 6158 60c3 6158
0004310 60c3 6158 0000 0000 0000 0001 0008 0000
0004320 0000 0000 0000 0000 0000 0000 0000 0000
*
0004350 0000 0000 0000 0000 0000 0000 000a 0000
0004360 0000 0000 0000 0000 0000 0000 0001 0000
0004370 0000 0000 0000 0000 0000 0000 0000 0000
*
0004500 41c0 0000 4000 0000 60c3 6158 60c3 6158
0004510 60c3 6158 0000 0000 0000 0002 0020 0000
0004520 0000 0000 0000 0000 0006 0000 0007 0000
0004530 0008 0000 0009 0000 0000 0000 0000 0000
0004540 0000 0000 0000 0000 0000 0000 0000 0000
*
0005000 0002 0000 000c 0201 002e 0000 0002 0000
0005010 000c 0202 2e2e 0000 000b 0000 0fe8 020a
0005020 6f6c 7473 662b 756f 646e 0000 0000 0000
0005030 0000 0000 0000 0000 0000 0000 0000 0000
*
0006000 000b 0000 000c 0201 002e 0000 0002 0000
0006010 0ff4 0202 2e2e 0000 0000 0000 0000 0000
0006020 0000 0000 0000 0000 0000 0000 0000 0000
*
0007000 0000 0000 1000 0000 0000 0000 0000 0000
0007010 0000 0000 0000 0000 0000 0000 0000 0000
*
0008000 0000 0000 1000 0000 0000 0000 0000 0000
0008010 0000 0000 0000 0000 0000 0000 0000 0000
*
0009000 0000 0000 1000 0000 0000 0000 0000 0000
0009010 0000 0000 0000 0000 0000 0000 0000 0000
*
0040000      

​dumpe2fs​

​ 解析镜像文件,查看文件系统的布局情况。

jiaming@ubuntu:~/Documents/linuxTest/Fs$ dumpe2fs test.img 
dumpe2fs 1.45.5 (07-Jan-2020)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          b60fa2c3-9ca6-4b55-a04e-72352970606b
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      ext_attr resize_inode dir_index filetype sparse_super large_file
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         not clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              32  # 最多支持32个inode
Block count:              64  # 一共有64个数据块
Reserved block count:     3   # 预留的数据块有3个
Free blocks:              53  # 空闲的数据块有53个
Free inodes:              21  # 空闲的inode有21个
First block:              0
Block size:               4096  # 每个数据块大小为4K
Fragment size:            4096
Blocks per group:         32768 # 每一组可以有32768个空闲块
Fragments per group:      32768
Inodes per group:         32
Inode blocks per group:   1
Filesystem created:       Sat Oct  2 21:38:11 2021
Last mount time:          Sat Oct  2 21:40:23 2021
Last write time:          Sat Oct  2 21:40:23 2021
Mount count:              1
Maximum mount count:      -1
Last checked:             Sat Oct  2 21:38:11 2021
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:   half_md4
Directory Hash Seed:      53115dc4-19cf-4020-988f-46e6373f16c6


Group 0: (Blocks 0-63)  # 这个迷你文件系统中有一个组
  Primary superblock at 0, Group descriptors at 1-1 # 超级块在第0个块,组描述符在第1个块
  Block bitmap at 2 (+2)  # 块位图在第2个块
  Inode bitmap at 3 (+3)  # 节点位图在第3个块
  Inode table at 4-4 (+4) # inode表在4~7个块
  53 free blocks, 21 free inodes, 2 directories
  Free blocks: 11-63  # 8~10个块是预留的
  Free inodes: 12-32      

分析超级块的信息。

​dd if=test.img bs=1 count=1024 skip=1024 | od -t x1 -Ax​

  • bs:设置读入/读出的块大小为bytes字节;
  • count:读取多少个块数据;
  • skip:从输入文件开头跳过多少个块后再开始读取数据;
  • od 命令用来显示数据内容​

    ​od [-A 地址进制] [-t 显示格式] 文件名​

  • d 十进制
  • 默认八进制
  • x 十六进制
  • n 不打印位移值
  • c ASCII 字符或反斜杠序列
  • d 有符号十进制树
  • f 浮点数
  • 八进制 默认值为02
  • u 无符号十进制数
  • x 十六进制数

可以对照 struct ext2_super_block 数据结构查看超级块信息。

【文件系统实验】奔跑吧Linux内核

挂载文件系统,并创建文件,填写内容。

jiaming@ubuntu:~/Documents/linuxTest/Fs$ debugfs 
debugfs 1.45.5 (07-Jan-2020)
debugfs:  open test.img  # open 子命令打开文件系统
debugfs:  blocks a.txt  # 查看文件占用了哪些数据块,占用第12个数据块
12 
debugfs:  imap a.txt # 查看文件inode节点情况
Inode 15 is part of block group 0 # 第15个inode节点,位于group0的第4个数据块,偏移量为0x0700
  located at block 4, offset 0x0700
debugfs:  q      

2. 删除文件内容

在上一个实验的基础上删除a.txt文件开头的几字节数据,然后调用sync命令来同步磁盘。
1. 分析a.txt文件使用的磁盘数据块是否发生了变化。
2. 画出上述过程在文件系统中的流程图。      

3. 块设备

1. 写一个简单的ramdisk设备驱动,并使用ext2文件系统的格式化工具进行格式化。
2. 在ramdisk设备驱动中,实现HDIO_GETINFO的ioctl命令,读出ramdisk的hd_deometry 参数。
3. 写一个简单的用户空间的测试程序来读取hd_geometry参数。      

4. 动手写一个简单的文件系统

写一个简单的文件系统,要求这个文件系统的存储介质基于内存。      

继续阅读