liwen01 2024.06.09 前言 Linux系統(tǒng)中的ext2、ext3、ext4 文件系統(tǒng),它們都有很強(qiáng)的向后和向前兼容性,可以在數(shù)據(jù)不丟失的情況下進(jìn)行文件系統(tǒng)的升級(jí)。目前ext4是一個(gè)相對(duì)較成熟、穩(wěn)定且高效的文件系統(tǒng),適用于絕大部分規(guī)模和需求的Linux環(huán)境。 ext4它突出的特點(diǎn)有:數(shù)
liwen01 2024.06.09
Linux系統(tǒng)中的ext2、ext3、ext4 文件系統(tǒng),它們都有很強(qiáng)的向后和向前兼容性,可以在數(shù)據(jù)不丟失的情況下進(jìn)行文件系統(tǒng)的升級(jí)。目前ext4是一個(gè)相對(duì)較成熟、穩(wěn)定且高效的文件系統(tǒng),適用于絕大部分規(guī)模和需求的Linux環(huán)境。
ext4它突出的特點(diǎn)有: 數(shù)據(jù)分段管理、多塊分配、延遲分配、持久預(yù)分配、日志校驗(yàn)、支持更大的文件系統(tǒng)和文件大小。
ext4文件系統(tǒng)的具體實(shí)現(xiàn)比較復(fù)雜,本文嘗試用比較簡(jiǎn)單的方式用一篇文章的篇幅來(lái)簡(jiǎn)單地介紹一下它的工作原理。
為了分析ext4 文件系統(tǒng)的內(nèi)部結(jié)構(gòu)和原理,這里我們?cè)贚inux中創(chuàng)建一個(gè)ext4文件系統(tǒng)鏡像,然后通過(guò)loop虛擬設(shè)備將ext4鏡像文件掛載到某個(gè)目錄上。具體實(shí)現(xiàn)步驟如下:
dd if=/dev/zero of=./ext4_image.img bs=1M count=1024
mkfs.ext4 ext4_image.img
sudo mount -o loop ext4_image.img /home/biao/test/ext4/ext4_simulator
dumpe2fs ext4_image.img
輸出內(nèi)容信息(中間省略了部分內(nèi)容):
dumpe2fs 1.44.1 (24-Mar-2018)
Filesystem volume name:
Last mounted on: /home/biao/test/ext4/ext4_simulator
Filesystem UUID: 0169498e-f5f7-4fb8-9e9e-532088e41333
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
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
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 65536
Block count: 262144
Reserved block count: 13107
Free blocks: 247703
Free inodes: 65517
First block: 0
Block size: 4096
Fragment size: 4096
Group descriptor size: 64
Reserved GDT blocks: 127
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8192
Inode blocks per group: 512
Flex block group size: 16
Filesystem created: Fri May 24 17:18:57 2024
Last mount time: Wed Jun 5 19:15:36 2024
Last write time: Wed Jun 5 19:15:36 2024
Mount count: 3
Maximum mount count: -1
Last checked: Fri May 24 17:18:57 2024
Check interval: 0 ()
Lifetime writes: 6997 kB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 32
Desired extra isize: 32
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: 0faf0e8c-f385-4ecd-b3a4-db2a3329e121
Journal backup: inode blocks
Checksum type: crc32c
Checksum: 0x32dc1b70
Journal features: journal_64bit journal_checksum_v3
Journal size: 32M
Journal length: 8192
Journal sequence: 0x00000017
Journal start: 1
Journal checksum type: crc32c
Journal checksum: 0xa3c1b983
Group 0: (Blocks 0-32767) csum 0xf19b [ITABLE_ZEROED]
Primary superblock at 0, Group descriptors at 1-1
Reserved GDT blocks at 2-128
Block bitmap at 129 (+129), csum 0x8efc34cf
Inode bitmap at 137 (+137), csum 0x49f91ed6
Inode table at 145-656 (+145)
28517 free blocks, 8176 free inodes, 3 directories, 8176 unused inodes
Free blocks: 4251-32767
Free inodes: 17-8192
..........
..........
..........
Group 7: (Blocks 229376-262143) csum 0x7daa [INODE_UNINIT, ITABLE_ZEROED]
Backup superblock at 229376, Group descriptors at 229377-229377
Reserved GDT blocks at 229378-229504
Block bitmap at 136 (bg #0 + 136), csum 0x5bd8cca0
Inode bitmap at 144 (bg #0 + 144), csum 0x00000000
Inode table at 3729-4240 (bg #0 + 3729)
32639 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 229505-262143
Free inodes: 57345-65536
從上面dumpe2fs的數(shù)據(jù)上我們可以看出,一個(gè)1GB大小的空間,ext4 文件系統(tǒng)將它分隔成了0~7的8個(gè)Group。
ext4 的總體磁盤(pán)布局如下:
從上圖可以看出:
為什么需要這樣設(shè)計(jì)?這個(gè)下面稍晚點(diǎn)再介紹
從上面《1.1 ext4文件系統(tǒng)信息表》中可以知道Primary superblock在第0號(hào)block,每個(gè)block的大小為4096Byte。
用hexdump 命令查看超級(jí)塊的數(shù)據(jù)
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 |............<...|
00000460 c2 02 00 00 6b 04 00 00 01 69 49 8e f5 f7 4f b8 |....k....iI...O.|
00000470 9e 9e 53 20 88 e4 13 33 00 00 00 00 00 00 00 00 |..S ...3........|
00000480 00 00 00 00 00 00 00 00 2f 68 6f 6d 65 2f 62 69 |......../home/bi|
00000490 61 6f 2f 74 65 73 74 2f 65 78 74 34 2f 65 78 74 |ao/test/ext4/ext|
000004a0 34 5f 73 69 6d 75 6c 61 74 6f 72 00 00 00 00 00 |4_simulator.....|
000004b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000004c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7f 00 |................|
000004d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000004e0 08 00 00 00 00 00 00 00 00 00 00 00 0f af 0e 8c |................|
000004f0 f3 85 4e cd b3 a4 db 2a 33 29 e1 21 01 01 40 00 |..N....*3).!..@.|
00000500 0c 00 00 00 00 00 00 00 81 5b 50 66 0a f3 01 00 |.........[Pf....|
........
biao@ubuntu:~/test/ext4$
對(duì)超級(jí)塊的部分?jǐn)?shù)據(jù)進(jìn)行解析:
從上表可以看出superblock的主要內(nèi)容有:
文件系統(tǒng)信息、塊大小和塊組信息、Inode 相關(guān)信息、文件系統(tǒng)大小和使用情況、日志相關(guān)信息、掛載信息、校驗(yàn)和和備份信息
。
其實(shí)使用dumpe2fs命令查看的ext4文件系統(tǒng)信息就是從superblock上的數(shù)據(jù)解析而來(lái)。
除了Primary superblock,還在不同的group中有備份superblock,其內(nèi)容與Primary superblock原始數(shù)據(jù)相同,Primary superblock損壞的時(shí)候可以從備份區(qū)恢復(fù)回來(lái)。
在 ext4 文件系統(tǒng)中,Group Descriptor(塊組描述符)是一個(gè)關(guān)鍵的結(jié)構(gòu),用于描述和管理文件系統(tǒng)的塊組(Block Group)。每個(gè)塊組包含文件系統(tǒng)中的一部分?jǐn)?shù)據(jù)塊和 inode,并且有自己的元數(shù)據(jù)來(lái)管理這些資源。Group Descriptor 在超級(jí)塊之后緊隨其后,是文件系統(tǒng)的組織和管理的核心部分
從上面《1.1 ext4文件系統(tǒng)信息表》中可以知道group0 的 Group descriptors 在第1個(gè)數(shù)據(jù)塊中,其大小為1個(gè)block
group 0 中 Group descriptors 的數(shù)據(jù)如下:
biao@ubuntu:~/test/ext4$ hexdump -s 4096 -n 4096 -C ext4_image.img
00001000 81 00 00 00 89 00 00 00 91 00 00 00 65 6f f0 1f |............eo..|
00001010 03 00 04 00 00 00 00 00 cf 34 d6 1e f0 1f 9b f1 |.........4......|
00001020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00001030 00 00 00 00 00 00 00 00 fc 8e f9 49 00 00 00 00 |...........I....|
00001040 82 00 00 00 8a 00 00 00 91 02 00 00 b5 79 fd 1f |.............y..|
00001050 03 00 04 00 00 00 00 00 c2 fd 0a 43 fd 1f c2 4a |...........C...J|
00001060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00001070 00 00 00 00 00 00 00 00 8e a7 8c 58 00 00 00 00 |...........X....|
.........
biao@ubuntu:~/test/ext4$
對(duì)Group descriptors 的數(shù)據(jù)進(jìn)行解析,可以看到詳細(xì)當(dāng)前group的詳細(xì)信息。
一個(gè)Group descriptors 占用一個(gè)block,它不僅僅記錄自己Group上的信息,還包括了其它group的Group descriptors
Block bitmap 塊位圖用于管理塊組(Block Group)中的數(shù)據(jù)塊,Block Bitmap 記錄了塊組中每個(gè)塊的使用狀態(tài),標(biāo)識(shí)哪些塊是已使用的,哪些塊是空閑的,里面數(shù)據(jù)是按位標(biāo)記,為1表示該塊已經(jīng)被使用。
查看Block bitmap中的數(shù)據(jù)
biao@ubuntu:~/test/ext4$ hexdump -s 528384 -n 4096 -C ext4_image.img
00081000 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................|
*
00081210 ff ff ff 07 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00081220 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00082000
biao@ubuntu:~/test/ext4$
與Block bitmap工作原理類似,Inode bitmap 是用于管理塊組(Block Group)中的inode。Inode Bitmap記錄了塊組中每個(gè)inode的使用狀態(tài),標(biāo)識(shí)哪些inode是已使用的,哪些inode是空閑的。
biao@ubuntu:~/test/ext4$ hexdump -s 561152 -n 4096 -C ext4_image.img
00089000 ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00089010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00089400 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................|
*
0008a000
biao@ubuntu:~/test/ext4$
索引節(jié)點(diǎn)表是相對(duì)比較復(fù)雜的一個(gè)元文件,從上面《1.1 ext4文件系統(tǒng)信息表》我們可以知道:
Inode size: 256
Inode table at 145-656 (+145)
查看索引節(jié)點(diǎn)信息:
biao@ubuntu:~/test/ext4$ hexdump -s 593920 -n 4096 -C ext4_image.img
00091000 00 00 00 00 00 00 00 00 81 5b 50 66 81 5b 50 66 |.........[Pf.[Pf|
00091010 81 5b 50 66 00 00 00 00 00 00 00 00 00 00 00 00 |.[Pf............|
00091020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00091070 00 00 00 00 00 00 00 00 00 00 00 00 6f 16 00 00 |............o...|
00091080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00091100 ed 41 00 00 00 10 00 00 78 15 61 66 e5 5d 50 66 |.A......x.af.]Pf|
00091110 e5 5d 50 66 00 00 00 00 00 00 07 00 08 00 00 00 |.]Pf............|
00091120 00 00 08 00 04 00 00 00 0a f3 01 00 04 00 00 00 |................|
00091130 00 00 00 00 00 00 00 00 01 00 00 00 91 10 00 00 |................|
00091140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00091170 00 00 00 00 00 00 00 00 00 00 00 00 fa d3 00 00 |................|
00091180 20 00 98 7a 60 ea ef 8e 60 ea ef 8e 78 f5 3f a0 | ..z`...`...x.?.|
00091190 81 5b 50 66 00 00 00 00 00 00 00 00 00 00 00 00 |.[Pf............|
000911a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00091270 00 00 00 00 00 00 00 00 00 00 00 00 8d 16 00 00 |................|
00091280 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
對(duì)第2個(gè)索引節(jié)點(diǎn)的參數(shù)進(jìn)行解析:
在ext4文件系統(tǒng)中,0~11號(hào)索引是特殊定義的索引節(jié)點(diǎn):
在 ext4 文件系統(tǒng)中,inode 是一個(gè)數(shù)據(jù)結(jié)構(gòu),代表文件系統(tǒng)中的每個(gè)文件和目錄。每個(gè) inode 包含了有關(guān)文件的元數(shù)據(jù),例如文件大小、權(quán)限、所有者信息等。inode.i_block 是 inode 結(jié)構(gòu)中用于指向文件數(shù)據(jù)塊的字段,是文件系統(tǒng)如何找到并訪問(wèn)文件內(nèi)容的核心部分.
inode.i_block 是 ext4 文件系統(tǒng)中確保文件數(shù)據(jù)高效存儲(chǔ)和訪問(wèn)的關(guān)鍵組件,i_block里的數(shù)據(jù)類型,需要根據(jù)i_flags中的參數(shù)來(lái)確認(rèn),上面《圖7.1 Inode table參數(shù)解析》i_flags 的值是0x080000,同使用的是 Inode uses extents (EXT4_EXTENTS_FL)
iblock的長(zhǎng)度是60字節(jié),我們下面通過(guò)iblock里的參數(shù)找到該inode對(duì)應(yīng)文件所在的block。
文件系統(tǒng)中文件信息如下:
root@ubuntu:/home/biao/test/ext4/ext4_simulator# tree
.
├── lost+found
├── test1
│?? └── 0000.media
├── test2
│?? └── 0011.media
├── test3
│?? └── 0022.media
└── test4
└── 0033.media
5 directories, 4 files
root@ubuntu:/home/biao/test/ext4/ext4_simulator#
如果我們要找到0033.media文件所在block,我們先通過(guò)stat 查看0033.media 的inode節(jié)點(diǎn)
biao@ubuntu:~/test/ext4/ext4_simulator/test4$ stat 0033.media
File: 0033.media
Size: 1662591 Blocks: 3248 IO Block: 4096 regular file
Device: 719h/1817d Inode: 16 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ biao) Gid: ( 1000/ biao)
Access: 2024-06-05 10:39:09.000000000 +0800
Modify: 2024-05-14 01:01:26.000000000 +0800
Change: 2024-06-05 10:39:09.423416410 +0800
Birth: -
biao@ubuntu:~/test/ext4/ext4_simulator/test4$
定位到索引所在的位置:
145 * 4096 +(16-1)*256 = 593,920 + 3,840 = 597,760 = 0x91F00
索引節(jié)點(diǎn)數(shù)據(jù)
*
00091f00 a4 81 e8 03 7f 5e 19 00 cd cf 5f 66 cd cf 5f 66 |.....^...._f.._f|
00091f10 66 47 42 66 00 00 00 00 e8 03 01 00 b0 0c 00 00 |fGBf............|
00091f20 00 00 08 00 01 00 00 00 0a f3 01 00 04 00 00 00 |................|
00091f30 00 00 00 00 00 00 00 00 96 01 00 00 b5 84 00 00 |................|
00091f40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
i_block 的偏移量是0x28,對(duì)i_block的數(shù)據(jù)進(jìn)行解析:
將邏輯塊0開(kāi)始的0x196個(gè)block映射到物理0x84b5開(kāi)始的0x196個(gè)物理塊中
0x84b5 = 33973
33973 * 4096 = 139,153,408 = 0x84B 5000
查看文件系統(tǒng)的0x84b5 block數(shù)據(jù),與0033.media文件的數(shù)據(jù)是相同的
第 0x84b5 block
biao@ubuntu:~/test/ext4$ hexdump -s 139153408 -n 4096 -C ext4_image.img
084b5000 01 00 00 00 25 25 01 00 7a 34 9e 74 8f 01 00 00 |....%%..z4.t....|
084b5010 8c d1 0f f2 ff ff ff ff 00 00 00 01 40 01 0c 01 |............@...|
084b5020 ff ff 01 40 00 00 03 00 90 00 00 03 00 00 03 00 |...@............|
084b5030 96 bc 09 00 00 00 01 42 01 01 01 40 00 00 03 00 |.......B...@....|
084b5040 90 00 00 03 00 00 03 00 96 a0 01 20 20 05 11 67 |........... ..g|
084b5050 be e4 4a 17 25 05 05 05 e1 00 00 03 00 01 00 00 |..J.%...........|
084b5060 03 00 14 2f 84 02 08 00 00 00 01 44 01 c0 73 c0 |.../.......D..s.|
084b5070 c6 d9 00 00 00 01 26 01 ac 39 80 1f cd 51 b5 b2 |......&..9...Q..|
084b5080 70 02 84 80 26 99 cd b5 f6 00 cf a3 06 b7 71 6b |p...&.........qk|
0033.media
biao@ubuntu:~/test/ext4/ext4_simulator/test4$ hexdump -s 0 -n 4096 -C 0033.media
00000000 01 00 00 00 25 25 01 00 7a 34 9e 74 8f 01 00 00 |....%%..z4.t....|
00000010 8c d1 0f f2 ff ff ff ff 00 00 00 01 40 01 0c 01 |............@...|
00000020 ff ff 01 40 00 00 03 00 90 00 00 03 00 00 03 00 |...@............|
00000030 96 bc 09 00 00 00 01 42 01 01 01 40 00 00 03 00 |.......B...@....|
00000040 90 00 00 03 00 00 03 00 96 a0 01 20 20 05 11 67 |........... ..g|
00000050 be e4 4a 17 25 05 05 05 e1 00 00 03 00 01 00 00 |..J.%...........|
00000060 03 00 14 2f 84 02 08 00 00 00 01 44 01 c0 73 c0 |.../.......D..s.|
00000070 c6 d9 00 00 00 01 26 01 ac 39 80 1f cd 51 b5 b2 |......&..9...Q..|
00000080 70 02 84 80 26 99 cd b5 f6 00 cf a3 06 b7 71 6b |p...&.........qk|
通過(guò)上面《圖7.2 特殊索引節(jié)點(diǎn)》我們知道根目錄的inode是2,查看根目錄的索引節(jié)點(diǎn)位置:
根目錄 inode 位置
145 * 4096 +(2-1)*256 = 593,920 + 256 = 594,176 = 0x91100
根目錄 inode 數(shù)據(jù)
*
00091100 ed 41 00 00 00 10 00 00 77 be 5f 66 e5 5d 50 66 |.A......w._f.]Pf|
00091110 e5 5d 50 66 00 00 00 00 00 00 07 00 08 00 00 00 |.]Pf............|
00091120 00 00 08 00 04 00 00 00 0a f3 01 00 04 00 00 00 |................|
00091130 00 00 00 00 00 00 00 00 01 00 00 00 91 10 00 00 |................|
00091140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
0x1091 = 4,241
4,241 * 4096 = 17,371,136 = 0x109 1000
biao@ubuntu:~/test/ext4$ hexdump -s 17371136 -n 4096 -C ext4_image.img
01091000 02 00 00 00 0c 00 01 02 2e 00 00 00 02 00 00 00 |................|
01091010 0c 00 02 02 2e 2e 00 00 0b 00 00 00 14 00 0a 02 |................|
01091020 6c 6f 73 74 2b 66 6f 75 6e 64 00 00 0c 00 00 00 |lost+found......|
01091030 10 00 05 02 74 65 73 74 31 00 00 00 01 20 00 00 |....test1.... ..|
01091040 10 00 05 02 74 65 73 74 32 00 00 00 02 20 00 00 |....test2.... ..|
01091050 10 00 05 02 74 65 73 74 33 00 00 00 03 20 00 00 |....test3.... ..|
01091060 98 0f 05 02 74 65 73 74 34 00 00 00 00 00 00 00 |....test4.......|
01091070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
01091ff0 00 00 00 00 00 00 00 00 0c 00 00 de 67 85 5b 11 |............g.[.|
01092000
biao@ubuntu:~/test/ext4$
可以看到根目錄上的所有信息,下面是對(duì)根目錄的目錄項(xiàng)進(jìn)行解析
同樣的方法,可以定位到各子目錄上的信息。
fsck
工具利用超級(jí)塊、塊組描述符、塊位圖和 inode 位圖來(lái)檢查文件系統(tǒng)的一致性。
fsck
能夠更快地進(jìn)行一致性檢查,減少系統(tǒng)恢復(fù)時(shí)間。
上面只是簡(jiǎn)單的介紹了ext4文件系統(tǒng)的基礎(chǔ)內(nèi)容,一些更加詳細(xì)的內(nèi)容,比如日志、碎片整理、軟連接與硬連接等等都還沒(méi)有介紹,受篇幅限制,這些以后再介紹吧。
機(jī)器學(xué)習(xí):神經(jīng)網(wǎng)絡(luò)構(gòu)建(下)
閱讀華為Mate品牌盛典:HarmonyOS NEXT加持下游戲性能得到充分釋放
閱讀實(shí)現(xiàn)對(duì)象集合與DataTable的相互轉(zhuǎn)換
閱讀鴻蒙NEXT元服務(wù):論如何免費(fèi)快速上架作品
閱讀算法與數(shù)據(jù)結(jié)構(gòu) 1 - 模擬
閱讀基于鴻蒙NEXT的血型遺傳計(jì)算器開(kāi)發(fā)案例
閱讀5. Spring Cloud OpenFeign 聲明式 WebService 客戶端的超詳細(xì)使用
閱讀Java代理模式:靜態(tài)代理和動(dòng)態(tài)代理的對(duì)比分析
閱讀Win11筆記本“自動(dòng)管理應(yīng)用的顏色”顯示規(guī)則
閱讀本站所有軟件,都由網(wǎng)友上傳,如有侵犯你的版權(quán),請(qǐng)發(fā)郵件[email protected]
湘ICP備2022002427號(hào)-10 湘公網(wǎng)安備:43070202000427號(hào)© 2013~2025 haote.com 好特網(wǎng)