Converting a Virtual Disk to Physical

Converting physical systems to virtual (P2V) is a common way to reduce overhead and conserve resources. Converting virtual to physical can be helpful too (V2P). Virtual systems are useful for testing and prototyping before installing on a physical system. The virtual disk can be converted to physical rather than installing from scratch.

With VirtualBox, converting a virtual hard disk to physical is possible with the vboxmanage command. Important to know is by default VirtualBox uses sparse files to create a virtual disk. This approach means the size of the virtual disk is not what will be the actual size of the disk.

Before converting a virtual disk to physical, use the VirtualBox GUI to note the actual disk size. Another method is within the virtual machine (VM) use fdisk -l.

The next step is ensuring the environment where the final physical disk image will be created has sufficient free disk space. Typically VirtualBox tries to create small disks when creating a new VM. For many people needing sufficient working space should not be a problem.

In some instances the final physical disk will be much larger than the original virtual sparse disk file. Consider using a large spare hard disk as a working environment.

First convert the virtual sparse disk into a fixed sized virtual disk file.

vboxmanage clonemedium disk dynamic.vdi fixed.vdi --variant Fixed

The final fixed size file likely will not match the size of the target physical disk.

One way to resolve that is to resize the file to the exact size of the target disk. Suppose the target disk is 20 GB. Use the fdisk -l command to find the actual size in bytes. For example, 20020396032 bytes:

vboxmanage modifymedium disk fixed.vdi --resizebyte 20020396032

Convert that fixed sized file into a disk image file:

vboxmanage clonemedium disk fixed.vdi fixed.img --format raw

Clone that image file to the physical target disk:

dd if=fixed.img of=/dev/sdX status=progress bs=4M conv=noerror

Where /dev/sdX is the actual target device node.

These steps can be combined when initially creating a VM. When creating the VM do not immediately create a respective disk. Create the disk manually:

vboxmanage createmedium disk --filename newdisk.vdi --sizebyte 20020396032

The virtual disk size then will be the same as the eventual target physical disk. By default the new virtual disk is dynamic rather than fixed. The sparse file size will be about 2 MB rather than 20020396032 bytes (20 GB).

After converting a virtual disk to physical be sure to run file system checks.

Posted: Category: Tutorial Tagged: Virtual Machines

Next: Monitoring CMOS Batteries

Previous: Cron Jobs On the Last Day of a Month