Reading Notes (8): Linux disk and File System Management (1), Linux private dish

Source: Internet
Author: User

Reading Notes (8): Linux disk and File System Management (1), Linux private dish

The most traditional Disk File System in Linux is EXT2 (of course, it has now been upgraded to EXT4). This chapter mainly introduces the disk file system in Linux, this article focuses on the Linux EXT2 file system.

We all know that disk partitions need to be formatted after they are completed. The reason for formatting is that the file system formats used by each operating system are different. In Linux, the file system format EXT2 is used. By default, the Windows operating system cannot identify the EXT2 File System in Linux. Generally, a partition is a file system. However, due to the use of new technologies, we can combine multiple partitions into a file system,So now we call a data that can be mountedA file system.

Linux Ext2 file system is an inode-based file system.In the Ext2 file system, inode is used to record the attributes and permissions of the file, block is used to record the actual content of the file, and the file system also has a superblock) the overall information of the file system is recorded, including the total amount of inode and block, usage, and remaining amount. The file system has planned inode and block at the beginning, so the inode and block remain unchanged unless you reformat the File System (or use resize2fs to change the file system size. In addition, the Linux Ext2 file system manages inode and block in block groups. Each block group has an independent inode/block/superblock system, in this way, it will not be difficult to manage because the number of inode and blocks in the entire file system is too large. The Ext2 file system structure can be used to represent:


As mentioned in previous articles,The first sector of each file system is the start sector. The Boot Sector can install the boot loader.Although each boot device only has one Master Boot Record (MBR), we have the opportunity to perform multiple boot operations because boot sectors of each file system can also Install Boot Loader.

Next is superblock. Although each block group may contain a super block, the entire file system should have only one super block. What does it mean? In fact, except for the first block group, other block groups generally do not have Super blocks. Even if they do, they are just backups of Super blocks in the first block group, is used for super block rescue.

The file system description is used to describe the start and end block numbers of each block group and the block numbers of each block group. The file system describes the information of each block group.

Indoe bitmap (indoe table): used to record which indoe of the current file system is used and which are not used.

Block bitmap (block table): used to record which blocks are used and which are not used in the current file system.

Inode table (inode table ):Inode is used to record the attributes of a file and the number of the block where the actual data of the file is located.Each file occupies one inode, and the size of each inode is fixed to 128 bytes. Therefore, we can find that the number of files that each file system can create is related to the number of inode, and because the size of each inode is fixed, the inode must record the number of the block where the actual data of the file is located. Therefore, the size of each file is also related to inode/block.The region of the inode record block number is defined as 12 Direct, 1 indirect, 1 double indirect, and 1 three indirect.

What does it mean? These 12 blocks direct to the inode are directly used to record file data. One indirect point is that the blocks direct to the inode are not directly used to store file data, on the contrary, other block numbers are stored in this block, and the blocks corresponding to these block numbers only store real file data. If it is double indirect, these blocks are also the block numbers used to record the real data. And so on. The third indirect refers to the third-layer block or is used to record the label.

If the size of each block is 1 k, the maximum size of each file can be calculated as follows: 12 values: 12*1 k = 12 k; 1 value: assume that four bytes are used to record the block number. Therefore, a block can record 256 (1024/4) block numbers, so it can store 256*1 k = k capacity. The dual indirect is: 256*256 k, or 256*256*256 k. Therefore, the maximum file size can be 12 k + 256 k + 256*256 k + 256*256*256 k = 16G.

Block table (data block) is a collection of blocks. The size of the block supported by the Ext2 file system is 1 k, 2 k, and 4 k. During formatting, the block size and quantity are fixed.In addition, each block can store up to one file's data.Therefore, when the file size is greater than the size of a block, the file data needs to occupy multiple block numbers. When the file data is smaller than the size of a block, the remaining space of the block will not be used, and the data of other files cannot be put in. Therefore, when the files stored in our file system are small and many files, and the block is selected relatively large (such as 4 k), it may cause a waste of space. However, selecting a block of 1 K is not omnipotent. Sometimes, when a large number of blocks are required because of the large size of files, the file system may suffer from poor read/write performance.Therefore, for details about how to select block size during formatting, referThe main purpose of future file systems.

Once the file system has been formatted, how can we view the above data? We can use the dumpe2fs command to learn more about the file system information. The dumpe2fs command is used as follows: dumpe2fs [-bh] device file name. The data displayed by dumpe2fs can be divided into two parts. The first part is the information recorded by the superblock of the file system, for example, the number and size of blocks and the number of indoe. The second part is the detailed information of each block group, such as the distribution of the block group and the block usage.

When we create a directory, the Linux Ext2 file system will allocate at least one inode and at least one block to the directory.As we have already said, a directory is actually a list of file names, so the contents of the Directory are the file names under the directory. Therefore, the inode assigned to the directory by Linux records the permissions and attributes of the Directory and the number of blocks allocated to the directory. The block records the file name in the Directory and the inode number corresponding to the file name.

How is the entire process implemented when we access a file? Take/etc/passwd as an example:

(1) Find the indoe of the root directory "/" through the mount point information. Generally, the node number of the top-level directory of all file systems is 2. For example, on my system, /home is a separate partition. You can use the ls-id/home command to query that the inode number of the/home directory is 2.

(2) Find the corresponding block through the block number recorded in inode in the root directory. The block contains all the file names under the root directory, find the indoe Number of the etc/directory (provided that you have the permission to access the root directory ).

(3) check according to the corresponding permission settings in the indoe of etc/. If yes, read the block content of etc/, which contains the indoe Number of the passwd file.

(4) access the indoe of the passwd file. After checking the permission, you can read the corresponding blcok through the block number recorded in the inode, these blocks store the actual data of the passwd file.

Because Ext2 file system does not support the log function, when our file system is in a state of data inconsistency (for example, sudden power failure leads to sudden system interruption ), when the system is restarted, You need to determine whether the file system is mounted and whether the file system status is clean to determine whether to force a data consistency check. If necessary, the e2fsck program is called to perform the check. However, this check is time-consuming because the entire file system needs to be searched and compared. Therefore, you can use the log file system. The log file system will specially plan a block to record the steps for writing or revising the file. Therefore, when data inconsistency occurs, you can directly check the log record block to quickly identify the problem.In Linux, Ext3 is a file system that supports the log function.

As mentioned earlier, since the disk speed is much slower than the memory speed, after modifying the data in the memory, Linux does not immediately update the modified data in the memory to the disk,Instead, the system writes the modified memory data (set the data in the memory to dirty at this time) to the disk asynchronously. You can also use the sync command.Manually force write to diskWhen the system shuts down normally, the shutdown command will take the initiative to call the sync command to write the modified data back to the disk. If the shutdown is not normal, because the data is not written back to the disk, therefore, after the system is restarted, it may take a lot of time to check the disk, or even cause file system damage.

In addition to the Linux standard file system Ext2, Linux also supports many file systems. You can view the file systems supported by Linux in the following ways:

Ls-l/lib/modules/$ (uname-r)/kernel/fs

You can view the file system that has been loaded to the memory in the following ways:

Cat/proc/filesystems

Although Linux supports many file systems, we do not need to know what the file system on each partition is) to manage all file systems.


Summary of Linux commands in this chapter:

Dumpe2fs.





You can find a job after learning the basics of Linux Private food

You're so funny!
What should I do after graduation? If I have just graduated, I still have hope!
I don't know what you want to learn about linux! I don't know how your programming basics are! Give you some suggestions:
1. If you want to perform O & M, you are advised to first learn about the database. For example, MYQSL, Oracle, and DB2, at least the same time. You can find a job and add points.
2. If you are a programmer C/C ++, you will be very popular. If you are a java programmer, you can
3. It is best to have experience in projects, including internships. If you do not have a graduation project, you can find a few questions on the Internet, it's better to try it over thousands of times! Reading thousands of rows is better than hitting a row by hand! A thousand rows by hand is better than a single step! Thousands of lines of single-step source code are better than a single-step compilation line !" Although this sentence is intended for C/C ++ programmers, the first three statements are applicable to any program!
4. Learn more about new technologies and visit CSDN to see what skills and qualities programmers should possess!
Write so much, hope it will be useful to you!

How does laruence's Linux basic learning for Private food (the third edition) book?

It must be better for the third edition. The basic knowledge of the third edition is more detailed and new things are added. If you want to buy books, I suggest you check the electronic version on the Internet, I think it is suitable for me to buy again.

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.