Linux-based disk management and file system operations

1. Disk Management

1. Disk structure

1) Physical structure
Disc: The hard disk has multiple discs, each with 2 sides
Magnetic head: One head on each side
Insert picture description here
2) Data structure

  • Sector: The disc is divided into multiple sectors, each sector stores 512 bytes of data, the smallest storage unit of the hard disk
  • Track: Concentric circles with different radii on the same disc are circular tracks drawn on the disc surface by the magnetic head
  • Cylindrical surface: a cylindrical surface composed of different discs with the same radius, composed of multiple tracks with the same radius

Cylinders, heads, and sectors can be used to uniquely locate each area on the disk

3) Hard disk storage capacity
Capacity = number of heads × number of tracks (cylinders) × number of sectors per track × number of bytes per sector (512 bytes)

4) Disk interface type (understand)
IDE: The parallel port data cable connects the motherboard and the hard disk. The anti-interference is too poor, and the cable takes up a large space, which is not good for the internal heat dissipation of the computer. It has been gradually replaced by SATA.
SATA: strong anti-interference, support hot swap and other functions, fast speed, strong error correction ability.
SCSI: Minicomputer system interface. SCSI hard disks are widely used by workstation-class personal computers and servers. The CPU occupancy rate is low during data transmission, the speed is fast, and it supports hot swap.
SAS: It is a new generation of SCSI technology, which is the same as SATA hard disks. It adopts serial technology to obtain higher transmission speed, which can reach 6Gb/s.
Fibre Channel: Fibre Channel disk greatly improves the communication speed of a multi-disk system. Hot plugging, high broadband, remote connections, and a large number of connected devices are the main characteristics of Fibre Channel.

2. MBR and disk partition

1) Master boot area (MBR: Master Boot Record)

  • MBR is located at the first physical sector of the hard disk
  • MBR contains the hard disk's master boot program and hard disk partition table
  • The MBR has a total of 512 bytes, the first 466 bytes are the master boot record, and the partition table is stored in the 477-510 bytes in the MBR sector
  • The partition table has 4 partition record areas, each of which occupies 16 bytes
  • In Linux, devices such as hard disks and partitions are all represented as
    sdb5 in the file /dev/sdb5.
    sd stands for SCSI devices (commonly used). hd stands for IDE devices
    . The sequence number of the hard disk. The letters a, b, c... represent
    the sequence number of the partition. Expressed by numbers 1, 2, 3... The primary partition starts from 1-4, and the first logical partition always starts from 5.

2) Disk partition structure

  • The number of primary partitions in the hard disk is only 4
  • The serial number of primary partition and extended partition is limited to 1~4
  • Extended partition is divided into logical partition
  • The serial number of the logical partition will always start from 5
  • The master boot record (MBR) disk partition supports a maximum volume of 2.2TB, more than 2.2T will use patede; each disk has up to 4 primary partitions, or 3 primary partitions, 1 extended partition, and multiple partitions in the extended partition Logical partitions).
    Insert picture description here

Second, the file system

1. File system type

1) XFS file system

  • Partition to store file and directory data
  • The high-performance journal file system is particularly good at handling large files and can support millions of terabytes of storage space.
  • The file system used by default in CentOS 7

2) Exchange file system-----SWAP

  • Create swap partition for Linux system
  • Generally set to 1.5~2 times the physical memory

3) Other file system types supported by Linux:
EXT4, FAT32, NTFS, LVM

2. View and manage disk partitions-----fdisk

fdisk -l [磁盘设备]
fdisk [磁盘设备]

Insert picture description here

View the disk partition
fdisk -l [disk device]
Device: the name of the device file of the partition.
Boot: Whether it is a boot partition. If it is, there is a "*" mark.
Start: The starting position (number of cylinders) of the partition in the hard disk.
End: The end position (number of cylinders) of the partition in the hard disk.
Blocks: The size of the partition, in Blocks (block), the default block size is 1024 bytes.
Id: System ID number corresponding to the partition. For example, 83 represents the XFS partition or EXT4 partition in Linux, and 8e represents the LVM logical volume. The swap partition is 82
System: partition type.

管理磁盘分区
fdisk /dev/sdb
交互模式中的常用指令:
m:获取帮助菜单
n:新建分区
p:查看分区情况
d:删除分区
t:变更分区的类型
w:保存分区操作并退出
q:不保存分区操作并退出

创建分区步骤
n
	--> p 主分区、e 扩展分区、l 逻辑分区
		--> 设置分区:1  (范围1-4,如按 Enter 键接受默认值)
			--> 设置柱面序列:直接按 Enter 键接受默认值
				--> 设置分区大小:+20G  (指定大小为 20GB,如按 Enter 键接受默认值表示所有空间)
					--w
变更硬盘(特别是正在使用的硬盘)的分区设置以后,建议最好将系统重启一次,或者执行“partprobe”命令使操作系统检测新的分区表情况。以防格式化分区时损坏硬盘中已有的数据。

I have built 1 partition here, so the default is to start from 2, follow the prompts, and don’t forget to save at the end.
Insert picture description here

3. Create a file system (format) ----- mkfs

命令格式:mkfs -t 文件系统类型 分区设备
        mkfs.文件系统类型 分区设备
示例:mkfs -t xfs /dev/sdb1 
     mkfs.xfs /dev/sdb1 

Here I have built 2 partitions, starting from the first partition sdb1, here I have created a file system, if you want to create it again, you need -f coverage, here is the picture to understand the command format, sdb2 is created using the second method, and the creation is successful The file system information will be displayed.
Insert picture description here

4. Create a swap file system-------mkswap

命令格式:mkswap 分区设备

创建交换文件系统
创建swap之前,目标分区应先通过fdisk 工具将分区类型ID 号设为 82
fdisk /dev/sdb
-->t
	-->5
		-->82

mkswap /dev/sdb5
swapon /dev/sdb5	#启用新增加的交换分区
swapoff /dev/sdb5	#停用指定的交换分区
swapon -s 			#查看每个分区的swap状态信息
free -m				#查看总的swap状态信息

First change the ID number to 82.
Insert picture description here
The following two are the comparison before and after the modification. Pay attention to the following ID and System changes
Insert picture description here

Insert picture description here
Create, enable and disable swap partitions
Insert picture description here

5. Mount and unmount the file system------mount, umount

挂载前先格式化
挂载文件系统、ISO镜像到指定文件夹
mount  [-t 类型]  存储设备  挂载点目录
mount  -o loop  ISO镜像文件  挂载点目录
-t:用于指定文件系统类型,通常可以省略,由系统自动识别
-o:挂载参数列表,以英文逗号分隔;如用来描述特殊设备,用loop指定

卸载已挂载的文件系统
卸载前提:挂载的设备或者目录没有被在使用中,要先退出挂载目录
umount [-lf] 存储设备目录或者挂载点目录
-l 表示解除正在繁忙的文件系统
-f 表示强制

查看磁盘使用情况
1.直接 mount 命令

2. df [选项]
-h:显示分区的容量单位
-T:显示文件系统的类型
-i:显示分区的inode号码数量

Mount sdb1 to the opt directory
Insert picture description here
. There are two ways to unmount the mounted file system:
umount /dev/sdb1
umount /opt
Insert picture description here

6. Set up automatic mounting of the file system

Set the automatic mounting of the file system. The
Linux operating system will automatically read the contents of the /etc/fstab file and automatically mount the specified file system every time it is powered on.
vim /etc/fstab
/dev/sdb1 /opt xfs defaults 0 0
/dev/sr0 /mnt iso9660 defaults 0 0

Field 1: Device name or device volume label name. (Partition)
The second field: the location of the mount point directory of the file system. (Mount point)
The third field: file system type, such as xfs, swap, etc. (File system type)
The fourth field: Mount parameters, that is, the parameters that can be used after the "-o" option of the mount command. For example, defaults (default parameters), rw (read and write), ro (read only), noexec (disable the execution program). (Mounting parameter)
The fifth field: indicates whether the file system needs dump backup (dump is a backup tool). Generally, it is set to 1 to indicate need, and to 0, it will be ignored by dump. (Backup)
The 6th field: This number determines the order of disk checking when the system starts. 0 means no inspection, 1 means priority inspection, and 2 means second inspection. The root partition should be set to 1, and the other partitions should be set to 2. (Disk check sequence)

After entering the correct format, restart after wq, the two mounts written will be automatically mounted upon restart.
Insert picture description here
Error example 1: When
inputting parameters, make sure that the input parameters are correct. If the file system type in the third field of the CD is entered incorrectly, it will cause the system to be unable to enter. You can only enter the emergency mode and modify it correctly before you can re-enter.
Insert picture description here
Error example 2:
Below is a typical example of what I mistyped. The reason is because I didn’t restart a line. It looks like the next line, but it actually follows the upper line.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51326240/article/details/109997458