Linux disk and file system management

1. Hard Disk Composition and Partitioning

        A hard disk consists of multiple disks. Each disk surface has two surfaces that can be read and written, which are read and written through the magnetic head. Each face is divided into multiple concentric circles from the inside to the outside, called tracks. Each track is divided into sectors according to a certain angle, the sector is the smallest physical storage unit, and the sector size is 512bytes. The first sector is the most important, which contains the MBR master boot record and the PT master partition table, accounting for 446 and 64bytes respectively. . The same group of tracks on the same position on each surface form a cylinder, the smallest unit of the partition.

        A maximum of 4 primary partitions and extended partitions; a maximum of one extended partition; a logical partition in an extended partition; the rest of the extended partitions can be formatted; a maximum of 11 logical partitions for SATA and 59 for IDE.


2. File system characteristics

        Three important features in the file system are super block, inode, block.

        super block: record the overall information of this file system

        inode: record the attributes of the file and the number of the block where the actual data is located

        block: record the contents of the file

        Access method: First find the inode corresponding to the file, and then take out the block number in the inode and read the content at the same time. This method is called an indexed file system. In addition to this, there is also a FAT reading method. This method has no inode, so the first block position is found first, and then the next block position is read in the block, and so on. The reading method will go back and forth in sequence. For reading, if the blocks written to the file data are scattered, the reading speed is slow at this time, and defragmentation is required, that is, the blocks belonging to the same file are merged together (why are they scattered? Because the deletion and addition of files lead to blockmaps Due to the allocation strategy). For the waste of disk space, use du -sb . to view the size of the current directory, and use du -sm . Use the number of blocks as the standard to display in MB, you can see the wasted space.


3. Ext file system

        The file system will plan the inode and block at the beginning, and generally will not change after that. However, for a large file system, there are many inodes and blocks in it, which is not easy to manage. Therefore, the file system will divide the file system into multiple block groups when formatting, and each block group has its own independent inode/block/super block system. At the same time, there is a boot sector at the front of the file system, which is used to install the boot loader and can create a multi-boot environment.

When the block is relatively large, the boot sector and the super block may be in the same block, that is, block 0. If the block is smaller, it will be in block 0 and 1 respectively. In short, the first 1024bytes of the file system is always the boot sector.

        For the block group above, it contains data block, inode table, super block, file system description, block bitmap, inode bitmap.

        data block: data block. The block sizes supported in the Ext file system are 1KB, 2KB and 4KB. Features: The size and number of blocks are determined during formatting; each block contains at most one file's data; each block A file will occupy multiple blocks; a small file will also occupy a block (resulting in waste). For the second point, if the block is too large, it will lead to waste. If it is too small, the inode will record too many block numbers, resulting in poor reading and writing of the file system.

        inodetable: The file attributes recorded by the inode are pointed to by access mode, owner and group, file size, file creation or state change time, last read time, file characteristics, and the actual content of the file. Features: The size of each inode is fixed at 128bytes; each file occupies only one inode; the number of files that the file system can create is related to the number of inodes; when the system reads a file, it must first find the inode, analyze the permissions, and then read from the inode Take the block number. For the first point, assume: the inode has 12 direct points, one indirect point, one double indirect point, one three indirect point, each block 1KB, then it can be achieved

        12+256+256*256+256*256*256(KB)=16GB

        superblock: Super block, which records information about the entire file system. Without superblock, there is no such file system. Mainly the total amount of blocks and inodes, the number of unused and used inodes/blocks, the size of blocks and inodes, the mount time of the file system, the time of the last data written and the fsck time, etc. A validbit indicates that 0 is not used. Mount 1 has been mounted. Generally speaking, a file system has only one superblock, so since each blcok group has a superblock? In fact, except for the first one, all subsequent ones are backups of the first superblock.

        file system desrciption: description of the file system, the block number describing the start and end of each blcok group,

        block bitmap block comparison table, when adding or deleting files, use this as a basis to change the bitmap to in use or not in use

        The inode bitmap is similar to the block bitmap, but this is whether the recorded inode number is used

Use sudo dumpe2fs /dev/sda2 to view the root directory file system information (if the -h parameter is added, the block group information will not be displayed):




It can be seen from the above that the block numbers occupied by Group0 are from 0 to 32767, the superblock is in block 0, and the file system description is in blocks 1 and 2.

            The block bitmap and inode bitmap are on blocks 1025 and 1041 respectively, and the inode table is on blocks 1057-1568

            Since the size of each inode is 256bytes, a total of 1568-1057+1=512 blocks are spent on the inode table, so the number of inodes is 512*4096/256=8192 inodes, there are 2258 inodes left in this group0, and the rest are 2084...


