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 file system on it. The commandmkfs(make file system) is the standard tool that allows formatting partitions with different types of file systems, such as ext4, XFS, Btrfs and others. This process is essential both in new facilities and in the re-use of disks.

What is mkfs and how it works

mkfs acts as a front that invokes the specific formators of each file system. For example,mkfs.ext4call the ext4 formator, whilemkfs.xfsruns the XFS. The basic syntax is:

mkfs -t tipo_dispositivo /dev/sdXN

where-tspecifies the type of file system and/dev/sdXNis the partition to format. If omitted-t, mkfs tries to infer the type from the name of the command used (e.g.,mkfs.ext4).

Types of more common file systems

  • 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 pooling of devices.
  • vfat(FAT32): useful for partitions that must be legible by Windows or external devices.

Useful mkfs options

In addition to the file system type, mkfs accepts various options that allow the formatting to be adjusted:

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

Practical examples

Formatting a partition/dev/sdb1as ext4 with labelDATAand block 4096 bytes:

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

Create an XFS file system in/dev/nvme0n1p2:

mkfs -t xfs /dev/nvme0n1p2

For a FAT32 partition for Windows exchange:

mkfs -t vfat -NOMBRE USB /dev/sdc1

Precautions before use mkfs

Formatting a partition removes all the data it contains. That is why it is essential:

  • Check the correct device withlsblkorfdisk -l.
  • Dismount partition if mounted:umount /dev/sdXN.
  • Consider back up important information.

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

Check the result

After formatting, you can inspect the created file system:

  • blkid /dev/sdXNshows the UUID, type and label.
  • df -hT /dev/sdXNindicates the type and space available.
  • Ride the partition and try writing / reading to ensure its integrity.

Conclusion

The commandmkfsis a powerful and versatile tool to prepare Linux partitions. Knowing your options and available file systems allows you to adapt the formatting to the needs of performance, compatibility and advanced features. Always remember to check the device and back up data before proceeding, thus ensuring a safe and successful operation.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish