- How to partition a drive on Linux
- How to partition a drive on Linux via command line
- Initializing a device with a partition table
- Creating a partition
- Checking the partition alignment
- Resizing a partition
- Removing a partition
- How to partition a drive on Linux via GUI
- Create new partition
- Resize a partition
- Delete a partition
- Closing Thoughts
- Related Linux Tutorials:
How to partition a drive on Linux
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
- How to partition a drive via command line
- How to partition a drive via GUI
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | parted, gparted |
Other | 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:
GNU Parted 3.4 Using /dev/sdX Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
(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:
(parted) mklabel New disk label type? msdos
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 :
(parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? ext4 Start? 0% End? 100%
(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 :
(parted) align-check alignment type(min/opt) [optimal]/minimal? minimal Partition number? 1 1 aligned
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
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
WARNING
There is no prompt for confirmation, so think twice before running this command to destroy an existing partition.
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.
To install gparted on Ubuntu, Debian, and Linux Mint:
To install gparted on Arch Linux and Manjaro:
After it is installed, follow the steps below to use gparted to create, resize, and delete partitions:
- Get started by searching for the gparted application in your desktop envrionment’s app launcher. You will be prompted for the root password upon opening the program.
Create new partition
As you can see, your disk currently has no partitions. Let’s add one by going to Device > Create Partition Table.
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.
NOTE
ext4 is the recommended file system to use on Linux systems, unless you have a special reason to format the disk with some other kind.
Resize a partition
Click on Partition > Resize in order to resize the currently selected partition.
Delete a partition
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.
Closing Thoughts
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.
Related Linux Tutorials:
Comments and Discussions