1 Disk Basics

1.1 Physical structure of hard Disks

Disk: A hard disk has multiple disks. Each disk has two sides (front and back).

Head: One head on each side. (There is not just one head)

1.2 Data Structure of hard Disks

  • Sectors: The disk is divided into sectors, each holding 512 bytes of data, the smallest unit of storage on a hard disk

  • Track: Concentric circles of different radii on the same disk. They are circular tracks drawn on the surface of the disk by the magnetic head to facilitate data storage

  • Cylinder: A cylindrical surface of the same radius of different disks, consisting of multiple tracks in a circle of the same radius

1.3 Disk Capacity

  • Disk storage capacity = Number of magnetic heads x Number of tracks (cylinders) x Number of sectors x Number of bytes per sector (512 bytes)
  • Cylinder/head sectors can be used to uniquely locate each region on the disk

1.4 Type of Disk interface

  • IDE (parallel port) : interface speed 133MB/s, parallel interface, early home computers. Parallel data line connecting motherboard and hard disk, anti-interference is poor, and the row takes up a large space, adverse to the internal heat dissipation of the computer, has been gradually replaced by SATA.
  • SCSI (parallel port) : interface speed 640MB/s, parallel interface, early server. System interface of minicomputer, SCSI hard disk is widely used by workstation level PC and server, low CPU usage during data transmission, fast speed, support hot swap, etc.
  • SATA (serial port) : interface speed 6Gpb/s, parallel interface, early server. Strong anti-interference, support hot swap and other functions, fast speed, strong error correction ability.
  • SAS: a new generation of SCSI technology, the same as SATA disk, adopts serial technology to achieve higher transmission speed, up to 6Gb/s. SAS is a whole cable. Data ports and power ports are integrated. SAS contains power cables, while SATA does not. SATA is actually a subset of SAS. SATA disks can be inserted into the SAS motherboard, but not vice versa.

1.5 Mechanical Hard Disks and SOLID State Disks

Mechanical hard disk (HDD) :

A Hard Disk Drive consists of a Disk, a magnetic head, a Drive shaft, a motor, a magnetic head controller, a data converter, interfaces, and a cache. The magnetic head can move along the radius of the disk, and with the disk rotating at a high speed of several thousand revolutions per minute, the magnetic head can be positioned at the specified position of the disk for data reading and writing operations. Messages are written to the disk by an electromagnetic current that changes the polarity of a magnetic head close to the magnetic surface and can be read in reverse. Hard disks as precision equipment, dust is its enemy, so the air into the hard disk must be filtered.

Solid State Drives (SSDS) :

A Solid State Disk or Solid State Drive, also known as a Solid State Drive, is a hard Disk made from an array of solid-state electronic memory chips. Solid-state drives (SSDS) are named after the Solid capacitance in Taiwan English. SSDS consist of a control unit and a storage unit (FLASH chip and DRAM chip). Solid state disks (SSDS) are the same as common hard disks in terms of interface specifications, definitions, functions, and usage methods. They are basically the same as common hard disks in terms of product appearance and size.

Advantages and disadvantages of mechanical hard disk and SOLID state disk:

  1. Shock resistance: Mechanical hard disks are disk type, data stored in the disk sector. And solid-state drives is to use flash particles (i.e., memory, MP3, U disk, such as storage medium), so there are no mechanical components inside SSD solid state hard drive, so that even in high-speed mobile even with flip tilt also won’t affect the normal use, and in the case of collisions and shocks to minimize the possibility of data loss. Compared with mechanical hard disk, solid hard has absolute advantage.
  2. Data storage speed: the speed of a mechanical disk is 120MB/S, the speed of a SATA SSD is 500MB/S, the speed of an NVMe (PCIe 3.0 x 2) SSD is 1800MB/S, and the speed of an NVMe (PCIe 3.0 x 4) SSD is 3500MB/S.
  3. Power consumption: Solid-state drives also consume less power than mechanical drives.
  4. Weight: Solid-state drives are lighter in weight, weighing 20-30 grams less than regular 1.8-inch drives.
  5. Noise: due to the solid hard belongs to no mechanical parts and flash chips, so it has the characteristics of low heat, fast heat dissipation, and no mechanical motor and fan.

Common hardware devices are in /dev:

[root@localhost ~]# ll /dev/sd* // 8 indicates the device type. 1 root disk 8, 0 2月 24 18:36 /dev/sda brw-rw----. 1 root disk 8, 1 2月 24 18:36 /dev/sda1 brw-rw----. 1 root disk 8, 2 2月 24 18:36 /dev/sda2 brw-rw----. 1 root disk 8, 16 2月 25 00:03 /dev/sdb brw-rw----. 1 root disk 8, 17 2月 25 00:03 /dev/sdb1 brw-rw----. 1 root disk 8, 18 2月 25 00:03 /dev/sdb2 brw-rw----. 1 root disk 8, 21 2月 25 00:03 /dev/sdb5 brw-rw----. 1 root disk 8, 22 February 25 00:03 /dev/sdb6Copy the code

2 MBR and disk partition structure

2.1 Master Boot Record (MBR)

  • The MBR is located in the first physical sector of the hard disk. The MBR contains the main boot program and partition table of the hard disk
  • The first sector totals 512 bytes, the first 446 bytes are the master boot record, and the partition table is stored in bytes 447-512 in the sector.
  • The partition table has four partition record areas, each of which is 16 bytes.

2.2 Disk Partition Representation

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

The Linux kernel reads resources, such as cd-rom drives and hard disks, in the form of device files. Therefore, hard disks and partitions are represented as different files.

Said: / dev/hda5

  • /dev/indicates the directory where the hardware device files reside
  • Hd: IDE device (SD: SCSI device)
  • A: Serial number of a hard disk. The serial number can be a, B, C… said
  • 5: partition number, indicating the fifth partition of the first hard disk interface

2.3 Disk Partition Structure

  • The number of primary partitions in the hard disk is only 4
  • The serial number of the primary partition and extended partition is limited to 1 to 4
  • Extended partitions are subdivided into logical partitions
  • The serial number of the logical partition will always start at 5

3 Type of the file system used in Linux

In a computer, a file system is a system that names files and places the logical storage and recovery of files.

The type of file system determines how and efficiently data can be stored and read into partitions.

3.1 XFS File System

  • The default file system in CenOS 7 is a high-performance log file system
  • A partition that holds file and directory data
  • Data integrity: Quick recovery of disk file contents in a very short time based on the logs recorded
  • Transport characteristics: With optimization algorithms, logging has very little impact on the overall file operation. Querying and allocating storage space is very fast.
  • Scalability: A full 64-bit file system that supports up to 8 exabytes of file systems
  • Transmission bandwidth: XFS can store data with performance close to raw device I/O. Read and write operations on a single file with throughput up to 4GB per second.

3.2 Swap Swapping a File system (Virtual Memory)

  • The swap partition is a supplement to the system RAM. The swap partition supports virtual memory.
  • When there is not enough RAM to store the data processed by the system, the data is written to the swap partition. When the system runs out of swap space, the kernel terminates the process due to running out of RAM.
  • If too much swap space is configured, the storage device is allocated but idle, resulting in waste. In addition, too much swap space may cover the problem of insufficient memory.

3.3 FAT16, FAT32

  • FAT32 refers to the disk file management mode that adopts 32-bit binary number record management. As the core of the FAT file system is the file allocation table, the name is derived from this mode.
  • FAT32 is developed from FAT and FAT16. Its advantages are stability and compatibility. It is fully compatible with Win 9X and previous versions and easy to maintain.
  • The disadvantages are poor security, and can only support a maximum of 32GB partition, a single file can only support a maximum of 4GB.

3.4 NTFS

NTFS file system has three functions: error warning function, disk self-repair function and log function.

3.5 EXT4 (Extended File System 4)

  • Default file system in CenOS 6
  • EXT4 is the latest version of the EXT file system. It provides many features, including nanosecond timestamps, creating and using huge files (16TB), file systems up to 1 exabyte, and speed improvements.
  • This applies to those where the partition capacity is not too large and updates are infrequent, such as the /boot partition.

3.6 JFS

It is mainly designed and developed to meet the high throughput and reliability requirements of the server. The maximum capacity of a single file is 16TB, and the file system supports a maximum of 1PB.

View the file system types supported by the current system.

[root@localhost ~]# cat /proc/filesystems // View the filesystem types supported by the system. Nodev sysfs nodev rootfs nodev ramfs nodev bdev nodev proc nodev cgroup nodev cpuset nodev tmpfs nodev devtmpfs nodev debugfs nodev securityfs nodev sockfs nodev pipefs nodev anon_inodefs nodev configfs nodev devpts nodev hugetlbfs nodev autofs nodev pstore nodev mqueue nodev selinuxfs xfs nodev rpc_pipefs nodev nfsdCopy the code

Manage disks and partitions – partition tool FDsik

Fdisk command format:

Fdisk -l [disk device] // View disk partitions interactively fdisk [disk device] // View and manage disk partitions interactivelyCopy the code

4.1 Viewing Disks and Partitions In a Non-interactive manner

Example:

[root@localhost ~]# fdisk -l /dev/sda // Units = Sector of 1 x 512 = 512 bytes Sector size (logical/physical) : 512 bytes / 512 bytes I/O size (minimum/Best) : 512 bytes / 512 bytes Disk label type: DOS Disk Identifier: 0x000C4763 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 31467519 14684160 8e Linux LVMCopy the code

4.2 Viewing and Managing Disk Partitions Interactive

Common instructions in interactive mode:

  • M Print out the menu (help list)
  • P Prints the current partition table
  • N Create a partition
  • D Delete a partition
  • T Changes the format and system ID of the partition
  • W to save
  • Q exit

After the partition is set, you can run the partprobe command to make the kernel read partition information (that is, refresh the partition list) again to avoid restarting the system.

Example:

[root@localhost ~]# fdisk /dev/sda/fdisk (util-linux 2.23.2) Changes stay in memory until you decide to write them to disk. Think twice before using write commands. Command (enter m for help) : M // M command Print menu command operation a toggle a bootable flag b edit BSD disklabel c toggle the DOS compatibility flag d delete a partition  g create a new empty GPT partition table G create an IRIX (SGI) partition table l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify The partition table w write table to disk and exit x extra functionality (experts only) command (enter m for help) : The p //p command prints the current partition table disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 Sector Units = Sector of 1 * 512 = 512 bytes Sector size (logical/physical) : 512 bytes / 512 bytes I/O size (minimum/best) : 512 bytes / 512 bytes Disk label type: DOS Disk Identifier: 0x000C4763 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 31467519 14684160 8e Linux LVM command (enter m for help) : the q //q command exitsCopy the code

The full process of setting up partitions is demonstrated in Section 7 below.

5 Creating a File System (Formatting)

5.1 Creating a File System — run the MKFS command

Make Filesystem creates a Filesystem (formatting).

MKFS command format:

MKFS -t File system type Partition device mkfs. file system type Partition device // In both command formats, the partition device location must be an absolute pathCopy the code

Example:

[root@localhost ~]# ls /sbin/mkfs* // View the supported file system types /sbin/mkfs /sbin/mkfs.ext2 /sbin/mkfs.fat /sbin/mkfs.vfat /sbin/mkfs.btrfs /sbin/mkfs.ext3 /sbin/mkfs.minix /sbin/mkfs.xfs /sbin/mkfs.cramfs /sbin/mkfs.ext4 /sbin/mkfs.msdos [root@localhost ~]# mkfs. XFS /dev/sdb1 // Format sdb1 into XFS file system meta-data=/dev/sdb3 isize=512 agcount=4, agsize=655360 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=2621440, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0Copy the code

