Format a Linux Partition Using mkfs Command
This guide explains how the mkfs command formats a new
disk partition in the Linux operating system. You will learn the
mechanics behind how mkfs writes file system structures to
raw storage blocks, the role of specific file system drivers, and the
practical command-line steps required to prepare, format, and verify
partitions using formats like ext4 and XFS.
What is the mkfs Command?
The mkfs (make file system) tool is a front-end wrapper
in Linux used to build a file system on a storage device, typically a
drive partition. Rather than implementing every file system directly,
mkfs detects the requested file system type and calls the
appropriate backend utility, such as mkfs.ext4,
mkfs.xfs, or mkfs.vfat.
How mkfs Operates on a Partition
When executed against a raw block device (such as
/dev/sdb1), mkfs performs several low-level
operations:
- Overwrites Existing Headers: It clears existing signatures, partition tables, or old file system superblocks to prevent conflicts.
- Creates the Superblock: It writes the primary superblock and backup superblocks across the partition. The superblock stores essential metadata, including the block size, total block count, free block counts, and file system status.
- Allocates Data Structures: Depending on the file
system,
mkfscreates inode tables (used by ext4 to track file attributes and locations) or allocation groups and B-trees (used by modern systems like XFS for scalable storage indexing). - Initializes the Journal: For journaling file systems (ext3, ext4, XFS), it sets aside dedicated space for the journal, which logs operations before committing them to protect data against sudden power loss.
- Generates a UUID: It assigns a Universally Unique
Identifier (UUID) to the partition so it can be reliably mounted via
/etc/fstab.
Step-by-Step Guide to Formatting a Partition
1. Identify the Target Partition
Before formatting, locate the exact device path to avoid overwriting critical data.
lsblkLocate your unformatted partition in the output (e.g.,
/dev/sdb1). Ensure the partition is not mounted.
2. Execute the mkfs Command
You can format the partition by specifying the file system type flag
(-t) or by calling the specific wrapper command
directly.
Using ext4 (Common for general-purpose Linux installations):
sudo mkfs -t ext4 /dev/sdb1Alternative syntax:
sudo mkfs.ext4 /dev/sdb1Using XFS (High-performance, ideal for large files and enterprise servers):
sudo mkfs.xfs /dev/sdb1Using FAT32 (For cross-platform compatibility with Windows and macOS):
sudo mkfs.vfat -F 32 /dev/sdb13. Verify the New File System
Confirm that the file system structure was written properly using the
blkid or lsblk command:
sudo blkid /dev/sdb1The output displays the assigned UUID and the declared
TYPE (e.g., TYPE="ext4").
4. Mount the Formatted Partition
Once formatted, create a mount point directory and attach the partition to the Linux directory tree:
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/dataCheck the available capacity and mount status:
df -h /mnt/data