Scheduling ScanDisk in Ubuntu Linux (fsck, tune2fs, e2fs, chkdsk)

Ubuntu Linux as a desktop has many advantages, one being that you have more power to do things you need to do. One major disadvantage is that few people know what to do well. I recently started to get random lock-ups and decided to force a filesystem check.

The equivalent of Windows scandisk / chkdsk on Linux is the command fsck (from "filesystem check"). Invoking it with no forethought results in a scary error message:

fsck from util-linux-ng 2.17.2 e2fsck 1.41.11 (14-Mar-2010) /dev/sda7 is mounted. WARNING!!! The filesystem is mounted. If you continue you ***WILL*** cause ***SEVERE*** filesystem damage. Do you really want to continue (y/n)?
Obviously, you don't want to kill yourself or your file system and neither do I. To check a disk in Ubuntu you first have to unmount it, which generally - and especially when it comes to your main partition - means that you have to reboot. To force a file system check on reboot, type the following in the terminal:
> sudo touch /forcefsck
This will create a file named "forcefsck" in the root directory, causing the system to perform a file system check. In a situation where the opposite is true - i.e., you want to bypass a filesystem check - you would issue the command:
> sudo touch /fastboot
If you find yourself doing this often, you might want to create an alias:
alias restart='sudo touch /forcefsck && sudo reboot'
Alternatively, you could create a .sh file containing the same commands as above, save it in your ~/bin and invoke it instead of the alias, but that seems rather silly.
One way you can force a filesystem check on every boot is to edit FSCKFIX=yes in /etc/default/rcS but this is an overkill, especially in journaled filesystem such as ext3 or ext4. In such a filesystem, a check is performed by default every 30 boot sequences, which is normally more than enough. If, however, you want to adjust that frequency (of checks), you might want to use tune2fs, as follows:
  • every 60 bootups: $sudo tune2fs -c 60
  • once a week (also works with d for days or m for months) $sudo tune2fs -i 1w
An older method involved the use of a script - AutoFsck. You may find it useful in older versions of Ubuntu or other distros using either GDM or KDM.
Hope this helps! Did you find anything other than touch /fastboot useful?
Sources / More info: uf, man-fsck, man-tune2fs, wiki-AutoFsck

Comments

Popular posts from this blog