DEV Community

Aviral Srivastava
Aviral Srivastava

Posted on

Disk Partitioning Strategies (fdisk, parted)

Disk Partitioning Strategies: A Deep Dive with fdisk and parted

Introduction

Disk partitioning is the process of dividing a physical hard drive (or solid-state drive) into multiple logical storage units called partitions. Each partition can be treated as a separate disk drive, allowing you to install multiple operating systems, segregate data, or improve overall system organization and performance. Understanding disk partitioning strategies and the tools used to implement them is crucial for system administrators, developers, and even power users looking to maximize their storage capabilities and maintain a well-structured system. This article will delve into the world of disk partitioning, focusing on two widely used command-line utilities: fdisk and parted. We will explore their features, advantages, disadvantages, and provide practical examples to help you master disk partitioning.

Prerequisites

Before diving into the specifics of fdisk and parted, it's important to understand the fundamental concepts of disk partitioning.

  • Storage Device Naming: In Linux-based systems, storage devices are typically identified using names like /dev/sda, /dev/sdb, /dev/nvme0n1, etc. /dev/sda usually represents the first SATA or SCSI drive, /dev/sdb the second, and so on. nvme0n1 represents the first NVMe drive. Partitions are appended to the device name, for example, /dev/sda1 represents the first partition on /dev/sda.

  • Partition Tables: A partition table is a data structure that resides at the beginning of the disk and describes the layout of the partitions. The two most common partition table schemes are:

    • MBR (Master Boot Record): An older standard that supports a maximum of four primary partitions or three primary partitions and one extended partition containing logical partitions. MBR has a size limitation of 2TB per partition.
    • GPT (GUID Partition Table): A modern standard that overcomes the limitations of MBR, supporting a virtually unlimited number of partitions and larger disk sizes (up to 8 ZiB). GPT is required for UEFI-based systems.
  • File Systems: A file system organizes data on a partition, allowing the operating system to read, write, and manage files. Common file systems include ext4, XFS, Btrfs (for Linux), NTFS (for Windows), and HFS+ (for macOS). A partition must be formatted with a file system before it can be used to store data.

  • Root Privileges: Partitioning operations require root privileges. You'll need to use sudo or login as root to execute fdisk or parted.

fdisk: The Classic Partitioning Tool

fdisk (Fixed Disk) is a command-line utility available on most Linux distributions for creating and managing partitions on hard drives. It primarily works with MBR partition tables but also supports GPT on newer versions.

Features of fdisk:

  • Interactive Mode: fdisk operates in an interactive mode, allowing you to enter commands to perform various tasks.
  • MBR and GPT Support: Modern versions of fdisk handle both MBR and GPT partition tables.
  • Basic Partition Operations: It supports creating, deleting, modifying, and listing partitions.
  • Setting Partition Type: Allows you to specify the partition type, indicating its intended use (e.g., Linux, Swap, Windows).
  • Bootable Flag: Can set a partition as bootable, indicating that the operating system can be loaded from it.

Advantages of fdisk:

  • Widely Available: fdisk is a standard utility included in almost every Linux distribution.
  • Simple and Familiar: Its command-line interface is relatively straightforward for basic partitioning tasks.
  • Lightweight: fdisk has minimal dependencies and a small footprint.

Disadvantages of fdisk:

  • Non-Interactive Deletion: Deleting a partition is a destructive operation. fdisk doesn't offer a safety net.
  • MBR Limitations: When working with MBR, you're limited to four primary partitions or three primary partitions and one extended partition.
  • Clumsier GPT support: GPT support is present in newer versions, but it is less intuitive than parted.
  • Less Flexible: Compared to parted, fdisk offers fewer advanced features.

