Linux force fsck on boot

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.

Run fsck on Mounted Partition

To avoid this unmount the partition using.

Then fsck can be safely run with.

Run fsck on Linux Partition

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:

Читайте также:  Linux цветной вывод консоль

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”.

Grub Advance Options

Then choose “Recovery mode”.

Select Linux Recovery Mode

In the next menu select “fsck”.

Select fsck Utility

You will be asked if you wish to have your / filesystem remounted. Select “yes” .

Confirm Root Filesystem

You should see something similar to this.

Running fsck Filesystem Check

You can then resume normal boot, by selecting “Resume”.

Select Normal Boot

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.

Parted Command in Linux

TLDR Man Pages for Linux Commands

apt-get Command Examples

Ubuntu apt-cache Commands

apt Command Examples

Funny Linux Commands

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

Читайте также:  Как проверить скорость канала linux

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

Источник

How to force fsck on reboot for Ubuntu 20.04

I am running Ubuntu 20.04 on an embedded device. I will like to ask how to force automatic fsck on bootup? In older Ubuntu versions, this can be achieved by editing the /etc/default/rcS and setting FSCKFIX=yes. However, I read that in newer Ubuntu, this file is not used anymore. So what will be the way to enable automatic fsck on bootup for newer Ubuntu?

@guiverc question is about how to initiate a fsck during boot, like we could in earlier days using sudo touch /forcefsck . To my consternation, I see today: sudo tune2fs -l /dev/nvme0n1p2 | grep checked — Last checked: Sat Oct 31 13:03:57 2020 . Does that indicate that the system file system is not anymore automatically checked nowadays?

3 Answers 3

The old convenient trick of creating a file /forcefsck to force a file check on reboot does not work anymore since systemd took over initialisation.

Change «Maximum number of mounts» (only ext file systems)

The quickest way, probably, is to temporarily change your Maximum mount count to 1 . That will cause the kernel and e2fsck to check the file system on the next reboot. However, this only works with ext file systems.

Читайте также:  Драйверы устройств операционной системы linux

First check your current setting in case you want to restore to default later:

sudo tune2fs -l /dev/nvme0n1p2 | grep 'Maximum mount' 

Substitute /dev/nvme0n1p2 by the device name of your system partition. Chances are this is set to -1 nowadays, disabling check based on the number of times the volume has been mounted.

Adjust the setting to 1 with the command:

sudo tune2fs -c 1 /dev/nvme0n1p2 

Now reboot — the volume should be checked. After reboot, you should reset the value to what it was before in order to avoid the partition being checked everytime.

Change kernel parameter

Another way is to pass kernel parameters during boot that control the systemd services for file system check. fsck.mode=force will force a file check.

To add a kernel parameter for a single time, boot to the Grub menu, highlight the entry and hit e . Move to the line starting with linux , hit End to move to the last line, add a space and the kernel parameter. Hit Ctrl + x to close and continue booting.

fsck.mode=force is the best solution, and the most similar to the /forcefsck old behavior. The best would be to create a new dedicated GRUB entry for that IMHO. (+1)

@Freedo I am not saying that as far as I see. I am providing two options, one specifically for ext4 (there may be similar options for e.g. btrfs or not), and the grub way, which does not depend on the specific file system.

Might seem obvious to some but will using this solution will fsck auto repair errors or just wait for user to enter a ‘y’ in case of errors ? I would like to autofix to make sure I can still access it remotely if this happens.

-c 1 is «invalid» and modern tune2fs 1.47.0 complains about it. LOL. Just set it to 2 and reboot two times.

If you want to force fsck with each reboot, there are few steps you need to follow.

amarcus@amarcus-desktop:~$ blkid /dev/mapper/vgubuntu-swap_1: UUID="d24b0766-c9be-49ef-9022-8ccae4f79801" TYPE="swap" /dev/mapper/vgubuntu-root: UUID="d414c4f9-da0d-42bf-8290-4bcb55b8d984" BLOCK_SIZE="4096" TYPE="ext4" amarcus@amarcus-desktop:~$ 
amarcus@amarcus-desktop:~$ cat /etc/fstab # /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # #      /dev/mapper/vgubuntu-root / ext4 errors=remount-ro 0 1 # /boot/efi was on /dev/nvme0n1p1 during installation UUID=0AE0-795B /boot/efi vfat umask=0077 0 1 /dev/mapper/vgubuntu-swap_1 none swap sw 0 0 UUID=D41C-2F17 /mnt/WDElements auto umask=0022,gid=1000,uid=1000,x-gvfs-show 0 0 

The last column that is a column 6, aka fsck PASS column is used by fsck to determine whether fsck should check filesystem before it is mounted and in which order given partitions in /etc/fstab should be checked

For root partitions, make sure that entry is set to 1

root@amarcus-desktop:~# tune2fs -c 1 /dev/mapper/vgubuntu-root 

Basically, in step 1 you are identifying which partition you want to check at boot.

In step 2, you are making sure that it takes higher priory. It’s useful if you are checking more than one partitions. It decide which should be taken up first, then second and so on.

In step 3, you are saying after how many mounts the partition should be checked. The argument 1 specifies that after one mount the partition should be checked. So basically it checks after each mount, i.e. after every restart.

Источник

Оцените статью
Adblock
detector