Home Linux Disk Management Create And Manage Disk Partitions With Parted In Linux

Create And Manage Disk Partitions With Parted In Linux

Parted Command Examples To Create Disk Partitions In Linux

By Karthick
Published: Last Updated on 1,040 views

There are many utilities, such as fdisk, cdisk, exists to partition the hard drive in Linux and Unix-like systems. In this article, we will focus on how to use the parted utility to create a partition and manage disk partitions (resizing, deleting partitions) in Linux.

What is Parted?

GNU Parted is a command line suite to create and manipulate partition tables in Linux and Unix systems. Using Parted, one can create, resize, delete partitions, reorganize disk usage, copy data and create disk images.

Parted contains a library, libparted, as well as a command-line frontend, parted, which can also be used in scripts. Parted is designed to minimize the chance data loss. It performs many safety checks in order to avoid data loss in unforeseen situations, for example power failure.

Parted is free and open source program that ships with modern Linux operating systems by default.

Install Parted in Linux

Parted comes installed with all Linux distributions. In case if parted is not available in your distribution, you can run the following commands to get it installed.

To install Parted on Alpine Linux, run:

$ sudo apk add parted

On Arch Linux, EndeavourOS, and Manjaro Linux, run:

$ sudo pacman -S parted

For Debian/Ubuntu and its derivatives, run the following command.

$ sudo apt -y install parted

Fedora, RHEL, AlmaLinux, and Rocky Linux:

$ sudo dnf install parted -y

Choosing The Right Device

The first thing to note before using parted is which disk you are going to partition. When you run the parted command without passing the disk name as the argument, it will take the first block device it finds and use it.

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

Heads Up: You should either use sudo or switch as root user and run the parted command.

To use the particular device, you have to pass the device (E.g. /dev/sdc) as the argument to the parted command.

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

Alternatively from inside the parted utility, you can switch between different drives using the "select" command.

(parted) select /dev/sdb
Using /dev/sdb
(parted) select /dev/sdc
Using /dev/sdc

To get the list of parted commands, run the help command.

(parted) help

  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted

Each of these commands has a separate help section. For example, if you want to get the supporting options for the print command you can run the following command.

(parted) help print

If you wish to see the partition table for all the available block devices run the following command from the terminal.

$ sudo parted -l

To see the partition table within the parted utility, use the "print" command.

The print command will give you information like disk model, disk size, partition scheme, and the partition table at last. Since this is a new disk there are no partitions created.

Print Command
Print Command

To get which partition scheme of the disk, you can run either the print command inside the parted console or run the following command directly from the terminal.

$ sudo parted /dev/sdc print | grep -i table
Partition Table: gpt

You can get the block device names alone by passing "devices" as an argument to print command.

(parted) print devices
/dev/sdc (21.5GB)
/dev/sda (53.7GB)
/dev/sdb (21.5GB)
/dev/sr0 (61.1MB)

You can get the amount of free space left on the disk by passing "free" as an argument to print command.

(parted) print free                                                       
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
        17.4kB  21.5GB  21.5GB  Free Space

To print all the device information within parted, pass "list,all" as an argument to the print command.

(parted) print list,all

Unlabeled Disk

Before creating partitions using the drive, the drive should be labeled to use a particular partition scheme. If you run the print command on an unlabeled disk you can see the ERROR.

(parted) print
Error: /dev/sdc: unrecognised disk label
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:

To create a label you can use either mklabel or mktable command. If you have no idea about the list of supported labels, run "help mklabel" or "help mktable".

Display mklabel command help
Display mklabel command help

I am going to label this drive to use the GPT scheme. If you need an MBR partition scheme, use msdos.

(parted) mklabel gpt

Now if you run the print command again, you can see the label.

Caution: Before running this command, make sure you are using the correct disk. Because this command can wipe out the entire partitions in a disk with its data.

Create New Partitions With Parted

To create partitions, you have to use the mkpart command.

Before running the mkpart command, take a look at the help information which will give the details about the list of options.

help mkpart

