How To Format A Disk In Linux

How To Format A Disk In Linux

Reading time1 min
#Linux#Storage#Filesystems#DiskFormatting#Partitioning#SSDOptimization

Mastering Disk Formatting in Linux: Expert-level Guidance

Misaligned partitions. Mismatched filesystems. Missed tuning opportunities. All commonplace, often ignored — until performance drops or data integrity issues emerge. Systems teams can’t afford those oversights, particularly when prepping SSDs or high-capacity drives for production workloads.


Prerequisites: Locate with Certainty

Run lsblk to visualize device topology. Never trust /dev/sdx naming alone—these mappings can shift after reboots or hot-pluggable events.

lsblk -o NAME,SIZE,TYPE,FSTYPE,UUID,MOUNTPOINT

Need deeper metadata (sector size, partition table type)? Use:

sudo fdisk -l /dev/sdb

Note: If dealing with NVMe drives (/dev/nvme0n1), partition names look like /dev/nvme0n1p1.

Absolutely confirm target media. Formatting is destructive and non-recoverable.


Partitioning: Alignment for Modern Devices

Unaligned partitions create non-trivial write-amplification, degrading SSDs quickly. Modern sector sizes (4K and up) demand explicit control.

Preferred: GPT with 1MiB-aligned partitions.

sudo parted /dev/sdb --script mklabel gpt
sudo parted /dev/sdb --script mkpart primary ext4 1MiB 100%

Alignment check (critical, especially for Samsung SSDs or WD Red drives):

sudo parted /dev/sdb align-check optimal 1

A non-aligned result? Reconfigure partition boundaries before proceeding. Miss this and expect erratic IOPS under load.


Filesystem Selection: Addressing Workload-Specifics

Choosing the wrong filesystem is rarely recoverable after the disk is live. Consider:

FilesystemKernel Support (as of 6.1)Notable ForCaveats
ext4Native, >15y matureSafety, broad supportLacks built-in compression
XFSNative, large scale (10TiB+)High concurrencyLess flexible for snapshots
BtrfsNative, evolving rapidlySnapshots, self-healUnforgiving under RAM stress
F2FSNative, SSD/flash-tunedSequential write speedOccasional kernel regressions

For generic datasets: ext4 or xfs.
For snapshotting, deduplication, or integrated compression: btrfs.
On single-board computers or eMMC/SD-backed systems: f2fs.


Creating and Formatting Partitions

Format with volume label and tuned reserved blocks:

sudo mkfs.ext4 -L proddata -m 1 /dev/sdb1
# For XFS
sudo mkfs.xfs -L proddata /dev/sdb1
# For Btrfs
sudo mkfs.btrfs -L proddata /dev/sdb1
  • -m 1 reduces reserved space to 1% (good for non-root-volumes >500GB; not for root).
  • Labels matter for orchestration scripts and automation hooks.
  • If you see:
    mkfs.ext4: Device size reported to be less than 1G.
    
    — Possible partition table overlap or incorrect start boundary. Recheck parted configuration.

Mount & Tuning: Squeeze Out IOPS

Edit /etc/fstab directly; rely on UUIDs, not device names:

UUID=4242-1a2b /data ext4 defaults,noatime,nodiratime,commit=60 0 2
  • noatime,nodiratime: Avoids redundant metadata writes.
  • commit=60: Extends journal commit interval; reduces write amplification on SSDs, at the slight risk of longer data loss window after a crash.

Post-format tuning:
Lower reserved blocks, disable auto fsck if monitored via other mechanisms.

sudo tune2fs -m 1 /dev/sdb1
sudo tune2fs -c 0 /dev/sdb1

Known issue: Some OS images forcibly revert tune2fs -c on reboot via systemd generator scripts.


Advanced: Pre-flight Diagnostics & Secure Erase

Check drive SMART status before putting into production:

sudo smartctl -a /dev/sdb

Don’t ignore Reallocated_Sector_Ct or high Wear_Leveling_Count on SSDs. Early signs of failure are often missed by routine monitoring.

Need a true secure erase for SSD/media repurposing?
Careful—ATA Secure Erase (supported on most SATA SSDs, e.g., Samsung 870 EVO) is irreversible:

sudo hdparm --user-master u --security-set-pass linux /dev/sdb
sudo hdparm --user-master u --security-erase linux /dev/sdb

Vendor SSD utilities sometimes required for NVMe (e.g., nvme format /dev/nvme0n1 --ses=1 for Micron/SK hynix).


Quick Workflow Checklist

  • Locate disk: lsblk, fdisk — confirm physical mapping.
  • Partition using GPT, align at 1MiB — don’t trust GUI tools’ “defaults”.
  • Choose filesystem relevant to workload.
  • Format with custom label, tune reserved blocks.
  • Set UUID-based fstab entries.
  • Apply mount options suited to the device: noatime, commit=60, etc.
  • Inspect SMART status before handoff.
  • Backups before touch—always.

Note: Some vendors ship SSDs with outdated or buggy firmware. Always check for updates before placing disks under production write loads; substantial performance improvements (or data corruption fixes) in changelogs aren’t uncommon.

Mastering disk formatting in Linux is fundamentally about precision—whether prepping block devices for PostgreSQL clusters, Ceph OSDs, or simple user volumes. Ignore details, and you inherit problems. Do it right, and the filesystems become invisible infrastructure.

For expansion: mounting intricacies (ACLs, selinux contexts), ZFS on Linux, or lived experience with multi-pathing and RAID—open topics.