How to Repair Linux Disk Errors

Share on Social Media

Learn how to repair Linux disk errors with this comprehensive guide. Follow our step-by-step instructions to diagnose and fix disk issues on your Linux system, ensuring optimal performance and data integrity. #centlinux #linux #diskrepair

List Linux Disk Partitions and Types

First of all you need to identify the disk partitions in your Linux server, their respective file systems and the path where they are being mounted.

By using a console or a ssh client, connect with your Linux server as root user.

You can execute the lsblk command with following switches at the Linux bash prompt to list the required information.

# lsblk -o NAME,FSTYPE,MOUNTPOINT
NAME        FSTYPE      MOUNTPOINT
sda
├─sda1      ext4        /boot
└─sda2      LVM2_member
  ├─cl-root xfs         /
  ├─cl-swap swap        [SWAP]
  └─cl-home xfs         /home
sr0

What is fsck Command?

The fsck (File System Consistency Check) command is a system utility in Unix-like operating systems, including Linux, used to check and repair file system inconsistencies. It is an essential tool for maintaining the integrity and health of file systems, ensuring that data is stored and retrieved correctly.

Purpose of fsck

  • Check File System Integrity: Scans the file system for inconsistencies and errors.
  • Repair File Systems: Fixes detected errors to ensure the file system operates correctly.
  • Prevent Data Loss: Helps prevent data corruption by maintaining file system integrity.

When to Use fsck

  • System Crashes: After an unexpected shutdown or crash.
  • Corruption Suspected: If you suspect file system corruption or encounter read/write errors.
  • Routine Maintenance: Regularly checking file systems for potential issues.

Basic Syntax

fsck [options] [file system]
  • [options]: Various flags to control the behavior of fsck.
  • [file system]: The file system device to be checked (e.g., /dev/sda1).

Common Options

  • -a or -y: Automatically repair the file system without prompting for confirmation.
  • -n: Do not make any changes; only check for errors.
  • -r: Interactively repair the file system, asking for confirmation before fixing each error.
  • -C: Show progress information during the check.
  • -t: Specify the type of file system to be checked (e.g., ext4, xfs).

Important Considerations

  • Unmount the File System: Always unmount the file system before running fsck to prevent data corruption. Use umount /dev/sda1 to unmount.
  • Single-User Mode: For checking the root file system, boot into single-user mode or use a live CD/USB to ensure the file system is not in use.
  • Backup Important Data: Before running fsck, back up critical data as a precaution.

Summary

The fsck command is a vital utility for maintaining file system integrity on Linux systems. It checks and repairs file systems, ensuring data consistency and preventing corruption. Proper use of fsck, including unmounting file systems and understanding its options, is essential for system administrators and users to keep their systems running smoothly.

If you are not used to Linux command line then we recommend that you should attend online training: Learn Bash Shell in Linux for Beginners

745772 0021

Repair Linux Disk Partitions: Ext4 Type

Get Last Scanned Time of a Linux Disk

You can find the last scan time for Linux Ext4 type partitions with the help of following command.

# tune2fs -l /dev/sda1 | grep checked
Last checked:             Sun Sep 29 20:03:14 2019

Scan & Repair Linux Disk Errors

To scan and repair disk in Linux, you can use fsck (File System Consistency Check) command. But you are required to unmount that partition before checking and repairing it.

# umount /dev/sda1

After successful unmount, execute fsck command at Linux bash prompt.

# fsck.ext4 /dev/sda1
e2fsck 1.45.6 (20-Mar-2020)
/dev/sda1: clean, 320/65536 files, 61787/262144 blocks

After checking and repairing your Linux disk, mount the partition again at its respective mountpoint.

For this purpose, execute following Linux command to mount all the disk partitions listed in /etc/fstab file.

# mount -a

Enable Scanning at Linux Startup

To enable disk checking at the time of Linux startup. You have to modify the Mount Count parameter for that disk partition.

# tune2fs -c 1 /dev/sda1
tune2fs 1.45.6 (20-Mar-2020)
Setting maximal mount count to 1

Reboot your Linux server now.

# reboot

Linux command fsck is now check your Ext4 disk partition on startup.

After reboot, get the Last Checked value for your disk partition, now it will show you the time of last Linux startup.

# tune2fs -l /dev/sda1 | grep checked
Last checked:             Sun Aug  1 22:50:46 2021

Set back the Mount Count parameter, or it will keep performing disk scans on each Linux boot.

# tune2fs -c -1 /dev/sda1
tune2fs 1.45.6 (20-Mar-2020)
Setting maximal mount count to -1

What is xfs_repair?

xfs_repair is a specialized utility in Linux for checking and repairing XFS file systems. XFS is a high-performance file system designed for large-scale storage systems, commonly used in enterprise environments. The xfs_repair tool is part of the XFS file system utilities and is used to fix corruptions and inconsistencies in XFS file systems.

Purpose of xfs_repair

  • Check XFS File System Integrity: Scans the XFS file system for inconsistencies and errors.
  • Repair XFS File Systems: Fixes detected issues to ensure the file system operates correctly and data integrity is maintained.

When to Use xfs_repair

  • After a Crash: Following an unexpected shutdown or system crash.
  • Suspected Corruption: If there are signs of file system corruption, such as read/write errors or unexpected behavior.
  • Regular Maintenance: Periodically checking the file system to prevent potential issues.

Basic Syntax

xfs_repair [options] device
  • device: The XFS file system device to be checked (e.g., /dev/sda1).

Common Options

  • -n: No-modify mode. Only checks the file system without making any changes.
  • -L: Force log zeroing. This option can be used to reset the log if it’s corrupted.
  • -P: Disable prefetching. Useful in environments with limited memory.

Important Considerations

  • Unmount the File System: Always unmount the XFS file system before running xfs_repair to prevent data corruption. Use umount /dev/sda1 to unmount.
  • Back Up Important Data: Before using xfs_repair, back up critical data as a precaution.
  • Log Zeroing: Use the -L option with caution, as it may result in data loss if the log contains important transactions.

Summary

xfs_repair is an essential tool for maintaining the integrity of XFS file systems. It scans for and repairs inconsistencies, ensuring the file system remains functional and data is intact. Proper use of xfs_repair, including unmounting the file system and understanding its options, is crucial for system administrators managing XFS file systems.

Repair Linux Disk Partitions: XFS Type

Scan & Repair Linux Disk Errors

XFS type disk partitions have their own set of commands, that are a little bit different from Ext4.

You must unmount a XFS disk partition before checking it for consistency.

# umount /dev/mapper/cl-home

We have xfs_repair command for checking and repairing the disk errors.

In some Linux distros, you may also find xfs_check command. This command only perform scanning of XFS type disk partitions and do not perform any repair.

But xfs_check command is not available in all Linux distros.

Alternatively, you can use xfs_repair command with -n switch to get the same functionality as of xfs_check.

# xfs_repair -n /dev/mapper/cl-home
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.

The above command only perform disk checking and do not try to repair any error.

Now, execute the xfs_repair command without -n switch and it will scan and repair disk in Linux.

# xfs_repair /dev/mapper/cl-home
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

Remount the XFS partition at its original mountpoint as listed in /etc/fstab file.

# mount -a

Enable Scanning at Linux Startup

In some scenarios you cannot unmount a disk partition, if the disk is in use by the Linux operating system. For this reason you may have to defer the disk checking until next system boot.

To enable xfs_repair command to run on Linux startup, add “fsck.mode=force fsck.repair=yes” at the end of GRUB menu kernel command.

You can refer to our previous post about Editing GRUB menu.

After Linux startup, check the system log to verify the execution of disk repair command.

# journalctl | grep  systemd-fsck

To permanently enable disk checking at startup, you have to add “fsck.mode=force fsck.repair=yes” in GRUB configuration files.

Edit grub configuration file in vim text editor.

# vi /etc/default/grub

Locate GRUB_CMDLINE_LINUX parameter and append “fsck.mode=force fsck.repair=yes” at the end of line.

GRUB_CMDLINE_LINUX="resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet fsck.mode=force fsck.repair=yes"

Regenerate GRUB menu configurations based on new parameters.

# grub2-mkconfig

Reboot your Linux operating system to verify the new settings.

# reboot

If you feel any difficulty understanding this Linux tutorial, we suggest that you should read The Linux Command Line, 2nd Edition by William Shotts (PAID LINK).

Final Thoughts

You have successfully learned how to scan and repair Linux disk errors. Here, we have repaired Linux disk partitions of Ext4 and XFS types.

Are you facing issues with your Linux system or need professional assistance to ensure it’s running smoothly? Look no further! I offer expert Linux support and maintenance services tailored to meet your specific needs.

Leave a Comment