How to Partition a Disk in Linux

How to Partition a Disk in Linux

Mastering disk management is a pivotal skill for anyone diving into the Linux ecosystem. Whether you’re setting up a new drive, carving out space for different file systems, or optimizing storage for virtual machines, understanding disk partitioning can be a lifesaver. In this guide, we’ll demystify the process and walk you through the steps to partition a disk in Linux, ensuring you have the foundational knowledge to handle your storage needs with confidence.

Key Reasons to Partition a Disk

Partitioning a disk in Linux offers several advantages. Here are some of the primary reasons why you might want to partition a disk:

  1. Organization: Creating distinct partitions allows you to segregate and systematically manage data, making it easier to locate and access files or directories based on their importance or frequency of use.
  2. Performance Optimization: Separate partitions, especially on hard disk drives, can lead to faster access times. Having a dedicated partition for specific tasks, like swapping or caching, can boost system performance.
  3. Multi-OS Booting: For those wanting to run multiple operating systems on a single machine, separate partitions are essential. Each operating system typically requires its own dedicated partition.
  4. Data Security and System Stability: By isolating the operating system on its own partition, any failure or corruption in other partitions won’t affect the system’s operation. This also makes system backups or reinstalls simpler without impacting personal data.
  5. Flexibility in File System Choices: Different tasks or applications might benefit from different file systems. With multiple partitions, you can allocate specific file systems (like ext4, NTFS, Btrfs) based on your particular needs for each partition.

In essence, partitioning a disk provides a blend of efficiency, flexibility, and safety, making it a recommended practice for both casual users and IT professionals.

Process to Partition a Disk in Linux

Here’s a step-by-step guide to partitioning a disk in Linux, including expected output and descriptions.

#1 Identify the Disk

Identify the disk you want to partition. In this example, we’ll be working with /dev/sda. This command lists all the disks and their partitions:

sudo fdisk -l

Sample Output:

Snapshot of sudo fdisk -l command in Linux Terminal.

#2 Start the Partitioning Tool

This command opens the fdisk utility for the specified disk (/dev/sda in this example).

sudo fdisk /dev/sda

Sample Output:

#3 Create a New Partition

Use this command to create a new partition (inside fdisk):

n

Sample Output:

Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Choose p for primary or e for extended. Most users will choose p for a primary partition.

#4 Specify Partition Details

After selecting “p” this is the expected output:

Partition number (1-4, default 2): 
First sector (499712-488397134, default 499712):
Last sector, +sectors or +size{K,M,G,T,P} (499712-488397134, default 488397134): 

Description:

  • Choose a partition number (usually default is fine).
  • Specify the starting sector (usually the default is fine).
  • Specify the size of the partition. You can use the +sizeG format to specify size in gigabytes (e.g., +20G for a 20GB partition).

#5 Save Changes

This command writes the changes, creating the new partition.

w

Sample Output:

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

#6 Format the New Partition

To set up the new partition, execute the following command. Ensure you substitute ‘X‘ with the specific partition number you’ve established.

sudo mkfs.ext4 /dev/sdaX

Sample Output:

mke2fs 1.43.4 (31-Jan-2017)
Creating filesystem with 5120000 4k blocks and 1280000 inodes
...

This step formats the new partition with the ext4 filesystem, making it ready for use.

#7 Mount the New Partition (Optional)

You can now mount the partition to a directory to start using it. This mounts the newly created partition to the desired directory path.

sudo mount /dev/sdaX /desired/directory/path

Congratulations! Now you’re able to partition a disk in Linux. Remember, before making any changes to disk partitions, it’s crucial to back up important data. Partitioning can result in data loss if not done carefully. Always double-check your commands and actions!

Conclusion

Looking to deploy Linux in the cloud? With Gcore Cloud, you can choose from Basic VM, Virtual Instances, or VPS/VDS suitable for Linux:

Choose an instance

Subscribe to our newsletter

Stay informed about the latest updates, news, and insights.