Disk partitioning, formatting, verification and mounting ---- fdisk, mkfs, mount

Disk partitioning, formatting, verification and mounting

Disk management is very important. When we want to add a new disk to the system, we should perform the following steps:

  1. Divide the disk to create usable hard disk partitions (fdisk/gdisk)
  2. Format the hard drive partition to create a system-usable file system (mkfs)
  3. Check the newly created file system (fsck)
  4. On the Linux system, establish a mount point and mount it (mount ; umount )

1. Observe the disk partition status lsblk, blkid

  1. lsblk (list block device): List all disks on the system
lsblk [选项] [device]
# 常用选项
# -d, --nodeps         仅列出磁盘本身不打印从属设备(slave)或占位设备(holder)
# -f, --fs             输出文件系统信息
# -i, --ascii          只使用 ascii 字符
# -m, --perms          输出权限信息
# -p, --paths          打印完整设备路径
# -t, --topology       输出拓扑信息

The picture below is the default disk list in my virtual machine:

Insert image description here

NAME: The file name of the device, the /dev leading directory will be ignored
MAJ:MIN: The device recognized by the kernel
RM: Whether it is unmountable device
SIZE: capacity
RO: whether it is a read-only device
TYPE: whether it is a disk (disk) or a partition ( partition), or read-only memory (rom) and other output
MOUNTPOINT: mount point

  1. blkid: Lists the device's UUID, device name, file system type and other parameters
    UUID is a globally unique identifier. Linux will assign a unique identifier to all devices in the system. The identifier can be used as a mount or to use this device or file system.

Insert image description here

2. Disk partition gdisk/fdisk

Currently, there are two main disk partition formats: MBR and GPT. The partitioning tools used in these two formats are different.
MBR partition table uses fdisk partition, and GPT partition uses gdisk partition.

(1) The concept of GPT partition and MBR partition

  1. MBR partition
    MBR means "Master Boot Record". It is called the "master boot record" because it is a special boot sector that exists at the beginning of the drive. This sector contains the installed operating system's boot loader and the drive's logical partition information. The so-called boot loader is a small piece of code used to load larger loaders on other partitions on the drive. If the MBR is overwritten, the operating system cannot start.
    MBR supports disks up to 2TB, but it cannot handle disks with a capacity larger than 2TB. MBR only supports up to 4 primary partitions - if you want more partitions, you need to create so-called "extended partitions" and create logical partitions within them.

  2. GPT partition
    GPT means GUID partition table. (GUID means Globally Unique Identifier).
    This is a new standard that is gradually replacing MBR. It and UEFI complement each other - UEFI is used to replace the old BIOS, while GPT replaces the old MBR. It’s called a “GUID Partition Table” because each partition on your drive has a globally unique identifier (GUID)—a randomly generated string that’s guaranteed to be Every GPT partition on a GPT partition is assigned a completely unique identifier.
    On MBR disks, partition and boot information are saved together. If this part of the data is overwritten or corrupted, things could be in trouble. In contrast, GPT saves multiple copies of this part of the information on the entire disk, so it is more robust and can recover this part of the information if it is damaged.
    GPT’s hard disk size is much larger, with almost no limit and can support up to 18EB. The number of primary partitions is also very large, and it can support up to 128 primary partitions, so there is no concept of extended partitions and logical partitions in GPT, they are all primary partitions, and of course they can be created. , but it's not necessary.
    GPT also has shortcomings, that is, it is not suitable for x86, that is, 32-bit systems.

(2) Add a new hard disk to an existing virtual machine

Here, first mount a new hard disk, find the virtual machine settings, click Add, and then basically take the default next step. The complete process is as follows:

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

After adding a new hard disk, start the virtual machine and use fdisk to check the information of the newly added hard disk (switch to root for all following commands)

fdisk -l # 列出已知硬盘分区的文件系统

Insert image description here

(3) Use fdisk to partition the new hard disk

Divide the newly added /dev/sdb into 3 primary partitions, each with 2GB, 3GB, and 5GB disk space.

fdisk /dev/sdb  # 列出/dev/sdb下的分区表
# 常用命令操作 (其他操作自行使用m选项进行查看)
#    d   delete a partition             删除一个磁盘分区
#    m   print this menu                显示可选的选项
#    n   add a new partition            新建一个磁盘分区
#    p   print the partition table      显示分区表格信息
#    q   quit without saving changes    退出并且保存修改
#    t   change a partition's system id 给分区设置系统号
#    w   write table to disk and exit   写入保存并退出

Tips: When creating a new partition, use the default starting address of the partition and assign the sector size by yourself.

The first primary partition /dev/sdb1

Insert image description here

Second primary partition /dev/sdb2
Insert image description here

The third primary partition /dev/sdb3 (remember to use the w option to save)
Insert image description here

After creating the three primary partitions, check again (fdisk -l hard disk name) and you can see that the three primary partitions have been created successfully.
Insert image description here

3. Disk formatting (creating file system)

File system formatting command: mkfs (make filesystem)
Format the Linux file system to ext4

mkfs.ext4 [-b size] [-L lable] 设备名称
# -b :设置区块的大小,1K,2k,4k
# -L :设置设备的标头名称

Format all three primary partitions in sequence: mkfs.ext4 /dev/sdb number
Insert image description here

After formatting one by one, use blkid to check. You can see that the three primary partitions under /dev/sdb have been formatted.
Insert image description here

4. Check the file system (check only if there is an error, not if it is normal)

Here, use fsck.ext4 to check and process the ext4 file system

fsck.ext4 [-pf] [-b 超级区块] 设备名称

tips: Only the super user root can use this command, and it will only be used when there is a problem with your file system. Under normal circumstances, using this command will cause harm to the system.
In addition, the hard disk partition being checked cannot be mounted on the system (it needs to be unmounted to check)

5. File system mounting and unmounting

The mount point is a directory, which is the entrance to the disk partition (file system).

  1. A single file system should not be mounted repeatedly on different mount points (directories)
  2. A single directory should not be mounted repeatedly on different mount points (directories)
  3. The directories to be used as mount points should theoretically be empty directories
    a. If the directory you use to mount is not empty, then when you mount After the file system is removed, the contents of the original directory will temporarily disappear, and the original contents will appear after the mount point is unmounted

mount command to mount

 mount [-lhV]
 mount -a [选项]
 mount [选项] [--source] <> | [--target] <目录>
 mount [选项] <> <目录>
 mount <操作> <挂载点> [<目标>]

umount command to uninstall

 umount [-hV]
 umount -a [选项]
 umount [选项] <> | <目录>

Use the /home/robin/data directory as the mount point for the first primary partition /dev/sdb1 of /dev/sdb
Insert image description here

After mounting, the mounting information of /home/robin/data changes and is mounted correctly.
Insert image description here

Uninstall it and use the df command to view it again
Insert image description here


Guess you like

Origin blog.csdn.net/m0_63622279/article/details/134338443
Recommended