How the Mount SD Card Android Process works

Mount Sd Card AndroidIn this blog post, I’d like to show you how the mount SD Card Android process takes place and in particular, how to configure Android in order to auto mount a new SD Card when plugged in. The situation in which the mount point is different from /mnt has been considered as well.

The following code has been tested on the Ltouch F Android tablet for industrial and home automation projects together with Android Gingerbread.

Mounting an SD Card on Android is a straightforward process. There might be differences from version to version, but the key concepts stay the same:

1. Identify the sys_path of the SD card device. Go to /dev/block/platform and list all the directories. In my case, I find the following directory: s3c-sdhci.3. This means that the SD card is inserted on the slot number 3.

2. To automatically mount it whenever the card is plugged in, you have to modify the vold.fstab configuration file. This file is usually located in /system/etc and you need the root permission to add your configuration commands.

I used the Ltouch F Android tablet that is a rooted device. However, if you already have a device with root permission just type

adb remount

in order to mount the system partition as writable (before opening a shell terminal).

3. Modify the vold.fstab file issuing the following command:

busybox vi vold.fstab

It will open the vi text editor. The vold.fstab is a configuration file that is read by the Android system in order to correctly mount SD Cards or external USB storage devices (such as flash memories). Every line in the file must adhere to the following format:

#######################
## Format: dev_mount <label> <mount_point> <part> <sysfs_path1…>
##
## label – Label for the volume
## mount_point – Where the volume will be mounted
## part – Partition # (1 based), or ‘auto’ for first usable partition.
## <sysfs_path> – List of sysfs paths to source devices
######################

So, for instance if you need to automatically mount the 3rd SD Card on /mnt/sdcard just append the following command to the file:

dev_mount sdcard /mnt/sdcard auto /devices/platform/s3c-sdhci.3/mmc_host/mmc3

where “sdcard” is the label associated to the volume, “/mnt/sdcard” is the path of the mount point in which you will find the SD files, “auto” specifies which partition to use and, as the header description suggests, it selects the first usable partition. The latter parameter specifies the path of the source device. Here I used /devices/platform/s3c-sdhci.3/mmc_host/mmc3. It is formed by a first common part “/devices/platform”, the directory part s3c-sdhci.3 that comes from point 1 and finally by the mmc_host/mmcX (where X usually identifies the number of the SD card) that specifies the type and number of the resource.

In case you need to configure a dual card setup but you have the /mnt/sdcard mount point already assigned to another volume you have to add a new mount point in /mnt or in other folder. Please remember that the /mnt folder is remounted at every boot therefore every modification you made inside of it (such as creating new folders) are not persistent and will be discarded.

You can for instance use another path as mount point such as /system. Suppose that you want to create a mount point on /system/sdcard1. In order to do that, you have to:

  • Create the sdcard folder on /system
mkdir /system/sdcard
  • Assign the following permission
chmod 0000 sdcard
  • Change the folder’s owner with
chown 1000:1000 sdcard
  • Modify the vold.fstab file as described above, but with the new mount point
dev_mount sdcard1 /system/sdcard1 auto /devices/platform/s3c-sdhci.3/mmc_host/mmc3

Note that I used 1000 as user and group because in Android (2.3) they are coded into the source files:

/* This is the master Users and Groups config for the platform.
** DO NOT EVER RENUMBER.
*/

#define AID_ROOT 0 /* traditional unix root user */

#define AID_SYSTEM 1000 /* system server */

#define AID_RADIO 1001 /* telephony subsystem, RIL */
#define AID_BLUETOOTH 1002 /* bluetooth subsystem */
#define AID_GRAPHICS 1003 /* graphics devices */
#define AID_INPUT 1004 /* input devices */
#define AID_AUDIO 1005 /* audio devices */
#define AID_CAMERA 1006 /* camera devices */
#define AID_LOG 1007 /* log devices */
#define AID_COMPASS 1008 /* compass device */
#define AID_MOUNT 1009 /* mountd socket */
#define AID_WIFI 1010 /* wifi subsystem */
#define AID_ADB 1011 /* android debug bridge (adbd) */
#define AID_INSTALL 1012 /* group for installing packages */
#define AID_MEDIA 1013 /* mediaserver process */
#define AID_DHCP 1014 /* dhcp client */
#define AID_SDCARD_RW 1015 /* external storage write access */
#define AID_VPN 1016 /* vpn system */
#define AID_KEYSTORE 1017 /* keystore subsystem */

#define AID_SHELL 2000 /* adb and debug shell user */
#define AID_CACHE 2001 /* cache access */
#define AID_DIAG 2002 /* access to diagnostic resources */

/* The 3000 series are intended for use as supplemental group id's only.
* They indicate special Android capabilities that the kernel is aware of. */
#define AID_NET_BT_ADMIN 3001 /* bluetooth: create any socket */
#define AID_NET_BT 3002 /* bluetooth: create sco, rfcomm or l2cap sockets */
#define AID_INET 3003 /* can create AF_INET and AF_INET6 sockets */
#define AID_NET_RAW 3004 /* can create raw INET sockets */
#define AID_NET_ADMIN 3005 /* can configure interfaces and routing tables. */

#define AID_MISC 9998 /* access to misc storage */
#define AID_NOBODY 9999

#define AID_APP 10000 /* first app user */

That’s it. Reboot the system and checks whether plugging in a new SD card, it will be mounted on the just created folder.

If you liked this post, please share it! Comments and suggestions are always appreciated!