Operations 8 min read

How to Scan and Repair Linux Disk Errors with fsck and xfs_repair

This guide walks you through identifying Linux disk partitions, checking and fixing Ext4 and XFS file systems using fsck and xfs_repair, and configuring automatic checks at boot by adjusting mount counts and GRUB parameters, complete with command examples and output screenshots.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Scan and Repair Linux Disk Errors with fsck and xfs_repair

What is fsck? The fsck (file system consistency check) utility verifies the integrity of Linux file systems.

Listing Disk Partitions

First, determine the partitions, their file system types, and mount points using:

# lsblk -o NAME,FSTYPE,MOUNTPOINT

Getting the Last Check Time for Ext2/3/4

Use tune2fs -l to find the last check date:

# tune2fs -l /dev/nvme0n1p1 | grep checked
Last checked: Wed Sep 1 15:42:11 2021

Scanning and Repairing an Ext4 Partition

Unmount the target partition before running the check: # umount /dev/nvme0n1p1 Run the file‑system‑specific check:

# fsck.ext4 /dev/nvme0n1p1
... /dev/nvme0n1p1: clean, 311/65536 files, 55546/262144 blocks

Remount all partitions defined in /etc/fstab:

# mount -a

Enabling Automatic Ext4 Checks at Boot

Set the maximal mount count to 1 so the file system is checked on every boot:

# tune2fs -c 1 /dev/nvme0n1p1
Setting maximal mount count to 1

Reboot the system; the check will run automatically:

# reboot

What is xfs_repair?

The xfs_repair utility is designed for large XFS file systems and can run without unmounting if the file system was cleanly unmounted; otherwise, unmount first.

Scanning and Repairing an XFS Partition

Unmount the XFS partition: # umount /dev/sda1 Perform a dry‑run check (no modifications):

# xfs_repair -n /dev/sda1
Phase 1 - find and verify superblock...
Phase 2 - using internal log ...
... (output truncated)

Run the full repair (without -n) to fix errors:

# xfs_repair /dev/sda1
Phase 1 - find and verify superblock...
Phase 7 - verify and correct link counts...
Done

Mount the repaired partition:

# mount /dev/sda1 /data/

Enabling Automatic XFS Checks at Boot

Add the kernel parameters fsck.mode=force fsck.repair=yes to GRUB_CMDLINE_LINUX in /etc/default/grub:

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"

Regenerate the GRUB configuration and reboot:

# grub2-mkconfig
# reboot

Conclusion

Using the steps above, you can successfully scan and repair both Ext4 and XFS file systems on Linux, and configure the system to perform automatic checks at each boot.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxfsckdisk repairxfs_repair
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.