Operation Realization of Linux File System

  • 2021-07-18 09:32:44
  • OfStack

This book note comparison mainly records the related operations of the file system.

Disk and directory capacity

The whole data of the disk is in the superblock block, but the capacity of each individual file is recorded in inode. It is often used to show the two commands of disk usage

df: List the overall disk usage of the file system

du: Evaluating disk usage for file systems (commonly used to evaluate directory capacity)


$ df [-ahikHTm] [ Directory or file name ]
 Parameters: 
-a:  List all file systems, including system-specific /proc(/proc Mounted in memory and does not take up disk space ) And other file systems; 
-k:  With KB Displays the capacity of each file system 
-m:  With MB Displays the capacity of each file system 
-h In a way that is easier for people to read GB , MB , KB Self-display in equal format 
-H:  With M=1000K  Replace M=1024K  Carry mode of 
-T:  It is listed along with the file system name of the partition (for example ext3 ) 
-i:  Instead of hard disk capacity, use inode Quantity display of 

The main data read by df is almost all for the whole file system, so the reading range is only the information in super block, so this command displays the results very quickly.


$ du [-ahskm]  File or directory name 
 Parameters: 
-a  List all files and directory capacity, because by default, only the number of files in the directory is counted 
-h  Display in a capacity that is easier for people to read 
-s  List the total amount, but not the capacity occupied by each individual directory 
-S  Excluding totals under subdirectories 
-k  With KB List capacity display 
-m  With MB List capacity display 

du looks up all the file data of the file system directly

Connection file: ln

There are two kinds of connection files under linux: One is a shortcut similar to that on Windows, which allows you to quickly connect to the target file or directory. The other one is to generate new file names instead of new files through inode connection of file system, which is called hard link (hard link).

hard link (hard and physical)

Each file occupies 1 inode, and the contents of the file are pointed to by the records of inode. To read a file, you must go through the directory record file name to point to the correct inode number to read it. The file name is related to the directory, while the file content is related to inode, and hard link is a new file name connected to an inode number in a directory.

vagrant@vagrant-ubuntu-trusty-64:~$ cd /tmp
vagrant@vagrant-ubuntu-trusty-64:/tmp$ touch tes
vagrant@vagrant-ubuntu-trusty-64:/tmp$ ln test test1
vagrant@vagrant-ubuntu-trusty-64:/tmp$ ll -i test test1
62273 -rw-rw-r-- 2 vagrant vagrant 0 Dec 17 12:39 test
62273 -rw-rw-r-- 2 vagrant vagrant 0 Dec 17 12:39 test1

You can see that the inode number of the two files is 1, their file permission attribute is completely 1, and the number of connections becomes 2.

The biggest advantage of hard connection is security. If you delete any 1 file name, inode and block both exist. At this time, you can read the correct file data through another file name. No matter which file name you use to edit, the final result will be written to the same inode and block, so you can modify the data.

1 Generally speaking, When you set up a connection file using hard link, The disk space and the number of inode will not change. hard link only writes one more associated data in block in a certain directory, which will neither increase inode nor consume the number of block (unless the disk is full when you add one more associated data in block, you may need to add one more block to record the file name association, which will lead to the change of disk space).

Restrictions on hard connections:

Cannot cross file systems Cannot connect to a directory, because if you use hard link to connect to a directory, the connected data needs to be connected with all the data under the connected directory, which will cause considerable complexity in the environment, so it is not supported for directories for the time being

symbolic link (Symbolic Connection, or Shortcut)

symbolic link is to create an independent file, and this file will make the data read point to the file name of the file it links to. Because only the file is used as the pointing operation, when the source file is deleted, the file of symbolic link will not find the source file and cannot be opened.


vagrant@vagrant-ubuntu-trusty-64:/tmp$ ln -s test test2
vagrant@vagrant-ubuntu-trusty-64:/tmp$ ll -i test test2
62273 -rw-rw-r-- 2 vagrant vagrant 0 Dec 17 12:39 test
62275 lrwxrwxrwx 1 vagrant vagrant 4 Dec 17 13:07 test2 -> test

