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.

Relevant Links not given above:

This entry was posted in SpinRite, Virtual Machines, VirtualBox and tagged , , , , , , . Bookmark the permalink.
  • http://caspan.com Caspan

    I have tried the exact same steps, I can see the drive listed and I can select it then SpinRite says it its selecting the drive for use and then I get the error:

    SpinRite’s final verification of its ability to safely read and write to this drive has failed! Due to some hardware or firmware (BIOS) trouble, SpinRite’s access to this drive could result in serious data corruption. Please see our web site for possible causes and cures. Update your BIOS?

    Any idea why I am getting this. I am an IT administrator myself and having my imaging machine available to me while running Spinrite would be so useful!

    • Gary__W

      As seen here: http://blog.nerdimmunity.com/2012/06/25/spinrite-vmware-and-windows-7/ take the disk offline and turn off readonly before running Spinrite:

      The fix for me is to use the DiskPart command line tool with some
      specific commands to get Windows to release the physical disk so VMware
      can use it.

      In my case, the disk I needed to operate is Disk 2.

      Run an elevated command prompt (as Administrator), then run “diskpart”. Once in diskpart, issue these commands.

      list disk
      select disk #
      offline disk
      attribute disk clear readonly
      rescan

      That should do it. The next time you boot the VM, it should be able to take ownership of the physical disk.

      • Charles A Templeton III

        That worked, thanks for the link to the blog post.

  • Pingback: Delicious Bookmarks for January 29th through January 30th « Lâmôlabs

  • Mike P.

    Thanks, but didn’t work for me. I get a “SpinRite Critical Error.” (Couldn’t establish exclusive read/write, or something like that.) Am running Win 7 64 as a host, with an AMD Phenom. (Virtualization enabled.) Running VBox 4.2.6 from “Run as Administrator” in a User account.

    VBox sees and lists the drive just fine. I tried both PIIX3 and PII4 IDE controllers in VBox, with and without Host caching. No Change.

    I removed the drive letter from Windows Disk Management, thinking that Windows could still see the drive and therefore, might have scheduled it for defragging or something. (Indexing off.) Windows does see the drive, BTW. I thought VBox would take exclusive use of the drive?

    I swapped controllers in VBox, putting the raw drive on the Secondary Master (moving the CD ROM to Primary Master to boot off the CD. Hmmm… now I’m wondering if I have to boot off the actual ISO so as to not access my CD ROM drive hardware?)

    SpinRite can see the drive but when it starts “working” it gives me the critical error above.

  • Pingback: My Homepage

  • Mike P.

    To avoid getting a “SpinRite Critical Error.” (can’t establish exclusive read/write, or something like that) you have to do two things. 1) You have to load SpinRite from the ISO image in Vbox (as opposed to loading from a CD in your hardware CD ROM drive). And 2) you have to go into Windows Disk Management and remove drive letter assignments from the partitions on the raw drive you will be SpinRiting.

    Success! Although it seems to take about 5x longer to SpinRite a drive from VBox than physical hardware.

  • Mike P.

    FYI: SMART data on a SATA raw drive will not be displayed in the VM. The only way I’ve found to see SMART data on a SATA drive is to use a SATA to IDE converter ($15) in physical hardware. Runs faster that way too than SpinRiting straight SATA.

  • Brad

    I’m still getting the “SpinRite Critical Error” that Mike P. described, having followed his steps of using a SpinRite ISO and removing drive letter assignments.

    I wonder what is different with my setup. It’s a 1TB SATA drive connected to an internal SATA header (not eSATA). It has 4 partitions: Recovery, System Reserve, Windows, and Files. I tried setting it to “Offline” as well and still got the red error in SpinRite.

    Windows is using the Intel 8 Series SATA AHCI Controller as the driver. Are you using AHCI mode as well, Dan?

    • romaimperator

      My hardware is using AHCI but in VirtualBox I used the default IDE controller. As I’m typing this I’m running Spinrite in Windows 7 on a recently purchased 2 TB SATA drive but one potential difference with this one is that since it’s brand new, it doesn’t even have a partition table on it but I wouldn’t think that would affect it.

      I know that Steve Gibson is working on adding AHCI support to Spinrite but it’s still going to be a while before he releases that update.