Linux

如何使用parted来管理存储空间大于2TB的磁盘设备

一 背景说明

客户提供一台磁盘空间有10TB的Linux服务器来存放数据库的异机备份。剩下的工作由我来执行。

机器信息如下:CentOS 7.6的操作系统,16GB内存,4CPU,系统盘40GB,存储设备/dev/sdb磁盘空间有10TB,其中/dev/sdb没有做任何分区。

[root@10 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  7.6G     0  7.6G   0% /dev
tmpfs          tmpfs     7.6G     0  7.6G   0% /dev/shm
tmpfs          tmpfs     7.6G  8.6M  7.6G   1% /run
tmpfs          tmpfs     7.6G     0  7.6G   0% /sys/fs/cgroup
/dev/sda1      ext4       40G  2.2G   36G   6% /
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/0
[root@10 ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   40G  0 disk 
└─sda1   8:1    0   40G  0 part /
sdb      8:16   0  9.8T  0 disk 
[root@10 ~]#  free -m
              total        used        free      shared  buff/cache   available
Mem:          15408         339       14619           8         449       14799
Swap:             0           0           0
[root@10 ~]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 106
Model name:            Intel(R) Xeon(R) Platinum 8378A CPU @ 3.00GHz
Stepping:              6
CPU MHz:               3000.006
BogoMIPS:              6000.01
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             48K
L1i cache:             32K
L2 cache:              1280K
L3 cache:              49152K
NUMA node0 CPU(s):     0-3
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 arat avx512vbmi umip avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq md_clear spec_ctrl intel_stibp arch_capabilities
[root@10 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@10 ~]# fdisk -l
​
Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000aa138
​
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    83886079    41942016   83  Linux
​
Disk /dev/sdb: 10737.4 GB, 10737418240000 bytes, 20971520000 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
​
[root@10 ~]# 

二 为什么使用parted工具

对于单块磁盘空间大小超过2TB的设备,fdisk不支持。所以,改为使用parted来对设备进行管理和分区。如下,直接使用fdisk操作超过2TB的设备,会收到报警提示:

[root@10 ~]# 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 0x9ddac320.

WARNING: The size of this disk is 10.7 TB (10737418240000 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID 
partition table format (GPT).


Command (m for help): 

三 使用parted对磁盘进行分区和管理

0 parted使用帮助查看

[root@10 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
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
(parted)                               

通过parted device_path 进入交互式模式,键入help获取更多帮助信息。如上:

mklabel,mktable LABEL-TYPE 可以创建partition table;

mkpart PART-TYPE [FS-TYPE] START END make a partition;

1 创建GPT分区表

[root@10 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
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
(parted) mktable gpt        ###注意,这里指出给该设备创建的是gpt类型的分区表                                    
(parted) print              ###再次print时,可以看到分区表类型                                              
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted)                  

2 创建分区

[root@10 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
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
(parted) mktable gpt                                                      
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart primary xfs 1 100%      #注意这里的命令,从第1个磁道开始,100%表示磁盘的所有空间都放到这个分区里;
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  10.7TB  10.7TB  xfs          primary

(parted) quit                                                             
Information: You may need to update /etc/fstab.

[root@10 ~]#                                          

3 挂载文件系统

[root@10 ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   40G  0 disk 
└─sda1   8:1    0   40G  0 part /
sdb      8:16   0  9.8T  0 disk 
└─sdb1   8:17   0  9.8T  0 part 
[root@10 ~]# mkdir -p /db_backup_no_remove/
[root@10 ~]# mount -t xfs /dev/sdb1 /db_backup_no_remove/
[root@10 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  7.6G     0  7.6G   0% /dev
tmpfs          tmpfs     7.6G     0  7.6G   0% /dev/shm
tmpfs          tmpfs     7.6G  8.6M  7.6G   1% /run
tmpfs          tmpfs     7.6G     0  7.6G   0% /sys/fs/cgroup
/dev/sda1      ext4       40G  2.2G   36G   6% /
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/0
/dev/sdb1      xfs       9.1T   33M  9.1T   1% /db_backup_no_remove
[root@10 ~]# 

4 写入/etc/fstab文件系统配置表

[root@10 ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Feb 10 08:57:10 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
#
UUID=4fde6d8c-b0bb-4d16-a95f-c578b5f9c2a6 /                       ext4    defaults        1 1
/dev/sdb1                                 /db_backup_no_remove/   xfs     defaults        1 1
[root@10 ~]# 

至此,设备已经通过parted进行分区管理,并且挂载到服务器上使用。

四 原来fdisk也可以管理大磁盘

以下步骤是测试和验证步骤。

1 卸载文件系统

[root@10 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  7.6G     0  7.6G   0% /dev
tmpfs          tmpfs     7.6G     0  7.6G   0% /dev/shm
tmpfs          tmpfs     7.6G  8.6M  7.6G   1% /run
tmpfs          tmpfs     7.6G     0  7.6G   0% /sys/fs/cgroup
/dev/sda1      ext4       40G  2.2G   36G   6% /
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/0
/dev/sdb1      xfs       9.1T   33M  9.1T   1% /db_backup_no_remove
[root@10 ~]# umount /db_backup_no_remove/
[root@10 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  7.6G     0  7.6G   0% /dev
tmpfs          tmpfs     7.6G     0  7.6G   0% /dev/shm
tmpfs          tmpfs     7.6G  8.6M  7.6G   1% /run
tmpfs          tmpfs     7.6G     0  7.6G   0% /sys/fs/cgroup
/dev/sda1      ext4       40G  2.2G   36G   6% /
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/0
[root@10 ~]# 

2 删除磁盘分区

[root@10 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  10.7TB  10.7TB  xfs          primary

(parted) rm 1              #通过rm 磁盘分区编号,来删除磁盘分区                                  
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) quit                                                             
Information: You may need to update /etc/fstab.

[root@10 ~]#                                

3 删除磁盘分区表

[root@10 ~]# wipefs /dev/sdb 
offset               type
----------------------------------------------------------------
0x200                gpt   [partition table]

[root@10 ~]# wipefs -a -t gpt -f /dev/sdb 
/dev/sdb: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
/dev/sdb: 8 bytes were erased at offset 0x9c3fffffe00 (gpt): 45 46 49 20 50 41 52 54
/dev/sdb: calling ioclt to re-read partition table: Success
[root@10 ~]# wipefs /dev/sdb 
offset               type
----------------------------------------------------------------
0x1fe                PMBR   [partition table]

[root@10 ~]# 

4 用fdisk来创建分区

[root@10 ~]# fdisk /dev/sdb 

WARNING: The size of this disk is 10.7 TB (10737418240000 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID 
partition table format (GPT).

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.


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
   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 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 (m for help): g
Building a new GPT disklabel (GUID: 0642A2B7-F284-4D01-8EA1-6846AF98D036)


Command (m for help): n
Partition number (1-128, default 1): 
First sector (2048-20971519966, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519966, default 20971519966): +100GB
Created partition 1


Command (m for help): n
Partition number (2-128, default 2): 
First sector (195313664-20971519966, default 195313664):  
Last sector, +sectors or +size{K,M,G,T,P} (195313664-20971519966, default 20971519966): +100GB
Created partition 2


Command (m for help): n
Partition number (3-128, default 3): 
First sector (390625280-20971519966, default 390625280): 
Last sector, +sectors or +size{K,M,G,T,P} (390625280-20971519966, default 20971519966): +100GB
Created partition 3


Command (m for help): n
Partition number (4-128, default 4): 
First sector (585936896-20971519966, default 585936896): +100GB
First sector (585936896-20971519966, default 585936896): 
Last sector, +sectors or +size{K,M,G,T,P} (585936896-20971519966, default 20971519966): +100GB
Created partition 4


Command (m for help): n
Partition number (5-128, default 5): 
First sector (781248512-20971519966, default 781248512): 
Last sector, +sectors or +size{K,M,G,T,P} (781248512-20971519966, default 20971519966): +3T      
Created partition 5


Command (m for help): n
Partition number (6-128, default 6): 
First sector (7223699456-20971519966, default 7223699456): 
Last sector, +sectors or +size{K,M,G,T,P} (7223699456-20971519966, default 20971519966): +3T
Created partition 6


Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@10 ~]# lsblk 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   40G  0 disk 
└─sda1   8:1    0   40G  0 part /
sdb      8:16   0  9.8T  0 disk 
├─sdb1   8:17   0 93.1G  0 part 
├─sdb2   8:18   0 93.1G  0 part 
├─sdb3   8:19   0 93.1G  0 part 
├─sdb4   8:20   0 93.1G  0 part 
├─sdb5   8:21   0    3T  0 part 
└─sdb6   8:22   0    3T  0 part 
[root@10 ~]# 

从上,我们可以看到告警说fdisk管理超过2TB大小的磁盘设备。然后,我们创建里 GPT类型的分区表。接下来,就可以正常创建和使用分区了。并且,此时,可以创建超过4个primary类型的分区。而且,也同样可以创建出超过2TB大小的磁盘分区了。

五 parted给设备创建多个分区

1 删除/dev/sdb设备上的分区

[root@10 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  100GB   100GB   xfs
 2      100GB   200GB   100GB
 3      200GB   300GB   100GB
 4      300GB   400GB   100GB
 5      400GB   3699GB  3299GB
 6      3699GB  6997GB  3299GB

(parted) rm 1
(parted) rm 2                                                             
(parted) rm 3                                                             
(parted) rm 4                                                             
(parted) rm 5
(parted) rm 6                                                             
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted)                                                    

2 用parted创建新的分区

(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart primary xfs 1 100GB
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  100GB  100GB  xfs          primary

(parted) mkpart primary xfs 100GB 200GB
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  100GB  100GB  xfs          primary
 2      100GB   200GB  100GB               primary

(parted) mkpart primary xfs 200GB 1200GB
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  100GB   100GB   xfs          primary
 2      100GB   200GB   100GB                primary
 3      200GB   1200GB  1000GB               primary

(parted) mkpart primary xfs 1000GB 70%
Warning: You requested a partition from 1000GB to 7516GB (sectors 1953125000..14680064000).
The closest location we can manage is 1200GB to 7516GB (sectors 2343749632..14680064000).
Is this still acceptable to you?
Yes/No? yes                                                               
(parted) print                                                            
Model: Huawei VBS fileIO (scsi)
Disk /dev/sdb: 10.7TB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  100GB   100GB   xfs          primary
 2      100GB   200GB   100GB                primary
 3      200GB   1200GB  1000GB               primary
 4      1200GB  7516GB  6316GB               primary

(parted) quit                                                             
Information: You may need to update /etc/fstab.

[root@10 ~]# lsblk                                                        
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0    40G  0 disk 
└─sda1   8:1    0    40G  0 part /
sdb      8:16   0   9.8T  0 disk 
├─sdb1   8:17   0  93.1G  0 part 
├─sdb2   8:18   0  93.1G  0 part 
├─sdb3   8:19   0 931.3G  0 part 
└─sdb4   8:20   0   5.8T  0 part 
[root@10 ~]#                        

说明:

mkpart primary xfs 1 100GB表示,从第1个扇区开始,创建100GB大小的磁盘,分区类型为primary;文件系统xfs;

接下来:mkpart primary xfs 100GB 200GB从100GB的地方开始,到200GB的地方结束,创建1个分区;

mkpart primary xfs 1000GB 70%;从1000GB的地方开始,磁盘总大小的70%的地方结束,创建1个分区;

六 小结和参考

  • 原来,个人一直以为fdisk不能创建和管理超过2TB大小的磁盘分区,其实是有误解的,当我们选定gpt类型的partition table时,同样可以创建分区;也可能是新版本的fdisk才支持的。[root@10 ~]# fdisk /dev/sdb WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. 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.Command (m for help):
  • 记得曾经在一个客户现场处理Oracle RAC时,规划ASM磁盘时,划分超过2TB,后来出现过问题的情况;现在回忆起来,还是历历在目;
  • parted创建和管理大磁盘确实比较方便好用;

参考:如何创建和删除磁盘分区表,注意是磁盘分区表,不是磁盘分区;

https://www.golinuxhub.com/2018/05/how-to-cleardelete-partition-table-disk-linux/

使用parted对磁盘进行分区和管理:

https://www.golinuxcloud.com/parted-command-in-linux/

留言