147

Aka: How can I convert a .vdi or .vmdk to a real installation on a physical disk?

Is it possible/feasible to migrate a Virtual Machine installation of Ubuntu onto a physical machine? If possible, how difficult would this be to accomplish and what steps would i need to take to prepare for the migration.

I'd like to do some testing on a VM and if it works out migrate that system to physical hardware instead of reinstalling everything. Is this possible?

1

5 Answers 5

115

Yes, it's possible. It's not even that hard, it just takes some time, a Ubuntu LiveCD, sticky-back-plastic, and an external USB disk (if you don't have more than one internal disks).

Preliminary step: Convert the disk to something useful

Both VMWare and VirtualBox (amongst others) use disk formats that don't lend themselves well to being written directly to disk. You can but I personally feel it's more convenient to write it to an intermediary, standard image first. You can do this from your current system without having to boot to a LiveCD.

  • Load a terminal and fire in:

    cd /media/wherever-the-image-is/
    
    # for VMWare
    sudo apt-get install qemu-kvm
    qemu-img convert your-vmware-disk.vmdk -O raw disk.img
    
    # For VirtualBox
    VBoxManage clonehd your-virtualbox-disk.vdi disk.img --format RAW
    
  • Move /media/wherever-the-image-is/disk.img somewhere that you're not about to write to. If you're planning to write it to the disk that it's currently sitting on, you'll want to stick it on a separate internal disk, or, worst-comes-to-worst an external disk.

  • The following instructions assume you've moved it to /media/dave/disk.img (dave is an external USB disk)

  • Before you do any serious writing, make sure you have backups. It's a cliche thing to say but one typo and there's a very real possibility you'll nuke your system. Assume that things will go wrong and be prepared. CloneZilla can help you take whole disk backups if you have somewhere for that data to be stored.

Write the image to a disk of its very own

You'll want to do something like this. This assumes you're going to overwrite a whole disk. If you want to do an alongside-Windows install, don't follow these instructions! Skip to after the bullets.

  • Boot into an Ubuntu Live CD and click Try Ubuntu.

  • Mount your the place where your vmdk image is being stored (eg the external USB disk as /media/dave). Do not mount the place where you want to write to.

  • Then we go to work:

    sudo dd if=/media/dave/disk.img of=/dev/sdX
    

    You want to replace sdX with the correct path to your destination disk. The sudo password is blank, just hit return.

  • You can then open gparted or something else and you should see your Ubuntu partition sitting on the disk. You should be able to expand it out.

Write the image to a disk alongside another operating system

This is perhaps an altogether safer way of doing things. The idea is very similar apart from you do a proper install of Ubuntu and then just sync over the files from disk.img.

  • Your LiveCD this time will need to be the same version of Ubuntu as your virtual install. Boot into the Live CD and, again, click Install.

  • Follow the installer through, repartition things as you see fit. About 10 minutes later, you'll be installed and it'll ask you to reboot. Don't reboot. Doesn't matter if you accidentally do, just make sure you're back in the LiveCD for the next instructions.

  • Mount your new install's partition and your external disk where you've stored the disk.img from earlier (just double click them in nautilus).

  • Mount your disk.img ISO in a terminal:

    sudo mkdir /media/oldinstall
    sudo mount -o loop /media/dave/disk.img /media/oldinstall
    

    -Note - After creating the desired .img file, sometimes when you try to mount it as a loop device you might get the following error

    "NTFS signature is missing. Failed to mount '/dev/loop0': Invalid argument The device '/dev/loop0' doesn't seem to have a valid NTFS. "

    A .img file sometimes contains a master boot record and a partition table before the actual partition that you might want to mount. Hence you need to find out the offset of the required partition. To this, one can use the parted tool. For an example, look at the answer by sisco311 in this link http://ubuntuforums.org/archive/index.php/t-1576011.html

    So, if you have determined your offset to be 'xxx' then you can mount your partition using

    sudo mount -o loop,offset=xxx /media/dave/disk.img /media/oldinstall
    
  • From there you can either cherry-pick files or just copy over everything on top of your new Ubuntu install using something like:

    sudo rsync /media/oldinstall/ /media/my-new-install/
    
  • Reboot and you should be greeted with your old VMWare install, but on bare metal. If you get nasty grub issues, you can fix it by going back to Live CD, chrooting and fixing things.

6
  • 7
    Are you aware of a tool that does this automatically?
    – the_drow
    Sep 8, 2013 at 10:55
  • 1
    For setups on Windows/VirtualBox, the VBoxManage internalcommands converttoraw your-virtualbox-disk.vdi /dev/sdX command will still work. All that is needed is to cd to Program Files\Oracle\VirtualBox before running the first command.
    – Aust
    Dec 18, 2014 at 18:06
  • 3
    @Aust I doubt that /dev/sdX is valid target in Windows. AFAIK it will be \\Device\Harddisk0 or something like this.
    – Danatela
    Dec 28, 2015 at 4:31
  • 5
    VBoxManage internalcommands converttoraw your-virtualbox-disk.vdi /dev/sdX doesn't work. In Linux it gives the error: VBoxManage: error: Cannot create destination file "/dev/sdX": VERR_ALREADY_EXISTS. It's necessary to use the VBoxManage command to send it to an image first. Then use DD to put the image to the physical disk. Apr 9, 2016 at 15:18
  • 1
    I just did that now with Xubuntu 19.04 and there are more needed steps: 1) on live cd go to new install disk and edit /etc/fstab, alter mount points of root and swap drives to existing ones (UUID's can be found by sudo fdisk -l and ls -lA /dev/disk/by-uuid 2) fixing grub is not that easy in my case, best fix was using a tool for it - help.ubuntu.com/community/Boot-Repair which fixed it Nov 1, 2019 at 10:43
10

Maybe it's not exactly what you are asking for, but it may accomplish what you want to do.

Since all your settings are stored in your home directory, you can just make a backup copy of it to another partition on a real disk. After you installed a fresh Ubuntu on a real disk, just run backup program once again to restore your home directory with all setting intact.

You can use a really neat backup program called Déjà Dup.

Info from ubuntu software center:

Déjà Dup is a simple backup tool. It hides the complexity of backing up the Right Way (encrypted, off-site, and regular) and uses duplicity as the backend.

Features:

  • Support for local, remote, or cloud backup locations, such as Amazon S3 or Rackspace Cloud Files

  • Securely encrypts and compresses your data

  • Incrementally backs up, letting you restore from any particular backup

  • Schedules regular backups

  • Integrates well into your GNOME desktop

enter image description here

You will be done in less than two hours!

good luck!

2
  • 1
    Sounds easy. Can s/o confirm it really works that way?
    – Mateng
    Aug 23, 2013 at 14:53
  • among other things.... the UID (user numbers) maybe different at that point in time.
    – Tomachi
    Nov 16, 2018 at 0:03
8

Try UDPCast

The idea is to stream the whole your vmdk out of it's Virtual Machine to physical machine, where it is written to the physical hdd.

Procedure is outlined below.

Since you have a vmdk file, you might have a VMWare Workstation at your disposal, even complete Virtual Machine this vmdk is attached to. Run your Virtual Machine with this particular vmdk attached, but instead of ordinary boot use PartedMagic liveCD to boot from.

When liveCD is started, navigate to main menu and find the UDPCast Disk Cloning. Its dialogs are self-explanatory (see the screenshot) UDPCast Disk Cloning start screen

After selecting this Virtual Machine to be the sender, you should select which drive you want to broadcast (using Unix notation, like /dev/sda).

After you've started the sender, you need to start receiver as well. Since you have a physical hdd, I'm assuming, you also have a complete PC with this hdd attached to it. Same thing here: you need to start liveCD with UDPCast Disk Cloning selecting receiver this time as well as appropriate physical hdd.

Worth noting, that you should make all the necessary arrangements to have network connectivity between your Virtual Machine and physical hardware. You should take necessary precautions if your vmdk contains private data, since its contents would be effectively streaming over your network. Another thing is that your target hdd should have no less storage capacity, than your vmdk's capacity. It is obvious, but also worth noting that your image is laid out one-to-one on your target hdd and you need to perform suitable operations with gparted or the like to make use of greater capacity of your new hdd.

2
  • I'd like to note, that this method is really straightforward and relatively easy to perform. Namely, the only preparation step you need to take is to prepare your LiveCD with UDPCast on it or any other LiveCD, which is sufficient to download and run UDPCast. If you have some virtual hd image, you have definitely used it in a Virtual Machine, which is already set and ready, on the other hand you have prepared your physical system and it's also ready for the task.
    – PF4Public
    Feb 17, 2016 at 13:05
  • There's more: UDPCast could be run directly from PXE. udpcast.linux.lu/bootloader.html#pxe
    – PF4Public
    Feb 17, 2016 at 13:15
6

If you've VirtualBox installed, then you can convert VDI (VirtualBox drive image) file into IMG format by using VBoxManage command, then copy into the physical disk, e.g.

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

Then you can use dd command to copy the file into the physical partition:

dd if=./file.img of=/dev/<partition_here>

Note: Use sudo if required. Be careful when specifying the right device in /dev.

1
  • 1
    vdi file is the virtual disk which can contain multiple partitions, right? How will it work if it is written to a particular partition in /dev/ instead of a disk in /dev?
    – gtux
    Apr 23, 2020 at 21:06
3

If you have the new physical disk attached to the machine the VM virtual disk is on, you can convert VDI to physical without an intermediate conversion, probably saving a bunch of time & disk space. This is assuming your physical disk is the same or larger than the virtual disk's maximum size. In my case, the goal was to convert a VDI to a ZFS zvol, but the approach should be the same with any block device.

First create a VMDK file pointing to your block device:

VBoxManage internalcommands createrawvmdk -filename physical.vmdk -rawdisk /dev/sdX

Then (with the VM shutdown!) you can clone the VDI to the VMDK:

VBoxManage clonemedium --existing virtual.vdi physical.vmdk

That'll take a while... When it's done, you can delete the VMDK file since it was only used as a pointer for VirtualBox to find the physical device.

In my case, the new zvol was exactly the same size as the virtual disk. Given that your physical disk is probably different, you'd need to employ some kind of partition resizing mechanism to fill the full disk. Gparted or something perhaps. You should be able to point that at the physical /dev/sdX device while still attached to the VM machine so you don't have to resort to boot CD's the physical host.

Once the resize is done, pop the drive in your physical machine, cross you fingers, and power up.

In the event you wanted to keep using the physical drive with VirtualBox, keep the VMDK around, and use VBoxManage storageattach to connect it to the VM.

2
  • It looks like it works, but the data gets corrupted somehow and the OS won't boot properly. Jun 14, 2018 at 14:34
  • 1
    convert a .vdi or .vmdk to a real physical disk is one thing, and make it bootable is an entirely different thing, the bootloader, /etc/fstab etc, all have to be taken care of before it can happen
    – xpt
    May 24, 2020 at 23:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .