How to Automount USB using mdev

NOTE: Factory already provides support for auto-mounting USB and MMC/SD cards by default when either BusyBox or udev are included. This document is provided only for those who are trying to enable this functionality outside of the Factory.

Prerequisites

  • Target RFS
    • busybox, with the following support enabled:
      • CONFIG_MOUNT=y
      • CONFIG_UMOUNT=y
      • CONFIG_MDEV=y
      • CONFIG_FEATURE_MDEV_CONF=y
      • CONFIG_FEATURE_MDEV_EXEC=y
  • Kernel
    • Hotplug support enabled (CONFIG_HOTPLUG=y)

Procedure

  1. On the target RFS, you must provide an mdev.conf file to run a script whenever a block device is added.
  2. Add a script to your target RFS that can mount and unmount devices when run.
  3. Make mdev execute when hotplug events occur.
  4. Optionally, add the files to your init or configuration RPM so that they are added automatically in builds.

Creating the mdev configuration

mdev uses a series of built-in rules to create device files when it is called. For instance, a rule exists that tells mdev to create the file /dev/sda if a SCSI disk is present when it is run. The typical way to invoke mdev is with the '-s' option, which searches for all attached devices and creates the proper nodes.

It is also possible to specify additional rules in a file if BusyBox was configured to support it using the CONFIG_FEATURE_MDEV_CONF option. The rules can also run scripts if the CONFIG_FEATURE_MDEV_EXEC option is set in the BusyBox build. This file is typically called /etc/mdev.conf.

To automount USB (or any block device), you can write a script that mdev will execute upon the addition or removal of a block device. You must also register mdev as the uevent handler, which is detailed in a section below.

The following rules run the script /etc/mdev/scripts/mount.sh upon addition or removal of a block device. This is sufficient for USB devices, but will also work for any device registered as an sd*.

sd[a-z][0-9]* root:root 660 @/etc/mdev/mount.sh add $MDEV
sd[a-z][0-9]* root:root 660 $/etc/mdev/mount.sh remove $MDEV

The file mdev.conf is an example of this. This file can be safely dropped into any Timesys rfs without conflict.

The general format for the rules are described in the man page for busybox, or in the file docs/mdev.txt in the BusyBox source tree.

Writing the mount script

The mount script is a basic shell script that runs the mount and umount commands. It also creates and destroys the mount directories if necessary. I have attached a mount script that works with the rules file called mount.sh . It will work with mdev, and possibly udev if the rules are written properly. It should be called /etc/mdev/mount.sh

Registering mdev as the uevent handler

BusyBox does not automatically register mdev as the uevent handler, so this must be done in an init script. The kernel uses the utility contained in the file /proc/sys/kernel/hotplug, or failing that, /sbin/hotplug. We can register mdev by piping it into that file.

The full procedure is to execute the following commands in your init script:

mount -t tmpfs mdev /dev
mkdir /dev/pts
mount -t devpts devpts /dev/pts
mount -t sysfs sysfs /sys
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s

The final mdev -s creates the initial device tree in /dev. Additional devices will be automatically added as they are registered with the kernel.

Note that older versions of busybox put mdev in /bin, and this would need to be changed accordingly.

Adding the files to your init package

To build the files into an init RPM package, just add them to the source tarball in the proper locations (./etc/*), and add the following lines to the files section of the RPM spec file:

%files

/etc/mdev.conf
/etc/mdev/mount.sh