Use of disk partition tools in Linux system

1. Preface

In the daily operation and maintenance work, partitioning the Linux system disk is a very common operation. Generally speaking, whether you use the fdisk tool or the parted tool, the general steps are the same. You need to mount the disk first, then partition the target disk and format it into the specified file system type, and finally create a mount point and mount it.

Before writing this blog, I have read other articles on disk partition. Basically, simple disk partition can be realized according to the operation. However, with regard to the parted tool, I found that many articles understand mkpart primary as creating a primary partition, but it is not. After creating the partition, you can execute print to see the partition information. Under the gpt partition, the primary is actually the Name of the partition, not the Type of the partition; Only under msdos (which can be understood as MBR partition) partition can the primary partition, extended partition and logical partition be distinguished. At this time, mkpart primary really creates a primary partition.

This paper combines fdisk and parted, two commonly used disk partitioning tools. The content of the article will involve the similarities and differences between the two tools, the differences between MBR partition and GPT partition, how to realize partition alignment and accurate partition, and some small problems in the process of use. If there is anything wrong, you are welcome to correct it in order to correct it in time.

2. Use fdisk and parted for disk partition

2.1. Similarities and differences between fdisk and parted

In Linux system, fdisk is almost the most commonly used disk partition tool, and it is also very simple to use. However, due to the limitation of MBR partition table, fdisk tool can only partition disks with a capacity of less than 2TB.

Of course, the parted tool can also partition disks with a capacity of less than 2TB. However, when the disk capacity exceeds 2TB, you can only use the parted tool to partition the disk, and you also need to set the disk to GPT format. For the description of MBR and GPT formats, see below.

2.2. Use fdisk for disk partition

A brief description of fdisk

Tip1: use fdisk to view the information of the target disk.

[root@ora12 ~]# fdisk -l /dev/sdc

Disk /dev/sdc: 2469.6 GB, 2469606195200 bytes, 4823449600 sectors   //Representation of disk capacity under different reference systems.
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes   //The size of logical sector and physical sector (it is of certain auxiliary significance for understanding partition alignment later).
I/O size (minimum/optimal): 512 bytes / 512 bytes

Tip2: parameter resolution commonly used when partitioning with fdisk.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition   //Deletes the specified partition according to the partition number.
   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   //Create a new partition.
   o   create a new empty DOS partition table
   p   print the partition table   //Print out the current disk partition information.
   q   quit without saving changes   //Exit without saving configuration information.
   s   create a new empty Sun disklabel
   t   change a partition's system id   //Modify the type of partition (the default is standard partition, and the setting "8e" represents LVM partition).
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit   //Save the configuration information and exit.
   x   extra functionality (experts only)

Partition using fdisk

Note 1:

For MBR partitions, you can create up to four primary partitions. If a disk needs to create more than four partitions, you should create at least one extended partition, and then create a logical partition based on the extended partition!

Step 1: create two partitions (partition 1 is a standard partition with a capacity of 100GB; partition 2 is an LVM partition, and all the remaining capacity is allocated to partition 2).

[root@ora12 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x899a1c95.

Command (m for help): n   //Fill in "n" here to create a new partition.
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):    //The default here is primary.
Using default response p
Partition number (1-4, default 1):    //Here is the partition number (starting from 1 by default and increasing in sequence).
First sector (2048-419430399, default 2048):    //The starting position of partition 1 sector.
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +100G   //The end position of the sector of partition 1 (the capacity of the partition can be calculated through the start position and end position of the sector; the capacity of the partition can also be directly specified by "+ 100G").
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): n   //Create a new partition.  
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p):    //Specifies the primary partition.
Using default response p
Partition number (2-4, default 2):   //The partition number is 2.
First sector (209717248-419430399, default 209717248): 
Using default value 209717248
Last sector, +sectors or +size{K,M,G} (209717248-419430399, default 419430399):    //Indicates that all remaining space is allocated to zone 2.
Using default value 419430399
Partition 2 of type Linux and of size 100 GiB is set

