浅谈EXT2文件系统----超级块

 超级块概述

在 EXT2 文件系统中,超级块(superblock)是一个非常重要的数据结构,包含了文件系统的全局信息。每个文件系统都有一个超级块,位于文件系统的第一个块之后,通常在块组的起始处。

超级块包含以下关键信息:

  1. 文件系统的大小:块总数、空闲块数、inode 总数等。
  2. 块和 inode 的大小:描述数据块和 inode 的尺寸。
  3. 文件系统状态:例如文件系统是否干净或是否发生错误。
  4. 挂载信息:挂载次数、最后挂载时间、上次检查时间等。
  5. 特性支持:例如是否支持扩展属性、目录索引等功能。
  6. 保留块数:为系统管理员保留的块数量,以防止磁盘满时影响系统操作。

超级块还存储了文件系统的创建时间和 UUID,用于唯一标识文件系统。

创建模拟设备

  • 创建虚拟设备

创建一个100M的磁盘设备,并进行分区和格式化为ext2文件系统

dd if=/dev/zero of=./disk.img bs=1M count=100
fdisk disk.img
losetup -fP disk.img
mkfs.ext2 /dev/loop0p1
  • 查看超级块
[root@node09 ~]# dumpe2fs /dev/loop0
dumpe2fs 1.42.9 (28-Dec-2013)
dumpe2fs: Bad magic number in super-block while trying to open /dev/loop0
Couldn't find valid filesystem superblock.
[root@node09 ~]# dumpe2fs /dev/loop0p1
[root@node09 ~]# dumpe2fs /dev/loop0p1
dumpe2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          b2986a8e-5b67-40c3-aa75-495e250daa56
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      ext_attr resize_inode dir_index filetype sparse_super
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              25376
Block count:              101376
Reserved block count:     5068
Free blocks:              96615
Free inodes:              25365
First block:              1
Block size:               1024
Fragment size:            1024
Reserved GDT blocks:      256
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         1952
Inode blocks per group:   244
Filesystem created:       Fri Sep 13 15:56:48 2024
Last mount time:          n/a
Last write time:          Fri Sep 13 15:56:48 2024
Mount count:              0
Maximum mount count:      -1
Last checked:             Fri Sep 13 15:56:48 2024
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:      8b289b24-46fe-412c-8be1-96ec46b08e0d


Group 0: (Blocks 1-8192)
  Primary superblock at 1, Group descriptors at 2-2
  Reserved GDT blocks at 3-258
  Block bitmap at 259 (+258), Inode bitmap at 260 (+259)
  Inode table at 261-504 (+260)
  7674 free blocks, 1941 free inodes, 2 directories
  Free blocks: 519-8192
  Free inodes: 12-1952
Group 1: (Blocks 8193-16384)
  Backup superblock at 8193, Group descriptors at 8194-8194
  Reserved GDT blocks at 8195-8450
  Block bitmap at 8451 (+258), Inode bitmap at 8452 (+259)
  Inode table at 8453-8696 (+260)
  7688 free blocks, 1952 free inodes, 0 directories
  Free blocks: 8697-16384
  Free inodes: 1953-3904
Group 2: (Blocks 16385-24576)
  Block bitmap at 16385 (+0), Inode bitmap at 16386 (+1)
  Inode table at 16387-16630 (+2)
  7946 free blocks, 1952 free inodes, 0 directories
  Free blocks: 16631-24576
  Free inodes: 3905-5856
Group 3 ....
....

解读超级块

以上输出信息的详细解释

一般信息:

dumpe2fs 1.42.9 (28-Dec-2013)

Filesystem volume name:   <none>
  • dumpe2fs 1.42.9: dumpe2fs 工具的版本。
  •  /dev/loop0p1: 被检查的设备或回环文件系统(这是一个回环设备上的分区)。

 文件系统详情:

Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          b2986a8e-5b67-40c3-aa75-495e250daa56
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
  • Filesystem volume name: 没有设置卷标。
  • Last mounted on: 没有可用信息,因为文件系统尚未挂载。
  • Filesystem UUID: 文件系统的唯一标识符(UUID),可用于在重启后识别该文件系统。
  • Filesystem magic number: 0xEF53,标识此文件系统为 ext2/ext3/ext4 类型。
  • Filesystem revision #: 1(动态),表示该文件系统支持动态特性集。

文件系统特性:

