Sunday, October 07, 2007

Going from IDE to SATA in Linux

A few years ago now I was shopping for a new motherboard and processor combo to perform an in-case upgrade to my outdated PC. One of my decisions was between two different motherboards. The only noticeable difference was that one supported SATA drives and the other was strictly IDE. It was an extra seven bucks for the SATA board ... so I went ahead and bought that one.

Since then I have only ever used IDE drives as that was all I had available, and I wasn't trying to spend any more on the upgrade than I had to. But I recently was gifted an 80 gig SATA drive. Now all I had to do was to get all of my data onto it. Luckily the IDE drive that the SATA drive is replacing is the same size: 80 gigs.

I put together a plan to completely "clone" my present system and copy all of my data onto the new SATA drive. This would keep me from having to migrate my data away, reinstall Frugalware Linux, and then put all of the data back. My plan worked surprisingly well, so here is what I did. I hope that this will help someone out there who stumbles upon this in a Google search.

1) First, I simply installed the drive (physically) and checked to ensure that the BIOS was detecting it correctly. That part was relatively simple. Then, I booted as I normally would.

2) Once I was booted into Linux, I opened up a terminal and ran the following command ...

dd if=/dev/hda of=/dev/sda bs=32768

That command in a nutshell, copied every single track from the IDE drive (hda) to my SATA drive (sda). It created a complete clone. This would have also worked had my new drive been larger (but not smaller, as that would produce an out of space error). The problem would have been then that the new partition did not fill the drive. You would have to use a partition editor to fix this problem, such as gparted. I should note that copying 80 gigs takes a pretty long time. About as long as it would have taken to low level format the IDE 80 gig drive. In my case, it was about 1 hour and 10 minutes.

3) Now that I have cloned my drive, I needed to make sure that Linux knows to boot from it. For that I first mounted the new partition. In my case, sda1 was the "data" partition on my SATA drive, and sda2 was the "swap" partition.

mkdir /mnt/newdrive
mount /dev/sda1 /mnt/newdrive
vi /mnt/newdrive/etc/fstab

My /etc/fstab file looked like this:
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
usbfs /proc/bus/usb usbfs devgid=23,devmode=664 0 0
tmpfs /dev/shm tmpfs defaults 0 0
/dev/hda2 swap swap defaults 0 0
/dev/hda1 / ext3 defaults 1 1
/dev/hdc /cdrom iso9660 user,noauto,ro 0 0

All that was necessary was to change the hda drives to sda drives. The final product looked like this:

none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
usbfs /proc/bus/usb usbfs devgid=23,devmode=664 0 0
tmpfs /dev/shm tmpfs defaults 0 0
/dev/sda2 swap swap defaults 0 0
/dev/sda1 / ext3 defaults 1 1
/dev/hdc /cdrom iso9660 user,noauto,ro 0 0

Make sure you save your changes, and quit.

4) Next I rebooted. Upon booting up, I went into the BIOS and changed by boot order so that it tried to boot from the SATA drive first. It did! At the Linux boot prompt (grub) I had to stop it from booting, and change the boot options. It was going to boot with "root=/dev/hda1", so I had to change it to "root=/dev/sda1". Having made the change, I booted right up!

5) This was a good chance to make sure that everything worked. All my data looked okay, no errors at booting time, etc. I ran "mount" by itself to ensure that I really was running from my SATA drive at this point ...

ray@frugal:~$ mount
/dev/sda1 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
usbfs on /proc/bus/usb type usbfs (rw,devgid=23,devmode=664)
tmpfs on /dev/shm type tmpfs (rw)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/hda1 on /media/disk type ext3 (rw,nosuid,nodev)

6) Having confirmed that this would work from here on out, I needed to make a change to grub so that it boots from the SATA drive from now on (and I wouldn't have to change the root line every time I booted).

vi /boot/grub/menu.lst

In the grub booter file, all I had to change was "hda" to "sda" in the applicable place. In the end, my boot file looked like this:

# /boot/grub/menu.lst - configuration file for GRUB
# This file is generated automatically by grubconfig

gfxmenu (hd0,0)/boot/grub/message

title Frugalware 0.6 (Terminus) - 2.6.20-fw4
kernel (hd0,0)/boot/vmlinuz root=/dev/sda1 ro quiet vga=791

title Memtest86+
kernel (hd0,0)/boot/memtest.bin

And that was it!

7) I reboot one more time, this time letting everything run it's course. The BIOS detected the drive ... Frugalware Linux came trying to boot from the correct drive ... and I booted (much quicker than before thanks to the obvious SATA speed advantages).

In closing: This was a "poor mans cloning process". Normally I wouldn't recommend cloning a system that was running. But there weren't any notable side effects. The only issues I found were that my Firefox settings seemed to be lost. The first time I fired it up after this process, I had to re-enter some passwords and such for my various Firefox add-ons. Most likely this was from having those files open and in use when they were copied from one drive to the other.

Was any of this helpful to you? Drop me a line and share your experiences.