How to Use the Command mkfs.ext4 (with Examples)

How to Use the Command mkfs.ext4 (with Examples)

The mkfs.ext4 command is used in Linux environments to create an ext4 file system. Ext4, the fourth extended filesystem, is one of the most widely used file systems for Linux. When executed, this command effectively formats a specified partition or volume, making it ready to store files and directories. Creating a file system involves setting up a space on which data can be efficiently organized and retrieved, and this command accomplishes that with ease for ext4 file systems. Below, we illustrate specific use cases of this command to further demonstrate its functionality and versatility.

Use case 1: Create an ext4 Filesystem inside Partition 1 on Device b (sdb1)

Code:

sudo mkfs.ext4 /dev/sdb1

Motivation:

This use case is fundamental for anyone setting up a new storage device, such as an external hard disk or SSD. When you first install a new storage device in your Linux system, it often comes unformatted or with a default file system not optimized for your specific needs. By creating an ext4 filesystem on /dev/sdb1, you format the partition (in this case, the first partition of the second hard drive) to the ext4 format, making it suitable for Linux file storage. This is particularly useful when performance, reliability, and advanced features such as journaling need to be utilized.

Explanation:

  • sudo: This command is used to execute mkfs.ext4 with superuser privileges, necessary for modifying storage partitions.
  • mkfs.ext4: The primary command used to create an ext4 file system on the specified partition or volume.
  • /dev/sdb1: Refers to the first partition on device b. This is typically how disks and their partitions are labeled in Linux systems (sda, sdb, sdc, and so on).

Example Output:

Upon successful execution, the command provides feedback on its process, often detailing the block size, filesystem size, reserved block count, and more, confirming the formatting procedure.

mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 9767649 4k blocks and 2441888 inodes
Filesystem UUID: e99f7c1d-b56f-4b63-a87b-8eac607ce35d
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 
	2654208, 4096000, 7962624

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done

Use case 2: Create an ext4 Filesystem with a Volume-Label

Code:

sudo mkfs.ext4 -L volume_label /dev/sdb1

Motivation:

In scenarios where system administrators are managing multiple storage devices, keeping track of them can become challenging. Assigning a volume label— a human-readable identifier— to the filesystem significantly simplifies management tasks. It allows for easy identification and mounting, especially when dealing with multiple disks or sharing the storage device over a network. For example, labeling partitions as backup, media, or data provides clarity and assists in automated scripts for mounting volumes.

Explanation:

  • sudo: Elevates the command execution to superuser level, permitting changes to system-level storage.
  • mkfs.ext4: Indicates the mkfs command variant used to create ext4 filesystems.
  • -L volume_label: The -L option is used to specify a label for the filesystem. This label can be a name of your choice, serving as an easy reference to identify the particular filesystem.
  • /dev/sdb1: Really points to the partition being formatted, as in the previous use case.

Example Output:

Once labeled, the command outputs messages similar to those of the prior example, with additional confirmation of the label establishment on the filesystem.

mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 9767649 4k blocks and 2441888 inodes
Filesystem UUID: e99f7c1d-b56f-4b63-a87b-8eac607ce35d
Filesystem volume name: volume_label
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 
	2654208, 4096000, 7962624

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done

Conclusion:

The mkfs.ext4 command is a vital utility in Linux for file system creation, offering flexibility for configuring reliable and efficient storage solutions. Through its basic usage and options such as labeling, it caters to both general and specific needs of system administrators, allowing them to format and organize storage volumes with precision and ease. Whether you are setting up a new partition or seeking to streamline device management, mkfs.ext4 provides the necessary tools to achieve your objectives efficiently.

Related Posts

How to Use the Command 'mongorestore' (with Examples)

How to Use the Command 'mongorestore' (with Examples)

The mongorestore utility is a powerful tool provided by MongoDB for importing a collection or database from a binary dump back into a MongoDB instance.

Read More
Understanding the 'docker stats' Command (with examples)

Understanding the 'docker stats' Command (with examples)

The docker stats command is a powerful tool for monitoring Docker containers.

Read More
How to use the command 'timeshift' (with examples)

How to use the command 'timeshift' (with examples)

Timeshift is a powerful system restore utility that allows users to create and manage snapshots of their system.

Read More