- How to Force Check Root Filesystem using fsck During Reboot
- Root Filesystem
- Tune2fs Output Before Reboot
- Create /forcefsck File to Force Check Root Filesystem
- Tune2fs Output After Reboot
- If you enjoyed this article, you might also like..
- How to Use ‘fsck’ to Repair Linux File System Errors
- When to Use fsck Command in Linux
- fsck Command Options
- Run fsck Command to Repair Linux File System Errors
- Understanding fsck Exit Codes
- Fsck Repair Linux Filesystem
- How to Run fsck on Linux Root Partition
- Force fsck Upon System Boot
- Run fsck in Rescue Mode
- Related Posts
- 37 thoughts on “How to Use ‘fsck’ to Repair Linux File System Errors”
How to Force Check Root Filesystem using fsck During Reboot
Performing fsck on non-root filesystem is fairly straight forward. But, for root filesytem, you cannot perform fsck when it is mounted. This quick tutorial explains how to force filesytem check for a root filesystem.
Root Filesystem
# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 63G 41G 19G 69% /
If you run a fsck to check your root filesystem, you’ll get the following error message, as /dev/sda1 is mounted, and your cannot perform fsck on a mounted filesystem.
# fsck /dev/sda1 fsck from util-linux-ng 2.17.2 e2fsck 1.41.12 (17-May-2010) /dev/sda1 is mounted. e2fsck: Cannot continue, aborting.
If you are performing a fsck on a non-root file system, you can just unmount that partition and perform fsck. But in this case, we like to perform fsck on a root filesystem. So, what is the solution? Also, if you are new to fsck, refer to this: 10 Linux Fsck Command Examples to Check and Repair Filesystem
Tune2fs Output Before Reboot
Before we reboot the system, let us check when was the last time fsck performed a check on the root filesystem. For this, use tune2fs command, and grep for “check” as shown below.
# tune2fs -l /dev/sda1 | grep -i check Last checked: Mon Nov 24 12:39:44 2015 Check interval: 15552000 (6 months) Next check after: Sun May 22 13:39:44 2016
- Last checked: indicates the last time the root filesystem check happened. This happened on Nov.
- Check internal: indicates how often the root filesystem will be checked. In this example, it will wait for another 6 months before fsck will be executed on this filesystem during reboot.
- Next check after: indicates the date and time after which when you perform a reboot, the filesystem will be checked. This will happen only after May.
But, in our case, we don’t want to wait until May. We want to perform root filesystem fsck check now.
Create /forcefsck File to Force Check Root Filesystem
So, to force a root filesystem check, one of the easy way is to reboot the system, and force fsck to perform the filesystem when the system is starting up before the root filesystem is mounted by the kernel.
To force the fsck during reboot, first create an empty file called forcefsck under / as shown below.
# cd / # touch forcefsck # ls -l /forcefsck -rw-r--r--. 1 root root 0 Mar 9 20:15 /forcefsck
Tune2fs Output After Reboot
If you have console access, you can see that fsck will be performing the check on the / filesystem during the reboot.
After the check, fsck will automatically delete the /forcefsck file that we created earlier. After the reboot, you won’t see this file anymore.
# ls -l /forcefsck ls: cannot access /forcefsck: No such file or directory
Now, if you execute tune2fs, you’ll see the “Last checked” field was updated with the current timestamp. This confirms that fsck performed the root filesystem check during the reboot.
# tune2fs -l /dev/sda1 | grep -i check Last checked: Wed Mar 09 20:30:04 2016 Check interval: 15552000 (6 months) Next check after: Mon Sep 05 21:30:04 2016
You’ll also see the value of “Next check after” changed accordingly. i.e 6 months from the time we rebooted the system.
If you enjoyed this article, you might also like..
Comments on this entry are closed.
dipanjan mukherjee – The OS checks for that file “forcefsck” in root / and if exists it will check disk. Very simple and can be created by a script or by default or can be touched like above example touch forcefsck, it doesn’t always work. File systems are checked every 20 or 30 restarts, etc or specified restart count. # tune2fs -c 50 /dev/hda1 Will change fsck check count to 50
If /forcefsk exists, mountall (who is responsible for mounting filesystems during boot) will be called with –force_fsck as paramaeter, causing it to run a filesystem check. This is controlled by mountall.conf.
I wouldn’t rely on /forcefsk in the root. That isn’t the best option but scheduling is and you know it will run the next time around after a reboot. The script that checks if /forcefsk exists should be verified first. I never rely on it or I double check that it exists first and it runs at boot time.
Nice Post, I found this some time ago and found your help a life saver!
Today I happened to stumble past it again, and had a FYI about doing a fsck in recovery.
You can force the partition to unmount by using “umount” in recovery like this:
# umount -dfrv /dev/sda1 It might throw a message not able to verify unmount — with a ref to mtab.
But, it works, just run this after: e2fsck -f -v -y -C 0 /dev/sda1 — and it will run like a champ! Hope this helps.
How to Use ‘fsck’ to Repair Linux File System Errors
Linux Filesystems are responsible for organizing how data is stored and recovered. One way or another, with time, the filesystem may become corrupted and certain parts of it may not be accessible. If your filesystem develops such inconsistency it is recommended to verify its integrity.
This can be completed via a system utility called fsck (file system consistency check), which checks the root file system automatically during boot time or ran manually.
In this article, we are going to review the fsck command and its usage to help you repair Linux disk errors.
When to Use fsck Command in Linux
There are different scenarios when you will want to run fsck. Here are a few examples:
- The system fails to boot.
- Files on the system become corrupt (often you may see input/output error).
- The attached drive (including flash drives/SD cards) is not working as expected.
fsck Command Options
The fsck command needs to be run with superuser privileges or root. You can use it with different arguments. Their usage depends on your specific case. Below you will see some of the more important options:
- -A – Used for checking all filesystems. The list is taken from /etc/fstab .
- -C – Show progress bar.
- -l – Locks the device to guarantee no other program will try to use the partition during the check.
- -M – Do not check mounted filesystems.
- -N – Only show what would be done – no actual changes are made.
- -P – If you want to check filesystems in parallel, including root.
- -R – Do not check the root filesystem. This is useful only with ‘ -A ‘.
- -r – Provide statistics for each device that is being checked.
- -T – Does not show the title.
- -t – Exclusively specify the Linux filesystem types to be checked. Types can be comma-separated lists.
- -V – Provide a description of what is being done.
Run fsck Command 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. For the purpose of this article, I will use my second drive /dev/sdb mounted in /mnt .
Here is what happens if I try to run fsck when the partition is mounted.
To avoid this unmount the partition using.
Then fsck can be safely run with.
Understanding fsck Exit Codes
After running fsck, it will return an exit code. These codes can be seen in fsck’s manual by running:
# man fsck 0 No errors 1 Filesystem errors corrected 2 System should be rebooted 4 Filesystem errors were left uncorrected 8 Operational error 16 Usage or syntax error 32 Checking canceled by user request 128 Shared-library error
Fsck Repair Linux Filesystem
Sometimes more than one error can be found on a filesystem. In such cases, you may want fsck to automatically attempt to correct the errors. This can be done with:
The -y flag, automatically “yes” to any prompts from fsck to correct an error.
Similarly, you can run the same on all filesystems (without root):
How to Run fsck on Linux Root Partition
In some cases, you may need to run fsck on the root partition of your system. Since you cannot run fsck while the partition is mounted, you can try one of these options:
We will review both situations.
Force fsck Upon System Boot
This is relatively easy to complete, the only thing you need to do is create a file called forcefsck in the root partition of your system. Use the following command:
Then you can simply force or schedule a reboot of your system. During the next bootup, the fsck will be performed. If downtime is critical, it is recommended to plan this carefully, since if there are many used inodes on your system, fsck may take some extra time.
After your system boots, check if the file still exists:
If it does, you may want to remove it in order to avoid fsck on every system boot.
Run fsck in Rescue Mode
Running fsck in rescue mode requires a few more steps. First, prepare your system for reboot. Stop any critical services like MySQL/MariaDB etc and then type.
During the boot, hold down the shift key so that the grub menu is shown. Select “Advanced options”.
Then choose “Recovery mode”.
In the next menu select “fsck”.
You will be asked if you wish to have your / filesystem remounted. Select “yes” .
You should see something similar to this.
You can then resume normal boot, by selecting “Resume”.
Conclusion
In this tutorial, you learned how to use fsck and run consistency checks on different Linux filesystems. If you have any questions about fsck, please do not hesitate to submit them in the comment section below.
I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.
Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.
Related Posts
37 thoughts on “How to Use ‘fsck’ to Repair Linux File System Errors”
fsck from util-linux 2.38.1 fsck.ext2: Bad magic number in super-block while trying to open /dev/SDA /dev/SDA : the super-block could not be read or does not describe a valid ext2/ext3/ext4 filesystem.
If the device is valid and it really contains an ext2/ext3/ext4 filesystem (and if not swap or ufs or something else), then the superblock is corrupt, and you might try running e2fsck with an alternate superblock:
# e2fsck -b 8193 or e2fsck -b 32768
Found a dos partition table in /dev/SDA. When I try to run the e2fsck commands it gave, it shows sh: syntax error: unexpected newline. And then when I try to unmount /dev/SDA it can’t unmount because it’s an invalid argument? I’m a total beginner at Linux and I really don’t know what to do now Reply
Capitalization and spelling matter on the shell. Device names are never capitalized (so sda, not SDA). unmount should be umount, too. Reply
I updated to the newest version of Ubuntu, now it will not boot. I get the BusyBox black screen and when I used the exit command, here’s the output: /init: line 866: logsave: not found
Failure: File system check of the root filesystem failed
The root filesystem in /dev/sda6 requires a manual fsck But when I attempt to do so, I get a text file busy error. How can I fix this? Reply
Good morning, happy new year to you. Thank you for the article and your time spent on this website your time spent doing this is very much appreciated by me. I am using a laptop and ran a disk check using the smart control application which stated errors were found. Although I deleted the partition and reformatted the drive the errors remained which made me curious about error checking and cyclic redundancy checks which lead me to do a little research on physical drive checks and more detailed analysis. Doing searching online drove me here and to the FSCK command which even though I had wiped the drive still showed a wealth of errors (As I ran it without the -y flag initially), your article provided some clarity I was missing and made me wonder if some of these errors are reached because there is still junk data left on the drive because I have not low-level formatted it. So now I am running the command as ‘sudo fsck -y -c /dev/sdb’ Now it will search for errors, fix them as found, and display ongoing progress… Which begs the question… If I low-level formatted this drive by overwriting it with zero, would these errors then be wiped free? And if that is true, then the error that may still be found would be from smart control querying the device bios to see if it’s detected possible hardware failure (As the drive would now be wiped completely clean of any software issues) which means that fsck is really only untangling crosslinked files/sectors/clusters, etc. Forgive the stupid questions, I am not well educated on computer drives from a hardware perspective, or a read/write/data perspective so I am aware I’m ignorant and most articles I’ve read only give so much in-depth information. In any case, I am going to let this command run in its entirety and see what the outcome is. Thank you for your time. Thank you for the article. Reply
Does Linux (ubuntu) install run fsck before installing? or format automatically “note” the problematic sectors on the new partitions? I try to install Ubuntu and always ran into disc problems How to force a correction before the install? Reply