- Linux force fsck on boot
- Contents
- Boot time checking
- Mechanism
- Forcing the check
- Tips and tricks
- Attempt to repair damaged blocks
- Repair damaged blocks interactively
- Changing the check frequency
- fstab options
- Troubleshooting
- Can’t run fsck on a separate /usr partition
- ext2fs: no external journal
- How to Use ‘fsck’ to Repair File System Errors in Linux
- When to Use fsck in Linux
- fsck Available options
- How to Run fsck to Repair Linux File System Errors
- Understanding fsck exit codes
- Repair Linux Filesystem Errors
- How to Run fsck on Linux Root Partition
- Force fsck Upon System Boot
- Run fsck in Rescue Mode
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- Linux Force fsck on the Next Reboot or Boot Sequence
- How does fsck force work at every boot on a Linux?
- Check the /etc/init/mountall.conf file, for example the file from Ubuntu 14.04 LTS
- Check the /etc/rc.sysinit file, for example the file from RHEL/CentOS Linux 6.x
- Force fsck on boot using /forcefsck
- Frce fsck on next boot using shutdown command (may not work on many modern distros)
- A note about systemd based systems
Linux force fsck on boot
fsck stands for «file system check» and it is used to check and optionally repair one or more Linux file systems. Normally, the fsck program will try to handle file systems on different physical disk drives in parallel to reduce the total amount of time needed to check all of the file systems (see fsck(8) ).
The Arch Linux boot process conveniently takes care of the fsck procedure for you and will check all relevant partitions on your drive(s) automatically on every boot. Hence, there is usually no need to resort to the command-line.
Contents
Boot time checking
Mechanism
There are two players involved:
- mkinitcpio offers you the option to fsck your root file system before mounting it via the fsck hook. If you do this, you should mount root read-write via the appropriate rw kernel parameter.[1]
- systemd will fsck all file systems having a fsck pass number greater than 0 (either with #fstab options or a user-supplied unit file). For the root file system, it also has to be mounted read-only initially with the kernel parameter ro and only then remounted read-write from fstab (note that the defaults mount option implies rw ).
The first option is the recommended default, and what you will end up with if you follow the Installation guide. If you want to go with option 2 instead, you should remove the fsck hook from mkinitcpio.conf and use ro on the kernel command-line. The kernel parameter fsck.mode=skip can be used to make sure fsck is disabled entirely for both options.
Forcing the check
If you use the base mkinitcpio hook, you can force fsck at boot time by passing fsck.mode=force as a kernel parameter. This will check every file system you have on the machine.
Alternatively, systemd provides systemd-fsck@.service(8) , which checks all configured file systems, which were not checked in the initramfs. However, checking the root file system this way causes a delay in the boot process, because the file system has to be remounted.
This article or section needs expansion.
Tips and tricks
Attempt to repair damaged blocks
To automatically repair damaged portions of an ext2/ext3/ext4 or FAT file system, run:
Repair damaged blocks interactively
This is useful for when files on the boot partition have changed, and the journal failed to properly update. In this case, unmount the boot partition, and run the following code to repair damaged portions:
Changing the check frequency
By default, fsck checks a file system every 30 boots (counted individually for each partition). To change the frequency of checking, run:
In this example, 20 is the number of boots between two checks.
Note that 1 would make it scan at every boot, while 0 would stop scanning altogether.
If you wish to see the frequency number and the current mount count for a specific partition, use:
fstab options
fstab is a system configuration file and is used to tell the Linux kernel which partitions (file systems) to mount and where on the file system tree.
A typical /etc/fstab entry may look like this:
The 6th column (in bold) is the fsck option.
- 0 — do not check.
- 1 — first file system (partition) to check; / (root partition) should be set to 1 .
- 2 — all other file systems to be checked.
Troubleshooting
Can’t run fsck on a separate /usr partition
- Make sure you have the required hooks in /etc/mkinitcpio.conf and that you remembered to re-generate your initramfs image after editing this file.
- Check your fstab! Only the root partition needs 1 at the end, everything else should have either 2 or 0 . Carefully inspect it for other typos, as well.
ext2fs: no external journal
There are times (due to power failure) in which an ext(3/4) file system can corrupt beyond normal repair. Normally, there will be a prompt from fsck indicating that it cannot find an external journal. In this case, run the following commands:
Unmount the partition based on its directory
Источник
How to Use ‘fsck’ to Repair File System Errors in Linux
Filesystems are responsible for organizing how data is stored and recovered. One way or another, with time, filesystem may become corrupted and certain parts of it may not be accessible. If your filesystem develops such inconsistency it is recommend to verify its integrity.
This can be completed via system utility called fsck (file system consistency check). This check can be done automatically during boot time or ran manually.
In this article, we are going to review the fsck utility and its usage to help you repair disk errors.
When to Use fsck in Linux
There are different scenarios when you will want to run fsck. Here are few examples:
- The system fails to boot.
- Files on the system become corrupt (often you may see input/output error).
- Attached drive (including flash drives/SD cards) is not working as expected.
fsck Available options
Fsck command needs to be run with superuser privileges or root. You can use it with different arguments. Their usage depend 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 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 filesystem types to be checked. Types can be comma separated list.
- -V – Provide description what is being done.
How to Run fsck 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 ran with.
Run fsck on Linux Partition
Understanding fsck exit codes
After running fsck, it will return an exit code. These cods can be seen in fsck’s manual by running:
Repair Linux Filesystem Errors
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 ran 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:
- Force fsck upon system boot
- Run fsck in rescue mode
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 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 the “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 to 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 filesystem. If you have any questions about fsck, please do not hesitate to submit them in the comment section below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
Linux Force fsck on the Next Reboot or Boot Sequence
I want to force fsck on the next reboot? How do I force fsck on a Linux based server or laptop? Is there any configuration file that I need to alter for this purpose on a Linux?
fsck command is used to check and optionally repair one or more Linux file systems. You don’t have to create or alter/modify any file. All you need to do is just create empty file called /forcefsck .[donotprint]
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | None |
Est. reading time | 1m |
[/donotprint]
How does fsck force work at every boot on a Linux?
On a Debian or Ubuntu Linux /etc/init.d/checkfs.sh (or /etc/init/mountall.conf on a Ubuntu LTS v14.04+) is used to run fsck command if /forcefsck .
Check the /etc/init/mountall.conf file, for example the file from Ubuntu 14.04 LTS
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
On a RHEL/CentOS 6.x /etc/rc.sysinit file is used to run fsck command. If there is a /fsckoptions file, the options are loaded from the /fsckoptions file. If there is a /forcefsck file, -f is added.
Check the /etc/rc.sysinit file, for example the file from RHEL/CentOS Linux 6.x
Fig.01: RHEL/CentOS 6.x /etc/rc.sysinit file,
Force fsck on boot using /forcefsck
By creating /forcefsck file you will force the Linux system (or rc scripts) to perform a full file system check. First, login as the root user:
$ su —
OR
$ sudo -s
Change directory to root (/) directory [optional]:
# cd /
Create a file called forcefsck:
# touch /forcefsck
Now, reboot the system:
# reboot
Frce fsck on next boot using shutdown command (may not work on many modern distros)
The -F option force fsck on reboot, login as root and type the following command to reboot and run fsck:
# shutdown -rF now
The above will check all the relevant partitions immediately.
A note about systemd based systems
The above mentioned solution only works with the old SysVinit and early versions of Upstart. It won’t work with systemd. systemd-fsck understands one kernel command line parameter:
fsck.mode=
One of “auto”, “force”, “skip”. Controls the mode of operation. The default is “auto”, and ensures that file system checks are done when the file system checker deems them necessary. “force” unconditionally results in full file system checks. “skip” skips any file system checks.
fsck.repair=
One of “preen”, “yes”, “no”. Controls the mode of operation. The default is ” preen”, and will automatically repair problems that can be safely fixed. “yes ” will answer yes to all questions by fsck and “no” will answer no to all questions.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник