Linux e2fsck Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the e2fsck command in Linux to check and repair corrupted Ext4 file systems. The e2fsck command is a powerful tool that can help you identify and fix file system errors, recover lost files, and optimize file system performance. You will start by understanding the purpose of the e2fsck command, then you will learn how to use it to check and repair a corrupted Ext4 file system. Finally, you will explore the dry run option and learn how to automatically fix errors in the file system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/dd -.-> lab-422655{{"`Linux e2fsck Command with Practical Examples`"}} end

Understand the Purpose of the e2fsck Command

In this step, you will learn about the purpose and usage of the e2fsck command in Linux. The e2fsck command is used to check and repair the Ext2, Ext3, and Ext4 file systems, which are commonly used file systems in Linux-based operating systems.

The e2fsck command performs the following tasks:

  1. Checks the file system for errors and inconsistencies.
  2. Repairs any errors or inconsistencies found in the file system.
  3. Optimizes the file system for better performance.

To understand the purpose of the e2fsck command, let's explore some common scenarios where it is used:

  1. Checking for file system corruption: If a file system becomes corrupted due to power failures, hardware failures, or improper system shutdowns, the e2fsck command can be used to check and repair the file system.

  2. Recovering lost files: The e2fsck command can help recover lost files by identifying and repairing file system metadata, such as inodes, directory structures, and block allocations.

  3. Optimizing file system performance: The e2fsck command can be used to optimize the file system by reorganizing data blocks and improving the file system's overall efficiency.

Now, let's try using the e2fsck command in the next step.

Check and Repair a Corrupted Ext4 File System

In this step, you will learn how to use the e2fsck command to check and repair a corrupted Ext4 file system.

First, let's create a file system image that we can use for this exercise:

sudo dd if=/dev/zero of=ext4_image.img bs=1M count=100
sudo mkfs.ext4 ext4_image.img

The above commands create a 100 MB file system image named ext4_image.img and format it as an Ext4 file system.

Now, let's intentionally corrupt the file system by modifying the superblock:

sudo debugfs -w ext4_image.img
debugfs: set_super_value s_magic 0x1234
debugfs: write_super_block
debugfs: quit

The above commands use the debugfs tool to modify the s_magic field in the superblock, which is a unique identifier for the Ext4 file system. This action will corrupt the file system.

To check and repair the corrupted file system, use the e2fsck command:

sudo e2fsck -f ext4_image.img

The -f option forces the file system check, even if the file system appears to be clean.

Example output:

ext4_image.img: recovering journal
ext4_image.img: clean, 11/25600 files, 25/100000 blocks

The output shows that the e2fsck command was able to recover the journal and repair the corrupted file system.

Perform a Dry Run and Fix Errors Automatically

In this step, you will learn how to perform a dry run of the e2fsck command and how to automatically fix any errors found in the file system.

First, let's create another corrupted file system image:

sudo dd if=/dev/zero of=ext4_image2.img bs=1M count=100
sudo mkfs.ext4 ext4_image2.img
sudo debugfs -w ext4_image2.img
debugfs: set_super_value s_magic 0x1234
debugfs: write_super_block
debugfs: quit

Now, let's perform a dry run of the e2fsck command to see what errors are found, without actually fixing them:

sudo e2fsck -n ext4_image2.img

The -n option tells e2fsck to perform a dry run, which means it will check the file system for errors but won't make any changes.

Example output:

ext4_image2.img: Superblock invalid, trying backup blocks...
ext4_image2.img: Bad magic number in super-block
ext4_image2.img: Group descriptors look bad... trying backup blocks
ext4_image2.img: Unable to fix errors, so marking file system read-only
ext4_image2.img: Filesystem has errors, check forced.

The output shows that the e2fsck command found several errors in the file system, including an invalid superblock and bad group descriptors.

To automatically fix the errors, you can run the e2fsck command without the -n option:

sudo e2fsck -y ext4_image2.img

The -y option tells e2fsck to automatically answer "yes" to all questions, allowing it to fix the errors without user intervention.

Example output:

ext4_image2.img: Superblock invalid, trying backup blocks...
ext4_image2.img: Using backup superblock.
ext4_image2.img: Group descriptors look bad... trying backup blocks
ext4_image2.img: Reinitializing group descriptors
ext4_image2.img: 11/25600 files (0.0% non-contiguous), 25/100000 blocks

The output shows that the e2fsck command was able to automatically fix the errors in the file system by using backup superblocks and group descriptors.

Summary

In this lab, you learned about the purpose and usage of the e2fsck command in Linux. The e2fsck command is used to check and repair the Ext2, Ext3, and Ext4 file systems, which are commonly used file systems in Linux-based operating systems. It can be used to check for file system corruption, recover lost files, and optimize file system performance. You then practiced using the e2fsck command to check and repair a corrupted Ext4 file system by intentionally modifying the superblock and then using the e2fsck command to fix the issues.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like