Retrieving files from a broken harddrive on linux

We recently had some trouble with a software raid (raid1 in this case) that was set up with mdadm. Dmesg showed that the drive had errors and so we replaced it. After the disk was replaced the other disk also started reporting errors and fsck would tell us that the superblock was fucked up and couldnt be read.

First thing we did was to dd the broken disk to the new one using ddrescue .

We tried rebuilding it by finding backup superblocks :
mke2fs -n /dev/sdb1 # lists superblocks
e2fsck -b number_from_output_above /dev/sdb1
While the above works in many cases, it didnt help us. All the superblocks where gone.

Thankfully we came across a tool named TestDisk that let us view and copy the data from the disk. Check it out, it’s awesome :)

 

Tags: , , , , , , , , , ,

Howto resize an ext2/3 filesystem on a partition or an image of a partition

Unmount disk or image
umount /dev/sda1
fsck -n /dev/sda1

Next we remove the journal from /dev/sda1, thus turning it into an ext2 partition:
tune2fs -O ^has_journal /dev/sda1

And fsck again..
e2fsck -f /dev/sda1

Now we resize our file system with resize2fs. resize2fs can resize ext2 file systems, but not ext3 file systems, that’s why we had to turn /dev/sda1 to ext2. Currently, 4.1GB are used on /dev/sda1 (see the df -h output above), So it’s safe to shrink it from 10GB to about 6GB (if you make it smaller than 4.1GB, you will lose data!). Therefore we run
resize2fs /dev/sda1 6000M

Aaaaand fsck again…
fsck -n /dev/sda1

Then we create the journal on our new /dev/sda1, thus turning it into an ext3 partition again:
tune2fs -j /dev/sda1

Tags: , , , , , , ,