+4 votes
329 views
FSCK Linux command to repair file system error

in Linux / Unix by (552k points)
reopened | 329 views

1 Answer

+5 votes
Best answer

1. Verify partition status with fsck on Linux
2. Use fsck command to repair file system errors on Linux
3. How to run fsck in rescue mode on Linux

When managing and using any operating system, it has many components internally, but one of the most vital for all optimal operation are file systems. Basically a file system refers to the methods and data structures that the operating system possesses which in turn it uses to follow the sequence of the files on a disk or partition, in other words, the file system allows you to organize the files On the disk..

Types File Systems
File systems can be classified in 3 ways that are:
  • Disk file systems.
  • Network file systems.
  • Special purpose file systems.
Common mistakes
Now, it is normal that at some point the file system, in this case we talk about Linux, present anomalies for causes such as:
  • Read and write errors
  • Presence of viruses or malware
  • Hardware level failures
  • The system does not boot.
  • The units do not respond
  • System failures and more.

In case the file system of the Linux distribution used is unstable, it presents errors of performance and reliability, as administrators or users we must proceed to validate its integrity and for this we have a utility called fsck which TechnoWikis will explain in detail.

What is fsck
Fsck (file system consistency check) is a command line utility developed to analyze and repair errors in the system file system.

This fsck check can be done automatically during boot time or run manually if necessary. fsck is used to verify and optionally repair one or more Linux file systems, file system elements (filesys) can cover values ​​such as:

  • device name (/ dev / hdc1, / dev / sdb2)
  • a mount point (/, / usr, / home)
  • a tag (ext2, ext3)
  • a UUID specifier (UUID = 8868abf6-88c5-4a83-98b8-bfc24057f7bd or LABEL = root).
By default, fsck will attempt to manage file systems on different physical disk drives in parallel to reduce the total amount of time required in the verification process. If no file system is specified on the command line, and the -A option is not indicated, fsck will run the verification of the file systems in the / etc / fstab directory in series.
Available fsck command options
The fsck command must be executed with superuser or root privileges and some of the parameters to be used are:
  • -A: Applies to check all file systems. The origin of the list is / etc / fstab.
  • -C: Displays the progress bar.
  • -l: Lock the device in order to ensure that no other program will attempt to use the partition during verification and thus cause errors
  • -M: Does not verify mounted file systems.
  • -N: deploys the action to be performed but does not really execute it.
  • -P: Allows you to check file systems in parallel, including root.
  • -R: Does not check the root file system. This is useful only with '-A'.
  • -r: provides statistics for each device that is being checked.
  • -T: Does not display the title.
  • -t: It allows us to specify exclusively the types of file systems to verify. The types can be a comma separated list.
  • -V: Provides a description of the action taken.
Exit Code Options
When fsck is running on Linux, an exit code associated with the result is displayed, the options of these codes are:
  • 0: No errors
  • 1: File system errors fixed
  • 2: The system must be restarted
  • 4: File system errors left uncorrected
  • 8: Operational error
  • 16: Error of use or syntax
  • 32: Check canceled by user request
  • 128: Shared library error

1. Verify partition status with fsck on Linux

Step 1

To execute fsck correctly it is necessary that the partition to be verified is not mounted, if we try to run fsck on a mounted drive we will see the following:
 sudo fsck / dev / sda1 
image
Step 2

In this case we must disassemble this unit by executing:
 umount / dev / sda1 
Step 3

Once disassembled, we execute the command on the partition to be analyzed again, in this case we have created a new partition, like this: “sudo fsck / dev / sdb” and this will be the result: image

2. Use fsck command to repair file system errors on Linux

Step 1

In the event that fsck detects errors in the file system we can execute the following line so that fsck tries to correct the errors automatically:
 sudo fsck -y / dev / sdb 
image
Step 2

The "-y" parameter automatically says "yes" to any fsck request to correct a Linux error. It is also possible to run the same on all file systems (without root) as follows:
 fsck -AR -y 
Step 3

We can have other important tasks in which to use the fsck command
How to run fsck on root partition
At specific times it may be necessary to run fsck on the root partition of the system, remember that it is not possible to run fsck while the partition is mounted, and root must always be mounted, it will be possible to use any of the following options:
  • Force fsck to be executed at system boot
  • Run fsck in rescue mode
How to force fsck on system boot
This is a simple task, for this we will create a file called forcefsck in the root partition of the system with the following commands:
 touch / forcefsck 
After this we will force or schedule a system restart, so, during the next boot, the fsck command will be executed. After the system boots up, we can verify if the file still exists with the following command:
 ls / forcefsck 
image

3. How to run fsck in rescue mode on Linux


For this process the first step is to stop any critical service, after this, reboot the system with the "reboot" command.
Step 1

In the start window we select the line “Advanced options for Ubuntu”:

image

Step 2

In the next window we will select the line (recovery mode):

image

Step 3

In the next window we select the “fsck” line:

image

Step 4

The following message will be displayed indicating if you want to reassemble the file system /, select "yes":

image

Step 5

It will start the process of analyzing the Linux file system:

image

Step 6

Once completed, the following window will be displayed:

image

Step 7

There we select the resume line to continue with the normal system startup. Recall that fsck takes the source list in the / etc / fstab directory, we can list the contents of that directory to know which partition will be analyzed by running the following line:
 cat / etc / fstab 
image
Step 8

In this directory there are certain variables that affect the behavior of fsck, these are.
FSCK_FORCE_ALL_PARALLEL
If this environment variable is set, fsck will attempt to verify all specified file systems in parallel, regardless of whether the file systems are on the same device.
FSCK_MAX_INST
This environment variable is responsible for limiting the maximum number of file system verifiers that can be run at the same time on Linux.
PATH
The PATH environment variable is used to find file system verifiers, first, a set of system directories is searched: / sbin, /sbin/fs.d, / sbin / fs, / etc / fs, and / etc, later the set of directories found in the PATH environment is searched.
FSTAB_FILE
This environment variable allows the system administrator to override the standard location of the / etc / fstab file.
LIBBLKID_DEBUG = 0xffff
Enable debug output.
LIBMOUNT_DEBUG = 0xffff
Enable debug output.
More options
Some additional options for use are:
  • Check only one type of file system:
 fsck -AR -t ext2 -y 
  • Do not run fsck on mounted drives
 mount | grep "/ dev / sd *" 
  • Skip the title displayed at the exit of the results
 fsck -TAR 
  • Repair damage automatically
 fsck -a -AR 

The fsck command is a comprehensive solution to optimize the use of the Linux file system and periodically check its status..


by (3.5m points)
edited

Related questions

+4 votes
1 answer
asked Oct 14, 2019 in Linux / Unix by backtothefuture (552k points) | 264 views
+4 votes
1 answer
asked May 22, 2019 in Linux / Unix by backtothefuture (552k points) | 362 views
+3 votes
1 answer
asked Oct 7, 2019 in Windows 10 by backtothefuture (552k points) | 362 views
+5 votes
1 answer
asked Nov 2, 2019 in Linux / Unix by backtothefuture (552k points) | 268 views
+3 votes
2 answers
asked Aug 23, 2019 in Mac by backtothefuture (552k points) | 6.9k views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users