This post shows steps to enable SD card access from Linux VirtualBox guest on Windows host. In this post, I use Windows 10 as the host and Ubuntu 18.04 as the guest.
Steps
1. Check Device ID for SD Card
1-1. Open Command Prompt (or Windows PowerShell) as administrator (e.g. “Windows” key + “x” and select “Command prompt (admin)”).
1-2. List disc drives using wmic command before inserting SD card to PC.
wmic diskdrive list brief
The output should be like this:
Caption DeviceID Model Partitions Size
SAMSUNG MZFLV256HCHP-000MV \\.\PHYSICALDRIVE0 SAMSUNG MZFLV256HCHP-000MV 3 256052966400
1-3. Insert an SD card to the PC and run the same command. You should be able to find an additional line, and that shows DeviceID for the SD card. In this example, it’s “\\.\PHYSICALDRIVE1“.
Caption DeviceID Model Partitions Size
Generic- USB3.0 CRW -SD USB Device \\.\PHYSICALDRIVE1 Generic- USB3.0 CRW -SD USB Device 2 31914086400
SAMSUNG MZFLV256HCHP-000MV \\.\PHYSICALDRIVE0 SAMSUNG MZFLV256HCHP-000MV 3 256052966400
2. Create Virtual Machine Disk file (VMDK) for SD Card
2-1. Move folder to VirtualBox installation folder. In this example, it’s “C:\Program Files\Oracle\VirtualBox\”.
cd "C:\Program Files\Oracle\VirtualBox\"
2-2. Create VMDK file for the SD card device with “VBoxManage.exe”. The last argument is the DeviceID for SD card. (See Step 1-3).
.\VBoxManage.exe internalcommands createrawvmdk -filename C:\sdcard.vmdk -rawdisk \\.\PHYSICALDRIVE1
If everything is fine, the output will be like this, and “sdcard.vmdk” will be created in root folder of C drive.
RAW host disk access VMDK file C:\sdcard.vmdk created successfully.
3. Attach SD card VMDK to VM
3-1. Launch VirtualBox as administrator.
3-2. Navigate to “Settint” > “Strage”.
3-3. Click on “Controller: SATA”.
3-4. Check “Use Host I/O Cache” check box.
3-5. Then, click on “Adds hard disk” icon.
3-6. Select “Choose Existing Disk”.
3-7. Navigate to C drive and select “sdcard.vmdk” file which is created in Step 2-2.
4. Verify
4-1. Launch the VM and verify. In my case it appeared as “/dev/sdb” as below (Raspbian OS image in 32GB SD card).
$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 29.8 GiB, 32010928128 bytes, 62521344 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb1d773dc
Device Boot Start End Sectors Size Id Type
/dev/sdb1 8192 96453 88262 43.1M c W95 FAT32 (LBA)
/dev/sdb2 98304 31116287 31017984 14.8G 83 Linux
Troubleshoot
I encountered several errors while trying to use SD card. Here are some troubleshooting.
Troubleshoot A
Error : I/O cache encountered an error while updating data in medium “ahci-0-1” (rc=VERR_ACCESS_DENIED)
A-1. If you got the error above during/after you made changes on the SD card, please go back to Step 2 and make sure “Use Host I/O Cache” check box is checked.
Troubleshoot B
Error: UUID of the medium does not match the value stored in the media registry. [1]
B-1. Make sure you are in VirtualBox installation folder and run the command below.
.\VBoxManage.exe list hdds
You will be able to find the UUID in the error details.
UUID: 5b951504-358d-4a6e-bb8f-61be3ec32367
Parent UUID: base
State: inaccessible
Type: normal (base)
Location: C:\Users\max\Desktop\sdcard.vmdk
Storage format: VMDK
Capacity: 0 MBytes
Encryption: disabled
B-2. Remove the disk with command below by specifying the UUID.
.\VBoxManage.exe closemedium disk 5b951504-358d-4a6e-bb8f-61be3ec32367
References
[1] Virtual Box UUID {07c3…} does not match the value {2c1b…} stored in the media registry – stack overflow