boot sector与super block的关系

本文详细介绍了Ext2文件系统的组织结构,包括块和块组的概念、超级块的位置及作用、目录条目的对齐方式等内容,并解释了不同块大小对文件系统布局的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This page focuses on the ext2 on-disk format. The Linux kernel's filesystem implementation (the code to read and write it) is documented in the kernel source, Documentation/filesystems/ext2.txt.

Note: for our purposes, ext3 and ext4 are just ext2 with some extra data fields.

Overview

Blocks and Block Groups

Every ext2 filesystem consists of blocks, which are divided into block groups. Blocks can be 1k, 2k, or 4k in length.[1]All ext2 disk layout is done in terms of these logical blocks, never in terms of 512-byte logical blocks.

Each block group contains as many blocks as one block can hold a bitmap for, so at a 1k block size a block group contains 8192 blocks (1024 bytes * 8 bits), and at 4k block size a block group contains 32768 blocks. Groups are numbered starting at 0, and occur one after another on disk, in order, with no gaps between them.

Block groups contain the following structures, in order:

  • Superblock (sometimes)
  • Group table (sometimes)
  • Block bitmap
  • Inode bitmap
  • Inode table
  • Data blocks

Not all block groups contain all structures. Specifically, the first two (superblock and group table) only occur in some groups, and other block groups start with the block bitmap and go from there. This frees up more data blocks to hold actual file and directory data, see the superblock description for details.

Each structure in this list is stored in its' own block (or blocks in the case of the group and inode tables), and doesn't share blocks with any other structure. This can involve padding the end of the block with zeroes, or extending tables with extra entries to fill up the rest of the block.

The linux/ext2_fs.h #include file defines struct ext2_super_block, struct ext2_group_desc, struct ext2_inode, struct ext2_dir_entry_2, and a lot of constants. Toybox doesn't use this file directly, instead it has an e2fs.h include of its own containting cleaned-up versions of the data it needs.

Superblock

The superblock contains a 1024 byte structure, which toybox calls "struct ext2_superblock". Where exactly this structure is to be found is a bit complicated for historical reasons.

For copies of the superblock stored in block groups after the first, the superblock structure starts at the beginning of the first block of the group, with zero padding afterwards if necessary (I.E. if the block size is larger than 1k). In modern "sparse superblock" filesystems (everything anyone still cares about), the superblock occurs in group 0 and in later groups that are powers of 3, 5, and 7. (So groups 0, 1, 3, 5, 7, 9, 25, 27, 49, 81, 125, 243, 343...) Any block group starting with a superblock will also have a group descriptor table, and ones that don't won't.

The very first superblock is weird. This is because if you format an entire block device (rather than a partition), you stomp the very start of the disk which contains the boot sector and the partition table. Back when ext2 on floppies was common, this was a big deal.

So the very first 1024 bytes of the very first block are always left alone. When the block size is 1024 bytes, then that block is left alone and the superblock is stored in the second block instead[2]. When the block size is larger than 1024 bytes, the first superblock starts 1024 bytes into the block, with the original data preserved by mke2fs and appropriate zero padding added to the end of the block (if necessary).

Group descriptor table

Block bitmap

Inode bitmap

Inode table

Data blocks

Directories

For performance reasons, directory entries are 4-byte aligned (rec_len is a multiple of 4), so up to 3 bytes of padding (zeroes) can be added at the end of each name. (This affects rec_len but not the name_len.)

The last directory entry in each block is padded up to block size. If there isn't enough space for another struct ext2_dentry the last

Question: is the length stored in the inode also padded up to block size?


Footnote 1: On some systems blocks can be larger than 4k, but for implementation reasons not larger than PAGE_SIZE. So the Alpha can have 8k blocks but most other systems couldn't mount them, thus you don't see this out in the wild much anymore.

Footnote 2: In this case, the first_data_block field in the superblock structure will be set to 1. Otherwise it's always 0. How this could POSSIBLY be useful information is an open question, since A) you have to read the superblock before you can get this information, so you know where it came from, B) the first copy of the superblock always starts at offset 1024 no matter what, and if your block size is 1024 you already know you skipped the first block.

