A partition is just space. Before it can hold files it needs a filesystem, and mkfs (make filesystem) creates one. This guide covers choosing a filesystem, the options that matter on servers, and doing it safely.

Warning: formatting erases everything on the target. Confirm the device with lsblk -f first.

Choose a filesystem

Filesystem Strengths Limits Typical use
ext4 Mature, robust, can shrink, default on Debian/Ubuntu Less suited to huge (100 TB+) volumes General purpose, root disks, most VPS data disks
XFS Excellent with large files and parallel I/O, default on RHEL Can’t shrink Databases, large storage, RHEL-family servers
Btrfs Snapshots, checksums, compression, subvolumes More complex, RAID5/6 not recommended Backup targets, systems that use snapshots
ZFS Like Btrfs plus a mature RAID and volume manager Out-of-tree module, uses RAM for cache Storage servers, data integrity

If unsure, use ext4. See Btrfs and ZFS for those options.

Create the filesystem

lsblk -f                         # confirm: vdb1 has no FSTYPE yet
sudo mkfs.ext4 -L data /dev/vdb1

Others:

sudo mkfs.xfs -L data /dev/vdb1
sudo mkfs.btrfs -L data /dev/vdb1
sudo mkfs.vfat -F 32 -n USB /dev/sdc1     # FAT32, for exchange with other OSes
sudo mkswap /dev/vdb2                     # swap partition

-L sets a label, which you can then use in fstab (LABEL=data), though UUIDs are more robust.

If the device already has a filesystem, mkfs may refuse or ask for confirmation. -F (ext4) or -f (XFS, Btrfs) forces it. Only use them when you’re sure.

Useful ext4 options

Reserved blocks. ext4 reserves 5% for root by default, so the system keeps working when users fill the disk. On a large data disk that’s a lot of space, for example 25 GB on 500 GB:

sudo mkfs.ext4 -m 1 -L data /dev/vdb1        # reserve 1% at creation
sudo tune2fs -m 1 /dev/vdb1                  # change later, no reformat needed

Keep the default on the root filesystem.

Inodes for many small files. Every file uses one inode, and their number is fixed at creation. Mail spools, caches and some backup tools create millions of tiny files and can run out of inodes while space remains (df -i shows 100%). Create more:

sudo mkfs.ext4 -i 4096 /dev/vdb1          # one inode per 4 KiB (default is 16 KiB)
sudo mkfs.ext4 -T small /dev/vdb1         # preset for small files

See inode usage full. XFS and Btrfs allocate inodes dynamically, so they don’t have this problem.

Large files only (backups, video): -T largefile creates fewer inodes and leaves more usable space.

Useful XFS options

Defaults are good. On RAID arrays, mkfs.xfs usually detects the stripe geometry itself. Reflinks (-m reflink=1, the default in recent versions) enable instant file copies with cp --reflink.

After formatting

sudo mkdir -p /mnt/data
sudo mount /dev/vdb1 /mnt/data
sudo chown deploy:deploy /mnt/data     # a fresh filesystem is owned by root
df -hT /mnt/data

Make it permanent in /etc/fstab using the UUID:

sudo blkid -s UUID -o value /dev/vdb1

See fstab. The new filesystem contains lost+found (on ext4). That’s normal, and fsck uses it.

Check and repair: fsck

Filesystems should be checked unmounted:

sudo umount /mnt/data
sudo fsck.ext4 -f /dev/vdb1        # ext4
sudo xfs_repair /dev/vdb1          # XFS (use -n for a read-only check)
sudo btrfs check /dev/vdb1         # Btrfs (read-only by default)

Never run fsck on a mounted filesystem. For the root filesystem, schedule a check at boot (sudo touch /forcefsck on older systems, or fsck.mode=force on the kernel command line), or run it from the console in recovery mode.

Change things later

sudo e2label /dev/vdb1 backups          # ext4 label
sudo xfs_admin -L backups /dev/vdb1     # XFS label (unmounted)
sudo tune2fs -l /dev/vdb1               # show ext4 settings (inode count, reserved blocks)
sudo tune2fs -U random /dev/vdb1        # new UUID (e.g. after cloning a disk)

Wipe old signatures

When reusing a disk that was part of RAID, LVM or ZFS, old metadata can confuse tools. Clear it:

sudo wipefs -a /dev/vdb         # remove all filesystem/RAID/partition signatures

Quick reference

Task Command
ext4 with label sudo mkfs.ext4 -L data /dev/vdb1
XFS sudo mkfs.xfs -L data /dev/vdb1
Less reserved space sudo tune2fs -m 1 /dev/vdb1
More inodes sudo mkfs.ext4 -i 4096 /dev/vdb1
Swap sudo mkswap /dev/vdb2
Check ext4 sudo fsck.ext4 -f /dev/vdb1
Wipe signatures sudo wipefs -a /dev/vdb

Related: fdisk, parted and mount.

  • Linux
  • Storage
  • Command Line