Sometimes you need to mount your SD cards inside a VirtualBox machine to work with the files on the card. Working with the Raspberry Pi SD card on Mac OS X is an example for this.
My Raspberry Pi runs on Linux with some version of the EXT file system. While there is some support for this on Mac OS X, the available solutions have a lot of limitations, especially when it comes to write support on EXT file systems. As I do not have an USB keyboard, I sometimes need to mount the filesystem on another system to fix a startup problem when some of my experiments go wrong.
VirtualBox in theory allows for passing the internal card reader of the MacBooks to the virtual machine. Unfortunately, this does not work for me (and it seems for many other people).
What you can do instead is to create a virtual image, that passes control to the device node to the VM. The following steps will show you how to accomplish this. I put together a script that performs these steps for you. You can download the script here:
Insert the card into the SD card reader slot.
You can either do this using the Disk Utility:
Be sure that you choose Unmount instead of Eject for all the mounted partitions on your SD card.
Alternatively, you can unmount it using the command line. First, find the name of your SD Card running the command
You should get a list of all the disks and partitions on your system. After you identified the proper disk, e.h. /dev/disk2 on my system, just run the command
diskutil unmountDisk /dev/diskX
where diskX is your disk.
Afterwards, create a VMDK file that maps to the disk:
sudo VBoxManage internalcommands createrawvmdk -filename <vmdk_file> -rawdisk </dev/diskX>
where vmdk_file is the filename that should be created and /dev/diskX is the name of the SD card.
As you will probably run VirtualBox as your normal user, you should make sure that he has the rights to perform all operations on the device. The disk device usually belongs to root:operator, and your user is probably not part of this group. So in order to get full access to the device, you need to either get to be a member of the operator group (and give full access to the group), or you give all access permissions to all users on the machine:
sudo chmod 666 vmdk_file sudo chmod 666 /dev/diskX
You can either do this using the VirtualBox graphical user interface. Select the machine that should work with the SD card. Open the settings dialog and add the file (in this example: sdcard.vmdk) as a hard disk to the SATA controller:
Afterwards, you can start the virtual machine and you should have access to the card inside the VM.
Instead of using the VirtualBox GUI, you can also add the disk to the VM on the command line:
VBoxManage storageattach "<name_of_your_vm>" --medium <vmdk_file> --storagectl SATA --port <port_number> --type hdd
Where name_of_your_vm is the name of the VM that should get access to the SD card, vmdk_file is the file that you just created, and port_number is the port of your SATA device that your file should be attached to.
I put together a script that does these steps for you. You can download the script here.
When you start the VM, you might get the following error message:
This probably comes from the fact that OS X remounted one or more of the SD card partitions. Try to unmount them and again and start the virtual machine again.
If you have an idea how this can be achieved more easily, please let me know in the comments.