Tuesday, July 5, 2011

Accessing your Physical Hard Drives/Disks from a Virtual Machine (VirtualBox)

I recently found that my computer running Ubuntu 64-bit had problems with the nForce motherboard SATA drivers and I kept getting errors. However, having used Linux long enough, I realized that I had a good alternative, which is to use Windows with a Virtual Machine running Ubuntu Server, easy enough to do.

But there was something that I forgot, which was that my hard drives were in ext4 format, but instead of taking the easy way (moving around data in a Live USB and reformatting the drives), I decided to try to access the data from the Ubuntu virtual machine, because I knew it was possible (without any proof).

Accessing the physical drives from VirtualBox allows for full support of the filesystems supported in the guest and (I'm pretty sure) at full speed. Combined with shared folders, this is very useful.

Any way, here is the end result done on VirtualBox (because it's the best and free):

First, you need to create a VMDK file describing the physical drive so that you can access it through VirtualBox. To do this you will first need to know the location of the drive, which looks like "\\.\PhysicalDriveX" where "X" is a number for Windows users, and /dev/sda (\\.\PhysicalDrive0), /dev/sdb (\\.\PhysicalDrive1), and so on for Linux users.

On Linux you can easily list the drives with the command (as root):
# fdisk -l
On Windows, you can find the drive number in many ways.
The easiest way is to open the Disk Manager by right clicking on "My Computer" or "Computer", clicking on "Manage" to open "Computer Management", and then clicking on "Disk Management". The bottom pane lists the disks with the correct disk numbers.




The way I like is to use Speccy, an awesome and light tool that shows all your specifications, and going to the hard drive tab because I found this tool useful later because VBoxManage.exe incorrectly guessed my hard drive's size and it caused errors, so I had to get the actual size from Speccy.



Once you have this, open up a command prompt, and we will go to the install directory and create a vmdk file. Here is an example for Drive 0 and Drive 1 on Windows (One vmdk per drive):
cd C:\Program Files\Oracle\VirtualBox
VBoxManage internalcommands createrawvmdk -filename C:\Users\norcalli\Desktop\Drive0.vmdk -rawdisk \\.\PhysicalDrive0
VBoxManage internalcommands createrawvmdk -filename C:\Users\norcalli\Desktop\Drive1.vmdk -rawdisk \\.\PhysicalDrive1
And the same command on Linux (VBoxManage should be in your PATH):

VBoxManage internalcommands createrawvmdk -filename ~/Desktop/Drive0.vmdk -rawdisk /dev/sda
VBoxManage internalcommands createrawvmdk -filename ~/Desktop/Drive1.vmdk -rawdisk /dev/sdb

You should now have one or more .vmdk files. Now, you can skip this next step, but I highly recommend getting a tool (like Speccy) that tells you the exact size of your disk (as in the underlined portion of the previous picture) to verify the vmdk's. If you open up the .vmdk file in a text editor (not notepad, which doesn't support Linux line endings; Wordpad works), you should see a bunch of lines. Look for a line similar to this:
RW 1953525168 FLAT "\\.\PhysicalDrive0" 0
OR
RW 1953525168 FLAT "/dev/sda" 0
The number after "RW" (1953525168) is the number of sectors in the Drive, standard sectors are 512 bytes, and in this case corresponds to a 1 TB (931 GB = 1953525168*512 bytes) drive. You should take the exact size of your drive and find the number of sectors it represents (for sizes in bytes, divide by 512, it should be an integer). Then verify this number is the same as the one in the vmdk, if its not, simply overwrite the vmdk.

Now we can load these vmdk's into VirtualBox, which honestly was the part that took me the longest to figure out for some reason, yet is the easiest to do. Open up VirtualBox and go into the Settings and then Storage tab, if you don't already have a SATA controller (or something more suited to your preference), you need to add one by clicking the bottom button in the picture. Once you have a drive controller added, click the other highlighted button to add a storage disk and pick "Choose existing disk." Then find the vmdk file, and you're done! Congratulations, you've just added access to a physical drive in VirtualBox.

A few notes:
You can add only a specific partition, but I didn't trust it (bad things occur with these kinds of things). You can try it if you have a drive without data you care about by slightly augmenting the command to create the vmdk (important: numbering starts from 1 not 0):

VBoxManage internalcommands createrawvmdk -filename C:\Users\norcalli\Desktop\Drive0.vmdk -rawdisk \\.\PhysicalDrive0 -partitions 1,3

No comments:

Post a Comment