How to Fix File System Errors in Linux Using ‘fsck’

How to Fix File System Errors in Linux Using ‘fsck’

Preface

The file system is responsible for organizing how data is stored and restored. Anyway, over time the file system may get corrupted and parts of it may become inaccessible. If your file system exhibits such inconsistencies, it is recommended to verify its integrity.

This can be done with a system utility called fsck (file system consistency check). This check can be done automatically during boot or run manually.

In this article, we will review the fsck utility and its usage to help you repair disk errors.

When to Use fsck in Linux

There are different scenarios when you want to run fsck. Here are some examples:

  • The system cannot boot.
  • A file on your system has become corrupted (typically you might see input/output errors).
  • Attached drives (including flash drives/SD cards) will not work properly.

Available fsck options

You need to use superuser privileges or root to run the Fsck command. You can use different parameters. Their usage depends on your specific situation. You'll see some of the more important options below:

  • -A – Used to check all file systems. This list is taken from /etc/fstab.
  • -C – Display a progress bar.
  • -l - Locks the device to ensure that no other programs try to use the partition during the check.
  • -M - Do not check mounted file systems.
  • -N - Only show what would be done - no actual changes are made.
  • -P - If you want to check file systems in parallel, including root.
  • -R - Do not check the root file system. This only applies to ‘ -A ’.
  • -r - Provides statistics for each device being checked.
  • -T – Do not display headers.
  • -t - Specifies the type of file system to check. Types can be a comma separated list.
  • -V - Provides a description of the operation in progress.

How to Run fsck to Repair Linux File System Errors

To run fsck, you need to make sure the partition you want to check is not mounted. For the purpose of this article, I will use the second drive /dev/sdb mounted at /mnt.

What happens if I try to run fsck while the partition is mounted.

# fsck /dev/sdb 

Run fsck on the mounted partition

To avoid this, use unmount partition.

# umount /dev/sdb

Then fsck can be run safely.

# fsck /dev/sdb 

Running fsck on Linux partitions

Understanding fsck exit codes

After running fsck it will return an exit code. These cods can be seen in fsck's man page by running the following command:

# man fsck 
0 No errors 
1 Filesystem errors corrected 
2 System should be rebooted 
4 Filesystem errors left uncorrected 
8 Operational error 
16 Usage or syntax error 
32 Checking canceled by user request 
128 Shared-library error

Fix Linux File System Errors

Sometimes multiple errors can be found on a file system. In this case, you might want fsck to automatically try to correct the errors. This can be done by:

# fsck -y /dev/sdb

The -y flag automatically "yes" to any prompts from fsck to correct errors.

Likewise, you can run the same operation on all file systems (without root):

$ fsck -AR -y

How to run fsck on Linux root partition

In some cases, you may need to run fsck on the root partition of your system. Since you cannot run fsck while the partition is mounted, you can try one of these options:

  • Force fsck on system boot
  • Run fsck in rescue mode

We will review both scenarios.

Force fsck on system startup

This is relatively easy to accomplish, the only thing you need to do is create a file called forcefsck in the root partition of your system. Use the following command:

# touch /forcefsck

You can then simply force or schedule a reboot of your system. During the next boot, fsck will be executed. If downtime is important, careful planning is recommended, as fsck may take some extra time if you have many used inodes on your system.

After the system boots, check if the file is still there:

# ls /forcefsck

If so, you may want to remove it to avoid fsck appearing every time the system boots.

Run fsck in rescue mode

Running fsck in rescue mode requires a few more steps. First prepare your system for a reboot. Stop any critical services like MySQL/MariaDB etc. and then type.

# reboot

During boot, hold down the shift key to bring up the grub menu. Select Advanced options.

Grub Advance Options

Then select Recovery Mode.

Select Linux Recovery Mode

In the next menu select 'fsck'.

Select fsck Utility

You will be asked if you wish to remount the / filesystem. Select yes.

Confirm the root file system

You should see something similar to this.

Run fsck file system check

You can then resume normal startup by selecting "Resume".

Select Normal Startup

in conclusion

In this tutorial, you have learned how to use fsck and run consistency checks on different Linux file systems. If you have any questions about fsck, don’t hesitate to submit them in the comments section below.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of upgrading Python and installing pip under Linux
  • How to use for loop combined with if to find files in Linux Shell directory
  • How to get the real path of the current script in Linux
  • Linux virtual memory settings tutorial and practice
  • Detailed usage of Linux text search command find
  • One question to understand multiple parameters of sort command in Linux
  • Linux editing start, stop and restart springboot jar package script example
  • Linux uses join -a1 to merge two files
  • How to deal with the prompt "Operation not permitted" when deleting files in Linux
  • Introduction to the use and disabling of transparent huge pages in Linux

<<:  In-depth understanding of the implementation principle of require loader

>>:  MySQL 8.0.3 RC is about to be released. Let’s take a look at the changes

Recommend

Summary of commonly used commands for docker competition submission

Log in to your account export DOCKER_REGISTRY=reg...

Detailed explanation of the relationship between Linux and GNU systems

Table of contents What is the Linux system that w...

Vue3 slot usage summary

Table of contents 1. Introduction to v-slot 2. An...

Detailed explanation of vue simple notepad development

This article example shares the specific code of ...

JavaScript implements password box verification information

This article example shares the specific code of ...

js to make a simple calculator

This article shares the specific code of making a...

Vue's detailed code for implementing the shuttle box function

Vue - implement the shuttle box function, the eff...

Directory permissions when creating a container with Docker

When I was writing a project yesterday, I needed ...

Detailed explanation of the use of vue-resource interceptors

Preface Interceptor In some modern front-end fram...

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...

Web Design: The Accurate Location and Use of Massive Materials

Three times of memorization allows you to remembe...

How to use file writing to debug a Linux application

In Linux, everything is a file, so the Android sy...

Sample code for configuring nginx to support https

1. Introduction Are you still leaving your websit...