A long, long time ago, someone mentioned to me that it was possible to zip up an entire installation, and transplant it into another hard drive. I only halfway believed it at the time, since it seemed rather far-fetched and I was but a newb (I still am). But it sat in the back of my mind like an emergency exit, in the odd chance that I might need to use it … even if it sounded preposterous.
But you know what? You can do it. It works fine. In fact in some situations, it’s easier than actually installing a full system.
I wanted to move the entire installation from my Thinkpad to a different hard drive. I was using the 120Gb Western Digital Scorpio that fell out of the sky into my hands, but I have a faster-yet-smaller hard drive — a 7200rpm 60Gb Hitachi — that I would prefer to use. I don’t need 120Gb of storage, and the space would be better spent in an rtorrent slave or in an external case.
But the Scorpio had a Crux installation on it — a six-month old console-only installation, and not one I was keen on rebuilding just to swap drives. I like the idea that it’s at least a half a year old, and that it hasn’t needed any maintenance other than regular updates.
Cloning was not an option, since the cloning tools I am familiar with don’t seem able to “scale down” an image to fit the drive — Clonezilla, which is a gift from heaven in the category of backup tools, gives me errors if the partitions don’t match quite right. And technically I don’t have space to mirror the drive to, since the only other drive I have is my storage volume, which is where the real-life important stuff lives.
So I began to wonder if I could really unload all the files, then copy them back, and still have a working system. I read it on the Internet, so it must be true. …
And this time it is. Here’s how:
First, offload all the files, by directory. I usually have a three-way partition split, with a separate /boot, root and /home. (Why? Don’t interrupt.) Jump into a live environment (any live CD will do) and make three new folders on a destination drive — in my case, it was the storage volume — and label them hda1, hda3 and hda4 … although really, the names don’t matter.
Mount them so you can write to them, maybe putting them in /media/…/hda1, hda3 and hda4. Just to keep things clear in your mind, you might want to make a /media/destination and /media/source folder, to avoid accidentally putting the wrong stuff in the wrong place.
Next, rsync is your friend. You’re three quick steps away from mirroring all the files.
rsync -a --progress /media/source/boot/ /media/destination/hda1/
rsync -a --progress /media/source/home/ /media/destination/hda4/
Now the last one is a little funny.
rsync -a --progress /media/source/ /media/destination/hda3/ --exclude=proc --exclude=sys
I left out files in /proc and /sys since most of those are generated by the running system, on the fly. And those trailing slashes are important. rsync is a really fantastic tool — one of my favorites — but it’s very precise about what you ask for. If you leave off a slash, it will insert an entire directory inside another, rather than making the two folders identical. Which is what it’s supposed to do, but not what we want.
One other thing: If you’re like me and you build your own kernels, remember that the kernel source code might only be 60Mb or so, but once it’s decompressed, configured, compiled and finished, that darn tree can take up an easy 500Mb. Before you go transferring your entire installation to an external drive across USB1.1😯 , you might want to take out some of the unwanted stuff. I am speaking from experience here.🙄
Wait patiently while the text scrolls by. … And once that’s finished, then you can swap the drive and reverse the process. I used the same partition arrangement with a slightly smaller /home directory, then rsync’d everything back across. It took a while, but after it was done and all the files were in place, I did a filesystem check and reinstalled grub (I use the command-line interface to install grub. I’m cool that way ) and rebooted.
And what to my wondering eyes should appear … but the same system I was using a few hours earlier, with all the stuff still in place, all the files and all the network connections and framebuffer support and so forth and so on, just like I left it on the other drive. No boot problems, no file inconsistencies, no error messages, nothing. It didn’t even know it was on a different drive.
So it’s true, you can actually copy an entire Linux installation between drives, and it’ll work. If you feel daring and want to try it for yourself you can give it a whirl, but make sure you save all the important stuff before you begin. Remember, you read it on the Internet, so it might be true. …🙄