Bembel-B Blog

2005/10/25

How to clone a defective hard drive, migrate a Linux installation, and locate defective files

As my (just 15 months old Samsung IDE) hdd continuously had more and more bad sectors, I had to replace it with a new (Seagate SATA) one. Here’s what I did to save as much data as possible, keeping my old filesystems (NTFS, FAT32, ext3, swap).

old opened hard disk drive

Cloning

Boot from the Linux Live-CD Knoppix 4.0.2 enabling DMA at the boot prompt with knoppix dma.

cat /proc/swaps shows me Knoppix is using the swap partition on my defective hdd.

All following commands in this entire HOWTO have to be issued as user root! Use su - to switch to root.

So I unmount the swap partition with swapoff /dev/hda6.

Now the cloning process using dd_rescue is started, filling unreadable sectors with zeroes.

dd_rescue -A /dev/hda /dev/sda

About two hours later copying of all 160GB is finished.
As I didn’t set a logfile for dd_rescue to report bad sectors and Knoppix’ dmesg has only a small buffer space (and the syslog file isn’t used at all), I sadly ain’t able to name all the bad sectors positions at this time. So you would better use that logfile option!
So for now I only know the number of bad blocks (1328!), the total amount of unreadable data (~700kB), and, from what I could read scrolling down the terminal screen, the approximate locations of the bad blocks given in MB counted from the beginning of the hdd.

Let the cloned Linux installation use the new hdd

As I wasn’t able to mount the cloned partitions immediately afterwards, I did a reboot of Knoppix and it worked then. Good thing!

mount -o remount,rw /dev/sda2
mount -o remount,rw /dev/sda3

I now have to fix the partitions locations definitions for my cloned Linux installation to use the new hard drive.

I add the line (hd1) /dev/sda to my boot partition at /mnt/sda2/grub/device.map.

Also change the grub bootloader configuration at /mnt/sda2/grub/grub.conf from hd0 to hd1 and the kernel parameter root like this:

--snip---
splashimage=(hd1,1)/grub/splash.xpm.gz
#hiddenmenu
title Fedora Core (2.6.12-1.1372_FC3)
        root (hd1,1)
        kernel /vmlinuz-2.6.12-1.1372_FC3 ro root=/dev/sda3 rhgb quiet
        initrd /initrd-2.6.12-1.1372_FC3.img
--snip---

Then there’s to fix the fstab at /mnt/sda3/etc/fstab. I change the device from /dev/hda to /dev/sda and also switch from using labels (/boot and /) for my Linux partitions to using the distinct device node names (/dev/sda2 and /dev/sda3).

Also mtab at /mnt/sda3/etc/mtab has to be fixed the same way.

And as I am using automount, these configuration files (/mnt/sda3/etc/auto.*) have to be fixed too.

Only thing left is the swap partition. I fixed that simply by rebooting from the new hdd (as defined in the BIOS settings) and typing this in the terminal:

swapon /dev/sda6
swapoff /dev/hda6

UPDATE: The above configuration still uses the bootloader in the old HDD’s MBR. The old HDD is still seen as the first boot device by the BIOS (therefore I use hd1 and not hd0). I figured that out when I wiped my old HHD’s partitions.
To exclusively boot from the new (cloned) HDD, you have to reinstall GRUB on this HDD. The process I would have had to use is a little tricky, and can be found in my posting about installing OSes on a SATA HDD.
In brief, one boots from the Fedora Rescue CD and chroots to the Fedora partition on the new HDD. Then fix the /boot/grub/system.map to see the HDD as first boot device and reinstall grub on this HDD. You would also have to fix /boot/grub/grub.conf to use hd0 instead of hd1 and configure your BIOS to boot from this HDD.
If you still have your old HDD intact (i.e. the system is booting from HDD), you most probably can skip the step of booting from the Rescue CD and simply boot from the HDD.

Let the cloned Windows XP installation use the new hdd

Heheh.. No way, dude! At least it would take hours and days from my knowledge. You would have to edit \boot.ini and also the System Registry’s drive and partition definitions. And then all paths and drive letters still point to the old hdd (as far it’s still plugged in).
So, once again, Microsith’s ultimate answer is: Reformat and reinstall! Outch!

Find bad sectors and affected files

The SMART system will only name the first bad sector it finds. On Linux there’s a tool called badblocks. I only tried its non-desctructive read-write test and it’s slow but reliable. But if you kept the dd_rescue log (unlike me), you find the block numbers there.

To locate a file by block number on a Linux filesystem (ext2, ext3, minix, reiserfs etc.), one can use debugfs as described in a HOWTO at smartmontools.

Locating files by sector or block number on a NTFS partition is possible under Linux with the use of ntfscluster found in the ntfsprogs utility suite by the Linux-NTFS Project.

How to do this on FAT or NTFS systems is still unknown to me. After googling for quite a long time I still did not come up with a solution. I guess one should be able to accomplish this with a disk editor. Still the question, which one would that be?!

Copyright notice

The photograph showing an old opened hard disk drive used in this entry is in the Public Domain and thus free of any Copyright restrictions. The content is provided by Michael Connors and Morguefile and has the ref id# 13218.

ChangeLog

[2006-04-23: Add tags.]
[2006-07-21: Reinstall GRUB to be able to boot exclusively from the clone. Locate files by sector or cluster number on NTFS partitions. More tags.]

Create a free website or blog at WordPress.com.