Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux file system structure

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

File system structure, to understand the file system, we should start from the file storage.

Hard disk structure:

Interaction: is the interior of the disk vacuum? Yes: 1, not 2

There is no vacuum inside the disk, but the air inside is very clean. If it is a vacuum, it is not conducive to heat dissipation.

Disk-related terminology:

The interior of the hard disk is a metal disk, which divides the circular disk into several sector-shaped areas, which is called the sector. Several sectors make up the whole disk. Why is it divided into sectors? It is the need of logical data, which can better manage the hard disk space. Taking the center of the disk as the center, the disk is divided into several concentric circles, and each "line" that divides the circle is called a track.

The disk in the hard disk has two sides, both of which can store data, while there is often more than one disk in the hard disk, and there are two common ones, so the tracks in the same position in the two disks form a "cylinder", and there are as many cylinders as there are tracks in the disk. Both sides of the disk can store data, to read it, there must be a head, so each side, there is a head, a disk has two heads.

The storage capacity of the hard disk = the number of heads × the number of tracks (cylinders) x the number of sectors x bytes per sector.

Tracks are numbered sequentially from outside to inside, starting with 0, and the number of sectors on each track is determined when the hard disk is formatted.

Files are stored on the hard disk, and the smallest storage unit of the hard disk is called the "Sector". Each sector stores 512 bytes (equivalent to 0.5KB).

The older CHS (Cylinder/Head/Sector: magnetic head (Heads), cylinder (Cylinder), sector (Sector)) structure system. Because a long time ago, when the capacity of hard disk was still very small, people used a structure similar to floppy disk to produce hard disk. That is, each track of the hard disk has the same number of sectors, resulting in the so-called 3D parameters, that is, the number of heads (Heads), the number of cylinders (Cylinders), the number of sectors (Sectors) and the corresponding 3D addressing method.

Interaction: is there anything wrong with the disk structure above?

There is something wrong with this structure:

In the old-fashioned disk, the sector of each track is the same, so the whole arc length of the outer track is larger than that of the inner sector, so the magnetic recording density is smaller than that of the inner track. In the end, it leads to the waste of space on the external track.

If you check your disk design engineer, what are you going to do about it? Which of the following methods do you choose?

Method 1: the width of each track is different, so that the area of each sector is the same as possible

Method 2: no longer cut across the board, so that the number of sectors in the track can be different.

Hard drives now use this technology: ZBR (Zoned Bit Recording) location recording (Zoned zennd)

Zoned-bit recording (ZBR location record) is a method of physically optimizing the storage space of a hard disk, which obtains more storage space by placing more sectors on the external track of the disk.

Schematic diagram of ZBR disk sector structure

Interaction: read data from the outside quickly? Or faster from the inside? Li: 1 outside: 2

The disk made by using ZBR location recording method has the following characteristics: it is fast to read the data in the outer circle and slow to read the data in the inner circle, so it is normal for the test hard disk to see the curve that the reading speed is getting slower and slower.

Interaction: the C disk or Linux boot partition of the windows installation system is usually installed on the outermost or innermost disk?

Windows: C disk is the most external installation and the fastest.

Linux: boot partition and swap partition, install the outermost

When writing data on disk, first from the outside to the inside.

13.1.2 clusters and block

Clusters are similar to block in Linux systems.

Example: in the win10 system, a new text file, "New text document .txt", only enters aa two characters.

Right-click the property to view the size: indicates that the default cluster size in my NTFS file system is 4KB

[root@xuegod63 ~] # stat / etc/passwd # View Linux block size

File: "/ etc/passwd"

Size: 2053 blocks: 8 IO blocks: 4096 = 4KB normal files

13.2 File system structure

The Linux file system consists of three parts: file name, inode,block

Linux file system: ext3,ext4,xfs

Windows file system: FAT32,NTFS

13.2.1 File name:

[root@xuegod63 ~] # cp / etc/passwd a.txt

[root@xuegod63 ~] # ls a.txt # a.txt is the file name

13.2.2 contents of inode

Inode contains the meta-information of the file, specifically, the following:

Number of bytes in the file

User ID of the file owner

Group ID of the file

Read, write and execute permissions of files

There are three timestamps for a file: ctime refers to the time when the inode last changed, mtime refers to the last time the file content changed, and atime refers to the last time the file was opened.

The number of links, that is, how many file names point to this inode

* location of file data block

You can use the stat command to view the inode information of a file:

[root@xuegod63 ~] # stat a.txt

File: 'a.txt'

Size: 2053 Blocks: 8 IO Block: 4096 regular file

Device: 803h/2051d Inode: 18521833 Links: 1

Access: (0644) Uid: (0 / root) Gid: (0 / root) Access most recent visit time: 2018-05-16 14 root 55 root 36.061095050 + 0800

Last modified time of Modify: 2018-05-16 14 55 Modify 36.062095050 + 0800

Last modified time of Change: 2018-05-16 14 55 Change 36.062095050 + 0800

Birth creation time:-

[root@xuegod63 ~] # ll / etc/passwd # ll is actually to view the inode information of passwd.

-rw-r--r--. 1 root root 2053 Sep 19 2017 / etc/passwd # ll View time is ctime time

Interaction:

What is ctime? Is it the creation time? No: 1

Mtime: the time when modify time modified the contents of the file

Atime: the time when access time accesses the contents of the file

Ctime refers to the last time the file attributes of inode changed, change time. For example: chmod + x a.sh

Mtime refers to the time when the content of the file was last changed, modify time. For example: echo aa > > a.sh or vim a.sh modify content

Atime refers to the time when the file was last viewed, access time. Such as: cat a.sh

Example 2: to test the mtime time, * modify the time first, and then implant the * program to prevent find /-mtime from viewing the * file.

[root@xuegod63 ~] # stat a.txt # View time

[root@xuegod63 ~] # date-s' 13VR 42'

[root@xuegod63 ~] # vim a.txt # Writing aaaa,vim modifies mtime and atime time

[root@xuegod63 ~] # stat a.txt # View time

[root@xuegod63 ~] # chmod + x a.txt # modify ctime, sometimes * forget to modify ctime time, so you can find /-ctime to see the * file.

[root@xuegod63 ~] # stat a.txt # View time

Learn this trick in one shot: 1

13.2.3 size of inode

Inode also consumes hard disk space, so when the hard disk is formatted, the operating system automatically divides the hard disk into two areas. One is the data area, which stores file data, and the other is the inode area (inode table), which stores the information contained in inode.

The size of each inode node is typically 128byte or 256byte. The total number of inode nodes is given when formatting. Assuming that in a 1GB hard disk, the size of each inode node is 128bytes, and each 1KB is set to an inode, then the size of the inode table will reach 128MB, accounting for 12.8% of the whole hard disk.

Inode number

Each inode has a number, and the operating system uses inode numbers to identify different files.

Instead of using file names within the Unix/Linux system, inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to identify. On the surface, the user opens the file by the file name. In fact, the internal process of the system is divided into three steps: first, the system finds the inode number corresponding to the file name; secondly, it obtains the inode information through the inode number; finally, according to the inode information, it finds the block where the file data is located and reads the data.

Example 1: using the ls-I command, you can see the inode number corresponding to the file name

[root@xuegod63] # ls-I a.txt

440269 a.txt

Example 2: to see the total number of inode and the number of used hard disk partitions, you can use the df command.

[root@localhost] # df-I

Filesystem Inodes IUsed IFree IUse% Mounted on

/ dev/sda2 640848 151010 489838 24% /

Tmpfs 145579 1 145578 1 per dev/shm

/ dev/sda1 51200 38 51162 1% / boot

Note: since each file must have an inode, it is possible that the inode has been used up but the hard drive is not full. At this point, a new file cannot be created on the hard drive.

13.2.4 Catalog Files

In Unix/Linux systems, a directory is also a file. To open a directory is actually to open a directory file.

The structure of the catalog file is very simple, which is a list of a series of catalog items. Each directory entry consists of two parts: the file name of the included file and the inode number corresponding to the file name.

[root@xuegod63 ~] # ls-id / etc

8388673 / etc

For example, the ls-I command lists the entire directory file, that is, the file name and inode number:

[root@xuegod63] # ls-I / etc

13.2.5 block Block Siz

Block is the real place where data is stored.

Block is the smallest unit of storage in the file system

The sector is the smallest unit of storage on a disk

It is called block in linux and cluster in windows.

Interaction: why should there be block, can you use the sector directly?

When the operating system reads the hard disk, it will not read it one by one sector (512 bytes), which is too inefficient, but read multiple sectors successively at one time, that is, read a "block" at one time. This "block", which consists of multiple sectors, is the smallest unit of file access. The most common size of a "block" is 1KB, that is, two sector sectors to form a block. Or 4K.

Scenario: if there is no block? What's gonna happen?

In the dead of night, I played a 3.6g cang.avi movie, read 512B at a time, addressed too many times, too slow. no, no, no.

As a result. You know...?

Result: avi is regarded as jpg

Example 1: format modify disk and modify cluster size

Actual combat: cluster and block size settin

Do you prefer bigger or smaller?

Cluster and block scaling up:

Advantages: high speed, saving addressing time, disadvantages: waste of space

For example: 2T hard disk, the first 1.5T, using 4K, the remaining 500g formatted into 64K clusters. Trade space for time

Example 2: view Linux system block size

[root@xuegod63 ~] # stat / etc/passwd | grep IO

Size: 2053 blocks: 8 IO blocks: 4096 ordinary files

# block to 4K

Summary:

The structure of hard disk: ZBR location record

Inode (the inode number is mainly seen in the inode table)

The inode number uniquely identifies a file (in a file system)

When the inode is used up, the file cannot be created.

The inode data volume setting is larger: multiple files can be created. Take up a lot of space

The inode data volume setting is smaller: very few files can be created. The space occupied is relatively small.

Block

Large block setting: high efficiency and low utilization.

Small block setting: low efficiency and high utilization.

13.3 hard links and soft links of files

13.3.1 Linux Link concept

There are two kinds of Linux links, one is called hard link (Hard Link), the other is called soft link, namely symbolic link (Symbolic Link). By default, the ln command produces hard links.

[hard connection]: a hard connection refers to a connection through an index node number. Inode can correspond to multiple file names.

In Linux's file system, files saved in disk partitions, regardless of type, are assigned a number called index node number (Inode Index).

In Linux, multiple file names can point to the same Inode. Generally speaking, this kind of connection is a hard connection.

The purpose of a hard connection is to allow a file to have multiple valid pathnames, so that users can establish a hard connection to important files to prevent "accidental deletion".

Deleting only one connection does not affect the Inode itself and other connections. Only when the last connection is deleted will the connection of the file's data block and directory be released. In other words, the condition for a file to be really deleted is that all hard-connected files associated with it are deleted.

[soft connection]: another kind of connection is called symbolic connection (Symbolic Link), also called soft connection. Soft-link files have shortcuts similar to Windows. It is actually a special file. In a symbolic link, a file is actually a text file that contains the location information of another file

13.3.2 practice-1: the ln command creates hard links

Syntax format: ln source file target file

[root@xuegod63 ~] # echo 1111 > a.txt

The principle of hard link is that multiple file names point to the same inode, so multiple file names share an inode number to achieve the purpose of sharing and backup.

Note: the deletion of the source file does not affect the normal use of the linked file

Hard links cannot be created for directories

Hard links cannot be created across partitions

The characteristics of hard links: can not be targeted at directories, cross-partition can not be achieved. Because each partition has its own independent INDOE number

Interaction: why is the number of links in a directory that has just been created 2?

[root@xuegod63 ~] # mkdir test

[root@xuegod63] # ll-d test/

Drwxr-xr-x 2 root root 6 May 16 15:55 test/

The default is a new empty directory, and the second field of this directory is 2 (contains two hidden directories, because each directory has a subdirectory that points to itself. " And points to the subdirectory ".." of its parent directory, so test is a link that hides the directory. Is the second link.

[root@xuegod63 ~] # ll-id test/ # two inode numbers are the same

2453723 drwxr-xr-x 2 root root 6 May 16 15:55 test/

[root@xuegod63 ~] # ll-id test/.

2453723 drwxr-xr-x 2 root root 6 May 16 15:55 test/.

13.3.3 ln-s create a soft connection

Soft links: equivalent to shortcuts in windows

Syntax: the name of the ln-s source file soft link

Example:

[root@xuegod63 ~] # cp / etc/passwd a.txt

[root@xuegod63] # ln-s a.txt a-link.txt

[root@xuegod63 ~] # ll a-link.txt

Lrwxrwxrwx 1 root root May 16 16:10 a-link.txt-> a.txt

[root@xuegod63 ~] # rm-rf a.txt

[root@xuegod63 ~] # ll a-link.txt

Lrwxrwxrwx 1 root root May 16 16:10 a-link.txt-> a.txt

Note: the source file is deleted and the linked file is invalid

Example 2: ability to create soft links for directories and across partitions

[root@xuegod63] # ln-s / boot/grub grub-link

Summary:

13.1 hard disk structure

13.2 File system structure

13.2 hard and soft links

13.4 practice: solve the problem that the disk has space but cannot create files-repair the server file system

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report