I am going to create two partitions of size 10GB each. Let's create the first partition.

(parted) mkpart
Partition name? []? project
File system type? [ext2]? ext4
Start? 1
End? 10GB

This step will be interactive.

  • Choose a name for the partition.
  • Choose the file system type. By default, it is set to ext2.
  • Choose the starting location of the disk. You can specify in terms of MB, GB. Here I have given as 1, so it will be starting from 1MB.
  • Choose the end location of the disk. Here, 10GB is given.

Run the print command and you can see the partition created.

(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
​
Number Start   End     Size   File system Name                 Flags
1     1049kB 10.0GB 9999MB               project

Let's create the second partition. You can combine all the interactive inputs and pass them as arguments to the mkpart command.

(parted) mkpart project1 ext4 10001MB 19999MB

The partitions you created are not yet saved. You have to use the "partprobe" command to inform the kernel about the partition table changes you made.

Exit out of parted utility by typing "quit" and run the partprobe command.

$ sudo partprobe

Delete Partitions

You can remove the partitions using the rm command. The rm command accepts the partition number as an argument and deletes that partition.

help rm

If the partition is active and already has a file system mounted, then it is best practice to clean up the data first, unmount the file system and then delete the partition.

If you try to delete the partition when in use, you will be prompted with a confirmation to remove the partition and it will throw you an error saying the changes will be effective post-reboot. Choose "Ignore".

(parted) rm 1
Warning: Partition /dev/sdc1 is being used. Are you sure you want to continue?
Yes/No? yes
Error: Partition(s) 1 on /dev/sdc have been written, but we have been unable to inform the kernel of the
change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You
should reboot now before making further changes.
Ignore/Cancel? ignore

Tagging Name To The Partition

You can use the "name" command to rename or assign a name to the partition. The name command accepts the partition number as the first argument followed by the new name.

(parted) name 2 p1

Here I am changing the name of partition 2 from project1 to p1.

Resize The Partition

You can shrink or extend the partition using the resizepart command. The command resizepart takes the partition ID as an argument. It will ask you what should be the size(End?).

Let’s shrink the first partition I created. The partition ID is 1 and the partition size is 10GB. Here I am shrinking the partition to 5GB.

(parted) resizepart 1
End? [10.0GB]? 5 GB
Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
Yes/No? yes

I have my second partition with 5GB space and I wish to add 2 more GB and make it 7GB.

(parted) resizepart 2
End? [15.0GB]? 17GB

Now if I print my partition table my first partition is shrinked to 5GB and the second partition is extended to 7GB.

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  5000MB  4999MB  ext4         p1
 2      10.0GB  17.0GB     7000MB  ext4         p2

Modify Partition Flags

Using the set command, you can assign a flag to the partition. To get the list of supported flags, run the following help command.

(parted) help set
Set command help
Set command help

Let’s say if you want to tag a partition as a swap partition, you have to run the command as follows

set [partition name] [flag] [ON/OFF]

(parted) set 1 swap ON

Create File System

Till now you have learned how to work with parted to do different disk partition operations. As you may already know, you need to format the partition and mount it to start using the partition.

You can check the partitions created by running the lsblk command.

$ lsblk
Lsblk command output
Lsblk command output

Let’s create a file system of type ext4 for /dev/sdc1 by running the following command.

$ mkfs.ext4 /dev/sdc1

Now mount the file system in any directory as you wish and use the partition.

$ mount /dev/sdc1 /mnt/
$ df -h /dev/sdc1
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc1       4.6G   19M  4.3G   1% /mnt

Conclusion

In this article, we have discussed how to use parted utility to create and manage disk partitions with Parted program in Linux. We also looked how to format the newly created partitions with ext4 filesystem and mount it in our system.

Starting from RHEL version 8, Red Hat is suggesting to use parted utility instead of fdisk. So If you are someone preparing for RHCSA certification then this article will be of great use to you.

For Linux desktop users, there is a GUI version of parted called GParted. We will talk about Gparted in detail in a separate article soon. Stay tuned!

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More