Parted is a free GNU utility for managing hard disk partitions from the command line. It can create, delete, resize and print disk partitions on Linux.
Generally, we use partitioning tools to partition disks to run multiple OSs, allocate specific system space, and separate valuable files or extended volumes.
Traditionally, many users use the fdisk tool to partition, which is the main reason for using parted when the disk size is greater than 2TB. Initially, only GPT was supported. Starting from util-linux 2.23, fdisk also supports GPT.
In this tutorial, I will show how to use the parted command for disk management in Linux
Install Parted on Linux
By default, Parted is installed in most modern Linux distributions. If it is not included in your distribution, install parted manually.
$ sudo apt-get install parted
Install separately on CentOS and RHEL:
$ sudo yum install parted
When you run the parted command without any options, it will print the version of the parted package, select the first drive by default, and wait for other commands when prompted. Parted commands must be run as the root user or a user with Sudo access.
$ sudo parted GNU Parted 3.2 Using /dev/xvda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
quit Exit from the separate prompt.
important: After typing the command, all changes will be made to the disk.
List disk partitions
Let’s check how to print all disk information and its partition. By default, parted selects the first drive. To print all disk partitions, enter
Note: The warning displayed in the output is because the disk has unallocated disk space yet to be provisioned.
[email protected]:~$ sudo parted GNU Parted 3.2 Using /dev/xvda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) print all Model: Xen Virtual Block Device (xvd) Disk /dev/xvda: 8590MB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 8590MB 8589MB primary ext4 boot Warning: Not all of the space available to /dev/xvdb appears to be used, you can fix the GPT to use all of the space (an extra 25165824 blocks) or continue with the current setting? Fix/Ignore? Ignore Model: Xen Virtual Block Device (xvd) Disk /dev/xvdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 1024MB 1024MB ext4 primary 2 1024MB 2048MB 1023MB ext4 primary (parted)
Instead, you can use a single command:
$ sudo parted /dev/xvda print all
If there are multiple disks, you can use the select command to choose between the disks:
Find unallocated space on disk
To find the unallocated space, select the disk and run
print free command.
Later, we will discuss how to use the resizepart command to expand the partition when there is more unallocated disk space.
Use parted to create a new disk partition
Usually, the operating system is installed on the first disk
/dev/sda. When adding a new disk, the operating system will select the next number as
/dev/sdb. To illustrate that I am using a Xen virtual disk, the virtual disk uses the naming convention as
I added a new 20GB disk, you can see
unrecognised disk label.
The first step is to set the required disk labels. The supported disk labels are bsd, loop, gpt, mac, msdos, pc98 and sun.
(parted) mklabel msdos
Now, I divide /dev/xvdb into two main partitions, the first partition is 10GB and the second partition is 5GB.
To create a new partition, we use
mkpart Commands to start 0 and end 10000:
To create the second partition, run the mkpart command again, specifying the start and end sizes
Note: The “main” concept is reflected in the MBR, GPT does not care, but the name must still be added.
You can also use mkpart to create a partition across the entire drive by specifying the percentage to use (here 0% to 100%).
$ sudo parted -a opt /dev/sda mkpart primary ext4 0% 100%
In addition to using the print command, you can also run other commands, such as
to see partitions created.
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 18M 1 loop /snap/amazon-ssm-agent/1566 loop1 7:1 0 93.8M 1 loop /snap/core/8935 loop2 7:2 0 93.9M 1 loop /snap/core/9066 xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdb 202:16 0 20G 0 disk ├─xvdb1 202:17 0 9.3G 0 part └─xvdb2 202:18 0 4.7G 0 part $
Now we can use the ex4 file system to format the partition (/dev/xvdb1), using mkfs.ext4 as follows:
$ sudo mkfs.ext4 /dev/xvdb1
Use resizepart to adjust the size of the disk partition
To expand a partition, you must first adjust its size. Simple resizing means moving the end of the partition.
Here, I will resize the second partition of /dev/xvdb and move the end position to 20000:
Note: growpart is another convenient tool for extending partitions on Linux.
To adjust the size of each file system to the new capacity, you must run file system-specific commands. Using extended file system in Linux
resize2fs The command is as follows:
$ sudo resize2fs /dev/xvdb2 resize2fs 1.44.1 (24-Mar-2018) Resizing the filesystem on /dev/xvdb2 to 2441340 (4k) blocks. The filesystem on /dev/xvdb2 is now 2441340 (4k) blocks long.
Delete partition from selected disk
To delete a partition, you should know the partition number on the disk. Adopt
To delete, you can use the rm command, followed by the partition number. Here we want to delete the second partition as follows:
(parted) rm 2
Set a flag on the partition
Parted allows setting flags on partitions. Don’t worry about certain logos depending on the disk label. The sign can be on or off. The most common signs are boot, laboratory, exchange, raid, LVM, etc.
The following command sets the LVM flag on partition 2:
(Separately) set 2 LVM in
Another practical example when you need to set up a boot partition:
(parted) set 2 boot on
Rescue Linux disk partition
When you accidentally delete a partition, rescue will help. The lost partition can be recovered by locating between the beginning and the end.
Let’s delete partition 1 on /dev/xvdb and restore it using rescue command:
Set default unit
The “Units” command in the division helps to set the default units to show capacity and location. Supported units are:
KiB- kibibyte MiB - mebibyte GiB - gibibyte TiB - tebibyte kB - kilobyte MB - megabyte GB - gigabyte TB - terabyte % - percentage of the device cyl - cylinders chs - cylinders, heads, sectors addressing compact - Use human-readable representation for output
The following command sets the unit to compact:
(parted) unit compact (parted) print Model: Xen Virtual Block Device (xvd) Disk /dev/xvda: 8590MB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 8590MB 8589MB primary ext4 boot (parted)
You can also print in the following units:
(parted) unit GB print Model: Xen Virtual Block Device (xvd) Disk /dev/xvda: 8.59GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 0.00GB 8.59GB 8.59GB primary ext4 boot (parted)
When updating this tutorial, we are using version 3.1, which is suitable for verifying the currently supported commands
-h Options. Parted is written directly to disk, so you should be very careful when running any commands.
When making any changes, make sure to select the correct drive, otherwise data may be lost.
If you have any questions or thoughts on this topic, please use the comments section below.