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 disk partition status lsblk, blkid

  1. lsblk (list block device): List all disks on the system
lsblk [options] [device]
# Common options
# -d, --nodeps Only lists the disk itself and does not print slave devices or holders.
# -f, --fs output file system information
# -i, --ascii Use only ascii characters
# -m, --perms output permission information
# -p, --paths print full device paths
# -t, --topology Output topology information

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

NAME: The file name of the device, the /dev leading directory will be ignored
MAJ:MIN: Device recognized by the kernel
RM: Is it an uninstallable device?
SIZE: Capacity
RO: Is it a read-only device?
TYPE: Is it a disk (disk), a partition (partition), or a read-only memory (rom), etc.
MOUNTPOINT: mount point

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

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 tables use fdisk partitions, and GPT partitions use gdisk partitions.

(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, 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 partitionsin 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 has a much larger hard drive size with almost no limit, supporting up to 18EB. The number of primary partitions is also very large, and it can support up to 128 primary partitions. Therefore, there is no concept of extended partitions and logical partitions in GPT. They are all primary partitions. Of course, they can be created, but it is not necessary.
    GPT also has disadvantages, 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:

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 # List file systems of known hard disk partitions

(3) Use fdisk to partition the new hard disk

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

fdisk /dev/sdb # List the partition table under /dev/sdb
# Common command operations (use the m option to view other operations)
# d delete a partition Delete a disk partition
# m print this menu displays available options
# n add a new partition Create a new disk partition
# p print the partition table Display partition table information
# q quit without saving changes Exit and save changes
# t change a partition's system id Set the system number for the partition
# w write table to disk and exit Write, save 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

Second primary partition /dev/sdb2

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

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.

3. Disk formatting (creating file system)

File system formatting command: mkfs (make filesystem)
Linux file system formatted as ext4

mkfs.ext4 [-b size] [-L labelable] device name
# -b: Set the block size, 1K, 2k, 4k
# -L: Set the header name of the device

Format all three primary partitions in sequence: mkfs.ext4 /dev/sdb number

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

4. Check the file system (check only when errors occur, not when normal)

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

fsck.ext4 [-pf] [-b superblock] device name

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 the file system, the contents of the original directory will temporarily disappear, and the original contents will appear after the mount point is uninstalled.

mount command to mount

 mount [-lhV]
 mount -a [options]
 mount [options] [--source] <source> | [--target] <directory>
 mount [options] <source> <directory>
 mount <action> <mountpoint> [<target>]

umount command to uninstall

 umount [-hV]
 umount -a [options]
 umount [options] <source> | <directory>

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

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

Uninstall it and use the df command to view it again

syntaxbug.com © 2021 All Rights Reserved.