The two files point to different inode numbers. The important content of the connection file is that it will write the file name of the target file. Because the file to the right of the arrow is 4 Byte, the size of the connection file is 4 byte.

The file created by symbolic link is a separate new file, so it will occupy inode and block.

When you modify the symbolic link file, it changes to the source file.


$ ln [-sf]  Source file   Object file 
 Parameters: 
-s:  If you connect without adding any parameters, that is hard link As for -s Is symbolic link
-f:  If the object file exists, take the initiative to delete the object file directly and rebuild it. 

About the number of connections to the directory

When connecting files with hard link, the number of connections to files will increase by 1. When we create an empty directory, because there are two directories:. and.., when we create an empty directory/tmp/testing, there will basically be three things:
/tmp/testing
/tmp/testing/.
/tmp/testing/..
Where the/tmp/testing and/tmp/testing/. both represent this directory, wherea/tmp/testing/.. represents the/tmp directory, so when we create a new directory, the number of new directory connections is 2, while the number of connections in the upper directory increases by 1.

Partitioning, formatting, verification and mounting of disks

If you want to add 1 hard disk to the system, you need to do the following actions:

Partition the disk to create a new available partition Format the partition to create a file system available to the system. If you want to be careful, you can check the newly created file system just now. On an linux system, you need to create a mount point (that is, a directory) and mount it.

Disk Partition: fdisk


$ fdisk [-l]  Device name 
 Parameters: 
-l:  Output all partition contents of the following device. If only fdisk -f The system will list the partitions of devices that the whole system can find. 

#  For example: 
#  Find the disk file name first 
vagrant@vagrant-ubuntu-trusty-64:/tmp$ df /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdc1 41251136 3631948 35883276 10% /
#  Input fdisk But don't add numbers 
vagrant@vagrant-ubuntu-trusty-64:/tmp$ fdisk /dev/hdc
 It prompts Command(m for help):  Input m You can see the relevant parameter prompt 
d  Representative deletion 1 Partitions 
n  On behalf of new 1 Partitions 
p  Represents that the partition table is displayed on the screen 
q  Means do not store, leave fdisk Program 
w  Represents writing the operation just now to the partition table 

Pay special attention to q and w. As long as you press q when leaving fdisk, all operations will not take effect. On the contrary, pressing w means that the operation will take effect.

Delete a disk partition

To delete a partition (for example, delete all partitions/dev/hdc), you need to do the following:

fdisk/dev/hdc: Enter the fdisk interface first p: Let's look at the partition information first. Suppose you want to delete the/dev/hdc1 d: To select 1 partition at this time, select 1 w or q: w can be stored to disk data tables and away from fdisk; If you regret it, press q directly to cancel the deletion just now.

New disk partition

Need to do 1 action:

fdisk/dev/hdc: Enter the fdisk interface first n: New partition p or e or l: Select a different partition type, where p represents a primary partition, e represents an extended partition, and l represents a logical partition 1-4: Partition number, optional 1-4. If it is a logical partition, you do not need to enter the partition number Enter the end cylinder number. If it is too much trouble to calculate the cylinder/partition size by yourself, you can use a form like "+ 512M" to ask the system to assign us the cylinder number closest to 512M p: View partition information w or q: w can be stored to disk data tables and away from fdisk; If you regret it, press q directly to cancel the deletion just now.

For the form of partition created (primary partition/extended partition/logical partition) and the size of partition, 1 generally speaking, the form of new partition will have the following situations:

There are numbers 1-4 left, and the system has no extended partition: the option for you to select Primary/Extended will appear, and you can specify the number between 1-4 1-4 are still left, and the system has extended partition: at this time, there will be an option for you to select Primary/Logical. If you choose p, you need to specify the number between 1 and 4; If you select l, you do not need to set the number, because the system will automatically specify the file name number of the logical partition 1-4 No remaining, and the system has extended partition: at this time, you will not be allowed to choose the partition type, but will directly enter the partition form of logical

1 Partition is needed to restart (reboot) to update the kernel partition table information, you can use "partprobe" to force the kernel to find the partition table again


Related articles: