Pages

Wednesday, June 10, 2015

Repair Filesystem Errors in Ubuntu with fsck

I have an ATA 80GB HDD in my laptop now, which is, well yes, old and has some issues. I wanted to test it with fsck and tell it to repair any file system errors. So I looked into the topic a bit.

Important about fsck

The most important thing you need to know about fsck is that it can only fix filesystems if they are not mounted. Hence when your computer is running, you can (if you really want to) run fsck on your /home directory, however not on root. So in general there are 2 main approaches that you can implement to check your entire hard drive,

Run a live CD version

Run fsck during boot

Here I will only explain how to run fsck on boot as I think this is much less of a hassle than creating a live CD, booting from it etc. Furthermore, the benefit of running fsck at boot (as you will see) is that you can schedule it at intervals of your liking. Sort of a "set it and forget it" approach.

Let fsck attempt to fix errors automatically

Firstly tell fsck to automatically repair filesystem inconsistencies during boot. This can be set in the following steps. First, open /etc/default/rcS with a text editor, e.g. nano

Schedule fsck run interval on boot

Scheduling an fsck check can be based on 2 main parameters, invoked by 2 command parameters,

-c = sets interval based on how many times the filesystem was mounted

-i = sets interval based on how much time has passed since the last check

The next step is to schedule fsck running in specific intervals. I have set mine for every 10 days initially, however later on I moved it to once per month to save some time at boot. Of course, next to this a mount-dependent check can be scheduled too.

Determine filesystem mount points

First of all, quickly check on which filesystem your root and home directory are mounted. I like to do this quickly and easily with

After this you are done, the above output will tell you when the previous and next checks were and will be. However, if you wish to run a one-off check at boot, you can do it as well.

Turning off periodic fsck check on boot

Quoting from the tune2fs manual,

"It is strongly recommended that either -c (mount-count-dependent) or -i (time-dependent) checking be enabled to force periodic full e2fsck(8) checking of the filesystem. Failure to do so may lead to filesystem corruption (due to bad disks, cables, memory, or kernel bugs) going unnoticed, ultimately resulting in data loss or corruption."

However, if you really want to switch it off completely,

sudo tune2fs -c -1 -i 0

Which will turn off both time and max-mount-count dependent checks.

Force check on next boot

If you want to do a check on the next boot execute the following command,

sudo touch /forcefsck

This will create a temporary file in / that tells fsck to execute a filesystem check on the next boot.