Linux File System

1. Preface

The basic idea of Linux is that everything is a file; Each file has a defined purpose. The first is that everything in the system comes down to a single file, including commands, hardware and software devices, operating systems, processes, etc. For the operating system kernel, they are considered to have their own characteristics or types of files. As far as Linux is Unix-based, it's also largely because the basic ideas of both are very similar.

2. Deep understanding of the Linux file system

2.1 inode and block Details

(1) Overview of inode and block

Files are stored on hard disks, where the smallest unit of storage is called a sector, where each sector stores 512 bytes. Typically, eight consecutive sectors form a "block", one of which is 4K in size and is the smallest unit of file access. When the operating system reads a hard disk, it reads multiple sectors in a row, one block at a time.
File data includes actual data and meta-information. File data is stored in "blocks". The area where file meta-information (such as the creator of the file, creation date, file size, file permissions, and so on) is stored is called inode. Therefore, a file must occupy an inode and at least one block. The inode does not contain a file name, which is stored in the directory. Everything on Linux is a file, so the directory is also a file.
_Each inode has a number, and the operating system uses the inode number to identify different files. Inside the Linux system, inode numbers are used to identify files. For the system, the file name is just an alias for the inode number, and the file name and the inode number are a one-to-one correspondence, one file name for each inode number. So when a user tries to access a file in a Linux system, the system first looks up the corresponding inode number according to the file name, obtains the inode information through the inode number, and then looks at the inode information to see if the user has access to the file, points to the corresponding data block and reads the data.

(2) inode content

The inode contains meta-information about the file, specifically the following:

  1. Bytes of file
  2. User ID of the file owner
  3. Group ID of the file
  4. Read, write, execute permissions for files
  5. There are three timestamps for the file:
Abbreviations)Full NameChinese NameMeaning
atimeaccessed timeAccess timeLast time the file was accessed
mtimemodified timeModification TimeThe last time the contents of the file were modified
ctimechange timeChange timeWhen the metadata of the file changed

6. Number of links, the number of filenames pointing to this inode

7. Location of file data blocks

(3) inode number for viewing files

There are two ways to view the inode number for a file name:

  1. Ls-i file name
  2. stat file name

(4) Size of inode

Inode also consumes hard disk space, so when formatted, the operating system automatically divides the hard disk into two zones. One is the data area, where file data is stored. The other is the inode zone, which stores the information contained in the inode. The size of each inode is generally 128 bytes or 256 bytes. Assuming that in a 1GB hard disk, each inode node is 128 bytes in size and an inode is set per KB, the size of the inode table will reach 128MB, which is 12.8% of the total hard disk.
_Normally, you don't need to focus on the size of a single inode, but on the total number of inodes. The total number of inodes is given when formatting. Execute the "df-i" command to see the total number of inodes corresponding to each hard disk partition and the number of inodes that have been used.

[root@c7-1 ~]#df -i
 file system          Inode Used(I)  available(I) Used(I)% mount point
devtmpfs         479977     406   479571       1% /dev
tmpfs            482670       1   482669       1% /dev/shm
tmpfs            482670    1260   481410       1% /run
tmpfs            482670      16   482654       1% /sys/fs/cgroup
/dev/sda2      26214400   59040 26155360       1% /
/dev/sda5      20971520       3 20971517       1% /data
/dev/sda1       1048576     328  1048248       1% /boot
tmpfs            482670       1   482669       1% /run/user/0

Because the inode number is separated from the file name, Linux systems have the following unique phenomena:

  1. The file name cannot be deleted properly when it contains special characters. At this point, deleting inode directly can delete files
  2. When moving or renaming a file, change the file name without affecting the inode number
  3. After opening the file, the system recognizes it as an inode number, regardless of its name
  4. When file data is modified and saved, a new inode number is generated
    [root@c7-1 ~]#touch test1
    [root@c7-1 ~]#ls -i
    100764458 test
    [root@c7-1 ~]#find ./ -inum 100764458 -exec rm -i {} \;
    rm: Whether to delete normal empty files "./test1"?y
    [root@c7-1 ~]#ls
    [root@c7-1 ~]#
    

(5) Recover Misdeleted ext format files

#extundelete is an open source Linux data recovery tool that supports ext3, ext4 file systems. (ext4 can only be restored in centos6, ext3 in centos7)

Add a hard disk
echo "- - -" > /sys/class/scsi_host/host0/scan
echo -e "n\np\n1\n\n\nw\n" | fdisk /dev/sdb
partprobe /dev/sdb
mkfs.ext3 /dev/sdb1
mkdir /test
mount /dev/sdb1 /test/
df -Th
#Install Dependent Packages
yum -y install e2fsprogs-devel e2fsprogs-libs gcc gcc-c++ make wget
#Compile and install extundelete 
cd /test
wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
tar jxvf extundelete-0.2.4.tar.bz2
cd extundelete-0.2.4/
./configure --prefix=/usr/local/extundelete && make && make install
ls /usr/local/extundelete/bin/extundelete
ln -s /usr/local/extundelete/bin/* /usr/bin/
#Simulate deletion and perform recovery
cd /test
echo a>a
echo a>b
echo a>c
echo a>d
ectundelete /dev/sdb1 --inode 2	#To see which files exist under file system/dev/sdb1, the inode node starts with 2, which represents the directory where the file system started
rm -rf /test/*
cd
umount /test/
df -Th
extundelete /dev/sdb1 --restore-all	#Restore all data under /dev/sdb1, a RECOVERED_appears in the current directory FILES directory, where recovered files are saved
ls RECOVERED_FILES/

2.2 Soft and Hard Links

(1) Concepts

Role: Link files to files or directories

Link File Classification:

 

(2) Command Management - ln

Format: ln [parameter] [source file or directory] [target file or directory]

 

Keywords: Linux vim

Added by rotwyla98 on Sun, 19 Dec 2021 02:45:53 +0200