93

Is is possible to convert a .vdi file into a .iso that can be burned to a cd or dvd and make it like an installer. Or Is it possible to convert virtual machines to physical environments?

quack quixote
  • 43,504
Wern Ancheta
  • 6,620

9 Answers9

74

Yes, and no.

You can convert a VDI into a disk image with the VBoxManage tool. This command clones a registered virtual disk image to another image file. If you want to convert your virtual disk to another format, this is the official VirtualBox tool to use[*].

VBoxManage clonehd file.vdi output.img --format RAW
  • If you're using a dynamic VDI, and you have an older version of VirtualBox, clonehd may not operate properly. VBoxManage's internalcommands tool includes converttoraw, which can convert a dynamic VDI into a raw disk image (source)[+].

VBoxManage internalcommands converttoraw file.vdi output.img

But... that output IMG file isn't an ISO image, and the OS that's installed will not be configured to run from a bootable CD/DVD. You can "burn" (write) the IMG onto a hard drive, and it might boot on bare hardware (eg not in a virtual machine). But it might not, because the OS installed on that IMG is expecting to see the virtual hardware that VirtualBox provides, and you're booting it on real hardware that it isn't expecting.

Some versions of Windows do not handle this situation well; some Linux distributions do. It is sometimes possible to configure an OS (beforehand or afterwards) to migrate it from one environment to the other like this, but specific steps depend completely on the OS being migrated.


On Windows, you may need to specify the full path to the program:

"C:\Program Files\Sun\VirtualBox\VBoxManage.exe" [...]

Add C:\Program Files\Sun\VirtualBox to your PATH to use the short version.


[*] I'm assuming the "--format RAW" option will convert to a standard disk image, as if you'd used the dd command on a physical harddrive. But frankly, I haven't found any documentation that backs this up, so be aware this may not be correct.

[+] I've just tested both commands under VirtualBox 3.1.2. Both output files are identical according to md5sum, but I haven't fully tested the output files.

See also the "All about VDIs" tutorial at the VirtualBox forums.

quack quixote
  • 43,504
22

If your vdi file contains partitions and you want to extract only one of them use the following:

First, as quack quixote said before, convert the vdi file to a raw image file:

# VBoxManage clonehd file.vdi file.raw --format RAW

Then set up a loop device for the image:

# loopdev=$(losetup --show -r -f file.raw)

Use kpartx to create devices for each partition in the raw file:

# apt-get install kpartx
# kpartx -a $loopdev

See which devices we have now. In this example, there's only one device as there's only one partition in the raw image:

# ls /dev/mapper/loop*
/dev/mapper/loop0p1

Now mount it to verify that all works properly:

# mkdir /mnt/part1
# mount /dev/mapper/loop0p1 /mnt/part1
# ls /mnt/part1
# umount /mnt/part1

Use dd to copy the partition contents to a another target partition:

# dd if=/dev/mapper/loop0p1 of=/dev/sda2 bs=1M

After you're done remove the device mappings again:

# kpartx -d file.raw
mxlian
  • 103
  • 7
dmoebius
  • 321
13

The answer is a definite yes, in case your host and guest system is Linux. It's done with the packages qemu and TKLPatch. You can use both VDI or VMDK files.

Read more:

Mateng
  • 107
8

I tried the VBoxManage clonehd file.vdi output.img --format RAW from the accepted answer but without much success.

What worked for me with a Windows 8.1 virtual machine on a Windows 7 host is this:

  1. Make a Windows 8.1 machine in VirtualBox, install and use is as you would normally do with a VM.
  2. Convert the Virtual Box .vdi hard disk to Windows VHD with VBoxManage clonehd source.vdi target.vhd --format vhd
  3. Mount the VHD in the host Windows machine as a drive (you can mount a VHD in the disk manager in Computer Management.
  4. Clone the VHD (mounted as a drive) to a another new hard disk (a USB attached laptop HD in my case) DriveImage XML or a similar disk cloning tool. You now basically have cloned the virtual hard disk .vdi to bare metal.
  5. Install the new hard disk in a machine. (in my case swap the laptop HDD)
  6. Insert a Windows 8.1 install USB/CD, boot from it, choose Advanced Options and Automatic Repair. The automatic repair will make the new hard disk bootable.
  7. Boot from the new hard disk with the cloned Windows 8.1 and run Windows Update. This will download drivers for your computer's hardware which were not know when you ran Windows in VirtualBox. You will have to install missing drivers manually if any.
  8. Uninstall VirtualBox Guest Additions. Optionally install VirtualBox so that your bare metal Windows 8.1 can be a VirtualBox host.
Jan H
  • 211
3

I do this process in a very fast, with just two lines of command in Linux:

#VBoxManage internalcommands converttoraw your_disk_virtual.vmdk your_disk_virtual.img

OR

#VBoxManage internalcommands converttoraw your_disk_virtual.vdi your_disk_virtual.img

# mv your_disk_virtual.img your_disk_virtual.iso

My full article: http://www.previsioni.com.br/jailsonjan/?p=389

jonsca
  • 4,084
JailsonJan
  • 31
  • 1
0

For newer Virtualboxes the VBoxManage command changed a litte.

Now the correct syntax for cloning a vdi file to raw is

VBoxManage clonemedium inputfile.vdi outputfile.img --RAW

Be patient during the conversion!

Note: For compatibility with earlier versions of Oracle VM VirtualBox, the clonevdi and clonehd commands are still supported and mapped internally to the clonemedium command.

abu_bua
  • 558
  • 9
  • 9
0

The titular question is whether it is possible to convert a VM to a physical drive (V2P). The answer is yes.

Several answers to this question take an external approach. That is, treat the VM like a single file (i.e., a VDI), and convert it as such. External approaches seem to have been less successful overall. They certainly were for me.

Most V2P efforts use internal approaches. These involve running software or a command inside the VM. Relevant software may be installed in the VM (e.g., Systemback), or it may pre-empt the installed system at boot time. For example, the user may attach a Clonezilla ISO to the VM, interrupt bootup to prioritize the Clonezilla ISO, and use that to image the installed system. As a different example, I have had good results using a Lubuntu ISO inside an Ubuntu VM -- again, preempting the bootup of the Ubuntu system -- to run dd commands to convert that Ubuntu VM to a physical environment, either through direct cloning or through creating an image file.

If you can run the VDI as a VM, I think you will find better results from an internal solution than from an external conversion effort.

0

On Ray Woodcock's edit over abu_bua's message (erroneous for me in 2023 on VirtualBox 6.1), VBoxManage clonemedium inputfile.vdi outputfile.img --RAW

should read --format=RAW (both "format" and "=" required)

VBoxManage clonemedium vdisk.vdi disk.img --format=RAW

optionally identical output (checked with diff) with (from Basj superuser.com/questions/1721151/from-virtualbox-vm-vdi-file-to-a-usb-bootable-iso-image)

VBoxManage internalcommands converttoraw file.vdi output.img

skipping the deprecated version become alias (no "=" before RAW)

VBoxManage clonehd file.vdi file.raw --format RAW

Sidenote: Debian11 Etecher-generated usb-key with Monterey12.6.4/MacbookPro2015 failed MackbookAir2014 boot after above instructions.

(insufficient reps to comment, ignorant about platform-PM Woodcock)

-1

Use the following command to convert:

VBoxManage clonehd CactiEZ-disk1.vmdk output.iso --format VMDK

Here is the output:

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VMDK'. UUID: 7f297ac6-95eb-4814-9237-1acfd6be976c
Gareth
  • 19,080
Anupam
  • 1