How to use fsck to find and repair disk errors & bad sectors

Use fsck to find and repair disk errors and bad sectors

how to find and repair disk errors and bad sectors using Fsck

Fsck

The “File System Consistency Check” is the full form for “fsck, It is a utility that examines the file system for errors(in Unix and Unix-like operating systems, such as Linux, macOS, and FreeBSD) and attempts to repair them if possible. It uses a combination of built-in tools to check the disk and generates a report of its findings.

On some systems, fsck runs automatically after an unclean shutdown or after a certain number of reboots.

When to Use fsck in Linux

Use fsck to check the file system,

  • If the system fails to boot.
  • If files on the system become corrupt (often you may see input/output error).
  • If attached drive (including flash drives/SD cards) is not working as expected.

Will fsck delete files

fsck does not touch the files. It is basically a front-end program that does all kinds of filesystem checks (ie. it checks the integrity of the journaling system). Besides checking inodes and blocks fsck also checks for incorrect

fsck Options and Arguments

Option Action
-a Attempt to fix errors automatically. Use with caution.
-f Force fsck to check a file system even if it thinks it’s clean.
-A Check all disks listed in /etc/fstab.
-C Show progress bar (ext2 and ext3 file systems only).
-M Skip mounted file systems.
-N Test run. Describes what would happen without executing the check itself.
-P Use with the -A option to run multiple checks in parallel.
-R If using the -A option, do not check the root filesystem.
-t Check only a specific type of filesystem.
-T Skip the title on startup.
-y Interactive repair mode.

How to Run fsck to Repair Linux File System Errors

In order to run fsck, you will need to ensure that the partition you are going to check is not mounted. The easiest and safest way to unmount your disk is to use Rescue Mode. You need to enter in to rescue mode via either single user mode or by uploading and booting rescue ISO on Cloud Server.

What is Rescue Mode

The rescue mode provides the ability to boot a small Red Hat Enterprise Linux environment entirely from CD-ROM, or some other boot method, instead of the system’s hard drive. As the name implies, rescue mode is provided to rescue you from something. By booting the system from an installation boot CD-ROM

If you are using fsck, the easiest and safest way to unmount your disk is to use Rescue Mode. Also the fsck should be run only as a user with root permissions.

How to view mounted Disks and their Location.

df’ command helps to view a list of currently mounted disks. If you are in Rescue Mode, the disk you want to check should not be listed in the output.

df -h

root@test:~# df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 739M 1016K 738M 1% /media/ramdisk
/dev/sdh 160M 160M 0 100% /media/sdh
/dev/loop0 146M 146M 0 100% /media/compressed_root
unionfs 739M 1016K 738M 1% /
devtmpfs 10M 0 10M 0% /dev

Run fdisk to view disk locations:

fdisk -l

Then copy the location of the target disk to use with the fsck command.

How to unmount manually the partition

Use the command umount to unmount the disk location copied in the last step. For example take it as /dev/sda

umount /dev/sda

If the disk is declared in /etc/fstab, change the mount point to none there as well.

How to Check for Errors on a Disk

Run fsck on the target disk, using the desired options. For example, check all file systems (-A) on /dev/sda:

fsck -A /dev/sda

After running fsck, it will return an exit code. The description of the codes are as follows:

Code Code Meaning
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 cancelled by user request
128 Shared-library error

Repair Linux Filesystem Errors

Sometimes more than one error can be found on a filesystem. In such cases run fsck to automatically attempt to correct the errors. This can be done with:

fsck -y /dev/sda

To run the same on all filesystems (without root):

fsck -AR -y

Performing a File System Check

To run fsck by entering the following command, replacing /dev/sda with the location of the disk you want to check and repair:

e2fsck -f /dev/sda

If no problems are detected, fsck will display the tests it performed

e2fsck -f /dev/sda
e2fsck 1.42.13 (13-Sept-2019)
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/sda: 109771/1568000 files (0.5% non-contiguous), 675014/6422528 blocks

If fsck determines that there is a problem with the filesystem, it will prompt you to fix problems as they are found during each test:

root@ttyS0:~# e2fsck -f /dev/sda
e2fsck 1.42.13 (13-Sept-2019)
ext2fs_check_desc: Corrupt group descriptor: bad block for block bitmap
e2fsck: Group descriptors look bad… trying backup blocks…
e2fsck: Bad magic number in super-block while using the backup blockse2fsck: gok
Superblock has an invalid journal (inode 8).
Clear?

Once the filesystem check completes, any problems detected should be fixed.

At last, to exit from rescue mode. Try reboot your server, If fsck fixed the issues, the server should boot normally.

reboot

GET STARTED WITH OUR HOSTING SUPPORT SERVICES

FAQ SECTION

What Is a bad sector ?

Bad sectors are basically clusters of data on a hard disk which are un readable. Bad sectors can cause certain damage to your hard disks. Bad sectors are common and mostly repairable.

How are HDD bad sectors created?

It could because of a number of reasons like- improper shutdowns, hard disk defects, other hardware defects , malware etc.

Can a Hard Bad sector can be fixed ?

Hard bad sectors refers to a physical damage of the HDD. Hard bad sectors cannot be repaired but are preventable.

What are the disadvantages of using Fsck ?

Fsck disk repairing is a time-consuming task as sometimes it takes multiple scans and especially slow in case of large disk sizes. Also requires prior knowledge and a general understanding of the file systems.

How to fix Bad sectors on windows ?

Bad sector issues can be fixed in windows using a tool called Chkdsk .

Rohith Krishnan

Rohith SK is an MSC computer science graduate living in Cochin, Kerala. As a technology enthusiast, he is always on the lookout for the latest trends and developments in the field, with a particular interest in cloud computing and DevOps updates. Apart from his passion for technology, Rohith SK is an avid reader and enjoys spending his free time exploring different genres of literature. He believes that reading is one of the best ways to expand one's knowledge and understanding of the world. With his expertise in computer science and a passion for technology, Rohith SK regularly contributes articles and blog posts on the latest trends and updates in the industry. His articles offer insights and valuable perspectives on the various aspects of cloud computing and DevOps, and are widely read and appreciated by readers all over the world. As an experienced technology writer and researcher, Rohith SK's articles are well-researched, informative, and easy to understand, making them accessible to readers of all levels of technical knowledge. Whether you're a beginner looking to learn more about the latest trends in technology, or an experienced professional seeking insights and updates, Rohith's articles are sure to provide valuable information and insights.

Leave a Reply