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就是其主设备号。