找回密码
 立即注册
首页 业界区 安全 文件系统(六):一文看懂linux ext4文件系统工作原理 ...

文件系统(六):一文看懂linux ext4文件系统工作原理

简千叶 7 天前
liwen01 2024.06.09
前言

Linux系统中的ext2、ext3、ext4 文件系统,它们都有很强的向后和向前兼容性,可以在数据不丢失的情况下进行文件系统的升级。目前ext4是一个相对较成熟、稳定且高效的文件系统,适用于绝大部分规模和需求的Linux环境。
ext4它突出的特点有:数据分段管理、多块分配、延迟分配、持久预分配、日志校验、支持更大的文件系统和文件大小。
ext4文件系统的具体实现比较复杂,本文尝试用比较简单的方式用一篇文章的篇幅来简单地介绍一下它的工作原理。
(一)创建ext文件系统

为了分析ext4 文件系统的内部结构和原理,这里我们在Linux中创建一个ext4文件系统镜像,然后通过loop虚拟设备将ext4镜像文件挂载到某个目录上。具体实现步骤如下:

  • 创建一个1GB的文件
  1. dd if=/dev/zero of=./ext4_image.img bs=1M count=1024
复制代码

  • 将这个文件格式化成 ext4 文件系统格式
  1. mkfs.ext4 ext4_image.img
复制代码

  • 通过Linux的loop虚拟设备将文件挂载到目录上
  1. sudo mount -o loop ext4_image.img /home/biao/test/ext4/ext4_simulator
复制代码

  • dumpe2fs 查看文件系统基本信息
  1. dumpe2fs ext4_image.img  
复制代码
输出内容信息(中间省略了部分内容):
  1. dumpe2fs 1.44.1 (24-Mar-2018)
  2. Filesystem volume name:   <none>
  3. Last mounted on:          /home/biao/test/ext4/ext4_simulator
  4. Filesystem UUID:          0169498e-f5f7-4fb8-9e9e-532088e41333
  5. Filesystem magic number:  0xEF53
  6. Filesystem revision #:    1 (dynamic)
  7. Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
  8. Filesystem flags:         signed_directory_hash
  9. Default mount options:    user_xattr acl
  10. Filesystem state:         clean
  11. Errors behavior:          Continue
  12. Filesystem OS type:       Linux
  13. Inode count:              65536
  14. Block count:              262144
  15. Reserved block count:     13107
  16. Free blocks:              247703
  17. Free inodes:              65517
  18. First block:              0
  19. Block size:               4096
  20. Fragment size:            4096
  21. Group descriptor size:    64
  22. Reserved GDT blocks:      127
  23. Blocks per group:         32768
  24. Fragments per group:      32768
  25. Inodes per group:         8192
  26. Inode blocks per group:   512
  27. Flex block group size:    16
  28. Filesystem created:       Fri May 24 17:18:57 2024
  29. Last mount time:          Wed Jun  5 19:15:36 2024
  30. Last write time:          Wed Jun  5 19:15:36 2024
  31. Mount count:              3
  32. Maximum mount count:      -1
  33. Last checked:             Fri May 24 17:18:57 2024
  34. Check interval:           0 (<none>)
  35. Lifetime writes:          6997 kB
  36. Reserved blocks uid:      0 (user root)
  37. Reserved blocks gid:      0 (group root)
  38. First inode:              11
  39. Inode size:               256
  40. Required extra isize:     32
  41. Desired extra isize:      32
  42. Journal inode:            8
  43. Default directory hash:   half_md4
  44. Directory Hash Seed:      0faf0e8c-f385-4ecd-b3a4-db2a3329e121
  45. Journal backup:           inode blocks
  46. Checksum type:            crc32c
  47. Checksum:                 0x32dc1b70
  48. Journal features:         journal_64bit journal_checksum_v3
  49. Journal size:             32M
  50. Journal length:           8192
  51. Journal sequence:         0x00000017
  52. Journal start:            1
  53. Journal checksum type:    crc32c
  54. Journal checksum:         0xa3c1b983
  55. Group 0: (Blocks 0-32767) csum 0xf19b [ITABLE_ZEROED]
  56.   Primary superblock at 0, Group descriptors at 1-1
  57.   Reserved GDT blocks at 2-128
  58.   Block bitmap at 129 (+129), csum 0x8efc34cf
  59.   Inode bitmap at 137 (+137), csum 0x49f91ed6
  60.   Inode table at 145-656 (+145)
  61.   28517 free blocks, 8176 free inodes, 3 directories, 8176 unused inodes
  62.   Free blocks: 4251-32767
  63.   Free inodes: 17-8192
  64. ..........
  65. ..........
  66. ..........
  67. Group 7: (Blocks 229376-262143) csum 0x7daa [INODE_UNINIT, ITABLE_ZEROED]
  68.   Backup superblock at 229376, Group descriptors at 229377-229377
  69.   Reserved GDT blocks at 229378-229504
  70.   Block bitmap at 136 (bg #0 + 136), csum 0x5bd8cca0
  71.   Inode bitmap at 144 (bg #0 + 144), csum 0x00000000
  72.   Inode table at 3729-4240 (bg #0 + 3729)
  73.   32639 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
  74.   Free blocks: 229505-262143
  75.   Free inodes: 57345-65536
复制代码
1.1 ext4文件系统信息表(二)ext4 磁盘布局

从上面dumpe2fs的数据上我们可以看出,一个1GB大小的空间,ext4 文件系统将它分隔成了0~7的8个Group。
ext4 的总体磁盘布局如下:
1.png

图2.1 ext4总体布局其中,每个Group中又有superblock、Group descriptors、bitmap、Inode table、usrer data、还有一些保留空间,细分之后的空间布局如下:
2.png

图2.2 ext4 group 布局从上图可以看出:

  • Backup superblock、Group descriptors、Reserved GDT 是分布在1、3、5、7 这几个Group中,2、4、6Group并没有这些信息。
  • Block bitmap、Inode bitmap、Inode table 这些元文件在每个Group中的位置并不一样,而是相差1个Block。
为什么需要这样设计?这个下面稍晚点再介绍
(三) superblock超级块

从上面《1.1 ext4文件系统信息表》中可以知道Primary superblock在第0号block,每个block的大小为4096Byte。
用hexdump 命令查看超级块的数据
[code]biao@ubuntu:~/test/ext4$ hexdump -s 0 -n 4096 -C ext4_image.img    00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|*00000400  00 00 01 00 00 00 04 00  33 33 00 00 97 c7 03 00  |........33......|00000410  ed ff 00 00 00 00 00 00  02 00 00 00 02 00 00 00  |................|00000420  00 80 00 00 00 80 00 00  00 20 00 00 9c c1 5d 66  |......... ....]f|00000430  00 d0 5f 66 02 00 ff ff  53 ef 01 00 01 00 00 00  |.._f....S.......|00000440  81 5b 50 66 00 00 00 00  00 00 00 00 01 00 00 00  |.[Pf............|00000450  00 00 00 00 0b 00 00 00  00 01 00 00 3c 00 00 00  |............
您需要登录后才可以回帖 登录 | 立即注册