linux驱动学习笔记(2.4) scull 脚本scull_init

向自己道歉,没能抽出更多的时间,进度如此的慢。

现在想认真学习下scull模块的这个初始化脚本 scull_init.sh

 

#!/bin/bash
# Sample init script for the a driver module <rubini@linux.it>

DEVICE="scull"
SECTION="misc"

# The list of filenames and minor numbers: $PREFIX is prefixed to all names
PREFIX="scull"
FILES="     0 0         1 1         2 2        3 3    priv 16 
        pipe0 32    pipe1 33    pipe2 34   pipe3 35
       single 48      uid 64     wuid 80"

INSMOD=/sbin/insmod; # use /sbin/modprobe if you prefer

function device_specific_post_load () {
    true; # fill at will
}
function device_specific_pre_unload () {
    true; # fill at will
}

# Everything below this line should work unchanged for any char device.
# Obviously, however, no options on the command line: either in
# /etc/${DEVICE}.conf or /etc/modules.conf (if modprobe is used)

# Optional configuration file: format is
#    owner  <ownername>
#    group  <groupname>
#    mode   <modename>
#    options <insmod options>
CFG=/etc/${DEVICE}.conf

# kernel version, used to look for modules
KERNEL=`uname -r`

#FIXME: it looks like there is no misc section. Where should it be?
MODDIR="/lib/modules/${KERNEL}/kernel/drivers/${SECTION}"
if [ ! -d $MODDIR ]; then MODDIR="/lib/modules/${KERNEL}/${SECTION}"; fi
#专有命令[ (左中括号, 特殊字符). 这个命令与test命令等价, 并且出于效率上的考虑, 这是一个内建命令
# -d FILE  FILE exists and is a directory

# Root or die
if [ "$(id -u)" != "0" ]
then
  echo "You must be root to load or unload kernel modules"
  exit 1
fi

# Read configuration file
if [ -r $CFG ]; then
# -r FILE   FILE exists and read permission is granted
#看到这儿了,
    OWNER=`awk "\\$1==\"owner\" {print \\$2}" $CFG`
#读取文件$CFG 若第一个域为owner,则打印第二个域,并赋值到变量OWNER
#但是这条指令在shell里未能正常运行,改为如下:类似的样子
# awk '$1=="DEVICE" {print $2}' scull.init
    GROUP=`awk "\\$1==\"group\" {print \\$2}" $CFG`
    MODE=`awk "\\$1==\"mode\" {print \\$2}" $CFG`
    # The options string may include extra blanks or only blanks
    OPTIONS=`sed -n '/^options / s/options //p' $CFG`
#打印包含options的行
fi


# Create device files
function create_files () {
    cd /dev
    local devlist=""
    local file
#如果变量用local来声明,那么它只能在该变量声明的代码块(block of code)中可见
    while true; do
	if [ $# -lt 2 ]; then break; fi
#$# Number of command-line arguments  or positional parameters   -lt  小于
	file="${DEVICE}$1"
	mknod $file c $MAJOR $2
	devlist="$devlist $file"
	shift 2
#The shift command reassigns the positional parameters, in effect shifting them to the left one notch
    done
    if [ -n "$OWNER" ]; then chown $OWNER $devlist; fi
    if [ -n "$GROUP" ]; then chgrp $GROUP $devlist; fi
    if [ -n "$MODE"  ]; then chmod $MODE  $devlist; fi
#  -n STRING the length of STRING is nonzero
}

# Remove device files
function remove_files () {
    cd /dev
    local devlist=""
    local file
    while true; do
	if [ $# -lt 2 ]; then break; fi
	file="${DEVICE}$1"
	devlist="$devlist $file"
	shift 2
    done
    rm -f $devlist
}

# Load and create files
function load_device () {
    
    if [ -f $MODDIR/$DEVICE.o ]; then
#-f  FILE exists and is a regular file
	devpath=$MODDIR/$DEVICE.o
    else if [ -f ./$DEVICE.o ]; then
	devpath=./$DEVICE.o
    else
	devpath=$DEVICE; # let insmod/modprobe guess
    fi; fi
    if [ "$devpath" != "$DEVICE" ]; then
	echo -n " (loading file $devpath)"
    fi

    if $INSMOD $devpath $OPTIONS; then
	MAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`
	remove_files $FILES
	create_files $FILES
	device_specific_post_load
    else
	echo " FAILED!"
     fi
}

# Unload and remove files
function unload_device () {
    device_specific_pre_unload 
    /sbin/rmmod $DEVICE
    remove_files $FILES
}


case "$1" in
  start)
     echo -n "Loading $DEVICE"
     load_device
     echo "."
     ;;
  stop)
     echo -n "Unloading $DEVICE"
     unload_device
     echo "."
     ;;
  force-reload|restart)
     echo -n "Reloading $DEVICE"
     unload_device
     load_device
     echo "."
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|force-reload}"
     exit 1
esac

exit 0

转载于:https://www.cnblogs.com/fly-fish/archive/2011/08/25/2153886.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值