Chapter IX disk storage and file system management

Posted by stomlin on Mon, 01 Nov 2021 02:19:25 +0100

1. Disk structure

1.1 equipment documents

Everything is a file: open(), read(), write(), close()

Device file: associated with a device driver, which can communicate with the corresponding hardware device

Equipment number:

  • Main equipment number: major number, which identifies the equipment type
  • Secondary equipment number: minor number, which identifies different equipment of the same type

Equipment type:

  • Block device: block, access unit "block", disk
  • Character device: char, access unit "character", keyboard

example:

[root@rocky8 ~]# ll /dev
total 0
crw-r--r-- 1 root root     10, 235 Oct 15 17:54 autofs
drwxr-xr-x 2 root root         180 Oct 15 17:54 block
drwxr-xr-x 2 root root          80 Oct 15 17:54 bsg
drwxr-xr-x 3 root root          60 Oct 15 17:54 bus
lrwxrwxrwx 1 root root           3 Oct 15 17:54 cdrom -> sr0
drwxr-xr-x 2 root root        2900 Oct 15 17:54 char
crw------- 1 root root      5,   1 Oct 15 17:54 console
lrwxrwxrwx 1 root root          11 Oct 15 17:54 core -> /proc/kcore
drwxr-xr-x 3 root root          60 Oct 15 17:54 cpu
crw------- 1 root root     10,  62 Oct 15 17:54 cpu_dma_latency
drwxr-xr-x 7 root root         140 Oct 15 17:54 disk
crw-rw---- 1 root audio    14,   9 Oct 15 17:54 dmmidi
drwxr-xr-x 3 root root         100 Oct 15 17:54 dri
crw-rw---- 1 root video    29,   0 Oct 15 17:54 fb0
lrwxrwxrwx 1 root root          13 Oct 15 17:54 fd -> /proc/self/fd
crw-rw-rw- 1 root root      1,   7 Oct 15 17:54 full
crw-rw-rw- 1 root root     10, 229 Oct 15 17:54 fuse
crw------- 1 root root    245,   0 Oct 15 17:54 hidraw0
crw------- 1 root root     10, 228 Oct 15 17:54 hpet
drwxr-xr-x 2 root root           0 Oct 15 17:54 hugepages
crw------- 1 root root     10, 183 Oct 15 17:54 hwrng
lrwxrwxrwx 1 root root          12 Oct 15 17:54 initctl -> /run/initctl
drwxr-xr-x 4 root root         280 Oct 15 17:54 input
crw-r--r-- 1 root root      1,  11 Oct 15 17:54 kmsg
lrwxrwxrwx 1 root root          28 Oct 15 17:54 log -> /run/systemd/journal/dev-log
crw-rw---- 1 root disk     10, 237 Oct 15 17:54 loop-control
drwxr-xr-x 2 root root          60 Oct 15 17:54 mapper
crw------- 1 root root     10, 227 Oct 15 17:54 mcelog
crw-r----- 1 root kmem      1,   1 Oct 15 17:54 mem
crw-rw---- 1 root audio    14,   2 Oct 15 17:54 midi
drwxrwxrwt 2 root root          40 Oct 15 17:54 mqueue
drwxr-xr-x 2 root root          60 Oct 15 17:54 net
crw-rw-rw- 1 root root      1,   3 Oct 15 17:54 null
crw------- 1 root root     10, 144 Oct 15 17:54 nvram
crw-r----- 1 root kmem      1,   4 Oct 15 17:54 port
crw------- 1 root root    108,   0 Oct 15 17:54 ppp
crw-rw-rw- 1 root tty       5,   2 Oct 15 18:00 ptmx
drwxr-xr-x 2 root root           0 Oct 15 17:54 pts
crw-rw-rw- 1 root root      1,   8 Oct 15 17:54 random
drwxr-xr-x 2 root root          60 Oct 15 17:54 raw
crw-rw-r-- 1 root root     10,  60 Oct 15 17:54 rfkill
lrwxrwxrwx 1 root root           4 Oct 15 17:54 rtc -> rtc0
crw------- 1 root root    251,   0 Oct 15 17:54 rtc0
brw-rw---- 1 root disk      8,   0 Oct 15 17:54 sda #8 represents the primary number of the equipment type, and 0 represents the secondary number
brw-rw---- 1 root disk      8,   1 Oct 15 17:54 sda1
brw-rw---- 1 root disk      8,   2 Oct 15 17:54 sda2
brw-rw---- 1 root disk      8,   3 Oct 15 17:54 sda3
brw-rw---- 1 root disk      8,   4 Oct 15 17:54 sda4
brw-rw---- 1 root disk      8,   5 Oct 15 17:54 sda5
crw-rw---- 1 root cdrom    21,   0 Oct 15 17:54 sg0
crw-rw---- 1 root disk     21,   1 Oct 15 17:54 sg1
drwxrwxrwt 2 root root          40 Oct 15 17:54 shm
crw------- 1 root root     10, 231 Oct 15 17:54 snapshot
drwxr-xr-x 3 root root         200 Oct 15 17:54 snd
brw-rw---- 1 root cdrom    11,   0 Oct 15 17:54 sr0
lrwxrwxrwx 1 root root          15 Oct 15 17:54 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root          15 Oct 15 17:54 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root          15 Oct 15 17:54 stdout -> /proc/self/fd/1
crw-rw-rw- 1 root tty       5,   0 Oct 15 17:54 tty
crw--w---- 1 root tty       4,   0 Oct 15 17:54 tty0
crw--w---- 1 root tty       4,   1 Oct 15 17:54 tty1
crw--w---- 1 root tty       4,  10 Oct 15 17:54 tty10
crw--w---- 1 root tty       4,  11 Oct 15 17:54 tty11
crw--w---- 1 root tty       4,  12 Oct 15 17:54 tty12
crw--w---- 1 root tty       4,  13 Oct 15 17:54 tty13
crw--w---- 1 root tty       4,  14 Oct 15 17:54 tty14
crw--w---- 1 root tty       4,  15 Oct 15 17:54 tty15
crw--w---- 1 root tty       4,  16 Oct 15 17:54 tty16
crw--w---- 1 root tty       4,  17 Oct 15 17:54 tty17
crw--w---- 1 root tty       4,  18 Oct 15 17:54 tty18
crw--w---- 1 root tty       4,  19 Oct 15 17:54 tty19
crw--w---- 1 root tty       4,   2 Oct 15 17:54 tty2
crw--w---- 1 root tty       4,  20 Oct 15 17:54 tty20
crw--w---- 1 root tty       4,  21 Oct 15 17:54 tty21
crw--w---- 1 root tty       4,  22 Oct 15 17:54 tty22
crw--w---- 1 root tty       4,  23 Oct 15 17:54 tty23
crw--w---- 1 root tty       4,  24 Oct 15 17:54 tty24
crw--w---- 1 root tty       4,  25 Oct 15 17:54 tty25
crw--w---- 1 root tty       4,  26 Oct 15 17:54 tty26
crw--w---- 1 root tty       4,  27 Oct 15 17:54 tty27
crw--w---- 1 root tty       4,  28 Oct 15 17:54 tty28
crw--w---- 1 root tty       4,  29 Oct 15 17:54 tty29
crw--w---- 1 root tty       4,   3 Oct 15 17:54 tty3
crw--w---- 1 root tty       4,  30 Oct 15 17:54 tty30
crw--w---- 1 root tty       4,  31 Oct 15 17:54 tty31
crw--w---- 1 root tty       4,  32 Oct 15 17:54 tty32
crw--w---- 1 root tty       4,  33 Oct 15 17:54 tty33
crw--w---- 1 root tty       4,  34 Oct 15 17:54 tty34
crw--w---- 1 root tty       4,  35 Oct 15 17:54 tty35
crw--w---- 1 root tty       4,  36 Oct 15 17:54 tty36
crw--w---- 1 root tty       4,  37 Oct 15 17:54 tty37
crw--w---- 1 root tty       4,  38 Oct 15 17:54 tty38
crw--w---- 1 root tty       4,  39 Oct 15 17:54 tty39
crw--w---- 1 root tty       4,   4 Oct 15 17:54 tty4
crw--w---- 1 root tty       4,  40 Oct 15 17:54 tty40
crw--w---- 1 root tty       4,  41 Oct 15 17:54 tty41
crw--w---- 1 root tty       4,  42 Oct 15 17:54 tty42
crw--w---- 1 root tty       4,  43 Oct 15 17:54 tty43
crw--w---- 1 root tty       4,  44 Oct 15 17:54 tty44
crw--w---- 1 root tty       4,  45 Oct 15 17:54 tty45
crw--w---- 1 root tty       4,  46 Oct 15 17:54 tty46
crw--w---- 1 root tty       4,  47 Oct 15 17:54 tty47
crw--w---- 1 root tty       4,  48 Oct 15 17:54 tty48
crw--w---- 1 root tty       4,  49 Oct 15 17:54 tty49
crw--w---- 1 root tty       4,   5 Oct 15 17:54 tty5
crw--w---- 1 root tty       4,  50 Oct 15 17:54 tty50
crw--w---- 1 root tty       4,  51 Oct 15 17:54 tty51
crw--w---- 1 root tty       4,  52 Oct 15 17:54 tty52
crw--w---- 1 root tty       4,  53 Oct 15 17:54 tty53
crw--w---- 1 root tty       4,  54 Oct 15 17:54 tty54
crw--w---- 1 root tty       4,  55 Oct 15 17:54 tty55
crw--w---- 1 root tty       4,  56 Oct 15 17:54 tty56
crw--w---- 1 root tty       4,  57 Oct 15 17:54 tty57
crw--w---- 1 root tty       4,  58 Oct 15 17:54 tty58
crw--w---- 1 root tty       4,  59 Oct 15 17:54 tty59
crw--w---- 1 root tty       4,   6 Oct 15 17:54 tty6
crw--w---- 1 root tty       4,  60 Oct 15 17:54 tty60
crw--w---- 1 root tty       4,  61 Oct 15 17:54 tty61
crw--w---- 1 root tty       4,  62 Oct 15 17:54 tty62
crw--w---- 1 root tty       4,  63 Oct 15 17:54 tty63
crw--w---- 1 root tty       4,   7 Oct 15 17:54 tty7
crw--w---- 1 root tty       4,   8 Oct 15 17:54 tty8
crw--w---- 1 root tty       4,   9 Oct 15 17:54 tty9
crw-rw---- 1 root dialout   4,  64 Oct 15 17:54 ttyS0
crw-rw---- 1 root dialout   4,  65 Oct 15 17:54 ttyS1
crw-rw---- 1 root dialout   4,  66 Oct 15 17:54 ttyS2
crw-rw---- 1 root dialout   4,  67 Oct 15 17:54 ttyS3
crw------- 1 root root     10, 239 Oct 15 17:54 uhid
crw------- 1 root root     10, 223 Oct 15 17:54 uinput
crw-rw-rw- 1 root root      1,   9 Oct 15 17:54 urandom
crw------- 1 root root    246,   0 Oct 15 17:54 usbmon0
crw------- 1 root root    246,   1 Oct 15 17:54 usbmon1
crw------- 1 root root    246,   2 Oct 15 17:54 usbmon2
crw-rw---- 1 root tty       7,   0 Oct 15 17:54 vcs
crw-rw---- 1 root tty       7,   1 Oct 15 17:54 vcs1
crw-rw---- 1 root tty       7,   2 Oct 15 17:54 vcs2
crw-rw---- 1 root tty       7,   3 Oct 15 17:54 vcs3
crw-rw---- 1 root tty       7,   4 Oct 15 17:54 vcs4
crw-rw---- 1 root tty       7,   5 Oct 15 17:54 vcs5
crw-rw---- 1 root tty       7,   6 Oct 15 17:54 vcs6
crw-rw---- 1 root tty       7, 128 Oct 15 17:54 vcsa
crw-rw---- 1 root tty       7, 129 Oct 15 17:54 vcsa1
crw-rw---- 1 root tty       7, 130 Oct 15 17:54 vcsa2
crw-rw---- 1 root tty       7, 131 Oct 15 17:54 vcsa3
crw-rw---- 1 root tty       7, 132 Oct 15 17:54 vcsa4
crw-rw---- 1 root tty       7, 133 Oct 15 17:54 vcsa5
crw-rw---- 1 root tty       7, 134 Oct 15 17:54 vcsa6
drwxr-xr-x 2 root root          60 Oct 15 17:54 vfio
crw------- 1 root root     10,  63 Oct 15 17:54 vga_arbiter
crw------- 1 root root     10, 137 Oct 15 17:54 vhci
crw------- 1 root root     10, 238 Oct 15 17:54 vhost-net
crw------- 1 root root     10, 241 Oct 15 17:54 vhost-vsock
crw------- 1 root root     10,  61 Oct 15 17:54 vmci
crw------- 1 root root     10,  59 Oct 15 17:54 vsock
crw-rw-rw- 1 root root      1,   5 Oct 15 17:54 zero

Device file naming for disk devices:

/dev/DEV_FILE
/dev/sdX #SCSI, SATA, SAS, IDE,USB
/dev/nvme0n# #Nvme protocol hard disk, such as the first hard disk: nvme0n1 and the second hard disk: nvme0n2

Virtual disk:

/dev/vdX
/dev/xvdX

Different disk IDS: a-z,aa,ab

example:

[root@rocky8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sr0     11:0    1  9.2G  0 rom 

Different partitions on the same device: 1,2

/dev/sda1
/dev/sda5

example:

[root@rocky8 ~]# fdisk /dev/sda -l
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos #dos is the MBR partition format
Disk identifier: 0x43d7507d

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 316671999 104857600   50G 83 Linux
/dev/sda4       316672000 419430399 102758400   49G  5 Extended
/dev/sda5       316674048 320868351   4194304    2G 82 Linux swap / Solaris
#*An asterisk indicates an active partition

Example: creating a device file

[root@rocky8 ~]# df /boot
Filesystem     1K-blocks   Used Available Use% Mounted on
/dev/sda1        1038336 191796    846540  19% /boot
[root@rocky8 ~]# ls /boot
config-4.18.0-305.3.1.el8_4.x86_64                       initramfs-4.18.0-305.3.1.el8_4.x86_64kdump.img
efi                                                      loader
grub2                                                    System.map-4.18.0-305.3.1.el8_4.x86_64
initramfs-0-rescue-ad9d4d0a9e7a41c6ba47c505d1172547.img  vmlinuz-0-rescue-ad9d4d0a9e7a41c6ba47c505d1172547
initramfs-4.18.0-305.3.1.el8_4.x86_64.img                vmlinuz-4.18.0-305.3.1.el8_4.x86_64
[root@rocky8 ~]# mknod /data/partition-sda1 b 8 1
[root@rocky8 ~]# ll /data/partition-sda1
brw-r--r-- 1 root root 8, 1 Oct 15 18:24 /data/partition-sda1
[root@rocky8 ~]# mount /data/partition-sda1 /mnt/
[root@rocky8 ~]# ls /mnt
config-4.18.0-305.3.1.el8_4.x86_64                       initramfs-4.18.0-305.3.1.el8_4.x86_64kdump.img
efi                                                      loader
grub2                                                    System.map-4.18.0-305.3.1.el8_4.x86_64
initramfs-0-rescue-ad9d4d0a9e7a41c6ba47c505d1172547.img  vmlinuz-0-rescue-ad9d4d0a9e7a41c6ba47c505d1172547
initramfs-4.18.0-305.3.1.el8_4.x86_64.img                vmlinuz-4.18.0-305.3.1.el8_4.x86_64

[root@rocky8 ~]# ll /dev/zero
crw-rw-rw- 1 root root 1, 5 Oct 15 18:20 /dev/zero
[root@rocky8 ~]# mknod /data/zero c 1 5
[root@rocky8 ~]# ll /data/zero 
crw-r--r-- 1 root root 1, 5 Oct 15 18:25 /data/zero

1.2 hard disk type


Hard disk interface type

  • IDE: 133MB/s, parallel interface, early home computer
  • SCSI: 640MB/s, parallel interface, early server
  • SATA: 6Gbps, the SATA data port is separated from the power port, that is, two lines are required, one data line and one power line
  • SAS: 6Gbps. SAS is a whole line. The data port and power port are integrated. The power supply line is included in SAS, but not in SATA. SATA standard is actually a subset of SAS standard. They are compatible. SATA hard disk can be inserted into SAS motherboard, and vice versa
  • USB: 480MB/s
  • M.2:

Note: the speed is not determined by the simple interface type. The speed of hard disk supporting Nvme protocol is the fastest

Server hard disk size

LFF: 3.5 inches, which is the size of desktop hard disk

SFF: Small Form Factor, Small Form Factor, 2.5 inches. Note that it is different from the 2.5-inch notebook hard disk

50. S means large and small respectively. At present, sff hard disks are used in servers or cabinets to consider increasing disk capacity per unit density, enhancing heat dissipation and reducing power consumption

1.3 mechanical hard disk and solid state disk

Mechanical hard disk (HDD): Hard Disk Drive is a traditional ordinary hard disk, which is mainly composed of disc, magnetic head, disc rotating shaft and control motor, magnetic head controller, data converter, interface, cache, etc. All disks in the mechanical hard disk are installed on a rotating shaft. Each disk is parallel. There is a magnetic head on the storage surface of each disk. The distance between the magnetic head and the disk is smaller than the diameter of the hair wire. All magnetic heads are connected to a magnetic head controller, which is responsible for the movement of each magnetic head. The magnetic head can move along the radius of the disc
With the direction movement and the high-speed rotation of the disc thousands of revolutions per minute, the magnetic head can be positioned at the specified position of the disc for data reading and writing. The data is written to the disk by the electromagnetic current through the magnetic head, and can also be read in the opposite way. The hard disk is a precision equipment, and the air entering the hard disk must be filtered

Solid State Drive (SSD): Solid State Drive is a hard disk made of solid-state electronic storage chip array, which is composed of control unit and storage unit (FLASH chip and DRAM chip). The interface specification, definition, function and use method of solid-state disk are exactly the same as those of ordinary hard disk, and the product shape and size are also the same as those of ordinary hard disk

Compared with HDD, SSD has obvious advantages in shock resistance, fall resistance, transmission rate, power consumption, weight and noise. The transmission rate performance of SSD is twice that of HDD

Compared with SSD, HDD has absolute advantages in price and capacity

***

Mechanical hard disk structure

Solid state drive (SSD)

1.4 hard disk storage terminology


Hard disk storage terminology CHS

  • head: number of heads = number of disks
  • Track: track = number of cylinders
  • Sector: sector, 512bytes
  • Cylinder: cylinder 1 cylinder = 512 * number of sectors / number of trackhead s = 51263*255=7.84M

Linux before CentOS 5 is partitioned by an integer multiple of the cylinder. After CentOS 6, it can support partitioning by sectors

example:

#View CHS
[root@centos6 ~]# fdisk -l /dev/sda

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008a04f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         131     1048576   83  Linux
Partition 1 does not end on cylinder boundary. #It is suggested that zone 1 is not divided by the boundary of cylinder
/dev/sda2             131       12910   102645760   83  Linux
/dev/sda3           12910       19437    52428800   83  Linux
/dev/sda4           19437       26109    53591040    5  Extended
/dev/sda5           19437       19698     2097152   82  Linux swap / Solaris
# Head head, available only on centos 6
#Track track
#sector
#cylinders cylinder

[root@centos6 ~]# echo "scale=2;512*63*255/1024/1024" |bc
7.84

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

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b65a1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   211814399   104857600   83  Linux
/dev/sda3       211814400   316671999    52428800   83  Linux
/dev/sda4       316672000   419430399    51379200    5  Extended
/dev/sda5       316674048   320868351     2097152   82  Linux swap / Solaris
#centos 7 and above are displayed in sectors

[root@centos7 ~]# fdisk -u=cylinder -l /dev/sda

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b65a1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         131     1048576   83  Linux
/dev/sda2             131       13185   104857600   83  Linux
/dev/sda3           13185       19712    52428800   83  Linux
/dev/sda4           19712       26109    51379200    5  Extended
/dev/sda5           19713       19974     2097152   82  Linux swap / Solaris
#-u=cylinder enter this parameter to display the cylinder

[root@rocky8 ~]# fdisk -u=cylinder -l /dev/sda
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Geometry: 255 heads, 2 sectors/track, 26108 cylinders
Units: cylinders of 510 * 512 = 261120 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43d7507d

Device     Boot  Start    End Cylinders  Size Id Type
/dev/sda1  *         5   4117      4113    1G 83 Linux
/dev/sda2         4117 415323    411207  100G 83 Linux
/dev/sda3       415323 620926    205604   50G 83 Linux
/dev/sda4       620926 822413    201488   49G  5 Extended
/dev/sda5       620930 629154      8225    2G 82 Linux swap / Solaris

Example: identify SSD and mechanical hard disk types

#1 means mechanical and 0 means SSD
[root@rocky8 ~]# lsblk -d -o name,rota
NAME    ROTA
sda        1
sr0        1
nvme0n1    0

[root@rocky8 ~]# ls /sys/block/
nvme0n1  sda  sr0
[root@rocky8 ~]# cat /sys/block/*/queue/rotational
0
1
1

[root@rocky8 ~]# cat /sys/block/sda/queue/rotational
1
[root@rocky8 ~]# cat /sys/block/sr0/queue/rotational
1
[root@rocky8 ~]# cat /sys/block/nvme0n1/queue/rotational
0

Location recording disk sector structure ZBR (Zoned Bit Recording)

CHS

  • CHS adopts 24 bit addressing
  • The first 10 bits represent cylinder, the middle 8 bits represent head, and the last 6 bits represent sector
  • Maximum addressing space 8 GB

LBA(logical block addressing)

  • LBA is an integer that completes disk specific addressing by converting it to CHS format
  • The ATA-1 specification defines a 28 bit addressing mode. Based on 512 bits per sector, the upper limit of 28 bit LBA defined by ATA-1 reaches 128 GiB. The ATA-6 specification in 2002 adopts 48 bit LBA, which is also calculated with 512 bits per sector, and the upper limit of capacity can reach 128
  • Petabytes

Since the addressing space of CHS addressing mode is within about 8GB, CHS addressing mode or LBA addressing mode can be used when the disk capacity is less than about 8GB; When the disk capacity is greater than about 8GB, LBA addressing mode can only be used

2. Manage storage

Use disk space procedure

  1. Equipment partition
  2. Create file system
  3. Mount a new file system

2.1 disk partition

2.1.1 why partition

  • Optimize I/O performance
  • Implement disk space quota limits
  • Improve repair speed
  • Isolation systems and procedures
  • Install multiple OS
  • Using different file systems

2.1.2 zoning mode

Two partition modes: MBR and GPT

2.1.2.1 MBR zoning

MBR: Master Boot Record, 1982, uses 32 bits to represent the number of sectors, and the partition does not exceed 2T

Division unit:

  • Before CentOS 5, it was divided by the whole cylinder
  • After CentOS 6, it can be divided by Sector

Track 0 sector 0: 512bytes

  • 446bytes: boot loader
  • 64bytes: partition table, where every 16bytes identifies a partition
  • 2bytes: 55AA

A hard disk in MBR partition can have up to 4 primary partitions, or 3 primary partitions + 1 expansion (N logical partitions)

MBR partition: 1 – 4 corresponding to primary and extended partitions, / dev/sda3, logical partition starts from 5, / dev/sda5

MBR partition structure


The hard disk master boot record MBR consists of four parts

  • The master boot program (offset 0000H – 0088H), which is responsible for loading from the active partition and running the system boot program
  • In the error message data area, the offset addresses 0089H – 00E1H are error messages, and 00E2H – 01BDH are all 0 bytes
  • The DPT (disk partition table) contains four partition entries with offset addresses of 01BEH – 01FDH. Each partition table entry is 16 bytes long, with a total of 64 bytes, including partition entry 1, partition entry 2, partition entry 3 and partition entry 4
  • End flag word, the two byte value of offset address 01FE – 01FF is end flag 55AA

DPT structure in MBR

Example: back up the partition table of MBR and restore after destruction

[root@rocky8 ~]# hexdump -C /dev/sda -n 512 -v
00000000  eb 63 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.c..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|
00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|
00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 05 7c  |. ..d|<.t...R..||
00000090  b4 41 bb aa 55 cd 13 5a  52 72 3d 81 fb 55 aa 75  |.A..U..ZRr=..U.u|
000000a0  37 83 e1 01 74 32 31 c0  89 44 04 40 88 44 ff 89  |7...t21..D.@.D..|
000000b0  44 02 c7 04 10 00 66 8b  1e 5c 7c 66 89 5c 08 66  |D.....f..\|f.\.f|
000000c0  8b 1e 60 7c 66 89 5c 0c  c7 44 06 00 70 b4 42 cd  |..`|f.\..D..p.B.|
000000d0  13 72 05 bb 00 70 eb 76  b4 08 cd 13 73 0d 5a 84  |.r...p.v....s.Z.|
000000e0  d2 0f 83 de 00 be 85 7d  e9 82 00 66 0f b6 c6 88  |.......}...f....|
000000f0  64 ff 40 66 89 44 04 0f  b6 d1 c1 e2 02 88 e8 88  |d.@f.D..........|
00000100  f4 40 89 44 08 0f b6 c2  c0 e8 02 66 89 04 66 a1  |.@.D.......f..f.|
00000110  60 7c 66 09 c0 75 4e 66  a1 5c 7c 66 31 d2 66 f7  |`|f..uNf.\|f1.f.|
00000120  34 88 d1 31 d2 66 f7 74  04 3b 44 08 7d 37 fe c1  |4..1.f.t.;D.}7..|
00000130  88 c5 30 c0 c1 e8 02 08  c1 88 d0 5a 88 c6 bb 00  |..0........Z....|
00000140  70 8e c3 31 db b8 01 02  cd 13 72 1e 8c c3 60 1e  |p..1......r...`.|
00000150  b9 00 01 8e db 31 f6 bf  00 80 8e c6 fc f3 a5 1f  |.....1..........|
00000160  61 ff 26 5a 7c be 80 7d  eb 03 be 8f 7d e8 34 00  |a.&Z|..}....}.4.|
00000170  be 94 7d e8 2e 00 cd 18  eb fe 47 52 55 42 20 00  |..}.......GRUB .|
00000180  47 65 6f 6d 00 48 61 72  64 20 44 69 73 6b 00 52  |Geom.Hard Disk.R|
00000190  65 61 64 00 20 45 72 72  6f 72 0d 0a 00 bb 01 00  |ead. Error......|
000001a0  b4 0e cd 10 ac 3c 00 75  f4 c3 00 00 00 00 00 00  |.....<.u........|
000001b0  00 00 00 00 00 00 00 00  7d 50 d7 43 00 00 80 04  |........}P.C....|
000001c0  01 04 83 fe c2 ff 00 08  00 00 00 00 20 00 00 fe  |............ ...|
000001d0  c2 ff 83 fe c2 ff 00 08  20 00 00 00 80 0c 00 fe  |........ .......|
000001e0  c2 ff 83 fe c2 ff 00 08  a0 0c 00 00 40 06 00 fe  |............@...|
000001f0  c2 ff 05 fe c2 ff 00 08  e0 12 00 f8 1f 06 55 aa  |..............U.|
00000200
#View the first 512 bytes of the disk
#80 04 000001c0 01 04 83 Fe C2 FF 00 08 00 00 00 00 20 00 first partition information
#00 fe 000001d0  c2 ff 83 fe c2 ff 00 08  20 00 00 00 80 0c  	 Second partition information
#00 Fe 000001e0 C2 FF 83 Fe C2 FF 00 08 A0 0C 00 40 06 third partition information
#00 Fe 000001f0 C2 FF 05 Fe C2 FF 00 08 E0 12 00 F8 1F 06 fourth partition information

#Backup MBR partition table
[root@rocky8 ~]# dd if=/dev/sda of=/data/mbr bs=1 count=64 skip=446
64+0 records in
64+0 records out
64 bytes copied, 0.000170499 s, 375 kB/s
#Backup disk partition table 		 skip skips 446 bytes relative to if=/dev/sda and backs up 64 bytes

[root@rocky8 ~]# hexdump -C /data/mbr -v
00000000  80 04 01 04 83 fe c2 ff  00 08 00 00 00 00 20 00  |.............. .|
00000010  00 fe c2 ff 83 fe c2 ff  00 08 20 00 00 00 80 0c  |.......... .....|
00000020  00 fe c2 ff 83 fe c2 ff  00 08 a0 0c 00 00 40 06  |..............@.|
00000030  00 fe c2 ff 05 fe c2 ff  00 08 e0 12 00 f8 1f 06  |................|
00000040

#Destroy MBR partition table
[root@rocky8 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446
64+0 records in
64+0 records out
64 bytes copied, 0.000139151 s, 460 kB/s
#Destroy the partition table. seek skips 446 bytes and writes 64 bytes relative to of=/dev/sda

[root@rocky8 ~]# hexdump -C /dev/sda -n 512 -v
00000000  eb 63 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.c..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|
00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|
00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 05 7c  |. ..d|<.t...R..||
00000090  b4 41 bb aa 55 cd 13 5a  52 72 3d 81 fb 55 aa 75  |.A..U..ZRr=..U.u|
000000a0  37 83 e1 01 74 32 31 c0  89 44 04 40 88 44 ff 89  |7...t21..D.@.D..|
000000b0  44 02 c7 04 10 00 66 8b  1e 5c 7c 66 89 5c 08 66  |D.....f..\|f.\.f|
000000c0  8b 1e 60 7c 66 89 5c 0c  c7 44 06 00 70 b4 42 cd  |..`|f.\..D..p.B.|
000000d0  13 72 05 bb 00 70 eb 76  b4 08 cd 13 73 0d 5a 84  |.r...p.v....s.Z.|
000000e0  d2 0f 83 de 00 be 85 7d  e9 82 00 66 0f b6 c6 88  |.......}...f....|
000000f0  64 ff 40 66 89 44 04 0f  b6 d1 c1 e2 02 88 e8 88  |d.@f.D..........|
00000100  f4 40 89 44 08 0f b6 c2  c0 e8 02 66 89 04 66 a1  |.@.D.......f..f.|
00000110  60 7c 66 09 c0 75 4e 66  a1 5c 7c 66 31 d2 66 f7  |`|f..uNf.\|f1.f.|
00000120  34 88 d1 31 d2 66 f7 74  04 3b 44 08 7d 37 fe c1  |4..1.f.t.;D.}7..|
00000130  88 c5 30 c0 c1 e8 02 08  c1 88 d0 5a 88 c6 bb 00  |..0........Z....|
00000140  70 8e c3 31 db b8 01 02  cd 13 72 1e 8c c3 60 1e  |p..1......r...`.|
00000150  b9 00 01 8e db 31 f6 bf  00 80 8e c6 fc f3 a5 1f  |.....1..........|
00000160  61 ff 26 5a 7c be 80 7d  eb 03 be 8f 7d e8 34 00  |a.&Z|..}....}.4.|
00000170  be 94 7d e8 2e 00 cd 18  eb fe 47 52 55 42 20 00  |..}.......GRUB .|
00000180  47 65 6f 6d 00 48 61 72  64 20 44 69 73 6b 00 52  |Geom.Hard Disk.R|
00000190  65 61 64 00 20 45 72 72  6f 72 0d 0a 00 bb 01 00  |ead. Error......|
000001a0  b4 0e cd 10 ac 3c 00 75  f4 c3 00 00 00 00 00 00  |.....<.u........|
000001b0  00 00 00 00 00 00 00 00  7d 50 d7 43 00 00 00 00  |........}P.C....|
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200
#Now the partition table information is 0

[root@rocky8 ~]# lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda       8:0    0  200G  0 disk 
├─sda1    8:1    0    1G  0 part /boot
├─sda2    8:2    0  100G  0 part /
├─sda3    8:3    0   50G  0 part /data
├─sda4    8:4    0    1K  0 part 
└─sda5    8:5    0    2G  0 part [SWAP]
sr0      11:0    1  9.2G  0 rom  
nvme0n1 259:0    0   10G  0 disk 

[root@rocky8 ~]# fdisk -l
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43d7507d


Disk /dev/nvme0n1: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
#Partition table information is no longer visible

[root@rocky8 ~]# scp /data/mbr 172.31.0.7:/data
The authenticity of host '172.31.0.7 (172.31.0.7)' can't be established.
ECDSA key fingerprint is SHA256:zb/8mo/ptS0h8eHVY1FDRuvh6aQj1opzpsD7khnYjSo.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.31.0.7' (ECDSA) to the list of known hosts.
root@172.31.0.7's password: 
mbr                                                                                                          100%   64    31.4KB/s   00:00  
#Backup partition table information to another machine

#Unable to start
[root@rocky8 ~]# reboot


#The system can no longer start

#Start with the CD, enter rescue mode, and select item 3 skip to shell


#Prompt that there is no partition. Restart and press enter to get a shell


#I can't see the disk information now


#There is no IP address now


#Add an IP address

# ip a a 172.31.1.8/21 dev ens33


#Copy the files backed up on 10.0.0.7 to the local

# scp 172.31.0.7:/data/mbr .


#Now the file has been copied


#You can see that the partition table information is 0


#Restore partition table information

# dd if=mbr of=/dev/sda bs=1 count=64 seek=446


#Now the partition table information has been restored

# hexdump -C /dev/sda -n 512 -v


#Write the information to disk and restart


#Now the system can start normally

Question: use the partition table of another host with the same partition policy to restore and recover the broken partition table of the current host?

2.1.2.2 GPT zoning

GPT: GUID (global unique identifiers) partition table supports 128 partitions, 64 bits, 8Z (512Byte/block) and 64Z (4096Byte/block)

The 128 bit UUID (universal unique identifier) is used to represent the disk and partition. The GPT partition table is automatically backed up in the first and last copies, with CRC check bits

UEFI (Unified Extensible Firmware Interface) hardware supports GPT, so that the operating system can be started

GPT partition structure

The GPT partition structure is divided into four areas:

  • GPT head
  • Partition table
  • GPT partition
  • Backup area

2.1.3 BIOS and UEFI

BIOS is a program solidified on the computer motherboard. It is mainly used for system self-test and booting the operating system. At present, the new computers are basically started by UEFI

BIOS (Basic Input Output System) mainly completes the system hardware self-test and booting the operating system. After the operating system starts, the BIOS task is completed. System hardware self-test: if the system hardware fails, the speaker on the motherboard will emit "didi" sounds with different lengths, which can simply judge the hardware failure. For example, "1 long and 1 short" usually indicates memory failure, "1 long and 3 short" usually indicates graphics card failure

BIOS was born in 1975. It was written in assembly language. At the beginning, it was only 16 bits, so it can only access 1M memory. The first 640K is called basic memory, and the last 384K memory is reserved for startup and various BIOS itself. BIOS can only recognize the hard disk initialized by the master boot record (MBR), which can support up to 2T hard disk and four main partitions (except the extended partition in the logical partition). At present, 64 bit system is generally implemented, and the traditional BIOS can no longer meet the requirements. At this time, EFI dominated by Intel was born

EFI (Extensible Firmware Interface) is an extensible firmware interface proposed by Intel for the architecture, interface and service of PC firmware. Its main purpose is to provide a set of consistent and correctly specified startup services on all platforms before OS loading (before startup). It is regarded as the successor of BIOS or understood as the new version of BIOS.

UEFI is developed based on EFI1.10. Its owner is no longer Intel, but an international organization called Unified EFI Form

UEFI(Unified Extensible Firmware Interface) is a unified extensible firmware interface, which is a standard for describing type interfaces in detail. UEFI is equivalent to a lightweight operating system, providing an interface between hardware and operating system and a graphical operating interface. The key is the introduction of GPT partition table, which supports hard disks above 2T, and the hard disk partition is not limited

Differences between BIOS and UEFI

The BIOS is written in 16 bit assembly language and can only run in real mode (the memory addressing mode is that the content of the 16 bit segment register is multiplied by 16(10H) as the segment base address, plus the 16 bit offset address to form a 20 bit physical address). The accessible memory space is 1MB and only supports the character operation interface

UEFI is written in 32-bit or 64 bit C language, which breaks through the limitation of real mode, can achieve the maximum addressing space, support graphical operation interface, save information in file mode, support GPT partition startup, and is suitable for cooperation with newer systems and hardware

BIOS+MBR and UEFI+GPT


MSDN(Microsoft Developer Network) points out that Windows can only be installed on the combination of BIOS + MBR or UEFI + GPT, while BIOS + GPT and UEFI + MBR are not allowed. However, it is possible to start Linux with BIOS + GPT + GRUB

Topics: Linux Operation & Maintenance architecture DevOps Cloud Native