2024-04-09

Cloning and Replacing a HDD with a SSD

One summer, I got into a groove of replacing spinning-platter hard drives with solid-state drives in (almost) all the computers in my house. Doing so brought new life into a few aging laptops (one, my wife's, literally went from booting up from a cold start in several minutes to less than one), and the home server (that spends most of its life as a glorified NAS, but also runs a couple virtual machines) showed a marked increase in performace.

I tried a couple different "packaged" software tools, but they either cost money, failed to work on drives not from a specific manufacturer, had very specific requirements (like the old and new drives had to be precisely the same size), were unable to handle any disk errors, or just plain didn't work. After a lot of trial and error, I found a way to do it, for free, in a consistent, repeatable way. I got pretty good with the process, but since it's been a long time, I worry that I might not remember exactly how to do it when the need arises. So, here's my process, as much for my own memory as for helping anyone else out.

The software I used is a "live" installation of GNOME Partition Editor. I've found that a USB stick is the most convenient these days, since optical drives are getting pretty rare. But anything that can be booted from should work. GParted's site has instructions for four different ways to make a bootable environment.

The only piece of hardware (other than bootable media) is a USB hard drive enclosure. For laptops, it's almost certainly a necessity, because you need to have both the old hard drive and the new SSD connected at the same time, and not many laptops have connections for multiple drives. Desktops, you might be able to get away with plugging the SSD directly into the system, if there are enough available connectors.

With both drives connected to the system, and the GParted boot media connected, boot up the computer into the GParted environment. (Getting the system to boot from the USB drive or other bootable media might require holding down a key on startup — personally, I start mashing the entire top row of keys on the keyboard, from Esc across all the function keys, to try to trigger a boot menu. If you get into Windows, holding the shift key when you click Restart from the Start menu should get you into a boot menu with the option to boot from something else.)


GParted's main window, from gparted.org

Once the GParted environment loads up, the most critical part is to identify which hard drive is which device name. Ideally, you should be able to see the hard drive with its existing partitions pretty clearly and distinctly from the new drive with no partitions (or just one empty partition). Use the dropdown button in the upper right to switch disks, and double- and triple-check that you know which drive is the "source" and which is the "target". For the purposes of this walkthrough, I'm considering the original hard drive "sda" and its shiny new replacement "sdb".

The next step is to copy the partition table, including the identifiers, from one disk to the other. While this can be done in one step, I like to keep it in two, because the order of parameters is backwards compared to what I'd expect, and I always feel like I'm wrong if I try to combine it.

Open up an XTerminal window by right-clicking the desktop, and run these two commands:

  • sudo sgdisk --backup=table /dev/sda
  • sudo sgdisk --load-backup=table /dev/sdb

I will say that I've seen some guides suggest that you should run another command to randomize the partition GUIDs, so that both disks don't have the same IDs. In my experience, though, if you do this, and then swap out the drive and try to run the new SSD in place of the old HDD, the OS may not recognize that the drive is the "same", and it'll be unable to find installed programs or files, even if they're still on the same drive letter or mount point. So, don't do that at this step.

With the partition tables copied, go back to GParted's main window, and refresh the drives. You should now see the same partition setup on sdb as you see on sda. Go to the first partition on sda, click Copy, switch over to the first partition on sdb, and click Paste. Repeat for all partitions. (I have come across partitions that are "unrecognized" that I can't copy/paste. Fortunately for me, I haven't needed whatever was on them, so I just keep my head in the sand and hope for the best. It's worked so far, knock on wood.) Once all the copy/paste commands are queued up, click the Apply button, and settle in for a long wait.

After everything's copied, now comes the fun part. Shut the system down, physically uninstall the old hard drive, install the new one in its place, and boot up the system. Assuming everything went according to plan, the system should boot up as if nothing had changed.

Once you've verified that the system accepts the new drive as its own, only then should you attempt to tinker. For instance, if the new drive is larger, you may want to move or resize partitions around. GParted is a good tool for doing that, as well. Make whatever changes you feel like, and then boot into the normal operating system to verify everything still works. Worst case, you can always go back to the original drive and re-clone.

Once you're happy with the new drive, you can decommission the old drive. I would not recommend plugging it into your running OS, since the partition GUIDs are duplicated and it might get confused. GParted doesn't seem to mind this; or you can use another running computer to plug the old drive into to reformat it. Removing and recreating partitions should get rid of the duplicate GUIDs, but if you want to be sure, you can boot into GParted again and run this command to give the partitions new GUIDs. (Make sure you are doing this on the old drive that you're not using anymore.) Note that sdx here is whatever device designation GParted gave the old disk. sudo sgdisk -G /dev/sdx

And that's it.