V27960021@dg03podv27960021kj4p:~/存储/moscow$ python storage.py dual 测试机 对比机 output_diff.xlsx 运行双机对比模式... 设备1: 测试机, 设备2: 对比机 开始解析: 测试机/18_lpdump.txt 文件内容预览: Slot 0: Metadata version: 10.2 Metadata size: 2640 bytes Metadata max size: 65536 bytes Metadata slot count: 3 Header flags: virtual_ab_device Partition table: ------------------------ Name: system_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 1527575 linear super 2048 ------------------------ Name: system_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: system_ext_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 2482807 linear super 1529856 ------------------------ Name: system_ext_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: vendor_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 715543 linear super 4014080 ------------------------ Name: vendor_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ----------------------... 错误: 未找到分区信息,请检查文件格式 开始解析: 对比机/18_lpdump.txt 文件内容预览: Slot 0: Metadata version: 10.2 Metadata size: 2640 bytes Metadata max size: 65536 bytes Metadata slot count: 3 Header flags: virtual_ab_device Partition table: ------------------------ Name: system_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 1540119 linear super 2048 ------------------------ Name: system_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: system_ext_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 2511655 linear super 1544192 ------------------------ Name: system_ext_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: vendor_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 714599 linear super 4057088 ------------------------ Name: vendor_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ----------------------... 错误: 未找到分区信息,请检查文件格式 扫描目录: 测试机 扫描目录: 对比机 创建总览Sheet页 无法获取 system 分区大小 无法获取 vendor 分区大小 无法获取 product 分区大小 无法获取 odm 分区大小 无法获取 my_product 分区大小 无法获取 my_engineering 分区大小 无法获取 my_stock 分区大小 无法获取 my_heytap 分区大小 无法获取 my_company 分区大小 无法获取 my_carrier 分区大小 无法获取 my_region 分区大小 无法获取 my_preload 分区大小 无法获取 data 分区大小 无法获取 my_bigball 分区大小 无法获取 my_manifest 分区大小 无法获取 cache 分区大小 对比报告已成功生成: output_diff.xlsx 对比报告已成功生成: output_diff.xlsx V27960021@dg03podv27960021kj4p:~/存储/moscow$ python storage.py dual 测试机 对比机 output_diff.xlsx 以下是18_lpdump.txt的内容Slot 0: Metadata version: 10.2 Metadata size: 2640 bytes Metadata max size: 65536 bytes Metadata slot count: 3 Header flags: virtual_ab_device Partition table: ------------------------ Name: system_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 1527575 linear super 2048 ------------------------ Name: system_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: system_ext_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 2482807 linear super 1529856 ------------------------ Name: system_ext_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: vendor_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 715543 linear super 4014080 ------------------------ Name: vendor_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: product_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 16159 linear super 4730880 ------------------------ Name: product_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_product_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 4165551 linear super 4747264 ------------------------ Name: my_product_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: odm_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 2075367 linear super 8912896 ------------------------ Name: odm_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_engineering_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 655 linear super 10989568 ------------------------ Name: my_engineering_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: vendor_dlkm_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 164735 linear super 10991616 ------------------------ Name: vendor_dlkm_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: system_dlkm_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 839 linear super 11157504 ------------------------ Name: system_dlkm_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_stock_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 4617279 linear super 11159552 ------------------------ Name: my_stock_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_heytap_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 655 linear super 15777792 ------------------------ Name: my_heytap_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_carrier_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 655 linear super 15779840 ------------------------ Name: my_carrier_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_region_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 168575 linear super 15781888 ------------------------ Name: my_region_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_company_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 655 linear super 15951872 ------------------------ Name: my_company_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_preload_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 3191663 linear super 15953920 ------------------------ Name: my_preload_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_bigball_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 655 linear super 19146752 ------------------------ Name: my_bigball_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Name: my_manifest_a Group: qti_dynamic_partitions_a Attributes: readonly Extents: 0 .. 911 linear super 19148800 ------------------------ Name: my_manifest_b Group: qti_dynamic_partitions_b Attributes: readonly Extents: ------------------------ Super partition layout: ------------------------ super: 2048 .. 1529624: system_a (1527576 sectors) super: 1529856 .. 4012664: system_ext_a (2482808 sectors) super: 4014080 .. 4729624: vendor_a (715544 sectors) super: 4730880 .. 4747040: product_a (16160 sectors) super: 4747264 .. 8912816: my_product_a (4165552 sectors) super: 8912896 .. 10988264: odm_a (2075368 sectors) super: 10989568 .. 10990224: my_engineering_a (656 sectors) super: 10991616 .. 11156352: vendor_dlkm_a (164736 sectors) super: 11157504 .. 11158344: system_dlkm_a (840 sectors) super: 11159552 .. 15776832: my_stock_a (4617280 sectors) super: 15777792 .. 15778448: my_heytap_a (656 sectors) super: 15779840 .. 15780496: my_carrier_a (656 sectors) super: 15781888 .. 15950464: my_region_a (168576 sectors) super: 15951872 .. 15952528: my_company_a (656 sectors) super: 15953920 .. 19145584: my_preload_a (3191664 sectors) super: 19146752 .. 19147408: my_bigball_a (656 sectors) super: 19148800 .. 19149712: my_manifest_a (912 sectors) ------------------------ Block device table: ------------------------ Partition name: super First sector: 2048 Size: 12348030976 bytes Flags: none ------------------------ Group table: ------------------------ Name: default Maximum size: 0 bytes Flags: none ------------------------ Name: qti_dynamic_partitions_a Maximum size: 12343836672 bytes Flags: none ------------------------ Name: qti_dynamic_partitions_b Maximum size: 12343836672 bytes Flags: none ------------------------ --------------- Snapshot state: --------------- Update state: none Using snapuserd: 0 Using userspace snapshots: 0 Using io_uring: 0 Using o_direct: 0 Using XOR compression: 1 Current slot: _a Boot indicator: booting from unknown slot Rollback indicator: No such file or directory Forward merge indicator: No such file or directory Source build fingerprint:
最新发布
08-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值