How can I clone a hard drive directly to a vdi image

39

22

I would like to make a VirtualBox .vdi image out of my hard drive.

I've found howto's online that describe doing this by first usind DD to create a .raw image, then using VBoxManage to convert the .raw to a .vdi. SO if my HD is 1 TB, this process (temporarily) requires 2TB of space, to store both the .raw and .vdi.

I only have a bit more than 1TB of free space. Is there a way to create a .vdi image of a hard drive, without first having to create a .raw image?

SuperElectric

Posted 2013-07-02T00:57:27.160

Reputation: 491

This is risky, but if you have your hard drive that you want to P2V, then you could create the image on your spare drive with about 1TB Free. Then format your original drive and copy the RAW file to the original drive. Then convert the RAW to VDI back onto your main drive. However, if it gets screwed up (could happen) then you're stuck with a RAW Image and no original. – kobaltz – 2013-07-02T01:03:25.173

@kolbatz I considered this, but I thought that it had a high chance of failure. When I image the hard drive, the resulting .raw file is the size of the hard drive. But before copying the file back to the original drive, I need to give the drive a filesystem. This involves writing some metadata to the drive, leaving not enough space to hold the drive-sized file. – SuperElectric – 2013-07-02T04:30:51.503

Answers

55

You can directly create an image with VBoxManage convertfromraw. First unmount the device, then:

VBoxManage convertfromraw /dev/sda MyImage.vdi --format VDI

Replace /dev/sda with whatever disk or partition you want to clone.

You may need to do this as root to gain access to the device. If so, then you should change ownership of the finished image.

user235224

Posted 2013-07-02T00:57:27.160

Reputation: 666

2Works beautifully and out of the box on OSX when you have VirtualBox installed. Thanks for pointing to this solution – JosFabre – 2016-04-16T15:32:23.520

8

I tried the accepted solution but for me it failed:

# VBoxManage convertfromraw /dev/sdg /path/to/file.vdi --format VDI
Converting from raw image file="/dev/sdg" to file="/path/to/file.vdi"...
Creating dynamic image with size 0 bytes (0MB)...
VBoxManage: error: Cannot create the disk image "/path/to/file.vdi": VERR_INVALID_PARAMETER

Maybe it couldn't detect the size because the disk was attached through USB?

So instead I got the size of the disk with fdisk -l

Disk /dev/sdg: 160.0 GB, 160041885696 bytes

And then I used the stdin form of convertfromraw

# dd if=/dev/sdg | VBoxManage convertfromraw stdin /path/to/file.vdi 160041885696 --format VDI
Converting from raw image file="stdin" to file="/path/to/file.vdi"...
Creating dynamic image with size 160041885696 bytes (152628MB)...

Daniel

Posted 2013-07-02T00:57:27.160

Reputation: 191

2

There are other safer ways to create a file of your current system that Virtualbox can work with. Vdi's are virtualbox specific files and are usually only generated by VB from a fresh virtual hard disk install.

You have many other options.

I recently used disk2vhd to create a .VHD (Microsoft Virtual Hard Disk) that Virtualbox imported beautifully. (Although it was an XP system) I don't think it works well with other OS's.

Alternatively there is Vmwares converter tool that can export your system to a variety of formats that virtualbox as well as other platforms can use. There are open standards for this kind of thing.

http://www.vmware.com/products/converter/features.html

Scandalist

Posted 2013-07-02T00:57:27.160

Reputation: 2 767

2Why are these methods "safer" in your opinion? – andcoz – 2017-02-25T11:44:32.133

disk2vhd uses a volume snapshot, which means it can be used to clone a system while it's running. – mwfearnley – 2017-10-17T14:41:42.243

"I don't think it works well with other OS" --> well actually it does, just sucessfully hot mirrored a Windows 10 with disk2vhd, worked flawlessly ! – Henrique de Sousa – 2019-01-03T22:46:57.973

0

There are several tools that can be useful for your purposes:

VBoxHDTools

Disk2vhd

Simple VHD Manager

VhdxTool

Mount VHD

gdiskdump (for Linux)

etc

Source: Incremental cloning

ajcg

Posted 2013-07-02T00:57:27.160

Reputation: 402