Disk, partitions, ext4, mkfs, /dev/sda, filesystem – these are common keywords when working with Linux servers, but what do they really mean?

Managing storage is a fundamental aspect of server administration, and it can initially seem intimidating. Terms like formatting, mounting, partition table (GPT, MBR), and choosing the right file system type (NTFS, FAT32, exFAT) can quickly become overwhelming.

This guide explains the essential concepts of disk partitioning and disk formatting in Linux. We’ll start by explaining what disk partitioning and disk formatting are – and why they’re necessary. Then, we’ll explore different types of file systems and help you understand their strengths and weaknesses. Finally, we’ll provide step-by-step instructions for both partitioning and formatting your disk on Linux systems.

So let’s get started!

What is a Disk Drive?

A disk drive, also known as a storage drive, is a non-volatile storage device for a computer. This device retains data even when the system loses power. The drive stores the operating system, applications, and user data, and the computer uses it to both read and write data.

Suggested read: 9 Best Linux Distros in 2024

What is a disk drive

Types of Disk Drives

Disk drives come in all shapes and sizes, each with its own speed and storage capabilities. Knowing their differences can help you pick the right one for your needs.

  • Hard Disk Drives (HDDs): HDDs store data on spinning magnetic platters and use a read/write head that moves across the surface. They are a cost-effective option for large storage capacities, but their mechanical nature results in slower access times.
  • Solid-State Drives (SSDs): SSDs use flash memory to store data, eliminating moving parts and providing significantly faster read/write speeds than HDDs. This results in faster boot times, application loading, and overall system responsiveness, although typically at a higher cost per gigabyte.
  • NVMe SSDs (Non-Volatile Memory Express SSDs): NVMe SSDs are a type of SSD that uses a high-speed PCIe interface. They deliver even greater performance than standard SATA SSDs. This interface allows for much higher bandwidth and lower latency, making them ideal for demanding workloads.

Suggested read: How to Check Running Processes in Linux Using ps, top, htop, and atop Commands

What is Disk Partitioning?

Disk partitioning is a fundamental aspect of storage management that divides a physical hard drive (or other storage device such as an SSD) into multiple logical sections. System administrators perform this division to organize data, isolate operating systems, or improve system performance.

Each partition functions as an independent unit, almost as if it were a separate physical disk. The operating system then treats each partition as a distinct volume. This separation allows multiple file systems and operating systems to coexist on a single physical drive and simplifies backup and recovery processes.

Suggested read: How to Kill a Process in Linux From the Command Line

How to Partition Disk Drive on Linux

Here’s a step-by-step guide to partitioning a disk drive on Linux:

Step 1: Identify the Disk

Before partitioning a disk, you need to identify the correct device name. Use the lsblk command in your terminal to list all block devices (disks and partitions) connected to your system.

After executing the command, carefully examine the output and pay attention to the NAME, SIZE, TYPE, and MOUNTPOINT columns.

You’re looking for a disk (indicated by TYPE being “disk”) that is not currently mounted (has no MOUNTPOINT) or is a disk you are sure you want to repartition (which results in losing all data stored on it). The NAME will typically be something like /dev/sda, /dev/sdb, /dev/nvme0n1, etc.

Be absolutely certain you have the correct device name – partitioning the wrong disk will result in data loss.

If in doubt, do not proceed!

To minimize risk, disconnect any non-essential external drives.

Step 2: Choose a Partitioning Tool

Linux offers several tools for partitioning disks, each with its own strengths. For command-line interaction, fdisk and parted are popular choices. fdisk is a classic, menu-driven utility suitable for most tasks, especially on disks using the older MBR partition scheme. On the other hand, parted supports both MBR and the newer GPT (GUID Partition Table) scheme and is often preferred for disks larger than 2TB.

The cfdisk utility offers a text-based interface that provides a middle ground between fdisk’s simplicity and parted’s power. GParted is a widely used and user-friendly option for a graphical interface, but it is not useful for Linux servers as they don’t have a GUI.

We’ll focus on using parted for this guide due to its versatility. To start the interactive partitioning session, we’ll use parted /dev/sdX (replacing /dev/sdX with the actual disk name).

Step 3: Create a New Partition

Once you’ve started parted with the correct disk (e.g., sudo parted /dev/sdb), you’ll be at the parted prompt. First, print the current partition table using the print command to double-check the device.

  • Select Partition Table Type (if needed): If the disk is brand new or you want to change the partition table type, use the mklabel command. For example, (parted) mklabel GPT.
  • Create the Partition: Use the mkpart command. You’ll be prompted to enter the following:
    • Partition type: primary, extended (only on MBR), or logical (only within an extended partition). With GPT, you’ll almost always choose the primary.
    • File system type: This is a hint for later formatting; it doesn’t actually format the partition. Choose ext4, fat32, ntfs, etc., as appropriate.
    • Start: Specify the partition’s starting point, either in megabytes (MB) or gigabytes (GB) or as a percentage of the disk (e.g., 0%).
    • End: Specify the partition’s ending point using the same units as the start (e.g., 100GB or 100%).
    • Example: (parted) mkpart primary ext4 0% 100% (creates a primary partition using the ext4 formatting that takes the entire disk).
  • Repeat (if needed): Repeat the mkpart command for each additional partition you want to create.

Step 4: Verify the Partition

After creating the partition(s), you can verify the changes using the print command within the parted prompt. This command displays the current partition table, showing the partition number, start and end points, size, file system type (hint), and any flags. Carefully review this output to ensure the partitions are as you intended.

If anything looks incorrect, you can use rm <number> (e.g., rm 1 to remove partition number 1) to delete a partition within parted and recreate it.

Once satisfied, type quit to exit parted and save the changes to the disk.

What is Disk Formatting?

Disk formatting prepares a storage device (the whole disk or a partition) for use by an operating system. This process creates a specific file system – a structured method for organizing and storing files. The operating system uses this file system to manage data on the device. Formatting lays the groundwork that defines how the operating system will track files, directories, and free space.

Suggested read: How to Check Linux CPU Usage or Utilization? (5 Ways)

What is a File System?

A file system is the underlying structure that an operating system uses to manage data on a storage device. It determines how files are named, stored, organized, and accessed. Think of it as the librarian of your hard drive, keeping track of where everything is located and how to retrieve it. Without a file system, a storage device would just be a collection of unorganized data.

There are multiple popular file system formats:

  • ext4 (Fourth Extended Filesystem): The ext4 file system is the standard and most widely used file system for Linux distributions. It’s a journaling file system that records changes before they are written to disk. This improves reliability and reduces the risk of data corruption in case of power failures or system crashes. ext4 offers excellent performance, supports large files and volumes, and is generally the best choice for Linux server environments. It also includes delayed and multiblock allocation features to enhance write performance.
  • NTFS (New Technology File System): NTFS is the primary file system used by Windows operating systems. Like ext4, it’s a journaling file system with improved reliability and data recovery capabilities. NTFS supports features such as file permissions, encryption (BitLocker), disk quotas, and shadow copies (for backups and restoring previous versions of files). It also supports much larger file and partition sizes than older FAT systems.
  • FAT32 (File Allocation Table 32): FAT32 is an older file system known for its broad compatibility. It’s commonly used on USB flash drives and SD cards because it can be read and written by virtually any operating system, including Windows, macOS, and Linux. However, FAT32 has significant limitations: it supports a maximum file size of only 4GB and a practical maximum volume size of 2TB. This makes it unsuitable for storing large files like modern video files.
  • exFAT (Extended File Allocation Table): exFAT is a Microsoft-developed file system designed to overcome the limitations of FAT32 while maintaining good cross-platform compatibility. It supports very large file and partition sizes, making it suitable for storing large media files and using large external drives. exFAT is often used on SDXC cards and large USB drives when compatibility with Windows and macOS is needed. It is often the preferred filesystem for USB thumb drives.

Suggested read: How do you check if the TCP port is open, closed, or in use on Linux?

How to Format a Disk Drive on Linux

Here’s a step-by-step guide to formatting a disk drive (or, more accurately, a partition) on Linux:

Step 1: Identify the Partition to Format

Before formatting, you must know the exact device name of the partition you want to format. Formatting the wrong partition will result in irreversible data loss.

Use the lsblk command in your terminal to list all block devices and their partitions. Carefully examine the output, paying attention to the NAME, SIZE, TYPE, and MOUNTPOINT columns.

You’re looking for the specific partition (e.g., /dev/sda1, /dev/sdb2, /dev/nvme0n1p1 – the “p” and number indicate a partition, not the whole disk). The TYPE column will show “part” for partitions. Double-check the SIZE to ensure it matches the partition you intend to format.

Step 2: Unmount the Partition

If the partition you intend to format is currently mounted (meaning it’s accessible as a directory), you must unmount it before formatting. Use the umount command followed by either the device name or the mount point. For example:

  • sudo umount /dev/sdX1 (replace /dev/sdX1 with the actual partition name)
  • sudo umount /mnt/mydirectory (if the partition is mounted at /mnt/mydirectory)

If the umount command reports that the device is busy, it means a process is still using the partition. You must identify and stop that process before you can unmount it.

Step 3: Format the Partition

Use the mkfs command (make file system) with the appropriate option for your chosen file system. The general format is: sudo mkfs -t [filesystem type] [options] /dev/sdX1. The -t option specifies the filesystem type. Here are the specific commands:

  • For ext4: sudo mkfs -t ext4 /dev/sdX1
  • For FAT32: sudo mkfs -t vfat -F 32 /dev/sdX1 (the -F 32 specifies FAT32; you can also use -F 16 for FAT16, but this is rarely needed)
  • For NTFS: sudo mkfs -t ntfs /dev/sdX1
  • For exFAT: sudo mkfs -t exfat /dev/sdX1

Remember to replace /dev/sdX1 with the actual partition name in the above examples.

Step 5: Verify the Format

After formatting the partition, you can verify the result using several methods:

  • lsblk -f: This command shows each partition’s file system type (FSTYPE) and UUID. Check that the FSTYPE matches what you intended.
  • blkid /dev/sdX1: This command specifically shows the specified partition’s UUID and TYPE (file system).
  • Mount and check: Mount the partition (e.g., sudo mount /dev/sdX1 /mnt) and then use df -h to check the disk space and file system type. This confirms that the partition is usable. After testing, you can unmount the partition with the command sudo umount /dev/sdX1 or sudo umount /mnt if that is where it is mounted.

Suggested read: How to Find Most Used Disk Space Directories and Files in Linux

Wrapping Up: Disk Partitioning and Formatting

Understanding the different file systems, choosing the right tools, and, most importantly, double-checking every command before execution are critical for maintaining data integrity and system stability. Remember to always back up important data before making any changes to partitions.

Managing Linux servers, especially tasks like disk partitioning and monitoring, can seem daunting. However, with tools such as RunCloud, the process becomes significantly easier and more streamlined.

RunCloud provides a user-friendly web interface that simplifies server management tasks, including monitoring disk usage, managing services, and deploying applications. Instead of wrestling with the command line for every operation, you can use RunCloud’s intuitive dashboard to oversee your server’s health and performance. This allows you to focus on your applications and websites rather than getting bogged down in the complexities of server administration.

Ready to experience the ease of Linux server management? Start your free trial with RunCloud today!

FAQs on Partitioning and Formatting Disk Drives on Linux

What is the difference between partitioning and formatting?

Partitioning divides a physical hard drive or storage device into logical sections. Each section, a partition, functions like an independent disk. On the other hand, formatting prepares a partition (or an entire unpartitioned disk) by creating a specific file system (e.g., ext4, NTFS, FAT32). This file system allows an operating system (such as Linux or Windows) to organize, store, and retrieve files. Partitioning divides the disk while formatting prepares a partition for data.

How do I know which file system to choose?

The best file system depends on system compatibility and your specific needs. Ext4 is generally recommended for Linux servers due to its performance, reliability, and features. If you require compatibility with Windows, NTFS or exFAT are good choices, especially for USB drives. FAT32 offers broad compatibility but has limitations on maximum file size and max volume size. Consider factors like journaling (for data recovery) and support for large files when selecting.

Can I resize partitions without losing data?

Yes, but it’s risky and requires careful planning. Tools like parted, GParted, and GNOME Disks allow resizing, but always back up your data first. Shrinking a partition is generally safer than expanding it, especially if the partition being expanded is at the end of the disk. If free space isn’t directly adjacent to the partition you’re expanding, data movement is required, increasing the risk of data loss if interrupted.

For command line (CLI) use, fdisk, parted, and cfdisk are powerful for partitioning.
mkfs (e.g., mkfs.ext4, mkfs.vfat) is used for formatting.
For a GUI, GParted and GNOME Disks offer user-friendly interfaces for both partitioning and formatting.
lsblk displays information about block devices (disks and partitions).
blkid shows UUIDs and file system types.
df helps monitor disk space usage.

Is it safe to format a disk drive?

Formatting is safe for the drive but permanently erases all data on the selected partition or disk. Always double-check that you’ve selected the correct partition (using lsblk, for example) before formatting. Ensure you have backups of any important data.

How do I recover data from a formatted drive?

Data recovery after formatting is possible but not guaranteed, and it becomes increasingly difficult the more the drive is used after formatting. Specialized data recovery software (often commercial) attempts to reconstruct file system structures. To maximize recovery chances, immediately stop using the drive after accidental formatting. Professional data recovery services are often the best option for critical data.

What are primary, extended, and logical partitions?

These terms relate to the older MBR (MS-DOS) partition table scheme. An MBR disk can have a maximum of four primary partitions. To overcome this limitation, one primary partition can be designated as an extended partition, which can then be subdivided into multiple logical partitions. GPT (GUID Partition Table), the modern standard, doesn’t have these limitations and supports many more partitions directly.

How do I check disk space usage on Linux?

The df (disk free) command is the primary tool. df -h displays usage in human-readable format (e.g., GB, MB). You can also use du (disk usage) to see the size of specific directories and files. GNOME Disks provides a graphical view of disk space.

Can you partition a drive without formatting?

Yes, you can. Partitioning simply creates the divisions on the disk. Formatting is a separate step that creates a file system within a partition to make it usable by an operating system. You might partition a disk and leave some partitions unformatted for later use or different purposes.