Copying / Replicating your Linux Install to a Different Hard Drive

I’ve had my current Linux install on a 300 gig drive for over a year now while my previous system was on a 500 gig drive, along with an ancient copy of Windows XP.  That drive has been laying around collecting dust.  I’ve been wanting to “copy” it over to
this larger drive.  My initial thought was to “replicate” my current disk over to it the way I did it in this previous post:  “How to Clone a Dying Hard Disk.”  This however, would’ve wiped the Windows XP partition, which came with my computer that I just haven’t quite brought myself to do (I might want to compile some Windows binaries with it with PAR, and for whatever reason, I’ve never been able to get ActivePerl to install on my other Windows 7 installation after several attempts).  Anyway, I was looking for another way to just copy individual partitions, rather than the entire drive.  Here’s what some research suggested and what I was able to get to work (YMMV! – WARNING: MANIPULATING PARTITIONS IS INHERENTLY DANGEROUS, A MISTYPED COMMAND CAN INSTANTLY DELETE YOUR DATA!  I can NOT guarantee that these steps will work for you in your situation, I can only state here from memory what worked for me!  I’m not responsible if this does not work for you, so if you are not comfortable attempting this, then DON’T!):

0)  (Optional, informative) Check if your running distro is fully up-to-date with packages, update anything that you need/want to update. Do hdparm -i; -I; -t; and -T commands writing out the results, save ’em to compare to your new drive if you want to compare the performance of the two disks.

1)  Boot up into a LiveCD and use GParted to (re)create and format partitions, MAKING ABSOLUTELY *SURE* YOU SELECT THE *NEW* DISK EACH TIME!!!!!!!! Also make sure your LiveCD didn’t mount anything (if so UMOUNT it!)

2)  Try using GParted to copy the old partitions to the new ones one at a time.
(MAKE SURE YOU SELECT YOUR EXISTING PARTITION(S) FIRST to copy, then paste into your NEW drive!)

If this fails (or GParted won’t let you paste) for a partition, try (as root with NEITHER mounted):

     e2image -ra -p /dev/sd<FROM> /dev/sd<TO>

Failing this: try mounting the first as readonly (mount -r), then the 2nd as read/write, and do:

     cp -afv /mnt/sd<FROM>/* /mnt/sd<TO>

Yes, this DOES copy symlinks, etc. AND preserves ownership, permissions, and timestamps!

3)  Get the UUID’s of your NEW partitions using:

     ls -l /dev/disk/by-uuid

and save this in a text editor/file somewhere.

4)  Mount your NEW (copied) root partition, and edit your NEW /etc/fstab, /etc/mtab, /boot/grub/menu.lst, and ALL files within /etc that contain “UUID”, since they ALL need to be changed.  Each old UUID needs to be changed to the UUID of the corresponding partition that it was copied to!  ALSO make sure you fix your menu.lst file to the NEW hd(0,#) for the new partitions your kernel(s) are in!!!  If you are moving to a disk that already has Windows on it from one that doesn’t, you’ll for sure have to change the “#” above AND add this to your NEW /boot/grub/menu.lst file

For Windows XP:
     title Microsoft Windows XP Professional
     root (hd0,0)
     chainloader +1

For Windows 7:
     title Microsoft Windows XP Professional
     title Windows 7
     hide (hd0.0)
     rootnoverify (hd0,1)
     chainloader +1

5)  Power down, REMOVE your old disk drive, make your new one primary, and try booting.  Most likely it won’t work, but no harm in tryin’!

6)  Power down and reboot in your LiveCD, and do as root:

     mkdir /mnt
     mount /dev/sd<yourNewRootPartition> /mnt
     mount --bind /dev /mnt/dev
     mount --bind /proc /mnt/proc
     mount --bind /sys /mnt/sys
     chroot /mnt

This makes your new hard-drive installation your “root” (/)!

     cd /boot

Select a SINGLE linux kernel you want to boot that isn’t working and do (as root):

     update-initramfs -k <version> -v

EXAMPLE where <version> is 4.0.0-antix.1-486-smp, if your chosen kernel is: vmlinuz-4.0.0-antix.1-486-smp

7)  Update-grub:

     >find /boot/grub/menu.lst
     >root (hd0,#)      <-based on output from find!
     >setup (hd0)

8)  Update-grub Again, just to make sure!

     grub-install      (if you're SURE grub's still broke AND you're SURE you're installing to MBR)

9)  Remove LiveCD, reboot selecting the kernel you did the update-initrams command on earlier (others likely won't boot) & do sign of the cross!

10)  After successfully booting into a linux kernel, go repeat the update-initrams you did earlier on each kernel, else removing any you don't need.

11)  Reboot into Windows (if you have it) to make sure it's ok.

12)  Reboot into your preferred production kernel!

Helpful links:

Copy Your Linux Install to a Different Partition or Drive, by (©) Ningappa Koneri, Linux Journal, July 9, 2009.
chroot to repair GRUB - How does it work?, by (©) Ubuntu Forums, Feb. 21, 2011.


Feel Free to Comment (Name/Email/Website optional):

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: