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,
  1. Run a live CD version
  2. 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
sudo nano /etc/default/rcS 
 And find the line at the end where it says,
# automatically repair filesystems with inconsistencies during boot
FSCKFIX=no
and change the tunable "no" to "Yes".

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
df -h
Example output of df -h command, showing mount points of different directories
From this you can see that my root directory is on /dev/sda2, while my /home is on /dev/sda3, hence I will tell fsck to check these filesystems.

Schedule fsck based on time interval

To tell fsck to check my root filesystem I have to issue the following command,
sudo tune2fs -i 1m /dev/sda2

Which will then return,
Information returned when executing above command.
This tells Ubuntu to run filesystem check at boot every month on the root directory. You can also use different intervals, below are some examples.
-i 10d = interval of 10 days
-i 1m = interval of 1 month
-i 2w = interval of 2 weeks

Schedule fsck based on number of mounts

Additionally, or instead of, time-based fileysystem checks you can also tell fsck to run after, say every filesystem 30 mounts. To do this use the -c parameter,
sudo tune2fs -c 30 /dev/sda2
Tip: If you wish to set any other interval, check out man tune2fs for more information!

Confirmation

Also, you can check if the effects really took place with,
sudo tune2fs -l /dev/sda2
This will give an output similar to the one below. The lines we are interested in are towards the bottom, highlighted in red.
tune2fs 1.42 (29-Nov-2011)
Filesystem volume name:   <none>
Last mounted on:          /
Filesystem UUID:          9f344424-1e1c-47db-a2b6-13104e5c754c
Filesystem magic number:  0xEE12
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              1120112
Block count:              4461312
Reserved block count:     223065
Free blocks:              1219061
Free inodes:              467223
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      893
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8176
Inode blocks per group:   511
RAID stride:              32603
Flex block group size:    16
Filesystem created:       Wed Oct 26 15:27:33 2011
Last mount time:          Wed Jun 10 08:06:25 2015
Last write time:          Wed Jun 10 09:53:37 2015
Mount count:              16
Maximum mount count:      27
Last checked:             Fri May 29 20:57:05 2015
Check interval:           2592000 (1 month)
Next check after:         Sun Jun 28 20:57:05 2015

Lifetime writes:          357 GB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:              256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
First orphan inode:       48
Default directory hash:   half_md4
Directory Hash Seed:      8dc55d5d-db7c-4717-ab06-6fad98d89896
Journal backup:           inode blocks
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.

No comments:

Post a Comment