[linux device driver] Chapter 03:scull

scull_load脚本如下:

#!/bin/shmodule="scullc"device="scullc"mode="664"# Group: since distributions do it differently, look for wheel or use staffif grep '^staff:' /etc/group > /dev/null; thengroup="staff"elsegroup="wheel"fi# remove stale nodesrm -f /dev/${device}? # invoke insmod with all arguments we got# and use a pathname, as newer modutils don't look in . by default/sbin/insmod -f ./$module.ko $* || exit 1major=`cat /proc/devices | awk "\\$2==\&;$module\&; {print \\$1}"`mknod /dev/${device}0 c $major 0mknod /dev/${device}1 c $major 1mknod /dev/${device}2 c $major 2mknod /dev/${device}3 c $major 3ln -sf ${device}0 /dev/${device}# give appropriate group/permissionschgrp $group /dev/${device}[0-3]chmod $mode /dev/${device}[0-3]针对上面的bash语句,逐步加以理解:#!/bin/sh<span style="white-space:pre"></span>//表示该文件是bash脚本,调用/bin/sh处理module="scullc"<span style="white-space:pre"></span>//将module赋值为scullcdevice="scullc"<span style="white-space:pre"></span>//将device赋值为scullcmode="664"<span style="white-space:pre"></span>//配置权限上面是一些初始化配置,然后是对不同的系统查询用户组并赋值的过程:# Group: since distributions do it differently, look for wheel or use staffif grep '^staff:' /etc/group > /dev/null; then //'^staff:'含义是只搜索行首为staff:的行,参见grep语法group="staff"elsegroup="wheel"fi在这里解释下/etc/group的作用,该文件主要用于存储用户组信息,,格式如下:

组名:口令:组标识号:组内用户列表

比如:

[root@localhost test6]# cat /etc/grouproot:x:0:root,linuxsirbin:x:1:root,bin,daemondaemon:x:2:root,bin,daemonsys:x:3:root,bin详细的文章可参见这个:/etc/group文件详解

于是group参数被定义为staff或者wheel。之后是删除老节点:

# remove stale nodesrm -f /dev/${device}? //删除/dev/scullc*

安装模块:

# invoke insmod with all arguments we got# and use a pathname, as newer modutils don't look in . by default/sbin/insmod -f ./$module.ko $* || exit 1含义是安装scullc.ko模块(并将传入的命令附着在module.ko后面),如果失败的话就退出该脚本。

将输出结果赋给major:

major=`cat /proc/devices | awk "\\$2==\&;$module\&; {print \\$1}"`上面的例子是从/proc/devices中查找scullc模块,并得到主设备号,major就是其主设备号。

得意时应善待他人,因为你失意时会需要他们

[linux device driver] Chapter 03:scull

相关文章:

你感兴趣的文章:

标签云: