Monday, 9 March 2015

FSCK command to recover corrupt Linux file system:


A file system is basically a way to organize data. All the tasks of efficient data storage, memory management, data retrieval, data updation are handled by the file system procedures. A Linux system is similar to directory storage. A Linux file system manages all the data in an ordered tree-like structure on the hard disk. A Linux file can be a Program, service, text, image, device and so forth. Common Linux file systems are Ext4, Ext3, Ext2; FAT32, FAT16, and FAT12 file system.

General LINUX file organization can be shown as below:



Like other file systems, the Linux file system is also prone to corruption. 

Common reasons for file system corruption are:

  • improper shutdown (hard reset)
  • improper start-up 
  • kernel errors 
  • mounting damaged file system
  • overlapping partitions
  • Hardware failures, Hard disk errors, bad block on disk, bad disk controller etc.

Any inconsistency/damage/corruption to the file system can make your data inaccessible from the Linux volumes. Luckily Linux provided the FSCK tool for recovering your data from such data loss situations. 


What is FSCK Basically? It’s a consistency checker for LINUX file system. The command performs the file system check for corruption or other related issues and later on performs repair of the damaged or corrupt file system.

Note: The fsck command should not be used on a mounted device. This can lead to serious data loss situations as it might be possible that the deleted files are still in use that may cause damage to the file system. 

The FSCK command is written as below:

fsck [ -sACVRTNP ] [ -t fstype ] [filesys ... ] [--] [ fs-specific-options ] 

Here, filesys can be a device name, a mount point, an ext2 label or UUID specifier.

You can use the fsck command to run on multiple Linux file system on different hard disks simultaneously. Thus it will take smaller time span to scan and repair multiple file systems parallely. 

If you specify the -As options in the command line then the fsck utility will check the file systems in /etc/fstab serial. Otherwise, if you skip to mention any file systems and -A option in the command line then by default the /etc/fstab file systems will be checked.  fsck acts like a front-end for the various file system checkers (fsck.fstype).

After execution of fsck command, check on multiple file systems a single collective exit code is returned. This code is the bit-wise OR of all the exit codes for individual file system.
The exit code returned by fsck is the sum of the following conditions: 

0 - No errors 
1 - File system errors corrected 
2 - System should be rebooted 
4 - File system errors left uncorrected 
8 - Operational error 
16 - Usage or syntax error 
32 - Fsck cancelled by user request 
128 - Shared library error 

Some common options used with the fsck command are:

-s 
While you are running fsck check over multiple systems then you need to use this option to serialize the operations and run the checkers in interactive mode. 

-t fslist 

Here, you can specify the list of file systems that are to be checked. You specify the file system names separated by comma. You can use the -A flag to make the checker to check only the file systems that match fslist. 
Along with the file systems you can also specify the options specifiers in the fslist parameter. Option specifiers have the format   opts=fs-option
IF the list is prefixed by a negation operator 'no' or '!', then the checker will check all the file systems excluding the listed ones. If you don’t apply this operator the file systems listed in fs list will be checked. 
If you prefix the options specifier with a negation operator, then the checker will only check file systems not containing the fs-option in the /etc/fstab mount options field.

-A 
This option makes the fsck to check all /etc/fstab file systems in one run.  

-P 
 Most of the sys admins use the -P option with -A flag to prevent their root file system from   re-partitioning. 

-R 
This option is used with the -A option. This option makes the fsck skip the root file system while checking the file systems with the -A flag

-C  
This option is used to display the completion/progress bars for those file systems checkers which support them. At a time, only single progress bar will be shown.

-N 
Using fsck command with -N only displays the whole process that would be done, it doesn't execute. 

-T 
Using fsck command with -T option will skip the title on start-up. 

-V 
This option enlists all file system-specific commands to be executed as verbose output.

fs-specific-options 

The file system-specific options and arguments are followed with a --. Such options are handled by the file system-specific checkers (fsck.fstype). 

Common file system-specific options are:

-a 
This option is used to perform an automatic file system repair without promoting or asking you for any permission/confirmations. This can be extremely dangerous in the case of extensive file system damage.

-r  
This option is used to perform interactive system repairs. This option first asks you for confirmation to perform file system repair. Then only it begins the repair process. 
     
If the FSCK fails to help out you for data recovery then you need to look for alternative Linux recovery software. Such Linux recovery software are easily available with free demo like Stellar Phoenix Linux Recovery Software that can recover Linux partitions in Windows. You just need to make an ample search and selection among the various Linux recovery software(s).