The mkfs command in Linux: formatting partitions

Introduction to the mkfs command in Linux

In the Linux environment, preparing a partition for use involves creating a filesystem on it. The mkfs (make file system) command is the standard tool that allows formatting partitions with various filesystem types, such as ext4, XFS, Btrfs, and others. This process is essential both in new installations and when reusing disks.

What is mkfs and how it works

mkfs acts as a frontend that invokes the specific formatters for each filesystem. For example, mkfs.ext4 calls the ext4 formatter, while mkfs.xfs runs the XFS formatter. The basic syntax is:

mkfs -t filesystem_type /dev/sdXN

where -t specifies the filesystem type and /dev/sdXN is the partition to format. If -t is omitted, mkfs attempts to infer the type from the command name used (e.g., mkfs.ext4).

Most common filesystem types

  • ext4: the default in many distributions, offers good performance and robustness.
  • XFS: ideal for large files and high-performance environments, with excellent scalability.
  • Btrfs: includes advanced features such as snapshots, compression, and device pooling.
  • vfat (FAT32): useful for partitions that need to be readable by Windows or external devices.

Useful mkfs options

In addition to the filesystem type, mkfs accepts various options that allow adjusting the formatting:

  • -L label: assigns a label to the filesystem (e.g., -L MY_DISK).
  • -b block_size: defines the block size in bytes (common in ext4: 1024, 2048, 4096).
  • -i bytes_per_inode: sets the ratio between bytes and inodes, useful for systems with many small files.
  • -f: forces formatting even if the partition appears to be mounted or contains data.
  • -V: verbose mode, shows details of the process.

Practical examples

Format partition /dev/sdb1 as ext4 with label DATA and block size 4096 bytes:

mkfs -t ext4 -L DATA -b 4096 /dev/sdb1

Create an XFS filesystem on /dev/nvme0n1p2:

mkfs -t xfs /dev/nvme0n1p2

For a FAT32 partition intended for sharing with Windows:

mkfs -t vfat -L USB /dev/sdc1

Precautions before using mkfs

Formatting a partition erases all data it contains. Therefore it is essential to:

  • Verify the correct device using lsblk or fdisk -l.
  • Unmount the partition if it is mounted: umount /dev/sdXN.
  • Consider backing up important information.

Running mkfs without proper verification can lead to irreversible data loss.

Verifying the result

After formatting, you can inspect the created filesystem:

  • blkid /dev/sdXN shows the UUID, type, and label.
  • df -hT /dev/sdXN indicates the type and available space.
  • Mount the partition and test read/write to ensure its integrity.

Conclusion

The mkfs command is a powerful and versatile tool for preparing partitions in Linux. Knowing its options and the available filesystems allows tailoring the formatting to performance, compatibility, and advanced feature needs. Always remember to verify the device and back up data before proceeding, ensuring a safe and successful operation.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .