[Linux] How to add and use a hard disk in Linux

How to add and use a hard disk in Linux? We can divide it into three steps, the first step is partitioning, the second step is formatting, and the third step is mounting.

1. Disk

1.1 MBR

MBR: Master Boot Record

MBR is located in the first physical sector of the hard disk

MBR contains the hard drive’s main boot program and hard drive partition table

The partition table has 4 partition recording areas, each of which occupies 16 bytes

In Linux, hard disks, partitions and other devices are represented as files

1.2 Disk partition structure

The number of primary partitions in the hard disk is only 4

The serial numbers of primary partitions and extended partitions are limited to 1 to 4

Extended partitions are divided into logical partitions

Logical partition numbers will always start from 5

1.3 Advantages of Partition

Optimize read and write performance Read and write

Implement disk space quota limits

Improve repair speed

Isolation Systems and Procedures

Install multiple OS

Use different file systems

2. File system type

2.1 XFS file system

1. Partition to store file and directory data

2. A high-performance log file system that is particularly good at processing large files and can support millions of TB of storage space

3. The file system used by default in CentOS 7 system

2.2 SWAP, swap file system

Create a swap partition for Linux system

Generally set to 1.5 to 2 times the physical memory

Other file system types supported by Linux

EXT4, FAT32, NTFS, LVM

Note: The default file system used by CentOS 6 is EXT4/3; the default file system used by CentOS 7 is XFS.

2.3 fdisk command

Format: fdisk -l [disk device]

Application: fdisk [disk device]

fdisk command (same as gidsk)

View or manage disk partitions

fdisk -l [disk_device] or fdisk [disk_device]

Common commands in interactive mode

Command Function
-m Provide help
-n New partition
-d Delete partition
-t Change partition type
-p Print the current partition status
-w Save and exit

2.4 Create a new hard drive

To add a new hard disk, the steps are as follows:

Settings – Hard Disk – Add;

Command: alias scan=’echo “- – -” > /sys/class/scsi_host/host0/scan;echo “- – -” > /sys/class/scsi_host/host1/scan;echo “- – -” > / sys/class/scsi_host/host2/scan’ can refresh the hard disk

Partition the disk /dev/sdb again;

w Save and exit otherwise it will not take effect

3. Create file system

3.1 mkfs

After dividing the new hard disk into partitions, you still need to format the partition (that is, create a file system) and mount it to a specified directory in the Linux system before it can be used to store files, directories and other data.

mkfs command

mkfs.xfs [specified partition]: Format the specified partition to xfs type;

mkfs.ext4 [specified partition]: Format the specified partition to ext4 type;

mkswap command

make swap, create a swap file system
mkswap partition device

mkswap [partition device]: Format the file system for the specified swap partition;

[root@localhost ~]# mkswap /dev/sdb1
mkswap: /dev/sdb1: warning: wiping old xfs signature.
Setting up swap space version 1, size = 5242876 KiB
No tag, UUID=d64e9dd2-42fc-4525-96f7-76728b1ccfe8

free -m: View system memory Situation, -m unified unit, view memory in megabytes

swapon [partition device]: Enable the newly added swap partition;

swapoff [partition device]: Disables the specified swap partition device;

lsblk can also check the hard disk usage, it also You can check the mounting status

3.2 Mount and unmount file systems

mount command

mount [-t type] storage device mount point directory

mount -o loop ISO image file mount point directory

According to the mounting rules, it is best to only mount a hard disk to one directory.

Two partitions are mounted to the same directory at the same time, and the last mounted partition is displayed. Note: Do not do this as it may cause data loss.

It is best to mount an empty directory, otherwise data will be lost.

umount device name|mount point

fuser -v to see who is using it

fuser -km forcefully kicks people down (don’t use it indiscriminately)

Permanently mounted through configuration files;

Command: vim/etc/fastb

3.3 View disk usage

df command

df [options] [file]

lsblk: View the current mounting status of all partition devices;

4. Experiment

fdisk /dev/sdb //Create primary partition
n

 +5G
w
fdisk /dev/sdb //Create logical partition
n


w
fdisk /dev/sdb //Create extended partition
n

 +5G
w
mkfs.xfs /dev/sdb1 //Format
mkfs.xfs /dev/sdb5 //Format
cd /
mkdir sdb1 //Create a hanging directory
mkdir sdb5 //Create a hanging directory
mount /dev/sdb1 /sdb1 //Mount to the corresponding directory
mount /dev/sdb5 /sdb5 mount to the corresponding directory
lsblk //View
vim /etc/fstab //Enter the configuration file
/dev/sdb1 /sdb1 xfs defaults 0 0 //Permanently mounted
/dev/sdb5 /sdb5 xfs defaults 0 0 //Permanently mounted
:wq! //Save and exit

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 16876 people are learning the system