virtual-machinevmware-workstationvmdk

Virtualize a HDD (dd image) into a Smaller VMware Image


I salvaged the HDD from my old desktop, and would like to virtualize it to run under VMware Workstation.

The problem is, the HDD (with several partitions) is 1 TB in size, and when I tried to clone it to an image (using dd), the resulting image is also 1 TB, and I will have problem maintaining a VM of that size.

I know that when creating a new Workstation VM, there is an option to not allocate all the space immediately.

How can I virtualize the HDD and "deflate" the unused parts of the HDD?


Solution

  • Managed to get a "deflated" VMDK file by doing the following.

    1. Mount each partition of the HDD

      e.g. mount -t ext4 /dev/sda1 /mnt/tmp

    2. For each partition, fill up empty space with zeros.

      e.g. dd if=/dev/zero of=/mnt/tmp/ZERO.TMP

    3. Delete the zeros file.

    4. Clone an image of the entire HDD

      e.g. dd if=/dev/sda of=/tmp/image.img

    5. Make a sparse copy of the image file.

      e.g. cp --sparse=always /tmp/image.img /tmp/image_sparse.img

    6. Use qemu-img to make a VMDK file from the sparse image file.

      e.g. qemu-img convert -O vmdk image_sparse.img image_sparse.vmdk