how to fix a corrupted Linux filesystem

Quote from Root on 10/08/2022, 17:25When an operating system crashes, due to a power failure,
improper shutdown,or other unexpected event,
filesystems mounted on it can become corrupted.When we talk about filesystem corruption (in general) we mean that the superblock
(this is the part of the filesystem that contains information about filesystem type,size, data blocks, free blocks,inodes) has incorrect information.
What we need to do?We will run the fsck command to fix the filesystem.First,lets simulate a power failure case.
To do it,we'll setup a virtual Linux machine in your favorite emulator
(could be virtualbox,vagrant or else....even an old pc)
REMEMBER! these commands may cause data loss if used the bad way!
So be careful!Now,go to the virtual machine,and use the "dd" command,
to copy some random data into a partition
(this simulates the corrupted data)So run this:
dd if=/dev/zero count=1 bs=4096 seek=0 of=/dev/sdxxwhere "sdxx" contains the filesystem you are corrupting on purpose,
lets say sda3 or sdb2.
Remember...DON'T DO THIS on a filesystem that contains important data!What is the usage of dd command:
Is used to copy (and optionally convert) data.
Keep in mind that if its used incorrectly, it can cause data loss!Now use the fsck command to repair the filesystem,as following:
fsck /dev/sdxxWhat is fsck:
fsck checks filesystems for inconsistencies and can also repair them.
It can be used manually, but may also be run automatically,
during system boot.
Remember! Unmount a filesystem before using the fsck utility !We may now confirm that the filesystem has been repaired using the fsck command again.
fsck /dev/sdxxIf you need to force a check on the clean partition,
use the fsck command with the -f argument.
fsck -f /dev/sdxxNow,lets use the dd command to corrupt the filesystem once again.
dd if=/dev/zero count=1 bs=4096 seek=0 of=/dev/sdxxAnd now,check and automatically repair the corrupted filesystem,
using the fsck command, with the -y argument.fsck -y /dev/sdxx
That's all! and remember,do not use fsck on a mounted filesystem!
When an operating system crashes, due to a power failure,
improper shutdown,or other unexpected event,
filesystems mounted on it can become corrupted.
When we talk about filesystem corruption (in general) we mean that the superblock
(this is the part of the filesystem that contains information about filesystem type,size, data blocks, free blocks,inodes) has incorrect information.
What we need to do?We will run the fsck command to fix the filesystem.
First,lets simulate a power failure case.
To do it,we'll setup a virtual Linux machine in your favorite emulator
(could be virtualbox,vagrant or else....even an old pc)
REMEMBER! these commands may cause data loss if used the bad way!
So be careful!
Now,go to the virtual machine,and use the "dd" command,
to copy some random data into a partition
(this simulates the corrupted data)
So run this:
dd if=/dev/zero count=1 bs=4096 seek=0 of=/dev/sdxx
where "sdxx" contains the filesystem you are corrupting on purpose,
lets say sda3 or sdb2.
Remember...DON'T DO THIS on a filesystem that contains important data!
What is the usage of dd command:
Is used to copy (and optionally convert) data.
Keep in mind that if its used incorrectly, it can cause data loss!
Now use the fsck command to repair the filesystem,as following:
fsck /dev/sdxx
What is fsck:
fsck checks filesystems for inconsistencies and can also repair them.
It can be used manually, but may also be run automatically,
during system boot.
Remember! Unmount a filesystem before using the fsck utility !
We may now confirm that the filesystem has been repaired using the fsck command again.
fsck /dev/sdxx
If you need to force a check on the clean partition,
use the fsck command with the -f argument.
fsck -f /dev/sdxx
Now,lets use the dd command to corrupt the filesystem once again.
dd if=/dev/zero count=1 bs=4096 seek=0 of=/dev/sdxx
And now,check and automatically repair the corrupted filesystem,
using the fsck command, with the -y argument.
fsck -y /dev/sdxx
That's all! and remember,do not use fsck on a mounted filesystem!