There are different tools to manage storage devices on Linux. One such commonly used tool is the parted command utility. It allows you to create, extend, and delete disk partitions on storage devices.
Parted command offers several benefits over other partition tools, such as fdisk, when it comes to managing disk partitions. Some of its key benefits are its support for resize and recovery operations.
Moreover, parted command gives you the flexibility of managing disk partitions through one of its two modes.
The interactive mode offers an interactive shell for manipulating partitions with parted-specific commands. The command line mode allows you to execute the parted commands non-interactively. This flexibility means that you can perform the same operation via either mode, depending on your preference.
In addition, parted supports a wide range of partition formats, including both the Master Boot Record (MBR) and GUID Partition Table (GPT) partition schemes, ensuring compatibility with older and modern systems.
In this article, we'll delve into some parted commands, demonstrating how to effectively work with parted in both modes. However, keep in mind that elevated privileges are required to use parted, and changes made with parted are immediate. Thus, remember to exercise caution when modifying disk partitions to avoid overwriting and losing important data.
Install parted
parted comes preinstalled on most Linux distributions. Nonetheless, if it isn't available on your machine, you can install it through one of the following commands, depending on your distribution.
For Ubuntu and Debian
sudo apt install parted
For CentOs, Fedora, and RHEL
sudo dnf install parted
For Arch Linux
sudo pacman -S parted
parted Syntax
In command-line mode, parted commands typically take the following form:
parted [options] [device] [command [options]]
The [command [options]] indicates commands that are specific to parted, such as "print".
Interactive Mode
Alternatively, you can use parted interactively by invoking the parted command without any options or additional commands, as demonstrated below.
sudo parted GNU Parted 3.4 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
In interactive mode, when you don't specify a specific block device, parted chooses the first block device it recognizes, which is /dev/sda in this example. At the end of the session, you can exit the interactive shell with the quit command.
1. List Available Partitions
Before making changes to a disk, you must examine the partitions present on the disk to have a complete understanding of the existing partition layout.
parted allows you to view available disk partitions in two ways. The first method involves the use of the "-l" or "--list" option with the parted command. This outputs the partition table of all block devices available on the machine.
sudo 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 540MB 538MB fat32 EFI System Partition boot, esp 3 540MB 107GB 107GB ext4 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 1612MB 1611MB ext4 2 1612MB 2136MB 524MB ext4 3 2136MB 2398MB 262MB ext4
The output produced above shows all the partitions present on the two block devices (/dev/sda and /dev/sdb) on the machine, including information about the start and end sectors, disk size, and file system of each partition.
The second method involves the use of the "print" command in the interactive shell, which shows the partitions on a disk.
(parted) print 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 540MB 538MB fat32 EFI System Partition boot, esp 3 540MB 107GB 107GB ext4
In the output produced, parted displays a list of the partitions existing on the currently selected disk. However, the print command also has additional options which modify its behavior, including:
devices — This option produces a list of all the block devices present on the machine, with their corresponding sizes.
(parted) print devices /dev/sda (107GB) /dev/sdb (21.5GB) /dev/sr0 (133MB) /dev/sr1 (3827MB)
free — When this option is specified, parted displays a list of available partitions and unallocated (unpartitioned) space on the currently selected disk. For example, in the output below, there are unallocated spaces before and after the first and last partitions of the /dev/sdb disk.
(parted) print free Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 17.4kB 1049kB 1031kB Free Space 1 1049kB 1612MB 1611MB ext4 2 1612MB 2136MB 524MB ext4 3 2136MB 2398MB 262MB ext4 2398MB 21.5GB 19.1GB Free Space
list and all — The "list" and "all" options perform a similar action to the "--list" option in parted's command-line mode, showing the partition tables of all block devices present on the machine.
Alternatively, the "p" command can be used as shorthand for the "print" command.
2. Select a Different Device
When using the interactive shell, you can easily switch the currently selected disk to a different device using the "select" command. For example, let's assume that the currently selected disk in the shell is /dev/sda. This can be changed to /dev/sdb, as demonstrated below.
(parted) select /dev/sdb Using /dev/sdb
The use of this command isn't limited to disk devices only. It can be used with a partition or an LVM logical volume.
3. Create a Partition Table
A partition table is an essential component of any disk. It defines the layout of partitions in a disk and determines how data is organized, stored, and accessed. Generally, to begin partitioning a disk, you need a partition table.
The parted command allows you to create various partition tables, including — GPT, MBR, Apple Partition Map (APM), using the "mklabel" command. The "mklabel" command accepts a disk label value, which can be one of the following supported disk labels:
- bsd
- loop
- gpt
- mac (APM)
- msdos (MBR)
- pc98
- sun
To create an MBR partition table, for instance, the msdos disk label is used, as demonstrated below:
(parted) mklabel msdos
Once this command is invoked, the partition table is created immediately.
Similarly, the partition table can be created in command-line mode as follows:
sudo parted /dev/sdc mklabel msdos
3. Create a Partition
After creating a partition table, you can start partitioning the disk. The "mkpart" command is used to create new disk partitions. Please note that when partitioning a disk, the prompts shown may differ depending on the disk's partition scheme. However, the overall process is quite similar.
For instance, the MBR partition scheme uses the concept of primary, extended, and logical partitions, which is absent in the GPT scheme. Therefore, while parted will ask you to specify a partition type on an MBR disk, it will only prompt you for the partition name on a GPT disk.
To partition a disk with an MBR partition table, you should keep in mind that you're limited to four primary partitions only. However, you can also create an extended partition, which can further be divided into logical partitions.
The following steps demonstrate how to create a primary partition in a disk with an MBR partition table.
First, enter the "mkpart" command in the parted interactive shell.
(parted) mkpart
Next, you're prompted to choose a partition type. Choose primary.
Partition type? primary/extended? primary
Then, the prompt asks for a file system type. This can be one of "ext2", "ext3", "ext4", "fat16", "fat32", "hfs", "hfs+", "linux-swap", "ntfs", "reiserfs", "udf", or "xfs". We'll choose "ext4" for this example.
File system type? [ext2]? ext4
Next, the prompt asks for the starting point of the partition's sector. Since there are no partitions on the disk, we'll choose 1. However, you should often take any existing partitions into consideration when choosing a partition's starting boundary.
Start? 1
Lastly, specify the end sector of the partition, which represents the partition's size. This value can be expressed either in kilobytes (KB), megabytes (MB), Gigabytes(GB), and so on by appending the corresponding unit to the size.
However, it'll default to megabytes if no unit is specified. For instance, the value specified below will create a partition with a size of 5GB.
End? 5GB
Alternatively, the steps above can be combined into a single command, as shown below.
(parted) mkpart primary ext4 1 5G
You can also create partitions in command-line mode by running the following command.
sudo parted /dev/sdc mkpart primary ext4 1 5G
4. Resize a Partition
After creating a partition, you may want to shrink or extend the size of the partition to meet your new requirements. parted allows you to achieve this using the "resizepart" command.
This command accepts two values — the partition's number, which you can obtain from executing the "print" command, and the end, representing the new adjusted size of the partition.
For example, using the "resizepart" command, we can resize the first partition of a disk to 2GB, as shown below.
(parted) resizepart 1 2G
Similarly, the following command can be used in command-line mode to achieve the same result.
sudo parted /dev/sdc resizepart 1 2G
5. Delete a Partition
The "rm" command is used to remove unwanted partitions. It is a straightforward process as you only need to provide it with the partition number. However, you should be careful when using this command since there's no confirmation prompt, and deletion is instantaneous.
To remove a partition, execute the "rm" command, specifying the partition's number, which you can obtain from the "print" command.
(parted) rm 1
This can be achieved in command line mode, too, using:
sudo parted /dev/sdc rm 1
6. Rescue a Partition
parted also allows you to recover lost or deleted partitions, previously located between a certain range, with the help of the "rescue" command.
This command accepts two value — start and end, representing the start and end positions where the search will be performed. This is particularly useful if you accidentally delete a partition while using the "rm" command.
For example, let's assume a partition that previously existed between 1 - 200MB was deleted accidentally. To recover this partition, the "rescue" command is used, as demonstrated below:
(parted) rescue 1 200 Information: A ext4 primary partition was found at 1049kB -> 199MB. Do you want to add it to the partition table? Yes/No/Cancel?
After parted finds a partition successfully, a confirmation prompt is presented to restore the deleted partition. Enter "yes" to recover the partition. Furthermore, the "print" command can then be used to confirm the restoration of the partition.
(parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdc: 975MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 199MB 198MB ext4 2 200MB 500MB 300MB second
The same action can be achieved in command-line mode.
sudo parted /dev/sdc rescue 1 200 searching for file systems... 2% (time left 00:39)Information: A ext4 primary partition was found at 1049kB -> 199MB. Do you want to add it to the partition table?
7. Change Default Unit
As mentioned previously, parted defaults to Megabytes (MB) if a unit isn't specified. This default unit is used when displaying sizes and interpreting inputs entered into the prompt. However, the default can easily be changed using the "unit" command to your preferred unit.
The unit command accepts a value corresponding to a unit. Furthermore, parted supports various units, including the following:
- s — Sectors
- B — Byte
- KiB — Kibibyte (1024 bytes)
- MiB — Mebibyte (1048576 bytes)
- GiB — Gibibyte (1073741824 bytes)
- TiB — Tebibyte (1099511627776 bytes)
- kB — Kilobyte (1000 bytes)
- MB — Megabyte (1000000 bytes)
- GB — Gigabyte (1000000000 bytes)
- TB — Terabyte (1000000000000 bytes)
- % — Disk Percentage (0 - 100)
- cyl — Cylinders
- chs — Cylinders, heads, and sectors
- compact — This is a special unit. It defaults to megabyte for inputs and uses a human-readable unit for outputs. It's also the default unit.
To change the default unit to gigabyte, for instance, the command below is used.
(parted) unit GB
Additionally, you should keep in mind that the unit you set doesn't persist after a session ends.
8. Change Partition Name
The Mac, PC98, and GPT partition schemes support assigning names when creating partitions. However, parted also allows you to modify these partition names using the "name" command.
The "name" command accepts two arguments — the partition's number and the partition's name. For example, the command below changes the name of the first partition to "foo".
(parted) name 1 foo
Similarly, in command mode, the following command achieves the same result.
sudo parted /dev/sdc name 1 foo
9. Change Partition Flag
Partition flags are certain attributes that provide information to the OS and various utilities on how a partition should be treated. These attributes indicate the purpose of the partition.
Furthermore, parted allows you to change a flag on a partition using the "set" command. This command accepts three values — the partition's number, the flag, and the flag's state (on/off). A variety of flags are supported, including:
- boot — This indicates that the partition is the bootable partition.
- msftdata — This identifies partitions that contain Microsoft filesystems (NTFS and FAT).
- bios_grub — This marks a partition that is a GRUB BIOS partition.
- esp — This indicates that the partition is a UEFI system partition.
- swap — This identifies swap partitions.
- LVM — This marks a partition as part of the logical volume manager (LVM) volume group.
However, certain flags only work with a specific partition scheme.
For example, the following command marks the first partition as bootable.
(parted) set 1 boot on
Similarly, the command-line equivalent of the command is:
sudo parted /dev/sdc set 1 boot on
Additionally, the "toggle" command is used to change a flag's state to either on or off. It accepts two values — the partition's number and the flag to be toggled. For instance, the following toggles the boot flag state of the first partition.
(parted) toggle 1 boot
Conclusion
parted is a powerful utility for managing disk partitions in Linux. With the flexibility of using either one of its two (interactive and command-line) modes, system administrators and users can easily manipulate disk partitions.
Additionally, its support for different operations, ranging from basic partitioning to recovering lost partitions, makes it an incredible tool for anyone tasked with managing storage systems.