Accessing SD Card from Linux VirtualBox Guest on Windows Host

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

 

 

Sponsor Link

16 Comments

  1. It doesn’t work for me. Seems to be doing the job but the actual data is not written on the card (altough I don’t get any error message). Any idea ?

  2. i m getting this error if my sd card reader is not connected.
    is it necessary that sd card reader should always be connected?
    without that i m unable to open ubuntu in VMBOX??
    any solution for this if i want to use ubuntu without sd card use.

    Could not open the medium ‘C:\sdcard.vmdk’.
    VD: error VERR_FILE_NOT_FOUND opening image file ‘C:\sdcard.vmdk’ (VERR_FILE_NOT_FOUND).

    Result Code:
    E_FAIL (0x80004005)
    Component:
    MediumWrap
    Interface:
    IMedium {ad47ad09-787b-44ab-b343-a082a3f2dfb1}

  3. Hi
    Thanks a lot for sharing, it helped me lot but I’m still blocked on an issue:
    I did this to be able to flash the SDCard but it seems it cannot be done if it is “mounted” as a SATA device.
    I’m using the pi-gen tools to create a RPi image and flash it but the script that flahses the sdcard begins with:

    select block_device_str in $(lsblk -nSp -o NAME,SIZE,VENDOR,MODEL,TRAN | grep -v sata);

    which excludes the sata devices…
    I tried to mount it as a an IDE device but it prevents the VM from booting (I think it fins a bootable partition on the SD Card)

    Any clue on that?

  4. Very helpful, this was the most effective way to backup several raspberry pi’s SD cards into images with clonezilla using my pc from work (since I’m not able to boot from live cds on that machine). Thanks a lot

  5. I had I/O errors even with Host I/O Cache checked. I ended up deleting any existing volumes on the SD-card, then enabled “Solid-state Drive” and “Hot-pluggable” under sdcard.vmdk Attributes. Then I was able to successfully partition the drive within the VM using gparted.

  6. This didn’t work for me. I get an error in step 3 trying to attach the sdcard to the VM. This is with Virtualbox 6.1.30 .

    Permission problem accessing the file for the medium ‘C:\sdcard.vmdk’ (VERR_ACCESS_DENIED).

    Result Code:
    VBOX_E_FILE_ERROR (0x80BB0004)
    Component:
    MediumWrap
    Interface:
    IMedium {ad47ad09-787b-44ab-b343-a082a3f2dfb1}
    Callee:
    IVirtualBox {d0a0163f-e254-4e5b-a1f2-011cf991c38d}
    Callee RC:
    VBOX_E_OBJECT_NOT_FOUND (0x80BB0001)

    C:\Program Files\Oracle\VirtualBox>.\VBoxManage.exe internalcommands createrawvmdk -filename C:\sdcard.vmdk -rawdisk \\.\PHYSICALDRIVE9
    RAW host disk access VMDK file C:\sdcard.vmdk created successfully.

    Strangely, the sdcard.vmdk file does not appear in the list of virtual hard disks.

    C:\Program Files\Oracle\VirtualBox>VBoxManage.exe list hdds
    UUID: 07fa5e64-3f70-40c2-acf6-141686be33b8
    Parent UUID: base
    State: created
    Type: normal (base)
    Location: D:\VM\Ubuntu 20\Ubuntu 20 Clone.vdi
    Storage format: VDI
    Capacity: 307200 MBytes
    Encryption: disabled

    UUID: 71fd0c2e-930e-4d0e-b8ad-b7a74716ad93
    Parent UUID: base
    State: created
    Type: normal (base)
    Location: C:\Users\Julien Pierre\VirtualBox VMs\Linux 64-bit\Linux 64-bit.vdi
    Storage format: VDI
    Capacity: 131072 MBytes
    Encryption: disabled

    I have tried running the Virtualbox GUI as administrator also, to no avail

  7. This is a good article, but the results are not what I expected.
    Perhaps I’m looking for the wrong thing.

    I need a linux *directory*, serving as a mount for the boot partition of an SD card.
    But I what I get is a linux *block device file*.
    My build process fails when it tries to copy an image file to the block device file.

    Is there a simple change to this process to get what I need, or am I barking up the wrong tree?
    Thanks in advance, kwa

Comments are closed.