Basics of mke2fs command in Linux with examples

In this article, we’ll learn about the usage of the mke2fs command in Linux.

mke2fs command is part of the e2fsprogs package, which provides the filesystem utilities for use with the ext2/3/4 file systems, and the mkfs command, which is the part of the util-linux package calls mke2fs when it is requested to create an ext2/3/4 filesystems. This utility allows you to create a filesystem from the terminal.

NOTE: Ensure to back up your data before proceeding further, as it can wipe all the data on your device.

Syntax of the mke2fs command

The syntax for the mke2fs command is given below:

mke2fs [ -c | -l filename ] [ -b block-size ] [ -f fragment-size ] [ -g blocks-per-group ] [ -G number-of-groups ] [ -i bytes-per-inode ] [ -I inode-size ] [ -j ] [ -J journal-options ] [ -K ] [ -N number-of-inodes ] [ -n ] [ -m reserved-blocks-percentage ] [ -o creator-os ] [ -O feature[,...] ] [ -q ] [ -r fs-revision-level ] [ -E extended-options ] [ -v ] [ -F ] [ -L volume-label ] [ -M last-mounted-directory ] [ -S ] [ -t fs-type ] [ -T usage-type ] [ -U UUID ] [ -V ] device [ blocks-count ]
mke2fs -O journal_dev [ -b block-size ] [ -L volume-label ] [ -n ] [ -q ] [ -v ] external-journal [ fs-size ]

To find out the version of the mke2fs installed on your system, execute:

mke2fs -V
Mke2fs Version
mke2fs version

How to create filesystem with checking for bad blocks on a device?

The parts of the device where storing data are no more reliable are known as the bad blocks. You can use the mke2fs command to identify such bad blocks.

The -c option is used to check for the bad blocks before creating a filesystem on a device.

Syntax to find bad blocks and create an ext2 filesystem using the mke2fs command:

sudo mke2fs -c /dev/sdbX
Checking For Bad Blocks Using Mke2fs
Checking for bad blocks using mke2fs

If you specify -c twice, then a slower read-write test is used instead of a fast read-only when the -c is specified only once.

sudo mke2fs -c -c /dev/sdbX
Checking For Bad Blocks With Read Write
Checking for bad blocks with read-write

How to create an ext2/3/4 filesystem with the mke2fs command?

To create an ext2/3/4 filesystem on a device, we use the -t option to specify the filesystem type we want to create.

Syntax to create a filesystem and quickly check for bad blocks on a device with mke2fs command:

sudo mke2fs -t fs_type -c /dev/sdbX

For ext4 filesystem replace fs_type with ext4 and specify the partition.

Creating Ext4 Partition
Creating ext4 partition on a device

How to create a filesystem with a volume label?

The -L option is used to set the volume label of a partition to a new volume label.

Syntax to create an ext4 filesystem with BACKUP volume label using mke2fs command:

sudo mke2fs -t ext4 -L BACKUP /dev/sdb1
Creating A File System With Volume Label
Creating a file system with a volume label

You can find out the label of a volume using the e2label command:

sudo e2label /dev/sdbX
Find Out Label Of A Volume
Find out the label of a volume

How to create a filesystem with a specific number of bytes per inode?

The -i option is used to specify the number of bytes per inode when creating a filesystem on a device. This specifies the limit of the files that can be kept in the storage.

Syntax to create an ext4 filesystem with 8192 bytes per inode, with a volume label BACKUP using mke2fs command:

sudo mke2fs -t ext4 -L BACKUP -i 8192 /dev/sdbX
Creating Filesystem With Specific Number Of Inodes Per Group
Creating filesystem with a specific number of inodes per group

Now, to find out the number of bytes per inode, execute the following df command:

df -i /dev/sdbX
Inodes And Usage
Inodes and Usage

Alternatively, you can also use the tune2fs command:

sudo tune2fs -l /dev/sdbX | grep Inode

How to create a filesystem with a specific inode size?

The -I option is used with the mke2fs command to specify the number of the inode size in terms of bytes. Inode size tells the size of each inode created.

Syntax to create an ext4 filesystem with specific node size using mke2fs command:

sudo mke2fs -t ext4 -I 128 /dev/sdbX
Creating Filesystem With Specific Inode Size
Creating filesystem with specific inode size

You can find the inode size using the tune2fs command:

sudo tune2fs -l /dev/sdbX | grep Inode

Conclusion

mke2fs is a powerful command to create ext2/3/4 filesystem on devices using your terminal. It is available on all Linux distributions. You can read its Linux man page here for an in-depth overview.

Thank you for reading! 😀