Fourth, the directory tree

        Use ls -li to view the inode number occupied by the files in the directory

        

        It can be seen that the inode numbers occupied by each file are different, respectively 129300, 129297, 129442

       The size of the viewing directory is generally a multiple of 1024, due to the size of the block

        

        Note that /proc is created in memory and does not take up disk space

        Directory: When creating a new directory, the file system will allocate at least one inode and one block. Record the permission attribute of the directory and the inode corresponding to the file and file name in the directory, respectively.

        File: When creating a new file, the file system allocates at least one inode. If the file size is 12*4K, the file size/4K blocks are allocated. Otherwise, one or more blocks are also considered to be allocated to the inode for indirect pointing.

        Directory read:

        

        For example, read the /etc/passwd file, then find the root directory with the inode number 2 of /dev/sda2 through the mount point information, and the inode has a new rx that can read the content of the block. After obtaining the blcok number, find /etc The inode number of the directory is 917505, and it is found that rx has the permission to read the block of /etc. After obtaining the block number, the inode number 919283 with the content of the passwd file is found. The inode contains the permission r, so the content of the passwd file can be read, and the relevant passwd can be retrieved. block read.

        Disk read and write: For the discrete problem of file data, you can copy the file, format the file system, and then copy the data back to solve it.


5. Ext3 journaling file system

        For the block group, the inode table and data block are called data storage areas, while others are called intermediate data, because these intermediate data will be affected with each addition, deletion and editing. In general, it can be completed successfully. However, if a power failure or a kernel error occurs when the file is written to the system, resulting in inconsistent data, the entire file system needs to be checked at the next restart, resulting in a long repair time, which leads to the rise of log files.

        When the system wants to write a file to a log file, it will first record the information that a file is going to be written to in the log record block. Then start writing the permissions and data of the file, and start updating the intermediate data. After completing the update of data and intermediate data, complete the recording of the file in the logging block.

        Asynchronous writing, if the data in memory has been changed, the data is set to Dirty at this time. You can use the sync command to manually force the write back to disk. sync is called during a graceful shutdown.


6. Mount points and VFS

        A filesystem must be linked to a directory tree before it can be used. This operation of combining a filesystem with a directory tree is called mounting. The mount point must be a directory, which is the entry point of the file system.

        

    As above, / /boot /home are all mount points, because the inode of the topmost directory of the file system is generally No. 2.

        

        Because / /. /.. are all in the same file system, and the inode numbers are the same, it means that it is the same file, that is to say, the upper-level directory of the root directory is itself


        VFS is the Virtual Filesystem Switch virtual file system, which is used to shield the differences of various file systems.


Seven, df and du

        df View disk capacity, use: df [-ahijkHTm] [directory or file name]

        -a list all filesystems, including /proc

        

        Where /dev/shm is the file system for memory virtual processing

        -k display capacity in KB; -m display capacity in MB; -h display for convenience; -H use M=1000K instead of 1024K;

        

        -T displays the filesystem name, such as ext3

        

        -i display the number of inodes instead of capacity

        

    

    du View directory capacity, use: du [-ahskm] file or directory name

    -a lists all directories and file sizes, the default is to only count directories

    

-h display for convenience; -s list totals without listing the capacity of each subdirectory; -S include the capacity under subdirectories; -k display in KB; -m display in MB



8. Link files

        Hard link, the inode number of the hard link is the same as the original file, so the content of the file can be accessed and modified by the same inode. After deleting the original file at the same time, the hard link can still access the file. However, the hard link cannot cross the file system and cannot be linked to the directory at the same time (because it is very complicated, it has not been implemented yet).

        Soft link, the Inode number of the soft link is different from the original file, but the soft link will point to the Inode number of the original file, so that it continues to rely on the original file for access and modification. At this time, if the original file is deleted, the soft link will be invalid, similar to a shortcut. Generally, soft links are used more, because there is no restriction on hard links.

        

        It can be seen that for the hard link ln passwd passwd-hd, the number of inodes used has not increased, while the soft link ln -s passwd passwd-so has increased by 1. There is no change for the capacity hard link, while the soft link has increased by 6, Because passwd is 6byte. When the original file is deleted, the hard link is still valid, but the soft link is abnormal.

        Regarding the number of file connections, it refers to how many hard links are attached to the file + 1. For directories, how many other directories point to the same directory as itself (usually . or .. works). In short The number of connections is how many duplicate inodes there are in the current file system.


Nine, disk partition

        fdisk /dev/sda to enter the partition interface.

        

        The first line is the filename and capacity of the disk. Next there are 255 heads, 63 sectors and 3916 cylinders. Then each cylinder is about 8MB in size.

        Use fdisk -l to list all the partitions in the hard disk, not only /dev/sda, for example, /dev/sdb after inserting the U disk will also be displayed

       Add and delete operations:

        delete all partitions

        

        Add a 512M primary partition 4

        

        Add an extended partition 1

        

        Add a logical partition 2048M

        

        Create a new partition sda6 , select the cylinder that has not been used in the extended partition to start, apply for a certain capacity, and then restart.

         Then format . If you use mkfs -t ext3 /dev/sda6, this will format by default. You can also use mke2fs [-b block size] [-i inode capacity] [-L volume label name] [-j] device. Where -j is Add log function.-i indicates that the file system has total capacity/inode capacity.

       Disk check : fsck [-t file system] [-ACay] device. Among them -A is to check the required device once according to the content of /etc/fstab, and it will usually be executed when booting. -a Automatically repair sectors, -y and -a Similarly, -C displays progress as a histogram

                    badblocks [-svw] device name. Where -s lists the progress on the screen, -v can see the progress on the screen, check which blocks are bad

       Disk mount : mount [-t filesystem] [-L label name] [-o option] [-n] device name mount point

                -a Mount all unmounted disks according to the configuration file /etc/fstab

                -l List currently mounted information

                -L mount using volume label

                -o Set the parameters of the mount, generally the default. One of them is -o remount is very useful

            For example, mount /dev/sda6 /mnt/sda6 will mount the newly created file system to the /mnt/sda6 directory. Valid until the next boot

            (The name of the file system supported by the system is in /etc/filesystems, the name of the file system that has been loaded by the system is in /proc/filesystems, and the file system driver is in the /lib/modules/$(uname -r)/kernel/fs directory)

       Directory mount:

                mkdir /mnt/home

                mount --bind /home /mnt/home

                This will mount /home to the /mnt/home directory.

        Disk unmount: you can use the device name umount /dev/sda6; you can also use the mount point to unmount umount /mnt/sda6; 

        Directory unmount: umount /mnt/home can only be unmounted with a mount point

       Modify the file system volume label name: e2label

                        e2label device name The new label name.

                        For example, e2label /dev/sda6 "mytest", use dumpe2fs -h /dev/sda6 to see that the file system name is changed to my_test

       hard disk test: hdparm [-Tt] device name

                    

                Test cache access performance and hard disk actual access performance separately

       Boot mount : Generally speaking, for manual mount, it will be invalid until the next boot. Therefore, it needs to be automatically mounted at boot.

                      

                In the above /etc/fstab file, the first column is the device file name of the file system, the second column is the mount point, the third column is the file system, and the fourth column is the default tree file system parameter. Can the fifth column be When used by the dump backup command, 0 means not backing up 1 means performing a dupm backup operation for each entry 2 means performing dupm backup operations irregularly, whether the sixth column is to use fsck to check sector 0, do not check 1, check 2 times at the earliest, and generally the root directory is 1 The rest are 2, and the above is the mount of /dev/sda6. /etc/fstab is the boot configuration file, and the mount of the actual file system is recorded in /etc/mtab and /proc/mounts.

        Special mounts: use loop to make large files into device mounts or swap file device mounts

                        First, dd if=/dev/zero of=/home/loopdev bs=1M count=512, which is to use the /dev/zero device to output 0 to the /home/loopdev file to make the content capacity reach 512 bs (1M) size. This way the /home directory generates a large file loopdev.

                                    Then mkfs -t ext3 /home/loopdev will format the large file, if it is a swap file, use mkswap /home/loopdev

                                    Finally, mount -o loop /home/loopdev /median/cdrom will mount the formatted device file, and use swapon to start the swap file.

                        In this way, a new partition is virtualized in the original partition

        The construction of memory swap space: similar to the above, just to modify the file system id, use t in the partition interface to change the original 82 to 82. Then restart, and then use mkswap /dev/sda7 to format the swap file.

        View the original memory swap space

         

        Add the newly created swap partition to swapon and revoke swapoff

          

        View existing swap file system

        

        Generally, a maximum of 32 swaps can be created

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325647546&siteId=291194637