Replacing a hard drive

I recently had to replace the hard drive on my laptop as the existing one started to develop bad blocks.

According to badblocks, there were about 100 bad blocks in /home/, so I got onto the phone to Mr Dell for a replacement. A couple of days later a replacement drive arrived. Now to copy all the data across from the existing drive.

Luckily I have an external USB HDD enclosure, so I can have both drives connected to the laptop simultaineously. I booted from a Knoppix CD to do this.

Partitioning

The first thing I did was copy the partion structure from the existing drive to the new one.
I did this by:

	sfdisk -d /dev/hdc > hdc.out 

That gave an output file similar to:

  /dev/hdc1 : start=       63, size=    96327, Id=de
/dev/hdc2 : start= 96390, size= 46508175, Id= 7, bootable
/dev/hdc3 : start= 46604565, size= 208845, Id=83
/dev/hdc4 : start= 46813410, size=109482975, Id= f
/dev/hdc5 : start= 46813473, size= 30716217, Id=83
/dev/hdc6 : start= 77529753, size= 30716217, Id=83
/dev/hdc7 : start=108246033, size= 4096512, Id=82
/dev/hdc8 : start=112342608, size= 43953777, Id=83

I then edited the file to change the references of /dev/hdc to /dev/sda (the current device for the target HDD. This gave me the following file (saved as sda.out):

 /dev/sda1 : start=       63, size=    96327, Id=de
/dev/sda2 : start= 96390, size= 46508175, Id= 7, bootable
/dev/sda3 : start= 46604565, size= 208845, Id=83
/dev/sda4 : start= 46813410, size=109482975, Id= f
/dev/sda5 : start= 46813473, size= 30716217, Id=83
/dev/sda6 : start= 77529753, size= 30716217, Id=83
/dev/sda7 : start=108246033, size= 4096512, Id=82
/dev/sda8 : start=112342608, size= 43953777, Id=8

To partition the new drive:

 sfdisk /dev/sda < sda.out

Transferring the data

I used 'dd' to copy my Windows partion as well as the Dell diagnostics partition to the new drive eg:

dd if=/dev/hda2 of=/dev/sda2

Changing things around

My previous disk had thefollowing (linux) partitions:

  • /
  • /boot
  • /usr
  • /home

I had come to the conclusion that the /usr partition wasn't serving any real purpose,so I decided to get rid of it in the copy process. To do this, I simply deleted the /, /usr, and /home partitions on the new disk and created / and /home partitions instead.

Copying the Linux partitions

I used rsync to copy the data from all of my linux partitions to the new drive. eg:

 rsync -a /dev/hdc5 /dev/sda4 (copy / from old to new)
and
rsync -a /dev/hdc6 /mnt/sda4/usr (copy /usr)

Bootloader

The last job to do was set up the boot loader on the new disk. To do this I booted from an emergency boot CD rather than the Knoppix CD. This may not have been necessary, but it worked. As root, I did the following:

Enter the grub subshell:

 grub

First I needed to tell grup where my root (/boot) directory / partition was located. As I had an existing system I could get grub to tell me this.

 find /boot/grub/stage1
(hd0,2)

This means that my /boot was a separate partition on the second partition of the main disk (even though my disk is usually /dev/hdc)

now set the root as determined above:

root (hd0,2) 

I the setup grub onto the MBR of my master drive

setup (hd0)

As I had changed my partition layout, I then had to edit my /boot/grub/menu.lst and /etc/fstab to reflect these changes.

All there was left to do was reboot and test. So far, so good ...