Linux系统之热插拨事件uevent

简介:

文章最后面我会附上medv.conf配置文档medv.txt,是英文的,部分朋友的英语不是很好,但还是希望大家要硬着头皮去读,不要有畏惧心理,相信你读完了后会给你带来很大的帮助。你要相信外文资料讲得很地道,翻译过来没有那种感觉,以上只是个人看法。


class_device_create

class_device_register
class_device_add
kobject_uevent(&class_dev->kobj, KOBJ_ADD);
kobject_uevent_env(kobj, action, NULL);


// action_string = "add";
action_string = action_to_string(action);


/* 分配保存环境变量的内存 */
/* environment values */
buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);

/* 设置环境变量 */
envp [i++] = scratch;
scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
envp [i++] = scratch;
scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
envp [i++] = scratch;
scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;

/* 调用应用程序: 比如mdev */
/* 启动脚本 echo /sbin/mdev > /proc/sys/kernel/hotplug 
* 设置了uevent_helper为“/sbin/mdev“
*/

argv [0] = uevent_helper;  // = "/sbin/mdev"
argv [1] = (char *)subsystem;
argv [2] = NULL;
call_usermodehelper (argv[0], argv, envp, 0);


分析: busybox mdev.c  


100ask: uevent_helper = /sbin/mdev
envp[0] = HOME=/
envp[1] = PATH=/sbin:/bin:/usr/sbin:/usr/bin
envp[2] = ACTION=add
envp[3] = DEVPATH=/class/sixth_drv/buttons
envp[4] = SUBSYSTEM=sixth_drv
envp[5] = SEQNUM=720
envp[6] = MAJOR=252
envp[7] = MINOR=0


mdev_main
temp = /sys/class/sixth_drv/buttons
make_device(temp, 0);
/* 确定设备文件名,类型,主次设备号 */
device_name = bb_basename(path);  = "buttons"


'c' == > 字符设备节点
根据"/sys/class/sixth_drv/buttons/dev"的内容确定主次设备号

mknod(device_name, mode | type, makedev(major, minor)


我接上U盘,想自动挂载,怎么办?
mdev.conf的格式:
<device regex> <uid>:<gid> <octal permissions> [<@|$|*> <command>]


device regex:正则表达式,表示哪一个设备
uid: owner
gid: 组ID
octal permissions:以八进制表示的属性
@:创建设备节点之后执行命令
$:删除设备节点之前执行命令
*: 创建设备节点之后 和 删除设备节点之前 执行命令
command:要执行的命令


写mdev.conf放在/etc/目下,sh脚本文件放在bin目录下,device regex用正则表达式
写相对方便一点,command可以写在脚本文件中,但是要注意sh脚本文件是可以执行的。


下面是几个例子:
1.
leds 0:0 777
led1 0:0 777
led2 0:0 777
led3 0:0 777
dma  0:0 777
2.
leds?[123]? 0:0 777


/*For your convenience, the shell env var $MDEV is set to the device name.  So if
the device "hdc" was matched, MDEV would be set to "hdc".*/
3.
leds?[123]? 0:0 777 @ echo create /dev/$MDEV > /dev/console


4.
leds?[123]? 0:0 777 * if [ $ACTION = "add" ]; then echo create /dev/$MDEV > /dev/console; else echo remove /dev/$MDEV > /dev/console; fi


5. 
leds?[123]? 0:0 777 * /bin/add_remove_led.sh


把命令写入一个脚本:
add_remove_led.sh


#!/bin/sh
if [ $ACTION = "add" ]; 
then 
echo create /dev/$MDEV > /dev/console; 
else 
echo remove /dev/$MDEV > /dev/console; 
fi


6. U盘自动加载
sda[1-9]+ 0:0 777 * if [ $ACTION = "add" ]; then mount /dev/$MDEV /mnt; else umount /mnt; fi


7.
sda[1-9]+ 0:0 777 * /bin/add_remove_udisk.sh


add_remove_udisk.sh
#!/bin/sh
if [ $ACTION = "add" ]; 
then 
mount /dev/$MDEV /tmp; 
else 
umount /tmp; 

fi



                        mdev.txt


-------------
 MDEV Primer
-------------


For those of us who know how to use mdev, a primer might seem lame.  For
everyone else, mdev is a weird black box that they hear is awesome, but can't
seem to get their head around how it works.  Thus, a primer.


-----------
 Basic Use
-----------


Mdev has two primary uses: initial population and dynamic updates.  Both
require sysfs support in the kernel and have it mounted at /sys.  For dynamic
updates, you also need to have hotplugging enabled in your kernel.


Here's a typical code snippet from the init script:
[0] mount -t proc proc /proc
[1] mount -t sysfs sysfs /sys
[2] echo /sbin/mdev > /proc/sys/kernel/hotplug
[3] mdev -s


Alternatively, without procfs the above becomes:
[1] mount -t sysfs sysfs /sys
[2] sysctl -w kernel.hotplug=/sbin/mdev
[3] mdev -s




Of course, a more "full" setup would entail executing this before the previous
code snippet:
[4] mount -t tmpfs -o size=64k,mode=0755 tmpfs /dev
[5] mkdir /dev/pts
[6] mount -t devpts devpts /dev/pts


The simple explanation here is that [1] you need to have /sys mounted before
executing mdev.  Then you [2] instruct the kernel to execute /sbin/mdev whenever
a device is added or removed so that the device node can be created or
destroyed.  Then you [3] seed /dev with all the device nodes that were created
while the system was booting.


For the "full" setup, you want to [4] make sure /dev is a tmpfs filesystem
(assuming you're running out of flash).  Then you want to [5] create the
/dev/pts mount point and finally [6] mount the devpts filesystem on it.


-------------
 MDEV Config   (/etc/mdev.conf)
-------------


Mdev has an optional config file for controlling ownership/permissions of
device nodes if your system needs something more than the default root/root
660 permissions.


The file has the format:
    <device regex>       <uid>:<gid> <permissions>
 or @<maj[,min1[-min2]]> <uid>:<gid> <permissions>


For example:
    hd[a-z][0-9]* 0:3 660


The config file parsing stops at the first matching line.  If no line is
matched, then the default of 0:0 660 is used.  To set your own default, simply
create your own total match like so:
.* 1:1 777


You can rename/move device nodes by using the next optional field.
<device regex> <uid>:<gid> <permissions> [=path]
So if you want to place the device node into a subdirectory, make sure the path
has a trailing /.  If you want to rename the device node, just place the name.
hda 0:3 660 =drives/
This will move "hda" into the drives/ subdirectory.
hdb 0:3 660 =cdrom
This will rename "hdb" to "cdrom".


Similarly, ">path" renames/moves the device but it also creates
a direct symlink /dev/DEVNAME to the renamed/moved device.


You can also prevent creation of device nodes with the 4th field as "!":
tty[a-z]. 0:0 660 !
pty[a-z]. 0:0 660 !


If you also enable support for executing your own commands, then the file has
the format:
<device regex> <uid>:<gid> <permissions> [=path] [@|$|*<command>]
    or
<device regex> <uid>:<gid> <permissions> [>path] [@|$|*<command>]
    or
<device regex> <uid>:<gid> <permissions> [!] [@|$|*<command>]


For example:
---8<---
# block devices
([hs]d[a-z]) root:disk660>disk/%1/0
([hs]d[a-z])([0-9]+) root:disk660>disk/%1/%2
mmcblk([0-9]+) root:disk660>disk/mmc/%1/0
mmcblk([0-9]+)p([0-9]+) root:disk660>disk/mmc/%1/%2
# network devices
(tun|tap) root:network660>net/%1
---8<---


The special characters have the meaning:
@ Run after creating the device.
$ Run before removing the device.
* Run both after creating and before removing the device.


The command is executed via the system() function (which means you're giving a
command to the shell), so make sure you have a shell installed at /bin/sh.  You
should also keep in mind that the kernel executes hotplug helpers with stdin,
stdout, and stderr connected to /dev/null.


For your convenience, the shell env var $MDEV is set to the device name.  So if
the device "hdc" was matched, MDEV would be set to "hdc".


----------
 FIRMWARE
----------


Some kernel device drivers need to request firmware at runtime in order to
properly initialize a device.  Place all such firmware files into the
/lib/firmware/ directory.  At runtime, the kernel will invoke mdev with the
filename of the firmware which mdev will load out of /lib/firmware/ and into
the kernel via the sysfs interface.  The exact filename is hardcoded in the
kernel, so look there if you need to know how to name the file in userspace.


------------
 SEQUENCING
------------


Kernel does not serialize hotplug events. It increments SEQNUM environmental
variable for each successive hotplug invocation. Normally, mdev doesn't care.
This may reorder hotplug and hot-unplug events, with typical symptoms of
device nodes sometimes not created as expected.


However, if /dev/mdev.seq file is found, mdev will compare its
contents with SEQNUM. It will retry up to two seconds, waiting for them
to match. If they match exactly (not even trailing '\n' is allowed),
or if two seconds pass, mdev runs as usual, then it rewrites /dev/mdev.seq
with SEQNUM+1.


IOW: this will serialize concurrent mdev invocations.


If you want to activate this feature, execute "echo >/dev/mdev.seq" prior to
setting mdev to be the hotplug handler. This writes single '\n' to the file.
NB: mdev recognizes /dev/mdev.seq consisting of single '\n' character
as a special case. IOW: this will not make your first hotplug event
to stall for two seconds.

目录
相关文章
|
12天前
|
Linux
在 Linux 系统中,“cd”命令用于切换当前工作目录
在 Linux 系统中,“cd”命令用于切换当前工作目录。本文详细介绍了“cd”命令的基本用法和常见技巧,包括使用“.”、“..”、“~”、绝对路径和相对路径,以及快速切换到上一次工作目录等。此外,还探讨了高级技巧,如使用通配符、结合其他命令、在脚本中使用,以及实际应用案例,帮助读者提高工作效率。
49 3
|
12天前
|
监控 安全 Linux
在 Linux 系统中,网络管理是重要任务。本文介绍了常用的网络命令及其适用场景
在 Linux 系统中,网络管理是重要任务。本文介绍了常用的网络命令及其适用场景,包括 ping(测试连通性)、traceroute(跟踪路由路径)、netstat(显示网络连接信息)、nmap(网络扫描)、ifconfig 和 ip(网络接口配置)。掌握这些命令有助于高效诊断和解决网络问题,保障网络稳定运行。
37 2
|
21天前
|
Linux 应用服务中间件 Shell
linux系统服务二!
本文详细介绍了Linux系统的启动流程,包括CentOS 7的具体启动步骤,从BIOS自检到加载内核、启动systemd程序等。同时,文章还对比了CentOS 6和CentOS 7的启动流程,分析了启动过程中的耗时情况。接着,文章讲解了Linux的运行级别及其管理命令,systemd的基本概念、优势及常用命令,并提供了自定义systemd启动文件的示例。最后,文章介绍了单用户模式和救援模式的使用方法,包括如何找回忘记的密码和修复启动故障。
42 5
linux系统服务二!
|
6天前
|
Ubuntu Linux 网络安全
linux系统ubuntu中在命令行中打开图形界面的文件夹
在Ubuntu系统中,通过命令行打开图形界面的文件夹是一个高效且实用的操作。无论是使用Nautilus、Dolphin还是Thunar,都可以根据具体桌面环境选择合适的文件管理器。通过上述命令和方法,可以简化日常工作,提高效率。同时,解决权限问题和图形界面问题也能确保操作的顺利进行。掌握这些技巧,可以使Linux操作更加便捷和灵活。
14 3
|
12天前
|
安全 网络协议 Linux
本文详细介绍了 Linux 系统中 ping 命令的使用方法和技巧,涵盖基本用法、高级用法、实际应用案例及注意事项。
本文详细介绍了 Linux 系统中 ping 命令的使用方法和技巧,涵盖基本用法、高级用法、实际应用案例及注意事项。通过掌握 ping 命令,读者可以轻松测试网络连通性、诊断网络问题并提升网络管理能力。
44 3
|
15天前
|
安全 Linux 数据安全/隐私保护
在 Linux 系统中,查找文件所有者是系统管理和安全审计的重要技能。
在 Linux 系统中,查找文件所有者是系统管理和安全审计的重要技能。本文介绍了使用 `ls -l` 和 `stat` 命令查找文件所有者的基本方法,以及通过文件路径、通配符和结合其他命令的高级技巧。还提供了实际案例分析和注意事项,帮助读者更好地掌握这一操作。
34 6
|
15天前
|
Linux
在 Linux 系统中,`find` 命令是一个强大的文件查找工具
在 Linux 系统中,`find` 命令是一个强大的文件查找工具。本文详细介绍了 `find` 命令的基本语法、常用选项和具体应用示例,帮助用户快速掌握如何根据文件名、类型、大小、修改时间等条件查找文件,并展示了如何结合逻辑运算符、正则表达式和排除特定目录等高级用法。
49 6
|
16天前
|
机器学习/深度学习 自然语言处理 Linux
Linux 中的机器学习:Whisper——自动语音识别系统
本文介绍了先进的自动语音识别系统 Whisper 在 Linux 环境中的应用。Whisper 基于深度学习和神经网络技术,支持多语言识别,具有高准确性和实时处理能力。文章详细讲解了在 Linux 中安装、配置和使用 Whisper 的步骤,以及其在语音助手、语音识别软件等领域的应用场景。
47 5
|
16天前
|
缓存 运维 监控
【运维必备知识】Linux系统平均负载与top、uptime命令详解
系统平均负载是衡量Linux服务器性能的关键指标之一。通过使用 `top`和 `uptime`命令,可以实时监控系统的负载情况,帮助运维人员及时发现并解决潜在问题。理解这些工具的输出和意义是确保系统稳定运行的基础。希望本文对Linux系统平均负载及相关命令的详细解析能帮助您更好地进行系统运维和性能优化。
39 3
|
16天前
|
监控 网络协议 算法
Linux内核优化:提升系统性能与稳定性的策略####
本文深入探讨了Linux操作系统内核的优化策略,旨在通过一系列技术手段和最佳实践,显著提升系统的性能、响应速度及稳定性。文章首先概述了Linux内核的核心组件及其在系统中的作用,随后详细阐述了内存管理、进程调度、文件系统优化、网络栈调整及并发控制等关键领域的优化方法。通过实际案例分析,展示了这些优化措施如何有效减少延迟、提高吞吐量,并增强系统的整体健壮性。最终,文章强调了持续监控、定期更新及合理配置对于维持Linux系统长期高效运行的重要性。 ####
下一篇
无影云桌面