Thursday, March 31, 2011

Virtualizing a PC

All hardware is eventually decommissioned. It may be broken, stolen, or just too old and slow. However, the software on it might still be needed. Not all software is easily transferable to a new PC with the latest OS on it. This quick guide helps you convert the old machine to a virtual machine, so that you can run it in VirtualBox. The process is called physical to virtual (P2V).

This article assumes you are at home in a Linux shell.

  1. Boot the old hardware from a Ubuntu live CD or USB stick (or any other live cd, for example the gparted cd).
  2. Prepare copying each file system with the following steps:
    1. Mount the filesystem
    2. Remove the contents of /tmp and trashcans in the home directories.
    3. Clear free space with the following commands:
      sudo -s dd if=/dev/zero of=/usr/bigfile; rm -f /usr/bigfile
  3. Now copy each physical disk to the target host computer (you could also do this per partition). With nc (netcat) and dd this is easy. Netcat essentially opens a pipe between two computers. We then use dd to stream the entire disk to and from the pipe. One of the two netcat's are in listening mode, the other connects to that one. It doesn't matter which one does the listening, except I found that netcat in MacOSX does not properly support listening. To speed up the transfer, gzip is used. Because we cleared empty sections of the disk, gzip is a quite efficient addition. (Update 2013-04-15: Now I would recommend a compression program that compress/decompress faster at the cost of compression efficiency. For example lzop.)
    Here are the two examples of copying the disk /dev/sda. The first puts netcat in listen mode on the target:
    on target> nc -l 19001 | gzip -d -c | dd of=hd-copy.raw on source> dd if=/dev/sda | gzip -c --fast | nc [target] 19001
    The second example puts netcat in listen mode on the source system (execute in the given order):
    on source> dd if=/dev/sda | gzip -c --fast | nc -l 19001 on target> nc [source] 19001 | gzip -d -c | dd of=hd-copy.raw

    Make sure you have a decent network connection, 1 Gbit/s is fine.

  4. The next step is to convert the raw image to a VirtualBox image. Do this with the following command (tested with VirtualBox 4.0.4):
    VBoxManage convertfromraw hd-copy.raw hd-copy.vdi --variant Standard
  5. If there really was a lot of empty space on the disk, you can compact it with the following command:
    VBoxManage modifyhd hd-copy.vdi --compact

References

Chapter 8 of the VirtualBox manual
Another P2V technique which also works in VMWare.

No comments:

Post a Comment