Article From:

If the storage space of the current host is insufficient, it is necessary to add a hard disk for expansion. This requires partitioning and formatting the newly added hard disk.

Before partitioning hard disks, let’s look at two kinds of hard disk partitioning table MBR and GPT.


Partition tables are very important for a hard disk. Partition tables tell the hard disk how to read and write data. Once partition tables are destroyed, the data on the hard disk can not be manipulated. At present, MBR and GPT are two main forms of hard disk partition tables, and GPT is gradually replacing MBR.

1.1、Master Boot Record MBR

MBR,Main Boot Record,Is located in the front of the hard disk boot code. It is responsible for judging the legitimacy of the partition when the disk is read and written, locating the partition boot information, and generating the partition boot information when initializing the hard disk.

Usually, we call the sector containing MBR boot code the primary boot sector. Because boot code takes up most of the space in this sector, this sector is called MBR sector (MBR for short). Because this sector undertakes management functions different from other common spaces on disk, itIt does not belong to any partition on disk, so the partition formatting command cannot clear the master boot record information. MBR consists of three parts, which occupy 512 bytes:

  1. The master boot record MBR, 446 bytes, is used to transfer system control to an operating system specified by the user and registered in the partition table when the hard disk starts.
  2. The disk partition table entry DPT, 64 bytes, is responsible for explaining the partition situation on the disk.
  3. End flag, 2 bytes, with AA55 value, looks like 55AA because the low is in the first place and the high is in the second place when storing.

MBRDisadvantage: Because the partition table entries in MBR only account for 64 bytes, and each partition table has 16 bytes, so only four partitions can be partitioned, more than four partitions must use the scheme of primary partition + extended partition; MBR can only support 2TB hard disk, and more than 2TB will not be recognized.

1.2、GUIDDisk partition table GPT

GPT,GUID Partition Table,The globally unique identification disk partition table is part of the EFI standard for extensible firmware interfaces and is used as an alternative to MBR. GPT has no limitation on the number of partitions and can manage up to 18EB hard disks; since it is part of the EFI standard, if you use GPT format disks as system disks, you need U.EFI motherboard and operating system support, non-system disk is not required; in addition, GPT only supports 64-bit operating system.

With the expansion of hard disk capacity and the popularity of UEFI, GPT is replacing MBR as the mainstream.

2、Common Disk Partitioning Tools

A hard disk can only be used if it is partitioned. Here are two commonly used disk partitioning tools under Linux, fdisk and parted. Fdisk only supports MBR, not GPT. If you need to create a partition table in GPT format, you need to use parted.

This paper introduces the use of two tools by taking an example of adding a 20GB disk to the current host. The current operating system is CentOS 7.5.1804.


fdiskIt is the next powerful disk partition management tool in Linux. It can be used to view disk usage or partition disks. Only partition tables in MBR format are supported, so hard disks larger than 2TB are not supported.

fdiskThere are two ways to use query mode and interaction mode.

When looking at disk usage, we usually use:

fdisk -l

When partitioning a hard disk is required, the following commands are usually used to enter interactive mode:

fdisk Device name

Common functional options in interactive mode:

  • d:Delete a partition
  • l:View the supported disk partition format
  • m:Help
  • n:Add a new partition
  • p:display partition information
  • q:Do not save exit
  • t:Change partition type
  • w:Save exit

Let’s first look at the current disk usage:


Currently, the host has only one hard disk, which is / dev / sda. Here we add a 20GB hard disk to it and see:


Here you can see the newly added hard disk (at the red box), the device name is / dev / sdb, and then use the fdisk tool to divide it into two partitions, one partition 10G, the remaining space to another partition, first enter the interaction mode of fdisk:


Enter n to create a new partition:


First, we are prompted to create a primary partition or an extended partition, and the number of current partitions will be identified. We enter p to create a primary partition:


We are prompted to set the partition number, where the default is used for direct return:


Set the starting sector of the partition and return directly using the default:


Set the end sector, the current partition size of 10GB, here according to the prompt input + 10G:


Hint that we have created a 10GB partition to see the current partition status:


You can see that the partition has been created successfully, as / dev / sdb1. Next, repeat the above actions and continue to divide another partition. After partition is completed, enter w to save and exit.


partedThere are also two ways to use query mode and interaction mode.

When looking at disk usage, we usually use:

parted -l

When partitioning a hard disk is required, the following commands are usually used to enter interactive mode:

parted Device name

Commonly used interactive mode commands:

  • mklabel:Create partition tables to set which partition table format the disk uses. If using GPT: mklabel GPT
  • mkpart:Create a new partition with the command format ofmkpart The starting and ending position of partition type [file system type].Partition type is primary partition, extended partition and logical partition; file system type is generally not specified, and partition is formatted with mkfs command after completion of partition.
  • print:Output partition information, abbreviated as P. The command has three options.freeDisplay all information about the current disk and display the remaining space.numberDisplay the information of the specified partition;allOr listDisplay all disk information
  • rm:delete a partition
  • select:Select the hard disk.
  • quit:Quit (Parted automatically saves changes to hard disk), abbreviated as Q

Let’s first look at the current disk information:


The red box is the new hard disk we added. There is no partition information at present. In the same way, the hard disk is divided into a 10GB and two partitions containing the remaining space, which enter the parted interaction mode:


First, we use GPT format for this hard disk:


If all data on the hard disk will be lost, enter yes to confirm the execution, and then view the information on the current disk:


Notice that the Partiton Table entry has changed from MSDOS at the top to gpt. Next, create a new partition:


As you can see here, the 10GB partition has been established, and then repeat the above action to create another partition (which can be used when determining the starting point of another partition)print freeLook at:


View partition information:


If the partition is successful, the input Q can be saved automatically.


Prompt to modify / etc / fstab to enable automatic mounting of partitions.

3、Format and mount new partitions

Partition creation must be formatted before it can be used. Formatting is to initialize the disk partition with the specified file system, which is the method used by the operating system to organize files on the disk. So only when a partition is formatted can data be stored on it.

Under Linux, partitions can be formatted using the mkfs command. The command grammar is as follows:

mkfs.File System Type [Options] Device Name

Common Options:

  • -f:Use this option to force rewriting when the partition already has a file system

Formatted partitions must be mounted in a directory for data storage. If the partition needs to be booted and automatically mounted, it must be/etc/fstabThe partition is added to the file, otherwise the mount operation must be restarted as long as the host restarts.

An example: Format the two partitions above partitioned and mount them in the / data1 and / data2 directories

Let’s first look at the current disk partition status (with emphasis on the / dev / SDB section):


The partition numbers show that the two partitions are / dev / sdb1 and / dev / sdb2, respectively. We format / dev / sdb1 into XFS and / dev / sdb2 into ext4:


Formatting is completed. We verify whether the formatting is successful by looking at the partition status of the disk:


The formatting is successful by displaying the information. Next, the partition and its corresponding mount point are viewed through the DF command:


The partitions / dev / sdb1 and / dev / sdb2 are not yet visible, so we will mount them next:


Check whether the mount was successful by using the DF command:


OK,Now that you can see the partitions / dev / sdb1 and / dev / sdb2 and their corresponding mount points, you can now CD to the corresponding directory for file access operations.

Leave a Reply

Your email address will not be published. Required fields are marked *