Linux check filesystem on boot

How To Force fsck (Filesystem Consistency Check) After Reboot

fsck is a tool for checking and repairing a filesystem on Linux, macOS, and FreeBSD, similar to the CHKDSK Windows tool.

With old sysvinit and Upstart, users could force a disk check on next reboot using a simple command ( sudo touch /forcefsck ), but that’s no longer the case nowadays, with most Linux distributions using systemd. This article explains how to force fsck to run at boot time in two ways, which work with systemd. You’ll also find a command that shows when a partition was last checked by fsck, at the end of the article.

Using the steps below you’ll force a filesystem consistency check each time your computer boots. There are also instructions for undoing this.

Option 1 (for ext2/ext3/ext4 filesystems only): force fsck on reboot using tune2fs

tune2fs can force a fsck on each reboot for EXT4, EXT3 and EXT2 filesystems only. Most Linux distributions use EXT3/4 by default, so unless you explicitly specified a different filesystem type, that’s what you’re using in most cases.

tune2fs can trigger a forced fsck on every reboot using the -c (max-mount-counts) option. This option sets the number of mounts after which the filesystem will be checked, so setting it to 1 will run fsck each time the computer boots. Setting it to -1 or 0 resets this (the number of times the filesystem is mounted will be disregarded by e2fsck and the kernel).

For this to work you’ll need to make sure the partition you want to check and repair using fsck has the pass number greater than 0 set in /etc/fstab (last column in /etc/fstab ). The root partition should be set to 1 (first to be checked), while other partitions you want to be checked should be set to 2 . Example:

# /etc/fstab: static file system information. /dev/sda1 / ext4 errors=remount-ro 0 1 /dev/sda5 /home ext4 defaults 0 2

After doing this, force a fsck on each boot for an EXT4, EXT3 or EXT2 filesystem using:

You’ll need to replace sdXY with the partition you want to check and repair. You can find this using a graphical application like Gparted, which displays all available partitions for each disk, or using a command line tool, like lsblk or fdisk ( sudo fdisk -l ).

Читайте также:  Write to file linux command line

Now you can reboot your system, and fsck should be forced to run a filesystem consistency check.

When you no longer want to force fsck to run on each boot, run the same command but with -1 instead of 1 :

You can also use this command to run fsck periodically. For example you could set fsck to run a filesystem check every 30 boots, by using -c 30 (e.g.: sudo tune2fs -c 30 /dev/sdXY ).

Option 2: force fsck on reboot using fsck.mode=force as a kernel parameter, and optionally fsck.repair=yes

This should work with many filesystem types, including ext4/ext3/ext2, xfs, ntfs, fat32, exfat, and more. It should also be noted that systemd-fsck does not know any details about specific filesystems, and simply executes file system checkers specific to each filesystem type ( /sbin/fsck.* ; e.g.: /sbin/fsck.ext4 ).

systemd runs fsck for each filesystem that has a fsck pass number greater than 0 set in /etc/fstab (last column in /etc/fstab ), so make sure you edit your /etc/fstab if that’s not the case. The root partition should be set to 1 (first to be checked), while other partitions you want to be checked should be set to 2 . Example:

# /etc/fstab: static file system information. /dev/sda1 / ext4 errors=remount-ro 0 1 /dev/sda5 /home ext4 defaults 0 2

All partitions that appear in /etc/fstab and have the pass (last column in fstab) number greater than 0 will be checked if you boot with the fsck.mode=force kernel parameter.

Now that we got that out of the way, this how how to force a fsck on boot with systemd. You need to add fsck.mode=force as a kernel parameter to your grub configuration file.

This command uses Nano command line text editor to open /etc/default/grub so you can edit it:

To force a fsck each time the computer boots, you’ll need to add fsck.mode=force to GRUB_CMDLINE_LINUX_DEFAULT, at the end of the line but before the last quote ( » ).

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash fsck.mode=force"

Make sure you don’t edit anything else, and that the edits you’ve made are correct, or else your computer may fail to boot!

After you’re done making the changes, save the file and exit (to save the file and exit Nano use: Ctrl + O , Enter , then Ctrl + X ).

There is a second kernel parameter that you can add, called fsck.repair . This has the default option preen , which automatically repairs problems that can safely be fixed. You can use fsck.repair=yes to answer «yes» to all questions by fsck, or «no» to answer no to all questions.

