How to access an SD card from a virtual machine?

66

37

I want to format an SD card from my Linux virtual machine. I have a built-in SD card reader in my laptop. I tried using VirtualBox and VMware Player and installed Ubuntu 10.04 guest. None of them are showing the SD card reader as a device. I can access the SD card from the Windows host.

I am not interested in solutions using shared folders as I want to access the SD card as hardware (it should show up in /dev).

I basically want to set up the SD card for BeagleBoard, but I don't want to install a physical Ubuntu on my PC.

Punit Soni

Posted 2012-01-01T03:00:56.243

Reputation: 793

1I had the same issue. The solutions below did work for me but were to inflexible. I ended up investing in a "USB Micro SD reader" which shows up by default in the virtual machine. This allowed me to plug and unplug the card without additional actions. Just another perspective on the solution – TheMeaningfulEngineer – 2015-08-23T14:39:38.640

1Had you installed virtualbox guest addons? – tumchaaditya – 2012-08-06T09:46:22.640

Answers

56

On newer MacBook Pro's the SD card slot is no longer exposed as a USB device. Because of this you'll need to attach the raw device to the VM in order to gain raw access to the whole card. Full documentation is in the Advanced Storage Configuration section of the VirtualBox documentation.

As clearly stated in the docs:

Warning - Raw hard disk access is for expert users only. Incorrect use or use of an outdated configuration can lead to total loss of data on the physical disk. Most importantly, do not attempt to boot the partition with the currently running host operating system in a guest. This will lead to severe data corruption.

As a quick guide these are the steps to attach the device to a linux VM:

  1. Identify the raw disk block device on your host system

    1. Insert the SD card into the slot
    2. Open a Terminal and type "mount". You'll see output identifying the mounted volume from your SD card. On my system I get the following

      $ mount
      /dev/disk1 on / (hfs, local, journaled)
      devfs on /dev (devfs, local, nobrowse)
      map -hosts on /net (autofs, nosuid, automounted, nobrowse)
      map auto_home on /home (autofs, automounted, nobrowse)
      /dev/disk2s1 on /Volumes/NO NAME (msdos, local, nodev, nosuid, noowners)`
      
    3. In my case the "NO NAME" volume is the SD card, so I take note of the /dev/disk2s1 part. In this particular case the s1 part represents a partition (slice) on the raw device, so the part we'll use later is just the /dev/disk2 part.

  2. Open Disk Utility and "Unmount" the volume. Do not "Eject" it. Note that you may need to unmount the volume once in a while during the next steps as I found OSX would automatically remount it at random.
  3. Now we set up VirtualBox to be aware of the raw device with the following command in the Terminal.

    1. Note that the /path/to/file.vmdk can be put anywhere, though it's a good idea to store this in the guest VM folder such as ~/VirtualBox VMs/guest-vm/sdcard.vmdk
    2. Note that the -rawdisk we use doesn't reference the partition (slice) but only the block device as a whole.

      $ VBoxManage internalcommands createrawvmdk -filename /path/to/file.vmdk -rawdisk /dev/disk2
      
  4. Next we attach the raw disk to a guest VM within the VirtualBox UI

    1. Ensure the guest VM is not running.
    2. Open the settings area for the guest VM
    3. Click on "Storage" in the toolbar
    4. Next to the controller click on the icon to "Add Hard Disk"
    5. Select "Choose existing disk"
    6. Navigate to the /path/to/file.vmdk you used in step 3 and select it
    7. You should now be returned to the Storage tab and see your file.vmdk in the list.
  5. Start the VM
  6. Depending on whether you have a GUI or not the SD card may or may not automatically mount. If you need to mount is manually it is simply exposed as another standard block device, so on my guest this was exposed as /dev/sdb.

Update

It seems some people may have trouble with accessing the raw device in step 3.2 or 4.6. Attempts to run some of the commands with sudo won't help as VirtualBox will be running as your logged in user and still won't be able to access the raw device correct or the generated vmdk file.

The following steps should help, though I haven't tested them.

  1. Check the account you're using on OSX has Admin privileges in the System Preferences/Users section. There's a good chance it should also be the primary account on the system for raw device access to work.
  2. Check the raw disk permissions

        $ ls -l /dev/disk2
        brw-r----- 1 jinn staff    14,  5 Aug 26 15:33 /dev/disk2
    

    These permissions must match your current logged in user account:

        $ id
        uid=501(jinn) gid=20(staff) ...snip...
    

    The permissions on the device should also permit your usual user account to both read (r) and write (w) to this device. DO NOT CHANGE PERMISSIONS ON THE DEVICE UNLESS YOU ABSOLUTELY KNOW WHAT YOUR'RE DOING.


Update #2

Turns out OSX Mountain Lion created the device with "root:operator" privileges and 0640 permissions. Unfortunately for now the only way to gain access to this device is to "sudo chown $USER" it to your current user each time you insert the device, however this is not recommended unless you really understand what you're doing. Hopefully either Apple or VirtualBox will find a more suitable solution.

JinnKo

Posted 2012-01-01T03:00:56.243

Reputation: 715

1

I followed this guide but had permission issues, I then preformed a chmod 777 on the device and it worked fine. More details here: https://balage.blogs.balabit.com/2010/10/how-to-boot-from-usb-stick-under-virtualbox/

– Samantha Catania – 2014-10-19T16:11:06.567

2I got RESOURCE_BUSY when trying to add the image as a Hard Disk to Virtual Box. I noticed that OSX has remounted the SD card partitions. Force them to unmount by typing the following in Terminal (On the host) sudo diskutil unmountDisk /dev/<disk path> – Rob – 2015-04-22T12:56:07.160

1I can confirm one needs to 1) unmount 2) chown 3) create virtual disk 4) unmount again 5) add to VM 6) unmount AGAIN 7) start the VM. — @JinnKo: can you add that to the answer? or can I? – Erik Kaplun – 2015-12-01T02:01:13.157

Thank you, this has been very helpful to me. I'd also be interested to know, 1) how do you safely remove the microSD card mount; and 2) how much of the above process needs to be repeated in order to re-mount it? Does the vmdk file need to be recreated? – LarsH – 2016-02-16T22:24:58.170

1For others who are trying this... After doing the above steps successfully, when I restarted my guest (and host?) OS, the SD drive showed up with an error in VirtualBox. So I went through the above steps again, including Update #2 (I'm on Mac OS X Yosemite). Then I got an error "UUID {...} of the medium '.../sdcard.vmdk' does not match the value {...} stored in the media registry ('.../Library/VirtualBox/VirtualBox.xml')." I edited sdcard.vmdk manually to change the UUID there to match the other one in the error message. From there, everything worked fine. FWIW. – LarsH – 2016-02-17T14:44:35.007

I get VBOX_E_FILE_ERROR when trying to add the disk. I chowned it and chmodded 777 before creating the image. Frustrating. – Elliott B – 2018-06-19T02:27:46.620

Any advice on what to do if you don't see your device with mount? My host system is Windows 7 with a built-in SD Card Reader. I've tried accessing the card from an Ubuntu 10.04 system through VMware Player and VirtualBox, but without any luck. – GrandAdmiral – 2013-04-09T22:13:44.883

Sorry @GrandAdmiral, no idea about details on Windows. However the key point is in the first paragraph: "you'll need to attach the raw device to the VM in order to gain raw access to the whole card". The same principle will apply on any host system regardless of OS. – JinnKo – 2013-04-13T13:32:59.270

2In Mountain Lion, got diskarbitrationd mounting partition from sdcard, to stop this : $ sudo launchctl list | grep diskarbitrationd 188 - com.apple.diskarbitrationd $ sudo kill -SIGSTOP 188 # pauses diskarbitrationd, disabling automount $ sudo kill -SIGCONT 188 # resumes diskarbitrationd, reenabling automount – ıɾuǝʞ – 2013-08-21T14:47:50.687

1

Thanks @kenji, that's an additional great tip for Mtn Lion users (I struggled a bit with the automount issue myself). My detailed instructions on the VirtualBox forums would benefit from this tip; I'll add a link to your comment there and below. Thanks again.

– likethesky – 2013-10-25T00:04:55.330

42

Answer for Windows users

  1. Get the DeviceID of your SD card reader.

    You'll need a card in the drive, mounted by windows.

    Enter this command

    wmic diskdrive list brief
    

    It should look something like this:

    C:\Users\Sandy Scott>wmic diskdrive list brief
    Caption                      DeviceID            Model                        Partitions  Size
    WDC WD7500BPKT-75PK4T0       \\.\PHYSICALDRIVE0  WDC WD7500BPKT-75PK4T0       3           750153761280
    O2Micro SD SCSI Disk Device  \\.\PHYSICALDRIVE1  O2Micro SD SCSI Disk Device  1           3964584960
    

    The last device is the SD card reader, so the DeviceID is \\.\PHYSICALDRIVE1

  2. Create the link file to the SD card

    Open a command windows as Administrator

    "C:\Program Files\Oracle\VirtualBox\VBoxManage" internalcommands createrawvmdk -filename "%USERPROFILE%/Desktop/sdcard.vmdk" -rawdisk "\\.\PHYSICALDRIVE1"
    

    This assumes the default installation path - change it if you need to. (Ensure quotes are around the rawdisk argument.)

    The .vmdk file is a link to the SD card, you can put it anywhere on your host system, but this command just puts it on your desktop for convenience.

  3. Follow steps 4-6 in JinnKo's answer, the only minor tweak is that you need to start VirtualBox as an Administrator

  1. Next we attach the raw disk to a guest VM within the VirtualBox UI
    1. Ensure the guest VM is powered off.
    2. Ensure VirtualBox is not running
    3. Start VirtualBox by right-clicking on it and choosing "Run as administrator"
    4. Open the settings area for the guest VM
    5. Click on "Storage" in the toolbar
    6. Next to the controller click on the icon to "Add Hard Disk"
    7. Select "Choose existing disk"
    8. Navigate to the /path/to/file.vmdk you used in step 3 and select it
    9. You should now be returned to the Storage tab and see your file.vmdk in the list.
  2. Start the VM
  3. Depending on whether you have a GUI or not the SD card may or may not automatically mount. If you need to mount is manually it is simply exposed as another standard block device, so on my guest this was exposed as /dev/sdb.

sandyscott

Posted 2012-01-01T03:00:56.243

Reputation: 551

In case you're trying to get this with cygwin watch for the escape characters. I didn't get the correct cygwin solution but noticed that the command that works in cmd doesn't work in cygwin. – TheMeaningfulEngineer – 2015-08-23T13:36:53.170

When I start VirtualBox as administrator I don't see my machines and so cannot change their settings. The reason is probably that I am not an admin user because I am working with links in shared folders which requires this. After changing my account type to administrator and adding the sdcard drive and running VirtualBox as admin, I cannot get past the login screen. I didn't find the problem in syslog. Only when the sdcard drive is removed again I can also login when running VirtualBox as admin, but of course then I don't see the sd card. So it doesn't work for me. :( – Frank Breitling – 2017-04-02T10:25:07.210

7

You are most likely missing the extensions if your SD reader is using your USB controller.

enter image description here

Under File > Preferences, follow the pic. Add and point to the downloaded extension.

enter image description here

Restart your Virtual Machine and you should have access to USB devices.

kobaltz

Posted 2012-01-01T03:00:56.243

Reputation: 14 361

Hi, I tried this solution and installed the virtualbox extension pack but still no success. I am doing this in macbook if that makes any difference. Also, in my macbook system profiler, the sd card reader is not listed under USB devices but as a separate category called "card reader". Also, I am able to see the devices which are listed under USB in virtualbox. – Punit Soni – 2012-01-02T22:16:53.530

4

On newer MacBook Pro's the SD card slot is no longer exposed as a USB device

To overcome this problem you can simply use an external Usb Memory Card Reader (i.e. sd card to usb adapter) instead of built-in card reader. It will expose your sd card as a USB device so that you can mount it easily just like any usb device. They're cheap devices and would be an acceptable solution considering all the hassle in the other way.

Ahmet

Posted 2012-01-01T03:00:56.243

Reputation: 141

This is the smartest idea ever, thanks! I even had a USB card reader lying around but didn't realize I could use it. – marco.m – 2019-06-05T14:45:55.740

4

I'm using Kubuntu 15.04, VirtualBox 4.3.26

This assumes that the sd card device is /dev/mmcbl0 , and the output .vdk will be sd-card.vmk :

sudo VBoxManage internalcommands createrawvmdk -filename ./sd-card.vmdk -rawdisk /dev/mmcblk0

Not sure this is the best way to fix permissions, but it worked:

sudo chmod 777 /dev/mmcblk0
sudo chmod 777 ./sd-card.vmdk

Now go to the VM Settings, select Storage, click on Controller:Sata, click on the icon for Add Hard Disk, select your .vdk (sd-card.vmdk in this case)

Bogdan

Posted 2012-01-01T03:00:56.243

Reputation: 51

3

I had a lot of troubles getting the solution by JinnKo to work, mostly due to my being on Mountain Lion (OS X 10.8) ... But with his kind assistance, I've solved the issues.

The full solution is detailed here.

However, the essence of my solution--for Mountain Lion users--is as follows (use JinnKo's steps above, but insert my step here before running the 'VBoxManage internalcommands createrawvmdk [...]' command (that is, in between his steps 2 & 3):

** Particularly for Mtn Lion ** Make sure you are the owner of this device. On 10.8 Mtn Lion, you will probably find that you aren't. Check it by doing:

ls -l /dev/disk*

With the '*' you will see ALL your disk devices, you can also do: ls -l /dev/diskX to just see 'X', for instance, I did:

ls -l /dev/disk5

On Mtn Lion ( 10.8.x ) you will see something like:

brw-r----- 1 root operator 1, <today's date/time> /dev/diskX

Where 'X' is your device number, like the '5' in /dev/disk5 ...

On Lion ( 10.7.x ), Snow Leopard (10.6.x ), or perhaps earlier versions of OS X, you will--if your username is 'brad'--likely see something like:

brw-r----- 1 brad operator 1, <today's date/time> /dev/diskX

Where 'X' is your device number, like the '5' in /dev/disk5 ... If you are the owner, then all's good, nothing to do here, go to the next step, that is, the createrawvmdk step (in @JinnKo 's great instructions above).

If you are not the owner (but 'root' or some other user is), then either (a) login as that admin user or (b) do the following (again, be very sure you are using the correct device number/letter here!):

* CAUTION * The below is only for those who want to risk permanently destroying data on their hard drive or flash storage drive. DO NOT DO THIS if you aren't sure you know what you're doing:

sudo chown <your username> /dev/disk5
sudo chown <your username> /dev/disk5s1

* CAUTION * DO NOT DO THIS if you aren't sure you know what you're doing. The above is only for those who want to RISK permanently destroying data on their hard drive or flash storage drive.

You will need to do it for ALL of your slices (s1, s2, s3, ... however many there are). For example, my username is 'brad', so I did: sudo chown brad /dev/disk5, then sudo chown brad /dev/disk5s1, then for s2, and s3 (since I had 3 existing partitions or slices already).

If you haven't partitioned the SD card, then you may only need to do the sudo chown <your username> /dev/diskX once (without doing any of the slices/partitions)

Hope this helps some other poor Mtn Lion users out there!

likethesky

Posted 2012-01-01T03:00:56.243

Reputation: 164

Excellent solution. I would have been lost without this write up. Thank you very much! – ggutenberg – 2013-09-14T02:02:34.990

1

I've so far failed in finding a way to do this directly, even with a USB card reader (it put the card on /dev/dm-1 rather than the expected /dev/mmcblk1).

My workround was to create a 4 GB virtual drive in VirtualBox. This could then (as /dev/sdb) take an install of BeagleBoard Ubuntu as documented.

I then shut down the VM, converted it to a RAW DD file and used DD to put it on the card: (on the host Mac):

VBoxManage clonehd UbuntuBeagleImage.vdi UbuntuBeagleImage.dd --format RAW
sudo dd if=UbuntuBeagleImage.dd of=/dev/rdisk(NUMBER) bs=1m

(/dev/disk2 in my case, but triplecheck as getting it wrong could bork your hard disk drive).

The dd command took about 1 hr 20 min!

I hope this helps. I'm still working towards an easier and quicker way - we shouldn't need to transfer 4 GB of data for 600 MB of OS.

Rich

Posted 2012-01-01T03:00:56.243

Reputation: 111

0

I installed the "Virtual Box Guest Additons CD" files, but my SD card reader was not showing in my Windows XP Virtual Machine. I was able to get mine working by enabling the USB device in Virtual Box and then re-starting the Virtual Machine.

skibulk

Posted 2012-01-01T03:00:56.243

Reputation: 189

0

As kobaltz mentioned. To support the USB 2.0 and 3.0 versions you need to install the Virtual Extension Pack, I installed the 5.2.22 Oracle VM VirtualBox Extension Pack, and you can download it from the Virtual Box download page. Once you download the file, you can directly go ahead and install it, the same will be added to your Virtual Box extensions.

enter image description here

You should also add your SD card to the device filters in our Virtual Box, please go to Settings – > USB -> Click on the Add SD card Attached option.

enter image description here

Now you will be able to your SD card in the Filters.

enter image description here

Go to the Files and see the see the SD card folders (I use Ubuntu Linux Distribution)

enter image description here

Sibeesh Venu

Posted 2012-01-01T03:00:56.243

Reputation: 101

0

For anyone who's using macos and following previous answers, if you still cannot access sd card due to permission issue,

run virtualbox with sudo will solve it.

sudo VirtualBox

Notice that the path to the vm files is different, in my case, it is:

/private/var/root/VirtualBox\ VMs/$VM_NAME/

If that still doesn't work, change the owner and acess of the disk of sd card, to your user account and 666, respectively.

Pengyuan Zhou

Posted 2012-01-01T03:00:56.243

Reputation: 1

0

For me the solution was to turn-on USB device with SD card, using
Devices -> USB devices ->My card reader,
and after few seconds new disks appear in my VM.

I have installed VirtualBox Extensions Pack before that.

Zdzisiek

Posted 2012-01-01T03:00:56.243

Reputation: 56