How to clone Linux disk of different sizes; large HDD to smaller SSD

Spread the love

WARNING: MISUSE OF THE COMMANDS IN THIS TUTORIAL CAN CAUSE DATA LOSS!
BACKUP YOUR DATA!
YOU’VE BEEN WARNED!


Step 1: Clone original HDD to larger, spare HDD

1.1. Connect SRC and DST disk to computer

1.2. Disconnect external drives that you may be using for backups, you won’t want to accidentally overwrite them!

1.3. Boot off of Ubuntu 14.04 live USB stick or your Ubuntu tower with SRC and DST connected

1.4. Use gparted to identify your boot OS, original SRC, and DST disk device ids, for example:

Write these down! If you mix these up you will LOSE ALL YOU DATA! Using all different sized disks makes this easier. If you have multiple disks of the same size, connect one disk at a time and label them. You don’t want to make a mistake here.

1.5. Clone SRC disk to DST disk using the pv & dd utilities (pv is available in the Ubuntu “universe” apt-get repo):

where:

for example on my system:

This may take several hours depending on your disk sizes, using pipe view (pv) will give you an estimate for how long it will take.

1.6. Power off your computer after the disk is cloned, the Linux kernel may not read the newly cloned disk correctly.


Step 2: Resize Cloned HDD

2.1. After powering off, disconnect the original drive to keep it safe from modification

2.2. Boot computer again, use gparted to resize and move all partitions so that they will fit on the new SSD

You can launch gparted with the DST device id to operate on just that disk, for example:

NOTE: gparted should display all partitions on the new disk without any warnings like /!\ next to a partition. All partitions should be readable by gparted with partition size and used listed next to each partition. If you are booting off a live USB stick, you will probably need to turn off the swap partition by right-clicking and selecting swapoff in the menu.

2.3. After resizing everything power off the computer


Step 3: Clone HDD To New SSD

3.1. With the power off, connect the new SSD to the computer

3.2. Boot computer with the resized HDD and the new SSD

3.3. Use gparted to identify your boot OS, original SRC, and DST disk device ids, for example:

Write these down! If you mix these up you will LOSE ALL YOU DATA!

3.4. Clone SRC disk to DST disk using the pv & dd utilities:

where:

for example on my system:

This may take several hours depending on your disk sizes, using pipe view (pv) will give you an estimate for how long it will take.

Since the destination SSD is likely smaller than the source HDD, dd will error out at the end stating the destination disk is out of space:
dd: error writing ‘/dev/sdc’: No space left on device
This is OKAY.

3.5. Power off the computer and disconnect the spare HDD, so moving forward we are only modifying the SDD


Step 4: Repair The Partition Table

The original partition table that got cloned to the SSD is now incorrect, the physical size of the disk has changed, we need to repair it. With Ubuntu 14.04, the partition table being used is something called a GPT. We will repair the partition using the tool gdisk.

4.1. Boot the computer without the spare HDD

4.2. Use gparted to identify your boot OS, and the new SSD device ids, for example:

Write these down! If you mix these up you will LOSE ALL YOU DATA!

4.3. Use gdisk to repair the GUID Partition Table (GPT):

4.4. Reboot the computer so the Linux kernel sees the new partition table


Step 5: Repair Boot Image

After all the work of resizing the partitions and repairing the partition table, the GRUB boot image is likely non functional. From my experience the BIOS can try to boot from the disk but we are left with a single ‘-‘ blinking character and nothing happens. The second stage boot loader can’t be found. Here we’ll repair it.

5.1. Boot computer

5.2. Use cgdisk to create a tiny partition to install grub2 on:

5.3. Select the free space at the beginning of the disk to create the new partition, it MUST be the first partition on the disk:


Step 6. Install Grub2

Now that we have a dedicated grub boot partition, we can proceed to install grub2 on the disk.

6.1. Mount the data partition disk for chroot operations:

6.2. Change root into mounted disk:

6.3. Reinstall grub2, recheck, recreate menu:

6.4. Exit the chroot shell and unmount everything:

6.5. Shutdown the computer and install the SSD into the new computer, it should now boot!

This entry was posted in computing and tagged . Bookmark the permalink.