说明

  对于小于2TB的磁盘可以使用fdisk和parted命令进行分区,一般采用fdisk命令。但是对于大于2TB的磁盘只能使用parted分区,且需要将磁盘转换为GPT格式。

格式

  parted [option] [device]

常用命令
  • -l:显示所有磁盘分区的信息
  • -h:查看帮助
分区命令

通过parted -h或直接进入parted进入交互式模式后输入h查看

[root@server-101 ~]# parted 
GNU Parted 3.1
使用 /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) h    #输入h                                                             
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment #检查磁盘分区,TYPE分为min/opt两个选项,N为磁盘分区好
  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 #挽救临近"起始点" "终止点"的遗失凤琴曲
  rm NUMBER                                delete partition NUMBER #删除编号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 #显示版本号
示例

1、显示分区情况的例子

[root@localhost ~]# parted -l
Model: ATA VMware Virtual S (scsi) #磁盘型
Disk /dev/sda: 21.5GB #磁盘大小
Sector size (logical/physical): 512B/512B #扇区大小
Partition Table: msdos #分区表类型
Disk Flags: 

Number  Start   End     Size    Type     File system  标志
 1      1049kB  525MB   524MB   primary  xfs          启动
 2      525MB   21.5GB  20.9GB  primary               lvm


错误: /dev/sdb: unrecognised disk label
Model: ATA VMware Virtual S (scsi)                                        
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: 

Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/centos-root: 18.8GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  标志
 1      0.00B  18.8GB  18.8GB  xfs


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/centos-swap: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system     标志
 1      0.00B  2147MB  2147MB  linux-swap(v1)


警告: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Model: NECVMWar VMware IDE CDR10 (scsi)                                   
Disk /dev/sr0: 4244MB
Sector size (logical/physical): 2048B/2048B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  标志
 2      4366kB  30.1MB  25.8MB  primary

每列具体说明:

  • Number:分区编号
  • Start:分区开始位置
  • End:分区结束位置
  • Size:分区大小
  • Type:分区类型
  • File system:文件系统
  • 标志:标志位

2、虚拟机进行分区

[root@localhost ~]# parted /dev/sdb  #指定需要分区的设备名称
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.                                                    
(parted) mklabel gpt  #为sdb磁盘创建GPT分区表,大于2TB的磁盘必须执行这一步                                                     
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
是/Yes/否/No? yes #输入yes                                                         
(parted) mkpart primary 0 500   #创建主分区,大小为500MB                                          
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? Ignore #忽略警告
(parted) p  #显示分区表信息                                                              
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name     标志
 1      17.4kB  500MB  500MB               primary

(parted) mkpart logical 501 1000 #创建逻辑分区 大小为500MB
(parted) p    #显示分区表信息                                                            
Model: ATA VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size   File system  Name     标志
 1      17.4kB  500MB   500MB               primary
 2      501MB   1000MB  499MB               logical

(parted) quit #退出
信息: You may need to update /etc/fstab.

[root@localhost ~]# ls /dev/sdb* #查看已分的分区,parted分区是即时生效,没有保存步骤
/dev/sdb  /dev/sdb1  /dev/sdb2

后续的分区格式化、分区挂载、开机启动自动挂载等步骤跟fdisk分区一样,参考前面。