Filesystem features:      ext_attr resize_inode dir_index filetype sparse_super
  • ext_attr: 支持扩展属性(用于 ACL、安全标签等)。
  • resize_inode: 支持调整文件系统大小。
  • dir_index: 目录索引已启用,这可以加快目录查找速度。
  • filetype: 文件系统在目录项中记录文件类型。
  • sparse_super: 使用稀疏超级块,减少开销,只在部分组中存储超级块备份。

文件系统标志:

Filesystem flags:         signed_directory_hash 
  • signed_directory_hash: 文件系统使用签名的目录哈希,用于确保使用哈希 B 树的目录的完整性。

默认挂载选项:

Default mount options:    user_xattr acl
  • user_xattr: 默认启用用户扩展属性。
  • acl: 默认启用访问控制列表(ACL)。

文件系统状态:

Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
  • Filesystem state: 干净,表示文件系统处于良好状态。
  • Errors behavior: 发现错误时系统会继续工作。
  • Filesystem OS type: 文件系统所属的操作系统类型。

Inode 和块信息:

Inode count:              25376
Block count:              101376
Reserved block count:     5068
Free blocks:              96615
Free inodes:              25365
First block:              1
Block size:               1024
Fragment size:            1024
Reserved GDT blocks:      256
  • Inode count: 25,376 个 inode(用于存储元数据的索引节点数量)。
  • Block count: 101,376 个块(总数据块数)。
  • Reserved block count: 为 root 用户保留 5,068 个块,防止磁盘填满时系统无法正常运行。
  • Free blocks: 96,615 个空闲块(未使用)。
  • Free inodes: 25,365 个空闲 inode,表示大多数文件存储空间尚未使用。
  • First block: 第 1 块是第一个数据块。
  • Block size: 1,024 字节,每个块大小为 1KB。
  • Fragment size: 1,024 字节(碎片是子块单位,这里与块大小一致)。
  • Reserved GDT blocks: 为组描述符表(GDT)保留 256 个块,GDT 包含块组的元数据。

块组信息:

Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         1952
Inode blocks per group:   244
  • Blocks per group: 每个块组包含 8,192 个块。
  • Fragments per group: 每个块组包含 8,192 个碎片。
  • Inodes per group: 每个块组包含 1,952 个 inode。
  • Inode blocks per group: 每个块组用 244 个块来存储 inode。

文件系统创建与维护:

Filesystem created:       Fri Sep 13 15:56:48 2024
Last mount time:          n/a
Last write time:          Fri Sep 13 15:56:48 2024
Mount count:              0
Maximum mount count:      -1
Last checked:             Fri Sep 13 15:56:48 2024
Check interval:           0 (<none>)
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
  • Filesystem created: 文件系统创建时间为 2024 年 9 月 13 日 15:56:48。
  • Last mount time: 没有可用信息,因为尚未挂载。
  • Last write time: 文件系统创建时的时间,即 2024 年 9 月 13 日 15:56:48。
  • Mount count: 0,表示文件系统尚未挂载。
  • Maximum mount count: -1,表示没有强制性挂载次数检查。
  • Last checked: 文件系统最后检查时间与创建时间相同。
  • Check interval: 0,表示没有设置自动检查间隔。
  • Reserved blocks uid: 0,表示 root 用户(UID 0)可以使用保留块。
  • Reserved blocks gid: 0,表示 root 组(GID 0)可以使用保留块。

其他元数据:

First inode:              11
Inode size:	          128
Default directory hash:   half_md4
Directory Hash Seed:      8b289b24-46fe-412c-8be1-96ec46b08e0d
  • First inode: 第一个 inode 的索引是 11。
  • Inode size: 每个 inode 为 128 字节(用于存储文件的元数据)。
  • Default directory hash: half_md4,用于目录索引的哈希算法。
  • Directory Hash Seed: 用于创建目录项哈希的种子值。

总体来看,该文件系统处于干净状态,块和 inode 的使用率较低,具备扩展属性、ACL、目录索引等功能,适合多种使用场景。

Group 0 的布局和使用情况

Group 0: (Blocks 1-8192)
  Primary superblock at 1, Group descriptors at 2-2
  Reserved GDT blocks at 3-258
  Block bitmap at 259 (+258), Inode bitmap at 260 (+259)
  Inode table at 261-504 (+260)
  7674 free blocks, 1941 free inodes, 2 directories
  Free blocks: 519-8192
  Free inodes: 12-1952

