Mo's Blog

Migrate Linux to New SSD in 30min

I once donated my old (and first) SSD to my parent's computer. Recently, I got a bit worried that it might fail and checked the status in Smartmontools. Based on this, I decided to migrate to a new SSD. I wanted to replace the drive of a Linux system in less than 30 minutes. Here, I document how it goes.

0. Prerequisites

The old and new SSDs need to be plugged into the computer. Ideally, boot a live system to avoid file corruption when using dd. However, I had no issues running this on the existing system. Nevertheless, I make a backup.

1. Duplicate the Partition Layout

First, you need to replicate the partition layout of your existing drive /dev/sda onto the new drive /dev/sdb. This ensures the new drive has the same partition structure.

Use sfdisk to dump the partition table of your existing drive:

sfdisk -d /dev/sda > pt

Inspect the pt file to ensure everything looks correct and write the partition layout to the new drive:

sfdisk /dev/sdb < pt

Now, the partition table on /dev/sdb matches your existing drive.

2. Clone the Boot/EFI Partition

Next, clone the boot or EFI partition, typically the first partition /dev/sda1, to the corresponding partition on your new drive.

Use the dd command for this:

dd if=/dev/sda1 of=/dev/sdb1

It will clone the boot partition and keep the UUID so your system can boot from the new SSD.

3. Format and Resize the Other Partitions

Using a tool like GParted, format the remaining partitions on your new drive. At this stage, you can change the filesystem to something more efficient. For example, you can switch from an older filesystem to ext4 for better performance and reliability.

If your new SSD is larger than your old one, don't forget to resize the partitions to use the additional space.

4. Mount Drives and Copy All System Files

Now, it's time to mount both the old and new SSDs so that you can transfer your system files.

mount /dev/sda2 /mnt/existing
mount /dev/sdb2 /mnt/new

Copy all the files from your old drive to the new one using the cp command:

cp -ra /mnt/existing/* /mnt/new/*

The -ra flags ensure that all files are copied recursively and preserve file attributes like ownership and permissions.

5. Update Partition UUIDs in fstab

As the new SSD will have different UUIDs for its partitions, you should update the /etc/fstab file on your new drive to reflect these changes. The fstab file tells your system which partitions to mount at boot.

vi /mnt/new/etc/fstab

Replace the old UUIDs with the new ones. You can find the new UUIDs using blkid

6. Boot into the New System and Make the Changes Permanent

Shut down your computer and swap the old SSD for the new one. During boot, enter the boot menu, press e to temporarily edit the boot configuration, and change the UUIDs to point to /dev/sda2 for your new drive.

Once you've successfully booted into your new system, run the following command to update GRUB and make the changes permanent: sudo update-grub

This command ensures that your system boots correctly with the new UUIDs every time you restart.