Continuing with the theme of broken backup servers (oh boy am I having fun this week) I decided it may be worth giving a short explanation of how to clone or move a Linux install from one hard drive to another (note I’m not talking about two completely identical machines. If this were the case, a simple disk image clone should do the trick).
So the situation is, you’ve got two hard drives, one was more or less written off by a mouse urinating on it, the other is slightly smaller but fully functional. You want to move the Linux install (including all configuration etc) onto the better, smaller drive with minimal hassle. Believe it or not, it’s not that difficult.
Please note the instructions I give below worked for me. I don’t and will not hold and responsibility for you trashing your own computer by following them, YMMV.
Ok. Step one I figured was to copy the filesystem from the old HD to the new one. The simplest was I could see of doing this was:
- Plug the new HD in via my removable caddy
- Boot up the existing Linux install and mount the new HD
So far so good. Next I cleaned up the new HD (when I say new, it’s actually quite old, but it’s new relative to this project if you follow), so as root “fdisk /dev/hdc” (in my case, obviously replace hdc with your second HD’s device) and removed all the existing partitions. I then created a new one for my root filesystem (and you will probably want one for your swap space).
Next we generate a new filesystem. In my case, an easy “mke2fs -j /dev/hdc1“. Don’t forget to write down the locations of those backup superblocks! Might just save your data some day.
Side note 1: I guess it’s entirely possible to copy your Linux install using this method to a mounted loop device (i.e. a disk image), although quite why you’d want to I can’t imagine… but the process would be exactly the same.
Side note 2: Personally I would always at this point perform a complete scan of the new HD just to be absolutely sure I’m not making things worse and wasting my time. The command “fsck.ext3 -cf” (for ext3 anyway) will perform a bad block scan, logging anything found for future avoidance. Even new HDs can come with bad blocks, if you notice a lot being reported, consider scapping that HD.
It’s important to take note at this point that you’re going to have problems if you’ve done one of the following things: a. created a new partition smaller than the amount of data in your existing partition (df -h will tell you) b. you’ve created a different filesystem type to what you’re currently using. It’s outside the scope of this blog to discuss those.
Next copy everything you want over to the new partition. In my case this was everything from /bin to /var, excluding /mnt and a few other mounted directories. Don’t copy mounted directories (that includes /proc and /mnt etc), rather just create them on the new partition with “mkdir /mnt/proc” etc). This can be easily achieved using “cp /bin /mnt/ -R” (and so on). Make sure you do copy /dev, I made that mistake first time and it produces some weird results.
- cp /everythingyouwant /mnt/ -R
OK! Nearly there. Shutdown, swap the two HD’s around and boot back up. Don’t forget to tell the BIOS of the changes and to check those jumper settings. Make sure your BIOS boots the old HD, at the moment GrUB doesn’t exist on the new one. Boot it, pass through GrUB as normal and you’ll hopefully be brought into Linux, from your new HD. If something goes wrong, post a comment and myself or someone else will try to help.
So assuming you’ve logged in all OK, you’re ready to install GrUB on your new HD. Once this is done, you’re ready to shutdown, remove the old HD, tell the BIOS to boot the new one and continue your normal work!
Run “grub-install” as root, let it figure out what exists and if all goes well, it’ll pick up your existing (copied from the old HD) /boot/grub/ directory and place it’s stage1 onto the new HD’s boot sector. If Grub doesn’t output any errors, you’re ready to try shutting down and rebooting. If Grub does complain, it might be something simple, it might be something complex. Again, feel free to leave a comment.
OK, reboot, set the BIOS to boot the new HD and see if it works. If it doesn’t, you’ve probably just mis-configured GrUB. A boot disk or booting the old HD will give you an environment to play in, the GrUB website has a lot of details on configuring GrUB correctly. Remember to keep the menu.lst the same as before, nothing in there should need changing (unless you’ve moved other HDs around or partitioned the new drive differently).
You should now be back, booted up and working! Congratulations! Told you it wasn’t too hard
Important note: If you’ve had serious problems with the old HD, it might be worth you reinstalling all packages (or reemerging if you’re a Gentoo user) to make sure none of your binaries are corrupt. On on Gentoo it’s something like “emerge –reemerge world” iirc, as for other distros I don’t know, look up the manuals. Also make sure none of your configurations or home brewed scripts need modifying now the HD has changed, I think particularly of SMART tools and software RAID. I also had a lot of problems getting Cron to run, so just watch your back and check everything extensively works before relying on it.
As always, any constructive comments would be appreciated.