Saturday, February 2, 2013

Using a raw disk as a VirtualBox drive in Windows 7

The other day I was listening to episode 386 of Security Now! and learned from a listener who had written in that VirtualBox was capable of using physical disks for virtual machines and that this would work with SpinRite. Using this feature it is possible to run a SpinRite scan on a secondary disk in a virtual machine while still using the host machine. So to make sure I don’t forget how this was done and to hopefully help someone else out there, here are the steps that I took to get this working. An interesting thing to note here, since you can create raw drives in OS X, you should be able to run in the virtual machine a SpinRite scan on a secondary disk on a Mac!
Since it was listed on the manual page and I don’t want anyone to lose data, read this:

Warning

Raw hard disk access is for expert users only. Incorrect use or use of an outdated configuration can lead to total loss of data on the physical disk. Most importantly, do not attempt to boot the partition with the currently running host operating system in a guest. This will lead to severe data corruption.
For more information check out this section on creating raw disks in the VirtualBox manual https://www.virtualbox.org/manual/ch09.html#rawdisk.
Another final note: while I have not verified that this occurs, it is possible that Windows will assign a different disk number to the drive after rebooting. A few options for handling this include:
  1. Create a raw virtual disk for each drive number naming them, so you can attach the correct one to the virtual machine before running it but not need to recreate the raw disk files.
  2. After running the SpinRite scan delete the raw virtual disk vmdk file to make sure you don’t use it accidentally.
  3. Follow the instructions on this post by Kevin Cave to create a raw virtual disk file that will always point to the correct drive.

Creating the Virtual Drive

1. Connect your hard drive.
This should be obvious. The first thing you should do is connect up the drive that you need to scan to the host machine.
2. Find the disk number.
After you have booted your machine back up you need to find out what number Windows has given to your disk. You can find this information in the disk management pane. First, open the start menu and right click on “Computer”. Select “Manage” from the menu. Next, select “Disk Management” from the pane on the left under the heading “Storage”. In the middle section you will see all of your hard drives and removable media drives listed. At the top of the bottom half should be at least two disks, the first labeled ”Disk 0″. Find the disk number for the drive you want to create the raw disk for (because you can’t use your host machine’s drive as a raw disk, obviously this drive should not have the (C:) partition on it).
Here’s a screenshot showing my desktop’s drives, the primary drive first as “Disk 0″ and the drive I want to run the scan on is the second, “Disk 1″.
screenshot of disk management
3. Run VirtualBox as an administrator.
Right click on the VirtualBox shortcut and select “Run as administrator” from the menu. If / when UAC opens a box requesting permission to run, select “yes”.
4. Open up a command prompt.
Open the start menu and in the search box type “cmd”. In the list above right click on “cmd.exe” and select “Run as administrator”.
5. Navigate to the VirtualBox folder.
Next, you need to navigate to the folder where you installed VirtualBox. For me, this is the default location and since I’m running 64-bit Windows 7, the installer installed the 64-bit version of VirtualBox. For me this means I needed to “cd” (change directory) into the regular Program Files folder.
cd C:\Program Files\Oracle\VirtualBox\
Note: For the more savvy Windows users, all you really need to do is add your VirtualBox install directory to the PATH environment variable.
5. Enter the following command
To create the raw disk for use with VirtualBox type the following command in the command prompt filling in the file name and the disk number in place of the # symbol:
VBoxManage internalcommands createrawvmdk -filename "FILENAME" -rawdisk "\\.\PhysicalDrive#"
Make sure that you include the double quotes around the filename and the disk name. This ensures that if there is a space in your filename that the command uses the entire path.
This is the command that I ran to create mine as an example using the disk number shown in the screenshot above and saving the disk image to my Downloads folder.
VBoxManage internalcommands createrawvmdk -filename "C:\Users\dan\Downloads\internalssd.vmdk" -rawdisk "\\.\PhysicalDrive1"
6. Create a new VirtualBox VM for running SpinRite.
In order to run SpinRite, you’ll need to create a VM and select the type “Other” then OS “DOS”. Follow the wizard and select how much RAM you’d like to allocate (I chose 128 MB which worked fine) and you don’t need to create a virtual hard disk since you are not installing an operating system.
7. Add the newly created virtual disk to your VirtualBox VM.
Navigate to the location of the file you created with the command and add it to your SpinRite VM. Lastly, you’ll have to mount the SpinRite.iso in the VM as well by adding a new CD/DVD drive and selecting the file on your machine.
8. Start up the VM and run SpinRite!
If everything went fine, SpinRite should discover the drive and show it in the list.
Note: You’ll need to run VirtualBox as an administrator anytime you want to run the VM so it can access the drive.
spinrite welcome screenspinrite drive selectionspinrite runningspinrite finished

Running on OS X and Linux

The only changes to the above steps that should be required for OS X and Linux (I have not tested these but if they work for you or a something different is required let me know) is to change the name of the raw disk in the command. On Linux use “/dev/sda” or whatever your drive is. On OS X use “/dev/disk1″ or whatever your drive is.

Troubleshooting

Hopefully those steps worked for you like they did for me. If you get an error like, “VERR_ACCESS_DENIED” you probably didn’t run the command prompt as an administrator. If you get an error like,
VBoxManage.exe: error: Failed to create the VirtualBox object!
VBoxManage.exe: error: Code CO_E_SERVER_EXEC_FAILURE (0x80080005) - Server execution failed (extended info not available)
VBoxManage.exe: error: Most likely, the VirtualBox COM server is not running or failed to start.
Then you probably didn’t run VirtualBox as an administrator.



(Source)

No comments:

Post a Comment