LinuxCommandLibrary

mke2fs

Create ext2/ext3/ext4 filesystems

TLDR

Create an ext2 filesystem in partition 1 of device b (sdb1)

$ mke2fs -t ext2 [/dev/sdb1]
copy

Create an ext3 filesystem in partition 1 of device b (sdb1)
$ mke2fs -t ext3 [/dev/sdb1]
copy

Create an ext4 filesystem in partition 1 of device b (sdb1)
$ mke2fs -t ext4 [/dev/sdb1]
copy

SYNOPSIS

mke2fs [-t filesystem-type] [-b block-size] [-i bytes-per-inode] [-L volume-label] [-U UUID] [-j] [-O feature[,...]] [-c] [-F] device [blocks-count]

PARAMETERS

-t
    Specifies the filesystem type to be created. Common types are ext2, ext3, and ext4. If not specified, ext4 is often the default on modern systems.

-b
    Specifies the size of blocks in bytes. Valid sizes are 1024, 2048, and 4096 bytes. Larger block sizes can reduce fragmentation but may waste space for very small files.

-i
    Specifies the ratio of bytes of space per inode. This value determines the number of inodes created. A smaller ratio (e.g., 1024) means more inodes, suitable for systems with many small files.

-L
    Sets the volume label for the filesystem. This label can be used to identify the filesystem when mounting or in /etc/fstab.

-U
    Sets the universally unique identifier (UUID) for the filesystem. If not specified, a random UUID is generated.

-j
    Creates an ext3 filesystem with a journal. This option is effectively a shorthand for -t ext3.

-O
    Enables or disables specific filesystem features. Features can be added (e.g., -O dir_index) or removed (e.g., -O ^has_journal). Many features are enabled by default for ext4.

-c
    Checks the device for bad blocks before creating the filesystem. This can be time-consuming, especially for large devices.

-F
    Forces mke2fs to proceed even if the specified device is not a block special device or if it is mounted. Use with extreme caution as it can lead to data loss.

-N
    Overrides the default number of inodes to be created. This can be useful for specific use cases where the default calculation is not optimal.

-m
    Specifies the percentage of blocks reserved for the superuser (root). Default is 5%, often reduced to 0-1% for data partitions.

-v
    Enables verbose output, showing more details about the filesystem creation process.

-q
    Enables quiet mode, suppressing most output during filesystem creation.

device
    The block device or file that will contain the filesystem (e.g., /dev/sdb1 or /path/to/image.img).

blocks-count
    The number of blocks on the device. If omitted, mke2fs automatically determines the size of the device.

DESCRIPTION

mke2fs is a powerful utility used to create ext2, ext3, and ext4 filesystems on a specified block device, such as a disk partition or a file-backed loop device. It is a fundamental tool for preparing storage for use in Linux systems.

When executed, mke2fs initializes the filesystem's structure, including the superblock, inode tables, data blocks, and block groups. For ext3 and ext4 filesystems, it also sets up a journal, which helps ensure data consistency and faster recovery after system crashes. The command overwrites any existing data on the target device, making it a destructive operation.

Users can customize various filesystem parameters like block size, bytes-per-inode ratio, volume label, and specific ext features, allowing for optimization based on the intended use case, such as large file storage or a high number of small files. mke2fs is an essential component of the e2fsprogs suite, which provides tools for managing ext-family filesystems.

CAVEATS

mke2fs is a destructive command. It will erase all existing data on the specified device. Always double-check the device argument to ensure you are formatting the correct partition or disk.

While mke2fs creates different ext filesystem versions (ext2, ext3, ext4), it does not convert an existing filesystem in place. For converting ext2 to ext3 (adding a journal) or ext3 to ext4 (enabling new features), use tune2fs.

Incorrect choices for parameters like block size or bytes-per-inode ratio can lead to inefficient space utilization or premature inode exhaustion for specific workloads.

DEFAULT BEHAVIOR AND SMART DEFAULTS

Without specific options, mke2fs intelligently determines optimal parameters based on the size of the device and the kernel's capabilities. For instance, on modern systems, it often defaults to creating an ext4 filesystem with appropriate block and inode sizes, aiming for a balance between performance and storage efficiency.

THE E2FSPROGS SUITE

mke2fs is part of the comprehensive e2fsprogs user-space utilities, which are essential for managing ext2, ext3, and ext4 filesystems. This suite also includes tools for checking (e2fsck), tuning (tune2fs), dumping information (dumpe2fs), and recovering (e2restore) these filesystems.

HISTORY

mke2fs is a core utility of the e2fsprogs package, originally developed by Theodore Ts'o for the Ext2 filesystem. As Linux evolved, so did its primary filesystem. mke2fs was updated to support Ext3 with the introduction of journaling, providing improved data integrity and faster crash recovery.

Later, support for Ext4 was added, incorporating features like extents (for better handling of large files), delayed allocation, and larger filesystem sizes. This continuous development reflects its central role in Linux disk management, adapting to modern storage requirements and performance optimizations.

SEE ALSO

mount(8), tune2fs(8), e2fsck(8), dumpe2fs(8), mkfs(8), fdisk(8), parted(8)

Copied to clipboard