5.2 Creating a Swap File System — run the mkswap command

Format of the mkswap command:

Swapon Partition device // Enable swap partition swapoff partition device // Disable swap partition swapon -s // Display all swap partitions that are enabledCopy the code

Example:

[root@localhost ~]# mkswap /dev/sdb6 // format sdb6 as swap UUID=2b59e021-35a5-489a-9450-849917a9850a [root@localhost ~]# swapon-s /dev/dm-1 partition 4194300 0-1 [root@localhost ~]# swapon /dev/sdb6 // Switch partition enabled [root@localhost ~]# swapon -s /dev/dm-1 partition 4194300 0-1 /dev/sdb6 partition 6289404 0-2 [root@localhost ~]# [root@localhost ~]# swapon -s // /dev/sdb6 No file name Type Size Used Permission /dev/dm-1 partition 4194300 0-1Copy the code

6 Mount and unmount the file system

6.1 Manual Mounting (Mounting the Vm at one time and canceling the mounting relationship after the vm is powered off)

6.1.1 Mounting a File System the mount command is used

Using the mount command is a one-time mount. The mount relationship will be cancelled after the machine is powered off. You need to mount it again when you start the machine next time.

Format of the mount command:

Mount [-t file system type] Mount point directory of the storage device mount -o loop Mount point directory of the ISO image file // Mount the ISO image to the specified folderCopy the code

Example:

[root@localhost data]# mount sdb1 to aa directory [root@localhost ~]# df -h File system capacity successfully mounted Available Used % Mount point /dev/mapper/centos-root 10G 4.9G 5.2g 49% / devtmpfs 897M 0 897M 0% /dev TMPFS 912M 0 912M /dev/shm TMPFS 912M 9.1m 903M 1% /run TMPFS 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 179M 836M 18% /boot TMPFS 183M 0 183M 0% /run/user/0 TMPFS 183M 40K 183M 1% /run/user/1005 /dev/sr0 4.3g 4.3g 0 100% /run/media/Amy/CentOS 7 x86_64 /dev/sdb1 10G 33M 10G 1% /data/aaCopy the code

Note:

  • The mount directory must exist in advance.
  • It is best to mount an empty directory, otherwise the original files under the mount point may be lost or hidden.
  • The mount point directory is not available to other processes.
  • Multiple devices cannot be mounted to a directory at the same time.
  • A partitioned device cannot be mounted to multiple directories at the same time.

6.1.2 Unmounting a file system — run the umount command

Format of the umount command:

Umount Location of the storage device Umount Mount point directoryCopy the code

Example:

[root@localhost data]# umount /dev/sdb1 [root@localhost data]# df -h // File system capacity unmounted Used Available % Mount point /dev/mapper/centos-root 10G 4.9G 5.2g 49% / devtmpfs 897M 0 897M 0% /dev TMPFS 912M 0 912M /dev/shm TMPFS 912M 9.1m 903M 1% /run TMPFS 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 1014M 179M 836M 18% /boot TMPFS 183M 0 183M 0% /run/user/0 TMPFS 183M 40K 183M 1% /run/user/1005 /dev/sr0 4.3g 4.3g 0 100% /run/media/Amy/CentOS 7 x86_64Copy the code

Note:

If the directory is in the mount directory, the mount cannot be unmounted. You need to change the directory and unmount it.

6.2 Configuring Automatic Mounting (Permanent Mounting) for a File System

After you run the mount command to manually mount the file device, you must write the mounting information to the /etc/fstab file. Otherwise, you need to mount the file again at the next startup.

The /etc/fstab file in the system can be regarded as the configuration file of the mount command, which stores the statically mounted data of the file system. Linux automatically reads the contents of the file upon each startup and automatically mounts the specified file system based on the configuration in the file. The default fstab file contains mounting configurations of the root partition, /boot partition, swap partition, and pseudo file systems such as proc and TMPFS.

