Linux disk management and file system (including disk management practice!)

Linux Disk Management and File System

1. Hard Disk Structure

1. The physical structure of the hard disk

  • Pan head: The hard disk has multiple platters, each with two sides
  • Magnetic head: one magnetic head on each side (the function of the magnetic head is to read and write)

Insert picture description here

2. The data structure of the hard disk

  • Sector: The disc is divided into multiple sectors, each sector stores 512 bytes of data, which is 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 circle

Insert picture description here

3. Hard disk capacity and interface

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

2) Cylinders/heads/sectors can be used to uniquely locate each area on the disk

3) Disk interface type:
IDE, SATA, SCSI, SAS, Fibre Channel

Types of hard disk interfaces:

  • 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 lot of 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 plug and other functions, fast speed, strong error correction ability.
  • SCSI-Minicomputer system interface. SCSI hard disks are widely used by workstation-level personal computers and servers. The CPU occupancy rate is low during data transmission, the speed is fast, and it supports hot plugging.
  • SAS-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 was developed to improve the speed and flexibility of a multi-disk storage system. Its appearance greatly improves the communication speed of a multi-disk system. Its main features are: hot pluggability, high-speed bandwidth, remote connection, large number of connected devices, etc.

Two, MBR and disk partition representation

1. Master Boot Record (MBR: Master Boot Record)

  • MBR is the master boot record, which is located at the first physical sector of the hard disk. The MBR contains the master boot program and hard disk partition table of the hard disk. 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 477-510 bytes in the MBR sector. The partition table has 4 partition record areas, each of which occupies 16 bytes.
  • The master boot record (MBR) disk partition supports a maximum volume of 2.2TB, and each disk has a maximum of 4 primary partitions, or 3 primary partitions and 1 extended partition (multiple logical partitions can be divided into the extended partition).
  • In Linux, devices such as hard disks and partitions are represented as files /dev/hda5

Note:

  • Where /dev/ is the directory where the hardware device file is located
  • hd means IDE device, sd means SCSI device
  • a represents the serial number of the hard disk, represented by the letters a, b, c...
  • 5 represents the sequence number of the partition, represented by numbers 1, 2, 3...

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

Insert picture description here

Three, file system type

1.XFS file system

​ ●Partition for storing files and directory data

​ ●High-performance log file system, especially good at processing large files, can support millions of terabytes of storage space.

​ ●The default file system used in the CentOS 7 system

2. SWAP, exchange file system

​ ●Create swap partition for Linux system

​ ●Generally set to 1.5 to 2 times the physical memory

3. Other file system types supported by Linux

​ ●EXT4、FAT32、NTFS、LVM

Fourth, detect and confirm the new hard drive

1. View the disk partition command format

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

Device: The device file name of the partition.

Boot: Whether it is a boot partition. If it is, it is marked with "*".

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

End: The ending position (number of cylinders) of the partition in the hard disk.

Id: The 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.

System: Partition type.

Insert picture description here

2. Manage disk partitions

fdisk /dev/sdx

Common commands in interactive mode

Common instructions function
m Get the help menu
n New partition
p Check the partition situation
d Delete partition
t Change the type of partition
w Save partition operation and exit
q Exit without saving partition operation

1) First shut down the virtual machine, and add the hard disk under the premise of shutting down the virtual machine. It is impossible to use the system disk as a disk partition, because it may directly cause the system to crash.

Insert picture description here

2) Edit the virtual machine settings, select Add→Hard Disk→Next

Insert picture description here

3) Select the disk type as SCSI(S)

Insert picture description here

4) Choose to create a new virtual disk (V)

Insert picture description here

5) The disk size here is changed to 60G, there is no need to change the red box below and the subsequent designated storage location, and click Finish.

Insert picture description here

Insert picture description here

6) After adding the hard disk, you will see an extra 60G hard disk in the hardware configuration, click OK and start the virtual machine. Open Xshell, connect to the virtual machine, and use the command fdisk -l to view the disk device, and find that there is an extra disk /dev/sdb.

Insert picture description here

7) Start to create a partition

n New partition

→p (where p represents the primary partition, e is the extended partition, and l is the logical partition)

—→Set Partition: 1 (Partition is 1-4, the default is 1, and the carriage return is 1 by default)

——→Set the cylinder sequence: directly press Enter to accept the default value (starting sector)

———→Set partition size: +20G (Specify the size as 20GB, such as press Enter to accept the default value means all the space)

————→w (Save the partition operation and exit)

8) First use fdisk /dev/sdb to enter the hard disk management, enter m to get a prompt

Insert picture description here

9) Enter n, then press Enter to add a partition (4 free partitions can be added), enter p to select to add a primary partition, press Enter to use the default value, the default partition 1, and then press Enter to use the default starting sector 2048. +10G means that the capacity of this partition is 10G

Insert picture description here

Enter p and press Enter to view the disk partition table. The primary partition /dev/sdb1 has been added as follows, with a size of 10G.

Insert picture description here

Enter l (lowercase L) to view the partition type and its corresponding ID number. The red box selects the commonly used partition type.

Insert picture description here

10) New partition → e partition type, select extended partition → partition number and start sector, directly press Enter to accept the default value → capacity size is set to 30G

Insert picture description here

11) The logical partition is divided from the extended partition, and the capacity of the extended partition = the sum of the capacities of each logical partition. Therefore, the total capacity of logical partitions cannot be larger than the capacity of extended partitions. Next, create two logical partitions, and the starting serial number of the logical partition is 5.

Insert picture description here

12) Hard disk capacity 60G-primary partition 1 capacity 10G-extended partition capacity 30G=20G, the remaining 20G is directly made into primary partitions 3 and 4, and then enter p to view the partitions

Insert picture description here

Insert picture description here

13) After confirming that the above operations are correct, enter w and press Enter to save the partition. However, after the partition is created, it is temporarily unavailable and needs to be formatted and mounted first.

Insert picture description here

5. Create a file system (format) and mount

  • mkfs command

    Make Filesystem, create a file system (format)

    Command format

    mkfs -t xfs /dev/sdb1
    
    mkfs.xfs /dev/sdb1
    

    Next, format partitions 1 and 3; because partition 2 is an extended partition and contains a partition table, it can only be used to continue to divide logical partitions, so it cannot be formatted; partition 4 will be used to create a swap file system later. Partitions 5 and 6 are logical partitions. Formatting the logical partition destroys the original extended partition table (linked list) of the extended partition. When the operating system reads the extended partition table, the chained table does not exist but becomes the formatted file system's own data. The chained table does not exist and an error is reported. The operating system thinks the partition table is wrong and reports an error, the partition table The error is a serious error.

Insert picture description here

So only need to format partitions 1 and 3, as shown above. However, these two partitions are still unavailable at present and need to be mounted. The specific operations are as follows:

Insert picture description here

Six, create a swap file system swap

mkswap command
make swap-create a swap file system

  • Command format
mkswap 分区设备
  • Create a swap file system

Before creating swap, the target partition should first use the fdisk tool to set the partition type ID number to 82 (that is, the swap type)

1. Enter the disk management through fdisk /dev/sdb

2. To change the partition type, press Enter→4 to select the partition number of the type to be changed, press Enter→enter the type code, press Enter

Insert picture description here

3. Enter p and press Enter to view the partition details → confirm that the 4 partition type has been changed and enter w, press Enter to save and exit

Insert picture description here

4. After changing the partition settings of the hard disk (especially the hard disk in use), it is recommended to restart the system once, or execute the "partprobe" command to make the operating system detect the new partition table. To prevent damage to the existing data in the hard disk when formatting the partition.

5. Create swap partition 4 and enable it, use free -m or free -g to view (-m means to display capacity in MB, -g means to display capacity in GB)

Insert picture description here

Seven, mount and unmount the file system mount and umount

Note: Formatting operation is required before mounting

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

    Command format

    mount  [-t 类型]  存储设备  挂载点目录
    mount  -o loop  ISO镜像文件  挂载点目录
    

-t: used to specify the file system type, usually can be omitted, automatically recognized by the system
-o: mount parameter list, separated by English commas; if used to describe special equipment, use loop to specify

  • Unmount the mounted file system

Note: The mounted device or directory to be unmounted is not in use, you must exit the mounted directory first

Command format

umount [-lf] 存储设备目录或者挂载点目录

-l means to release the busy file system
-f means to force

  • View disk usage

1) Direct mount command

2) df [options]
-h: display the capacity unit of the partition
-T: display the type of file system
-i: display the number of inode numbers of the partition

There are two ways to uninstall the mounted file system:

umount /dev/sdb1
umount /opt

Example: Mount /dev/sdb1 to the /opt directory and unmount it.

Insert picture description here

Insert picture description here

Eight, the 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 turned 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 of the program). (Mount 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 sequence of disk checks when the system is started. 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 inputting the correct format, ESC exits the input mode, and then restarts after :wq. The two written mounts will be automatically mounted when restarting.

Insert picture description here

After restarting, it is found that /dev/sdb1 has been automatically mounted to the /opt directory.

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/110481672