Partitioning Disks by Using parted

To create and manage hard disks that use GPTs, use the parted command. The command enables you to perform typical partition operations as fdisk. However, parted is more advanced because it supports a larger set of commands and more disk label types including GPT disks.

Before running parted, complete the following requirements first:

  • Unmount any mounted partition on the disk.

  • Disable any partition that's being used as swap space by using the swapoff command.

  • Backup the data on the disk to be configured.

You can use parted either interactively or directly with command line arguments. To run parted interactively, specify only the name of the disk device as an argument, for example:

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

The following commands are useful for managing partitions:

print

Displays the current partition table.

mklabel

Creates a partition type according to the label you choose.

mkpart

Starts the process for creating new partitions.

quit

Exits the session.

Note:

In interactive sessions, changes are committed to disk immediately. Unlike fdisk, the parted utility doesn't have an option for quitting without saving changes.

help

Displays all the supported commands in the interactive mode.

Creating Partitions

The following example shows how to use the different parted commands to create 2 disk partitions. The first partition is assigned 2 GB while the second partition uses all the remaining disk space.

sudo parted /dev/sdb

The command runs a menu-based system where you must select the appropriate responses to configure the partition. Example inputs are displayed in the following interactive session:

GNU Parted 3.6
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 16.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags

(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? <Enter>
Start? 1
End? 2GB
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 16.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  2000MB  1999MB  primary  ext2         lba

(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? <Enter>
Start? 2001
End? -0
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 16.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  2000MB  1999MB  primary  ext2         lba
 2      2001MB  16.8GB  14.8GB  primary  ext2         lba

(parted) quit
                  

Note:

Unless you specify otherwise, the size for the Start and End offsets is in megabytes. To use another unit of measure, type the value and the unit together, for example, 2GB. To assign all remaining disk space to the partition, enter -0 for the End offset as shown in the example.

Customizing Labels

By default, parted creates msdos-labeled partitions. When partitioning with this label, you're also prompted for the partition type. Partition types can be primary, extended, or logical.

To use a different label, you would need to specify that label first with the mklabel command before creating the partition. Depending on the label, you would be prompted during the partitioning process for information, such as the partition name, as shown in the following example:

sudo parted /dev/sdb

The command runs a menu-based system where you must select the appropriate responses to configure the partition. Example inputs are displayed in the following interactive session:

GNU Parted 3.6
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
New disk label type? gpt
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
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 16.8GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(parted) mkpart
Partition name?  []? Example
File system type?  [ext2]? linux-swap
Start? 1
End? 2GB
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 16.8GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system     Name     Flags
 1      1049kB  2000MB  1999MB  linux-swap(v1)  Example  swap

(parted) quit
                  

To know which types of file systems and labels are supported by parted, consult the GNU Parted User Manual at https://www.gnu.org/software/parted/manual/, or enter info parted to view the online user manual. For more information, see the parted(8) manual page.