Monday, June 22, 2015

Access SD card in OnBoard reader through VirtualBox Guest (Windows only)

NOTE: Instructions for use only on a Windows system. The second half of instructions may be translatable to a Mac or Linux machine, but I am unsure of that.

Recently went through the process of being able to read and write to an SD card for the purpose of putting Fedora 22 on to use for a Raspberry Pi 2. Wanted to jot down a few quick notes and tips to make the process a little easier. As a note, this was done on a 32 GB SDHC micro card. Shouldn't play too much of a factor if your card is different then that, but wanted to make a note of it anyway.

  1. To begin, insert card into the onboard reader
  2. If not a new card, format it.
  3. Open command prompt (right click to run as admin) and issue the following command
    1. wmic diskdrive list brief
That command will give a listing of all physical drives available on the system, and their DeviceID to access them. Find the line item for your SD card reader, and make a note of its ID name (e.g. \\.\PHYSICALDRIVE0)

That will give you the name to access it, but next in order to use it through VirtualBox we will have to make use of the VBoxManage feature of VirtualBox.
  1. Open a command prompt (make sure to run as administrator)
  2. If you do not have the VirtualBox install directory saved in your PATH, then I would recommend you set your working directory to there
    1. (e.g. C:\Program Files\Oracle\VirtualBox)
Here is where you will run the command to create a raw disk VMDK file to access the SD card. The command will be of this format:

VBoxManage internalcommands createrawvmdk -filename <.vmdk location> -rawdisk <Physical Disk ID>

The 'filename' flag is the location where the .vmdk file will be created and the 'rawdisk' flag is the physical drive ID name of the disk to be used. The final command will look something like this:

VBoxManage internalcommands createrawvmdk -filename C:\Users\Doe\Documents\name.vmdk -rawdisk \\.\PHYSICALDRIVE0

Issuing that command will create the file 'name.vmdk' that has direct disk access to whatever is \\.\PHYSICALDRIVE0.

When using this file in VirtualBox, make sure to run VirtualBox as 'Administrator'. 

I do want to make a VERY important note here: with that above command, any VirtualBox guest with access to that file has raw access to it, meaning that guest can modify the filesystem as it pleases. With the wrong command, the entire disk format can be destroyed and all data lost. It is highly recommended you read the official documentation of this command to get a better understanding of it.

Another important note I wish to make here is that the SD card may not always have the physical drive ID that you originally found. One thing that was run into during this process is that a USB drive was inserted into the system before the VirtualBox guest was started up. Windows detected the new drive and bumped it up in the physical drive list. So though the file was pointing to \\.\PHYSICALDRIVE2, the SD card was now \\.\PHYSICALDRIVE3. If it had not been noticed then all data on the USB drive could have been lost.

Final note you might wish to be aware of, is that file I/O is a bit odd with this. When the drive is mounted in the guest machine, you can read any data stored on it very easily. But for whatever reason, whatever the very first partition is, you cannot reliably write to it. For some reason, Windows retains dedicated full control over the very first partition of the disk so the guest cannot write to it. If there is a second partition, you can still write to that one though. Also, despite this, any and all writing to the direct filesystem is still capable. So though you can not write to the very first partition of the disk, you can still delete that partition and create a new one. Does not always work, but very often works.

No comments:

Post a Comment