linux disk, partition, partition management, swap

disk

The magnetic disk is composed of multiple disks. The front and back sides of each disk are readable and writable. The magnetic particles on the surface of the disk are changed by the magnetic head to complete reading and writing.
When the disk leaves the factory, there are many divided concentric rings on the disk. The data is stored in the magnetic particles on the ring, and such a ring is the track.
Each disk has many tracks that are invisible to the naked eye. The tracks are divided into multiple sectors, and each sector can store 512 bytes.
Cylinder is an abstract concept. Tracks in the same vertical area of multiple disks are called cylinder. Data is stored in sectors on tracks with the same radius. When reading and writing, the seek time of magnetic head can be reduced
A magnetic head is a metal block on a disk track that is responsible for reading and writing

Disk naming

The disk on the physical machine is named sda (a is a disk, and the subsequent disks are divided backward according to letters, such as SDB and sdc). The disk will be followed by the numbers 1, 2 and 3. The partition divided for this disk, such as sdb2, is the 2 partition of the second disk.
The naming method of virtual server is basically the same as that of physical machine, but the s of sda is replaced by v, that is, vda.

Partition management

Partition is mainly used to store data by category and facilitate management. There are two methods of partition: MBR and GPT. It can be mounted normally only after partition and formatting the corresponding partition.
MBR can be divided into up to 4 primary partitions, or 3 primary partitions, and extended partitions under partition 4.

fdisk /dev/sdb
n Add a new partition
p View current partition
d delete a partition
w preservation
m Show help
q sign out

GPT supports the allocation of 128 primary partitions

gdisk /dev/sdb
#The command is basically the same as fdisk. But here
? Show help

mkfs format partition can be operated on the whole disk or a single partition. Generally, do not format the whole disk directly (all partitions will be out of format). Format after partition, and exchange space will be reserved in the header

#ex2, ext3, ext4. The disk format is the specified type
mkfs.ext4 /dev/sdb

#xfs formats the partition to the specified type
mkfs.xfs  /dev/sdb

#Specify the size of a data block 4096 bytes = 4k
mkfs.xfs -b size=1024 /dev/sdb2

#Force format
mkfs -f   /dev/sdb

mount

Temporary mount

mount /dev/sdb1 /test

Mount the partition entry to the test directory, and then mount all storage, reading and other operations under the directory, which is equivalent to

sdb1 Inside
-t Specifies the file system mount partition
-a Check and mount/etc/fstab Unmounted devices in configuration file
-o Specify mount parameters  ro Read only, rw Reading and writing

uninstall

umount /dev/sdb1  
#Unmount this section

These are temporary operations. You can change the contents of the file in etc/fstab for configuration

#
# /etc/fstab
# Created by anaconda on Mon Jul  5 20:00:37 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=7f59131b-e148-4e00-a283-564047f809d7 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0


/dev/sdb1               data                    xfs     defaults        0 0  
#The first column is the equipment to be mounted
#Second column mount entry directory
#Column 3 partition type   
#Column 4 default mount parameters
#async/sync stores data asynchronously or synchronously. Asynchronous is the default
#Whether user/nouser allows ordinary users to mount using the mount command. The default is unmount
#exec/noexec whether to allow executable files to execute. The default is exec
#suid/nosuid whether suid files are allowed. The default is suid
#auto/noauto whether the file system is mounted when the mount -a command is executed. The default is auto
#Whether rw/ro is mounted in read-only or read-write mode. The default is rw
#defaults has rw, suid, exec, dev, auto, nouser, async
#The fifth column whether to back up every day 0 no backup 1 daily backup 2 irregular backup
#Whether the sixth column checks the disk 0 every time it is powered on. Do not check 1. Check 2. Double validation

Copy according to the above configuration
After writing, mount -a test can be used to check whether the written content is wrong

#Check the etc/fstab file and mount all partitions that can be mounted
mount -a 

swap

swap virtual memory can be used when the physical memory module is insufficient (when the physical memory module is insufficient, it will randomly kill an ongoing process to free memory, and when the memory overflows, various commands may not be used normally, and the system will report an oom exception). swap is used as memory, but this method is very slow. After all, disk is used to flush the memory.

mkswap /dev/sdb1
#Format as swap partition

free -m 
#View swap partition size

swapon /dev/sdb1
#Expand swap partition
swapon -a
#Add all swap partitions
swapoff /dev/sdb1
#Close a swap partition
swapoff -a 
#Close all swap partitions

inode and block

In order to simplify the use of disk, the operating system provides a secondary system FS (file system)
By default, the disk is divided into two storage areas. One is the Inode for storing metadata, each Inode block block is 128bit, and the other is the block for storing real data, each block occupies 4kb. When the data written in the block is marked for use, the Inode block is associated with and records the file corresponding to the block and the location of the block in the disk.
If most of the files stored in the disk are small files, which occupy a small space, but the number of files is large, it may lead to obvious memory, but it is unable to create a folder, report an error, and the capacity is insufficient. This is that the Inode number has been occupied, and there is no available Inode number for new files. Therefore, in order to prevent this problem, you can adjust the number of inodes before creating partitions

During work, you may encounter the problem of disk damage, which can be used

xfs_repair /dev/sdb1    
#Repair the disk, but it may not be possible to repair it
-L When the repair fails, you can try forced repair, but forced repair may cause partial data loss, which is not recommended

Keywords: Linux

Added by dotBz on Fri, 14 Jan 2022 15:21:36 +0200