Every hard disk, in order to be accessible under Linux, must have at least one partition on it. A partition is a way to logically separate different sections of a disk. For example, a 4 TB hard drive could have four different 1 TB partitions, and all would appear as separate storage systems under the operating system. Alternatively, a hard disk could simply contain a single partition that spans the entire volume. The configuration is entirely up to the user.
Whatever configuration you decide for your hard disk, setting up partitions is one of the most essential and dangerous task to perform when working with operating systems. It is possible to create new partitions, delete partitions, and to shrink or expand existing partitions. In the sections below, we will assume that you need to add a new partition to a hard disk that is either currently unpartitioned or already contains some partitions.
In this tutorial, we will cover the step by step instructions to partition a hard drive or solid state drive on an existing Linux system. We will show the necessary steps for both a brand new disk and one that already has one or more partitions on the disk. You will see the steps for both command line and GUI methods, so you can follow along with set of instructions you are most comfortable with. Let’s get started.
In this tutorial you will learn:
How to add partition to new, blank disk
How to add to, delete, and resize an existing partition table
Privileged access to your Linux system as root or via the sudo command.
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
How to partition a drive on Linux via command line
Your steps for partitioning a hard drive on Linux will vary depending on whether your disk already contains existing partitions or not. For example, if your hard disk already contains a partition that spans the entire volume, then you will first need to shrink the existing partition in order to be able to add a new one to the disk.
In the step by step instructions below, we will initialize a disk with a partition table, create a new partition, check the partition alignment, resize a partition, and remove an existing partition. This should cover a variety of scenarios depending on whether you are trying to partition a new disk or one with existing partitions.
Initializing a device with a partition table
The device we will be working with in these steps is /dev/sdX. To run in interactive mode we must launch parted with root permissions, passing as an argument to the command, the path of device we want to operate on, in this case:
$ sudo parted /dev/sdX
The parted prompt will be opened:
GNU Parted 3.4
Using /dev/sdX
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
First, need to visualize the current state of the drive. This will tell us what the current partition table looks like, or if one even exists at all. Therefore we will use the print command:
(parted) print
Error: /dev/sdX: unrecognised disk label
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdX: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
As you can see, since /dev/sdX does not contain a partition table, parted only shows us information about the disk model, total size and sector size. To be able to use the disk we need to initialize it, therefore we must create a partition table on it.
The command that allows us to create a partition table is mklabel. If we do not specify what kind of partition table we want to create, parted will ask us in the prompt:
(parted) mklabel
New disk label type? msdos
In this case we create a traditional msdos partition table. Other valid values are “aix”, “amiga”, “bsd”, “dvh”, “gpt”, “loop”, “mac”, “pc98” and “sun”. As said before, we could have also specified the type of partition table as an argument to the mklabel command.
Creating a partition
Next, we will create our first partition on the device. We will need to provide the partition type, choosing between primary or extended, the filesystem type (optional), the partition starting point and the partition ending point. Again if not provided directly, those values will be requested interactively. The command to create a partition is mkpart:
In this example, we have chosen to create a primary partition, formatted with the ext4 file system, and we are letting the partition span the entire disk, hence it starts at 0% and ends at 100%. Alternatively, you can specify a start and end in MiB or GiB values instead of percentages as we did above.
We can now run the print command again in order to see the partition we have just created:
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdX: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 42.9GB 42.9GB primary ext4 lba
Checking the partition alignment
The alignment of a partition is a very important factor to optimize performance. With parted, we can check two type of alignments, minimal and optimal. In minimal mode, the program checks that the partition respects the minimum alignment value to physical blocks, while in optimal mode, it checks if the partition is aligned to a multiple of the physical block size, to provide optimal performances. The command to use to perform those checks is align-check:
Once the command is run in interactive mode, we are prompted to provide the type of alignment we want to check (optimal is used by default) and the partition number (1). In this case parted confirmed that the partition is aligned correctly.
Resizing a partition
Resizing a partition is also a very dangerous operation, especially if the partition already contains a filesystem. Be aware that when changing the size of a partition, parted will never adapt the filesystem to it, therefore, especially when shrinking, you must use the dedicated tools to resize the filesystem in use first. The command used to perform a partition resize is resizepart. Our partition size currently consumes 100% of the disk; if for example, we would like to extend it to cover only 50% of the device instead, we would type:
(parted) resizepart
Partition number? 1
End? [42.9GB]? 50%
Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
Yes/No? yes
The partition now covers only half the space on the device.
Removing a partition
Removing a partition is just as easy. Obviously we should perform such an operation with the greatest amount of caution. The command to use in this case is rm:
(parted) rm
Partition number? 1
To confirm your changes before exiting parted, use the print command, and then exit out of parted with quit:
(parted) print
And:
(parted) quit
How to partition a drive on Linux via GUI
There are many GUI programs which can also be used to create or edit partitions for a hard disk on Linux. In this tutorial, we will focus on using gparted, which, as the name implies, is the GUI counterpart of the command line parted tool that we used above.
You can use the appropriate command below to install gparted with your system’s package manager.
The first thing we need to do is select the correct device that we will be working with from the upper right corner. On our test system, this would be /dev/sdb as indicated in the screenshot below.
👁 First select the correct hard disk to work with in gparted First select the correct hard disk to work with in gparted
Create new partition
As you can see, your disk currently has no partitions. Let’s add one by going to Device > Create Partition Table.
Next, select the type of partition you would like to create. We will by sticking with msdos in our tutorial but feel free to pick something else. GPT is recommended for hard disks greater than 2 TB in size.
👁 Select partition type and confirm that these changes will erase current disk data Select partition type and confirm that these changes will erase current disk data
Now that the partition table has been created, we can move forward with adding a new partition to the hard disk. This option can be found by heading to Partition > New. 👁 Select to create a new partition Select to create a new partition
On this menu, we get to select the size of our new partition. Rather than working with exact values, feel free to use the sliders with your mouse in order to configure the size that you want. For our example, we will simply make one partition that spans the entire size of the hard disk. Click ‘Add’ when ready to proceed.
👁 Select size of partition and optionally a name Select size of partition and optionally a name
The changes are not yet written to the disk, but we can see in the preview pane the configurations that we are about to apply. Once you are ready, click on Edit > Apply All Operations.
👁 Applying the changes to disk Applying the changes to disk
Resize a partition
Click on Partition > Resize in order to resize the currently selected partition.
Enter the new start and end points for your partition. Alternatively, simply use the slider to adjust the size of the partition. In this case, we will shrink our partition to about half the size.
👁 Enter values and then click Resize when done Enter values and then click Resize when done
To delete the partition, just select it in gparted and navigate to Partition > Delete. When done, click Edit > Apply All Operations to confirm the changes.
In this tutorial, we saw how to partition a hard drive from command line and GUI on a Linux system. Managing partitions is a dangerous task that should be performed with the utmost caution. The command line and GUI both prove as viable methods for managing partitions, especially in the case of parted and gparted, which are closely related tools.