Copy a Hard Drive to Any VM Anywhere with SSH
Have you ever wanted to import an OVA into services like Linode, Vultr, AWS, and others? Have you ever wanted to get your VM off of one of those services? Well let me introduce the magic of SSH, dd, and Gzip.
To get started, you need to boot Linux on both VM’s and have root access. To make it easy, you can boot Ubuntu live CD’s on them. The machine you are copying to (the new one) needs to be accessible on the open internet.
On the live CD on the new machine, type the following:
sudo -i
passwd
And input a somewhat secure password.
Next, edit the sshd config to allow root login:
nano /etc/ssh/sshd_config
Then set
PermitRootLogin yes
Save and exit with control+x.
Next, start the SSH daemon:
systemctl start sshd
Now you should be able to connect to the new machine. On the old machine, try to ssh in to the box with the root user and the password you set.
Next, you need to check the drive labels for both the old and the new machine, fdisk -l
will help you with that. Finally, on the old machine, run this long command, and it will begin the disk transfer:
dd if=/dev/OldDrive | gzip -c | ssh root@NewIP 'gunzip - | dd of=/dev/NewDrive status=progress'
Once it’s done, you should be able to boot that machine.
Bootloaders can potentially end up causing problems. If the old machine is UEFI, the new one will have to be as well or it won’t boot. BIOS to UEFI will *probably* work, but could end up causing its own set of problems. If you clone the disk and it does not boot properly at first, see if you have the ability Another thing to keep in mind is what virtualization technology your hosting provider is using, so that you can install the proper drivers and/or guest agent for that provider.
You can also clone the old disk to an img file, which can then be read to other disks later. This can be done by modifying the above command slightly to be:
dd if=/dev/OldDrive | gzip -c | ssh NewComputer@NewIP 'gunzip - | dd of=/path/somewhere/clone.img status=progress'
You can also do this the other direction, with the new machine connecting to the old one:
ssh root@OldIP 'dd if=/dev/OldDrive | gzip -c' | gunzip - | dd of=/path/somewhere/clone.img status=progress
Giving dd the .img file as the input will allow you to restore from this backup. I recommend gzip-ing it if you plan to keep it for a while.
If you have issues or questions, feel free to post below.
One thought on “Copy a Hard Drive to Any VM Anywhere with SSH”