How To Repair ext4 and XFS File systems on Linux

What is Linux Filesystem?. In Linux, there is only one major directory called root directory (/). All others are a subdirectories of the root directory. This is what is referred to as a filesytem in Linux, where all physical hard drives and partitions are unified into a single directory structure. Filesystem handles positioning of data on a storage, which without it the system would not know where files begin and end. When a new physical hard drive is added it has to be mounted as part of the root directory.

Types of Filesystems in Linux

Linux filesystems have been evolving with new developments as discussed below:

  • Ext: An old filesystem and is no longer supported.
  • Ext2: The file Linux filesystem that allowed up to two terabytes of data.
  • Ext3: Devised from ext2 with upgrades and backward compatibility.
  • Ext4: Faster and supports larger files. It is the default filesystem for current Linux systems.
  • XFS: Highly scalable and high performance filesystem. Is the default file system for Red Hat Enterprise Linux.
  • Btrfs: From Oracle and not as stable as Ext in some distributions but offers excellent performance.

Common Linux Subdirectories

As explained earlier, file directories are all part of the root directory (/). Filesystem determines how the files are placed in the storage. Below are some of the common Linux subdirectories with what they do:

  • /bin: Contains Linux core commands such as ls, mv.
  • /boot: Contains boot loader and boot files.
  • /dev: Where all physical drives are mounted like USBs DVDs.
  • /etc: Contains configurations for the installed packages.
  • /home: All user directories are created in this directory with a name like /home/likegeeks.
  • /lib: Where the libraries of the installed packages are located.
  • /media: Where external devices like DVDs are mounted, and you can access their files from here.
  • /mnt: Where you mount other things Network locations and some distros, you may find your mounted USB or DVD.
  • /opt: Some optional packages are located here and managed by the package manager.
  • /proc: Holds every detail about the filesystem.
  • /root: Root user home folder
  • /sbin: Like /bin, but binaries here are for root user only.
  • /tmp: Contains the temporary files.
  • /usr: Where the utilities and files shared between users and Linux.
  • /var: Stores system logs and other variable data.

How to Repair Ext Linux Filesystems

Filesystem takes care of how files are stored and restored. At some point you may notice that the filesystem may have been corrupted and some subdirectories become inaccessible. In such a case, it is necessary to check the filesystem integrity. This can be achieved using a utility called ‘fsck’ in Linux. Below are the scenarios that may require us to use fsck command:

  • System fails to boot
  • Files get corrupted
  • Attached drives not working as they should.

Using fsck to repair Linux Ext Filesystems

The command needs to be run as root as a user with root privileges. There are several options that the command can be used with as explained below:

  • -A: Checks all filesystems according what is contained in /etc/fstab.
  • -C: Shows progress bar
  • -l: Locks the device being checked so that no other program tries to use it while being checked.
  • -M: Avoids checking mounted filesystems
  • -N: Only show what would be done.
  • -P: For checking filesystems in parallel, root included
  • -R: To avoid checking root filesystem, only useful with ‘-A’
  • -r: Gives statistics for every device being checked
  • -T: No title
  • -t: Exclusively specify the filesystem types to be checked, use commas to separate lists
  • -V: Describes what is being done.

In order to run ‘fsck’ to repair a filesystem, you should ensure the target directory is not mounted. For example, I have a drive partition /dev/sdb1. To repair this drive with fsck, I would run the command:

sudo fsck /dev/sdb1

If mounted, unmount with the command:

sudo umount /dev/sdb1

In case where there are more than one errors, use -y option to try and automatically repair all.

sudo fsck -y /dev/sdb1

You can also pass several options depending on what are looking for. Example:

sudo fsck.ext4 -cDfty -C 0 /dev/sdb1

Where the flags are as explained below:

  • -c – check for bad sectors
  • -D – optimize directories if possible
  • -f – force check, even if filesystem seems clean
  • -t – print timing stats (use -tt for more)
  • -y – assume answer “yes” to all questions
  • -C 0 – print progress info to stdout

Repairing LVM filesystems with fsck

To check for lvm filesystem, we use lsblk command.

$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
loop0                       7:0    0   55M  1 loop  /snap/core18/1754
loop1                       7:1    0 69.4M  1 loop  /snap/lxd/18251
loop2                       7:2    0 69.4M  1 loop  /snap/lxd/18324
loop4                       7:4    0 27.1M  1 loop  /snap/snapd/7264
loop5                       7:5    0 55.4M  1 loop  /snap/core18/1932
loop6                       7:6    0   31M  1 loop  /snap/snapd/9721
sr0                        11:0    1 1024M  0 rom   
xvda                      202:0    0   20G  0 disk  
├─xvda1                   202:1    0    1M  0 part  
├─xvda2                   202:2    0    1G  0 part  /boot
└─xvda3                   202:3    0   19G  0 part  
  └─ubuntu--vg-ubuntu--lv 253:0    0   19G  0 lvm   /
xvdb                      202:16   0   20G  0 disk  
└─xvdb1                   202:17   0   20G  0 part  
  └─md127                   9:127  0   20G  0 raid1 
xvdc                      202:32   0   20G  0 disk  
└─xvdc1                   202:33   0   20G  0 part  
  └─md127                   9:127  0   20G  0 raid1 

From the above output, lvm is named ubuntu–vg-ubuntu–lv but we need to get the full name for us to be able to repair with fcsk. Run lvscan command to get the fill name.

$ sudo lvscan
 ACTIVE            '/dev/ubuntu-vg/ubuntu-lv' [<19.00 GiB]

The partition is active and we can go ahead to run fsck as below:

sudo fsck /dev/ubuntu-vg/ubuntu-lv

If the partition was inactive, you need to first make it active to be able to run fsck. To make it active, we use the command as below:

sudo lvchange -ay /dev/ubuntu-vg/ubuntu-lv

You can also run a forced check with assumes ‘yes’ as below

sudo fsck -fy /dev/ubuntu-vg/ubuntu-lv

How to repair xfs filesystems

XFS filesyst is the default file system for Red Hat Enterprise Linux. It supports up to 16TB file sizes. They are susceptible to damage and there are several ways to troubleshoot and restore the filesystem.

  • xfs_fsr: Since XFS is an extend-based filesystem, xfs_fsr recogninizes and improves the layout of the file extends to improve performance. You can run the command even if the filesystem is mounted.
  • xfs_repair: For repairing corrupted/ damaged file system. The filesystem has to be unmounted.
  • xfs_db: For debugging an XFS filesystem.

To repair a filesystem with xfs_repair, we use the syntax below:

sudo xfs_repair /mount/point

You can first check the filesystem problems without repairing as below:

sudo xfs_check /dev/xvdb1

Then you can repair:

sudo xfs_repair /dev/xvdb1

For LVM file system:

sudo xfs_repair /dev/ubuntu-vg/ubuntu-lv

In this guide we have looked at what Linux filesystem is and how to repair both ext and xfs file systems in case the files get corrupted. I hope the guide has been informative. Check more interesting Linux guides below:

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Unlock the Right Solutions with Confidence

At CloudSpinx, we don’t just offer services - we deliver clarity, direction, and results. Whether you're navigating cloud adoption, scaling infrastructure, or solving DevOps challenges, our seasoned experts help you make smart, strategic decisions with total confidence. Let us turn complexity into opportunity and bring your vision to life.

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

The Linux Kernel acts as the core of any Operating system by linking the hardware and software components and managing […]

The kernel of an operating system (OS) is the program that allows hardware and software to communicate with one another. […]

Monitoring is the process of ensuring that you keep track of the performance of your IT infrastructure. This includes servers, […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.