Managing Disks and Partitions with fdisk and parted

Introduction

Managing disks and partitions is a core skill for any system administrator or power user. Whether preparing a fresh server, resizing filesystems on-the-fly, or recovering from a corrupted table, fdisk and parted are two of the most versatile command-line utilities available on Linux.

Understanding Disk Partitioning

Master Boot Record (MBR)

  • Sector limit: Up to 2 TB per disk.
  • Partition table: Located in the first 512 bytes.
  • Max partitions: Four primary, or three primary one extended.
  • Legacy compatibility: Well-supported by older BIOS systems.

GUID Partition Table (GPT)

  • Sector limit: Virtually unlimited (up to 9.4 ZB).
  • Partition table: Redundant headers at beginning and end.
  • Max partitions: Typically 128 by default.
  • UEFI requirement: Standard on modern systems for booting.

fdisk: A Traditional MBR Tool (and GPT-aware on Newer Releases)

Invoking fdisk

To examine all connected block devices:

sudo fdisk -l

To work on a specific disk:

sudo fdisk /dev/sdX

Common Interactive Commands

Command Description
p Print current partition table
n Add a new partition
d Delete a partition
a Toggle bootable flag
t Change partition type
w Write table to disk and exit
q Quit without saving changes

Non-Interactive Scripting

You can pipe commands via echo or use a here-document. Example:


sudo fdisk /dev/sdX ltltEOF
n
p
1

 10G
w
EOF
  

parted: Flexible GPT and MBR Management

Setting a Partition Table Label

parted supports both msdos (MBR) and gpt:


sudo parted /dev/sdX mklabel gpt
  

Interactive Mode


sudo parted /dev/sdX
(parted) print
(parted) mkpart primary ext4 1MiB 20GiB
(parted) set 1 boot on
(parted) quit
  

Non-Interactive Usage amp Units

By default, parted uses MiB and GiB. To work in sectors, bytes or percentages:


sudo parted -s /dev/sdX unit s mkpart primary ext4 2048 409600
  

Resizing Partitions and Filesystems

  1. Unmount the filesystem: sudo umount /dev/sdX1
  2. Resize partition: parted /dev/sdX resizepart 1 50GiB
  3. Grow the filesystem: for ext4, sudo resize2fs /dev/sdX1

Aligning for Performance

Modern SSDs and RAID arrays benefit from proper alignment. Use MiB boundaries:


(parted) mkpart primary ext4 1MiB 100%
  

Best Practices and Caution

  • Backup first: Always snapshot or image disks before modifying partitions.
  • Double-check device names: Mixing up /dev/sda and /dev/sdb can be disastrous.
  • Sync and refresh: Run sudo partprobe or reboot to ensure the kernel sees changes.
  • Filesystem check: After resizing, run fsck to verify integrity.

Securing Remote Disk Management

When administering disks on remote servers, encryption and secure tunnels are vital. Typical workflow:

  1. SSH into the server: ssh user@host
  2. Use sudo fdisk or sudo parted as above.
  3. Ensure your connection is protected by a VPN:

Conclusion

Mastering fdisk and parted gives you full control over disk layout, offering legacy compatibility and modern flexibility. By combining careful planning, best practices, and secure remote access (e.g., via a reputable VPN), you can confidently partition, resize, and maintain storage on any Linux system.

Download TXT




Leave a Reply

Your email address will not be published. Required fields are marked *