7

I am looking for ways to remotely clone a live server into a virtual machine, in order to use it as a test environment that is as identical as possible to the live server. This would be used to test changes, new software versions, etc. The clone would need to contain the boot manager, partition info, etc.

What are ways I could accomplish this with a minimum of impact on the live server, while producing a working image that I can boot from (assuming it is mounted in the proper way or converted to an image after the clone)?

The live server runs Centos 6.7, and I'm planning to use Virtualbox to run the clone. I am doing this from a Mac with Homebrew, bonus points if the answer will work from a Mac but can use *nix if needed.

I've tried using rsync and creating an image from the resulting folder, but it was missing partition & boot info so I couldn't use the resulting image in Virtualbox without first installing centos then copying over all the files - I felt this wouldn't be faithful enough for a test environment.

Update: I've tried using the VMware converter but unfortunately it only seems available for windows and I'm not able to use it.

Instead, I'm downloading an image of the server by using dd over ssh. I plan to convert this image once it's downloaded into a .vdi using virtuablox's command line utility. If this works I'll update the question with more details.

  • 1
    since you're running the VM on a remote linux server, i'd recommend using kvm rather than virtualbox. virtualbox is nice for running some vms on a user's desktop, but kvm is better suited to servers. You can use virt-manager on your Mac if you want a GUI connection to manage the VM or use its console (e.g. see github.com/jeffreywildman/homebrew-virt-manager). kvm + qemu + libvirt. they can use the raw dd image directly, or you can convert it to a compressed, snapshottable qcow2 image file with qemu-img – cas Aug 4 '16 at 9:07
  • actually, you can use qemu-img rather than dd if you want, to directly read the block device(s) of the live server and create qcow2 or other image file types (incl. vdi, vmdk, raw, and more), . see qemu-img --help or the man page for details. btw, it's best to clone the live server while it's not running. – cas Aug 4 '16 at 9:14
  • You do have good backups of your production system right? Great time to find out... and as a bonus, you'll end up with the clone you were asking for. If not, well, fix your backup system/method first! – ivanivan Sep 30 '18 at 1:31
7

Physical to virtual - P2V - is the procedure you are looking for. rsync will not help in this case.

You should use a Vmware's free VMware Converter tool to convert the disks of your physical server to virtual disks, create a virtual machine in VirtualBox and add the existing virtual disks to it.
You'll find a complete step-by-step description here.

  • 2
    +1. actually rsync can be used, but you have to boot the VM with a live CD image, create, format, and mount any partitions on the VM's virtual disk(s), rsync to those partitions, and then chroot into the system and use grub-install to make it bootable. moderately complicated but a LOT less downtime for the live server. – cas Aug 4 '16 at 9:17
  • actually yes - this is the case when rsync would be useful – mazs Aug 4 '16 at 14:41
  • I have used rsync to perform P2V in the past. Works well. See my linked answer in this question. – Kyle Oct 20 at 10:50
0
  1. Start in rescue mode
  2. Type this command to connect and download server disk and save it to client location

    ssh root@xx.xx.xx.xx "dd if=/dev/sda " | dd of=/home/backups/centos_image.img If you get connection refused, make sure that you add sshd in the reboot cron job and enable it to run in debug mode. If you want to use windows, make sure you have Cygwin

  3. After you download the .img, you will see the following output

    xxxxxxxxxx bytes (x.x GB) copied, xxx.xxx s, x.x MB/s

  4. Now use VBoxManage to convert it to .vdi

    VBoxManage convertfromraw --format VDI [filename].img [filename].vdi

  5. Mount the VDI as a hard disk

0

I wrote a step-by-step detailed answer of how I solved a very similar challenge on the question: Turning a running Linux system into a KVM instance on another machine. I hope it proves a useful answer for this question too.

Goal of the answer: to take a physical Linux P node running live-production and virtualise it. Without having to create and allocate multi terabyte disks, nor have to use md raid in the V guest, because the target hypervisor (Proxmox 5) used ZoL/ZFS. Also wanted to mitigate downtime/reboots on the running P node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.