前言:
目前大部分Linux操作系统使用的文件系统是ext4和xfs, 了解ext4在磁盘中的分布
1. 容量概念
对于储存几个概念的解析:
sector(扇区) :
1.磁盘最小的储存单位,可以通过命令行 fdisk -l得知单位每sector的大小(一般是512byte)
2.机械硬盘HDD的可用空间大小计算公式是 heads(磁头数量) cylinders(柱面数量) sectors(扇区数量) * 每个sector大小(512byte)
3.所以固态可用空间的总大小是 sectors(扇区数量) * 每个sector大小(512byte)。
4.这几个属性是固定不能修改, 但可以通过命令读取得到。因为固态硬盘SSD没有磁头柱面的概念。
// 1073741824 bytes的大小刚好是 sectors * 512 bytes 得出来的
root@xxxxxx:~# fdisk -l /dev/rbd0
Disk /dev/rbd0: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 4194304 bytes / 4194304 bytes
Disklabel type: dos
Disk identifier: 0x5510f42b
Device Boot Start End Sectors Size Id Type
/dev/rbd0p1 8192 2097151 2088960 1020M 83 Linux
root@xxxxxx:~#
block (块) :
1.是文件系统EXT4,FAT32,XFS等最小的储存单位,使用命令 blkid 可查看文件系统类型。
2.每一个block只能存储一个文件的数据。
3.当格式化一个文件系统时,如果选择不当,就会造成大量的磁盘空间浪费。
4.默认操作系统每个block的大小是4k(4096bytes),一个block由连续的sector组成。
5.一个文件在文件系统中的block不一定是连续的。
inode (索引节点) :
1.记录文件的权限、属性和数据所在块block的号码。
2.每个文件都有且仅有一个的inode,每个inode都有自己的编号,可以把inode简单地理解为文档索引。
data block (数据区块) :
1.block可以区分成两种概念,第一就是上述说的文件系统中最小的储存单位(存在1个block存下多个inode的说法),第二也是这里谈及到数据区块
2.存储的文件内容,也叫数据区块(data block),每个block都有自己的编号,Ext2支持的单位block容量仅为1k、2k、4k
目录的data block保存该目录下所有文件以及子目录的名字,inode编号等信息。
2. Ext4文件系统磁盘布局
Ext4文件系统把整个分区划分成各个block group(块组),每个block group由superblock(超级块),block group(区块群组), block bitmap(区块对照表), inode bitmap(inode 对照表)和group descriptor以及inode table、data block组成。
1024 bytes 的 Group 0 Padding(boot block)只有 块组0 有,用于装载该分区的操作系统。MBR 为主引导记录用来引导计算机。在计算机启动时,BIOS 读入并执行 MBR,MBR 作的第一件事就是确定活动分区(这对应于双系统的计算机开机时选择启动项,单系统的直接就能确定了所以就不需要选择),读入活动分区的引导块(Boot block),引导块再加载该分区中的操作系统。
官方网站:https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout
图2为简略图省略了块组描述与预留GDT部分。
superblock(超级块)
1.记录文件系统(filesystem)的整体信息,包括inode/block的总量、使用量、剩余量、大小、以及文件系统的格式和相关信息。
备注:superblock对于文件系统太重要了,但是文件系统的superblock又只有一个,所以除了第一个block group含有superblock外,后续block group都可能会含有备份的superblock,目的就是为了避免superblock单点无法救援的问题。
block bitmap(区块对照表)
1.一个block只能被一个文件使用,当我们新增文件时,肯定需要使用新的block来记录文件数据。那么如何快速地知道,哪些block是新的?哪些block是已经使用了的?block bitmap就是这样被设计出来,记录所有使用和未使用的block号码。同样的,
2.我们删除文件时,先从block bitmap中找到对应的block号码,然后更新标志为未使用,最后释放block。
inode bitmap(inode 对照表)
和block bitmap一样的设计理念,只不过它记录地是已使用和未使用的inode号码,这里就不再敖述了。
group descriptor
描述每个区段(block group)开始和结束的block号码,以及说明每个区段(inodemap、blockmap、inode table)分别介于哪些block号码之间。
inode table
该组所有inode的合集,可以通过inode编号 以inode table起始位置作偏移找到inode所在的位置。
data block
上节已叙述,为存放文件或目录数据的地方。
2.1 获取ext4分区相关信息
可以使用dumpe2fs 打印ext4分区的详细信息。
Inode count: 65280
Block count: 261120
Block size: 4096
Inode size: 256
Blocks per group: 32768
Inodes per group: 8160
可以从以上获取得整个分区inode的数量是65280 block的数量261120,一个block的大小是4K,每个block group有 32768 block (4096 * 32768 大小)
其中一个block 可以存放 4096Bytes (block size) / 2566Bytes (Inode size) = 16 个 inode
一个 block group 有 8160 个 inode
root@xxxxxx:~# dumpe2fs /dev/rbd0p1
dumpe2fs 1.44.1 (24-Mar-2018)
Filesystem volume name: <none>
Last mounted on: /mnt/ceph-vol1
Filesystem UUID: 6f074db1-8f6f-4313-851d-fa3c9b7590f8
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: 65280
Block count: 261120
Reserved block count: 13056
Free blocks: 247156
Free inodes: 65267
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: 8160
Inode blocks per group: 510
RAID stride: 1024
RAID stripe width: 1024
Flex block group size: 16
Filesystem created: Wed Oct 14 17:42:24 2020
Last mount time: Thu Oct 15 17:35:48 2020
Last write time: Thu Oct 15 17:35:48 2020
Mount count: 2
Maximum mount count: -1
Last checked: Wed Oct 14 17:42:24 2020
Check interval: 0 (<none>)
Lifetime writes: 153 MB
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: 6a8799b4-6528-499d-9f62-fde45cb8991a
Journal backup: inode blocks
Checksum type: crc32c
Checksum: 0x1ab86e25
Journal features: journal_64bit journal_checksum_v3
Journal size: 16M
Journal length: 4096
Journal sequence: 0x00000010
Journal start: 1
Journal checksum type: crc32c
Journal checksum: 0xdc3ea2cb
Group 0: (Blocks 0-32767) csum 0xe2fe [ITABLE_ZEROED]
Primary superblock at 0, Group descriptors at 1-1
Reserved GDT blocks at 2-128
Block bitmap at 129 (+129), csum 0x06292657
Inode bitmap at 137 (+137), csum 0xc6f39952
Inode table at 145-654 (+145)
28533 free blocks, 8143 free inodes, 6 directories, 8143 unused inodes
Free blocks: 4235-32767
Free inodes: 18-8160
Group 1: (Blocks 32768-65535) csum 0x4d02 [INODE_UNINIT, ITABLE_ZEROED]
Backup superblock at 32768, Group descriptors at 32769-32769
Reserved GDT blocks at 32770-32896
Block bitmap at 130 (bg #0 + 130), csum 0xfa6bcbac
Inode bitmap at 138 (bg #0 + 138), csum 0x00000000
Inode table at 655-1164 (bg #0 + 655)
27518 free blocks, 8160 free inodes, 0 directories, 8160 unused inodes
Free blocks: 32898-33791, 38912-65535
Free inodes: 8161-16320
Group 2: (Blocks 65536-98303) csum 0xba1c [INODE_UNINIT, ITABLE_ZEROED]
Block bitmap at 131 (bg #0 + 131), csum 0x8c3dbe87
Inode bitmap at 139 (bg #0 + 139), csum 0x00000000
Inode table at 1165-1674 (bg #0 + 1165)
28672 free blocks, 8160 free inodes, 0 directories, 8160 unused inodes
Free blocks: 69632-98303
Free inodes: 16321-24480
Group 3: (Blocks 98304-131071) csum 0x579e [INODE_UNINIT, BLOCK_UNINIT, ITABLE_ZEROED]
Backup superblock at 98304, Group descriptors at 98305-98305
Reserved GDT blocks at 98306-98432
Block bitmap at 132 (bg #0 + 132), csum 0x00000000
Inode bitmap at 140 (bg #0 + 140), csum 0x00000000
Inode table at 1675-2184 (bg #0 + 1675)
32639 free blocks, 8160 free inodes, 0 directories, 8160 unused inodes
Free blocks: 98433-131071
Free inodes: 24481-32640
Group 4: (Blocks 131072-163839) csum 0xfc69 [INODE_UNINIT, BLOCK_UNINIT, ITABLE_ZEROED]
Block bitmap at 133 (bg #0 + 133), csum 0x00000000
Inode bitmap at 141 (bg #0 + 141), csum 0x00000000
Inode table at 2185-2694 (bg #0 + 2185)
32768 free blocks, 8160 free inodes, 0 directories, 8160 unused inodes
Free blocks: 131072-163839
Free inodes: 32641-40800
Group 5: (Blocks 163840-196607) csum 0x0b15 [INODE_UNINIT, BLOCK_UNINIT, ITABLE_ZEROED]
Backup superblock at 163840, Group descriptors at 163841-163841
Reserved GDT blocks at 163842-163968
Block bitmap at 134 (bg #0 + 134), csum 0x00000000
Inode bitmap at 142 (bg #0 + 142), csum 0x00000000
Inode table at 2695-3204 (bg #0 + 2695)
32639 free blocks, 8160 free inodes, 0 directories, 8160 unused inodes
Free blocks: 163969-196607
Free inodes: 40801-48960
Group 6: (Blocks 196608-