[linux device driver] Chapter 03:scull_load脚本理解

scull_load脚本如下:

#!/bin/sh
module="scullc"
device="scullc"
mode="664"

# Group: since distributions do it differently, look for wheel or use staff
if grep '^staff:' /etc/group > /dev/null; then
    group="staff"
else
    group="wheel"
fi

# remove stale nodes
rm -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 1

major=`cat /proc/devices | awk "\\$2==\"$module\" {print \\$1}"`

mknod /dev/${device}0 c $major 0
mknod /dev/${device}1 c $major 1
mknod /dev/${device}2 c $major 2
mknod /dev/${device}3 c $major 3
ln -sf ${device}0  /dev/${device}

# give appropriate group/permissions
chgrp $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赋值为scullc
device="scullc"<span style="white-space:pre">		</span>//将device赋值为scullc
mode="664"<span style="white-space:pre">		</span>//配置权限
上面是一些初始化配置,然后是对不同的系统查询用户组并赋值的过程:

# Group: since distributions do it differently, look for wheel or use staff
if grep '^staff:' /etc/group > /dev/null; then //'^staff:'含义是只搜索行首为staff:的行,参见grep语法
    group="staff"
else
    group="wheel"
fi
在这里解释下/etc/group的作用,该文件主要用于存储用户组信息,格式如下:

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

比如:

[root@localhost test6]# cat /etc/group
root:x:0:root,linuxsir
bin:x:1:root,bin,daemon
daemon:x:2:root,bin,daemon
sys:x:3:root,bin

详细的文章可参见这个: /etc/group文件详解

于是group参数被定义为staff或者wheel。

之后是删除老节点:

# remove stale nodes
rm -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就是其主设备号。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值