Copying a Running Physical Server to Virtual Box
I have a server running Ubuntu 10.04. As I want to test some maintenance work, such as upgrading to 12.04, I thought I should create a VM copy of the server using Virtual Box. I don't have a physical access to the server, so I considered the following options: I do
-
dd
'ing the entire hard disk to a raw image on my pc and then creating a VDI out of the rawdd
image. - Installing the same ubuntu version, install update and mount the VDI locally and rsync the filesystem from the server to the mounted VDI creating a logical copy of the server.
The rsync solution is a bit more complicated, but seems to save bandwidth, and allows me to create a "copy" of the server with slightly different configuration (e.g. smaller disk).
What do you think is the best way to do so? One of the above methods? A different one?
Solution 1:
Using rsync
directly turned out to be more problematic than I first thought:
- The command needs to be run as root on the remote machine.
- I didn't won't to enable remote root login.
- The filesystem in based on LVM and it was a hassle to mount the actual root partition of the VM from inside the LVM in the VDI file.
For these reasons I ended up with a 2 step process:
- Copy all the files from the remote machine to the local host machine.
- Copy the files to the guest.
I finally went with basic tar
, although I could have used rdiffdir
provided by duplicity
to save some bandwidth.
I started out by setting up an SSH tunnel which I could use to tunnel the tar
files so I wouldn't have to write the archive to the filesystem I was copying.
local$ ssh user@remote -R 3000:localhost:3000
loacl$ nc -l 3000 > filesystem.tar.gz
remote$ sudo tar -vcz / --exclude={/dev,/proc,/sys,/tmp} | nc localhost 3000
This created a tar archive of the entire filesystem in my local host machine. The next step was to untar it on the guest:
guest$ cd /
guest$ nc -l 3000 | sudo tar -xvz
local$ nc guest_ip 3000 < filesystem.tar.gz
In my case /etc/fstab
referenced filesystems by their UUIDs, so I had to update it with the output of guest$ sudo blkid
. I also had to update the networking settings in /etc/udev/rules.d/70-persistent-net.rules
(interface names) and /etc/network/interfaces
(ip addresses).
Solution 2:
Admittedly the dd
-approach or creating an otherwise image of the server is likely to have bandwith issues in case you only have remote access to the server.
Installing Ubuntu 10.04 server to a VDI only to be able to copy an existing installation also is not really needed (but it may save some bandwith).
Let me suggest the following approach:
- Create a Virtual Machine with an empty virtual hard disk of desired size.
- Boot the VM with any live (Ubuntu) system from an .iso file.
- Partition the hard drive (include boot flag and format with the same file system your old server has).
- Mount this partition to your live environment.
-
Copy all files from the old server to the yet empty partition of your VM
rsync -avze ssh user@host:/ /<mountpoint>
Install Grub resp. Grub2 to the MBR of the now populated VDI drive.
- Reboot.