4

I currently have a 128GB SSD. Its using encryption that comes with Ubuntu.

What's the best way to clone this to another SSD that's bigger and then expand the partition to be bigger to fit this new larger SSD?

Can I just clone the disk like for like, and then use something like Gparted to expand the partition? Or does it not work like that with the encryption ?

Any help would be appreciated.

5

Assumptions:

Because I cannot comment on your post I am going to have to assume some things:

  1. Your SSD mount point is located at /dev/sda
  2. Your bigger SSD mount point is located at /dev/sdb
  3. You are using LUKS full disk encryption
  4. Your encrypted partition is /dev/sda1
  5. The unecrypted mount point where your file system is located is /dev/mapper/sda1_crypt and it is using an ext4 file system

Easier Method:

The easiest and slowest way would be to use dd

sudo dd  if=/dev/sda of=/dev/sdb bs=64k  

to copy every byte from the smaller SSD to the larger SSD. This would give you a fully boot-able system you would have to disconnect the smaller SSD in order to boot because both SSDs share the same UUID which the system uses to identify individual disks. Before booting you would expand the physical partition using fdisk.

sudo fdisk /dev/sdb

fdisk is an interactive tool, you would first delete the partition LUKS is on (because its ending address is shorter than the new bigger SSD), then you would create a new partition (the defaults fdisks uses will fill all unused space), then save (this is how software "extends" a partition). then you would expand the LUKS container using cryptsetup

sudo cryptsetup luksOpen /dev/sdb1 sdb1_crypt
sudo cryptsetup resize /dev/sdb1_crypt

and finally you would expand the files system using resize2fs

sudo resize2fs /dev/mapper/sdb1_crypt

Faster Method:

A faster and more complex method would be to create a new partition sdb1 with fdisk on the bigger SSD for the encrypted volume, then create a new LUKS volume using

sudo cryptsetup luksFormat [OPTIONS] /dev/sdb1

and mount it on sdb1_crypt.

sudo cryptsetup luksOpen /dev/sdb1 sdb1_crypt

then use dd to copy the unencrypted file system from one encryption volume to another.

sudo dd if=/dev/mapper/sda1_crypt of=/dev/mapper/sdb1_crypt bs=64k

then expand the file system with resize2fs

sudo resize2fs /dev/mapper/sdb1_crypt

In order to be able to boot from the new bigger SSD you would have to install grub in the MBR of the SSD with

grub-install [OPTIONS] /dev/sdb

Note:

All the commands used are highly configurable and you will want to use some of their options because you know your system better than I do, use man <command> or <command> --help to learn about what they can do.

  • The first method has some issues. sudo cryptsetup resize /dev/sdb1 fails with "Device sdb1 not found". one must first sudo cryptsetup luksOpen /dev/sdb1 sdb1_crypt and then use sudo cryptsetup resize sdb1_crypt. Also after that resize2fs fails with "Bad magic number in super-block while trying to open". I'm not sure the cause but could it be because the original drive is currently mounted (we are dd'ing from that system) and has the same uuid as the new cloned drive? – cyphunk Dec 21 '17 at 16:04
  • If anyone uses that route they may try with a second device or bootable cd for conducting the resize steps – cyphunk Dec 21 '17 at 16:16
  • 1
    Okay final follow up. I was able to get the first method working. I used gparted so before continuing install gparted on your system. Then after after executing the dd command to copy the root drive to the new external drive, shutdown, remove the original drive and put in the new drive. Boot to the new drive. Use gparted to resize root parition. Then: sudo cryptsetup resize <name> sudo pvresize /dev/mapper/<name> sudo lvresize -l +100%FREE /dev/mapper/<name> sudo resize2fs -p /dev/mapper/<name> (in my case <name> was name was vg0-root) – cyphunk Dec 21 '17 at 17:05
  • @cyphunk Thank you for pointing out LVM. While I did not include it in my set of assumptions I should have known because the OP mentioned "using encryption that comes with Ubuntu." – Cybernaut Jan 30 '18 at 23:55
  • Instructions still work, with some tweaks, for GPT disks, with Ubuntu 18.04. – sergtech Jul 21 '18 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.