mkfs

On this page 7

mkfs (make filesystem) is the Linux command that creates a new, empty filesystem on a block device such as a disk partition, a logical volume or an image file. In Linux filesystem administration, it is the step between partitioning a disk and mounting it: mkfs writes the superblock, allocation structures, inode tables and root directory that turn raw sectors into storage the kernel can mount.

Running mkfs is destructive by design. It overwrites the metadata of any existing filesystem on the target, so the previous files become inaccessible for practical purposes. Choosing the correct device and filesystem type is therefore the most important part of using the command.

How mkfs builds a filesystem

The mkfs command itself is a thin front end from the util-linux package. Given -t type, it calls a filesystem-specific builder named mkfs.type, such as mkfs.ext4, mkfs.xfs or mkfs.btrfs, and passes the remaining options through. Modern documentation recommends calling the specific builder directly, and the generic wrapper is considered deprecated in favor of those tools.

The builder computes the filesystem geometry from the device size and options, then writes the on-disk structures. For ext4 this means block groups, each with a block bitmap, an inode bitmap and an inode table, plus a primary superblock and backup superblock copies. XFS instead creates allocation groups with B+tree indexes, while Btrfs writes a superblock and initial copy-on-write trees.

Most builders also assign a filesystem UUID and an optional label, which blkid and lsblk later report and which fstab can reference. On SSDs and thin-provisioned storage, many builders first issue a discard (TRIM) of the whole device, which can take noticeable time on large drives.

The table below lists the most common builders and what they create.

BuilderFilesystemTypical use
mkfs.ext4ext4General-purpose Linux root and data volumes
mkfs.xfsXFSLarge volumes and parallel workloads
mkfs.btrfsBtrfsSnapshots, checksums and subvolumes
mkfs.vfatFAT32EFI system partitions and removable media
mkfs.f2fsF2FSFlash storage such as SD cards
mkswapSwap areaSwap partitions, not a filesystem
Common filesystem builders on Linux

Key mkfs options and parameters

Most mkfs parameters are fixed at creation time and cannot be changed later without reformatting, which is why they deserve attention before the command runs. Options differ between builders, but the same concepts recur: block size, inode allocation, labels, reserved space and journal settings.

ParameterExample optionWhat it affects
Block sizemkfs.ext4 -b 4096Allocation unit, usually 4 KiB matching page size
Inode ratiomkfs.ext4 -i 16384Maximum number of files on ext4
Reserved blocksmkfs.ext4 -m 1Space kept for root, 5 percent by default
Label-L data or -n DATA for vfatHuman-readable name for mounting
Stripe alignmentmkfs.xfs -d su=64k,sw=4Performance on RAID arrays
Data profilemkfs.btrfs -d raid1 -m raid1Redundancy across multiple devices
Force overwrite-f (XFS, Btrfs) or -F (ext4)Proceeds despite an existing signature
Important creation parameters and their effects

The inode count matters most on ext4, because ext4 allocates inodes statically. A volume holding millions of small files, such as a mail spool or a cache directory, can report no space left on device while gigabytes remain free if the inode ratio was too high. XFS and Btrfs allocate inodes dynamically and avoid this specific failure.

A useful safety option is the dry run. mkfs.ext4 -n shows what would be created, including the backup superblock locations, without writing anything, and those locations help later recovery with fsck if the primary superblock is damaged.

mkfs is one step in the storage provisioning chain, and confusing it with neighboring tools is a frequent source of mistakes. Partitioning tools define where a filesystem lives, mkfs creates it, and mount attaches it to the directory tree.

ToolPurposeRelation to mkfs
fdisk, partedCreate partition tables and partitionsRuns before mkfs
LVM toolsCreate logical volumes from physical disksProvides devices for mkfs
mkfsWrites a new empty filesystemThe formatting step itself
mountAttaches a filesystem at a mount pointRuns after mkfs
fsckChecks and repairs an existing filesystemNever creates one
tune2fs, xfs_adminChange labels and some settings laterAdjusts what mkfs created
mkfs compared with neighboring storage tools

Some properties can be adjusted after creation: tune2fs changes the ext4 label, reserved percentage and certain features, and resize2fs or xfs_growfs enlarge a filesystem. Block size and, on ext4, the total inode count cannot be changed this way.

Using mkfs safely in practice

Safe use of mkfs starts with identifying the target device unambiguously. Device names such as /dev/sdb can change between boots, so administrators confirm size, model and existing contents with lsblk -f and blkid immediately before formatting, and prefer stable paths under /dev/disk/by-id/.

  1. Confirm the device with lsblk -f, checking size and current filesystem.
  2. Make sure nothing is mounted from that device, including swap.
  3. Run the specific builder, for example mkfs.ext4 -L backup /dev/sdb1.
  4. Record the new UUID from blkid and add it to fstab.
  5. Mount and test with mount -a before rebooting.

Current builders refuse to overwrite a device that is mounted or already carries a known signature unless a force option is given. This protection is helpful, but it is not a backup strategy: an administrator who adds -f out of habit removes the last safeguard.

Common mkfs mistakes and their consequences

Most mkfs problems are human errors in target selection or parameters, not software faults. The table below pairs typical symptoms with causes and remedies.

SymptomLikely causeFix
Data on another disk vanishedWrong device name formattedStop writes and attempt recovery from backup
No space left with free blocksToo few inodes on ext4Reformat with a lower inode ratio
Partition table destroyedWhole disk formatted instead of partitionRestore the table, then check the data
Device or resource busyTarget still mounted or in useUnmount it or disable swap first
Slow writes on RAIDStripe geometry not setRecreate with correct alignment options
Typical mkfs errors with causes and fixes

Formatting rewrites metadata rather than every data block, so file contents may partly survive and forensic tools can sometimes recover them. That is a reason to treat mkfs as unsuitable for secure erasure; tools such as blkdiscard or device-level secure erase are designed for that purpose.

The short version

A new disk is like an empty warehouse with no shelves, labels or inventory list. mkfs builds the shelving and the catalog so that Linux knows where each file can be stored and found later.

The catch is that building new shelves throws away the old catalog. If mkfs is pointed at a disk that already holds files, those files become unreachable, so it pays to double-check which disk is selected.

Where it makes a difference

A systems administrator adds a 4 TB disk to a file server that will store several million small thumbnail images. Using lsblk -f, the administrator confirms the new disk is /dev/sdc, empty and unmounted, then creates one partition with parted.

Knowing that the default ext4 inode ratio could run out with so many small files, the administrator runs mkfs.ext4 -L thumbs -i 8192 -m 1 /dev/sdc1, which doubles the inode count compared with the default and reduces reserved space to 1 percent. The UUID goes into fstab and the volume is mounted at /srv/thumbs.

Months later the volume holds nine million files at 60 percent capacity, with inodes still well below their limit, avoiding the out-of-space errors seen on an older server formatted with defaults.