Skip to content

Instantly share code, notes, and snippets.

@Shourai
Created November 2, 2018 11:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shourai/2c55f3912e1ed83853a73456bb9a8c0d to your computer and use it in GitHub Desktop.
Save Shourai/2c55f3912e1ed83853a73456bb9a8c0d to your computer and use it in GitHub Desktop.
Mount SD Cards within VirtualBox on Mac OS X
From: https://blog.lobraun.de/2015/06/06/mount-sd-cards-within-virtualbox-on-mac-os-x/
Mount SD Cards within VirtualBox on Mac OS X
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 SD Cards into your card reader
Insert the card into the SD card reader slot.
Unmount the partitions from Mac OS X
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
diskutil list
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.
Create a VDMK file that maps to the 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.
Grant permissions on these devices
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
Add this VDMK file to the virtual machine that should get access to the SD card reader
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.
Start the virtual machine
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.
Lothar Braun
Read more posts by this author.
@wojciechczyz
Copy link

Unfortunately, under Monterey mounting drive fails:

sudo VBoxManage internalcommands createrawvmdk -filename ./sd-card.vmdk -rawdisk /dev/disk2
VBoxManage: error: VMDK: could not open raw disk file '/dev/disk2'
VBoxManage: error: Error code VERR_ACCESS_DENIED at /Users/vbox/tinderbox/6.1-mac-rel/src/VBox/Storage/VMDK.cpp(3459) in function int vmdkCreateRawImage(PVMDKIMAGE, const PVDISKRAW, uint64_t)
VBoxManage: error: Cannot create the raw disk VMDK: VERR_ACCESS_DENIED
VBoxManage: error: The raw disk vmdk file was not created

@carlossg
Copy link

carlossg commented Nov 6, 2022

Unfortunately, under Monterey mounting drive fails:

sudo VBoxManage internalcommands createrawvmdk -filename ./sd-card.vmdk -rawdisk /dev/disk2 VBoxManage: error: VMDK: could not open raw disk file '/dev/disk2' VBoxManage: error: Error code VERR_ACCESS_DENIED at /Users/vbox/tinderbox/6.1-mac-rel/src/VBox/Storage/VMDK.cpp(3459) in function int vmdkCreateRawImage(PVMDKIMAGE, const PVDISKRAW, uint64_t) VBoxManage: error: Cannot create the raw disk VMDK: VERR_ACCESS_DENIED VBoxManage: error: The raw disk vmdk file was not created

@wojciechczyz you need to give permissions to your user to the device

sudo chown $USER /dev/disk2

@nabilfreeman
Copy link

All this to put some ROMs on my Steam Deck's SD card

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment