如何使用“parted”命令创建分区

在 Linux 下创建或者管理分区有不同的选项 Parted 就是其中之一。
本文说明了它的用法和创建新分区表时的用例。
以交互方式使用 parted 一次输入一个命令。
仅包含设备作为调用交互模式的参数。

使用 parted 创建分区

1.选择要分区的硬盘

选择正在创建分区的磁盘,在下面的示例中 /dev/sdb 正在分区。
我们可以通过以下两种方式选择磁盘。

# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

或者

# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sdb                                                  
Using /dev/sdb
(parted)

可以执行“help”来查看 parted 命令中提供的各种选项

2.设置分区表类型

以下示例使用 mklabel 命令创建一个新的分区表。
磁盘标签类型必须是以下之一:aix、amiga、bsd、dvh、gpt、mac、msdos、pc98、sun 或者 loop。

(parted) help mklabel                                                     
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
	LABEL-TYPE is one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop

在上面的输出中,可以看到 parted 支持不同的 LABEL-TYPE。
需要注意的是,对于通用 Linux,需要使用 msdos 作为 LABEL-TYPE。

(parted) mklabel msdos                                                    
Warning: 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

3.检查可用空间和现有分区

要检查磁盘上的可用空间和任何现有分区,请使用 print 子命令。
如我们所见,磁盘上有 21.5GB 的可用空间,并且磁盘上尚未创建任何分区。

(parted) print free                                                       
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start   End     Size    Type  File system  Flags
        32.3kB  21.5GB  21.5GB        Free Space
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start  End  Size  Type  File system  Flags
(parted)

4. 使用 mkpart 在选定磁盘中创建主分区或者逻辑分区

可以使用 mkpart 命令创建主分区或者逻辑分区。
选项可以分别是主要的和逻辑的。
两个选项的示例如下所示(我们将创建 2 个 200MB 的分区作为主分区和逻辑分区):
a)
创建主分区

(parted) mkpart primary                                                   
File system type?  [ext2]? ext4                                           
Start? 0                                                                  
End? 200MB
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start  End    Size   Type     File system  Flags
 1      512B   200MB  200MB  primary

湾创建逻辑分区

(parted) mkpart logical                                                   
parted: invalid token: logical
Partition type?  primary/extended? extended                               
Start? 201M                                                               
End? 402M
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start  End    Size   Type      File system  Flags
 1      512B   200MB  200MB  primary
 2      201MB  402MB  200MB  extended               lba

注意:除非要创建逻辑分区,否则 Parted 在创建主分区时会要求 FS-Type。

之路教程 https://onitr oad .com

使用 rm 命令删除分区

也可以使用“rm”命令删除现有分区,如下例所示,我们有 2 个分区,编号为 1 和 2.

(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start  End    Size   Type      File system  Flags
 1      512B   200MB  200MB  primary
 2      201MB  402MB  200MB  extended               lba

删除分区 2:

(parted) rm                                                               
Partition number? 2

确认我们现在只能看到分区号 1.

(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 
Number  Start  End    Size   Type     File system  Flags
 1      512B   200MB  200MB  primary
日期:2020-09-17 00:10:55 来源:oir作者:oir