Command (m for help): t   //Modify the partition type.
Partition number (1,2, default 2):   //Specify the target partition to modify (the default is the most recently created partition, that is, partition 2).
Hex code (type L to list all codes): 8e   // "8e" stands for LVM partition.
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p   //Print out all current partition information.

Disk /dev/sdb: 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: 0x899a1c95

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux   //sdb1 is a standard partition with a capacity of 100GB.
/dev/sdb2       209717248   419430399   104856576   8e  Linux LVM   //sdb1 is an LVM partition with a capacity of 100GB.

Command (m for help): w   //Save the configuration information and exit.
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Step 2: update the disk partition table information of the kernel.

[root@ora12 ~]# partprobe 

Step 3: query the partition information of "/ dev/sdb".

[root@ora12 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 99.5G  0 part 
  ├─centos-root 253:0    0 91.5G  0 lvm  /
  └─centos-swap 253:1    0    8G  0 lvm  [SWAP]
sdb               8:16   0  200G  0 disk 
├─sdb1            8:17   0  100G  0 part 
└─sdb2            8:18   0  100G  0 part 
sdc               8:32   0  2.3T  0 disk 
[root@ora12 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 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: 0x899a1c95

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209717247   104857600   83  Linux
/dev/sdb2       209717248   419430399   104856576   8e  Linux LVM

Step 4: format partition.

[root@ora12 ~]# mkfs -t ext4 /dev/sdb1 / / specify the file system type to format the partition through "- t".
[root@ora12 ~]# date && mkfs. Ext4 / dev / sdb2 & & date / / for large disk partitions, you can use "date" to calculate the approximate time required to complete the whole format.

Step 5: mount the partition to the file system.

[root@ora12 ~]# mkdir -p /test_dir/{sdb1_dir,sdb2_dir} / / create two directories as mount points.
[root@ora12 ~]# ls -l /test_dir/
total 0
drwxr-xr-x 2 root root 6 Jul  9 00:09 sdb1_dir
drwxr-xr-x 2 root root 6 Jul  9 00:09 sdb2_dir

[root@ora12 ~]# blkid /dev/sdb2 / / query the Block ID corresponding to "/ dev/sdb2".
/dev/sdb2: UUID="6f29e755-07b4-4375-a9dc-690d49ceccb7" TYPE="ext4" 

[root@ora12 ~]# Echo "/ dev / sdb1 / test_dir / sdb1_dir ext4 defaults 0" > > / etc / fstab / / set auto mount, which supports direct mount by partition.
[root@ora12 ~]# Echo "UUID = 6f29e755-07b4-4375-a9dc-690d49ceccb7 / test_dir / sdb2_dir ext4 defaults 0" > > / etc / fstab / / set automatic mount. It also supports direct mount with the UUID of the partition (the UUID method is recommended).

[root@ora12 ~]# mount -a / / mount and check the mount result.
[root@ora12 ~]# df -lh |grep sdb
/dev/sdb1                 99G   61M   94G   1% /test_dir/sdb1_dir
/dev/sdb2                 99G   61M   94G   1% /test_dir/sdb2_dir

2.3. Partition disks using parted

A brief description of parted

Tip1: parameter resolution commonly used when partitioning with parted.

[root@ora12 ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help                                                             
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted

2.3. 1. Use parted to partition the disk interactively

Create MSDOS partition using parted

Step 1: use parted to create 3 primary partitions and 3 logical partitions.

[root@ora12 ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel msdos   //Indicates that the disk is set to msdos format.                                           
(parted) mkpart primary   //Indicates that the partition to be created is designated as the primary partition.                                         
File system type?  [ext2]? ext4   //Specify the file system type (in the experiment, ext4 was set but did not take effect, and it is recommended that it should be formatted separately during mounting).                                        
Start? 1MiB   //Set the starting position of the partition (taking 1MiB as the starting position is to realize partition alignment).                                                       
End? 102400MiB   //Set the end position of the partition (taking 102400MiB as the end position is to accurately create a 100G partition. Later, you can see that the partition capacity is 100G by using lsblk).                                                     
(parted) mkpart primary
File system type?  [ext2]? ext4                                           
Start? 102401MiB                                                          
End? 204800MiB                                                            
(parted) mkpart primary                                                   
File system type?  [ext2]? ext4                                           
Start? 204801MiB                                                          
End? 307200MiB
(parted) mkpart extended  //In msdos format, only 4 primary partitions can be created at most, so only the last partition can be created as an extended partition.                                         
Start? 307201MiB                                                          
End? 100%   //100% means that all the remaining capacity is allocated to the extended partition.
(parted) mkpart logic 307202MiB 409600MiB   //Create logical partition
(parted) mkpart logic 409601MiB 512000MiB                             
(parted) mkpart logic 512001MiB -1   //"- 1" means that all the remaining capacity is allocated to this logical partition.
(parted) quit                                                     
Information: You may need to update /etc/fstab.

Step 2: View partitioned information.

[root@ora12 ~]# parted -s /dev/sdc print
Model: VMware Virtual disk (scsi)
Disk /dev/sdc: 2470GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  107GB   107GB   primary
 2      107GB   215GB   107GB   primary
 3      215GB   322GB   107GB   primary
 4      322GB   2470GB  2147GB  extended               lba
 5      322GB   429GB   107GB   logical
 6      429GB   537GB   107GB   logical
 7      537GB   2470GB  1933GB  logical


[root@ora12 ~]# lsblk |grep sdc
sdc               8:32   0  2.3T  0 disk 
├─sdc1            8:33   0  100G  0 part   //You can intuitively see that the size of each partition except sdc7 partition is 100G.
├─sdc2            8:34   0  100G  0 part 
├─sdc3            8:35   0  100G  0 part 
├─sdc4            8:36   0    1K  0 part 
├─sdc5            8:37   0  100G  0 part 
├─sdc6            8:38   0  100G  0 part 
└─sdc7            8:39   0  1.8T  0 part 

Step 3: create a mount point and format the above partition for mounting.

[root@ora12 ~]# for i in {1,2,3,5,6,7};do mkdir -p /msdos/dir$i;done / / batch create mount points.
[root@ora12 ~]# for i in {1,2,3,5,6,7};do mkfs.ext4 /dev/sdc$i;done / / batch format partition.
[root@ora12 ~]# for i in {1,2,3,5,6,7};do mount /dev/sdc$i /msdos/dir$i;done / / mount in batches.

Step 4: check the mounting results.

[root@ora12 ~]# df -lh |grep sdc
/dev/sdc1                 99G   61M   94G   1% /msdos/dir1
/dev/sdc2                 99G   61M   94G   1% /msdos/dir2
/dev/sdc3                 99G   61M   94G   1% /msdos/dir3
/dev/sdc5                 99G   61M   94G   1% /msdos/dir5
/dev/sdc6                 99G   61M   94G   1% /msdos/dir6
/dev/sdc7                1.8T   77M  1.7T   1% /msdos/dir7

Creating GPT partitions using parted

Step 1: create 5 primary partitions using parted.

[root@ora12 ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt   //Indicates that the disk is set to gpt format.                                                
(parted) mkpart part1   //Here, mkpart is followed by the Name of the partition (many people still write primary here, but at this time, primary only represents the partition Name rather than specifying the primary partition).                              
File system type?  [ext2]? ext4                                           
Start? 1MiB                                                               
End? 102400MiB                                                            
(parted) mkpart part2
File system type?  [ext2]? ext4                                   
Start? 102401MiB                                                          
End? 204800MiB                                                            
(parted) mkpart part3
File system type?  [ext2]? ext4                                
Start? 204801MiB                                                          
End? 307200MiB                                                            
(parted) mkpart part4
File system type?  [ext2]? ext4                             
Start? 307201MiB                                                          
End? 409600MiB                                                            
(parted) mkpart part5
File system type?  [ext2]? ext4   //It can be seen intuitively that under the gpt format, it is not limited by the number of four primary partitions (Part1 -- Part5 partitions are created).        
Start? 409601MiB                                                          
End? 100%

Step 2: for the operation of mounting, please refer to the previous content, which is omitted here.

2.3. 2. Use parted to partition the disk non interactively

Create MSDOS partition using parted

[root@ora12 ~]# parted -s /dev/sdc mklabel msdos mkpart primary 1MiB 102400MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart primary 102401MiB 204800MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart primary 204801MiB 307200MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart extended 307201MiB 100% / / during non interactive creation, it is not supported to allocate all the remaining space with "- 1".
[root@ora12 ~]# parted -s /dev/sdc mkpart logic 307202MiB 409600MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart logic 409601MiB 512000MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart logic 512001MiB 100%


[root@ora12 ~]# parted -s /dev/sdc print
Model: VMware Virtual disk (scsi)
Disk /dev/sdc: 2470GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  107GB   107GB   primary   ext4
 2      107GB   215GB   107GB   primary   ext4
 3      215GB   322GB   107GB   primary   ext4
 4      322GB   2470GB  2147GB  extended               lba
 5      322GB   429GB   107GB   logical   ext4
 6      429GB   537GB   107GB   logical   ext4
 7      537GB   2470GB  1933GB  logical   ext4


[root@ora12 ~]# lsblk |grep sdc
sdc               8:32   0  2.3T  0 disk 
├─sdc1            8:33   0  100G  0 part 
├─sdc2            8:34   0  100G  0 part 
├─sdc3            8:35   0  100G  0 part 
├─sdc4            8:36   0    1K  0 part 
├─sdc5            8:37   0  100G  0 part 
├─sdc6            8:38   0  100G  0 part 
└─sdc7            8:39   0  1.8T  0 part 

Creating GPT partitions using parted

[root@ora12 ~]# parted -s /dev/sdc mklabel gpt mkpart part1 1MiB 102400MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart part2 102401MiB 204800MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart part3 204801MiB 307200MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart part4 307201MiB 409600MiB
[root@ora12 ~]# parted -s /dev/sdc mkpart part5 409601MiB 100%


[root@ora12 ~]# parted -s /dev/sdc print
Model: VMware Virtual disk (scsi)
Disk /dev/sdc: 2470GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  107GB   107GB   ext4         part1
 2      107GB   215GB   107GB   ext4         part2
 3      215GB   322GB   107GB   ext4         part3
 4      322GB   429GB   107GB                part4
 5      429GB   2470GB  2040GB  ext4         part5


[root@ora12 ~]# lsblk |grep sdc
sdc               8:32   0  2.3T  0 disk 
├─sdc1            8:33   0  100G  0 part 
├─sdc2            8:34   0  100G  0 part 
├─sdc3            8:35   0  100G  0 part 
├─sdc4            8:36   0  100G  0 part 
└─sdc5            8:37   0  1.9T  0 part 

3. Difference between MBR partition and GPT partition

Before we talk about this problem, first we need to understand what partitions are for? Take the notebook computer as an example. Suppose a 500GB disk is configured. When installing the Windows system, we will generally divide it into a C disk for installing the system and a D disk for storing personal data. In fact, Disk C and disk d here are two partitions, but we are used to saying that there are two disks on the computer. Similarly, partitioning the disks in the Linux system is also to mount them to different directories (mount points), so that users can store different data files in different directories.

Therefore, partition, in plain terms, is to delimit blocks of areas on the physical disk and map (or mount) these areas to the specified directory. Users can customize the capacity of each partition and put different data files into different directories according to their own plans without interfering with each other.

Due to the different data structures of the underlying physical disk addressing and indexing, the number and type of partitions that can be created in MBR format and GPT format are also different. A brief introduction will be given below.

MBR: simplified structure diagram of Master Boot Record disk partition

Because the area used to record the disk partition table information in the MBR sector is only 64 Bytes, and every 16 Bytes is used for the identification of one partition, there can be at most 4 primary partitions for the MBR partition; If you need to create more than 4 partitions, you must create at least one extended partition, and then create a logical partition.

For EBR sector, the area used to record disk partition table information is also 64 Bytes, which is also the identification of one partition every 16 Bytes. The first group of 16 Bytes is used to point to its own boot sector; The second group of 16 Bytes is used to point to the EBR sector of the next logical partition (if there are no other logical sectors, all are set to 0); the last two groups of 32 Bytes are not used temporarily.

GPT: GUID Partition Table simplified structure diagram of disk partition

Similarities and differences between MBR partition and GPT partition

  • In terms of the number of partitions, MBR partitions can support up to 4 primary partitions. If more than 4 partitions need to be divided, at least one extended partition needs to be created, and then a logical partition needs to be created. GPT partitions do not distinguish between primary partitions, extended partitions and logical partitions, Moreover, the number of partitions supported is enough to meet the needs of production testing (I have read the posts of other bloggers. Some say that the number of partitions is unlimited in theory, and some say that there are up to 128 partitions under Windows. Bloggers do not go into this area; generally speaking, if the disk space is greater than 2TB and the number of partitions is relatively large, I will choose to use GPT partitions).
  • In terms of partition capacity, the maximum supported partition capacity of MBR partition is 2TB. When it exceeds 2TB, it will still be calculated as 2TB; GPT partitions do not have this limit, so generally, after the disk capacity exceeds 2TB, the disk will be set to GPT format.
  • In terms of partition security, because the partition table in GPT format will have backup and corresponding verification algorithm, it will be more secure than the partition in MBR format.

4. How to achieve partition alignment

In fact, what I wanted to express in this chapter was how to achieve accurate partition. For example, for a 2TB disk, I want to create a partition with a size of 100GB. However, when we partition, we clearly calculate 100GB, but after the partition is completed, when we check the partition size through lsblk and other commands, we will find that it is always a little different, For people with obsessive-compulsive disorder, this looks very uncomfortable.

When I originally talked about this topic, I thought of a problem I had encountered before, which is that disk partitions are not 4K aligned. Therefore, in this chapter, I will share some of my experiences on partition alignment and how to achieve accurate partition. Of course, there are some incorrect places. Friends are welcome to correct them.

Let's look at this picture first: (the sdb1 partition is not aligned and the sdc1 partition is aligned)

Through the following statement, we can judge whether a disk partition is aligned:

parted /dev/sdc align-check optimal 1   //The number 1 represents the partition number, and the returned result is aligned, indicating that the partition is aligned.

First of all, we should understand that if the disk partitions are not aligned, some redundant actions will be triggered during data reading and writing, which will reduce the read and write performance of the disk. Here you can refer to Partition 4K alignment, all you want to know is here Personally, I think it's very good. In short, if the physical sector and the logical sector are not fully aligned in the zoned partition, it will cause the upper layer data to be written to one physical sector at a time when the disk is dropped. However, due to the lack of alignment, the data may be scattered to multiple physical sectors, which will generate redundant I/O and increase the overhead of disk address space mapping management, This is some of the redundant actions I mentioned above.

After further testing, I found that:

If we use MiB as the calculation unit when partitioning, whether using fdisk tool or parted tool, the obtained partitions are aligned, and the final partition size is consistent with that calculated by us. For details, see the effects of sdc1 and sdc2 in the figure.

This will involve the different effects of using MiB and MB as different calculation units, which will correspond to whether we convert units in 1024 or 1000. In fact, using MiB units is calculated in standard binary, while using MB is calculated in decimal. Obviously, the MiB unit using binary calculation will be more in line with the standard of computer data processing, which is why when using MiB as the unit for partition, the partition is both aligned and accurate.

In addition, when purchasing disks, we will also find that the capacity marked on the disk is 3.2TB, but the disk space recognized in the system will be less than this value. The reason is that the disk manufacturer converts it in 1000, while the system converts it in 1024.

5. Summary

In the daily operation and maintenance work, generally speaking, you can simply use fdisk and parted tools to partition, format the partition and mount it. As far as I am concerned, I use the fdisk tool most. For large capacity disks, they are often used directly as raw disks.

This article involves a lot of things, but it is not deep enough in principle. On the one hand, I am not engaged in the research of the underlying system, on the other hand, I don't have so much time to study. However, if you don't have a good mind and forget some concepts and commands in the future, you can review them through this article.

Keywords: Linux Operation & Maintenance

Added by if on Sat, 25 Dec 2021 18:06:57 +0200