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:
1 2 3 |
OS = /dev/sda SRC = /dev/sdc DST = /dev/sdb |
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):
1 2 |
sudo su pv -tpreb {SRC} | dd of={DST} bs=4K conv=notrunc,noerror,sync |
where:
1 2 |
{SRC} = source device id {DST} = destination device id |
for example on my system:
1 |
pv -tpreb /dev/sdc | dd of=/dev/sdb bs=4K conv=notrunc,noerror,sync |
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:
1 |
sudo gparted /dev/sdb |
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:
1 2 3 |
OS = /dev/sda SRC = /dev/sdb DST = /dev/sdc |
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:
1 2 |
sudo su pv -tpreb {SRC} | dd of={DST} bs=4K conv=notrunc,noerror,sync |
where:
1 2 |
{SRC} = source device id {DST} = destination device id |
for example on my system:
1 |
pv -tpreb /dev/sdb | dd of=/dev/sdc bs=4K conv=notrunc,noerror,sync |
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:
1 2 |
OS = /dev/sda SSD = /dev/sdb |
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):
1 2 3 4 5 6 7 |
sudo su gdisk /dev/sdb Command (? for help): x # enter Expert mode Expert command (? for help): e # relocate backup to end of disk Expert command (? for help): w # write table to disk and exit OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully. |
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:
1 2 |
sudo su cgdisk /dev/sdb |
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:
1 2 3 4 5 6 7 8 9 |
[ New ] First sector (34-2047, default = 34): <return> Size in sectors or {KMGTP} (default = 2014): <return> Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): ef02 # bios boot Current partition name is '' Enter new partition name, or to use the current name: grub_boot<return> [ Write ] |
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:
1 2 3 4 5 6 |
sudo su mount /dev/sdb1 /mnt mount --bind /dev /mnt/dev mount --bind /dev/pts /mnt/dev/pts mount --bind /proc /mnt/proc mount --bind /sys /mnt/sys |
6.2. Change root into mounted disk:
1 |
chroot /mnt |
6.3. Reinstall grub2, recheck, recreate menu:
1 2 3 |
grub-install /dev/sdb grub-install --recheck /dev/sdb update-grub |
6.4. Exit the chroot shell and unmount everything:
1 2 3 4 5 6 |
exit umount /mnt/sys umount /mnt/proc umount /mnt/dev/pts umount /mnt/dev umount /mnt |
6.5. Shutdown the computer and install the SSD into the new computer, it should now boot!