Linux mkfs.ext2 Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the mkfs.ext2 command to create an ext2 file system on a partition. The ext2 file system is one of the oldest and most widely used file systems in the Linux operating system. You will learn how to create an ext2 file system with custom parameters, such as block size and number of inodes. The lab also covers the basics of the mkfs.ext2 command, including its syntax and common options. This lab is designed to help you develop your skills in disk and file system utilities, which are essential for system administration tasks in a Linux environment.

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/dd("`File Converting/Copying`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/sudo -.-> lab-422808{{"`Linux mkfs.ext2 Command with Practical Examples`"}} linux/dd -.-> lab-422808{{"`Linux mkfs.ext2 Command with Practical Examples`"}} linux/mount -.-> lab-422808{{"`Linux mkfs.ext2 Command with Practical Examples`"}} end

Introduction to the mkfs.ext2 Command

In this step, you will learn about the mkfs.ext2 command, which is used to create an ext2 file system on a partition. The ext2 file system is one of the oldest and most widely used file systems in the Linux operating system.

The mkfs.ext2 command is used to format a partition or block device with the ext2 file system. The ext2 file system is a journaling file system, which means that it keeps track of changes to the file system and can recover from system crashes or power failures more easily than non-journaling file systems.

To create an ext2 file system, you can use the following command:

sudo mkfs.ext2 /dev/sdb1

This command will create an ext2 file system on the /dev/sdb1 partition.

Example output:

mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 2621440 1k-blocks and 655360 inodes
Filesystem UUID: 5e7c1c2f-0c7a-4f3e-b9d6-a9d4d5e8b7a0
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

The mkfs.ext2 command has several options that you can use to customize the file system. For example, you can specify the block size, the number of inodes, and the file system label. You can use the man mkfs.ext2 command to learn more about the available options.

Creating an ext2 File System on a Partition

In this step, you will learn how to create an ext2 file system on a partition using the mkfs.ext2 command.

First, let's create a new partition on the virtual disk. You can use the fdisk command to create a new partition:

sudo fdisk /dev/sdb

Follow the prompts to create a new partition. Once the partition is created, you can use the mkfs.ext2 command to format it with the ext2 file system:

sudo mkfs.ext2 /dev/sdb1

This will create an ext2 file system on the /dev/sdb1 partition. You can customize the file system parameters by using additional options with the mkfs.ext2 command. For example, you can set the block size, the number of inodes, and the file system label.

Example output:

mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 2621440 1k-blocks and 655360 inodes
Filesystem UUID: 5e7c1c2f-0c7a-4f3e-b9d6-a9d4d5e8b7a0
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

Now, you can mount the ext2 file system to a directory and start using it.

Formatting an ext2 File System with Custom Parameters

In this step, you will learn how to format an ext2 file system with custom parameters using the mkfs.ext2 command.

The mkfs.ext2 command provides several options to customize the file system. Here are some of the common options:

  • -b or --block-size: Specifies the block size of the file system. The default is 1024 bytes.
  • -i or --inode-ratio: Specifies the ratio of inodes to blocks. The default is 16384.
  • -L or --label: Specifies the file system label.
  • -m or --reserved-blocks-percentage: Specifies the percentage of the file system blocks that are reserved for use by the root user. The default is 5%.

Let's create an ext2 file system with a block size of 4096 bytes, an inode ratio of 8192, and a file system label of "my_ext2_fs":

sudo mkfs.ext2 -b 4096 -i 8192 -L my_ext2_fs /dev/sdb1

Example output:

mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 2621440 4k blocks and 655360 inodes
Filesystem UUID: 5e7c1c2f-0c7a-4f3e-b9d6-a9d4d5e8b7a0
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

You can verify the file system parameters using the tune2fs command:

sudo tune2fs -l /dev/sdb1

This will display the file system parameters, including the block size, inode ratio, and file system label.

Summary

In this lab, you learned about the mkfs.ext2 command, which is used to create an ext2 file system on a partition. The ext2 file system is one of the oldest and most widely used file systems in the Linux operating system. You also learned how to create an ext2 file system on a partition using the mkfs.ext2 command, and how to customize the file system with various options.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like