Difference between revisions of "Automatically mount removable media"

From ArmadeusWiki
Jump to: navigation, search
(Default case)
m (Default case)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
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.
 
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==
+
==Default case (mdev)==
* You are using default armadeus rootfs and so hotplugging is handled by an application called ''mdev''. This can be easily checked with:
+
* If you are using default armadeus rootfs, hotplugging is handled by an application called ''mdev''. This can be easily checked with:
 
<pre class="apf">
 
<pre class="apf">
 
# cat /proc/sys/kernel/hotplug
 
# cat /proc/sys/kernel/hotplug
Line 8: Line 8:
 
</pre>
 
</pre>
  
* It means that each time Linux kernel detect a new device it will call ''/sbin/mdev'' program with specific paramaeters.
+
* It means that each time Linux kernel detect a new device it will call ''/sbin/mdev'' program with specific parameters.
 
* 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:
 
* 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:
 
<source lang="diff">
 
<source lang="diff">
Line 14: Line 14:
 
-sd[a-z].*      0:6    660
 
-sd[a-z].*      0:6    660
 
-mmcblk[0-9].*  0:6    660
 
-mmcblk[0-9].*  0:6    660
+sd[a-z]*         0:6    660
+
+sd[a-z]          0:6    660
+mmcblk[0-9]*     0:6    660
+
+mmcblk[0-9]    0:6    660
+sd[a-z][0-9]      0:6    660 */etc/hotplug/automounter.sh
+
+sd[a-z][0-9]      0:6    660 */lib/mdev/automounter.sh
+mmcblk[0-9]p[0-9] 0:6    660 */etc/hotplug/automounter.sh
+
+mmcblk[0-9]p[0-9] 0:6    660 */lib/mdev/automounter.sh
  
 
</source>
 
</source>
* 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.
+
* this way ''mdev'' will call a script named ''/lib/mdev/automount.sh'' each time a USB drive or an MMC/SD (with a recognized 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:
+
* create directory ''/lib/mdev''  
 
<pre class="apf">
 
<pre class="apf">
mkdir /etc/hotplug
+
# mkdir /lib/mdev
vi /etc/hotplug/automounter.sh
+
</pre>
 +
*then we will have to write what this script is going to do. So take your favorite editor and fill ''/lib/mdev/automounter.sh'' with:
 +
<pre class="apf">
 +
# vi /lib/mdev/automounter.sh
 
</pre>
 
</pre>
  
Line 32: Line 35:
 
destdir=/media
 
destdir=/media
  
eumount()
+
my_umount()
 
{
 
{
 
if grep -qs "^/dev/$1 " /proc/mounts ; then
 
if grep -qs "^/dev/$1 " /proc/mounts ; then
Line 41: Line 44:
 
}
 
}
  
emount()
+
my_mount()
 
{
 
{
 
mkdir -p "${destdir}/$1" || exit 1
 
mkdir -p "${destdir}/$1" || exit 1
Line 54: Line 57:
 
case "${ACTION}" in
 
case "${ACTION}" in
 
add|"")
 
add|"")
eumount ${MDEV}
+
my_umount ${MDEV}
emount ${MDEV}
+
my_mount ${MDEV}
 
;;
 
;;
 
remove)
 
remove)
eumount ${MDEV}
+
my_umount ${MDEV}
 
;;
 
;;
 
esac
 
esac
 
</source>
 
</source>
 
<pre class="apf">
 
<pre class="apf">
# chmod a+x /etc/hotplug/automounter.sh
+
# chmod a+x /lib/mdev/automounter.sh
# reboot
+
 
</pre>
 
</pre>
 +
 +
* As we now use ''/media'' to create dynamic mount points we will move it from FLASH to RAM by using a tmpfs mount. Take your favorite editor again and edit ''/etc/fstab'' file to add it the following line:
 +
<source lang="diff">
 +
...
 +
usbfs          /proc/bus/usb  usbfs    defaults          0      0
 +
+tmpfs          /media        tmpfs    defaults          0      0
 +
</source>
 +
:
 +
* Then, reboot and everything should now work :-). For example when plugging a MMC/SD you will get:
 +
<pre class="apf">
 +
# mmc0: host does not support reading read-only switch. assuming write-enable.
 +
mmc0: new SD card at address 0002
 +
mmcblk0: mmc0:0002 SD    1.87 GiB
 +
mmcblk0: p1
 +
 +
# ls -al /media/
 +
total 32
 +
drwxrwxrwt    3 root    root            60 Jan  1 00:27 .
 +
drwxrwxr-x  18 root    root          1248 Feb  6  2012 ..
 +
drwxrwxrwx    7 root    root        16384 Jan  1 00:00 mmcblk0p1
 +
 +
# mount
 +
...
 +
/dev/mmcblk0p1 on /media/mmcblk0p1 type vfat (rw,sync,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=iso8859-1)
 +
</pre>
 +
 +
* Don't forget that if you want to remove your device, you will have to manually umount it before. Example for a MMC/SD:
 +
<pre class="apf">
 +
# umount /media/mmcblk0p1
 +
</pre>
 +
 +
==Other cases==
 +
* well... in that cases I suppose you know what you're doing !:-)
 +
* if using udev instead of mdev for example, then you will have to activate corresponding Buildroot packages (eg usbmount)
  
 
==Links==
 
==Links==
 
* http://lists.busybox.net/pipermail/buildroot/2011-August/045187.html
 
* http://lists.busybox.net/pipermail/buildroot/2011-August/045187.html
 +
* http://linux.die.net/man/8/hotplug

Latest revision as of 19:43, 17 September 2014

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 (mdev)

  • If you are using default armadeus rootfs, 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 parameters.
  • 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 */lib/mdev/automounter.sh
+mmcblk[0-9]p[0-9] 0:6     660 */lib/mdev/automounter.sh
  • this way mdev will call a script named /lib/mdev/automount.sh each time a USB drive or an MMC/SD (with a recognized partition table) is plugged in.
  • create directory /lib/mdev
# mkdir /lib/mdev
  • then we will have to write what this script is going to do. So take your favorite editor and fill /lib/mdev/automounter.sh with:
# vi /lib/mdev/automounter.sh
#!/bin/sh

destdir=/media

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

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

my_mount()
{
	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|"")
	my_umount ${MDEV}
	my_mount ${MDEV}
	;;
remove)
	my_umount ${MDEV}
	;;
esac
# chmod a+x /lib/mdev/automounter.sh
  • As we now use /media to create dynamic mount points we will move it from FLASH to RAM by using a tmpfs mount. Take your favorite editor again and edit /etc/fstab file to add it the following line:
...
usbfs           /proc/bus/usb  usbfs    defaults          0      0
+tmpfs           /media         tmpfs    defaults          0      0
  • Then, reboot and everything should now work :-). For example when plugging a MMC/SD you will get:
# mmc0: host does not support reading read-only switch. assuming write-enable.
mmc0: new SD card at address 0002
mmcblk0: mmc0:0002 SD    1.87 GiB 
 mmcblk0: p1

# ls -al /media/
total 32
drwxrwxrwt    3 root     root            60 Jan  1 00:27 .
drwxrwxr-x   18 root     root          1248 Feb  6  2012 ..
drwxrwxrwx    7 root     root         16384 Jan  1 00:00 mmcblk0p1

# mount
...
/dev/mmcblk0p1 on /media/mmcblk0p1 type vfat (rw,sync,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=iso8859-1)
  • Don't forget that if you want to remove your device, you will have to manually umount it before. Example for a MMC/SD:
# umount /media/mmcblk0p1

Other cases

  • well... in that cases I suppose you know what you're doing !:-)
  • if using udev instead of mdev for example, then you will have to activate corresponding Buildroot packages (eg usbmount)

Links