linux驱动学习笔记(2.4) scull 脚本scull_init
生活随笔
收集整理的這篇文章主要介紹了
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 35single 48 uid 64 wuid 80"INSMOD=/sbin/insmod; # use /sbin/modprobe if you preferfunction 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" ] thenecho "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 blanksOPTIONS=`sed -n '/^options / s/options //p' $CFG` #打印包含options的行 fi# Create device files function create_files () {cd /devlocal devlist="" local file #如果變量用local來聲明,那么它只能在該變量聲明的代碼塊(block of code)中可見 while true; doif [ $# -lt 2 ]; then break; fi #$# Number of command-line arguments or positional parameters -lt 小于 file="${DEVICE}$1"mknod $file c $MAJOR $2devlist="$devlist $file"shift 2 #The shift command reassigns the positional parameters, in effect shifting them to the left one notch doneif [ -n "$OWNER" ]; then chown $OWNER $devlist; fiif [ -n "$GROUP" ]; then chgrp $GROUP $devlist; fiif [ -n "$MODE" ]; then chmod $MODE $devlist; fi #? -n STRING the length of STRING is nonzero }# Remove device files function remove_files () {cd /devlocal devlist=""local filewhile true; doif [ $# -lt 2 ]; then break; fifile="${DEVICE}$1"devlist="$devlist $file"shift 2donerm -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.oelse if [ -f ./$DEVICE.o ]; thendevpath=./$DEVICE.oelsedevpath=$DEVICE; # let insmod/modprobe guessfi; fiif [ "$devpath" != "$DEVICE" ]; thenecho -n " (loading file $devpath)"fiif $INSMOD $devpath $OPTIONS; thenMAJOR=`awk "\\$2==\"$DEVICE\" {print \\$1}" /proc/devices`remove_files $FILEScreate_files $FILESdevice_specific_post_loadelseecho " FAILED!"fi }# Unload and remove files function unload_device () {device_specific_pre_unload /sbin/rmmod $DEVICEremove_files $FILES }case "$1" instart)echo -n "Loading $DEVICE"load_deviceecho ".";;stop)echo -n "Unloading $DEVICE"unload_deviceecho ".";;force-reload|restart)echo -n "Reloading $DEVICE"unload_deviceload_deviceecho ".";;*)echo "Usage: $0 {start|stop|restart|force-reload}"exit 1 esacexit 0轉載于:https://www.cnblogs.com/fly-fish/archive/2011/08/25/2153886.html
總結
以上是生活随笔為你收集整理的linux驱动学习笔记(2.4) scull 脚本scull_init的全部內容,希望文章能夠幫你解決所遇到的問題。