17

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.

2 Answers2

19

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/sdX
  2. Your bigger SSD mount point is located at /dev/sdY
  3. You are using LUKS full disk encryption
  4. Your encrypted partition is /dev/sdX1
  5. The unecrypted mount point where your file system is located is /dev/mapper/sdX1_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/sdX of=/dev/sdY 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/sdY

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 fdisk 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/sdY1 sdY1_crypt
sudo cryptsetup resize /dev/sdY1_crypt

and finally you would expand the files system using resize2fs

sudo resize2fs /dev/mapper/sdY1_crypt

Faster Method:

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

sudo cryptsetup luksFormat [OPTIONS] /dev/sdY1

and mount it on sdY1_crypt.

sudo cryptsetup luksOpen /dev/sdY1 sdY1_crypt

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

sudo dd if=/dev/mapper/sdX1_crypt of=/dev/mapper/sdY1_crypt bs=64k

then expand the file system with resize2fs

sudo resize2fs /dev/mapper/sdY1_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/sdY

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.

Cybernaut
  • 306
  • 1
    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
  • 1
    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
  • 3
    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 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 05:41
  • recommend you update your answer with sdX and sdY, so someone copy paste failing wont hose their system. – Kevin Nov 22 '18 at 05:53
5

I'd like to add some information to this thread that could help others.

As of todays date (Oct 27th 2019) I can assert the easiest method for others trying to clone a full disc encrypted system (LVM on LUKS) will be a combination of clonezilla and gparted.

Use a live USB of clonezilla and drop to a shell when first booted, decrypt both disks with cryptsetup -v luksOpen /dev/sdX unique-name for each disk.

Re-enter clonezilla's menu by exiting the shell prompt (Ctrl -D or just exit)

Then start a disk-to-disk transfer, for my 2 SSD's (one hooked up by usb external caddy) of 500Gb and 1Tb it took around 3 hours.

Reboot to the newly cloned disk to check it functions then boot from a gparted live cd / usb. As of Gparted 0.28 it now supports resizing luks partitions, grow the partition after decrypting and voila!

IT JUST WORKS

  • 1
    Your update suggests to...

    Use a live USB of clonezilla and drop to a shell when first booted, decrypt both disks with cryptsetup -v luksOpen /dev/sdX unique-name for each disk.

    I have looked up the man page for cryptsetup, but still do not understand what for each disk and unique name mean in this context. I suppose this is the name as it appears in the mapper?

    Could you please edit your update to include an explanation. This would make your answer useful to me.

    I have output for lsblk --fs, but no space here to put it. Is any of that useful for this context? –

    – Jörn Guy Süß Aug 13 '21 at 03:28