mkfs
Create a filesystem on a device
TLDR
Build a Linux ext2 filesystem on a partition
Build a filesystem of a specified type
Build a filesystem of a specified type and check for bad blocks
SYNOPSIS
mkfs [options] [-t type] device [fs-options]
PARAMETERS
device
The special file (e.g., /dev/sda1) representing the block device or partition on which to create the file system.
-t type
Specifies the type of file system to be built (e.g., ext4, xfs, vfat). If omitted, mkfs may default to ext2 or require explicit specification depending on the system configuration.
fs-options
Additional, file system-specific options that are passed directly to the underlying mkfs.type utility. For example, -b for block size with ext4 or -L for a label.
-c, --check
Check the device for bad blocks before creating the file system. This option scans the entire device and can take a considerable amount of time.
-l filename
Read the list of known bad blocks from the specified filename and mark them as unusable by the file system.
-F, --force
Force the creation of the file system, even if a file system is already detected on the device or if the device is not a block special device. Use with extreme caution as it bypasses safety checks.
-V, --verbose
Produce verbose output, showing more details about the file system creation process, including commands executed and parameters used.
-q, --quiet
Suppress output, operating in a quieter mode. Only critical errors will be displayed.
size
The number of blocks to be used for the file system. This option is less commonly used with the mkfs wrapper, as the underlying mkfs.type utilities usually determine the size from the device.
DESCRIPTION
The mkfs (make file system) command is a crucial utility used to build a new file system on a specified block device, typically a disk partition or a logical volume.
It functions primarily as a front-end wrapper. When invoked, mkfs dispatches the request to a file system-specific utility, such as mkfs.ext4, mkfs.xfs, or mkfs.btrfs, based on the file system type provided (e.g., using the -t option). These underlying utilities then initialize the device by writing the necessary metadata structures: creating a superblock, inode tables, block groups, and setting up the root directory.
This process prepares the device for storing files and directories, making it ready to be mounted and used by the operating system. It is imperative to exercise extreme caution when using mkfs, as it permanently destroys all existing data on the target device.
CAVEATS
Using mkfs is a destructive operation that will erase all existing data on the specified device. Always double-check the target device (/dev/sdXn) before execution to prevent irreversible data loss on incorrect partitions.
The device must not be mounted when mkfs is run. Attempting to format a mounted file system can lead to system instability, unrecoverable data corruption, or kernel panics. Unmount the device first using the umount command.
mkfs typically requires root privileges (or sudo) to execute, as it modifies low-level device structures.
USAGE EXAMPLE
To create an ext4 file system on the first partition of the second disk, /dev/sdb1:
sudo mkfs -t ext4 /dev/sdb1
To create an XFS file system with a label "MY_DATA" and force overwrite on /dev/sdc2 (often invoked directly through mkfs.xfs for specific options):
sudo mkfs.xfs -L MY_DATA -f /dev/sdc2
IDENTIFYING THE CORRECT DEVICE
Before running mkfs, it's critical to correctly identify the target block device to prevent accidental data loss. Commands like lsblk, fdisk -l, parted -l, or inspecting the /dev/ directory can help list and identify available disks and partitions. Always ensure the device is unmounted before formatting.
HISTORY
mkfs has been a fundamental command in Unix and Linux systems for decades. Initially, each file system type had its own dedicated utility (e.g., mkfs.minix, mkfs.ext). Over time, a generic mkfs wrapper was developed to provide a unified interface, dispatching to the appropriate file system-specific tool based on the -t option or by symlinking mkfs.type to mkfs. This modular design allowed for easier integration of new file system types without changing the core mkfs command. Its evolution mirrors the development of various Linux file systems, from the early ext family to modern ones like xfs and btrfs.