FSCK
====================================================================================
FSCK (File System Consistency Check)
------------------------------------------------------------------------------------------------------------------------------------------------
What does FSCK do?
fsck (file system consistency check) is a system utility used to check and repair filesystems. FSCK is made up of various tools that are made to handle different filesystem types, these are stored within /usr/sbin:
lrwxrwxrwx 1 root root 8 Mar 23 2022 dosfsck -> fsck.fat
-rwxr-xr-x 1 root root 360280 Jun 1 2022 e2fsck
-rwxr-xr-x 1 root root 43440 Apr 9 15:32 fsck
-rwxr-xr-x 1 root root 1185 Feb 24 2022 fsck.btrfs
-rwxr-xr-x 1 root root 31168 Apr 9 15:32 fsck.cramfs
lrwxrwxrwx 1 root root 6 Jun 1 2022 fsck.ext2 -> e2fsck
lrwxrwxrwx 1 root root 6 Jun 1 2022 fsck.ext3 -> e2fsck
lrwxrwxrwx 1 root root 6 Jun 1 2022 fsck.ext4 -> e2fsck
-rwxr-xr-x 1 root root 84360 Mar 23 2022 fsck.fat
-rwxr-xr-x 1 root root 55712 Apr 9 15:32 fsck.minix
lrwxrwxrwx 1 root root 8 Mar 23 2022 fsck.msdos -> fsck.fat
lrwxrwxrwx 1 root root 8 Mar 23 2022 fsck.vfat -> fsck.fat
-rwxr-xr-x 1 root root 1968 Feb 9 2022 fsck.xfs
-rwxr-xr-x 1 root root 51592 Nov 1 2022 ntfsclone
-rwxr-xr-x 1 root root 35200 Nov 1 2022 ntfscp
Purpose of fsck
- Checking Filesystem Integrity: It scans the filesystem for inconsistencies and potential errors, such as corrupted metadata, lost clusters, and bad sectors.
- Repairing Errors:
fsckcan fix detected issues to prevent data loss and improve system stability.
------------------------------------------------------------------------------------------------------------------------------------------------
You can only run a filesystem check on an unmounted disk.
Scanning a specific disk:disk (optional repair):
fsck -t ext4 /dev/sda2
Scanning all disks (optional repair)
fsck -AFSCK Options:
| -A | Check all filesystems. |
| -t [option] | Specify filesystem type |
-y |
Automatically attempt to fix any errors without user prompt |
| -n | Do not attempt to repair |
| -f | Forces a check, even if the filesystem appears to be fine |
| -T | Skip mounted filesystems |
| -R | Skip the root filesystem |
Running a fsck check without repair
if filesystem corruption hasn't yet been confirmed, we first want to run a read only check of the filesystem.
We first need to identify the device to check:
root@test:~# df
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 400556 1068 399488 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 15371208 7497096 7071504 52% /
tmpfs 2002776 0 2002776 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
/dev/sda2 1992552 256828 1614484 14% /boot
tmpfs 400552 4 400548 1% /run/user/0In this case, I'm wanting to run a check for '/dev/mapper/ubuntu--vg-ubuntu--lv' which is mounted to /
A. Boot the server into 'single user mode'
B. Once in single user mode, we can look to initiate the fsck
We will first need to check the filesystem type being used:
get the device name:
root@test:~# df
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 400556 1068 399488 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 15371208 7497096 7071504 52% /
tmpfs 2002776 0 2002776 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
/dev/sda2 1992552 256828 1614484 14% /boot
tmpfs 400552 4 400548 1% /run/user/0
In this example, we want to check '/dev/mapper/ubuntu--vg-ubuntu--lv' which is mounted to /.
Check the filesystem type:
root@test:~# blkid /dev/mapper/ubuntu--vg-ubuntu--lv
/dev/mapper/ubuntu--vg-ubuntu--lv: UUID="2f1c5c3e-54e0-4edc-9d19-a1f170959479" BLOCK_SIZE="4096" TYPE="ext4"
As you can see, in this example the type is ext4.
C. Running the fsck:
The general command structure for running an fsck is as below:
##Check for errors (No repair)
fsck.filesystem_type /dev/device_name -o ro
##Check and repair errors
fsck.filesystem_type /dev/device_name
In this example, I'm going to run a check and then a repair seperately:
##Check for errors (No repair)
fsck.ext4 /dev/mapper/ubuntu--vg-ubuntu--lv -o ro
##Check and repair errors
fsck.ext4 /dev/mapper/ubuntu--vg-ubuntu--lv
2.
3.