What are disk sectors, IO blocks, inodes, files, directories, hard links and soft links?

Make a note of your own understanding.

1, the sector

A sector is a physical partition on a disk, something that actually exists and can be felt.

Let’s take a look at how to view some information about a disk, using the fdisk -l command. I have several disks here, but I only took a screenshot of one disk shown here, as shown in the figure, and the disk I took was /dev/sda.

As shown in the red box, Unit means Unit and sector means sector. The storage unit of a disk is a sector.

You can also see from the first row that the disk is 599.6GB in size and has a total of 11,1062784 sectors.

You can see from the second line that each sector has a size of 512bytes.

You can see from the third line that the logical size of the sector is 512bytes and the physical size is 512bytes.

According to the help in the figure above, the logical sector size can be changed from 512bytes to 4096bytes. (Sector sizes vary from system to system)

What are the physical parts of a disk besides sectors?Image source:Blog.csdn.net/william_mun…

As shown in the figure, the disk is made up of a group of disks, each with a certain gap between the disks, connected by a spindle.

A disk is divided into equally spaced circular areas called tracks, each of which is divided by the same amount of space. The areas divided are called sectors (they look like sectors, so they are called sectors).

Each disk has a magnetic head for addressing (moving around the disk to find areas of data storage) and reading and writing data.

The same track on different disks forms a cylinder, called a cylinder, but this cylinder is not a real physical part.

To summarize their relationship:

A disk can have multiple disks. Each disk corresponds to a head. A disk can be divided into multiple tracks, and a track can be divided into multiple sectors.

The total disk size is: Number of disks (or heads) x Number of tracks x Number of sectors per track x Sector size

2. I/O block (disk block)

When we talk about a disk block, we might think of it as a physical partition of the disk, but it is not.

A sector is the smallest unit of disk storage. Go to big say, still have track, disc and so on.

If a disk block is not a physical partition of a disk, what is it?

We may have a common sense perception, is disk IO speed is very slow, if at the time of reading data, to read a sector data, first sent to the upper layers, and return to read the next sector, then sent to the upper layers, so repeatedly, the upper application requires frequent interaction with the disk, it’s a waste of time.

Therefore, the concept of a disk block is introduced. It logically regards multiple consecutive sectors as a whole, and then reads data from all sectors in the logical disk block for upper-layer applications at a time.

So we need to distinguish between sectors and disk blocks that correspond to different objects:

The sector corresponds to the hardware level, which is the partition of the disk surface, and is a real physical component.

The disk block corresponds to the software layer, which logically treats multiple contiguous sectors as a whole.

For example, if the disk sector size is 512 bytes and the block size is 4096 bytes, each block records eight consecutive sectors. Data is not stored in blocks, but in sectors, and blocks keep track of which sectors are their own.

How to check the size of an IO block:

1) under the root user, perform the tune2fs – l/dev/sda | grep “Block Size”

2) or use sudo tune2fs – l/dev/sda | grep “Block Size” 3, the inode

Each file has data and metadata, the data is the content of the file, it is stored in the data area of the disk;

The file metadata includes the following items:

File size, file block information, size of a block, file type (common file, directory or symbolic link), device number, index node number, number of hard links, file access time, file content modification time, and file attribute modification timeEach file has an inode, and each inode has a unique inode number. How do I check the inode number of a file?

1) You can view this by using stat filename

2) Or switch to the directory where the file resides and run the ll -i command to view the file inode number in the first column.

Each inode has a unique number. This number is limited on each disk. Once the inode number is assigned, no new files can be created.

Since files need a unique inode and each inode needs a unique number, which is finite, no inodes can be created and no files can be created.

Sometimes we find ourselves trying to create a new file, but we can’t.

So we need to identify the problem. How do we identify the problem?

Use df-hl to check whether the disk usage is full. If the disk usage reaches 100%, the disk is full and we need to delete something to free up storage space.

But what if we check the disk usage and find that instead of the disk being full, the disk has a lot of free space?

As we mentioned above, we can see whether the inode number is used up.

Use df -i to check disk inode usage. If the disk inode usage reaches 100%, delete some files to release the occupied inode number.

Does anyone have any questions? Why is there so much disk space and the inode numbers run out?

Inode numbers are limited. Disk space is also limited. But if the file system is dominated by small files, each file takes up a little bit of space. It is highly likely that the above inode numbers will run out, but there will be plenty of disk space left.

4. Files and directories

In Linux, everything is a file.

How do you tell if it’s a file or a directory in Linux?

1) Use the ll command to determine: if it starts with D, it is a directory (d is short for directory); if it starts with -, it is a common file.2) Run the stat command to check whether: 5. Hard and soft links

When we create a file, the file name is actually a hard link, a file has at least one hard link, but a hard link can not point to more than one file.

A file system can find the inode number through a hard link, and then find the inode. By obtaining the file metadata stored in the inode, the file data stored in the disk can be read.

How to check the number of hard links in a file?

1) Check by ll:

The second column is the number of hard linksHave you noticed that the number of hard links in an ordinary file is different from the number of hard links in a directory? An ordinary file has only one hard link, while a directory has two. Why? For directory files, in addition to the inode can be obtained by the directory name, it can also be obtained by the.(dot), because the dot represents the current directory. So we see that the number of hard links in the directory is 2.

2) You can also check the number of hard links in the file by using stat: What are soft links? A soft link is an independent file (classified as a symbolic link) with its own inode, which stores information about the file it points to. Because the soft link is separate from the file it points to, deleting the soft link does not affect the file it points to.

Run the ln -s 123 softlink123 command to set up a softlink to file 123Stat Softlink123 shows that this file is a symbolic link, that is, a softlink. It has its own inode, so it is an independent file. Since its inode stores information about the file to which it points, softlink123 can also read the data of file 123You can see that the contents of the file read from the hard link and soft link are the same