How to Repair linux Filesystem – Fix file system errors using fsck command

How to Repair linux Filesystem – Fix file system errors using fsck command


 

What is Fsck ?

fsck stand for file system consistency check. Fsck is a tool used on linux servers to check and repair file system errors. Filesystem might gets corrupted due to power failure, hardware failure, unclean shutdown etc. You might see errors like “touch: cannot touch file: Read-only file system” if there is file system errors on your linux server.
Linux server administrators can manually run fsck command to check consistency errors and fix them. You must have server root access to run fsck on a linux server. Fsck command is same as ‘chkdsk’ command in windows servers.
 

How to run FSCK? (file system consistency check)


IMPORTANT : You must never run file system on a mounted file system because it not safe and might lead to data corruption. You must run fsck only in rescue mode or in single user mode.

You must unmount the drive before running fsck. You might see the below error if the drive is not umounted.

/dev/sda is mounted.
e2fsck: Cannot continue, aborting.

You must reboot the server to single user mode before running fsck. Running fsck on mounted file system might lead to issues.

1. Log into your linux server via SSH as ‘root’ user

2. Reboot the server in single user mode by running the below command

command : init 1

init 1 command will take you to runlevel 1 (Single user mode)

3. unmount the file system that is having issues

command to unmount : umount /var

The above command will unmount /var partition that is having file system errors.

4. Run fsck on this partion after unmounting

Command : fsck /dev/sda1

OR

fsck /var

If no file system are specified in the command line then fsck will start checking filesystems in /etc/fstab file.

Use “fdisk -l” command if you don’t know the partition number. This command will list the partition tables

[root@server ~]# fdisk -l

Disk /dev/xvda: 8589 MB, 8589934592 bytes, 16777216 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
 

How to auto repair linux file system


Use “-a” option to automatically repair the file system. This option will repair the file system without asking any questions.

fsck -a /dev/sda1
 

How to force a file system check


Use “-f” option to force check the file system even if the filesystem is marked clean.

fsck -f /dev/sda2

-y = -y means “yes” to all questions. Without ‘y’ option fsck might ask for your confirmation for every fix it does.

fsck -yf /dev/sda2

You can also use the option “-p” with fsck like given below

Command : fsck -p /var

-p means automatic repair mode and it will not ask any questions
 

How to check all filesystem


Use “-A” option will go through the /etc/fstab file and will try to check all file system in one run. When -A is used the fs_passno (6th field) in the /etc/fstab file will checked and the file system will be checked in that order.
file system with zero fs_passno value will not be checked. File system with lowest fs_passno value will be checked first.
 

Skip fsck on mounted file system


Use “-M” option to skip fsck check on mounted file system. Running fsck on mounted file system will lead to damage so use -M to skip check on all mounted partitions. “-A” option will return an exist code of 0 if the filesystem is mounted.
Your sfiles ystem might get corrupted if you run fsck on mounted file system so always use “-M” option.

fsck -M /tmp

When you use “-M” option you can use “-R” along with it to skip the root file system check.

fsck -AR
 

How to specify file system type in fsck command


Use “-t” option to specify the filesystem type in fsck. Example is given below :

fsck -t ext3 /dev/sda3