Linux mke2fs Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux mke2fs command to create Ext4 filesystems on partitions. The lab covers understanding the purpose and syntax of the mke2fs command, creating an Ext4 filesystem on a partition, and customizing filesystem parameters. By the end of this lab, you will be able to create and manage Ext4 filesystems on your Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") subgraph Lab Skills linux/sudo -.-> lab-422806{{"`Linux mke2fs Command with Practical Examples`"}} linux/df -.-> lab-422806{{"`Linux mke2fs Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the mke2fs Command

In this step, we will learn about the purpose and syntax of the mke2fs command in Linux. The mke2fs command is used to create a new ext2, ext3, or ext4 filesystem on a specified partition or block device.

To understand the purpose and syntax of the mke2fs command, let's explore the following:

sudo mke2fs --help

Example output:

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

The key options in the mke2fs command are:

  • -t: Specifies the filesystem type (e.g., ext4)
  • -b: Sets the block size (default is 4096 bytes)
  • -i: Sets the bytes-per-inode ratio
  • -L: Sets the volume label
  • -O: Enables specific filesystem features
  • -m: Sets the percentage of reserved blocks
  • -U: Sets the UUID (Universally Unique Identifier) for the filesystem

By understanding the purpose and syntax of the mke2fs command, you can create customized ext2, ext3, or ext4 filesystems on your Linux system.

Create an Ext4 Filesystem on a Partition

In this step, we will create an Ext4 filesystem on a partition using the mke2fs command.

First, let's create a new partition on the virtual disk in our Docker container. We'll use the fdisk command for this:

sudo fdisk /dev/sdb

Follow the prompts to create a new partition. For example, you can create a primary partition occupying the entire disk.

Once the partition is created, we can use the mke2fs command to create an Ext4 filesystem on it:

sudo mke2fs -t ext4 /dev/sdb1

This will create an Ext4 filesystem on the /dev/sdb1 partition.

Example output:

mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 5242880 4k blocks and 1310720 inodes
Filesystem UUID: 7d9c1a3e-b7a4-4a4e-8a1a-2d9f2d1d9d1d
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

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

The key options used in this command are:

  • -t ext4: Specifies the Ext4 filesystem type
  • /dev/sdb1: The partition where the filesystem will be created

Now the Ext4 filesystem has been created on the partition, and you can use it for your storage needs.

Customize Filesystem Parameters with mke2fs

In this step, we will learn how to customize the filesystem parameters when creating an Ext4 filesystem using the mke2fs command.

First, let's create a new partition on the virtual disk in our Docker container:

sudo fdisk /dev/sdb

Follow the prompts to create a new primary partition.

Now, let's create an Ext4 filesystem on the new partition with some customized parameters:

sudo mke2fs -t ext4 -b 4096 -i 8192 -L "my_filesystem" /dev/sdb1

The key options used in this command are:

  • -t ext4: Specifies the Ext4 filesystem type
  • -b 4096: Sets the block size to 4096 bytes
  • -i 8192: Sets the bytes-per-inode ratio to 8192 bytes
  • -L "my_filesystem": Sets the volume label to "my_filesystem"
  • /dev/sdb1: The partition where the filesystem will be created

Example output:

mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 5242880 4k blocks and 1310720 inodes
Filesystem UUID: 7d9c1a3e-b7a4-4a4e-8a1a-2d9f2d1d9d1d
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

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

By customizing the filesystem parameters, you can optimize the Ext4 filesystem for your specific use case and storage requirements.

Summary

In this lab, we learned about the purpose and syntax of the mke2fs command in Linux, which is used to create new ext2, ext3, or ext4 filesystems on a specified partition or block device. We explored the key options of the mke2fs command, such as setting the filesystem type, block size, bytes-per-inode ratio, volume label, and various filesystem features. We then demonstrated how to create an Ext4 filesystem on a new partition using the mke2fs command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like