当前位置:网站首页>Understanding formatting principles

Understanding formatting principles

2020-11-06 21:04:00 Zhang Yanfei Allen

In the foreword 《 Disk opening : Remove the hard shell of the mechanical hard disk !》 and 《 Disassemble the SSD structure 》 in , We learned that the basic unit of hard disk is sector . stay 《 Disk partitioning also implies technical skills 》 We also learned about how disk partitioning works , However, the hard disk just divided can not be directly used by the operating system , It has to be formatted . So let's have a brief talk today ,Linux What's going on in the format below .

Linux The format command is mkfs,mkfs When formatting, you need to specify the partition and file system type . This command is actually to partition and manage our continuous disk space . I did it on my machine , Output is as follows :

# mkfs -t ext4 /dev/vdb
mke2fs 1.42.9 (28-Dec-2013)
 File system tags =
OS type: Linux
 Block size =4096 (log=2)
 Block size =4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
 The first data block =0
Maximum filesystem blocks=2174746624
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624, 11239424, 20480000, 23887872  

Let's take a closer look at the information carried in the output above .

inode And block

In the above results, we see several important information

  • Block size :4096 byte
  • inode Number :6553600
  • block Number :26214400

The block size is set to 4096 byte , Let's analyze two scenarios

  • If your file system is all for storage 1KB The following little file , This time your disk 1/3 Space will be wasted and unusable .
  • If all your papers are GB The big documents above , This is your inode In an index node, you need to maintain many of them directly or indirectly block Reference no.

Obviously , In both cases 4096 The block size of bytes is not appropriate . You need to choose your own block size and reformat it .

Let's look at the other two data ,inode Quantity and sum block Number . We use it block Divide the quantity by inode,26214400/6553600=4, That is to say, on average 4 individual block There will be one. inode. Two more extreme examples :

  • Case one , Let's say our documents are all 4KB Following , So the last thing that we use our file system is inode It's all used up , also 1/3 Of block Free , And there's no way to create new files anymore .
  • The second case , If our files are all very large , Every document needs 1000 individual block, The end result is block It's all used up , however inode It's all free again , There is no way to create documents at this time .

In these cases ,block and inode The ratio is not suitable for your use , You need to reconfigure according to your business .mkfs The results in a fool's format can't meet your business needs , You need to use some other formatting commands , such as mke2fs, This command allows you to enter more detailed formatting options ,demo as follows :

mke2fs -j -L " Volume label " -b 2048 -i 8192 /dev/sdb1

Block group

Let's go back to the formatted results , The results show some and groups Related east east , as follows :

800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group

So this groups What the hell is that ? In fact! , All formatted inode It's not placed next to each other , Again block Neither . It's divided into group, every last group There are some in it inode and block. The logic diagram is as follows :

file

How big is this block group ? Note that there is only one data block bitmap in each block , If your block size is 4KB, Such a bit Represents a block of data ,4KB There can be 32KB individual bit, Can manage 32K*4K=128M A block of data . Let's actually verify , as follows :

# dumpe2fs /dev/vdb
......
Block size:               4096
Inode size:               256
Inode count:              6553600
Block count:              26214400
......
Group 16: (Blocks 524288-557055) [INODE_UNINIT, ITABLE_ZEROED]
  Checksum 0xe838, unused inodes 8192
  Block bitmap at 524288 (+0), Inode bitmap at 524304 (+16)
  Inode The watch is located in  524320-524831 (+32)
  24544 free blocks, 8192 free inodes, 0 directories, 8192 Unused inodes
   Number of blocks available : 532512-557055
   You can use inode Count : 131073-139264
......
Group 799: (Blocks 26181632-26214399) [INODE_UNINIT, ITABLE_ZEROED]
......

The information contained in the above results is as follows :

  • The partition has been formatted in total 800 Block groups
  • Block group 16 share 32K individual block( The first 524288-557055),
  • block The bitmap is in 524288 On this block
  • inode The bitmap is in 524304 On this block
  • inode table Occupied 612 individual block(524320-524831)
  • The rest of it block(32K-1-1-612) It's really for users , At present, the idle and unallocated are in Free blocks You can see that .

Understand the table of contents again

Okay , After understanding the above principles , Let's go back and see how the data used by directories is organized on disk . When you create a directory , The operating system will be in inode Find unused on bitmap inode Number , When you find it, put inode Assigned to you . The directory will be assigned a by default block, So we need to check block Bitmap , Find and assign one block. stay block Inside , What is stored is defined by the file system itself desty It's structured , Each structure will save the file name under it , Of documents inode Number and other information . The final space used by an actual folder on disk is shown in the figure below :

file

The directory block The files and subdirectories under it are saved in desty Structure , Save their file names and inode Number . Understand the catalog , The same is true for documents . Also need to consume inode, When data is written , Apply again block.

Conclusion

A hard disk is a large array of sectors , It can't be used by us , It needs to be partitioned 、 Format and mount three steps . Partition is to divide all sectors into different blocks according to the cylinder , Formatting changes the original array of sectors into a Linux The file system uses inode、block Wait for the basic elements . The formatter feels a little bit like the chopper in the chef team , Turn raw materials into scallion that can be directly used by cooks , Meat section . After formatting, mount it through the last step , The corresponding order is mount, Then you can create and save files under it .

In fact, the equipment that has just been partitioned can also be used , The partition at this time is called bare partition , It's also called naked devices . such as oracle It is to bypass the operating system and use naked devices directly . But you can't use it at this time Linux It's packaged for you in the file system inode、block Composed of files and directories , The development workload will increase .

The most time-consuming part of writing is drawing , I'm going blind , Give me a compliment when I pass by , Thank you !


file


Development of hard disk album of internal training :


My official account is 「 Develop internal skill and practice 」, I'm not just talking about technical theory here , It's not just about practical experience . It's about combining theory with practice , Deepen the understanding of theory with practice 、 Use theory to improve your technical practice ability . Welcome to my official account , Please also share with your friends ~~~

版权声明
本文为[Zhang Yanfei Allen]所创,转载请带上原文链接,感谢