Virtualizing a physical Linux machine


March 30, 2009

Due to some hardware trouble with my main work machine, I’m presently working in a virtual machine on my personal computer. After a few dim trails, I found a pretty straightforward method to clone my work computer into a virtual machine image, so that I am able to work in the exact same environment I would have on my physical work computer. Here’s how to do it:

  1. Clone the drive using dd (the following example assumes your drive is /dev/sda and you have an external drive mounted at /media/removable:

    [root@localhost]# dd if=/dev/sda of=/media/removable/sda.img bs=1024

  2. Use qemu-img to convert the raw bits of the drive to an image in the appropriate format for the virtual machine monitor you want to use (QEMU or VMWare):

    [root@localhost]# echo This converts sda.img to a QEMU disk image [root@localhost]# qemu-img convert -f raw sda.img -O qcow sda-qcow.img

    [root@localhost]# echo This converts sda.img to a vmware disk image [root@localhost]# qemu-img convert -f raw sda.img -O vmdk sda.vmdk

  3. Create a new virtual machine that uses this drive image, using the interface for your preferred virtual machine monitor.

I was able to image and convert a 100 gb drive in around six hours. My drive was an LVM volume and the home partition was encrypted with LUKS; I was delighted to see that qemu-img handled these oddball features of my drive flawlessly. (I can’t think of a technical reason why these wouldn’t be supported, but I’m nonetheless inclined to be pleasantly surprised when things work as they should out of the box.)