How to use FSCK for Data Recovery with Linux.
The fsck (File System Consistency Check) utility checks a Linux filesystems for errors or corruptions.
fsck comes by with all Linux distributions and is similar to the CHKDSK command in Windows.
The fsck utility is used to correct errors in the ext file-system EXT. EXT uses inodes which describes an object such as a file or a directory. Each inode stores the attributes and disk block locations of the object’s data. The inode system creates a separate table on each l device, called the inode table, to store the file information. Each file has an entry in the inode table. In addition the following metadata is kept about each object
- The file name
- The file size
- The owner of the file
- The group the file belongs to
- Access permissions for the file
- Pointers to each disk block that contains data from the file
Each inode in the inode table has a number (called the inode number), assigned by the file system as data files are created. Linux uses the inode number to identify the file rather than using the full file name and path.
If you want to follow this guide you will need
- Linux or UNIX-like system
- A user with root permissions
When to Use fsck in Linux
The fsck tool can be used in various situations:
- If your Linux system becomes unbootable you will need to run fsck .
- If you get an input/output error accessing files on your system it is likely they have become corrupt.
- You can also use the fsck utility to check the health of external drives, such as SD cards or USB flash drives.
To find out storage devices on a Linux we want to scan we use the Linux df command
To list partitions for your first disk eg /dev/sda, for example, use the following command:
sudo parted /dev/sda ‘print’
sda refers to your disk under Linux , the second would be sdb, sdc and so on.
We have only one partition on the drive sda.
Before we can scan a disk for errors with fsck, we need to unmount sda . This ensures Linux has no filesystem components open for use
sudo umount /dev/sda
Note that you cannot unmount root filesystems. Once the ext filesystem is not in use fsck can be used on a running machine. More on that towards the end of the guide.
Run fsck to Check for Errors
Now that you have unmounted the disk, you can run fsck. To check the second disk, enter:
sudo fsck /dev/sdb
The exit code the fsck utility returns is the sum of these states:
Mount the Disk
After unmounting a storage device and running the file check you will to mount the disk so you can use itn.
In our case, we will remount the sdb disk:
The command for this is mount /dev/sdb
Fix Errors Automatically
To try to fix potential problems without getting any prompts, pass the -y option to fsck.
fsck -y /dev/sdb
This way, you say “yes, try to fix all detected errors” without being prompted every time.
If no errors are found, the output looks the same as without the -y option.
Display fsck Errors
Use the -n option if you want to check potential error on a without repairing them.
We have a second drive sdb with some journaling errors. The -n flag prints the error without fixing it: sudo fsck -n /dev/sdb
Run fsck on Linux Root Partition
As we already mentioned, fsck cannot check root partitions while the machine is running. To check a Linux root partitions we need to boot into recovery mode
- Press and hold the shift key during boot-up. The GNU GRUB menu appears.
- Select Advanced options for Ubuntu.
- Then, select the (recovery mode) entry at the end. Let the system load into the Recovery Menu.
- Then select fsck from the menu.
After fsck has finished checking your root filesytem linux will resume booting your Linux distro presuming all errors have been fixed. Any further errors would most likely be physical errors such as bad sectors on the device
Full List of input parameters
||Attempt to fix errors automatically. Use with caution.|
||Force fsck to check a file system even if it thinks it’s clean.|
||Check all disks listed in
||Show progress bar (ext2 and ext3 file systems only).|
||Skip mounted file systems.|
||Test run. Describes what would happen without executing the check itself.|
||Use with the
||If using the
||Check only a specific type of filesystem.|
||Skip the title on startup.|
||Interactive repair mode.|
fsck is an important data recovery tool in the linux world and it’s judicious use will save much heartache.