8. Disk management, partition and file system mount, uninstall

1. Disk structure and partition representation

1.1 Disk Basics

A hard disk (Hard Disk Drive, HDD for short) is one of the commonly used storage devices for computers.

1.1.1 Hard disk structure

(1) 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.

  • Magnetic track: Concentric circles with different radii on the same platter, which is a circular track drawn by the magnetic head on the surface of the platter, which is convenient for data storage.

  • Cylindrical surface: A cylindrical surface composed of different disks with the same radius, consisting of multiple tracks with the same radius circle.

(2) Physical structure

  • Platter: A hard disk has multiple platters, each with 2 sides.

  • Heads: One head per side.

(3) Storage capacity:

  • Hard disk storage capacity = number of heads * number of tracks (cylinders) * number of sectors per track * number of bytes per sector (512 bytes)

  • Can use cylinder/head sector to uniquely locate each area on the disk

(4) View partition information

1.1.2 Hard disk interface

Hard disks are roughly divided into ATA (IDE), SATA, and SCSI according to different data interfaces. The interface speed is not the actual hard disk transmission speed.

  • IDE (parallel port): The parallel port data cable is connected to the motherboard and the hard disk. The anti-interference is too poor, and the cable takes up a lot of space, which is not conducive to the internal heat dissipation of the computer. It has been gradually replaced by SATA.

SATA (serial port): Strong anti-interference, support functions such as hot swap, fast speed, strong error correction ability.

  • SCSI minicomputer system interface, SCSI hard disks are widely used in workstation-class personal computers and servers. During data transmission, the CPU usage rate is low, the speed is fast, and hot swapping is supported.

  • SAS is a new generation of SCSI technology, same as SATA hard disk, it adopts serial technology to obtain higher transmission speed, which can reach 6Gb/s.

1.2 Disk partition representation

1.2.1 MBR (Master Boot Record)

  • MBR is located at the first physical sector of the hard disk. MBR contains the main boot program and hard disk partition table of the hard disk (MBR: Master Boot Record, 1982, using 32 bits to represent the number of sectors, and the partition does not exceed 2T).

  • The MBR has a total of 512 bytes, the first 446 bytes are the master boot record, and the partition table is stored in the 447-512 bytes in the MBR sector.

  • The partition table has 4 partition record areas, and each partition record area occupies 16 bytes.

  • The master boot record (MBR) disk partition supports a maximum volume of 2.2TB, and each disk can have up to 4 primary partitions, or 3 primary partitions, 1 extended partition, and multiple logical partitions in the extended partition.

1.2.2 Representation of disk partition

When the Linux kernel reads resources such as optical drives and hard disks, it is done in the form of “device files”. Therefore, hard disks and partitions are represented as different files.

Means: /dev/sda5

  • /dev/: The directory where the hardware device files are located.

  • hd: indicates the IDE device.

  • sd: Indicates the SCSI device.

  • a: The serial number of the hard disk, which hard disk it represents, represented by a, b, c….

  • 5: The sequence number of the partition, indicating the fifth partition of the first hard disk interface.

1.2.3 Disk partition structure

  • There are only 4 primary partitions in the hard disk.

  • The sequence numbers of primary partitions and extended partitions are limited to 1~4.

  • Extended partitions are subdivided into logical partitions.

  • The serial number of the logical partition will always start from 5.

  • Extended partitions cannot be used, they must be divided into logical partitions before they can be used

Common hard disks can be divided into primary partitions, extended partitions, and logical partitions. Usually, there are only four primary partitions, and extended partitions can be regarded as a special type of primary partition. Logical partitions can be established in extended partitions. The main partition is generally used to install the operating system, and the extended partition is mostly used to store file data.

1.2.4 View disk usage

  • The direct mount command can display

1.2.5 File system type used in Linux

The file system type determines the method and efficiency of storing and reading data into the partition.

  • XFS file system

  • A partition that stores file and directory data.

  • A high-performance log-type file system, especially good at processing large files, and can support millions of terabytes of storage space.

  • The file system used by default in the CenOS 7 system.

  • The file system enables the log function, so even if a downtime occurs, there is no fear of data damage. This file system can recover data in a short time according to the log records.

  • Swap swap file system (equivalent to virtual memory under Windows system)

  • Create a swap partition for the Linux system.

  • Generally set to 1.5~2 times of physical memory.

  • EXT4 (extended fourth generation file system)

  • Partitions for storing directories and data

  • Typical journaling file system

  • EXT4 (the default file system of CentOS 6 system) FAT32, NTFS, LVM logical volume

  • Linux supports other system file types

  • FAT32 (up to 4G), NTFS, FAT16

  • EXT4 (centos7), XFS (centos7), JFS. . .

2. Manage disks and partitions

2.1 Disk Management

2.1.1 Check and confirm the new hard disk

Basic format:

View disk partitions: fdisk -l [disk device]

[root@cheng0307 ~]# fdisk -l /dev/sda

?

Manage disk partitions: fdisk [disk device]

[root@cheng0307 ~]# fdisk /dev/sda

?

Identify the disk without rebooting:

echo “- – – “> /sys/class/scsi_host/host0/scan

If the system still cannot recognize the hard disk, you can restart the system

Device: the device file name of the partition;

Boot: Whether it is a boot partition, if yes, it will be marked with “*”;

Start: the starting position of the partition in the hard disk (number of cylinders);

End: the end position of the partition in the hard disk (number of cylinders);

Blocks: The size of the partition, in units of Blocks (blocks), the default block size is 1024 bytes.

Id: the system ID number corresponding to the partition, 83 represents the default partition (XFS or EXT4) in Linux, 8e represents the LVM logical volume; System: partition type.

