SD Card Access on an Ubuntu virtual machine running on a Windows host through VirtualBox
by Conrad Gomes on
We will take a look at how to access an SD Card on a Windows Laptop through an Ubuntu VirtualBox virtual machine.
Some of the embedded linux platforms like the i.MX28 evaluation kit from Freescale have SD card interfaces with which a firmware image can be flashed and booted up on the platform.
If you work in an environment where you use Microsoft Windows as your primary OS you might end up using a Linux environment as a virtual machine on Windows. For all work involving embedded development for your target board you will mostly use linux based tools to build code, remote debugging or even flashing your firmware on some sort of flash based device.
Our Embedded Development Environment
We have a i.mx28 Evaluation Kit with a SD card interface. The platform supports booting from firmware flashed on the SD card. In order to access the SD card we use a linux based tool for the embedded platform. Unfortunately our linux development machine is a virtual machine of Ubuntu running on Oracle’s VirtualBox.
The challenge here is to allow the virtual machine running the guest Ubuntu OS to recognize the SD card interface on the laptop running Windows OS as the host OS.
Specifications of the environment:
-
Host OS : Windows 7 Professional
-
VirtualBox : Version 4.3.8
-
Guest OS : Ubuntu 13.04 (GNU/Linux 3.8.0-19-generic i686)
Step 2: Determine the physical drive number allocated to the SD Card
In our case we work with a micro SD card and have to use an adapter as shown in the diagram below:
After we have plugged in our SD card we need to know what physical drive number is allocated to it by the Windows 7 system. This will be used as a parameter to VBoxManage when we’re configuring VirtualBox to use the SD card. We will use the Windows Management Instrumentation command-line (WMIC) tool to determine the physical drive.
The command might require administrator priviledges. After execution we get to know that our SD card has been allocated a physical drive number of 1.
C:\Users\Conrad.Gomes>wmic diskdrive list brief /format:list
Step 3: Creating and mapping a disk file to the SD Card
The SD card is treated as a physical disk in the system. We need to create an image that will represent the entire "raw disk" represented by the SD card. To do so we will use the VBoxManage utility available with the VirtualBox installation. We first change the directory to the VirtualBox installation path where the VBoxManage utility exists. Since VirtualBox is now owned by Oracle it will be installed in the "Oracle" directory in "Program Files".
C:\Users\Conrad.Gomes>cd "C:\Program Files\Oracle\VirtualBox"
C:\Program Files\Oracle\VirtualBox>
The capture below highlights the result of the above step
We should now be able to create the vmdk image mapped to the SD card using the command below. Two arguments need to be supplied, the absolute path of the file on the host OS i.e. the Windows7 machine which will map to the SD card and the raw disk which is expressed as "\\.\PhysicalDriveX" where "X" is the physical drive number of the SD card that we obtained using wmic.
C:\Program Files\Oracle\VirtualBox>VBoxManage.exe internalcommands createrawvmdk -filename "D:\Work\SD-Card.vmdk" -rawdisk "\\.\PhysicalDrive1"
If it succeeds you should see a success message as shown in the screen capture:
Step 4: Allowing the guest OS to access the SD card
Make sure the guest OS virtual machine is "Powered Off" before changing its settings. To modify the settings of the virtual machine we have to power it off. If not powered off the system will not allow us to add the hard disk mapped SD-Card.vmdk file.
Once powered off go to the settings of the virtual machine and under the "Controller: SATA" settings click on "Add Hard Disk" button as shown below:
Locate the VMDK file created in the previous step and open it to add it as a hard disk.
Once successfully opened we should be able to see the file listed as a hard disk under "Controller: SATA" as shown below:
Step 5: Accessing The SD Card On The Guest OS
The next step is to power on the virtual machine and see the SD card added as a hard disk in the system. We use fdisk to check and see if the SD card is listed in the system. Use of fdisk may require sudo privileges.
ubuntu@ubuntu-VirtualBox:~$ sudo fdisk -l
[sudo] password for ubuntu:
The SD card is visible as /dev/sde device in the system. Further more we can see the various partitions and their file formats. It is the last device listed by fdisk. For comparison sake fdisk can be run before adding the SD card as explained here and then compared to see the new hard disk device: