Sunday, December 16, 2012

Migrate physical PC to Oracle Virtualbox

Some day ago we had to return back a old desktop computer. In this computer we had software and configuration we rarely use. We decide to don’t clone the PC in another physical computer because didn’t use this desktop very often. We decide to create a virtual machine to start when needed. We could creare a new VM with a fresh Windows XP installation, but install and configure all the software we had was too long. So we decide to clone the physical machine.

There are two easy way to clone a physical machine in a virtual machine for free. Those two method create a copy of your hard-drive to use with you favorite VM software: Microsoft Virtual PC, VMware, Oracle Virtualbox (with some edit with KVM, XEN too)

The first one is Disk2vhd from Microsoft. This sofware works only on Microsoft OS, and need a running Microsoft OS to work. You can use the computer you want to clone to run the program an save the images on another hard-drive (network, usb). I think you can connect the hard drive you want to clone to another pc and clone it, but I never tried.
I tried to use this method, but the program wasn't able to start on the Desktop. It worked fine with my laptop.

The second one is WmWare Converter
I don’t know much about this software. I know it’s free but I had to give them my email and accept something to use it so I quit before start.

I decided to use a third way. It (should) works with all kind of OS.

What we need:
Second computer
Big hard-drive.
usb-key or cd-rom with a live version of Linux

Step 1 Clone the hard drive

We need to clone the hard drive, to do that I used Linux command DD. It’s a very powerful and  dangerous command. It can clone hard drive, cd-rom or just file byte to byte.
Can be dangerous because if you set wrong the source and the target you might overwrite the data you want to clone.

the command is: dd if=(source) of=(target)

DD will clone every single byte of your hard drive, even the free space, so it will create a image as big as your hard drive. So be sure you are going to have a usb or network hardrive big enough to contain this image.

I started a live distro on the pc i wanted to clone.
Opened a terminal
and used DD.

dd if=/dev/sda of=/<path>/clone_sda.img

I didn’t had a usb hardrive so I decided to copy over SSH:


dd if=/dev/sda | ssh username@backupserver "dd of=/<path>/clone_sda.img"

Step 2 Create VDI virtual hard drive

This and next step need Virtualbox.

Pretty straightforward:

VBoxManage convertfromraw clone_sda.img clone_sda.vdi --format VDI

* Step 1 and 2 can be pipelined, bu you need some extrasteps check

Step 3 Compress VDI virtual hard drive

To compress the VDI image you have to “wipe” all free space.
When you delete a file in your harddrive you don’t overwrite the data of your file with ZEROES but you just “forgot” you you put the data. 
The compression just trows away all the zeroes we have in our hardrive. So we must wipe the free space before launch the program.


After the wipe the harddrive: 

VBoxManage modifyvdi <hd_path> compact