这段输出描述了 ext2 文件系统中第 0 组(Group 0)的布局和使用情况。它详细列出了该组的超级块、组描述符、位图、inode 表等信息。以下是对每个部分的解释:

Group 0: (Blocks 1-8192)

  • Group 0:表示块组 0,该组的块范围是 1 到 8192。

Primary superblock at 1, Group descriptors at 2-2

  • Primary superblock at 1:超级块位于块 1。超级块存储了整个文件系统的重要元数据。

  • Group descriptors at 2-2:组描述符位于块 2,它包含了当前组中块和 inode 的位置信息。

Reserved GDT blocks at 3-258

  • Reserved GDT blocks at 3-258:块 3 到 258 为保留的组描述符表(GDT)块。GDT 储存块组描述符的冗余信息,用于容错。

Block bitmap at 259 (+258), Inode bitmap at 260 (+259)

  • Block bitmap at 259 (+258):块位图存储在块 259,位图用于记录哪些数据块是已使用的,哪些是空闲的。+258 表示该位图与超级块位置的偏移量。

  • Inode bitmap at 260 (+259):inode 位图存储在块 260,类似地用于记录哪些 inode 是已使用的,哪些是空闲的。+259 表示与超级块位置的偏移量。

Inode table at 261-504 (+260)

  • Inode table at 261-504 (+260):inode 表存储在块 261 到 504,包含文件系统中每个文件和目录的元数据。+260 表示它与超级块的偏移量。

7674 free blocks, 1941 free inodes, 2 directories

  • 7674 free blocks:该组中有 7674 个空闲块,表示这些块尚未分配给文件或目录。

  • 1941 free inodes:该组中有 1941 个空闲的 inode,表示这些 inode 还没有分配给文件或目录。

  • 2 directories:该组中有 2 个已分配的目录。

Free blocks: 519-8192

  • Free blocks: 519-8192:空闲数据块的编号范围是 519 到 8192,这些块可以分配给新文件或目录。

Free inodes: 12-1952

  • Free inodes: 12-1952:空闲 inode 的编号范围是 12 到 1952,这些 inode 可以用于新文件或目录的创建。

总结来说,这段输出详细展示了 ext2 文件系统中第 0 块组的结构,包括超级块、组描述符、位图、inode 表的存放位置,以及当前组中的空闲块和 inode 的数量及范围。

超级块原始数据

磁盘分区情况:

[root@node09 ~]# fdisk /dev/loop0 -l

Disk /dev/loop0: 104 MB, 104857600 bytes, 204800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x76dcb4a8

      Device Boot      Start         End      Blocks   Id  System
/dev/loop0p1            2048      204799      101376   83  Linux
[root@node09 ~]# 

通过以上中的数据:

Block size:               1024

Group 0: (Blocks 1-8192)
  Primary superblock at 1, ...
再加上2个扇区的boot sector可以计算得出磁盘设备 /dev/loop0 中的2050扇区后的2个扇区中的内容就是超级块中的内容(512*2050=1049600)

[root@node09 ~]# hexdump -s 1049600 -n 1024 /dev/loop0 -C
00100400  20 63 00 00 00 8c 01 00  cc 13 00 00 67 79 01 00  | c..........gy..|
00100410  15 63 00 00 01 00 00 00  00 00 00 00 00 00 00 00  |.c..............|
00100420  00 20 00 00 00 20 00 00  a0 07 00 00 00 00 00 00  |. ... ..........|
00100430  40 f0 e3 66 00 00 ff ff  53 ef 01 00 01 00 00 00  |@..f....S.......|
00100440  40 f0 e3 66 00 00 00 00  00 00 00 00 01 00 00 00  |@..f............|
00100450  00 00 00 00 0b 00 00 00  80 00 00 00 38 00 00 00  |............8...|
00100460  02 00 00 00 01 00 00 00  b2 98 6a 8e 5b 67 40 c3  |..........j.[g@.|
00100470  aa 75 49 5e 25 0d aa 56  00 00 00 00 00 00 00 00  |.uI^%..V........|
00100480  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
001004c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 01  |................|
001004d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
001004e0  00 00 00 00 00 00 00 00  00 00 00 00 8b 28 9b 24  |.............(.$|
001004f0  46 fe 41 2c 8b e1 96 ec  46 b0 8e 0d 01 00 00 00  |F.A,....F.......|
00100500  0c 00 00 00 00 00 00 00  40 f0 e3 66 00 00 00 00  |........@..f....|
00100510  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00100560  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00100570  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00100800
[root@node09 ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值