2.1.2 Planning the partitions in the hard disk (fdisk command)

The following are all command options of fdisk, some are not commonly used, just for understanding.

  • interoperable instruction instruction

options

effect

m

get help menu

p

View partition status

no

New partition

d

delete partition

t

change partition type

w

Save the partition operation and exit

q

Exit without saving the partition operation

  • The extended command is basically similar to the fdisk command, but there are some differences.

(1) Use fdisk /dev/sda to enter the hard disk interactive management interface, and enter m to get help.

(2) Examples:

  • Add an 80GB SCSI hard disk to the host

  • Partition the hard drive

  • Divide 2 primary partitions, each with 20 GB, and the remaining space is an extended partition.

  • Create two logical partitions in the extended partition, with capacities of 2GB and 10GB respectively.

  • Change the type of the first logical partition to swap.

  • Confirm the partition settings, save and exit.

  • Use partprobe to detect changes in hard disk partitions

  • step:

The lsblk command can view partition information in detail.

1. Add a hard disk first, and let the system recognize it, you can restart it, or enter a command without restarting.

echo “- – – “> /sys/class/scsi_host/host0/scan

echo “- – – “> /sys/class/scsi_host/host1/scan

echo “- – – “> /sys/class/scsi_host/host2/scan

2. The fdisk partition is easy to use and maximizes hard disk performance.

3. Confirm the file system after formatting, and the Windows system cannot be used without formatting.

4. Mount.

  • Specific steps (see my own article 1)

2.2 Create a file system Make Filesystem, create a file system (format)

Format: mkfs -t file system type partition device (absolute path is required)

Create system files (format) mkfs -t xfs /dev/sdb1 and mkfs .xfs /dev/sdbl

  • View the UUID number (the UUID number can only be seen after formatting)

2.3 Create a swap file system-make swap command

Format: mkswap partition device

swapon /dev/ sdb2 #Enable the newly added swap partition

swapoff /dev/ sdb2 #deactivate the specified swap partition

mkswap /dev/sdb2 #create partition

Swapon is to enable swap #Note: /dev/sdb2(swap partition)

Swapoff is to close swap #Note: /dev/sdb2(swap partition)

swapon – S #View the swap status information of each partition

free -m #View the total swap status information

  • Case: EAT32 creates system files: mkfs -t vfat -F 32 /dev/sdb6 mkfs.vfat -F 32 /dev/sdb6

3. Mount and unmount file system – mount / umount

3.1 Mount the file system and ISO image to the specified folder

Format:

?

  • mount [-t file system type] storage device mount point directory

#It is used to specify the file system type, which can usually be omitted and automatically recognized by the system

  • mount -o loop IOS image file mount point directory

-o options is mainly used to describe the mounting method of the device or file

#Mount parameter list, separated by English commas: if used to describe special equipment, use 1oop to specify

loop: used to mount a file as a hard disk partition to the system

  • [root@localhost /]# mount /dev/sdb2 /qwer/

Notice:

1. It is best to hang the empty file, otherwise the file may be lost

2. If it is in the mounted directory, you cannot unmount it

3. Cannot mount multiple devices in one directory

  • Common options

-t

Used to specify the file system type, usually can be omitted (automatically recognized by the system)

-0

A list of mount parameters, separated by English commas; or used to describe special devices, specified by loop.

  • df -Th, the following is the expansion command

3.2 Unmount the mounted file system

Format:

?

umount storage device location or mount point directory

[root@cheng0307 /]# umount /dev/cdrom

[root@cheng0307 /]# umount /mnt

3.3 Set the file system to automatically mount

The /etc/fstab file in the system can be regarded as the configuration file of the mount command, which stores the static mount data of the file system. Linux will automatically read the content of this file every time it is turned on, and automatically mount the specified file system. The default fstab file includes the root partition, /boot partition, swap partition, and pseudo file systems such as proc and tmpfs. Mount configuration.

In the /etc/fstab file, each line records the mounting configuration information corresponding to a partition or device. The information includes six fields from left to right (separated by spaces or tabs), and the meaning of each part is as follows.

/dev/mapper/centos-swap swap swap defaults 0 0
  • The first field: device name or device volume label name.

  • Field 2: The location of the mount point directory for the file system.

  • Field 3: file system type, such as XES, Swap, etc.

  • The fourth field: mount parameters, that is, the parameters that can be used after the mount command “-0” option.

For example, defaults, rw, ro, and noexec represent default parameters, writable, read-only, and disable execution, respectively.

  • Field 5: Indicates whether the file system needs dump backup (dump is a backup tool).

Generally, when it is set to 1, it means that it is needed, and when it is set to 0, it will be ignored by dump.

  • Field 6: This number determines the order in which magnetic checks are performed at system startup. 0 means no check, 1 means check first, and 2 means check second. The root partition should be set to 1 and the other partitions to 2.

  • By adding the corresponding mount configuration in the /etc/fstab file, the specified partition can be automatically mounted after booting. For example, doing the following will add a configuration record for automatically mounting the partition /dev/sdb2

  • Auto mount of cdrom not set

  • Set auto mount of cdrom

4. Summary

1. Create a new hard disk and use it

a. Partition: fdisk and gdisk (expand, do knowledge)

b. Format (mkfs -t mkswap partition device)

c. mount/permanent mount (mount / umount)

2. New swap space, open, close

mkswap plus partition (hardware device) format

switch on

swap off off

free -h to check whether swap is enabled

3. Permanently mount CD-ROM and hard disk

4. fdisk partition

5. Basic structure of hard disk

Three elements: sector, track, cylinder