Linux磁盘管理命令-parted分区命令

时间:2024-07-19 07:15:01

Parted(Partition Editor)是一个功能强大的命令行工具,用于磁盘分区管理。它支持创建、删除、调整磁盘分区,以及显示磁盘的详细信息。

查看磁盘信息

parted -l
root@huhy:~# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  96.6GB  96.6GB  ext4
 3      96.6GB  98.8GB  2147MB  ext4


Error: /dev/sdb: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:

Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0
has been opened read-only.
Error: /dev/sr0: unrecognised disk label
Model: NECVMWar VMware SATA CD01 (scsi)
Disk /dev/sr0: 2755MB
Sector size (logical/physical): 2048B/2048B
Partition Table: unknown
Disk Flags:

root@huhy:~#

parted -l 命令显示了系统中三个磁盘设备的信息:/dev/sda、/dev/sdb 和 /dev/sr0。/dev/sdb 没有识别到有效的分区表,需要对其进行初始化和分区操作。

选择磁盘:在操作之前,首先需要选择要操作的磁盘,这会进入Parted的交互模式,可以在此模式下执行具体的操作

parted /dev/sdb

print;显示磁盘分区信息

(parted) print
Error: /dev/sdb: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
(parted)

创建新的分区表,类型如下

  • gpt:GUID Partition Table,适用于现代系统和大容量磁盘。
  • msdos:适用于旧系统,最大支持 2TB 磁盘。
(parted) mklabel gpt

创建新分区;一个50GB的 ext4 分区,名称primary(可自定义),除了使用绝对数值(如 0GB、50GB)外,还可以使用百分比、扇区、MB、TB 等单位来表示。

(parted) mkpart primary ext4 0GB 50GB
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  50.0GB  50.0GB  ext4         primary

删除分区;通过Number删除

(parted) rm 1
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags

调整分区大小:使用resizepart命令可以调整选定分区的大小

(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  50.0GB  50.0GB  ext4         huhy

(parted) resizepart 1 60GB
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  60.0GB  60.0GB  ext4         huhy

退出

(parted) quit

查看验证

root@huhy:~# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  96.6GB  96.6GB  ext4
 3      96.6GB  98.8GB  2147MB  ext4


Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  60.0GB  60.0GB               huhy


Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0
has been opened read-only.
Error: /dev/sr0: unrecognised disk label
Model: NECVMWar VMware SATA CD01 (scsi)
Disk /dev/sr0: 2755MB
Sector size (logical/physical): 2048B/2048B
Partition Table: unknown
Disk Flags: