How to scan and repair Linux disk errors

Time:2022-5-13

In this article, you will learn how to use fsck and XFS_ The repair command fixes Linux disk errors.
What is fsck?
Fsck (file system consistency check) is a tool used to check the file system consistency of Linux operating system.
How to scan and repair Linux disk errors
List Linux disk partitions and types:
First, you need to determine the disk partitions of the Linux system, their respective file systems, and mount points.

The following uses the lsblk command to list the disk description, file system type, and mount point:

[[email protected] ~]# lsblk -o NAME,FSTYPE,MOUNTPOINT
NAME FSTYPE MOUNTPOINT
sda
sdb
sdc
sdd
sr0 iso9660
nvme0n1
├─nvme0n1p1 ext4 /boot
└─nvme0n1p2 LVM2_member
├─cl-root xfs /
└─cl-swap swap [SWAP]
How to scan and repair Linux disk errors how to scan and repair Linux disk errors

Gets the last scan time of the Linux disk
You can use the following command to find the last scan time of ext2, ext3 and ext4 partitions.

[[email protected] ~]# tune2fs -l /dev/nvme0n1p1 |grep checked
Last checked: Wed Sep 1 15:42:11 2021
How to scan and repair Linux disk errors how to scan and repair Linux disk errors

Scan and repair ext4 type disk partitions
To scan Linux disk partitions, you can use the fsck (file system consistency check) command. However, you need to unmount the partition before checking and repairing it. In the following example, we uninstall the / boot partition.

[[email protected] ~]# umount /dev/nvme0n1p1
After the uninstall is successful, use fsck Ext4 check / dev / nvme0n1p1 partition

[[email protected] ~]# fsck.ext4 /dev/nvme0n1p1
e2fsck 1.44.3 (10-July-2018)
/dev/nvme0n1p1: clean, 311/65536 files, 55546/262144 blocks
How to scan and repair Linux disk errors how to scan and repair Linux disk errors
After checking and repairing your Linux disk, mount the partition again at its respective mount point. Execute the following command to mount all the disk partitions listed in the / etc / fstab configuration file.

[[email protected] ~]# mount -a
How to scan and repair Linux disk errors how to scan and repair Linux disk errors

Enable ext4 disk partition scanning at Linux startup
Enable disk checking at system startup. You must modify the mount count parameter of this partition.

[[email protected] ~]# tune2fs -c 1 /dev/nvme0n1p1
tune2fs 1.44.3 (10-July-2018)
Setting maximal mount count to 1
How to scan and repair Linux disk errors how to scan and repair Linux disk errors
After restarting the system, the fsck command will now check your ext4 disk partition at startup.

[[email protected] ~]# reboot
After the reboot, get the last checked value of the disk partition, and now it will display the time when Linux was last started.

[[email protected] ~]# tune2fs -l /dev/nvme0n1p1 |grep checked
Last checked: Tue Sep 14 11:08:11 2021
How to scan and repair Linux disk errors how to scan and repair Linux disk errors
Restore the default value of the mount count parameter, otherwise it will continue to perform disk scanning every time the system starts.

[[email protected] ~]# tune2fs -c -1 /dev/nvme0n1p1
tune2fs 1.44.3 (10-July-2018)
Setting maximal mount count to -1
How to scan and repair Linux disk errors how to scan and repair Linux disk errors

What is XFS_ REPAIR?
xfs_ The repair utility is highly scalable and is designed to effectively repair very large file systems with many inodes. Unlike other Linux file systems, XFS_ Repair will not run at startup, even if the XFS file system is not completely unmounted. If an unclean uninstall occurs, XFS_ Repair will only replay the logs when mounting to ensure the consistency of the file system.

Scan and repair XFS type disk partitions
XFS type disk partition has its own set of commands, which is a little different from ext4. XFS disk partitions must be unmounted before checking their consistency.

[[email protected] ~]# umount /dev/sda1
We have XFS_ The repair command is used to check and repair disk errors. Using XFS_ Use the – N option of the repair command to check for disk errors.

[[email protected] ~]# xfs_repair -n /dev/sda1
Phase 1 – find and verify superblock…
Phase 2 – using internal log

    - zero log...
    - scan filesystem freespace and inode maps...
    - found root inode chunk

Phase 3 – for each AG…

    - scan (but don't clear) agi unlinked lists...
    - process known inodes and perform inode discovery...
    - agno = 0
    - agno = 1
    - agno = 2
    - agno = 3
    - process newly discovered inodes...

Phase 4 – check for duplicate blocks…

    - setting up duplicate extent list...
    - check for inodes claiming duplicate blocks...
    - agno = 0
    - agno = 1
    - agno = 2
    - agno = 3

No modify flag set, skipping phase 5
Phase 6 – check inode connectivity…

    - traversing filesystem ...
    - traversal finished ...
    - moving disconnected inodes to lost+found ...

Phase 7 – verify link counts…
No modify flag set, skipping filesystem flush and exiting.
How to scan and repair Linux disk errors how to scan and repair Linux disk errors
The above command only performs disk check and does not attempt to fix any errors. Now, execute XFS without the – N option_ Repair command, which will scan and repair Linux disk partitions.

[[email protected] ~]# xfs_repair /dev/sda1
Phase 1 – find and verify superblock…
Phase 2 – using internal log

    - zero log...
    - scan filesystem freespace and inode maps...
    - found root inode chunk

Phase 3 – for each AG…

    - scan and clear agi unlinked lists...
    - process known inodes and perform inode discovery...
    - agno = 0
    - agno = 1
    - agno = 2
    - agno = 3
    - process newly discovered inodes...

Phase 4 – check for duplicate blocks…

    - setting up duplicate extent list...
    - check for inodes claiming duplicate blocks...
    - agno = 0
    - agno = 1
    - agno = 2
    - agno = 3

Phase 5 – rebuild AG headers and trees…

    - reset superblock...

Phase 6 – check inode connectivity…

    - resetting contents of realtime bitmap and summary inodes
    - traversing filesystem ...
    - traversal finished ...
    - moving disconnected inodes to lost+found ...

Phase 7 – verify and correct link counts…
done
How to scan and repair Linux disk errors how to scan and repair Linux disk errors
Next, mount the partition

[[email protected] ~]# mount /dev/sda1 /data/
Enable XFS disk partition scanning at Linux startup
In some cases, you cannot unmount a disk partition if the Linux operating system is using it. For this reason, you may have to postpone the disk check until the next system boot.

To make XFS_ The repair command can be run when Linux starts. Please add “fsck. Mode = force fsck. Repair = yes” at the end of the grub menu kernel command.

Check the system log to verify the execution of the disk repair command.

[[email protected] ~]# journalctl |grep systemd-fsck
To permanently enable disk checking at startup, you must add “fsck. Mode = force fsck. Repair = yes” to the grub configuration file.

Next, edit the grub configuration file and find grub_ CMDLINE_ Linux parameter and append fsck. At the end of the line mode=force fsck. repair=yes。

[[email protected] ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap fsck.mode=force fsck.repair=yes”
GRUB_DISABLE_RECOVERY=”true”
GRUB_ENABLE_BLSCFG=true
How to scan and repair Linux disk errors how to scan and repair Linux disk errors
Regenerate grub menu configuration according to the new parameters.

[[email protected] ~]# grub2-mkconfig
How to scan and repair Linux disk errors how to scan and repair Linux disk errors
Then restart the system:

reboot

summary
XFS and ext4 file systems have been successfully scanned and repaired so far.

Recommended Today

[Android advanced] flutter radar scanning effect and flutter rotary scanning

design sketch:1 . Test demo startup file main() { runApp(MaterialApp( home: SignSwiperPage(), )); } class SignSwiperPage extends StatefulWidget { @override _SignSwiperPageState createState() => _SignSwiperPageState(); } class _SignSwiperPageState extends State<SignSwiperPage> with SingleTickerProviderStateMixin { } The next code is_ Written in signswiperpagestate 2 . Animation controllers are used to achieve rotation //Animation controller AnimationController _animationController; @override void […]