Saturday, November 5, 2011

How to create a Virtualbox image from a physical partition by one command

I managed to create an image from a running Linux machine using the following command

Command:

sudo dd if=DRIVE | VBoxManage convertfromraw stdin FILENAME BYTES

Real Example

sudo dd if=/dev/sda | VBoxManage convertfromraw stdin MyLinuxImage.vdi 120034123776



Notes:

1- To get the number of bytes in partition, you can use the following command
sudo fdisk -l /dev/sda

2- You need to use /dev/xxx not /dev/xxx1 or xxx2 so that the image contains MBR records

For more information check the following link:
https://www.virtualbox.org/wiki/Migrate_Windows


16 comments:

  1. Thanks, useful post.

    ReplyDelete
  2. so the 120034123776 number is the size of the (for example sda1) partition I want copy?

    ReplyDelete
  3. Yes, please read the 2 notes @ the end of the post, good luck

    ReplyDelete
  4. Any idea how to do things the other way around? Create a linux iso to install on a physical machine from a virtual box image?

    ReplyDelete
  5. I don't know a solution for that

    ReplyDelete
  6. Karim. your post helped me too. Thanks!

    For Scott MacDonald's vdi-to raw see:
    http://blog.mybox.ro/2010/11/03/how-to-use-a-raw-disk-image-file-in-virtualbox/

    ReplyDelete
  7. Hi, some idea to convert vdi image to real physical partition?

    ReplyDelete
  8. I didn't face this case before, if you found a solution please post it here for other people

    ReplyDelete
  9. Answering previous question(my own question), to convert from vdi to physical real partition. In my case I wanted to migrate from vdi files to lvm partitions on Linux, for it I created a logical volume with exact size of the vdi file, to see the exact size of the vdi file I used:

    $ VBoxManage list hdds # it will show disks files with UUIDs
    $ VBoxManage showhdinfo <UUID> # it will show the logical size in Mb

    Then create the lvm partition with retrieved size
    # lvcreate -n <lvm-name> -L <size>M <lvm-group>

    Now convert the vdi file to raw format
    $ VBoxManage internalcommands converttoraw <vdi-file>.vdi <raw-file>.raw

    Create the vmdk file to use the lvm partition
    $ VBoxManage internalcommands createrawvmdk -filename <vmdk-file-name>.vmdk -rawdisk /dev/<lvm-partition>

    Copy the raw image to the lvm physical partition
    # dd if=<raw-file>.raw of=/dev/<lvm-partition>

    Is important to use dd to copy, it will copy as disk image. Then as last in the settings of the VM change the disk from vdi file to the vmdk create file.

    ReplyDelete
  10. Thanks for this.

    How can we do virtual to physical.
    On Macosx, How with one command,
    migrate a mounted raw image of Windows to a NTFS Bootcamp partition ?

    I used this tutorial: huguesval.com/blog/2012/02/installing-windows-7-on-a-mac-without-superdrive-with-virtualbox

    And like the last step I want to copy the raw mounted image to the Bootcamp partition.

    Thx!

    ReplyDelete
  11. I have a problem when I use dd it errors out after the file reaches 4GB. Any help I need a point in the right direction.

    ReplyDelete
  12. I answered my own question. Formated my external to NTFS. I forgot that FAT can't handle anything over 4GB. hehehe silly newbie mistake.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Is there a command that can grab only the space used in the physical partition instead of the whole drive?

    for example, if I am only using 15 GB of space on my (500 GB) hard drive, then I only want to convert those 15GB from rawtovdi instead of the whole 500 GB.

    Greatly appreciated!

    ReplyDelete
  15. how do when already have a image of a single partition. sda1.bin for example
    thanks

    ReplyDelete