Using fdisk: Example

  1. Identify the Disk:

    sudo fdisk -l
    

    This command lists all available disks and partitions. Identify the disk you want to partition (e.g., /dev/sdb).

  2. Start fdisk:

    sudo fdisk /dev/sdb
    

    This starts fdisk in interactive mode for the specified disk.

  3. Common Commands:

    • m: Display the menu of available commands.
    • n: Create a new partition.
    • p: Print the partition table.
    • d: Delete a partition.
    • t: Change a partition's system ID (type).
    • w: Write changes to disk and exit.
    • q: Quit without saving changes.
  4. Creating a Partition (Example):

    Command (m for help): n
    Partition type
       p   primary (0 primary, 0 extended, 4 free)
       e   extended (container for logical partitions)
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-10485759, default 2048):
    Last sector, +/-size{K,M,G,T,P} (2048-10485759, default 10485759): +2G
    

    This example creates a 2GB primary partition. You'll be prompted for the partition number, first sector, and last sector or size.

  5. Writing Changes:

    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.
    

    After creating or modifying partitions, use the w command to write the changes to the disk.

parted: The Powerhouse Partitioning Tool

parted (Partition Editor) is a more advanced command-line utility that provides greater flexibility and functionality compared to fdisk. It supports both MBR and GPT partition tables and offers a wider range of operations.

Features of parted:

  • MBR and GPT Support: Supports both partition table schemes.
  • Resize Partitions: Can resize existing partitions, allowing you to adjust their size without data loss (in some cases, and depending on the file system).
  • File System Creation: parted can create file systems on partitions.
  • Rescue Damaged Partitions: Offers some capabilities to attempt to rescue data from damaged partitions.
  • Scripting Support: Can be used in scripts for automated partitioning.
  • Units of Measure: Supports various units of measure (e.g., MB, GB, TB).
  • Non-Destructive Operations (generally): While still dangerous if used incorrectly, parted tends to be more careful about avoiding data loss.

Advantages of parted:

  • More Powerful: Offers a broader range of features and options compared to fdisk.
  • GPT Support: Provides excellent support for GPT partition tables.
  • Resizing Capabilities: Allows resizing of partitions.
  • Scripting: Suitable for automated partitioning tasks.

Disadvantages of parted:

  • More Complex: The command-line interface can be more complex than fdisk.
  • Higher Learning Curve: Requires more time to learn and master.
  • Requires More Resources: Can consume slightly more system resources.

Using parted: Example

  1. Identify the Disk: (Same as fdisk example)

    sudo parted -l
    
  2. Start parted:

    sudo parted /dev/sdb
    
  3. Common Commands:

    • mklabel <disk label type>: Create a new disk label (e.g., mklabel gpt or mklabel msdos).
    • mkpart <partition type> <file system type> <start> <end>: Create a new partition (e.g., mkpart primary ext4 0% 100%). The start and end are specified as percentages or absolute positions.
    • print: Display the partition table.
    • rm <partition number>: Delete a partition.
    • resize <partition number> <start> <end>: Resize a partition.
    • quit: Exit parted.
  4. Creating a GPT Partition (Example):

    sudo parted /dev/sdb
    (parted) mklabel gpt
    (parted) mkpart primary ext4 0% 100%
    (parted) print
    (parted) quit
    

    This example creates a GPT disk label and then a single partition that occupies the entire disk, formatted as ext4.

  5. Creating MBR Partition (Example):

    sudo parted /dev/sdb
    (parted) mklabel msdos
    (parted) mkpart primary ext4 0% 50%
    (parted) mkpart primary ext4 50% 100%
    (parted) print
    (parted) quit
    

    This example creates an MBR disk label and then two primary partitions, each occupying half of the disk, both formatted as ext4.

Conclusion

fdisk and parted are essential tools for managing disk partitions in Linux environments. fdisk is a straightforward and widely available utility suitable for basic partitioning tasks, especially on MBR disks. parted, on the other hand, offers greater power and flexibility, particularly when dealing with GPT disks, resizing partitions, or automating partitioning processes. Choosing the right tool depends on your specific needs and level of expertise. Remember to always back up your data before performing any partitioning operations to avoid data loss. Understanding the features and limitations of each tool will allow you to effectively manage your storage and optimize your system's performance. Always double check your commands and target disks before applying changes, as incorrect partitioning can lead to data loss or system instability.

Top comments (1)

Collapse
 
hashbyt profile image
Hashbyt

This article provides a comprehensive overview of disk partitioning with fdisk and parted. It's a valuable resource for anyone looking to enhance their storage management skills. Great job!