linux字符设备模块加载脚本

设备号动态分配的字符设备模块加载及设备节点创建脚本:(摘自LDD3)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
# $Id: scull_load,v 1.4 2004/11/03 06:19:49 rubini Exp $
module="scull"
device="scull"
mode="664"
# Group: since distributions do it differently, look for wheel or use staff
if grep -q '^staff:' /etc/group; then
group="staff"
else
group="wheel"
fi
# invoke insmod with all arguments we got
# and use a pathname, as insmod doesn't look in . by default
/sbin/insmod ./$module.ko $* || exit 1
# retrieve major number
major=$(awk "\$2==\"$module\" {print \$1}" /proc/devices)
# Remove stale nodes and replace them, then give gid and perms
# Usually the script is shorter, it's scull that has several devices in it.
rm -f /dev/${device}[0-3]
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}
chgrp $group /dev/${device}[0-3]
chmod $mode /dev/${device}[0-3]
rm -f /dev/${device}pipe[0-3]
mknod /dev/${device}pipe0 c $major 4
mknod /dev/${device}pipe1 c $major 5
mknod /dev/${device}pipe2 c $major 6
mknod /dev/${device}pipe3 c $major 7
ln -sf ${device}pipe0 /dev/${device}pipe
chgrp $group /dev/${device}pipe[0-3]
chmod $mode /dev/${device}pipe[0-3]
rm -f /dev/${device}single
mknod /dev/${device}single c $major 8
chgrp $group /dev/${device}single
chmod $mode /dev/${device}single
rm -f /dev/${device}uid
mknod /dev/${device}uid c $major 9
chgrp $group /dev/${device}uid
chmod $mode /dev/${device}uid
rm -f /dev/${device}wuid
mknod /dev/${device}wuid c $major 10
chgrp $group /dev/${device}wuid
chmod $mode /dev/${device}wuid
rm -f /dev/${device}priv
mknod /dev/${device}priv c $major 11
chgrp $group /dev/${device}priv
chmod $mode /dev/${device}priv

获取动态分配到的主设备号:

1
2
# retrieve major number
major=$(awk "\$2==\"$module\" {print \$1}" /proc/devices)