Linux disk management and file system technologies you will often use

1. Disk structure

(1) The physical structure of the hard disk

1. Disc: The hard disk has multiple discs, each with 2 sides
2. Magnetic head: One magnetic head on each side

(2) Data structure of sector hard disk

1. Sector: The disk is divided into multiple sectors, each sector stores 512 bytes of data, the smallest storage unit of the hard disk.
2. Tracks: Concentric circles with different radii on the same disk are placed on the disk by the head Circular trajectory drawn on the surface
3. Cylinder: a cylindrical surface composed of different discs with the same radius, composed of multiple tracks with the same radius
Insert picture description here

(3) Disk structure

1. Hard disk storage capacity = number of heads × number of tracks (cylinders) × number of sectors per track x number of bytes per sector (512 bytes)
2. Cylinder/head sector can be used to uniquely locate each disk on the disk Area
3. Disk interface type
IDE, SATA, SCSI, SAS, Fibre Channel
HERE: 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 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 disk is 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 swapping.
SAS: It is a new generation of SCSI technology, which is the same as SATA hard disk. Both adopt serial technology to obtain higher transmission speed, which can reach 6Gb/s.

2. MBR and disk partition representation

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

1. The MBR is located at the first physical sector of the hard disk.
2. The MBR contains the master boot program of the hard disk and the hard disk partition table.
3. The partition table has 4 partition recording areas, each of which occupies 16 bytes.
4. Linux Represents hard disks, partitions and other devices as files in

/dev/hda5     
#/dev 是文件所在的目录,hd 表示IDE设备,sd 表示SCSI设备;
#硬盘的顺序号,以字母a、b、c.....表示;分区的顺序号,以数字1、2、 3....表示

(Two), disk partition representation

In Linux, hard disks, partitions and other devices are represented as files
/dev/sdb5
sd stands for SCSI device, hd stands for IDE device
hard disk serial number, with letters a, b, c...representing the
partition serial number, and numbers 1, 2, 3... The primary partition starts from 1-4, and the first logical partition always starts from 5.

(Three), the master boot record (MBR) disk partition

MBR is the master boot record, 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 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.

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, 1 extended partition, and multiple logical partitions in the extended partition.

Third, the disk partition structure

(1) The concept of disk partition

1. There are only 4 primary partitions in the hard disk.
2. The serial numbers of primary and extended partitions are limited to 1~4.
3. Extended partitions are divided into logical partitions.
4. The number of logical partitions will always start from 5.

Four, file system type

(1) XFS file system

1. Partition for storing files and directory data.
2. High-performance log file system, especially good at handling large files, which can support millions of terabytes of storage space.
3. The file system used by default in CentOS 7

(2) SWAP exchange file system

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

(3) Other file system types supported by Linux

   EXT4、FAT32、NTFS、 LVM

Five, detect and confirm the new hard drive

(1), fdisk command

1. View or manage disk partitions

fdisk -l 磁盘设备

or

fdisk  磁盘设备

2. Common commands in interactive mode
m、p、 n、 d、 t、 w、q
m: Get the help menu
n: Create a new partition
p: View the status of the partition
d: Delete the partition
t: Change the type of the partition
w: Save the partition operation and exit
q: Exit without saving the partition operation

Use fdisk /dev/sda to enter interactive mode to use the above commands
Insert picture description here

(Two), view the disk partition

fdisk -l [disk device]

Use the fdisk command to view the disk partitions. From the figure below, you can see that there is only one disk sda. The disk sda1\sda2\sda3 is the main partition, sda3 is the swap mode, sda4 is the extended partition, and sda5 is the logical partition.
Insert picture description here

Device: The device file name of the partition.
Boot: Whether it is a boot partition. If it is, there is a "*" mark.
Start: the starting position of the partition in the hard disk (number of cylinders)
End: the ending position of the partition in the hard disk (number of cylinders)
Blocks: the size of the partition, in Blocks (blocks) as the unit, 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, 82 represents the swap file system, and 8e represents the LVM logical volume.
System: Partition type.

(Three), manage disk partitions

fdisk /dev/sdb

You need to create a new disk before managing the disk partition. You cannot directly use the system disk for management, otherwise the computer system will crash.
Insert picture description here
After creating a new disk, use the fdisk -l command to check it and find that there is a new sdb disk.
Insert picture description here

(4) Steps to create partition

n
-> p primary partition, e extended partition, l logical partition
-> set partition: 1 (range 1-4, such as press Enter to accept the default value)
-> set cylinder sequence: directly press Enter to accept the default value
-> Set partition size: +20G (specify the size as 20GB, such as press Enter to accept the default value means all space)
--w
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.

After creating a new disk, use the fdisk /dev/sdb command to enter the new disk, and then use the "n" option to start creating partitions. At the beginning of creating a partition, it will prompt how many primary partitions, how many extended partitions and how many available partitions there are, as shown in the red box in the figure below.
Insert picture description here

Then use the "p" option to create the first primary partition. At this time, the partition number starts from "1" with the default partition number, and press "Enter" to continue; next to the selection of the starting sector size, the default value is also used here Starting from "2048", press "Enter" to continue; then to select the initial size of the sector, here you need to write down the size of primary partition 1 you need, and 10Gib is selected in the figure below. Then use the "p" option to view and find that the primary partition 1 has been created.
Insert picture description here

If you want to change the system type, you can first use the "l" option to check which system types can be replaced.
Insert picture description here

Continue to create primary partition 2. The operation process is the same as that of creating primary partition 1.
Insert picture description here

Then create an extended partition. Note that the option to create an extended partition is "e". The partition space can be larger, because the subsequent logical partitions are separated from the extended partition.
Insert picture description here

Then you can create a logical partition. Note that the option to create a logical partition is "l". Other operations are the same as creating a primary partition before. Also note that the logical partitions all start from "5".
Insert picture description here

Create another logical partition 6, give all the remaining space to 6, and press "Enter" directly.
Insert picture description here

Finally, create a swap partition. When you give it 10Gib, the value is out of range. This is because there is too much space for other partitions. There may be some differences from 10Gib, but just press "Enter" directly, and all the remaining Give it all the space.
Insert picture description here

After partitioning, remember to use "w" to save the configuration and exit
Insert picture description here

When the partition is finished, it cannot be used directly, and it can be used after formatting the disk. Formatting only needs to format the xfs format, that is, the primary partition. There are two formatting methods, one is: mkfs -t xfs /dev/sdb1 and the other is: mkfs.xfs /dev/sdb1 Note: It is best to restart the system after partitioning, or execute the "partprobe" command to operate The system detects the new partition table to prevent damage to the existing data in the hard disk when the partition is formatted.
Use the mkfs -t xfs /dev/sdb1 command to format the sdb1 partition. The xfs in this command means that the file system type of partition 1 is xfs.
Insert picture description here

Use another method: mkfs.xfs /dev/sdb2 can also format partition 2
Insert picture description here

After all partitions are formatted, if you want to use the disk (file system), you need to mount the disk on a mount point (ie a directory), so you need to create a new mount under the root directory table of Contents.
Insert picture description here

When the file system (partition 1) is mounted, you can create files in the partition
Insert picture description here

Now change sdb6 to swap system. Before creating swap, the target partition should be set to 82 by using the fdisk tool.
Insert picture description here

You can check it after the modification and remember to save it.
Insert picture description here

Use the make swap command to create a swap file system, the free -m command is to view the size of the partition space, and -m is to display the size in megabytes.
Insert picture description here

Six, create a file system

(1), mkfs command

1. Make Filesystem, create a file system (format)

mkfs  -t  文件系统类型  分区设备

2. Example

[root@localhost ~ ]# Is  /sbin/mkfs*
/sbin/mkfs  /sbin/mkfs.cramfs  /sbin/mkfs.ext3  /sbin/mkfs.minix
/sbin/mkfs.btrfs  /sbin/mkfs.ext2  /sbin/mkfs.ext4 /sbin/mkfs.xfs
[root@localhost~]# mkfs  -t  xfs  /dev/sdb1

3. Create a file system
The process of creating a file system is the process of formatting partitions
mkfs -t xfs /dev/sdb1
mkfs.xfs /dev/sdb1

(Two), mkswap command

1. Make swap, create a swap file system

mkswap  分区设备

2. Example:

[root@localhost ~]# mkswap   /dev/sdb5
[root@localhost ~]# cat  /proc/meminfo  |  grep  SwapTotal
SwapTotal:  8257532 kB
[root@localhost ~]# swapon  /dev/sdb5
[root@localhost ~]# cat  /proc/meminfo  |  grep  SwapTotal
SwapTotal:  10354680 kB
[root@localhost ~]# swapoff  /dev/sdb5

3. Create a swap file system
Before creating swap, the target partition should be set to 82 with the fdisk tool.
fdisk /dev/sdb
–>t
–>3
–>82

mkswap /dev/sdb3
swapon /dev/sdb3 #Enable the newly added swap partition
swapoff /dev/sdb3 #Disable the specified swap partition
swapon -s #View the swap status information of each partition
free -m #View the total swap status Information
Use df -h to check the mount point

Use the swapon /dev/sdb6 command to enable swap and find that the space becomes larger and you can operate it inside; use the swapoff /dev/sdb6 command to close swap and the space becomes smaller.
Insert picture description here

Use swapon -s to view the swap status information of each partition
Insert picture description here

Seven, mount and unmount the file system

(1) Manual mounting

1. The mount command

1) Mount the file system and ISO image to the specified folder

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; or used to describe special equipment, specified by loop

mount   /dev/cdrom  /mnt
mount   /dev/sdbl   /opt

Mount the image file that has been downloaded to the system

mount  -o  loop   Centos-7-x86_64-DVD-1708.iso  /media

2), view disk usage
-> direct mount command

->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

Use the mount /dev/sdb1 /datal command to mount sdb1
Insert picture description here

Among them -h: display the capacity unit of the partition -T: display the type of file system
Insert picture description here

2, umount command

1), unmount the mounted file system

umount 存储设备位置
umount   挂载点目录

Unmount the mounted file system.
Unmount prerequisite: the mounted device or directory is not in use, you must exit the mount directory first

umount    -If    存储设备目录或者挂载点目录

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

Use the umount command to unmount the mounted file system. When a "target busy" error occurs, it is because you are under the disk and you need to exit to the root directory for unmounting.
Insert picture description here

(Two), automatic mounting

1. /etc/fstab configuration file
1), contains the file system records that need to be automatically mounted after booting

vim   /etc/fstab
.....//省略部分内容
/dev/sdb1      /sdb01         xfs        dafaults    0   0
#分区         挂载点        文件系统类型

2. Set up automatic mounting of the file system
The Linux operating system will automatically read the contents of the /etc/fstab file every time it is powered on, and automatically mount the specified file system.

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.
Field 2: The location of the mount point directory of the file system.
Field 3: File system type, such as xfs, swap, etc.
Field 4: Mount parameters, which are the parameters that can be used after the "-0" option of the mount command. For example, defaults (default parameters), w (read and write), ro (read only), noexec (disable execution of programs).
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.
Field 6: This number determines the order of disk checks when the system starts. 0 means no inspection, 1 means priority inspection, 2 means second inspection. Root partition can be set to 1, other partitions can be set to 2

The Linux operating system will automatically read the contents of the /etc/fstab file every time it is powered on, and automatically mount the specified file system. So you need to save the files that need to be automatically mounted in this file.
Insert picture description here

After saving "wq", use the "init 6" command to restart, and also restart the virtual machine, and then reconnect to find that the two files have been automatically mounted.
Insert picture description here

Guess you like

Origin blog.csdn.net/Gengchenchen/article/details/110005350