字符哑终端终端环境下如何实现u盘自动挂载

linux下实现U盘和sd卡的自动挂载
时间: 22:23:41
&&&& 阅读:517
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&span style=&font-family:Arial, Helvetica, sans-&&&strong&目的:使U盘和sd卡在linux系统中进行插入和拔除时能自动挂载和卸载,不需要手动mount和umount。&/strong&&/span&&span style=&font-family:Arial, Helvetica, sans-&&&strong&步奏:&/strong&&/span&&span style=&font-family:Arial, Helvetica, sans-&&&span style=&color:#ff0000;&&1、在/etc/init.d/rcS中加入以下语句&/span&&/span&
echo /sbin/mdev & /proc/sys/kernel/hotplug2、在/etc下的mdev.conf文件中加入(如果没有这个文件,则新建一个)
sd[a-z][0-9]
0:0 666 @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
0:0 666 $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
ub[a-z][0-9]
0:0 666 @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
0:0 666 $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
mmcblk[0-9]p[0-9] 0:0 666 @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
mmcblk[0-9]
0:0 666 $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
3、在/etc下建立hotplug文件夹,并在该文件夹下建立两个文件
insert.sh:
if [ -n &$1& ]; then
if [ -b /dev/$1 ]; then
if [ ! -d /media ]; then
mkdir -p /media
if [ ! -d /media/$1 ]; then
mkdir -p /media/$1
mount /dev/$1 /media/$1
if [ $? -ne 0 ]; then
rm -rf /media/$1
remove.sh:
MOUNTS=$(mount | grep $1 | cut -d' ' -f3)
umount $MOUNTS
rm -rf $MOUNTS
& & & & 这样在插入U盘或者sd卡使,系统就会在根目录下生成media目录,并将U盘或SD卡挂载在这个目录下。这些步骤也可以在制作文件系统时执行,这样linux系统就能永久自带这个自动挂载的功能。
注:使用上述方法需要如下所示在busybox配置中选择支持mdev。
标签:&&&&&&&&&&&&原文:http://blog.csdn.net/emsoften/article/details/
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!主题 : 开发板上如何实现u盘自动挂载到某个目录
级别: 新手上路
金钱: 170 两
威望: 115 点
贡献值: 0 点
综合积分: 34 分
&开发板上如何实现u盘自动挂载到某个目录
我这个礼拜实现了用udev+hal+dbus的方式实现了: 当有热插拔设备的时候这个热插拔事件通过Udev向Hal发送Netlink socket消息,Hal在经过对消息的解析,最后将热插拔事件通知Dbus。但是Hal这个开源软件比较大, 而且依赖于glib,所以想把它抽掉。 所以我想请教一下大家怎样在开发板上实现u盘自动挂载并通知应用程序的方法。
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 论坛版主
发帖: 5407
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11054 分
我想这个功能用udev自己应该也可以实现,这是Angstrom里面的做法:1)在udev的rules里加上下面几行# Media automountingSUBSYSTEM==&block&, ACTION==&add&&&&&RUN+=&/etc/udev/scripts/mount.sh&SUBSYSTEM==&block&, ACTION==&remove& RUN+=&/etc/udev/scripts/mount.sh&2)创建脚本/etc/udev/scripts/mount.sh#!/bin/sh## Called from udev# Attemp to mount any added block devices # and remove any removed devices#MOUNT=&/bin/mount&PMOUNT=&/usr/bin/pmount&UMOUNT=&/bin/umount&name=&`basename &$DEVNAME&`&for line in `cat /etc/udev/mount.blacklist | grep -v ^#`do&&&&if ( echo &$DEVNAME& | grep -q &$line& )&&&&then&&&&&&&&logger &udev/mount.sh& &[$DEVNAME] is blacklisted, ignoring&&&&&&&&&exit 0&&&&fidoneautomount() {&&&&&&&&! test -d &/media/$name& && mkdir -p &/media/$name&&&&&&&&&if ! $MOUNT -t auto -o sync $DEVNAME &/media/$name&&&&&then&&&&&&&&#logger &mount.sh/automount& &$MOUNT -t auto $DEVNAME \&/media/$name\& failed!&&&&&&&&&rm_dir &/media/$name&&&&&else&&&&&&&&logger &mount.sh/automount& &Auto-mount of [/media/$name] successful&&&&&&&&&touch &/tmp/.automount-$name&&&&&fi}&&&&rm_dir() {&&&&# We do not want to rm -r populated directories&&&&if test &`find &$1& | wc -l | tr -d & &`& -lt 2 -a -d &$1&&&&&then&&&&&&&&! test -z &$1& && rm -r &$1&&&&&else&&&&&&&&logger &mount.sh/automount& &Not removing non-empty directory [$1]&&&&&fi}if [ &$ACTION& = &add& ] && [ -n &$DEVNAME& ]; then&&&&if [ -x &$PMOUNT& ]; then&&&&&&&&$PMOUNT $DEVNAME 2& /dev/null&&&&elif [ -x $MOUNT ]; then&&&&&&&&&&&&$MOUNT $DEVNAME 2& /dev/null&&&&fi&&&&&&&&# If the device isn't mounted at this point, it isn't configured in fstab&&&&# : Small correction: The rootfs partition may be called just &rootfs& and not by&&&&#&&&&&&&& its true device name so this would break. If the rootfs is mounted on two places&&&&#&&&&&&&&during boot, it confuses the heck out of fsck. So Im auto-adding the root-partition&&&&#&&&&&&&&to /etc/udev/mount.blacklist via postinst &&&&cat /proc/mounts | awk '{print $1}' | grep -q &^$DEVNAME$& || automount &&&&fiif [ &$ACTION& = &remove& ] && [ -x &$UMOUNT& ] && [ -n &$DEVNAME& ]; then&&&&for mnt in `cat /proc/mounts | grep &$DEVNAME& | cut -f 2 -d & & `&&&&do&&&&&&&&$UMOUNT $mnt&&&&done&&&&&&&&# Remove empty directories from auto-mounter&&&&test -e &/tmp/.automount-$name& && rm_dir &/media/$name&fi这样,当block设备载入或卸载时,系统会自动尝试将其挂载到/media目录下的对应设备名的目录下
&If you have an apple and I have an apple and we exchange apples, then you and I willstill each have one apple. But if you have an idea and I have an idea and we exchangethese ideas, then each of us will have two ideas.&
级别: 新手上路
金钱: 170 两
威望: 115 点
贡献值: 0 点
综合积分: 34 分
感谢楼主回复!我在/etc/udev/rules.d 下面加了一个10-usb.rules的文件,内容是:KERNEL==&sda&, SUBSYSTEM==&block& RUN+=&/root/usbmount.sh&我在root下面加了usbmount.sh 内容是:#!/bin/bashLOG=/var/log/usb-hotplug.log#lap=$(date --rfc-3339=ns)#echo &$lop: $DEVPATH requesting $ACTION& && $LOGecho &kluter auto mount&if[ $ACTION == &add& ]&&&&then&&&&&&&&mount -t vfat -o umask=000,noatime,async,codepage=936,iocharset=gb2312 \&&&&&&&&&&&&/dev/sda /var/tmpelif[ $ACTION == &remove& ]&&&&then&&&&umount -l /var/tmpfi可是当板子跑起来的时候我插入u盘, 还是不能自动mount,照你说的方法也试过了,还是不行,而且我感觉脚本根本就没有跑起来,因为echo的字串没有印出来,/var/log下面也没有生成usb-hotplug.log日志。我用的文件系统是光盘自带的。难道是规则文件没有被udevd识别么?
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 论坛版主
发帖: 5407
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11054 分
那就找原因啊,为什么脚本没有跑起来
&If you have an apple and I have an apple and we exchange apples, then you and I willstill each have one apple. But if you have an idea and I have an idea and we exchangethese ideas, then each of us will have two ideas.&
级别: 新手上路
金钱: 170 两
威望: 115 点
贡献值: 0 点
综合积分: 34 分
嗯,我再找找看
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 新手上路
金钱: 170 两
威望: 115 点
贡献值: 0 点
综合积分: 34 分
还是挂不上,郁闷~
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 新手上路
金钱: 170 两
威望: 115 点
贡献值: 0 点
综合积分: 34 分
现在我觉得很奇怪的是,我写了一个简单的脚本:#!/bin/shecho &kluter auto mount&if [ $ACTION = &add& ]; then&&&&/bin/mount /dev/sda /var/tmpelif [ $ACTION = &remove& ]; then&&&&/bin/umount -l /var/tmpfi我照shell编程里面说的#chmod +x usb.sh&& 然后./usb.sh 结果报错:-/bin/sh: ./usb.sh: not found我又 #sh usb.sh 又报错:kluter auto mountusb.sh: line 5: syntax error: &elif& unexpected (expecting &then&)搞不懂了,我都是按照shell编程里面的语法。
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 论坛版主
发帖: 5407
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11054 分
我不知道你的shell出了什么问题,但你至少需要把if [ $ACTION = &add& ]; then改成if [ “$ACTION” = &add& ]; then
&If you have an apple and I have an apple and we exchange apples, then you and I willstill each have one apple. But if you have an idea and I have an idea and we exchangethese ideas, then each of us will have two ideas.&
级别: 新手上路
金钱: 170 两
威望: 115 点
贡献值: 0 点
综合积分: 34 分
谢谢,这个确实是语法错误不过我现在在脚本里只写了一个ls命令,在PC上可以跑,但是开发板上跑不了我觉得应该是开发板shell有问题。
人总是在绝望的边缘 才能看清楚自己软弱的样子
级别: 新手上路
金钱: 155 两
威望: 31 点
贡献值: 0 点
综合积分: 62 分
HAL这个开源的东西 去哪里下啊??我写了一个qt检测U盘插拔的东西,在板子上运行时提示:&?????????QDBusInterface?& &The name org.freedesktop.Hal was not provided by any .service files&Object::connect: No such signal QDBusAbstractInterface::DeviceAdded(QString)Object::connect:&&(receiver name: 'Widget')Object::connect: No such signal QDBusAbstractInterface::DeviceRemoved(QString)Object::connect:&&(receiver name: 'Widget')这什么意思啊??是不是没有HAL???
开发板销售: 售后客服: Powered byLinux下C语言来检测USB设备以及自动区分U盘和硬盘并自动挂载
本文地址:/*************************************************************************
&&File&Name:&test.c
&&Author:&lizhu
&&Created&Time:&日&星期六&11时05分05秒
&************************************************************************/
#include&stdio.h&
#include&unistd.h&
#include&string.h&
#include&stdlib.h&
#define&DEVTYPE_DISK&2
#define&DEVTYPE_U&&&&3
//外部设备的子结构
typedef&struct&tagDevParam_T
&&&&unsigned&char&&devname[12];
&&&&unsigned&char&&///2----硬盘,3----U盘
&&&&unsigned&char&&partition_
&&&&unsigned&int&dev_//KB
&&&&unsigned&int&partition_size[12];/*分区大小,单位KB*/
}&tagDevParam_t,&*ptagDevParam_t;
typedef&struct&tagDevinfo_T
&&&&tagDevParam_t&dev[8];
&&&&int&&&&&&/*总共的设备数量*/
&&&&int&&&&&disk_
}&tagDevinfo_t,&*ptagDevinfo_t;
int&DvrCpFile(char&*srcFile,&char&*targetFile,&int&findString,&char&*string)
&&&&&&&&int&ret&=&0;
&&&&&&&&FILE&*srcfp&=&NULL;
&&&&&&&&FILE&*targetfp&=&NULL;
&&&&&&&&int&length&=&0;
&&&&&&&&ssize_t&
&&&&&&&&size_t&len&=&0;
&&&&&&&&char&*p&=&NULL;
&&&&&&&&char&*line&=&NULL;
&&&&&&&&if(access(srcFile,&F_OK)&!=&0)
&&&&&&&&&&&&printf(&ERROR:&src&file&is&not&exist!&);
&&&&&&&&&&&&return&-1;
&&&&&&&&if(ret&==&0)
&&&&&&&&&&&&srcfp&=&fopen(srcFile,&&r&);
&&&&&&&&&&&&targetfp&=&fopen(targetFile,&&w+&);
&&&&&&&&&&&&if((targetfp&!=&NULL)&&&&(srcfp&!=&NULL))
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&while((size&=&getline(&line,&&len,&srcfp))&!=&-1)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&if(size&&&0)
&&&&&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&&&&&if(findString&==&1)
&&&&&&&&&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&&&&&&&&&p&=&strstr(line,&string);
&&&&&&&&&&&&&&&&&&&&&&&&&&&&if(p&!=&NULL)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&length&=&fwrite(line,&size,&1,&targetfp);
&&&&&&&&&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&&&&&&&&&else
&&&&&&&&&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&&&&&&&&&length&=&fwrite(line,&size,&1,&targetfp);
&&&&&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&fclose(srcfp);
&&&&&&&&&&&&&&&&fclose(targetfp);
&&&&&&&&&&&&&&&&if(line)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&free(line);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&ret&=&0;
&&&&&&&&&&&&}
&&&&&&&&&&&&else
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&if(srcfp)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&printf(&ERROR:&open&flie&%s&,&targetFile);
&&&&&&&&&&&&&&&&&&&&fclose(srcfp);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&if(targetfp)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&printf(&ERROR:&open&flie&%s&,&srcFile);
&&&&&&&&&&&&&&&&&&&&fclose(targetfp);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&ret&=&-1;
&&&&&&&&&&&&}
&&&&&&&&return&
int&CheckIsDiskOrUsbDisk(unsigned&char&*devname)
&&&&FILE&*
&&&&int&&ret&=&-1;
&&&&char&buffer[80],&*&line&=&NULL;
&&&&ssize_t&
&&&&size_t&len&=&0;
&&&&int&fal=0;
&&&&memset(buffer,&0,&80);
&&&&char&tmpfilename[64];
&&&&if(tmpnam(tmpfilename)&==&NULL)
&&&&&&&&sprintf(tmpfilename,&&%s&,&&/tmp/check_dev_type.txt&);
&&&&fp&=&fopen(tmpfilename,&&w+&);
&&&&sprintf(buffer,&&/sys/block/%s/removable&,&devname);
&&&&DvrCpFile(buffer,&tmpfilename,&fal,&NULL);
&&&&size&=&getline(&line,&&len,&fp);
&&&&fclose(fp);
&&&&ret&=&atoi(line);
&&&&switch(ret)
&&&&&&&&case&0:
&&&&&&&&&&&&ret&=&DEVTYPE_DISK;
&&&&&&&&&&&&
&&&&&&&&case&1:
&&&&&&&&&&&&ret&=&DEVTYPE_U;
&&&&&&&&&&&&
&&&&&&&&default:
&&&&&&&&&&&&ret&=&-1;
&&&&&&&&&&&&
&&&&if(line)
&&&&&&&&free(line);
&&&&remove(tmpfilename);
&&&&return&
int&main(){
&&&&int&&&&&&&&&&&xx=1,i=0,dev_
&&&&ssize_t&&&&&&&
&&&&size_t&&&&&&&&len&=&0;
&&&&unsigned&char&k;
&&&&tagDevinfo_t&&
&&&&char&&&&&&&&&&tmp_devname[12];
&&&&char&&&&&&&&&&*line&=&NULL;
&&&&char&&&&&&&&&&*
&&&&char&&&&&&&&&&*
&&&&char&&&&&&&&&&seps[]&=&&&&;
&&&&FILE&&&&&&&&&&*fp&=&NULL;
&&&&bzero(tmp_devname,&12);
&&&&bzero(&devinfo,&sizeof(devinfo));
&&&&DvrCpFile(&/proc/partitions&,&&/tmp/partitions.txt&,&xx,&&sd&);
&&&&fp&=&fopen(&/tmp/partitions.txt&,&&r&);
&&&&/*获得系统&硬盘&和移动设备总结构*/
&&&&while((size&=&getline(&line,&&len,&fp))&!=&-1)
&&&&&&&&pname&=&strrchr(line,&'&');
&&&&&&&&if(pname&==&NULL)
&&&&&&&&&&&&
&&&&&&&&pname++;
&&&&&&&&if(memcmp(pname,&tmp_devname,&3)&==&0)&//新分区
&&&&&&&&&&&&i&=&0;
&&&&&&&&&&&&token&=&strtok(line,&seps);
&&&&&&&&&&&&while(token&!=&NULL)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&if(i&==&2)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&k=devinfo.dev[devinfo.devcount-1].partition_
&&&&&&&&&&&&&&&&&&&&devinfo.dev[devinfo.devcount-1].partition_size[k]=atoi(token);
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&i++;
&&&&&&&&&&&&&&&&token&=&strtok(NULL,&seps);
&&&&&&&&&&&&}
&&&&&&&&&&&&devinfo.dev[devinfo.devcount-1].partition_count++;
&&&&&&&&else//新设备
&&&&&&&&&&&&memcpy(devinfo.dev[devinfo.devcount].devname,&pname,&3);
&&&&&&&&&&&&i&=&0;
&&&&&&&&&&&&token&=&strtok(line,&seps);
&&&&&&&&&&&&while(token&!=&NULL)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&if(i&==&2)
&&&&&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&&&&&devinfo.dev[devinfo.devcount].dev_totalspace&=&atoi(token);&
&&&&&&&&&&&&&&&&}
&&&&&&&&&&&&&&&&i++;
&&&&&&&&&&&&&&&&token&=&strtok(NULL,&seps);
&&&&&&&&&&&&}
&&&&&&&&&&&&devinfo.devcount++;
&&&&&&&&memcpy(tmp_devname,&pname,&3);
printf(&dve&count&=%d\n&,devinfo.devcount);
for(i&=&0;&i&&&devinfo.&i++){
printf(&dev&name=%s\n&,devinfo.dev[i].devname);
dev_tpye&=&CheckIsDiskOrUsbDisk(devinfo.dev[i].devname);
devinfo.dev[i].devtype&=&dev_
if(devinfo.dev[i].devtype&==&DEVTYPE_U)
printf(&this&is&USB&drive&);
if(devinfo.dev[i].devtype&==&DEVTYPE_DISK)
printf(&this&is&&hard&drive&);
&&&&return&0;
上一篇:下一篇:
<div class="ds-thread" data-thread-key="335" data-title="Linux下C语言来检测USB设备以及自动区分U盘和硬盘并自动挂载" data-url="http://tongxinmao.com/Article/Detail/id/335"树莓派Raspberry Pi实战之命令行下实现USB存储设备自动挂载的评论_Linux公社-Linux系统门户网站
你好,游客
你好,想请教下关于在树莓派命令行模式下自动挂载U盘的实现问题,我按你的方法并不能实现自动挂载,你是如何实现的呢?
liuruigege
真的可以实现吗?我按照您的方法怎么就不行呢?
你好,想请教下关于在树莓派命令行模式下自动挂载U盘的实现问题,我按你的方法并不能实现自动挂载,你是如何实现的呢?
(0)真的可以实现吗?我按照您的方法怎么就不行呢?其他回答(1)
在虚拟机里面装吧
园豆:10187
清除回答草稿
&&&您需要以后才能回答,未注册用户请先。}

我要回帖

更多关于 字符哑终端 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信