Automatically mount removable media

From ArmadeusWiki
Revision as of 20:05, 16 February 2012 by JulienB (Talk | contribs) (Default case)

Jump to: navigation, search

When using USB key or MMC/SD in a final application, it could be useful if these media were automatically mount when inserted by the user. We will explain you how to implement this behaviour in this tutorial.

Default case

  • You are using default armadeus rootfs and so hotplugging is handled by an application called mdev. This can be easily checked with:
# cat /proc/sys/kernel/hotplug
/sbin/mdev
  • It means that each time Linux kernel detect a new device it will call /sbin/mdev program with specific paramaeters.
  • You can configure mdev with its config file: /etc/mdev.conf. To add automatic mount of USB and SD/MMC devices you can apply the following changes (- -> remove line, + -> add line) in the configuration file:
# Block devices
-sd[a-z].*       0:6     660
-mmcblk[0-9].*   0:6     660
+sd[a-z]*          0:6     660
+mmcblk[0-9]*     0:6     660
+sd[a-z][0-9]      0:6     660 */etc/hotplug/automounter.sh
+mmcblk[0-9]p[0-9] 0:6     660 */etc/hotplug/automounter.sh
  • this way mdev will called a script named /etc/hotplug/automount.sh each time a USB drive or an MMC/SD is plugged with a recognised partition table is plugged in.
  • Now we have to write what this script will have to do. So take your favorite editor and fill /etc/hotplug/automounter.sh with:
mkdir /etc/hotplug
vi /etc/hotplug/automounter.sh
#!/bin/sh

destdir=/media

eumount()
{
	if grep -qs "^/dev/$1 " /proc/mounts ; then
		umount "${destdir}/$1";
	fi

	[ -d "${destdir}/$1" ] && rmdir "${destdir}/$1"
}

emount()
{
	mkdir -p "${destdir}/$1" || exit 1

	if ! mount -t auto -o sync "/dev/$1" "${destdir}/$1"; then
		# failed to mount, clean up mountpoint
		rmdir "${destdir}/$1"
		exit 1
	fi
}

case "${ACTION}" in
add|"")
	eumount ${MDEV}
	emount ${MDEV}
	;;
remove)
	eumount ${MDEV}
	;;
esac
# chmod a+x /etc/hotplug/automounter.sh
# reboot

Links