Clone a laptop using NVME over TCP

Clone a laptop using NVME over TCP

I recently bought a new laptop and needed to set it up before using it. But I wasn’t in the mood to go through all the usual steps I talked about in the previous post. I complained about this to my colleague, and in response he suggested copying the entire drive to a new laptop. Although the idea seemed interesting to me, I had my doubts, which I shared with him.

  1. I don’t have the tools to open up the old laptop and connect the new drive via USB to the new laptop.

  2. I’m using full disk encryption, the old laptop has a 512GB drive and the new one is 1TB NVME; I don’t know how to change the size of LUKS.

He replied that both problems can be solved. At the first stage, you can simply access the drive with NVME over TCP, connect it over the network and create a full copy of the drive, and the rest is quite simple. In short, he offered the following:

  1. Export disk using old laptop nvmet-tcp.

  2. Create a copy of the disk on a new laptop.

  3. Resize the partition to use the entire 1 TB.

  4. Resize LUKS.

  5. Resize the BTRFS root disk.

We export the disk via NVME TCP

A colleague said that the easiest way to do this is with systemd-storagetm.service. This service can be called simply by downloading to storage-target-mode.targetpointing out rd.systemd.unit=storage-target-mode.target. But he recommended not to use it because I need to configure the dracut initrd image to enable network services and also configure WiFi from this mode, which is quite a complicated process.

So instead I just booted both laptops with the GRML rescue CD. And in order to export the NVME drive to the new laptop using the Linux nvmet-tcp module, I followed this step:

modprobe nvemt-tcp
cd /sys/kernel/config/nvmet
mkdir ports/0
cd ports/0
echo "ipv4" > addr_adrfam
echo 0.0.0.0 > addr_traaddr
echo 4420 > addr_trsvcid
echo tcp > addr_trtype
cd /sys/kernel/config/nvmet/subsystems
mkdir testnqn
echo 1 >testnqn/allow_any_host
mkdir testnqn/namespaces/1
cd testnqn
# replace the device name with the disk you want to export
echo "/dev/nvme0n1" > namespaces/1/device_path
echo 1 > namespaces/1/enable
ln -s "../../subsystems/testnqn" /sys/kernel/config/nvmet/ports/0/subsystems/testnqn

These commands ensure that the device is exported using NVME over TCP. Next, you need to recognize it on the new laptop and connect the device:

nvme discover -t tcp -a <ip> -s 4420
nvme connectl-all -t tcp -a <> -s 4420

The nvme list will show the device connected to the new laptop, and we can proceed to the next stage, which is to copy the disk.

We copy the disk

I just used the command to copy the root drive to the new laptop. Since the new laptop doesn’t have ethernet, I had to use only WiFi and it took seven and a half hours to copy all 512GB to the new laptop. Copy speed was about 18-20 MB/s. Another solution would be to create an initial partition and filesystem, then rsync the root drive or BTRFS directly to migrate the filesystem.

dd if=/dev/nvme2n1 of=/dev/nvme0n1 status=progress bs=40M

We change the size of the partition and the LUKS container

The last part was very simple. After running parted, it found that the partition table didn’t match the size of the disk and asked if I needed to fix it, to which I said yes. Next I needed to install cloud-guest-utils to get growpart to fix the second partition. The following command expanded the partition to the entire 1 TB:

growpart /dev/nvem0n1 p2

Next I used cryptsetup-resize to increase the size of the LUKS container.

cryptsetup luksOpen /dev/nvme0n1p2 ENC
cryptsetup resize ENC

Then I restarted the drive and everything worked. After logging in, I resized the BTRFS file system. Resizing BTRFS requires a system install, so I couldn’t try to do it on live boot.

btfs filesystem resize max /

Conclusion

The only gain from this process is that I now have a new laptop, and it’s like I’m still working with the old one. It usually takes a week or two to fully set up a laptop, but I managed to save all that time.

Also, thanks to my colleague, I learned how to export disks using NVME over TCP.

Related posts