Moving Ubuntu to a New Hard Drive

Well, I guess it had to happen some time…  the system disk on my home Ubuntu server started going south recently.  Just a few errors here and there, but once it starts, it only gets worse.  So I thought I’d write down the steps I took to move my system to a new disk, partly for my own reference, and partly in hopes that someone else will find it useful.

First, grab a copy of a “live boot” Linux distro that will run off a CD.  I use Knoppix, but there are others available too.  Attach the new disk to the system, boot off the CD, and mount both the old and new disks.  Make sure the old disk is mounted with ‘errors=continue’ option so that it’ll keep going when errors are encountered.

Use “tar” to copy the root filesystem from the old drive to the new.  Example:

cd /oldroot

tar cvpf – . | (cd /newroot; tar xpf -)

You might want to capture the output of the tar command as well, so you can go back over it and see where it found errors on the old disk.  That way you get an idea if any important files might be corrupted.

When the tar command completes, make sure you have a more-or-less complete copy of the old root filesystem.  An easy way to do this is to run ‘df’ and make sure both copies take up roughly the same amount of disk space.

If your old disk has multiple partitions, you’ll want to copy these as well.

Shut down, remove the old disk, and jumper the new one (if necessary) so it appears as the same device.  Reboot into Knoppix and re-mount the new disk.

Install the grub boot loader on the new disk:

/sbin/grub-install –root-directory=/newroot /dev/sda

Some Linux versions refer to disks by UUID rather than device name.  If this is the case, you’ll need to go into /etc/fstab and /boot/grub/menu.lst and change the UUID to reference the new disk.  You can find the new disk’s UUID by looking in /dev/disk/by-uuid.

My old disk had a swap partition, and I didn’t create one on the new disk.  Instead, I commented the swap partition out in /etc/fstab, booted the system without swap initially, then created a swap area on the filesystem:

dd if=/dev/zero of=/swap bs=1024 count=4194304

mkswap /swap

swapon /swap

This gave me a 4-gig swap area.  To automatically add it at boot time, add to /etc/fstab:

/swap swap swap defaults 0 0

I’m sure I’ve left something out somewhere, but that’s the general idea.