How to repair the file system automatically with fsck

tune2fs

Over time the cost of hardware is getting lower and its capacity increasing, but if something important we carry on our computers is the information stored there, for which we could say almost without fear of being wrong that the hard drive is the part that should worry us the most. Unfortunately this is not always achieved and many users leave their health to chance, until when something bad finally happens it is too late for regrets and valuable documents, images, videos or even music are lost.

The issue is that with a little work we can leave everything very well arranged for the system to take care of automatically check the status of our file system to determine if everything is in order, and this in Linux we can do it through a veteran who still has a lot to give: the command fsck. Of course, then each distro has its way of doing things, so let's see how to configure it in the most important distros.

In the case of Debian and its derivatives (for example Ubunto or Linux Mint, we rely on the file / etc / default / rcS, which we open for editing:

sudo gedit / etc / default / rcS

Then we add:

FSCKFIX = yes

In the case of CentOS, for example, things are different and we rely on the file / etc / sysconfig / autofsck which we also open to edit (in my case, with Gedit):

sudo gedit / etc / sysconfig / autofsck

Then we add a line in said file:

AUTOFSCK_DEF_CHECK = yes

Now, those steps that we indicated in the previous paragraphs are used for when we want run the check using fsck at every system startup, and although it is very good it can take a long time if our disk drives and partitions are very large. Therefore, we can take advantage of the advantages offered by another Linux tool called tune2fs, which among other things enables us carry out periodic checks to our file system so that these are carried out from time to time but not at each start of our computer.

We first check the status of the current configuration, executing:

sudo tune2fs -l / dev / sda1

We can analyze what the computer gives me when I run it, and for this we have to pay attention to some of the parameters that tune2fs looks at. For example, 'Filesystem state', which as we see in the upper image of this post in my case marks me 'clean' and that's a good start. However, do not be fooled, and below we see other equally important parameters that are not so positive.

E.g. 'Mount count', which indicates the number of times our file system has to be mounted before re-checking and that in my case is 270, a very high number, that without neglecting that the last time fsck was run on my system was on May 7, 2013. Another is that of 'Check interval', which indicates the maximum time in months that we want to allow to pass without performing this integrity check; if it is set to 0 as in my case, it will not be taken into account.

If we want the check to run every 30 system starts:

sudo tune2fs -c 30 / dev / sda1

If we want a maximum of 3 months to pass before performing the check again:

sudo tune2fs -i 3m / dev / sda1

But if Linux differs in something, it is in the fact that it offers us a multitude of options, so to all that we can add the possibility of performing the integrity check of our file system at the next system startup, that is, we do it on demand and only once.

For this we execute:

sudo touch / forcefsck

With this we create an empty file called forcefsck, which will be located in the root directory and thanks to this the next time the computer is started the file system will be checked using fsck, and after that this file will be deleted automatically so that in subsequent starts it will no longer be performed.

As we can see, in Linux the possibilities are always many and thanks to this we can adapt this important functionality to our needs, especially because of the value of all the information we have stored on our hard drives. In this way we can perform an integrity check manually when we know that we will not need the computer and therefore we are not worried about what it may take, or else do it on a scheduled basis every certain number of system restarts.


5 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   sundaypv said

    I wrote sudo tune2fs -l / dev / sda1 in the terminal and the following came out;
    domingopv @ pc1: ~ $ sudo tune2fs -l / dev / sda1
    [sudo] password for sundaypv:
    tune2fs 1.42.9 (4-Feb-2014)
    tune2fs: Bad magic number in super-block while trying to open / dev / sda1
    Could not find a valid superblock for the filesystem.
    domingopv @ pc1: ~ $
    What does this mean?

  2.   Juan said

    That you got into an operating system like me and now I don't know how to get out of this

  3.   HugoDes said

    when they indicate / dev / sda1 they are assuming you have linux installed on that partition.

    you must first check where your linux is installed (you can use gparted) and place the correct partition (example / dev / sda7)

  4.   America said

    Greetings, is it 100% necessary to disassemble the disk and if so, how is it disassembled and then reassembled?
    I have 7 centos.

  5.   George said

    Thanks. Greetings from Perillo (Oleiros) - A Coruña.