Linux Programming Security

e2fsck – Repair your corrupted Linux file system

e2fsck Repair filesystem

There might be many reasons for your Linux file system to go into unusable or corrupted state. Such as: Sudden power failure, faulty device drivers, faulty software package, unstable updates etc. and this might leave your Linux system in unusable state. e2fsck is a tool that comes handy in such situations and helps you recover your file system and restore your Linux system to usable state. e2fsck checks for ext2/ext3/ext4 for errors and repair them accordingly. In case of ext3 and ext4 filesystems which uses journal, if your Linux OS has been shut down uncleanly without any errors, usually, after replaying the committed transactions in the journal, the file system should be marked as clean and ready for use. Thus, for filesystems that use Journaling, it will normally replay the journal and exit, unless its superblock shows that further checking is needed.

e2fsck in action

Also see: Reset Linux password

Usually it is not recommend to use this tool on mounted file system as it might cause further problems. Even if you still choose to run it on safe file system, result produced by e2fsck still won’t be valid.

Check a partition

You should have root privileges to run this command. If not, you’ll get the error message.

#e2fsck /dev/sdb1

File system need not to be mounted. If the file system is mounted, you’ll get the error message shown below.

#e2fsck /dev/sdb1
e2fsck 1.41.12 (17-May-2010)
e2fsck: Device or resource busy while trying to open /dev/sdb1
Filesystem mounted or opened exclusively by another program?

Automatic repair using e2fsck

Using -p option, you can direct e2fsck to check and automatically repair all found problems without asking user for confirmation.

#e2fsck -p /dev/sdb1

You can also use option -y, which will automatically answer “yes” to all the questions that will be asked by the e2fsck command.

#e2fsck -y /dev/sdb1

Check only (no change)

Using -n option, will direct the e2fsck to perform check only. i.e it will not make any changes to the filesystem, it will only check.

#e2fsck -n /dev/sdb1

Force the filesystem check

If you have a clean file system but you want to force the filesystem check then use the -f option as shown below.

# e2fsck -f /dev/sdb1
e2fsck 1.35 (28-Feb-2004)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 250/73187328 files (6.0% non-contiguous), 26838739/146374231 blocks

Display a progress bar during e2fsck check

Using -C option, this option is used for file descriptor, where e2fsck will send the output, it is very useful if you are doing e2fsck from a shell script.

Moreover if you specify “-C 0”, it will display a progress bar while e2fsck is doing the check, which is very handy, if you want to track the progress of your checking process.

e2fsck -f -C 0 /dev/sdb1
e2fsck 1.35 (28-Feb-2004)
Pass 1: Checking inodes, blocks, and sizes
/dev/sdb1: |=====                                             - 9.5%|

Syntax

#e2fsck [ -pacnyrdfkvtDFV ] [ -b superblock ] [ -B blocksize ] [ -l|-L bad_blocks_file ] [ -C fd ] [ -j external-journal ] [ -E extended_options ] device

All the other useful options

Option Description
-a It’s a similar option as -p. Only difference is, It’s used for backward compatibly.
-b Using this option, you can give an substitute superblock. This option is handy when the main superblock is corrupted. Filesystems that has 1k blocksizes, a backup superblock can be found at block 8193; for filesystems with 2k blocksizes, at block 16384; and for 4k block‐sizes, at block 32768. Use -n option to see otherwise available backup superblocks. The  -b option, which specifies blocksize of the filesystem must be specified in order for the superblock locations that are printed out to be accurate
-B Using this option will result in searching for the superblock using different block sizes. This option forces to use only the given superblock blocksize. If the superblock is not found, it will terminate with a fatal error.
-c Using this option, it will use badblocks program to do a read-only scan of the device in order to find any bad blocks. If any bad blocks are found, they are added to the bad block inode to prevent them from being allocated to a file or directory. If this option is specified twice, then the bad block scan will be done using a non-destructive read-write test.
-C Using this option it will write completion information to the specified file descriptor so that the progress of the filesystem check can be monitored. This option is typically used by programs which are running e2fsck.
-d Print debugging output. Not useful except to debug e2fsck command itself.
-D Use this option to optimize directories in filesystem. Does this by reindexing them, or by sorting and compressing directories for smaller directories, or for filesystems using traditional linear directories.
-E Using this option you can set e2fsck extended options. These are comma separated, and may take an argument using the equals (‘=’) sign. It supports ea_ver=extended_attribute_version and fragcheck
-f Force checking even if the file system seems clean
-F Flush the filesystem device’s buffer caches before beginning. Only really useful for doing time trials.
-j Set the pathname where the external-journal for this filesystem can be found.
-k When combined with the -c option, any existing bad blocks in the bad blocks list are preserved, and any new bad blocks found by running badblocks(8) will be added to the existing bad blocks list.
-l   Add the block numbers listed in the file specified by filename to the list of bad blocks.
-L Using this option set the bad blocks list to be the list of blocks specified by filename. (This option is the same as the -l option, except the bad blocks list is cleared before the blocks listed in the file are added to the bad blocks list.)
-n Using this option open the filesystem read-only, and assume an answer of `no’ to all questions. Allows e2fsck to be used non-interactively. This option may not be specified at the same time as the -p or -y options.
-p Automatically repair (“preen”) the file system. This option will cause the automatic fixing of any filesystem problems that can be safely fixed without human intervention. If e2fsck discovers a problem which may require the system administrator to take additional corrective action, it will print a description of the problem and then exit with the value 4 logically or’ed into the exit code.
-r This option does nothing at all; it is provided only for backwards compatibility.
-t Print the timing statistics. If the option is used two times, additional timing statistics are printed on a pass by pass basis.
-v Verbose mode. Shows detail view of the processes.
-V Print version information and exit.
-y Set the default answers to “yes” for all questions; allows e2fsck to be used without interacting. It is not recommended option to use when optoins -n or -p are allowed.

The exit code returned by e2fsck is the sum of the following conditions:

0 – No errors
1 – File system errors corrected
2 – File system errors corrected, system should
be rebooted
4 – File system errors left uncorrected
8 – Operational error
16 – Usage or syntax error
32 – E2fsck canceled by user request
128 – Shared library error

About the author

Ajay Verma

A Computer Science Graduate, who works extensively on open source projects. His Areas Of interest are: Network Security, Linux Administration, FOSS, Python, and C programming.

Add Comment

Click here to post a comment