Читайте также:  Epson v30 linux driver

Example (force fsck on each reboot and answer «yes» to all fsck questions):

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash fsck.mode=force fsck.repair=yes"

Add this to /etc/default/grub in the same way as explain before.

After you’ve finished editing /etc/default/grub , update your Grub2 configuration:

sudo grub2-mkconfig -o /boot/grub2/grub.cfg
sudo grub-mkconfig -o /boot/grub/grub.cfg

On other Linux distributions you can try the commands mentioned above. If they don’t work, search for how to update the Grub2 configuration for your Linux distribution.

When you’re done, reboot the system and fsck should run a filesystem consistency check on boot.

To stop force-checking your filesystem on each boot, remove fsck.mode=force (and fsck.repair=yes if you added it) from your /etc/default/grub configuration file, and update grub, as already explained.

How to find out when a filesystem was last checked by fsck

You can find out when a filesystem was last checked by fsck using tune2fs:

sudo tune2fs -l /dev/sdXY | grep checked

Replace sdXY with the partition you want to see when it was last checked. Example:

$ sudo tune2fs -l /dev/sda1 | grep checked Last checked: Thu May 23 15:16:23 2019

Источник

How to Check/Repair Linux File System on Boot

An operating system user that is still a learner in the OS world can never escape the challenges the current elite users have overcome. The user impact of such challenges depends on the operating system flavor or distribution you are under.

For Linux users, you might think you are facing an impossible challenge only to realize that there are multiple valid solutions for every single OS problem faced.

The Linux file system is a maze that most users hesitate to enter. It is because this file system is responsible for the performance consistency of your Linux operating system and any slight modification to its hierarchy or file system structure can be a nightmare for any user.

The FSCK Linux Command

The Linux operating system handles file system issues through the fsck command-line utility. It is a prepackaged file system check toolkit that interactively checks and repairs corrupted files that might affect the performance of your operating system.

Most of the time, it is these corrupt files that are responsible for failed system boots and partition mounts.

FSCK Command Usage

The standard syntax for fsck command-line utility is as follows:

Читайте также:  Чем создать linux swap

You need to be a sudoer user or have sudo privileges to effectively use the FSCK tool.

FSCK adheres to some predefined rules.

Linux fstab File

  • It references the /etc/fstab file when a user does not provide [FILESYSTEM] as a command argument.

The column for the (/) and (ext4) is set to 1 because its file system checks and repair are prioritized.

It is not recommended to use the fsck command with mounted partitions as there is a high likelihood of permanent file system damage.

Repairing A Corrupted Linux File System

The first step is to identify the device partition that holds the Linux file system you would like to check and repair.

List Linux File System Partitions

The second step is to unmount the device whose file system you want to check and repair. e.g.

The third step is to check and possibly repair the device’s file system using FSCK.

The -p command option is for automatic repair without any user intervention.

The final step is to re-mount your device once the fsck file system check and repair is complete.

Repairing Linux File System on Boot-Time

On a running machine, it is not possible to simultaneously check and repair your Linux file system while using the OS. In this case, we have the options of using a live CD/bootable usb drive or checking and repairing the Linux file system on boot (For installed Linux systems).

Linux File System Repair on Boot

1. Restart your machine, press [Esc] or [Shift] and select Advanced Options on the resulting boot menu.

Ubuntu Advanced Options

2. Select Recovery Mode and choose the fsck option from the resulting list of menu options.

Ubuntu Recovery Mode

3. Hit [Enter] and choose the fsck menu option.

Ubuntu Fsck Option

4. Choose Yes when asked whether to remount your OS root file system.

Mount Ubuntu Filesystem

5. Once the file system check and repair is complete, direct your system to resume normal boot.

Resume Ubuntu Normal Boot

Live CD/USB File System Repair

  1. Boot into your machine from the live distribution (CD or USB).
  2. Open the Linux terminal and use sudo fdisk -l command to find the targeted Linux root partition name.
  3. If the root partition is identified by a name like /dev/sda1, running the following command will check and possibly repair its associated file system.

FSCK is a very effective tool to get you started on understanding the nature and performance of your Linux system. The man fsck command has more options on using the FSCK utility to check and optionally repair broken or corrupt Linux file systems.

Источник

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