View the contents of the /etc/fstab file:

[root@localhost ~]# create by anaconda on Tue Jan 18 17:29:36 2022 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=87263b93-1e10-49c6-a30c-2b80b6b17038 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0Copy the code

In the /etc/fstab file, each row records the mount configuration information of a partition or device. From left to right, six fields (separated by Spaces or tabs) are recorded. Take /dev/mapper/centos-root/XFS defaults 0 0 as an example. The meanings are described as follows:

  • Field 1: Device name or volume label name, or UUID of the device (UUID is recommended and can be queried by running lsbik).
  • Field 2: Mount point directory location of the file system.
  • Field 3: File system type, such as XFS and swap.
  • Field 4: Mount parameters, which are available after the “-o” option of the mount command. For example, default, RW, ro, and noexec are default parameters, readable and writable, read-only, and disabled executables respectively.
  • Field 5: Indicates whether the file system requires dump backup (dump is a backup tool). Generally, if the value is set to 1, it is required. If the value is set to 0, it is ignored by dump.
  • Field 6: This number determines the order in which disk checks are performed at system startup. 0 indicates that no check is performed, 1 indicates that the check is performed first, and 2 indicates that the check is performed second. You can set the root partition to 1 and other partitions to 2. Otherwise, the system performance may deteriorate.

Example:

Edit the /etc/fstab file and enable /dev/sdb1 to be automatically mounted to the /data/aa directory after each startup

[root@localhost ~]# blkid // check the UUID of the hardware device /dev/sda1: UUID="87263b93-1e10-49c6-a30c-2b80b6b17038" TYPE=" XFS "/dev/sda2: UUID="NOm6dB-M1Oe-2DBL-JyAF-p9by-VTLr-14MAfJ" TYPE="LVM2_member" /dev/sdb1: UUID="e0b714cd-c33e-42b2-a051-1e1f3333b4b7" TYPE="xfs" /dev/sdb5: UUID="f9f897a8-ae5d-4ec3-a48f-33e2757213ae" TYPE="xfs" /dev/sdb6: UUID="2b59e021-35a5-489a-9450-849917a9850a" TYPE="swap" /dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" /dev/mapper/centos-root: UUID="d0d8117f-e6af-46de-985e-910fafd9d4a2" TYPE="xfs" /dev/mapper/centos-swap: UUID=" 8a01EFF6-7570-42db-ba4d-a7ff11acbdb3 "TYPE="swap" [root@localhost ~]# vim /etc/fstab // UUID=e0b714cd-c33e-42b2-a051-1e1f3333b4b7 /data/aa xfs defaults 0 0Copy the code

Note:

When modifying the /etc/fstab file, ensure that each field is filled in correctly. If any error occurs, the next startup fails due to a Control-d error message.

7 Complete the procedure for configuring Disk Partitions

Experiment content:

1) Add and check a new hard disk: Add a 20 GB hard disk for the host.

2) Partition the hard disk into one primary partition (10 GB) and one extended partition (8 GB). Two logical partitions are created in the extended partition with the capacity of 2 GB and 6 GB respectively.

3) Create a file system: format the primary partition and the first logical partition as XFS file systems. The second logical partition is formatted as a swap file system.

4) Mount the file system: Mount the primary partition to /data/aa and the first logical partition to /data/bb.

Experimental steps:

Step 1 Check and confirm the new hard disk

After a new disk is added, refresh the disk interface so that the system can identify the new disk. The command is as follows:

[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host0/scan [root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host1/scan [root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host2/scan [root@localhost ~]# LSBLK // View block devices, SDB NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8 0 0 0 0 disk ├─sda1 8 0 1 1 0 part /boot ├─ sda2 8 0 0 0 0 Part ├ ─ centos - root 253:0 0 0 LVM / 10 G └ ─ centos - swap 253:1 0 4 G 0 LVM/swap SDB he 0 0, 20 G disk sr0 11:0 1 4.2 G 0 ROMCopy the code

Step 2: Use the fdisk tool to configure disk partitions

Step 3: Specify a file system for the partition (formatting)

1) Format /dev/sdb1 and /dev/sdb5 as XFS file systems — mkfs.xfs command

[root@localhost ~]# mkfs. XFS /dev/sdb1 // Format sdb1 as XFS file system [root@localhost ~]# mkfs. XFS /dev/sdb5 // Format sdb5 as XFS file systemCopy the code

2) Format /dev/sdb6 as a swap file system — mkswap

[root@localhost ~]# mkswap /dev/sdb6 // format sdb6 as swap UUID=2b59e021-35a5-489a-9450-849917a9850a [root@localhost ~]# swapon-s /dev/dm-1 partition 4194300 0-1 [root@localhost ~]# swapon /dev/sdb6 // Switch partition enabled [root@localhost ~]# swapon -s /dev/dm-1 partition 4194300 0-1 /dev/sdb6 partition 6289404 0-2 [root@localhost ~]# [root@localhost ~]# swapon -s // /dev/sdb6 No file name Type Size Used Permission /dev/dm-1 partition 4194300 0-1Copy the code

Step 4: Mount the file system

Create /data directory and subdirectories aa and bb.

Mount SDB1 and Sdb5 to AA and BB respectively.

Method 1: Run the mount command to mount the vm at one time

[root@localhost /]# mkdir /data // create /data directory [root@localhost /]# CD /data [root@localhost data]# mkdir aa bb [root@localhost data]# ls aa bb [root@localhost data]# mount /dev/sdb1 /data/aa // Create subdirectories aa and bb under /data [root@localhost data]# mount /dev/sdb5 /data/bb // File system type Capacity Used Available Used % Mount point /dev/mapper/centos-root XFS 10G 4.9G 5.2g 49% / devtmpfs devtmpfs 897M 0 897M 0% /dev TMPFS TMPFS 912M 0 912M 0% /dev/shm TMPFS TMPFS 912M 9.1m 903M 1% /run TMPFS TMPFS 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 179M 836M 18% /boot tmpfs tmpfs 183M 0 183M 0% /run/user/0 tmpfs tmpfs 183M 40K 183M 1% /run/user/1005 /dev/sr0 iso9660 4.3g 4.3g 0 100% /run/media/Amy/CentOS 7 x86_64 /dev/sdb5 XFS 2.0g 33M 2.0g 2% /data/bb /dev/sdb1 xfs 10G 33M 10G 1% /data/aaCopy the code

Method 2: Modify the /etc/fstab file to enable automatic mounting. Be sure to check carefully before saving.

[root@localhost ~]# blkid // check the UUID of the hardware device /dev/sda1: UUID="87263b93-1e10-49c6-a30c-2b80b6b17038" TYPE=" XFS "/dev/sda2: UUID="NOm6dB-M1Oe-2DBL-JyAF-p9by-VTLr-14MAfJ" TYPE="LVM2_member" /dev/sdb1: UUID="e0b714cd-c33e-42b2-a051-1e1f3333b4b7" TYPE="xfs" /dev/sdb5: UUID="f9f897a8-ae5d-4ec3-a48f-33e2757213ae" TYPE="xfs" /dev/sdb6: UUID="2b59e021-35a5-489a-9450-849917a9850a" TYPE="swap" /dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" /dev/mapper/centos-root: UUID="d0d8117f-e6af-46de-985e-910fafd9d4a2" TYPE="xfs" /dev/mapper/centos-swap: UUID=" 8a01EFF6-7570-42db-ba4d-a7ff11acbdb3 "TYPE="swap" [root@localhost ~]# vim /etc/fstab // UUID=e0b714cd-c33e-42b2-a051-1e1f3333b4b7 /data/aa xfs defaults 0 0 UUID=f9f897a8-ae5d-4ec3-a48f-33e2757213ae /data/bb xfs defaults 0 0Copy the code