shell实现自动挂载U盘
(2013-07-09 17:26:38)
标签:
it |
脑子闲下来的时候,总要找点事做,忽然在百度知道看到有人求如何侦测U盘,我就搜了一圈,发现了关于hotplug的东东。
vim scsi.hotplug
#!/bin/sh
#
# What to do with this USB hotplug event?
#
# code by T-bagwell
case $ACTION in
add)
#echo $ACTION>/dev/tts/1
DIR="/dev/mmc"
for I in "$DIR/"*;do
#echo
$I>/dev/tts/1
if [ -d $I
-a -e $I/part1 ];then
#echo "found&mount $I/part1">/dev/tts/1
mount -t vfat -o sync $I/part1 /tmp/sdcard 2>/dev/tts/1
else
#echo "$I/part1 not found">/dev/tts/1
sleep 1;
if [ -d $I -a -e $I/part1 ];then
#echo "found&mount $I/part1">/dev/tts/1
mount -t vfat -o sync $I/part1 /tmp/sdcard 2>/dev/tts/1
fi
fi
done
;;
remove)
#echo $ACTION>/dev/tts/1
#echo "umount /tmp/sdcard">/dev/tts/1
umount /tmp/sdcard 2>/dev/tts/1
;;
*)
exit 1
;;
esac
要实现U盘的自动挂载,linux系统本身首先要满足以下几点要求:
首先,内核要支持hotplug和可移动设备。
其次,系统要安装udev,hotplug,hal,dbus等组件。而且要启动这些服务,并将这些服务加入到开机自启动列表中。
在此基础上,当有U盘插入时,linux内核会首先发现,然后会通过/sbin/hotplug脚本在/etc/hotplug.d/default/usb目录中寻找以.hotplug结尾的可执行的脚本,并运行该脚本来实现USB设备的自动挂载。如果该目录下没有找到以.hotplug结尾的可执行的脚本,就会在/etc/hotplug.d/default目录下寻找。
因此要实现U盘的自动挂载,我们就可以通过编写一个hotplug脚本来实现。在绝大多数的嵌入式linux系统中,U盘被系统当作一种scsi设备来处理。我们编写一个名为scsi.hotplug的shell脚本程序,并将其放在/etc/hotplug.d中。
vim scsi.hotplug
#!/bin/sh
#
# What to do with this USB hotplug event?
#
# code by T-bagwell
case $ACTION in
add)
remove)
*)
esac