How to use the command 'mkfs' (with examples)
- Linux
- December 17, 2024
The mkfs
command, short for “make filesystem,” is a utility tool in Linux operating systems used to build a filesystem on a disk partition. A filesystem organizes how data is stored and retrieved - without a filesystem, data would be stored in a large heap, making it unclear where one piece of data ends and the next begins. While mkfs
is a versatile tool, it’s important to note that it is deprecated in favor of filesystem-specific utilities like mkfs.ext4
, mkfs.ntfs
, etc. These updated commands provide more advanced capabilities and are tailored to specific filesystems. Nevertheless, understanding mkfs
can provide foundational knowledge for handling filesystems.
Use case 1: Build a Linux ext2 filesystem on a partition
Code:
mkfs path/to/partition
Motivation:
When you are setting up a new hard drive or preparing a partition for use, creating a filesystem is a crucial first step. The ext2 filesystem, short for second extended filesystem, is a traditional Linux filesystem often used for simplicity and reliability. Using mkfs
without specifying a filesystem type constructs an ext2 filesystem by default. This is a practical choice for systems that do not require journaling features, such as USB drives or certain lightweight Linux installations.
Explanation:
mkfs
: The command used to create or build a filesystem.path/to/partition
: This is a placeholder indicating the specific path where the filesystem should be created. It is crucial to specify the correct partition path (/dev/sdX
) to avoid undesired data loss on other partitions.
Example output:
Creating filesystem with 102400 1k blocks and 25688 inodes
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
This output indicates that a new ext2 filesystem has been successfully created with details on block size and inode count. It also lists the locations of superblock backups, which are essential for filesystem recovery.
Use case 2: Build a filesystem of a specified type
Code:
mkfs -t ext4 path/to/partition
Motivation:
Different filesystems offer different benefits. The ext4 filesystem, for instance, is an enhanced version of ext3 with support for larger files and improved performance through journaling, which enhances data integrity. Using -t
with mkfs
specifies the type of filesystem to be created, offering flexibility in choosing the most suitable filesystem type for your usage requirements. This is particularly useful when the advantages of a specific filesystem, like ext4’s efficiency, are needed.
Explanation:
mkfs
: This initiates the command to create a filesystem.-t ext4
: The-t
option followed byext4
specifies that the type of filesystem to build is ext4. Ext4 is known for improved performance and reliability compared to its predecessors.path/to/partition
: As before, this indicates the exact location where the filesystem is to be created.
Example output:
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 1048576 4k blocks and 262144 inodes
Filesystem UUID: ec2c7332-4225-4f6d-9cb8-33720811fabd
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
The output reflects a successful creation of an ext4 filesystem, with a count of blocks and inodes specific to this filesystem type. The UUID provided is a unique identifier helpful in identifying and managing partitions.
Use case 3: Build a filesystem of a specified type and check for bad blocks
Code:
mkfs -c -t ntfs path/to/partition
Motivation:
When dealing with hardware that might have physical issues, such as aging or previously damaged disks, it is wise to ensure the data integrity. The -c
option in conjunction with mkfs
performs a check for bad blocks on the partition before creating the filesystem. NTFS is especially useful on systems where compatibility with Windows is necessary, as it is the filesystem primarily used by Windows operating systems. Checking for bad blocks can prevent potential data loss and ensure a more reliable storage medium.
Explanation:
mkfs
: Initializes the command for creating a filesystem.-c
: This instructs the command to check the partition for bad blocks. Detection of bad blocks helps in avoiding unreliable sectors for data storage.-t ntfs
: Here,-t
specifies that the filesystem to be created is NTFS. This type of filesystem is ideal for cross-platform compatibility, especially with Windows.path/to/partition
: The path identifies where the filesystem should be established.
Example output:
Checking for bad blocks (read-only test): done
NTFS: version 3.1
Created a new NTFS 16k-blocksize filesystem with 1310720 blocks and 327680 inodes.
The output confirms that a bad block check was performed, and details the successful creation of an NTFS filesystem, highlighting specifics like block size and the NTFS version used.
Conclusion:
The mkfs
command is a powerful tool for filesystem management, allowing users to create filesystems suited to their specific requirements. Whether choosing a default ext2, a more advanced ext4, or conducting a bad block check with NTFS, understanding how to leverage mkfs
is fundamental to managing and optimizing data storage in Linux environments. Although it is deprecated in favor of more specific utilities, knowing mkfs
offers valuable insights into foundational Linux system tasks.