[How To] Port your Encrypted Btrfs System Disk to a New Disk (Using DD-Copy)

Preclaimer: Do under no circumstances mount the dd-copied new btrfs disk and the old btrfs disk at the same time because both file systems would become badly damaged!

Scenario:
I had used the standard Calamares encrypted install of Manjaro KDE Plasma stable:
btrfs with a swap partition (so hibernation works).
Now I want to use another NVME or SSD or HDD as the system drive. In my case it is bigger.
In any case the size should be at least equal to the old NVME or SSD or HDD

Install your new drive in your device. Check if there is a faster connection slot available on your motherboard in case of an m.2-NVME.

Create a backup copy of your home folder.

Boot into a live system and there dd-copy the whole drive to an image on an external drive. Do not decrypt or mount the system drive! The OLD system drive here is assumed to be /dev/nvmeXnY, in my case, /dev/nvme0n1
YOU NEED TO ADJUST THE DEVICE NAMES

sudo dd if=/dev/nvmeXnY of=/run/media/EXTERNALDEVICENAME/dd-image status=progress

Once this has finished, open KDE Partition Manager and for your /dev/nvmeXnY create a new partition table. THIS WILL DELETE YOUR SYSTEM DRIVE (but you now have a backup image). Hit apply. Inside it, create an new unformatted partition. Hit apply.

Then select your NEW disk in KDE Partition Manager YOU NEED TO ADJUST THE DEVICE NAME:
/dev/nvmeZnA – In my case it is /dev/nvme1n1.
There, create a new partition table, gpt. This again will delete the data if there is any. Hit apply. Inside it, create an new unformatted partition. Hit apply.

You can skip the following step if your new drive is exact the exact size of the old drive:

sudo sfdisk --dump /dev/nvmeZnA > /run/media/EXTERNALDEVICENAME/partition_table_newdisk_unformatted

You may want to fill your NEW system disk with random data:

sudo cryptsetup -y -v luksFormat /dev/nvmeZnA
# Type YES in capitals
# Enter your password twice. You will need it just once.
sudo pacman -Sy pv
sudo cryptsetup luksOpen /dev/nvmeZnA whatever
sudo su
pv -tpreb /dev/zero | dd of=/dev/mapper/whatever bs=128M
exit
sudo cryptsetup luksClose whatever

DD copy the image to the new disk:

sudo dd if=/run/media/EXTERNALDEVICENAME/dd-image of=/dev/nvmeZnA status=progress

You can skip the following steps ((continue here*)) if your new drive is exact the exact size of the old drive:

sudo sfdisk --dump /dev/nvmeZnA > /run/media/EXTERNALDEVICENAME/partition_table_after_dd

Now we will look at the two files partition_table_after_dd and partition_table_newdisk_unformatted:

Look at line the following line of partition_table_newdisk_unformatted:
last-lba: LONGERNUMBER
Take that longer number and copy-paste it into partition_table_after_dd and replace
“last-lba: SHORTERNUMBER”
Save the file.

sudo sfdisk /dev/nvmeZnA < /run/media/EXTERNALDEVICENAME/partition_table_after_dd

This will just correct the disk size (resp. it’s available size).

*Next follow the steps in

to reinstall you GRUB.

In my case (gpt, luks, btrfs and UEFI) the commands were (again, ADJUST DEVICE NAMES):

user $ lsblk -o PATH,PTTYPE,PARTTYPE,FSTYPE,PARTTYPENAME
user $ sudo fdisk -l /dev/nvmeZnA
user $ sudo cryptsetup luksOpen /dev/nvmeZnAp2 cryptroot
user $ su 
root # mount -o subvol=@ /dev/mapper/cryptroot /mnt 
root # mount /dev/nvmeZnAp1 /mnt/boot/efi

Create the chroot environment and use bash as shell

root # manjaro-chroot /mnt /bin/bash 
root # grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=manjaro --recheck
root # grub-mkconfig -o /boot/grub/grub.cfg 

Verify the existence of an EFI system partition

root # lsblk -o PATH,PTTYPE,PARTTYPE,FSTYPE,PARTTYPENAME COPY TO CLIPBOARD

Verify the efi filesystem is loaded

root # ls /sys/firmware/efi 
root # exit

Try loading the efi filesystem

root # modprobe efivarfs 

Re-enter chroot

root # manjaro-chroot /mnt /bin/bash 

Then mount the efi filesystem

root # mount -t efivarfs efivarfs /sys/firmware/efi/efivars 

Verify the efi filesystem is loaded

root # ls /sys/firmware/efi 
exit
exit
reboot

Snapshots and hibernation will continue working.

1 Like