In-depth analysis of the ext2 File System mke2fs

Source: Internet
Author: User
The focus of the previous blog post is actually the overall organizational framework of ext2. We know that the ext2 file system is composed of block groups, and the organizational structure of each block group is as follows. We even directly present the contents in the super block and group descriptor in hexadecimal format. This blog post mainly describes how to generate an ext2 file system suitable for mke2fs, which is basically a parameter selection problem. Common mke2fs parameters include:   1)-B block-size.   Block Size is an important parameter in the ext2 file system. Currently, only 1024, 2048, and 4096 are supported. One of the points mentioned in the previous blog is that the block size determines the maximum number of blocks managed by each block group. This is because when one block is used as a bitmap in a block group, whether or not a part of the block group is idle. 1024 ---- up to 8 K blocks ---------- the size of the block group is 8 MB (1024 bytes/block * 8 K blocks) 2048 ---- A maximum of 16 K blocks --------- the block group size is 32 M bytes 4096 ---- a maximum of 32 K fast ---------- the block group size is M bytes. Block-size is important, not only does it determine the attributes of the block group, but also the attributes (maximum length) of an ext2 file ). We know that inode has an array of 15 characters pointing to the data storage block of the file. The first 12 are direct pointers pointing to the block where the data is located. 13th array elements are level 1 Indirect pointers, 14th array elements are level 2 indirect pointers, and 15th elements are level 3 indirect pointers, the maximum length of files supported by ext2 is greatly extended. The document organization is not the focus of this blog.
Table 1. File addressing range corresponding to various data blocks
Block Size Direct addressing Indirect addressing Secondary indirect addressing Three indirect addressing
1024 12kb 268kb 64.26 MB 16.06 GB
2048 24kb 1.02 MB 513.02 MB 265.5 GB
4096 48kb 4.04 MB 4 GB ~ 4 TB
The default value is 1024. Blocksize determines the total number of blocks (when the disk space is certain. For example, the space is 500 mb ).
  1. Linux-Tods:/mnt/bean # mke2fs/dev/loop0
  2. Mke2fs 1.41.9 (22-aug-2009)
  3. Filesystem label =
  4. OS type: Linux
  5. Block size = 1024 (log = 0)
  6. Fragment size = 1024 (log = 0)
  7. 128016 inodes, 512000 Blocks
  8. 25600 blocks (5.00%) reserved for the Super User
  9. First data block = 1
  10. Maximum filesystem blocks = 67633152
  11. 63 BLOCK groups
  12. 8192 blocks per group, 8192 fragments per group
  13. 2032 inodes per group
  14. Superblock backups stored on blocks:
  15. 8193,245 77, 40961,573 45, 73729,204 221185,401
  16. Writing inode tables: Done
  17. Writing superblocks and filesystem accounting information: Done
  18. This filesystem will be automatically checked every 26 mounts or
  19. 180 days, whichever comes first. Use tune2fs-C or-I to override.
Or Linux-Tods:/mnt/bean # tune2fs-L/dev/loop0tune2fs 1.41.9 (22-aug-2009) filesystem volume name: <none> last mounted on: <not available> filesystem UUID: using magic number: 0xef53filesystem Revision #: 1 (dynamic) filesystem features: ext_attr resize_inode dir_index filetype using flags: Using default mount options: (none) filesystem state: cleanerrors behavior: continuefilesystem OS type: linuxinode count: 128016 block count: 512000 reserved block count: 25600 free blocks: 493526 free inodes: 128005 first block: 1 block size: 1024 Fragment Size: 1024 users can change it to 4096, in this case, the total number of blocks will change from 512000 to 128000.
  1. Linux-Tods:/mnt/bean # mke2fs-B 4096/dev/loop0
  2. Mke2fs 1.41.9 (22-aug-2009)
  3. Filesystem label =
  4. OS type: Linux
  5. Block size = 4096 (log = 2)
  6. Fragment size = 4096 (log = 2)
  7. 128000 inodes, 128000 Blocks
  8. 6400 blocks (5.00%) reserved for the Super User
  9. First data block = 0
  10. Maximum filesystem blocks = 134217728
  11. 4 block groups
  12. 32768 blocks per group, 32768 fragments per group
  13. 32000 inodes per group
  14. Superblock backups stored on blocks:
  15. 32768,983 04
  16. Writing inode tables: Done
  17. Writing superblocks and filesystem accounting information: Done
  18. This filesystem will be automatically checked every 39 mounts or
  19. 180 days, whichever comes first. Use tune2fs-C or-I to override.
Bytes --------------------------------------------------------------------------------------------------- 2)-M reserved-blocks-PercentageThe proportion of the block retained by the Super User. To prevent all blocks from being used up, you must reserve a certain proportion of blocks for super users if they cannot be started. The default value is 5%. Below is the reserved block in the default parameters in the previous blog. Total number of blocks * % 5 = number of reserved blocks. Block count: 512000 reserved block count: 25600 let's change its parameter to 10%: we can see that the reserved block count is changed to 51200. In general, this parameter does not need to be changed.
  1. Linux-Tods:/mnt/bean # mke2fs-M 10/dev/loop0
  2. Mke2fs 1.41.9 (22-aug-2009)
  3. Filesystem label =
  4. OS type: Linux
  5. Block size = 1024 (log = 0)
  6. Fragment size = 1024 (log = 0)
  7. 128016 inodes, 512000 Blocks
  8. 51200 blocks (10.00%) reserved for the Super User
  9. First data block = 1
  10. Maximum filesystem blocks = 67633152
  11. 63 BLOCK groups
  12. 8192 blocks per group, 8192 fragments per group
  13. 2032 inodes per group
  14. Superblock backups stored on blocks:
  15. 8193,245 77, 40961,573 45, 73729,204 221185,401
  16.  
  17. Writing inode tables: Done
  18. Writing superblocks and filesystem accounting information: Done
  19.  
  20. This filesystem will be automatically checked every 23 mounts or
  21. 180 days, whichever comes first. Use tune2fs-C or-I to override.
Bytes ------------------------------------------------------------------------------------------------------- 3-I bytes-per-inode This parameter is an important parameter in my opinion. At first glance, I don't know what it is. When I deeply understand the Linux kernel and talk about mke2fs, I say that the default option is to allocate an inode for every 8192 byte groups. I didn't understand what it meant for the first time. After all, it is not as easy to understand as blocksize. Then I understood it. (In addition, this parameter ulk is incorrect. I tested it. By default, mke2fs is 4096 bytes per inode.) This parameter indicates how many bytes are allocated with an inode. This parameter reflects our expectation for the size of each file stored in our file system. In other words, we expect that each file is about 4096 bytes, and each file requires an inode. Therefore, we need 500 m/4096 = 12800 inode in total. For example, if I plan to store very small files in my ext2, for example, most files are smaller than 1 K. If the-I value is 8192, the following situation occurs: There are 512000 blocks, but there are only 64008 inode. If you haven't realized what I mean, check that there are about 1000 free inode in group 0, but there are nearly 8000 free blocks. According to my settings, most of my files are small, so when the number of my files reaches 1000, free inode is used up first, and group 0 has nearly 7000 idle parts. That is to say, if the-I parameter is selected, the inode is used up first. Even if there is a large free block, you cannot create files. OK. The value range of this parameter is 1024 ~ 65536. The Setting principle is: consider the size of most of your files. Interestingly, you can set it to no integer multiple of 1024.

Click (here) to fold or open

  1. Linux-Tods:/mnt/bean # mke2fs-I 8192/dev/loop0
  2. Mke2fs 1.41.9 (22-aug-2009)
  3. Filesystem label =
  4. OS type: Linux
  5. Block size = 1024 (log = 0)
  6. Fragment size = 1024 (log = 0)
  7. 64008 inodes, 512000 Blocks
  8. 25600 blocks (5.00%) reserved for the Super User
  9. First data block = 1
  10. Maximum filesystem blocks = 67633152
  11. 63 BLOCK groups
  12. ......
  13. Linux-Tods:/mnt/bean # dumpe2fs/dev/loop0
  14. Group 0: (Blocks 1-8192)
  15. Primary superblock at 1, group descriptors at 2-3
  16. Reserved gdt blocks at 4-259
  17. Block bitmap at 260 (+ 259), inode bitmap at 261 (+ 260)
  18. Inode table at 262-388 (+ 261)
  19. 7790 free blocks, 1005 free inodes, 2 Directories
  20. Free blocks: 403-8192
  21. Free inodes: 12-1016
  22. Group 1: (blocks 8193-16384)
  23. Backup superblock at 8193, group descriptors at 8194-8195
  24. Reserved gdt blocks at 8196-8451
  25. Block bitmap at 8452 (+ 259), inode bitmap at 8453 (+ 260)
  26. Inode table at 8454-8580 (+ 261)
  27. 7804 free blocks, 1016 free inodes, 0 Directories
  28. Free blocks: 8581-16384
  29. Free inodes: 1017-2032
You can set it to 1524.
  1. Linux-Tods:/mnt/bean # mke2fs-I 1524/dev/loop0
  2. Mke2fs 1.41.9 (22-aug-2009)
  3. Filesystem label =
  4. OS type: Linux
  5. Block size = 1024 (log = 0)
  6. Fragment size = 1024 (log = 0)
  7. 344232 inodes, 512000 Blocks
  8. 25600 blocks (5.00%) reserved for the Super User
  9. First data block = 1
  10. Maximum filesystem blocks = 67633152
  11. 63 BLOCK groups
  12. 8192 blocks per group, 8192 fragments per group
  13. 5464 inodes per group
  14. Superblock backups stored on blocks:
The last problem, I thought for a long time and didn't want to understand it. If a passing expert knows the answer, let me know: Below is the default option of mke2fs, and we found that there are 16 more inode count, the default-I is 4096 bytes per inode. It should have been 500 mb/4096b = 128000, but the result of dumpefs is 128016. I cannot explain the 16 inode. I saw some e2fsprogs code yesterday and I didn't see it.
  1. Inode count: 128016
  2. Block count: 512000
  3. Reserved block count: 25600
  4. Free blocks: 493526
  5. Free inodes: 128005
  6. First block: 1
  7. Block Size: 1024
Other parameters can also be specified, but I don't think it is necessary. For example, inode size can be specified. But I think 128 bytes is quite good and it is not a waste, it is also suitable for the cache line size. It is a waste of space to set it to 256. For another example, block per group, we can calculate the maximum value based on blocksize. For example, 1024 can support a maximum of 8192 blocks per group. You have to set it to 4096 blocks per group, in fact, it is not necessary. Reference 1 how to restore files deleted on Linux using ulk2 Linux man page3 e2fsprogs4

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.